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.
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.
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]
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.
27 #include <sys/zfs_context.h>
28 #include <sys/fm/fs/zfs.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>
39 #include <sys/blkptr.h>
40 #include <sys/zfeature.h>
43 * ==========================================================================
44 * I/O type descriptions
45 * ==========================================================================
47 const char *zio_type_name
[ZIO_TYPES
] = {
48 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl"
52 * ==========================================================================
54 * ==========================================================================
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
;
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.
73 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
74 * regular blocks are not deferred.
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 */
81 * An allocating zio is one that either currently has the DVA allocate
82 * stage set or will have it later in its lifetime.
84 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
86 int zio_requeue_io_start_cut_in_line
= 1;
89 int zio_buf_debug_limit
= 16384;
91 int zio_buf_debug_limit
= 0;
94 static inline void __zio_execute(zio_t
*zio
);
97 zio_cons(void *arg
, void *unused
, int kmflag
)
101 bzero(zio
, sizeof (zio_t
));
103 mutex_init(&zio
->io_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
104 cv_init(&zio
->io_cv
, NULL
, CV_DEFAULT
, NULL
);
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
));
115 zio_dest(void *arg
, void *unused
)
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
);
129 vmem_t
*data_alloc_arena
= NULL
;
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);
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.
144 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
145 size_t size
= (c
+ 1) << SPA_MINBLOCKSHIFT
;
149 while (p2
& (p2
- 1))
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.
159 if (arc_watch
&& !IS_P2ALIGNED(size
, PAGESIZE
))
162 if (size
<= 4 * SPA_MINBLOCKSIZE
) {
163 align
= SPA_MINBLOCKSIZE
;
164 } else if (IS_P2ALIGNED(size
, PAGESIZE
)) {
166 } else if (IS_P2ALIGNED(size
, p2
>> 2)) {
172 int flags
= zio_bulk_flags
;
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
);
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
);
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
];
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
];
204 kmem_cache_t
*last_cache
= NULL
;
205 kmem_cache_t
*last_data_cache
= NULL
;
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
]);
212 zio_buf_cache
[c
] = NULL
;
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
]);
218 zio_data_buf_cache
[c
] = NULL
;
221 kmem_cache_destroy(zio_vdev_cache
);
222 kmem_cache_destroy(zio_link_cache
);
223 kmem_cache_destroy(zio_cache
);
231 * ==========================================================================
232 * Allocate and free I/O buffers
233 * ==========================================================================
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.
243 zio_buf_alloc(size_t size
)
245 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
247 ASSERT3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
249 return (kmem_cache_alloc(zio_buf_cache
[c
], KM_PUSHPAGE
));
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)
259 zio_data_buf_alloc(size_t size
)
261 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
263 ASSERT(c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
265 return (kmem_cache_alloc(zio_data_buf_cache
[c
], KM_PUSHPAGE
));
269 zio_buf_free(void *buf
, size_t size
)
271 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
273 ASSERT(c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
275 kmem_cache_free(zio_buf_cache
[c
], buf
);
279 zio_data_buf_free(void *buf
, size_t size
)
281 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
283 ASSERT(c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
285 kmem_cache_free(zio_data_buf_cache
[c
], buf
);
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.
296 return (kmem_cache_alloc(zio_vdev_cache
, KM_PUSHPAGE
));
300 zio_vdev_free(void *buf
)
302 kmem_cache_free(zio_vdev_cache
, buf
);
307 * ==========================================================================
308 * Push and pop I/O transform buffers
309 * ==========================================================================
312 zio_push_transform(zio_t
*zio
, void *data
, uint64_t size
, uint64_t bufsize
,
313 zio_transform_func_t
*transform
)
315 zio_transform_t
*zt
= kmem_alloc(sizeof (zio_transform_t
), KM_SLEEP
);
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
;
322 zt
->zt_next
= zio
->io_transform_stack
;
323 zio
->io_transform_stack
= zt
;
330 zio_pop_transforms(zio_t
*zio
)
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
);
339 if (zt
->zt_bufsize
!= 0)
340 zio_buf_free(zio
->io_data
, zt
->zt_bufsize
);
342 zio
->io_data
= zt
->zt_orig_data
;
343 zio
->io_size
= zt
->zt_orig_size
;
344 zio
->io_transform_stack
= zt
->zt_next
;
346 kmem_free(zt
, sizeof (zio_transform_t
));
351 * ==========================================================================
352 * I/O transform callbacks for subblocks and decompression
353 * ==========================================================================
356 zio_subblock(zio_t
*zio
, void *data
, uint64_t size
)
358 ASSERT(zio
->io_size
> size
);
360 if (zio
->io_type
== ZIO_TYPE_READ
)
361 bcopy(zio
->io_data
, data
, size
);
365 zio_decompress(zio_t
*zio
, void *data
, uint64_t size
)
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
);
374 * ==========================================================================
375 * I/O parent/child relationships and pipeline interlocks
376 * ==========================================================================
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.)
387 zio_walk_parents(zio_t
*cio
)
389 zio_link_t
*zl
= cio
->io_walk_link
;
390 list_t
*pl
= &cio
->io_parent_list
;
392 zl
= (zl
== NULL
) ? list_head(pl
) : list_next(pl
, zl
);
393 cio
->io_walk_link
= zl
;
398 ASSERT(zl
->zl_child
== cio
);
399 return (zl
->zl_parent
);
403 zio_walk_children(zio_t
*pio
)
405 zio_link_t
*zl
= pio
->io_walk_link
;
406 list_t
*cl
= &pio
->io_child_list
;
408 zl
= (zl
== NULL
) ? list_head(cl
) : list_next(cl
, zl
);
409 pio
->io_walk_link
= zl
;
414 ASSERT(zl
->zl_parent
== pio
);
415 return (zl
->zl_child
);
419 zio_unique_parent(zio_t
*cio
)
421 zio_t
*pio
= zio_walk_parents(cio
);
423 VERIFY(zio_walk_parents(cio
) == NULL
);
428 zio_add_child(zio_t
*pio
, zio_t
*cio
)
430 zio_link_t
*zl
= kmem_cache_alloc(zio_link_cache
, KM_SLEEP
);
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.
439 ASSERT(cio
->io_child_type
<= pio
->io_child_type
);
444 mutex_enter(&cio
->io_lock
);
445 mutex_enter(&pio
->io_lock
);
447 ASSERT(pio
->io_state
[ZIO_WAIT_DONE
] == 0);
449 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
450 pio
->io_children
[cio
->io_child_type
][w
] += !cio
->io_state
[w
];
452 list_insert_head(&pio
->io_child_list
, zl
);
453 list_insert_head(&cio
->io_parent_list
, zl
);
455 pio
->io_child_count
++;
456 cio
->io_parent_count
++;
458 mutex_exit(&pio
->io_lock
);
459 mutex_exit(&cio
->io_lock
);
463 zio_remove_child(zio_t
*pio
, zio_t
*cio
, zio_link_t
*zl
)
465 ASSERT(zl
->zl_parent
== pio
);
466 ASSERT(zl
->zl_child
== cio
);
468 mutex_enter(&cio
->io_lock
);
469 mutex_enter(&pio
->io_lock
);
471 list_remove(&pio
->io_child_list
, zl
);
472 list_remove(&cio
->io_parent_list
, zl
);
474 pio
->io_child_count
--;
475 cio
->io_parent_count
--;
477 mutex_exit(&pio
->io_lock
);
478 mutex_exit(&cio
->io_lock
);
480 kmem_cache_free(zio_link_cache
, zl
);
484 zio_wait_for_children(zio_t
*zio
, enum zio_child child
, enum zio_wait_type wait
)
486 uint64_t *countp
= &zio
->io_children
[child
][wait
];
487 boolean_t waiting
= B_FALSE
;
489 mutex_enter(&zio
->io_lock
);
490 ASSERT(zio
->io_stall
== NULL
);
493 zio
->io_stall
= countp
;
496 mutex_exit(&zio
->io_lock
);
501 __attribute__((always_inline
))
503 zio_notify_parent(zio_t
*pio
, zio_t
*zio
, enum zio_wait_type wait
)
505 uint64_t *countp
= &pio
->io_children
[zio
->io_child_type
][wait
];
506 int *errorp
= &pio
->io_child_error
[zio
->io_child_type
];
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);
516 if (*countp
== 0 && pio
->io_stall
== countp
) {
517 pio
->io_stall
= NULL
;
518 mutex_exit(&pio
->io_lock
);
521 mutex_exit(&pio
->io_lock
);
526 zio_inherit_child_errors(zio_t
*zio
, enum zio_child c
)
528 if (zio
->io_child_error
[c
] != 0 && zio
->io_error
== 0)
529 zio
->io_error
= zio
->io_child_error
[c
];
533 * ==========================================================================
534 * Create the various types of I/O (read, write, free, etc)
535 * ==========================================================================
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
)
546 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
547 ASSERT(P2PHASE(size
, SPA_MINBLOCKSIZE
) == 0);
548 ASSERT(P2PHASE(offset
, SPA_MINBLOCKSIZE
) == 0);
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
);
554 zio
= kmem_cache_alloc(zio_cache
, KM_SLEEP
);
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
;
563 zio
->io_child_type
= ZIO_CHILD_LOGICAL
;
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
;
578 zio
->io_logical
= NULL
;
580 bzero(&zio
->io_bp_copy
, sizeof (blkptr_t
));
581 bzero(&zio
->io_bp_orig
, sizeof (blkptr_t
));
586 zio
->io_ready
= NULL
;
587 zio
->io_physdone
= NULL
;
589 zio
->io_private
= private;
590 zio
->io_prev_space_delta
= 0;
592 zio
->io_priority
= priority
;
595 zio
->io_vsd_ops
= NULL
;
596 zio
->io_offset
= offset
;
597 zio
->io_timestamp
= 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
));
607 zio
->io_reexecute
= 0;
608 zio
->io_bp_override
= NULL
;
609 zio
->io_walk_link
= NULL
;
610 zio
->io_transform_stack
= NULL
;
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
;
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
));
627 zio
->io_state
[ZIO_WAIT_READY
] = (stage
>= ZIO_STAGE_READY
);
628 zio
->io_state
[ZIO_WAIT_DONE
] = (stage
>= ZIO_STAGE_DONE
);
631 zio
->io_bookmark
= *zb
;
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
);
641 taskq_init_ent(&zio
->io_tqent
);
647 zio_destroy(zio_t
*zio
)
649 kmem_cache_free(zio_cache
, zio
);
653 zio_null(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, zio_done_func_t
*done
,
654 void *private, enum zio_flag flags
)
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
);
666 zio_root(spa_t
*spa
, zio_done_func_t
*done
, void *private, enum zio_flag flags
)
668 return (zio_null(NULL
, spa
, NULL
, done
, private, flags
));
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
)
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
);
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
,
692 zio_priority_t priority
, enum zio_flag flags
, const zbookmark_phys_t
*zb
)
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
) &&
703 zp
->zp_copies
<= spa_max_replication(spa
));
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
);
710 zio
->io_ready
= ready
;
711 zio
->io_physdone
= physdone
;
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).
720 if (data
== NULL
&& zio
->io_prop
.zp_dedup_verify
) {
721 zio
->io_prop
.zp_dedup
= zio
->io_prop
.zp_dedup_verify
= B_FALSE
;
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
)
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
);
742 zio_write_override(zio_t
*zio
, blkptr_t
*bp
, int copies
, boolean_t nopwrite
)
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
));
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.
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
;
761 zio_free(spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
)
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().
769 if (BP_IS_EMBEDDED(bp
))
771 metaslab_check_free(spa
, bp
);
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.
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
);
784 VERIFY0(zio_wait(zio_free_sync(NULL
, spa
, txg
, bp
, 0)));
789 zio_free_sync(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
793 enum zio_stage stage
= ZIO_FREE_PIPELINE
;
795 ASSERT(!BP_IS_HOLE(bp
));
796 ASSERT(spa_syncing_txg(spa
) == txg
);
797 ASSERT(spa_sync_pass(spa
) < zfs_sync_pass_deferred_free
);
799 if (BP_IS_EMBEDDED(bp
))
800 return (zio_null(pio
, spa
, NULL
, NULL
, NULL
, 0));
802 metaslab_check_free(spa
, bp
);
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
810 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
))
811 stage
|= ZIO_STAGE_ISSUE_ASYNC
;
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
);
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
)
826 dprintf_bp(bp
, "claiming in txg %llu", txg
);
828 if (BP_IS_EMBEDDED(bp
))
829 return (zio_null(pio
, spa
, NULL
, NULL
, NULL
, 0));
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.
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.
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) */
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
);
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
)
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
);
868 zio
= zio_null(pio
, spa
, NULL
, NULL
, NULL
, flags
);
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
));
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
)
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
);
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
);
894 zio
->io_prop
.zp_checksum
= checksum
;
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
)
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
);
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
);
915 zio
->io_prop
.zp_checksum
= checksum
;
917 if (zio_checksum_table
[checksum
].ci_eck
) {
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.
924 void *wbuf
= zio_buf_alloc(size
);
925 bcopy(data
, wbuf
, size
);
926 zio_push_transform(zio
, wbuf
, size
, size
, NULL
);
933 * Create a child I/O to do some work for us.
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)
940 enum zio_stage pipeline
= ZIO_VDEV_CHILD_PIPELINE
;
943 ASSERT(vd
->vdev_parent
==
944 (pio
->io_vd
? pio
->io_vd
: pio
->io_spa
->spa_root_vdev
));
946 if (type
== ZIO_TYPE_READ
&& bp
!= NULL
) {
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.
953 pipeline
|= ZIO_STAGE_CHECKSUM_VERIFY
;
954 pio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
957 if (vd
->vdev_children
== 0)
958 offset
+= VDEV_LABEL_START_SIZE
;
960 flags
|= ZIO_VDEV_CHILD_FLAGS(pio
) | ZIO_FLAG_DONT_PROPAGATE
;
963 * If we've decided to do a repair, the write is not speculative --
964 * even if the original read was.
966 if (flags
& ZIO_FLAG_IO_REPAIR
)
967 flags
&= ~ZIO_FLAG_SPECULATIVE
;
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
);
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
++;
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)
987 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
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
,
993 ZIO_STAGE_VDEV_IO_START
>> 1, ZIO_VDEV_CHILD_PIPELINE
);
999 zio_flush(zio_t
*zio
, vdev_t
*vd
)
1001 zio_nowait(zio_ioctl(zio
, zio
->io_spa
, vd
, DKIOCFLUSHWRITECACHE
,
1003 ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
));
1007 zio_shrink(zio_t
*zio
, uint64_t size
)
1009 ASSERT(zio
->io_executor
== NULL
);
1010 ASSERT(zio
->io_orig_size
== zio
->io_size
);
1011 ASSERT(size
<= zio
->io_size
);
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.
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
;
1024 * ==========================================================================
1025 * Prepare to read and write logical blocks
1026 * ==========================================================================
1030 zio_read_bp_init(zio_t
*zio
)
1032 blkptr_t
*bp
= zio
->io_bp
;
1034 if (BP_GET_COMPRESS(bp
) != ZIO_COMPRESS_OFF
&&
1035 zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1036 !(zio
->io_flags
& ZIO_FLAG_RAW
)) {
1038 BP_IS_EMBEDDED(bp
) ? BPE_GET_PSIZE(bp
) : BP_GET_PSIZE(bp
);
1039 void *cbuf
= zio_buf_alloc(psize
);
1041 zio_push_transform(zio
, cbuf
, psize
, psize
, zio_decompress
);
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
);
1048 ASSERT(!BP_IS_EMBEDDED(bp
));
1051 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) && BP_GET_LEVEL(bp
) == 0)
1052 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1054 if (BP_GET_TYPE(bp
) == DMU_OT_DDT_ZAP
)
1055 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1057 if (BP_GET_DEDUP(bp
) && zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
1058 zio
->io_pipeline
= ZIO_DDT_READ_PIPELINE
;
1060 return (ZIO_PIPELINE_CONTINUE
);
1064 zio_write_bp_init(zio_t
*zio
)
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
;
1075 * If our children haven't all reached the ready stage,
1076 * wait for them and then repeat this pipeline stage.
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
);
1082 if (!IO_IS_ALLOCATING(zio
))
1083 return (ZIO_PIPELINE_CONTINUE
);
1085 ASSERT(zio
->io_child_type
!= ZIO_CHILD_DDT
);
1087 if (zio
->io_bp_override
) {
1088 ASSERT(bp
->blk_birth
!= zio
->io_txg
);
1089 ASSERT(BP_GET_DEDUP(zio
->io_bp_override
) == 0);
1091 *bp
= *zio
->io_bp_override
;
1092 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1094 if (BP_IS_EMBEDDED(bp
))
1095 return (ZIO_PIPELINE_CONTINUE
);
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.
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
);
1108 ASSERT(!zp
->zp_nopwrite
);
1110 if (BP_IS_HOLE(bp
) || !zp
->zp_dedup
)
1111 return (ZIO_PIPELINE_CONTINUE
);
1113 ASSERT(zio_checksum_table
[zp
->zp_checksum
].ci_dedup
||
1114 zp
->zp_dedup_verify
);
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
);
1121 zio
->io_bp_override
= NULL
;
1125 if (!BP_IS_HOLE(bp
) && bp
->blk_birth
== zio
->io_txg
) {
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.
1135 pass
= spa_sync_pass(spa
);
1137 ASSERT(zio
->io_txg
== spa_syncing_txg(spa
));
1138 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1139 ASSERT(!BP_GET_DEDUP(bp
));
1141 if (pass
>= zfs_sync_pass_dont_compress
)
1142 compress
= ZIO_COMPRESS_OFF
;
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
));
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
);
1171 * Round up compressed size to MINBLOCKSIZE and
1175 P2ROUNDUP(psize
, (size_t)SPA_MINBLOCKSIZE
);
1176 if (rounded
> psize
) {
1177 bzero((char *)cbuf
+ psize
, rounded
- psize
);
1180 if (psize
== lsize
) {
1181 compress
= ZIO_COMPRESS_OFF
;
1182 zio_buf_free(cbuf
, lsize
);
1184 zio_push_transform(zio
, cbuf
,
1185 psize
, lsize
, NULL
);
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.
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
;
1203 zio
->io_pipeline
= ZIO_REWRITE_PIPELINE
| gang_stages
;
1204 zio
->io_flags
|= ZIO_FLAG_IO_REWRITE
;
1207 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
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);
1218 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
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
);
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
;
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
;
1241 return (ZIO_PIPELINE_CONTINUE
);
1245 zio_free_bp_init(zio_t
*zio
)
1247 blkptr_t
*bp
= zio
->io_bp
;
1249 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
) {
1250 if (BP_GET_DEDUP(bp
))
1251 zio
->io_pipeline
= ZIO_DDT_FREE_PIPELINE
;
1254 return (ZIO_PIPELINE_CONTINUE
);
1258 * ==========================================================================
1259 * Execute the I/O pipeline
1260 * ==========================================================================
1264 zio_taskq_dispatch(zio_t
*zio
, zio_taskq_type_t q
, boolean_t cutinline
)
1266 spa_t
*spa
= zio
->io_spa
;
1267 zio_type_t t
= zio
->io_type
;
1268 int flags
= (cutinline
? TQ_FRONT
: 0);
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.
1275 if (zio
->io_flags
& (ZIO_FLAG_CONFIG_WRITER
| ZIO_FLAG_PROBE
))
1279 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1281 if (t
== ZIO_TYPE_WRITE
&& zio
->io_vd
&& zio
->io_vd
->vdev_aux
)
1285 * If this is a high priority I/O, then use the high priority taskq if
1288 if (zio
->io_priority
== ZIO_PRIORITY_NOW
&&
1289 spa
->spa_zio_taskq
[t
][q
+ 1].stqs_count
!= 0)
1292 ASSERT3U(q
, <, ZIO_TASKQ_TYPES
);
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.
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
);
1305 zio_taskq_member(zio_t
*zio
, zio_taskq_type_t q
)
1307 kthread_t
*executor
= zio
->io_executor
;
1308 spa_t
*spa
= zio
->io_spa
;
1311 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1312 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1314 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
1315 if (taskq_member(tqs
->stqs_taskq
[i
], executor
))
1324 zio_issue_async(zio_t
*zio
)
1326 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
1328 return (ZIO_PIPELINE_STOP
);
1332 zio_interrupt(zio_t
*zio
)
1334 zio_taskq_dispatch(zio
, ZIO_TASKQ_INTERRUPT
, B_FALSE
);
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().
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.
1350 static zio_pipe_stage_t
*zio_pipeline
[];
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.
1361 zio_execute(zio_t
*zio
)
1363 fstrans_cookie_t cookie
;
1365 cookie
= spl_fstrans_mark();
1367 spl_fstrans_unmark(cookie
);
1370 __attribute__((always_inline
))
1372 __zio_execute(zio_t
*zio
)
1374 zio
->io_executor
= curthread
;
1376 while (zio
->io_stage
< ZIO_STAGE_DONE
) {
1377 enum zio_stage pipeline
= zio
->io_pipeline
;
1378 enum zio_stage stage
= zio
->io_stage
;
1383 ASSERT(!MUTEX_HELD(&zio
->io_lock
));
1384 ASSERT(ISP2(stage
));
1385 ASSERT(zio
->io_stall
== NULL
);
1389 } while ((stage
& pipeline
) == 0);
1391 ASSERT(stage
<= ZIO_STAGE_DONE
);
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
;
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.
1403 * For VDEV_IO_START, we cut in line so that the io will
1404 * be sent to disk promptly.
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
);
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.
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
);
1427 zio
->io_stage
= stage
;
1428 rv
= zio_pipeline
[highbit64(stage
) - 1](zio
);
1430 if (rv
== ZIO_PIPELINE_STOP
)
1433 ASSERT(rv
== ZIO_PIPELINE_CONTINUE
);
1439 * ==========================================================================
1440 * Initiate I/O, either sync or async
1441 * ==========================================================================
1444 zio_wait(zio_t
*zio
)
1448 ASSERT(zio
->io_stage
== ZIO_STAGE_OPEN
);
1449 ASSERT(zio
->io_executor
== NULL
);
1451 zio
->io_waiter
= curthread
;
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
);
1460 error
= zio
->io_error
;
1467 zio_nowait(zio_t
*zio
)
1469 ASSERT(zio
->io_executor
== NULL
);
1471 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1472 zio_unique_parent(zio
) == NULL
) {
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.
1480 spa_t
*spa
= zio
->io_spa
;
1482 pio
= spa
->spa_async_zio_root
[CPU_SEQID
];
1485 zio_add_child(pio
, zio
);
1492 * ==========================================================================
1493 * Reexecute or suspend/resume failed I/O
1494 * ==========================================================================
1498 zio_reexecute(zio_t
*pio
)
1500 zio_t
*cio
, *cio_next
;
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
);
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
;
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;
1519 if (IO_IS_ALLOCATING(pio
))
1520 BP_ZERO(pio
->io_bp
);
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'.
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
);
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.
1543 if (!(pio
->io_flags
& ZIO_FLAG_GODFATHER
))
1548 zio_suspend(spa_t
*spa
, zio_t
*zio
)
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
));
1555 cmn_err(CE_WARN
, "Pool '%s' has encountered an uncorrectable I/O "
1556 "failure and has been suspended.\n", spa_name(spa
));
1558 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE
, spa
, NULL
, NULL
, 0, 0);
1560 mutex_enter(&spa
->spa_suspend_lock
);
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
);
1567 spa
->spa_suspended
= B_TRUE
;
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
);
1578 mutex_exit(&spa
->spa_suspend_lock
);
1582 zio_resume(spa_t
*spa
)
1587 * Reexecute all previously suspended i/o.
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
);
1600 return (zio_wait(pio
));
1604 zio_resume_wait(spa_t
*spa
)
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
);
1613 * ==========================================================================
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.
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.
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.
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.
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.
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.
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.
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.
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.
1674 * In all cases, the gang tree allows complete recovery from partial failure.
1675 * ==========================================================================
1679 zio_read_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
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
));
1690 zio_rewrite_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
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
);
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.)
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
));
1712 * If we are here to damage data for testing purposes,
1713 * leave the GBH alone so that we can detect the damage.
1715 if (pio
->io_gang_leader
->io_flags
& ZIO_FLAG_INDUCE_DAMAGE
)
1716 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
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
);
1728 zio_free_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1730 return (zio_free_sync(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1731 ZIO_GANG_CHILD_FLAGS(pio
)));
1736 zio_claim_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1738 return (zio_claim(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1739 NULL
, NULL
, ZIO_GANG_CHILD_FLAGS(pio
)));
1742 static zio_gang_issue_func_t
*zio_gang_issue_func
[ZIO_TYPES
] = {
1751 static void zio_gang_tree_assemble_done(zio_t
*zio
);
1753 static zio_gang_node_t
*
1754 zio_gang_node_alloc(zio_gang_node_t
**gnpp
)
1756 zio_gang_node_t
*gn
;
1758 ASSERT(*gnpp
== NULL
);
1760 gn
= kmem_zalloc(sizeof (*gn
), KM_SLEEP
);
1761 gn
->gn_gbh
= zio_buf_alloc(SPA_GANGBLOCKSIZE
);
1768 zio_gang_node_free(zio_gang_node_t
**gnpp
)
1770 zio_gang_node_t
*gn
= *gnpp
;
1773 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
1774 ASSERT(gn
->gn_child
[g
] == NULL
);
1776 zio_buf_free(gn
->gn_gbh
, SPA_GANGBLOCKSIZE
);
1777 kmem_free(gn
, sizeof (*gn
));
1782 zio_gang_tree_free(zio_gang_node_t
**gnpp
)
1784 zio_gang_node_t
*gn
= *gnpp
;
1790 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
1791 zio_gang_tree_free(&gn
->gn_child
[g
]);
1793 zio_gang_node_free(gnpp
);
1797 zio_gang_tree_assemble(zio_t
*gio
, blkptr_t
*bp
, zio_gang_node_t
**gnpp
)
1799 zio_gang_node_t
*gn
= zio_gang_node_alloc(gnpp
);
1801 ASSERT(gio
->io_gang_leader
== gio
);
1802 ASSERT(BP_IS_GANG(bp
));
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
));
1810 zio_gang_tree_assemble_done(zio_t
*zio
)
1812 zio_t
*gio
= zio
->io_gang_leader
;
1813 zio_gang_node_t
*gn
= zio
->io_private
;
1814 blkptr_t
*bp
= zio
->io_bp
;
1817 ASSERT(gio
== zio_unique_parent(zio
));
1818 ASSERT(zio
->io_child_count
== 0);
1823 if (BP_SHOULD_BYTESWAP(bp
))
1824 byteswap_uint64_array(zio
->io_data
, zio
->io_size
);
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
);
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
))
1834 zio_gang_tree_assemble(gio
, gbp
, &gn
->gn_child
[g
]);
1839 zio_gang_tree_issue(zio_t
*pio
, zio_gang_node_t
*gn
, blkptr_t
*bp
, void *data
)
1841 zio_t
*gio
= pio
->io_gang_leader
;
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
);
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.
1853 zio
= zio_gang_issue_func
[gio
->io_type
](pio
, bp
, gn
, data
);
1856 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
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
))
1862 zio_gang_tree_issue(zio
, gn
->gn_child
[g
], gbp
, data
);
1863 data
= (char *)data
+ BP_GET_PSIZE(gbp
);
1867 if (gn
== gio
->io_gang_tree
)
1868 ASSERT3P((char *)gio
->io_data
+ gio
->io_size
, ==, data
);
1875 zio_gang_assemble(zio_t
*zio
)
1877 blkptr_t
*bp
= zio
->io_bp
;
1879 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== NULL
);
1880 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
1882 zio
->io_gang_leader
= zio
;
1884 zio_gang_tree_assemble(zio
, bp
, &zio
->io_gang_tree
);
1886 return (ZIO_PIPELINE_CONTINUE
);
1890 zio_gang_issue(zio_t
*zio
)
1892 blkptr_t
*bp
= zio
->io_bp
;
1894 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_DONE
))
1895 return (ZIO_PIPELINE_STOP
);
1897 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== zio
);
1898 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
1900 if (zio
->io_child_error
[ZIO_CHILD_GANG
] == 0)
1901 zio_gang_tree_issue(zio
, zio
->io_gang_tree
, bp
, zio
->io_data
);
1903 zio_gang_tree_free(&zio
->io_gang_tree
);
1905 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1907 return (ZIO_PIPELINE_CONTINUE
);
1911 zio_write_gang_member_ready(zio_t
*zio
)
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
;
1918 ASSERTV(zio_t
*gio
= zio
->io_gang_leader
);
1920 if (BP_IS_HOLE(zio
->io_bp
))
1923 ASSERT(BP_IS_HOLE(&zio
->io_bp_orig
));
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
));
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
);
1938 mutex_exit(&pio
->io_lock
);
1942 zio_write_gang_block(zio_t
*pio
)
1944 spa_t
*spa
= pio
->io_spa
;
1945 blkptr_t
*bp
= pio
->io_bp
;
1946 zio_t
*gio
= pio
->io_gang_leader
;
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
;
1953 int copies
= gio
->io_prop
.zp_copies
;
1954 int gbh_copies
= MIN(copies
+ 1, spa_max_replication(spa
));
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
);
1962 pio
->io_error
= error
;
1963 return (ZIO_PIPELINE_CONTINUE
);
1967 gnpp
= &gio
->io_gang_tree
;
1969 gnpp
= pio
->io_private
;
1970 ASSERT(pio
->io_ready
== zio_write_gang_member_ready
);
1973 gn
= zio_gang_node_alloc(gnpp
);
1975 bzero(gbh
, SPA_GANGBLOCKSIZE
);
1978 * Create the gang header.
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
);
1984 * Create and nowait the gang children.
1986 for (g
= 0; resid
!= 0; resid
-= lsize
, g
++) {
1987 lsize
= P2ROUNDUP(resid
/ (SPA_GBH_NBLKPTRS
- g
),
1989 ASSERT(lsize
>= SPA_MINBLOCKSIZE
&& lsize
<= resid
);
1991 zp
.zp_checksum
= gio
->io_prop
.zp_checksum
;
1992 zp
.zp_compress
= ZIO_COMPRESS_OFF
;
1993 zp
.zp_type
= DMU_OT_NONE
;
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
;
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
));
2008 * Set pio's pipeline to just wait for zio to finish.
2010 pio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2013 * We didn't allocate this bp, so make sure it doesn't get unmarked.
2015 pio
->io_flags
&= ~ZIO_FLAG_FASTWRITE
;
2019 return (ZIO_PIPELINE_CONTINUE
);
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.
2031 zio_nop_write(zio_t
*zio
)
2033 blkptr_t
*bp
= zio
->io_bp
;
2034 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2035 zio_prop_t
*zp
= &zio
->io_prop
;
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
));
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.
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
);
2059 * If the checksums match then reset the pipeline so that we
2060 * avoid allocating a new bp and issuing any I/O.
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);
2071 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2072 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
2075 return (ZIO_PIPELINE_CONTINUE
);
2079 * ==========================================================================
2081 * ==========================================================================
2084 zio_ddt_child_read_done(zio_t
*zio
)
2086 blkptr_t
*bp
= zio
->io_bp
;
2087 ddt_entry_t
*dde
= zio
->io_private
;
2089 zio_t
*pio
= zio_unique_parent(zio
);
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
;
2098 zio_buf_free(zio
->io_data
, zio
->io_size
);
2099 mutex_exit(&pio
->io_lock
);
2103 zio_ddt_read_start(zio_t
*zio
)
2105 blkptr_t
*bp
= zio
->io_bp
;
2108 ASSERT(BP_GET_DEDUP(bp
));
2109 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2110 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
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
);
2119 ASSERT(zio
->io_vsd
== NULL
);
2122 if (ddp_self
== NULL
)
2123 return (ZIO_PIPELINE_CONTINUE
);
2125 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
2126 if (ddp
->ddp_phys_birth
== 0 || ddp
== ddp_self
)
2128 ddt_bp_create(ddt
->ddt_checksum
, &dde
->dde_key
, ddp
,
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
));
2136 return (ZIO_PIPELINE_CONTINUE
);
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
));
2143 return (ZIO_PIPELINE_CONTINUE
);
2147 zio_ddt_read_done(zio_t
*zio
)
2149 blkptr_t
*bp
= zio
->io_bp
;
2151 if (zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_DONE
))
2152 return (ZIO_PIPELINE_STOP
);
2154 ASSERT(BP_GET_DEDUP(bp
));
2155 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2156 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
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
;
2162 ASSERT(spa_load_state(zio
->io_spa
) != SPA_LOAD_NONE
);
2163 return (ZIO_PIPELINE_CONTINUE
);
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
);
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;
2174 ddt_repair_done(ddt
, dde
);
2178 ASSERT(zio
->io_vsd
== NULL
);
2180 return (ZIO_PIPELINE_CONTINUE
);
2184 zio_ddt_collision(zio_t
*zio
, ddt_t
*ddt
, ddt_entry_t
*dde
)
2186 spa_t
*spa
= zio
->io_spa
;
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.
2195 for (p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2196 zio_t
*lio
= dde
->dde_lead_zio
[p
];
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);
2205 for (p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2206 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
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
;
2214 ddt_bp_fill(ddp
, &blk
, ddp
->ddp_phys_birth
);
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
);
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
));
2232 return (error
!= 0);
2240 zio_ddt_child_write_ready(zio_t
*zio
)
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
];
2253 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2255 ddt_phys_fill(ddp
, zio
->io_bp
);
2257 while ((pio
= zio_walk_parents(zio
)) != NULL
)
2258 ddt_bp_fill(ddp
, pio
->io_bp
, zio
->io_txg
);
2264 zio_ddt_child_write_done(zio_t
*zio
)
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
];
2273 ASSERT(ddp
->ddp_refcnt
== 0);
2274 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2275 dde
->dde_lead_zio
[p
] = NULL
;
2277 if (zio
->io_error
== 0) {
2278 while (zio_walk_parents(zio
) != NULL
)
2279 ddt_phys_addref(ddp
);
2281 ddt_phys_clear(ddp
);
2288 zio_ddt_ditto_write_done(zio_t
*zio
)
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
);
2300 ASSERT(ddp
->ddp_refcnt
== 0);
2301 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2302 dde
->dde_lead_zio
[p
] = NULL
;
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
);
2317 zio_ddt_write(zio_t
*zio
)
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
;
2327 ddt_t
*ddt
= ddt_select(spa
, bp
);
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
);
2336 dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
2337 ddp
= &dde
->dde_phys
[p
];
2339 if (zp
->zp_dedup_verify
&& zio_ddt_collision(zio
, ddt
, dde
)) {
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?)
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
;
2352 zp
->zp_dedup
= B_FALSE
;
2354 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
2356 return (ZIO_PIPELINE_CONTINUE
);
2359 ditto_copies
= ddt_ditto_copies_needed(ddt
, dde
, ddp
);
2360 ASSERT(ditto_copies
< SPA_DVAS_PER_BP
);
2362 if (ditto_copies
> ddt_ditto_copies_present(dde
) &&
2363 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] == NULL
) {
2364 zio_prop_t czp
= *zp
;
2366 czp
.zp_copies
= ditto_copies
;
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.
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
;
2382 return (ZIO_PIPELINE_CONTINUE
);
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
);
2390 zio_push_transform(dio
, zio
->io_data
, zio
->io_size
, 0, NULL
);
2391 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] = dio
;
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
]);
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
);
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
);
2412 zio_push_transform(cio
, zio
->io_data
, zio
->io_size
, 0, NULL
);
2413 dde
->dde_lead_zio
[p
] = cio
;
2423 return (ZIO_PIPELINE_CONTINUE
);
2426 ddt_entry_t
*freedde
; /* for debugging */
2429 zio_ddt_free(zio_t
*zio
)
2431 spa_t
*spa
= zio
->io_spa
;
2432 blkptr_t
*bp
= zio
->io_bp
;
2433 ddt_t
*ddt
= ddt_select(spa
, bp
);
2437 ASSERT(BP_GET_DEDUP(bp
));
2438 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2441 freedde
= dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
2443 ddp
= ddt_phys_select(dde
, bp
);
2445 ddt_phys_decref(ddp
);
2449 return (ZIO_PIPELINE_CONTINUE
);
2453 * ==========================================================================
2454 * Allocate and free blocks
2455 * ==========================================================================
2458 zio_dva_allocate(zio_t
*zio
)
2460 spa_t
*spa
= zio
->io_spa
;
2461 metaslab_class_t
*mc
= spa_normal_class(spa
);
2462 blkptr_t
*bp
= zio
->io_bp
;
2466 if (zio
->io_gang_leader
== NULL
) {
2467 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2468 zio
->io_gang_leader
= zio
;
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
));
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.
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
);
2490 spa_dbgmsg(spa
, "%s: metaslab allocation failure: zio %p, "
2491 "size %llu, error %d", spa_name(spa
), zio
, zio
->io_size
,
2493 if (error
== ENOSPC
&& zio
->io_size
> SPA_MINBLOCKSIZE
)
2494 return (zio_write_gang_block(zio
));
2495 zio
->io_error
= error
;
2498 return (ZIO_PIPELINE_CONTINUE
);
2502 zio_dva_free(zio_t
*zio
)
2504 metaslab_free(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
, B_FALSE
);
2506 return (ZIO_PIPELINE_CONTINUE
);
2510 zio_dva_claim(zio_t
*zio
)
2514 error
= metaslab_claim(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
);
2516 zio
->io_error
= error
;
2518 return (ZIO_PIPELINE_CONTINUE
);
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.
2527 zio_dva_unallocate(zio_t
*zio
, zio_gang_node_t
*gn
, blkptr_t
*bp
)
2531 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
));
2532 ASSERT(zio
->io_bp_override
== NULL
);
2534 if (!BP_IS_HOLE(bp
))
2535 metaslab_free(zio
->io_spa
, bp
, bp
->blk_birth
, B_TRUE
);
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
]);
2546 * Try to allocate an intent log block. Return 0 on success, errno on failure.
2549 zio_alloc_zil(spa_t
*spa
, uint64_t txg
, blkptr_t
*new_bp
, uint64_t size
,
2554 ASSERT(txg
> spa_syncing_txg(spa
));
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.
2562 error
= metaslab_alloc(spa
, spa_log_class(spa
), size
,
2563 new_bp
, 1, txg
, NULL
,
2564 METASLAB_FASTWRITE
| METASLAB_GANG_AVOID
);
2568 error
= metaslab_alloc(spa
, spa_normal_class(spa
), size
,
2569 new_bp
, 1, txg
, NULL
,
2570 METASLAB_FASTWRITE
);
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
);
2590 * Free an intent log block.
2593 zio_free_zil(spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
)
2595 ASSERT(BP_GET_TYPE(bp
) == DMU_OT_INTENT_LOG
);
2596 ASSERT(!BP_IS_GANG(bp
));
2598 zio_free(spa
, txg
, bp
);
2602 * ==========================================================================
2603 * Read and write to physical devices
2604 * ==========================================================================
2607 zio_vdev_io_start(zio_t
*zio
)
2609 vdev_t
*vd
= zio
->io_vd
;
2611 spa_t
*spa
= zio
->io_spa
;
2613 ASSERT(zio
->io_error
== 0);
2614 ASSERT(zio
->io_child_error
[ZIO_CHILD_VDEV
] == 0);
2617 if (!(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
2618 spa_config_enter(spa
, SCL_ZIO
, zio
, RW_READER
);
2621 * The mirror_ops handle multiple DVAs in a single BP.
2623 return (vdev_mirror_ops
.vdev_op_io_start(zio
));
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
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.
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();
2643 (void) atomic_cas_64(&spa
->spa_last_io
, old
, new);
2646 align
= 1ULL << vd
->vdev_top
->vdev_ashift
;
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
);
2658 zio_push_transform(zio
, abuf
, asize
, asize
, zio_subblock
);
2662 * If this is not a physical io, make sure that it is properly aligned
2663 * before proceeding.
2665 if (!(zio
->io_flags
& ZIO_FLAG_PHYSICAL
)) {
2666 ASSERT0(P2PHASE(zio
->io_offset
, align
));
2667 ASSERT0(P2PHASE(zio
->io_size
, align
));
2670 * For physical writes, we allow 512b aligned writes and assume
2671 * the device will perform a read-modify-write as necessary.
2673 ASSERT0(P2PHASE(zio
->io_offset
, SPA_MINBLOCKSIZE
));
2674 ASSERT0(P2PHASE(zio
->io_size
, SPA_MINBLOCKSIZE
));
2677 VERIFY(zio
->io_type
!= ZIO_TYPE_WRITE
|| spa_writeable(spa
));
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.
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
);
2701 if (vd
->vdev_ops
->vdev_op_leaf
&&
2702 (zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
)) {
2704 if (zio
->io_type
== ZIO_TYPE_READ
&& vdev_cache_read(zio
))
2705 return (ZIO_PIPELINE_CONTINUE
);
2707 if ((zio
= vdev_queue_io(zio
)) == NULL
)
2708 return (ZIO_PIPELINE_STOP
);
2710 if (!vdev_accessible(vd
, zio
)) {
2711 zio
->io_error
= SET_ERROR(ENXIO
);
2713 return (ZIO_PIPELINE_STOP
);
2717 return (vd
->vdev_ops
->vdev_op_io_start(zio
));
2721 zio_vdev_io_done(zio_t
*zio
)
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
;
2727 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
))
2728 return (ZIO_PIPELINE_STOP
);
2730 ASSERT(zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
);
2732 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
) {
2734 vdev_queue_io_done(zio
);
2736 if (zio
->io_type
== ZIO_TYPE_WRITE
)
2737 vdev_cache_write(zio
);
2739 if (zio_injection_enabled
&& zio
->io_error
== 0)
2740 zio
->io_error
= zio_handle_device_injection(vd
,
2743 if (zio_injection_enabled
&& zio
->io_error
== 0)
2744 zio
->io_error
= zio_handle_label_injection(zio
, EIO
);
2746 if (zio
->io_error
) {
2747 if (!vdev_accessible(vd
, zio
)) {
2748 zio
->io_error
= SET_ERROR(ENXIO
);
2750 unexpected_error
= B_TRUE
;
2755 ops
->vdev_op_io_done(zio
);
2757 if (unexpected_error
)
2758 VERIFY(vdev_probe(vd
, zio
) == NULL
);
2760 return (ZIO_PIPELINE_CONTINUE
);
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.
2768 zio_vsd_default_cksum_finish(zio_cksum_report_t
*zcr
,
2769 const void *good_buf
)
2771 /* no processing needed */
2772 zfs_ereport_finish_checksum(zcr
, good_buf
, zcr
->zcr_cbdata
, B_FALSE
);
2777 zio_vsd_default_cksum_report(zio_t
*zio
, zio_cksum_report_t
*zcr
, void *ignored
)
2779 void *buf
= zio_buf_alloc(zio
->io_size
);
2781 bcopy(zio
->io_data
, buf
, zio
->io_size
);
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
;
2790 zio_vdev_io_assess(zio_t
*zio
)
2792 vdev_t
*vd
= zio
->io_vd
;
2794 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
))
2795 return (ZIO_PIPELINE_STOP
);
2797 if (vd
== NULL
&& !(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
2798 spa_config_exit(zio
->io_spa
, SCL_ZIO
, zio
);
2800 if (zio
->io_vsd
!= NULL
) {
2801 zio
->io_vsd_ops
->vsd_free(zio
);
2805 if (zio_injection_enabled
&& zio
->io_error
== 0)
2806 zio
->io_error
= zio_handle_fault_injection(zio
, EIO
);
2809 * If the I/O failed, determine whether we should attempt to retry it.
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.
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 */
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
);
2828 * If we got an error on a leaf device, convert it to ENXIO
2829 * if the device is not accessible at all.
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
);
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.
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
;
2845 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
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
);
2854 return (ZIO_PIPELINE_CONTINUE
);
2858 zio_vdev_io_reissue(zio_t
*zio
)
2860 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
2861 ASSERT(zio
->io_error
== 0);
2863 zio
->io_stage
>>= 1;
2867 zio_vdev_io_redone(zio_t
*zio
)
2869 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_DONE
);
2871 zio
->io_stage
>>= 1;
2875 zio_vdev_io_bypass(zio_t
*zio
)
2877 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
2878 ASSERT(zio
->io_error
== 0);
2880 zio
->io_flags
|= ZIO_FLAG_IO_BYPASS
;
2881 zio
->io_stage
= ZIO_STAGE_VDEV_IO_ASSESS
>> 1;
2885 * ==========================================================================
2886 * Generate and verify checksums
2887 * ==========================================================================
2890 zio_checksum_generate(zio_t
*zio
)
2892 blkptr_t
*bp
= zio
->io_bp
;
2893 enum zio_checksum checksum
;
2897 * This is zio_write_phys().
2898 * We're either generating a label checksum, or none at all.
2900 checksum
= zio
->io_prop
.zp_checksum
;
2902 if (checksum
== ZIO_CHECKSUM_OFF
)
2903 return (ZIO_PIPELINE_CONTINUE
);
2905 ASSERT(checksum
== ZIO_CHECKSUM_LABEL
);
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
;
2911 checksum
= BP_GET_CHECKSUM(bp
);
2915 zio_checksum_compute(zio
, checksum
, zio
->io_data
, zio
->io_size
);
2917 return (ZIO_PIPELINE_CONTINUE
);
2921 zio_checksum_verify(zio_t
*zio
)
2923 zio_bad_cksum_t info
;
2924 blkptr_t
*bp
= zio
->io_bp
;
2927 ASSERT(zio
->io_vd
!= NULL
);
2931 * This is zio_read_phys().
2932 * We're either verifying a label checksum, or nothing at all.
2934 if (zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_OFF
)
2935 return (ZIO_PIPELINE_CONTINUE
);
2937 ASSERT(zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_LABEL
);
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
);
2949 return (ZIO_PIPELINE_CONTINUE
);
2953 * Called by RAID-Z to ensure we don't compute the checksum twice.
2956 zio_checksum_verified(zio_t
*zio
)
2958 zio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
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 * ==========================================================================
2971 zio_worst_error(int e1
, int e2
)
2973 static int zio_error_rank
[] = { 0, ENXIO
, ECKSUM
, EIO
};
2976 for (r1
= 0; r1
< sizeof (zio_error_rank
) / sizeof (int); r1
++)
2977 if (e1
== zio_error_rank
[r1
])
2980 for (r2
= 0; r2
< sizeof (zio_error_rank
) / sizeof (int); r2
++)
2981 if (e2
== zio_error_rank
[r2
])
2984 return (r1
> r2
? e1
: e2
);
2988 * ==========================================================================
2990 * ==========================================================================
2993 zio_ready(zio_t
*zio
)
2995 blkptr_t
*bp
= zio
->io_bp
;
2996 zio_t
*pio
, *pio_next
;
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
);
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);
3011 if (bp
!= NULL
&& bp
!= &zio
->io_bp_copy
)
3012 zio
->io_bp_copy
= *bp
;
3015 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
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
);
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.
3029 for (; pio
!= NULL
; pio
= pio_next
) {
3030 pio_next
= zio_walk_parents(zio
);
3031 zio_notify_parent(pio
, zio
, ZIO_WAIT_READY
);
3034 if (zio
->io_flags
& ZIO_FLAG_NODATA
) {
3035 if (BP_IS_GANG(bp
)) {
3036 zio
->io_flags
&= ~ZIO_FLAG_NODATA
;
3038 ASSERT((uintptr_t)zio
->io_data
< SPA_MAXBLOCKSIZE
);
3039 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
3043 if (zio_injection_enabled
&&
3044 zio
->io_spa
->spa_syncing_txg
== zio
->io_txg
)
3045 zio_handle_ignored_writes(zio
);
3047 return (ZIO_PIPELINE_CONTINUE
);
3051 zio_done(zio_t
*zio
)
3053 zio_t
*pio
, *pio_next
;
3057 * If our children haven't all completed,
3058 * wait for them and then repeat this pipeline stage.
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
);
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);
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
)));
3086 if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
)
3087 VERIFY(BP_EQUAL(zio
->io_bp
, &zio
->io_bp_orig
));
3091 * If there were child vdev/gang/ddt errors, they apply to us now.
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
);
3098 * If the I/O on the transformed data was successful, generate any
3099 * checksum reports now while we still have the transformed data.
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
;
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
);
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
);
3119 if (asize
!= zio
->io_size
)
3120 zio_buf_free(abuf
, asize
);
3124 zio_pop_transforms(zio
); /* note: may set zio->io_error */
3126 vdev_stat_update(zio
, zio
->io_size
);
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.
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);
3139 if (zio
->io_error
) {
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.
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);
3151 if ((zio
->io_error
== EIO
|| !(zio
->io_flags
&
3152 (ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_DONT_PROPAGATE
))) &&
3153 zio
== zio
->io_logical
) {
3155 * For logical I/O requests, tell the SPA to log the
3156 * error and generate a logical data ereport.
3158 spa_log_error(zio
->io_spa
, zio
);
3159 zfs_ereport_post(FM_EREPORT_ZFS_DATA
, zio
->io_spa
,
3164 if (zio
->io_error
&& zio
== zio
->io_logical
) {
3166 * Determine whether zio should be reexecuted. This will
3167 * propagate all the way to the root via zio_notify_parent().
3169 ASSERT(zio
->io_vd
== NULL
&& zio
->io_bp
!= NULL
);
3170 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
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
;
3177 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
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
;
3188 if (!(zio
->io_flags
& ZIO_FLAG_CANFAIL
) && !zio
->io_reexecute
)
3189 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
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
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.
3206 zio_inherit_child_errors(zio
, ZIO_CHILD_LOGICAL
);
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
);
3213 zio_gang_tree_free(&zio
->io_gang_tree
);
3216 * Godfather I/Os should never suspend.
3218 if ((zio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
3219 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
))
3220 zio
->io_reexecute
= 0;
3222 if (zio
->io_reexecute
) {
3224 * This is a logical I/O that wants to reexecute.
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.
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.
3236 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
3238 zio
->io_gang_leader
= NULL
;
3240 mutex_enter(&zio
->io_lock
);
3241 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
3242 mutex_exit(&zio
->io_lock
);
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.
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
);
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
);
3262 if ((pio
= zio_unique_parent(zio
)) != NULL
) {
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.
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
) {
3273 * We'd fail again if we reexecuted now, so suspend
3274 * until conditions improve (e.g. device comes online).
3276 zio_suspend(zio
->io_spa
, zio
);
3279 * Reexecution is potentially a huge amount of work.
3280 * Hand it off to the otherwise-unused claim taskq.
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,
3288 return (ZIO_PIPELINE_STOP
);
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
));
3296 * Report any checksum errors, since the I/O is complete.
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
);
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
);
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.
3320 mutex_enter(&zio
->io_lock
);
3321 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
3322 mutex_exit(&zio
->io_lock
);
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
);
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
);
3340 return (ZIO_PIPELINE_STOP
);
3344 * ==========================================================================
3345 * I/O pipeline definition
3346 * ==========================================================================
3348 static zio_pipe_stage_t
*zio_pipeline
[] = {
3354 zio_checksum_generate
,
3369 zio_checksum_verify
,
3373 /* dnp is the dnode for zb1->zb_object */
3375 zbookmark_is_before(const dnode_phys_t
*dnp
, const zbookmark_phys_t
*zb1
,
3376 const zbookmark_phys_t
*zb2
)
3378 uint64_t zb1nextL0
, zb2thisobj
;
3380 ASSERT(zb1
->zb_objset
== zb2
->zb_objset
);
3381 ASSERT(zb2
->zb_level
== 0);
3383 /* The objset_phys_t isn't before anything. */
3387 zb1nextL0
= (zb1
->zb_blkid
+ 1) <<
3388 ((zb1
->zb_level
) * (dnp
->dn_indblkshift
- SPA_BLKPTRSHIFT
));
3390 zb2thisobj
= zb2
->zb_object
? zb2
->zb_object
:
3391 zb2
->zb_blkid
<< (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
);
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
);
3399 if (zb1
->zb_object
< zb2thisobj
)
3401 if (zb1
->zb_object
> zb2thisobj
)
3403 if (zb2
->zb_object
== DMU_META_DNODE_OBJECT
)
3405 return (zb1nextL0
<= zb2
->zb_blkid
);
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
);
3419 module_param(zio_bulk_flags
, int, 0644);
3420 MODULE_PARM_DESC(zio_bulk_flags
, "Additional flags to pass to bulk buffers");
3422 module_param(zio_delay_max
, int, 0644);
3423 MODULE_PARM_DESC(zio_delay_max
, "Max zio millisec delay before posting event");
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");
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");
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");
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");