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) 2013 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>
41 * ==========================================================================
43 * ==========================================================================
45 uint8_t zio_priority_table
[ZIO_PRIORITY_TABLE_SIZE
] = {
46 0, /* ZIO_PRIORITY_NOW */
47 0, /* ZIO_PRIORITY_SYNC_READ */
48 0, /* ZIO_PRIORITY_SYNC_WRITE */
49 0, /* ZIO_PRIORITY_LOG_WRITE */
50 1, /* ZIO_PRIORITY_CACHE_FILL */
51 1, /* ZIO_PRIORITY_AGG */
52 4, /* ZIO_PRIORITY_FREE */
53 4, /* ZIO_PRIORITY_ASYNC_WRITE */
54 6, /* ZIO_PRIORITY_ASYNC_READ */
55 10, /* ZIO_PRIORITY_RESILVER */
56 20, /* ZIO_PRIORITY_SCRUB */
57 2, /* ZIO_PRIORITY_DDT_PREFETCH */
61 * ==========================================================================
62 * I/O type descriptions
63 * ==========================================================================
65 char *zio_type_name
[ZIO_TYPES
] = {
66 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl"
70 * ==========================================================================
72 * ==========================================================================
74 kmem_cache_t
*zio_cache
;
75 kmem_cache_t
*zio_link_cache
;
76 kmem_cache_t
*zio_vdev_cache
;
77 kmem_cache_t
*zio_buf_cache
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
78 kmem_cache_t
*zio_data_buf_cache
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
79 int zio_bulk_flags
= 0;
80 int zio_delay_max
= ZIO_DELAY_MAX
;
82 extern int zfs_mg_alloc_failures
;
85 * The following actions directly effect the spa's sync-to-convergence logic.
86 * The values below define the sync pass when we start performing the action.
87 * Care should be taken when changing these values as they directly impact
88 * spa_sync() performance. Tuning these values may introduce subtle performance
89 * pathologies and should only be done in the context of performance analysis.
90 * These tunables will eventually be removed and replaced with #defines once
91 * enough analysis has been done to determine optimal values.
93 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
94 * regular blocks are not deferred.
96 int zfs_sync_pass_deferred_free
= 2; /* defer frees starting in this pass */
97 int zfs_sync_pass_dont_compress
= 5; /* don't compress starting in this pass */
98 int zfs_sync_pass_rewrite
= 2; /* rewrite new bps starting in this pass */
101 * An allocating zio is one that either currently has the DVA allocate
102 * stage set or will have it later in its lifetime.
104 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
106 int zio_requeue_io_start_cut_in_line
= 1;
109 int zio_buf_debug_limit
= 16384;
111 int zio_buf_debug_limit
= 0;
114 static inline void __zio_execute(zio_t
*zio
);
117 zio_cons(void *arg
, void *unused
, int kmflag
)
121 bzero(zio
, sizeof (zio_t
));
123 mutex_init(&zio
->io_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
124 cv_init(&zio
->io_cv
, NULL
, CV_DEFAULT
, NULL
);
126 list_create(&zio
->io_parent_list
, sizeof (zio_link_t
),
127 offsetof(zio_link_t
, zl_parent_node
));
128 list_create(&zio
->io_child_list
, sizeof (zio_link_t
),
129 offsetof(zio_link_t
, zl_child_node
));
135 zio_dest(void *arg
, void *unused
)
139 mutex_destroy(&zio
->io_lock
);
140 cv_destroy(&zio
->io_cv
);
141 list_destroy(&zio
->io_parent_list
);
142 list_destroy(&zio
->io_child_list
);
149 vmem_t
*data_alloc_arena
= NULL
;
151 zio_cache
= kmem_cache_create("zio_cache", sizeof (zio_t
), 0,
152 zio_cons
, zio_dest
, NULL
, NULL
, NULL
, KMC_KMEM
);
153 zio_link_cache
= kmem_cache_create("zio_link_cache",
154 sizeof (zio_link_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, KMC_KMEM
);
155 zio_vdev_cache
= kmem_cache_create("zio_vdev_cache", sizeof(vdev_io_t
),
156 PAGESIZE
, NULL
, NULL
, NULL
, NULL
, NULL
, KMC_VMEM
);
159 * For small buffers, we want a cache for each multiple of
160 * SPA_MINBLOCKSIZE. For medium-size buffers, we want a cache
161 * for each quarter-power of 2. For large buffers, we want
162 * a cache for each multiple of PAGESIZE.
164 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
165 size_t size
= (c
+ 1) << SPA_MINBLOCKSHIFT
;
169 while (p2
& (p2
- 1))
174 * If we are using watchpoints, put each buffer on its own page,
175 * to eliminate the performance overhead of trapping to the
176 * kernel when modifying a non-watched buffer that shares the
177 * page with a watched buffer.
179 if (arc_watch
&& !IS_P2ALIGNED(size
, PAGESIZE
))
182 if (size
<= 4 * SPA_MINBLOCKSIZE
) {
183 align
= SPA_MINBLOCKSIZE
;
184 } else if (IS_P2ALIGNED(size
, PAGESIZE
)) {
186 } else if (IS_P2ALIGNED(size
, p2
>> 2)) {
192 int flags
= zio_bulk_flags
;
195 * The smallest buffers (512b) are heavily used and
196 * experience a lot of churn. The slabs allocated
197 * for them are also relatively small (32K). Thus
198 * in over to avoid expensive calls to vmalloc() we
199 * make an exception to the usual slab allocation
200 * policy and force these buffers to be kmem backed.
202 if (size
== (1 << SPA_MINBLOCKSHIFT
))
205 (void) sprintf(name
, "zio_buf_%lu", (ulong_t
)size
);
206 zio_buf_cache
[c
] = kmem_cache_create(name
, size
,
207 align
, NULL
, NULL
, NULL
, NULL
, NULL
, flags
);
209 (void) sprintf(name
, "zio_data_buf_%lu", (ulong_t
)size
);
210 zio_data_buf_cache
[c
] = kmem_cache_create(name
, size
,
211 align
, NULL
, NULL
, NULL
, NULL
,
212 data_alloc_arena
, flags
);
217 ASSERT(zio_buf_cache
[c
] != NULL
);
218 if (zio_buf_cache
[c
- 1] == NULL
)
219 zio_buf_cache
[c
- 1] = zio_buf_cache
[c
];
221 ASSERT(zio_data_buf_cache
[c
] != NULL
);
222 if (zio_data_buf_cache
[c
- 1] == NULL
)
223 zio_data_buf_cache
[c
- 1] = zio_data_buf_cache
[c
];
227 * The zio write taskqs have 1 thread per cpu, allow 1/2 of the taskqs
228 * to fail 3 times per txg or 8 failures, whichever is greater.
230 zfs_mg_alloc_failures
= MAX((3 * max_ncpus
/ 2), 8);
241 kmem_cache_t
*last_cache
= NULL
;
242 kmem_cache_t
*last_data_cache
= NULL
;
244 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
245 if (zio_buf_cache
[c
] != last_cache
) {
246 last_cache
= zio_buf_cache
[c
];
247 kmem_cache_destroy(zio_buf_cache
[c
]);
249 zio_buf_cache
[c
] = NULL
;
251 if (zio_data_buf_cache
[c
] != last_data_cache
) {
252 last_data_cache
= zio_data_buf_cache
[c
];
253 kmem_cache_destroy(zio_data_buf_cache
[c
]);
255 zio_data_buf_cache
[c
] = NULL
;
258 kmem_cache_destroy(zio_vdev_cache
);
259 kmem_cache_destroy(zio_link_cache
);
260 kmem_cache_destroy(zio_cache
);
268 * ==========================================================================
269 * Allocate and free I/O buffers
270 * ==========================================================================
274 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
275 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
276 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
277 * excess / transient data in-core during a crashdump.
280 zio_buf_alloc(size_t size
)
282 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
284 ASSERT(c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
286 return (kmem_cache_alloc(zio_buf_cache
[c
], KM_PUSHPAGE
| KM_NODEBUG
));
290 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
291 * crashdump if the kernel panics. This exists so that we will limit the amount
292 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
293 * of kernel heap dumped to disk when the kernel panics)
296 zio_data_buf_alloc(size_t size
)
298 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
300 ASSERT(c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
302 return (kmem_cache_alloc(zio_data_buf_cache
[c
],
303 KM_PUSHPAGE
| KM_NODEBUG
));
307 zio_buf_free(void *buf
, size_t size
)
309 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
311 ASSERT(c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
313 kmem_cache_free(zio_buf_cache
[c
], buf
);
317 zio_data_buf_free(void *buf
, size_t size
)
319 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
321 ASSERT(c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
323 kmem_cache_free(zio_data_buf_cache
[c
], buf
);
327 * Dedicated I/O buffers to ensure that memory fragmentation never prevents
328 * or significantly delays the issuing of a zio. These buffers are used
329 * to aggregate I/O and could be used for raidz stripes.
334 return (kmem_cache_alloc(zio_vdev_cache
, KM_PUSHPAGE
));
338 zio_vdev_free(void *buf
)
340 kmem_cache_free(zio_vdev_cache
, buf
);
345 * ==========================================================================
346 * Push and pop I/O transform buffers
347 * ==========================================================================
350 zio_push_transform(zio_t
*zio
, void *data
, uint64_t size
, uint64_t bufsize
,
351 zio_transform_func_t
*transform
)
353 zio_transform_t
*zt
= kmem_alloc(sizeof (zio_transform_t
), KM_PUSHPAGE
);
355 zt
->zt_orig_data
= zio
->io_data
;
356 zt
->zt_orig_size
= zio
->io_size
;
357 zt
->zt_bufsize
= bufsize
;
358 zt
->zt_transform
= transform
;
360 zt
->zt_next
= zio
->io_transform_stack
;
361 zio
->io_transform_stack
= zt
;
368 zio_pop_transforms(zio_t
*zio
)
372 while ((zt
= zio
->io_transform_stack
) != NULL
) {
373 if (zt
->zt_transform
!= NULL
)
374 zt
->zt_transform(zio
,
375 zt
->zt_orig_data
, zt
->zt_orig_size
);
377 if (zt
->zt_bufsize
!= 0)
378 zio_buf_free(zio
->io_data
, zt
->zt_bufsize
);
380 zio
->io_data
= zt
->zt_orig_data
;
381 zio
->io_size
= zt
->zt_orig_size
;
382 zio
->io_transform_stack
= zt
->zt_next
;
384 kmem_free(zt
, sizeof (zio_transform_t
));
389 * ==========================================================================
390 * I/O transform callbacks for subblocks and decompression
391 * ==========================================================================
394 zio_subblock(zio_t
*zio
, void *data
, uint64_t size
)
396 ASSERT(zio
->io_size
> size
);
398 if (zio
->io_type
== ZIO_TYPE_READ
)
399 bcopy(zio
->io_data
, data
, size
);
403 zio_decompress(zio_t
*zio
, void *data
, uint64_t size
)
405 if (zio
->io_error
== 0 &&
406 zio_decompress_data(BP_GET_COMPRESS(zio
->io_bp
),
407 zio
->io_data
, data
, zio
->io_size
, size
) != 0)
408 zio
->io_error
= SET_ERROR(EIO
);
412 * ==========================================================================
413 * I/O parent/child relationships and pipeline interlocks
414 * ==========================================================================
417 * NOTE - Callers to zio_walk_parents() and zio_walk_children must
418 * continue calling these functions until they return NULL.
419 * Otherwise, the next caller will pick up the list walk in
420 * some indeterminate state. (Otherwise every caller would
421 * have to pass in a cookie to keep the state represented by
422 * io_walk_link, which gets annoying.)
425 zio_walk_parents(zio_t
*cio
)
427 zio_link_t
*zl
= cio
->io_walk_link
;
428 list_t
*pl
= &cio
->io_parent_list
;
430 zl
= (zl
== NULL
) ? list_head(pl
) : list_next(pl
, zl
);
431 cio
->io_walk_link
= zl
;
436 ASSERT(zl
->zl_child
== cio
);
437 return (zl
->zl_parent
);
441 zio_walk_children(zio_t
*pio
)
443 zio_link_t
*zl
= pio
->io_walk_link
;
444 list_t
*cl
= &pio
->io_child_list
;
446 zl
= (zl
== NULL
) ? list_head(cl
) : list_next(cl
, zl
);
447 pio
->io_walk_link
= zl
;
452 ASSERT(zl
->zl_parent
== pio
);
453 return (zl
->zl_child
);
457 zio_unique_parent(zio_t
*cio
)
459 zio_t
*pio
= zio_walk_parents(cio
);
461 VERIFY(zio_walk_parents(cio
) == NULL
);
466 zio_add_child(zio_t
*pio
, zio_t
*cio
)
468 zio_link_t
*zl
= kmem_cache_alloc(zio_link_cache
, KM_PUSHPAGE
);
472 * Logical I/Os can have logical, gang, or vdev children.
473 * Gang I/Os can have gang or vdev children.
474 * Vdev I/Os can only have vdev children.
475 * The following ASSERT captures all of these constraints.
477 ASSERT(cio
->io_child_type
<= pio
->io_child_type
);
482 mutex_enter(&cio
->io_lock
);
483 mutex_enter(&pio
->io_lock
);
485 ASSERT(pio
->io_state
[ZIO_WAIT_DONE
] == 0);
487 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
488 pio
->io_children
[cio
->io_child_type
][w
] += !cio
->io_state
[w
];
490 list_insert_head(&pio
->io_child_list
, zl
);
491 list_insert_head(&cio
->io_parent_list
, zl
);
493 pio
->io_child_count
++;
494 cio
->io_parent_count
++;
496 mutex_exit(&pio
->io_lock
);
497 mutex_exit(&cio
->io_lock
);
501 zio_remove_child(zio_t
*pio
, zio_t
*cio
, zio_link_t
*zl
)
503 ASSERT(zl
->zl_parent
== pio
);
504 ASSERT(zl
->zl_child
== cio
);
506 mutex_enter(&cio
->io_lock
);
507 mutex_enter(&pio
->io_lock
);
509 list_remove(&pio
->io_child_list
, zl
);
510 list_remove(&cio
->io_parent_list
, zl
);
512 pio
->io_child_count
--;
513 cio
->io_parent_count
--;
515 mutex_exit(&pio
->io_lock
);
516 mutex_exit(&cio
->io_lock
);
518 kmem_cache_free(zio_link_cache
, zl
);
522 zio_wait_for_children(zio_t
*zio
, enum zio_child child
, enum zio_wait_type wait
)
524 uint64_t *countp
= &zio
->io_children
[child
][wait
];
525 boolean_t waiting
= B_FALSE
;
527 mutex_enter(&zio
->io_lock
);
528 ASSERT(zio
->io_stall
== NULL
);
531 zio
->io_stall
= countp
;
534 mutex_exit(&zio
->io_lock
);
539 __attribute__((always_inline
))
541 zio_notify_parent(zio_t
*pio
, zio_t
*zio
, enum zio_wait_type wait
)
543 uint64_t *countp
= &pio
->io_children
[zio
->io_child_type
][wait
];
544 int *errorp
= &pio
->io_child_error
[zio
->io_child_type
];
546 mutex_enter(&pio
->io_lock
);
547 if (zio
->io_error
&& !(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
548 *errorp
= zio_worst_error(*errorp
, zio
->io_error
);
549 pio
->io_reexecute
|= zio
->io_reexecute
;
550 ASSERT3U(*countp
, >, 0);
551 if (--*countp
== 0 && pio
->io_stall
== countp
) {
552 pio
->io_stall
= NULL
;
553 mutex_exit(&pio
->io_lock
);
556 mutex_exit(&pio
->io_lock
);
561 zio_inherit_child_errors(zio_t
*zio
, enum zio_child c
)
563 if (zio
->io_child_error
[c
] != 0 && zio
->io_error
== 0)
564 zio
->io_error
= zio
->io_child_error
[c
];
568 * ==========================================================================
569 * Create the various types of I/O (read, write, free, etc)
570 * ==========================================================================
573 zio_create(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
574 void *data
, uint64_t size
, zio_done_func_t
*done
, void *private,
575 zio_type_t type
, int priority
, enum zio_flag flags
,
576 vdev_t
*vd
, uint64_t offset
, const zbookmark_t
*zb
,
577 enum zio_stage stage
, enum zio_stage pipeline
)
581 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
582 ASSERT(P2PHASE(size
, SPA_MINBLOCKSIZE
) == 0);
583 ASSERT(P2PHASE(offset
, SPA_MINBLOCKSIZE
) == 0);
585 ASSERT(!vd
|| spa_config_held(spa
, SCL_STATE_ALL
, RW_READER
));
586 ASSERT(!bp
|| !(flags
& ZIO_FLAG_CONFIG_WRITER
));
587 ASSERT(vd
|| stage
== ZIO_STAGE_OPEN
);
589 zio
= kmem_cache_alloc(zio_cache
, KM_PUSHPAGE
);
592 zio
->io_child_type
= ZIO_CHILD_VDEV
;
593 else if (flags
& ZIO_FLAG_GANG_CHILD
)
594 zio
->io_child_type
= ZIO_CHILD_GANG
;
595 else if (flags
& ZIO_FLAG_DDT_CHILD
)
596 zio
->io_child_type
= ZIO_CHILD_DDT
;
598 zio
->io_child_type
= ZIO_CHILD_LOGICAL
;
601 zio
->io_logical
= NULL
;
602 zio
->io_bp
= (blkptr_t
*)bp
;
603 zio
->io_bp_copy
= *bp
;
604 zio
->io_bp_orig
= *bp
;
605 if (type
!= ZIO_TYPE_WRITE
||
606 zio
->io_child_type
== ZIO_CHILD_DDT
)
607 zio
->io_bp
= &zio
->io_bp_copy
; /* so caller can free */
608 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
609 zio
->io_logical
= zio
;
610 if (zio
->io_child_type
> ZIO_CHILD_GANG
&& BP_IS_GANG(bp
))
611 pipeline
|= ZIO_GANG_STAGES
;
613 zio
->io_logical
= NULL
;
615 bzero(&zio
->io_bp_copy
, sizeof (blkptr_t
));
616 bzero(&zio
->io_bp_orig
, sizeof (blkptr_t
));
621 zio
->io_ready
= NULL
;
623 zio
->io_private
= private;
624 zio
->io_prev_space_delta
= 0;
626 zio
->io_priority
= priority
;
629 zio
->io_vsd_ops
= NULL
;
630 zio
->io_offset
= offset
;
631 zio
->io_deadline
= 0;
632 zio
->io_timestamp
= 0;
635 zio
->io_orig_data
= zio
->io_data
= data
;
636 zio
->io_orig_size
= zio
->io_size
= size
;
637 zio
->io_orig_flags
= zio
->io_flags
= flags
;
638 zio
->io_orig_stage
= zio
->io_stage
= stage
;
639 zio
->io_orig_pipeline
= zio
->io_pipeline
= pipeline
;
640 bzero(&zio
->io_prop
, sizeof (zio_prop_t
));
642 zio
->io_reexecute
= 0;
643 zio
->io_bp_override
= NULL
;
644 zio
->io_walk_link
= NULL
;
645 zio
->io_transform_stack
= NULL
;
647 zio
->io_child_count
= 0;
648 zio
->io_parent_count
= 0;
649 zio
->io_stall
= NULL
;
650 zio
->io_gang_leader
= NULL
;
651 zio
->io_gang_tree
= NULL
;
652 zio
->io_executor
= NULL
;
653 zio
->io_waiter
= NULL
;
654 zio
->io_cksum_report
= NULL
;
656 bzero(zio
->io_child_error
, sizeof (int) * ZIO_CHILD_TYPES
);
657 bzero(zio
->io_children
,
658 sizeof (uint64_t) * ZIO_CHILD_TYPES
* ZIO_WAIT_TYPES
);
659 bzero(&zio
->io_bookmark
, sizeof (zbookmark_t
));
661 zio
->io_state
[ZIO_WAIT_READY
] = (stage
>= ZIO_STAGE_READY
);
662 zio
->io_state
[ZIO_WAIT_DONE
] = (stage
>= ZIO_STAGE_DONE
);
665 zio
->io_bookmark
= *zb
;
668 if (zio
->io_logical
== NULL
)
669 zio
->io_logical
= pio
->io_logical
;
670 if (zio
->io_child_type
== ZIO_CHILD_GANG
)
671 zio
->io_gang_leader
= pio
->io_gang_leader
;
672 zio_add_child(pio
, zio
);
675 taskq_init_ent(&zio
->io_tqent
);
681 zio_destroy(zio_t
*zio
)
683 kmem_cache_free(zio_cache
, zio
);
687 zio_null(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, zio_done_func_t
*done
,
688 void *private, enum zio_flag flags
)
692 zio
= zio_create(pio
, spa
, 0, NULL
, NULL
, 0, done
, private,
693 ZIO_TYPE_NULL
, ZIO_PRIORITY_NOW
, flags
, vd
, 0, NULL
,
694 ZIO_STAGE_OPEN
, ZIO_INTERLOCK_PIPELINE
);
700 zio_root(spa_t
*spa
, zio_done_func_t
*done
, void *private, enum zio_flag flags
)
702 return (zio_null(NULL
, spa
, NULL
, done
, private, flags
));
706 zio_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
707 void *data
, uint64_t size
, zio_done_func_t
*done
, void *private,
708 int priority
, enum zio_flag flags
, const zbookmark_t
*zb
)
712 zio
= zio_create(pio
, spa
, BP_PHYSICAL_BIRTH(bp
), bp
,
713 data
, size
, done
, private,
714 ZIO_TYPE_READ
, priority
, flags
, NULL
, 0, zb
,
715 ZIO_STAGE_OPEN
, (flags
& ZIO_FLAG_DDT_CHILD
) ?
716 ZIO_DDT_CHILD_READ_PIPELINE
: ZIO_READ_PIPELINE
);
722 zio_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
,
723 void *data
, uint64_t size
, const zio_prop_t
*zp
,
724 zio_done_func_t
*ready
, zio_done_func_t
*done
, void *private,
725 int priority
, enum zio_flag flags
, const zbookmark_t
*zb
)
729 ASSERT(zp
->zp_checksum
>= ZIO_CHECKSUM_OFF
&&
730 zp
->zp_checksum
< ZIO_CHECKSUM_FUNCTIONS
&&
731 zp
->zp_compress
>= ZIO_COMPRESS_OFF
&&
732 zp
->zp_compress
< ZIO_COMPRESS_FUNCTIONS
&&
733 DMU_OT_IS_VALID(zp
->zp_type
) &&
736 zp
->zp_copies
<= spa_max_replication(spa
));
738 zio
= zio_create(pio
, spa
, txg
, bp
, data
, size
, done
, private,
739 ZIO_TYPE_WRITE
, priority
, flags
, NULL
, 0, zb
,
740 ZIO_STAGE_OPEN
, (flags
& ZIO_FLAG_DDT_CHILD
) ?
741 ZIO_DDT_CHILD_WRITE_PIPELINE
: ZIO_WRITE_PIPELINE
);
743 zio
->io_ready
= ready
;
750 zio_rewrite(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
, void *data
,
751 uint64_t size
, zio_done_func_t
*done
, void *private, int priority
,
752 enum zio_flag flags
, zbookmark_t
*zb
)
756 zio
= zio_create(pio
, spa
, txg
, bp
, data
, size
, done
, private,
757 ZIO_TYPE_WRITE
, priority
, flags
, NULL
, 0, zb
,
758 ZIO_STAGE_OPEN
, ZIO_REWRITE_PIPELINE
);
764 zio_write_override(zio_t
*zio
, blkptr_t
*bp
, int copies
, boolean_t nopwrite
)
766 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
767 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
768 ASSERT(zio
->io_stage
== ZIO_STAGE_OPEN
);
769 ASSERT(zio
->io_txg
== spa_syncing_txg(zio
->io_spa
));
772 * We must reset the io_prop to match the values that existed
773 * when the bp was first written by dmu_sync() keeping in mind
774 * that nopwrite and dedup are mutually exclusive.
776 zio
->io_prop
.zp_dedup
= nopwrite
? B_FALSE
: zio
->io_prop
.zp_dedup
;
777 zio
->io_prop
.zp_nopwrite
= nopwrite
;
778 zio
->io_prop
.zp_copies
= copies
;
779 zio
->io_bp_override
= bp
;
783 zio_free(spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
)
785 metaslab_check_free(spa
, bp
);
788 * Frees that are for the currently-syncing txg, are not going to be
789 * deferred, and which will not need to do a read (i.e. not GANG or
790 * DEDUP), can be processed immediately. Otherwise, put them on the
791 * in-memory list for later processing.
793 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
) ||
794 txg
!= spa
->spa_syncing_txg
||
795 spa_sync_pass(spa
) >= zfs_sync_pass_deferred_free
) {
796 bplist_append(&spa
->spa_free_bplist
[txg
& TXG_MASK
], bp
);
798 VERIFY0(zio_wait(zio_free_sync(NULL
, spa
, txg
, bp
, 0)));
803 zio_free_sync(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
807 enum zio_stage stage
= ZIO_FREE_PIPELINE
;
809 dprintf_bp(bp
, "freeing in txg %llu, pass %u",
810 (longlong_t
)txg
, spa
->spa_sync_pass
);
812 ASSERT(!BP_IS_HOLE(bp
));
813 ASSERT(spa_syncing_txg(spa
) == txg
);
814 ASSERT(spa_sync_pass(spa
) < zfs_sync_pass_deferred_free
);
816 metaslab_check_free(spa
, bp
);
820 * GANG and DEDUP blocks can induce a read (for the gang block header,
821 * or the DDT), so issue them asynchronously so that this thread is
824 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
))
825 stage
|= ZIO_STAGE_ISSUE_ASYNC
;
827 zio
= zio_create(pio
, spa
, txg
, bp
, NULL
, BP_GET_PSIZE(bp
),
828 NULL
, NULL
, ZIO_TYPE_FREE
, ZIO_PRIORITY_NOW
, flags
,
829 NULL
, 0, NULL
, ZIO_STAGE_OPEN
, stage
);
836 zio_claim(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
837 zio_done_func_t
*done
, void *private, enum zio_flag flags
)
842 * A claim is an allocation of a specific block. Claims are needed
843 * to support immediate writes in the intent log. The issue is that
844 * immediate writes contain committed data, but in a txg that was
845 * *not* committed. Upon opening the pool after an unclean shutdown,
846 * the intent log claims all blocks that contain immediate write data
847 * so that the SPA knows they're in use.
849 * All claims *must* be resolved in the first txg -- before the SPA
850 * starts allocating blocks -- so that nothing is allocated twice.
851 * If txg == 0 we just verify that the block is claimable.
853 ASSERT3U(spa
->spa_uberblock
.ub_rootbp
.blk_birth
, <, spa_first_txg(spa
));
854 ASSERT(txg
== spa_first_txg(spa
) || txg
== 0);
855 ASSERT(!BP_GET_DEDUP(bp
) || !spa_writeable(spa
)); /* zdb(1M) */
857 zio
= zio_create(pio
, spa
, txg
, bp
, NULL
, BP_GET_PSIZE(bp
),
858 done
, private, ZIO_TYPE_CLAIM
, ZIO_PRIORITY_NOW
, flags
,
859 NULL
, 0, NULL
, ZIO_STAGE_OPEN
, ZIO_CLAIM_PIPELINE
);
865 zio_ioctl(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, int cmd
,
866 zio_done_func_t
*done
, void *private, int priority
, enum zio_flag flags
)
871 if (vd
->vdev_children
== 0) {
872 zio
= zio_create(pio
, spa
, 0, NULL
, NULL
, 0, done
, private,
873 ZIO_TYPE_IOCTL
, priority
, flags
, vd
, 0, NULL
,
874 ZIO_STAGE_OPEN
, ZIO_IOCTL_PIPELINE
);
878 zio
= zio_null(pio
, spa
, NULL
, NULL
, NULL
, flags
);
880 for (c
= 0; c
< vd
->vdev_children
; c
++)
881 zio_nowait(zio_ioctl(zio
, spa
, vd
->vdev_child
[c
], cmd
,
882 done
, private, priority
, flags
));
889 zio_read_phys(zio_t
*pio
, vdev_t
*vd
, uint64_t offset
, uint64_t size
,
890 void *data
, int checksum
, zio_done_func_t
*done
, void *private,
891 int priority
, enum zio_flag flags
, boolean_t labels
)
895 ASSERT(vd
->vdev_children
== 0);
896 ASSERT(!labels
|| offset
+ size
<= VDEV_LABEL_START_SIZE
||
897 offset
>= vd
->vdev_psize
- VDEV_LABEL_END_SIZE
);
898 ASSERT3U(offset
+ size
, <=, vd
->vdev_psize
);
900 zio
= zio_create(pio
, vd
->vdev_spa
, 0, NULL
, data
, size
, done
, private,
901 ZIO_TYPE_READ
, priority
, flags
, vd
, offset
, NULL
,
902 ZIO_STAGE_OPEN
, ZIO_READ_PHYS_PIPELINE
);
904 zio
->io_prop
.zp_checksum
= checksum
;
910 zio_write_phys(zio_t
*pio
, vdev_t
*vd
, uint64_t offset
, uint64_t size
,
911 void *data
, int checksum
, zio_done_func_t
*done
, void *private,
912 int priority
, enum zio_flag flags
, boolean_t labels
)
916 ASSERT(vd
->vdev_children
== 0);
917 ASSERT(!labels
|| offset
+ size
<= VDEV_LABEL_START_SIZE
||
918 offset
>= vd
->vdev_psize
- VDEV_LABEL_END_SIZE
);
919 ASSERT3U(offset
+ size
, <=, vd
->vdev_psize
);
921 zio
= zio_create(pio
, vd
->vdev_spa
, 0, NULL
, data
, size
, done
, private,
922 ZIO_TYPE_WRITE
, priority
, flags
, vd
, offset
, NULL
,
923 ZIO_STAGE_OPEN
, ZIO_WRITE_PHYS_PIPELINE
);
925 zio
->io_prop
.zp_checksum
= checksum
;
927 if (zio_checksum_table
[checksum
].ci_eck
) {
929 * zec checksums are necessarily destructive -- they modify
930 * the end of the write buffer to hold the verifier/checksum.
931 * Therefore, we must make a local copy in case the data is
932 * being written to multiple places in parallel.
934 void *wbuf
= zio_buf_alloc(size
);
935 bcopy(data
, wbuf
, size
);
936 zio_push_transform(zio
, wbuf
, size
, size
, NULL
);
943 * Create a child I/O to do some work for us.
946 zio_vdev_child_io(zio_t
*pio
, blkptr_t
*bp
, vdev_t
*vd
, uint64_t offset
,
947 void *data
, uint64_t size
, int type
, int priority
, enum zio_flag flags
,
948 zio_done_func_t
*done
, void *private)
950 enum zio_stage pipeline
= ZIO_VDEV_CHILD_PIPELINE
;
953 ASSERT(vd
->vdev_parent
==
954 (pio
->io_vd
? pio
->io_vd
: pio
->io_spa
->spa_root_vdev
));
956 if (type
== ZIO_TYPE_READ
&& bp
!= NULL
) {
958 * If we have the bp, then the child should perform the
959 * checksum and the parent need not. This pushes error
960 * detection as close to the leaves as possible and
961 * eliminates redundant checksums in the interior nodes.
963 pipeline
|= ZIO_STAGE_CHECKSUM_VERIFY
;
964 pio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
967 if (vd
->vdev_children
== 0)
968 offset
+= VDEV_LABEL_START_SIZE
;
970 flags
|= ZIO_VDEV_CHILD_FLAGS(pio
) | ZIO_FLAG_DONT_PROPAGATE
;
973 * If we've decided to do a repair, the write is not speculative --
974 * even if the original read was.
976 if (flags
& ZIO_FLAG_IO_REPAIR
)
977 flags
&= ~ZIO_FLAG_SPECULATIVE
;
979 zio
= zio_create(pio
, pio
->io_spa
, pio
->io_txg
, bp
, data
, size
,
980 done
, private, type
, priority
, flags
, vd
, offset
, &pio
->io_bookmark
,
981 ZIO_STAGE_VDEV_IO_START
>> 1, pipeline
);
987 zio_vdev_delegated_io(vdev_t
*vd
, uint64_t offset
, void *data
, uint64_t size
,
988 int type
, int priority
, enum zio_flag flags
,
989 zio_done_func_t
*done
, void *private)
993 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
995 zio
= zio_create(NULL
, vd
->vdev_spa
, 0, NULL
,
996 data
, size
, done
, private, type
, priority
,
997 flags
| ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_RETRY
,
999 ZIO_STAGE_VDEV_IO_START
>> 1, ZIO_VDEV_CHILD_PIPELINE
);
1005 zio_flush(zio_t
*zio
, vdev_t
*vd
)
1007 zio_nowait(zio_ioctl(zio
, zio
->io_spa
, vd
, DKIOCFLUSHWRITECACHE
,
1008 NULL
, NULL
, ZIO_PRIORITY_NOW
,
1009 ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
));
1013 zio_shrink(zio_t
*zio
, uint64_t size
)
1015 ASSERT(zio
->io_executor
== NULL
);
1016 ASSERT(zio
->io_orig_size
== zio
->io_size
);
1017 ASSERT(size
<= zio
->io_size
);
1020 * We don't shrink for raidz because of problems with the
1021 * reconstruction when reading back less than the block size.
1022 * Note, BP_IS_RAIDZ() assumes no compression.
1024 ASSERT(BP_GET_COMPRESS(zio
->io_bp
) == ZIO_COMPRESS_OFF
);
1025 if (!BP_IS_RAIDZ(zio
->io_bp
))
1026 zio
->io_orig_size
= zio
->io_size
= size
;
1030 * ==========================================================================
1031 * Prepare to read and write logical blocks
1032 * ==========================================================================
1036 zio_read_bp_init(zio_t
*zio
)
1038 blkptr_t
*bp
= zio
->io_bp
;
1040 if (BP_GET_COMPRESS(bp
) != ZIO_COMPRESS_OFF
&&
1041 zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1042 !(zio
->io_flags
& ZIO_FLAG_RAW
)) {
1043 uint64_t psize
= BP_GET_PSIZE(bp
);
1044 void *cbuf
= zio_buf_alloc(psize
);
1046 zio_push_transform(zio
, cbuf
, psize
, psize
, zio_decompress
);
1049 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) && BP_GET_LEVEL(bp
) == 0)
1050 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1052 if (BP_GET_TYPE(bp
) == DMU_OT_DDT_ZAP
)
1053 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1055 if (BP_GET_DEDUP(bp
) && zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
1056 zio
->io_pipeline
= ZIO_DDT_READ_PIPELINE
;
1058 return (ZIO_PIPELINE_CONTINUE
);
1062 zio_write_bp_init(zio_t
*zio
)
1064 spa_t
*spa
= zio
->io_spa
;
1065 zio_prop_t
*zp
= &zio
->io_prop
;
1066 enum zio_compress compress
= zp
->zp_compress
;
1067 blkptr_t
*bp
= zio
->io_bp
;
1068 uint64_t lsize
= zio
->io_size
;
1069 uint64_t psize
= lsize
;
1073 * If our children haven't all reached the ready stage,
1074 * wait for them and then repeat this pipeline stage.
1076 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_READY
) ||
1077 zio_wait_for_children(zio
, ZIO_CHILD_LOGICAL
, ZIO_WAIT_READY
))
1078 return (ZIO_PIPELINE_STOP
);
1080 if (!IO_IS_ALLOCATING(zio
))
1081 return (ZIO_PIPELINE_CONTINUE
);
1083 ASSERT(zio
->io_child_type
!= ZIO_CHILD_DDT
);
1085 if (zio
->io_bp_override
) {
1086 ASSERT(bp
->blk_birth
!= zio
->io_txg
);
1087 ASSERT(BP_GET_DEDUP(zio
->io_bp_override
) == 0);
1089 *bp
= *zio
->io_bp_override
;
1090 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1093 * If we've been overridden and nopwrite is set then
1094 * set the flag accordingly to indicate that a nopwrite
1095 * has already occurred.
1097 if (!BP_IS_HOLE(bp
) && zp
->zp_nopwrite
) {
1098 ASSERT(!zp
->zp_dedup
);
1099 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
1100 return (ZIO_PIPELINE_CONTINUE
);
1103 ASSERT(!zp
->zp_nopwrite
);
1105 if (BP_IS_HOLE(bp
) || !zp
->zp_dedup
)
1106 return (ZIO_PIPELINE_CONTINUE
);
1108 ASSERT(zio_checksum_table
[zp
->zp_checksum
].ci_dedup
||
1109 zp
->zp_dedup_verify
);
1111 if (BP_GET_CHECKSUM(bp
) == zp
->zp_checksum
) {
1112 BP_SET_DEDUP(bp
, 1);
1113 zio
->io_pipeline
|= ZIO_STAGE_DDT_WRITE
;
1114 return (ZIO_PIPELINE_CONTINUE
);
1116 zio
->io_bp_override
= NULL
;
1120 if (bp
->blk_birth
== zio
->io_txg
) {
1122 * We're rewriting an existing block, which means we're
1123 * working on behalf of spa_sync(). For spa_sync() to
1124 * converge, it must eventually be the case that we don't
1125 * have to allocate new blocks. But compression changes
1126 * the blocksize, which forces a reallocate, and makes
1127 * convergence take longer. Therefore, after the first
1128 * few passes, stop compressing to ensure convergence.
1130 pass
= spa_sync_pass(spa
);
1132 ASSERT(zio
->io_txg
== spa_syncing_txg(spa
));
1133 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1134 ASSERT(!BP_GET_DEDUP(bp
));
1136 if (pass
>= zfs_sync_pass_dont_compress
)
1137 compress
= ZIO_COMPRESS_OFF
;
1139 /* Make sure someone doesn't change their mind on overwrites */
1140 ASSERT(MIN(zp
->zp_copies
+ BP_IS_GANG(bp
),
1141 spa_max_replication(spa
)) == BP_GET_NDVAS(bp
));
1144 if (compress
!= ZIO_COMPRESS_OFF
) {
1145 void *cbuf
= zio_buf_alloc(lsize
);
1146 psize
= zio_compress_data(compress
, zio
->io_data
, cbuf
, lsize
);
1147 if (psize
== 0 || psize
== lsize
) {
1148 compress
= ZIO_COMPRESS_OFF
;
1149 zio_buf_free(cbuf
, lsize
);
1151 ASSERT(psize
< lsize
);
1152 zio_push_transform(zio
, cbuf
, psize
, lsize
, NULL
);
1157 * The final pass of spa_sync() must be all rewrites, but the first
1158 * few passes offer a trade-off: allocating blocks defers convergence,
1159 * but newly allocated blocks are sequential, so they can be written
1160 * to disk faster. Therefore, we allow the first few passes of
1161 * spa_sync() to allocate new blocks, but force rewrites after that.
1162 * There should only be a handful of blocks after pass 1 in any case.
1164 if (bp
->blk_birth
== zio
->io_txg
&& BP_GET_PSIZE(bp
) == psize
&&
1165 pass
>= zfs_sync_pass_rewrite
) {
1166 enum zio_stage gang_stages
= zio
->io_pipeline
& ZIO_GANG_STAGES
;
1168 zio
->io_pipeline
= ZIO_REWRITE_PIPELINE
| gang_stages
;
1169 zio
->io_flags
|= ZIO_FLAG_IO_REWRITE
;
1172 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
1176 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1178 ASSERT(zp
->zp_checksum
!= ZIO_CHECKSUM_GANG_HEADER
);
1179 BP_SET_LSIZE(bp
, lsize
);
1180 BP_SET_PSIZE(bp
, psize
);
1181 BP_SET_COMPRESS(bp
, compress
);
1182 BP_SET_CHECKSUM(bp
, zp
->zp_checksum
);
1183 BP_SET_TYPE(bp
, zp
->zp_type
);
1184 BP_SET_LEVEL(bp
, zp
->zp_level
);
1185 BP_SET_DEDUP(bp
, zp
->zp_dedup
);
1186 BP_SET_BYTEORDER(bp
, ZFS_HOST_BYTEORDER
);
1188 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1189 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1190 zio
->io_pipeline
= ZIO_DDT_WRITE_PIPELINE
;
1192 if (zp
->zp_nopwrite
) {
1193 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1194 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1195 zio
->io_pipeline
|= ZIO_STAGE_NOP_WRITE
;
1199 return (ZIO_PIPELINE_CONTINUE
);
1203 zio_free_bp_init(zio_t
*zio
)
1205 blkptr_t
*bp
= zio
->io_bp
;
1207 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
) {
1208 if (BP_GET_DEDUP(bp
))
1209 zio
->io_pipeline
= ZIO_DDT_FREE_PIPELINE
;
1212 return (ZIO_PIPELINE_CONTINUE
);
1216 * ==========================================================================
1217 * Execute the I/O pipeline
1218 * ==========================================================================
1222 zio_taskq_dispatch(zio_t
*zio
, zio_taskq_type_t q
, boolean_t cutinline
)
1224 spa_t
*spa
= zio
->io_spa
;
1225 zio_type_t t
= zio
->io_type
;
1226 int flags
= (cutinline
? TQ_FRONT
: 0);
1229 * If we're a config writer or a probe, the normal issue and
1230 * interrupt threads may all be blocked waiting for the config lock.
1231 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1233 if (zio
->io_flags
& (ZIO_FLAG_CONFIG_WRITER
| ZIO_FLAG_PROBE
))
1237 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1239 if (t
== ZIO_TYPE_WRITE
&& zio
->io_vd
&& zio
->io_vd
->vdev_aux
)
1243 * If this is a high priority I/O, then use the high priority taskq if
1246 if (zio
->io_priority
== ZIO_PRIORITY_NOW
&&
1247 spa
->spa_zio_taskq
[t
][q
+ 1].stqs_count
!= 0)
1250 ASSERT3U(q
, <, ZIO_TASKQ_TYPES
);
1253 * NB: We are assuming that the zio can only be dispatched
1254 * to a single taskq at a time. It would be a grievous error
1255 * to dispatch the zio to another taskq at the same time.
1257 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
1258 spa_taskq_dispatch_ent(spa
, t
, q
, (task_func_t
*)zio_execute
, zio
,
1259 flags
, &zio
->io_tqent
);
1263 zio_taskq_member(zio_t
*zio
, zio_taskq_type_t q
)
1265 kthread_t
*executor
= zio
->io_executor
;
1266 spa_t
*spa
= zio
->io_spa
;
1269 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1270 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1272 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
1273 if (taskq_member(tqs
->stqs_taskq
[i
], executor
))
1282 zio_issue_async(zio_t
*zio
)
1284 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
1286 return (ZIO_PIPELINE_STOP
);
1290 zio_interrupt(zio_t
*zio
)
1292 zio_taskq_dispatch(zio
, ZIO_TASKQ_INTERRUPT
, B_FALSE
);
1296 * Execute the I/O pipeline until one of the following occurs:
1297 * (1) the I/O completes; (2) the pipeline stalls waiting for
1298 * dependent child I/Os; (3) the I/O issues, so we're waiting
1299 * for an I/O completion interrupt; (4) the I/O is delegated by
1300 * vdev-level caching or aggregation; (5) the I/O is deferred
1301 * due to vdev-level queueing; (6) the I/O is handed off to
1302 * another thread. In all cases, the pipeline stops whenever
1303 * there's no CPU work; it never burns a thread in cv_wait_io().
1305 * There's no locking on io_stage because there's no legitimate way
1306 * for multiple threads to be attempting to process the same I/O.
1308 static zio_pipe_stage_t
*zio_pipeline
[];
1311 * zio_execute() is a wrapper around the static function
1312 * __zio_execute() so that we can force __zio_execute() to be
1313 * inlined. This reduces stack overhead which is important
1314 * because __zio_execute() is called recursively in several zio
1315 * code paths. zio_execute() itself cannot be inlined because
1316 * it is externally visible.
1319 zio_execute(zio_t
*zio
)
1324 __attribute__((always_inline
))
1326 __zio_execute(zio_t
*zio
)
1328 zio
->io_executor
= curthread
;
1330 while (zio
->io_stage
< ZIO_STAGE_DONE
) {
1331 enum zio_stage pipeline
= zio
->io_pipeline
;
1332 enum zio_stage stage
= zio
->io_stage
;
1337 ASSERT(!MUTEX_HELD(&zio
->io_lock
));
1338 ASSERT(ISP2(stage
));
1339 ASSERT(zio
->io_stall
== NULL
);
1343 } while ((stage
& pipeline
) == 0);
1345 ASSERT(stage
<= ZIO_STAGE_DONE
);
1347 dp
= spa_get_dsl(zio
->io_spa
);
1348 cut
= (stage
== ZIO_STAGE_VDEV_IO_START
) ?
1349 zio_requeue_io_start_cut_in_line
: B_FALSE
;
1352 * If we are in interrupt context and this pipeline stage
1353 * will grab a config lock that is held across I/O,
1354 * or may wait for an I/O that needs an interrupt thread
1355 * to complete, issue async to avoid deadlock.
1357 * For VDEV_IO_START, we cut in line so that the io will
1358 * be sent to disk promptly.
1360 if ((stage
& ZIO_BLOCKING_STAGES
) && zio
->io_vd
== NULL
&&
1361 zio_taskq_member(zio
, ZIO_TASKQ_INTERRUPT
)) {
1362 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, cut
);
1368 * If we executing in the context of the tx_sync_thread,
1369 * or we are performing pool initialization outside of a
1370 * zio_taskq[ZIO_TASKQ_ISSUE|ZIO_TASKQ_ISSUE_HIGH] context.
1371 * Then issue the zio asynchronously to minimize stack usage
1372 * for these deep call paths.
1374 if ((dp
&& curthread
== dp
->dp_tx
.tx_sync_thread
) ||
1375 (dp
&& spa_is_initializing(dp
->dp_spa
) &&
1376 !zio_taskq_member(zio
, ZIO_TASKQ_ISSUE
) &&
1377 !zio_taskq_member(zio
, ZIO_TASKQ_ISSUE_HIGH
))) {
1378 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, cut
);
1383 zio
->io_stage
= stage
;
1384 rv
= zio_pipeline
[highbit(stage
) - 1](zio
);
1386 if (rv
== ZIO_PIPELINE_STOP
)
1389 ASSERT(rv
== ZIO_PIPELINE_CONTINUE
);
1395 * ==========================================================================
1396 * Initiate I/O, either sync or async
1397 * ==========================================================================
1400 zio_wait(zio_t
*zio
)
1404 ASSERT(zio
->io_stage
== ZIO_STAGE_OPEN
);
1405 ASSERT(zio
->io_executor
== NULL
);
1407 zio
->io_waiter
= curthread
;
1411 mutex_enter(&zio
->io_lock
);
1412 while (zio
->io_executor
!= NULL
)
1413 cv_wait_io(&zio
->io_cv
, &zio
->io_lock
);
1414 mutex_exit(&zio
->io_lock
);
1416 error
= zio
->io_error
;
1423 zio_nowait(zio_t
*zio
)
1425 ASSERT(zio
->io_executor
== NULL
);
1427 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1428 zio_unique_parent(zio
) == NULL
) {
1430 * This is a logical async I/O with no parent to wait for it.
1431 * We add it to the spa_async_root_zio "Godfather" I/O which
1432 * will ensure they complete prior to unloading the pool.
1434 spa_t
*spa
= zio
->io_spa
;
1436 zio_add_child(spa
->spa_async_zio_root
, zio
);
1443 * ==========================================================================
1444 * Reexecute or suspend/resume failed I/O
1445 * ==========================================================================
1449 zio_reexecute(zio_t
*pio
)
1451 zio_t
*cio
, *cio_next
;
1454 ASSERT(pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1455 ASSERT(pio
->io_orig_stage
== ZIO_STAGE_OPEN
);
1456 ASSERT(pio
->io_gang_leader
== NULL
);
1457 ASSERT(pio
->io_gang_tree
== NULL
);
1459 pio
->io_flags
= pio
->io_orig_flags
;
1460 pio
->io_stage
= pio
->io_orig_stage
;
1461 pio
->io_pipeline
= pio
->io_orig_pipeline
;
1462 pio
->io_reexecute
= 0;
1463 pio
->io_flags
|= ZIO_FLAG_REEXECUTED
;
1465 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
1466 pio
->io_state
[w
] = 0;
1467 for (c
= 0; c
< ZIO_CHILD_TYPES
; c
++)
1468 pio
->io_child_error
[c
] = 0;
1470 if (IO_IS_ALLOCATING(pio
))
1471 BP_ZERO(pio
->io_bp
);
1474 * As we reexecute pio's children, new children could be created.
1475 * New children go to the head of pio's io_child_list, however,
1476 * so we will (correctly) not reexecute them. The key is that
1477 * the remainder of pio's io_child_list, from 'cio_next' onward,
1478 * cannot be affected by any side effects of reexecuting 'cio'.
1480 for (cio
= zio_walk_children(pio
); cio
!= NULL
; cio
= cio_next
) {
1481 cio_next
= zio_walk_children(pio
);
1482 mutex_enter(&pio
->io_lock
);
1483 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
1484 pio
->io_children
[cio
->io_child_type
][w
]++;
1485 mutex_exit(&pio
->io_lock
);
1490 * Now that all children have been reexecuted, execute the parent.
1491 * We don't reexecute "The Godfather" I/O here as it's the
1492 * responsibility of the caller to wait on him.
1494 if (!(pio
->io_flags
& ZIO_FLAG_GODFATHER
))
1499 zio_suspend(spa_t
*spa
, zio_t
*zio
)
1501 if (spa_get_failmode(spa
) == ZIO_FAILURE_MODE_PANIC
)
1502 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1503 "failure and the failure mode property for this pool "
1504 "is set to panic.", spa_name(spa
));
1506 cmn_err(CE_WARN
, "Pool '%s' has encountered an uncorrectable I/O "
1507 "failure and has been suspended.\n", spa_name(spa
));
1509 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE
, spa
, NULL
, NULL
, 0, 0);
1511 mutex_enter(&spa
->spa_suspend_lock
);
1513 if (spa
->spa_suspend_zio_root
== NULL
)
1514 spa
->spa_suspend_zio_root
= zio_root(spa
, NULL
, NULL
,
1515 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
1516 ZIO_FLAG_GODFATHER
);
1518 spa
->spa_suspended
= B_TRUE
;
1521 ASSERT(!(zio
->io_flags
& ZIO_FLAG_GODFATHER
));
1522 ASSERT(zio
!= spa
->spa_suspend_zio_root
);
1523 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1524 ASSERT(zio_unique_parent(zio
) == NULL
);
1525 ASSERT(zio
->io_stage
== ZIO_STAGE_DONE
);
1526 zio_add_child(spa
->spa_suspend_zio_root
, zio
);
1529 mutex_exit(&spa
->spa_suspend_lock
);
1533 zio_resume(spa_t
*spa
)
1538 * Reexecute all previously suspended i/o.
1540 mutex_enter(&spa
->spa_suspend_lock
);
1541 spa
->spa_suspended
= B_FALSE
;
1542 cv_broadcast(&spa
->spa_suspend_cv
);
1543 pio
= spa
->spa_suspend_zio_root
;
1544 spa
->spa_suspend_zio_root
= NULL
;
1545 mutex_exit(&spa
->spa_suspend_lock
);
1551 return (zio_wait(pio
));
1555 zio_resume_wait(spa_t
*spa
)
1557 mutex_enter(&spa
->spa_suspend_lock
);
1558 while (spa_suspended(spa
))
1559 cv_wait(&spa
->spa_suspend_cv
, &spa
->spa_suspend_lock
);
1560 mutex_exit(&spa
->spa_suspend_lock
);
1564 * ==========================================================================
1567 * A gang block is a collection of small blocks that looks to the DMU
1568 * like one large block. When zio_dva_allocate() cannot find a block
1569 * of the requested size, due to either severe fragmentation or the pool
1570 * being nearly full, it calls zio_write_gang_block() to construct the
1571 * block from smaller fragments.
1573 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1574 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1575 * an indirect block: it's an array of block pointers. It consumes
1576 * only one sector and hence is allocatable regardless of fragmentation.
1577 * The gang header's bps point to its gang members, which hold the data.
1579 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1580 * as the verifier to ensure uniqueness of the SHA256 checksum.
1581 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1582 * not the gang header. This ensures that data block signatures (needed for
1583 * deduplication) are independent of how the block is physically stored.
1585 * Gang blocks can be nested: a gang member may itself be a gang block.
1586 * Thus every gang block is a tree in which root and all interior nodes are
1587 * gang headers, and the leaves are normal blocks that contain user data.
1588 * The root of the gang tree is called the gang leader.
1590 * To perform any operation (read, rewrite, free, claim) on a gang block,
1591 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1592 * in the io_gang_tree field of the original logical i/o by recursively
1593 * reading the gang leader and all gang headers below it. This yields
1594 * an in-core tree containing the contents of every gang header and the
1595 * bps for every constituent of the gang block.
1597 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1598 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1599 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1600 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1601 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1602 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1603 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1604 * of the gang header plus zio_checksum_compute() of the data to update the
1605 * gang header's blk_cksum as described above.
1607 * The two-phase assemble/issue model solves the problem of partial failure --
1608 * what if you'd freed part of a gang block but then couldn't read the
1609 * gang header for another part? Assembling the entire gang tree first
1610 * ensures that all the necessary gang header I/O has succeeded before
1611 * starting the actual work of free, claim, or write. Once the gang tree
1612 * is assembled, free and claim are in-memory operations that cannot fail.
1614 * In the event that a gang write fails, zio_dva_unallocate() walks the
1615 * gang tree to immediately free (i.e. insert back into the space map)
1616 * everything we've allocated. This ensures that we don't get ENOSPC
1617 * errors during repeated suspend/resume cycles due to a flaky device.
1619 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1620 * the gang tree, we won't modify the block, so we can safely defer the free
1621 * (knowing that the block is still intact). If we *can* assemble the gang
1622 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1623 * each constituent bp and we can allocate a new block on the next sync pass.
1625 * In all cases, the gang tree allows complete recovery from partial failure.
1626 * ==========================================================================
1630 zio_read_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1635 return (zio_read(pio
, pio
->io_spa
, bp
, data
, BP_GET_PSIZE(bp
),
1636 NULL
, NULL
, pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
),
1637 &pio
->io_bookmark
));
1641 zio_rewrite_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1646 zio
= zio_rewrite(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1647 gn
->gn_gbh
, SPA_GANGBLOCKSIZE
, NULL
, NULL
, pio
->io_priority
,
1648 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
1650 * As we rewrite each gang header, the pipeline will compute
1651 * a new gang block header checksum for it; but no one will
1652 * compute a new data checksum, so we do that here. The one
1653 * exception is the gang leader: the pipeline already computed
1654 * its data checksum because that stage precedes gang assembly.
1655 * (Presently, nothing actually uses interior data checksums;
1656 * this is just good hygiene.)
1658 if (gn
!= pio
->io_gang_leader
->io_gang_tree
) {
1659 zio_checksum_compute(zio
, BP_GET_CHECKSUM(bp
),
1660 data
, BP_GET_PSIZE(bp
));
1663 * If we are here to damage data for testing purposes,
1664 * leave the GBH alone so that we can detect the damage.
1666 if (pio
->io_gang_leader
->io_flags
& ZIO_FLAG_INDUCE_DAMAGE
)
1667 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
1669 zio
= zio_rewrite(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1670 data
, BP_GET_PSIZE(bp
), NULL
, NULL
, pio
->io_priority
,
1671 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
1679 zio_free_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1681 return (zio_free_sync(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1682 ZIO_GANG_CHILD_FLAGS(pio
)));
1687 zio_claim_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1689 return (zio_claim(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1690 NULL
, NULL
, ZIO_GANG_CHILD_FLAGS(pio
)));
1693 static zio_gang_issue_func_t
*zio_gang_issue_func
[ZIO_TYPES
] = {
1702 static void zio_gang_tree_assemble_done(zio_t
*zio
);
1704 static zio_gang_node_t
*
1705 zio_gang_node_alloc(zio_gang_node_t
**gnpp
)
1707 zio_gang_node_t
*gn
;
1709 ASSERT(*gnpp
== NULL
);
1711 gn
= kmem_zalloc(sizeof (*gn
), KM_PUSHPAGE
);
1712 gn
->gn_gbh
= zio_buf_alloc(SPA_GANGBLOCKSIZE
);
1719 zio_gang_node_free(zio_gang_node_t
**gnpp
)
1721 zio_gang_node_t
*gn
= *gnpp
;
1724 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
1725 ASSERT(gn
->gn_child
[g
] == NULL
);
1727 zio_buf_free(gn
->gn_gbh
, SPA_GANGBLOCKSIZE
);
1728 kmem_free(gn
, sizeof (*gn
));
1733 zio_gang_tree_free(zio_gang_node_t
**gnpp
)
1735 zio_gang_node_t
*gn
= *gnpp
;
1741 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
1742 zio_gang_tree_free(&gn
->gn_child
[g
]);
1744 zio_gang_node_free(gnpp
);
1748 zio_gang_tree_assemble(zio_t
*gio
, blkptr_t
*bp
, zio_gang_node_t
**gnpp
)
1750 zio_gang_node_t
*gn
= zio_gang_node_alloc(gnpp
);
1752 ASSERT(gio
->io_gang_leader
== gio
);
1753 ASSERT(BP_IS_GANG(bp
));
1755 zio_nowait(zio_read(gio
, gio
->io_spa
, bp
, gn
->gn_gbh
,
1756 SPA_GANGBLOCKSIZE
, zio_gang_tree_assemble_done
, gn
,
1757 gio
->io_priority
, ZIO_GANG_CHILD_FLAGS(gio
), &gio
->io_bookmark
));
1761 zio_gang_tree_assemble_done(zio_t
*zio
)
1763 zio_t
*gio
= zio
->io_gang_leader
;
1764 zio_gang_node_t
*gn
= zio
->io_private
;
1765 blkptr_t
*bp
= zio
->io_bp
;
1768 ASSERT(gio
== zio_unique_parent(zio
));
1769 ASSERT(zio
->io_child_count
== 0);
1774 if (BP_SHOULD_BYTESWAP(bp
))
1775 byteswap_uint64_array(zio
->io_data
, zio
->io_size
);
1777 ASSERT(zio
->io_data
== gn
->gn_gbh
);
1778 ASSERT(zio
->io_size
== SPA_GANGBLOCKSIZE
);
1779 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
1781 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
1782 blkptr_t
*gbp
= &gn
->gn_gbh
->zg_blkptr
[g
];
1783 if (!BP_IS_GANG(gbp
))
1785 zio_gang_tree_assemble(gio
, gbp
, &gn
->gn_child
[g
]);
1790 zio_gang_tree_issue(zio_t
*pio
, zio_gang_node_t
*gn
, blkptr_t
*bp
, void *data
)
1792 zio_t
*gio
= pio
->io_gang_leader
;
1796 ASSERT(BP_IS_GANG(bp
) == !!gn
);
1797 ASSERT(BP_GET_CHECKSUM(bp
) == BP_GET_CHECKSUM(gio
->io_bp
));
1798 ASSERT(BP_GET_LSIZE(bp
) == BP_GET_PSIZE(bp
) || gn
== gio
->io_gang_tree
);
1801 * If you're a gang header, your data is in gn->gn_gbh.
1802 * If you're a gang member, your data is in 'data' and gn == NULL.
1804 zio
= zio_gang_issue_func
[gio
->io_type
](pio
, bp
, gn
, data
);
1807 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
1809 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
1810 blkptr_t
*gbp
= &gn
->gn_gbh
->zg_blkptr
[g
];
1811 if (BP_IS_HOLE(gbp
))
1813 zio_gang_tree_issue(zio
, gn
->gn_child
[g
], gbp
, data
);
1814 data
= (char *)data
+ BP_GET_PSIZE(gbp
);
1818 if (gn
== gio
->io_gang_tree
)
1819 ASSERT3P((char *)gio
->io_data
+ gio
->io_size
, ==, data
);
1826 zio_gang_assemble(zio_t
*zio
)
1828 blkptr_t
*bp
= zio
->io_bp
;
1830 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== NULL
);
1831 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
1833 zio
->io_gang_leader
= zio
;
1835 zio_gang_tree_assemble(zio
, bp
, &zio
->io_gang_tree
);
1837 return (ZIO_PIPELINE_CONTINUE
);
1841 zio_gang_issue(zio_t
*zio
)
1843 blkptr_t
*bp
= zio
->io_bp
;
1845 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_DONE
))
1846 return (ZIO_PIPELINE_STOP
);
1848 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== zio
);
1849 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
1851 if (zio
->io_child_error
[ZIO_CHILD_GANG
] == 0)
1852 zio_gang_tree_issue(zio
, zio
->io_gang_tree
, bp
, zio
->io_data
);
1854 zio_gang_tree_free(&zio
->io_gang_tree
);
1856 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1858 return (ZIO_PIPELINE_CONTINUE
);
1862 zio_write_gang_member_ready(zio_t
*zio
)
1864 zio_t
*pio
= zio_unique_parent(zio
);
1865 ASSERTV(zio_t
*gio
= zio
->io_gang_leader
;)
1866 dva_t
*cdva
= zio
->io_bp
->blk_dva
;
1867 dva_t
*pdva
= pio
->io_bp
->blk_dva
;
1871 if (BP_IS_HOLE(zio
->io_bp
))
1874 ASSERT(BP_IS_HOLE(&zio
->io_bp_orig
));
1876 ASSERT(zio
->io_child_type
== ZIO_CHILD_GANG
);
1877 ASSERT3U(zio
->io_prop
.zp_copies
, ==, gio
->io_prop
.zp_copies
);
1878 ASSERT3U(zio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(zio
->io_bp
));
1879 ASSERT3U(pio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(pio
->io_bp
));
1880 ASSERT3U(BP_GET_NDVAS(zio
->io_bp
), <=, BP_GET_NDVAS(pio
->io_bp
));
1882 mutex_enter(&pio
->io_lock
);
1883 for (d
= 0; d
< BP_GET_NDVAS(zio
->io_bp
); d
++) {
1884 ASSERT(DVA_GET_GANG(&pdva
[d
]));
1885 asize
= DVA_GET_ASIZE(&pdva
[d
]);
1886 asize
+= DVA_GET_ASIZE(&cdva
[d
]);
1887 DVA_SET_ASIZE(&pdva
[d
], asize
);
1889 mutex_exit(&pio
->io_lock
);
1893 zio_write_gang_block(zio_t
*pio
)
1895 spa_t
*spa
= pio
->io_spa
;
1896 blkptr_t
*bp
= pio
->io_bp
;
1897 zio_t
*gio
= pio
->io_gang_leader
;
1899 zio_gang_node_t
*gn
, **gnpp
;
1900 zio_gbh_phys_t
*gbh
;
1901 uint64_t txg
= pio
->io_txg
;
1902 uint64_t resid
= pio
->io_size
;
1904 int copies
= gio
->io_prop
.zp_copies
;
1905 int gbh_copies
= MIN(copies
+ 1, spa_max_replication(spa
));
1909 error
= metaslab_alloc(spa
, spa_normal_class(spa
), SPA_GANGBLOCKSIZE
,
1910 bp
, gbh_copies
, txg
, pio
== gio
? NULL
: gio
->io_bp
,
1911 METASLAB_HINTBP_FAVOR
| METASLAB_GANG_HEADER
);
1913 pio
->io_error
= error
;
1914 return (ZIO_PIPELINE_CONTINUE
);
1918 gnpp
= &gio
->io_gang_tree
;
1920 gnpp
= pio
->io_private
;
1921 ASSERT(pio
->io_ready
== zio_write_gang_member_ready
);
1924 gn
= zio_gang_node_alloc(gnpp
);
1926 bzero(gbh
, SPA_GANGBLOCKSIZE
);
1929 * Create the gang header.
1931 zio
= zio_rewrite(pio
, spa
, txg
, bp
, gbh
, SPA_GANGBLOCKSIZE
, NULL
, NULL
,
1932 pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
1935 * Create and nowait the gang children.
1937 for (g
= 0; resid
!= 0; resid
-= lsize
, g
++) {
1938 lsize
= P2ROUNDUP(resid
/ (SPA_GBH_NBLKPTRS
- g
),
1940 ASSERT(lsize
>= SPA_MINBLOCKSIZE
&& lsize
<= resid
);
1942 zp
.zp_checksum
= gio
->io_prop
.zp_checksum
;
1943 zp
.zp_compress
= ZIO_COMPRESS_OFF
;
1944 zp
.zp_type
= DMU_OT_NONE
;
1946 zp
.zp_copies
= gio
->io_prop
.zp_copies
;
1947 zp
.zp_dedup
= B_FALSE
;
1948 zp
.zp_dedup_verify
= B_FALSE
;
1949 zp
.zp_nopwrite
= B_FALSE
;
1951 zio_nowait(zio_write(zio
, spa
, txg
, &gbh
->zg_blkptr
[g
],
1952 (char *)pio
->io_data
+ (pio
->io_size
- resid
), lsize
, &zp
,
1953 zio_write_gang_member_ready
, NULL
, &gn
->gn_child
[g
],
1954 pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
),
1955 &pio
->io_bookmark
));
1959 * Set pio's pipeline to just wait for zio to finish.
1961 pio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1964 * We didn't allocate this bp, so make sure it doesn't get unmarked.
1966 pio
->io_flags
&= ~ZIO_FLAG_FASTWRITE
;
1970 return (ZIO_PIPELINE_CONTINUE
);
1974 * The zio_nop_write stage in the pipeline determines if allocating
1975 * a new bp is necessary. By leveraging a cryptographically secure checksum,
1976 * such as SHA256, we can compare the checksums of the new data and the old
1977 * to determine if allocating a new block is required. The nopwrite
1978 * feature can handle writes in either syncing or open context (i.e. zil
1979 * writes) and as a result is mutually exclusive with dedup.
1982 zio_nop_write(zio_t
*zio
)
1984 blkptr_t
*bp
= zio
->io_bp
;
1985 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1986 zio_prop_t
*zp
= &zio
->io_prop
;
1988 ASSERT(BP_GET_LEVEL(bp
) == 0);
1989 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1990 ASSERT(zp
->zp_nopwrite
);
1991 ASSERT(!zp
->zp_dedup
);
1992 ASSERT(zio
->io_bp_override
== NULL
);
1993 ASSERT(IO_IS_ALLOCATING(zio
));
1996 * Check to see if the original bp and the new bp have matching
1997 * characteristics (i.e. same checksum, compression algorithms, etc).
1998 * If they don't then just continue with the pipeline which will
1999 * allocate a new bp.
2001 if (BP_IS_HOLE(bp_orig
) ||
2002 !zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_dedup
||
2003 BP_GET_CHECKSUM(bp
) != BP_GET_CHECKSUM(bp_orig
) ||
2004 BP_GET_COMPRESS(bp
) != BP_GET_COMPRESS(bp_orig
) ||
2005 BP_GET_DEDUP(bp
) != BP_GET_DEDUP(bp_orig
) ||
2006 zp
->zp_copies
!= BP_GET_NDVAS(bp_orig
))
2007 return (ZIO_PIPELINE_CONTINUE
);
2010 * If the checksums match then reset the pipeline so that we
2011 * avoid allocating a new bp and issuing any I/O.
2013 if (ZIO_CHECKSUM_EQUAL(bp
->blk_cksum
, bp_orig
->blk_cksum
)) {
2014 ASSERT(zio_checksum_table
[zp
->zp_checksum
].ci_dedup
);
2015 ASSERT3U(BP_GET_PSIZE(bp
), ==, BP_GET_PSIZE(bp_orig
));
2016 ASSERT3U(BP_GET_LSIZE(bp
), ==, BP_GET_LSIZE(bp_orig
));
2017 ASSERT(zp
->zp_compress
!= ZIO_COMPRESS_OFF
);
2018 ASSERT(bcmp(&bp
->blk_prop
, &bp_orig
->blk_prop
,
2019 sizeof (uint64_t)) == 0);
2022 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2023 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
2026 return (ZIO_PIPELINE_CONTINUE
);
2030 * ==========================================================================
2032 * ==========================================================================
2035 zio_ddt_child_read_done(zio_t
*zio
)
2037 blkptr_t
*bp
= zio
->io_bp
;
2038 ddt_entry_t
*dde
= zio
->io_private
;
2040 zio_t
*pio
= zio_unique_parent(zio
);
2042 mutex_enter(&pio
->io_lock
);
2043 ddp
= ddt_phys_select(dde
, bp
);
2044 if (zio
->io_error
== 0)
2045 ddt_phys_clear(ddp
); /* this ddp doesn't need repair */
2046 if (zio
->io_error
== 0 && dde
->dde_repair_data
== NULL
)
2047 dde
->dde_repair_data
= zio
->io_data
;
2049 zio_buf_free(zio
->io_data
, zio
->io_size
);
2050 mutex_exit(&pio
->io_lock
);
2054 zio_ddt_read_start(zio_t
*zio
)
2056 blkptr_t
*bp
= zio
->io_bp
;
2059 ASSERT(BP_GET_DEDUP(bp
));
2060 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2061 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2063 if (zio
->io_child_error
[ZIO_CHILD_DDT
]) {
2064 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2065 ddt_entry_t
*dde
= ddt_repair_start(ddt
, bp
);
2066 ddt_phys_t
*ddp
= dde
->dde_phys
;
2067 ddt_phys_t
*ddp_self
= ddt_phys_select(dde
, bp
);
2070 ASSERT(zio
->io_vsd
== NULL
);
2073 if (ddp_self
== NULL
)
2074 return (ZIO_PIPELINE_CONTINUE
);
2076 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
2077 if (ddp
->ddp_phys_birth
== 0 || ddp
== ddp_self
)
2079 ddt_bp_create(ddt
->ddt_checksum
, &dde
->dde_key
, ddp
,
2081 zio_nowait(zio_read(zio
, zio
->io_spa
, &blk
,
2082 zio_buf_alloc(zio
->io_size
), zio
->io_size
,
2083 zio_ddt_child_read_done
, dde
, zio
->io_priority
,
2084 ZIO_DDT_CHILD_FLAGS(zio
) | ZIO_FLAG_DONT_PROPAGATE
,
2085 &zio
->io_bookmark
));
2087 return (ZIO_PIPELINE_CONTINUE
);
2090 zio_nowait(zio_read(zio
, zio
->io_spa
, bp
,
2091 zio
->io_data
, zio
->io_size
, NULL
, NULL
, zio
->io_priority
,
2092 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
));
2094 return (ZIO_PIPELINE_CONTINUE
);
2098 zio_ddt_read_done(zio_t
*zio
)
2100 blkptr_t
*bp
= zio
->io_bp
;
2102 if (zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_DONE
))
2103 return (ZIO_PIPELINE_STOP
);
2105 ASSERT(BP_GET_DEDUP(bp
));
2106 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2107 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2109 if (zio
->io_child_error
[ZIO_CHILD_DDT
]) {
2110 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2111 ddt_entry_t
*dde
= zio
->io_vsd
;
2113 ASSERT(spa_load_state(zio
->io_spa
) != SPA_LOAD_NONE
);
2114 return (ZIO_PIPELINE_CONTINUE
);
2117 zio
->io_stage
= ZIO_STAGE_DDT_READ_START
>> 1;
2118 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
2119 return (ZIO_PIPELINE_STOP
);
2121 if (dde
->dde_repair_data
!= NULL
) {
2122 bcopy(dde
->dde_repair_data
, zio
->io_data
, zio
->io_size
);
2123 zio
->io_child_error
[ZIO_CHILD_DDT
] = 0;
2125 ddt_repair_done(ddt
, dde
);
2129 ASSERT(zio
->io_vsd
== NULL
);
2131 return (ZIO_PIPELINE_CONTINUE
);
2135 zio_ddt_collision(zio_t
*zio
, ddt_t
*ddt
, ddt_entry_t
*dde
)
2137 spa_t
*spa
= zio
->io_spa
;
2141 * Note: we compare the original data, not the transformed data,
2142 * because when zio->io_bp is an override bp, we will not have
2143 * pushed the I/O transforms. That's an important optimization
2144 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2146 for (p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2147 zio_t
*lio
= dde
->dde_lead_zio
[p
];
2150 return (lio
->io_orig_size
!= zio
->io_orig_size
||
2151 bcmp(zio
->io_orig_data
, lio
->io_orig_data
,
2152 zio
->io_orig_size
) != 0);
2156 for (p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2157 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2159 if (ddp
->ddp_phys_birth
!= 0) {
2160 arc_buf_t
*abuf
= NULL
;
2161 uint32_t aflags
= ARC_WAIT
;
2162 blkptr_t blk
= *zio
->io_bp
;
2165 ddt_bp_fill(ddp
, &blk
, ddp
->ddp_phys_birth
);
2169 error
= arc_read(NULL
, spa
, &blk
,
2170 arc_getbuf_func
, &abuf
, ZIO_PRIORITY_SYNC_READ
,
2171 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2172 &aflags
, &zio
->io_bookmark
);
2175 if (arc_buf_size(abuf
) != zio
->io_orig_size
||
2176 bcmp(abuf
->b_data
, zio
->io_orig_data
,
2177 zio
->io_orig_size
) != 0)
2178 error
= SET_ERROR(EEXIST
);
2179 VERIFY(arc_buf_remove_ref(abuf
, &abuf
));
2183 return (error
!= 0);
2191 zio_ddt_child_write_ready(zio_t
*zio
)
2193 int p
= zio
->io_prop
.zp_copies
;
2194 ddt_t
*ddt
= ddt_select(zio
->io_spa
, zio
->io_bp
);
2195 ddt_entry_t
*dde
= zio
->io_private
;
2196 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2204 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2206 ddt_phys_fill(ddp
, zio
->io_bp
);
2208 while ((pio
= zio_walk_parents(zio
)) != NULL
)
2209 ddt_bp_fill(ddp
, pio
->io_bp
, zio
->io_txg
);
2215 zio_ddt_child_write_done(zio_t
*zio
)
2217 int p
= zio
->io_prop
.zp_copies
;
2218 ddt_t
*ddt
= ddt_select(zio
->io_spa
, zio
->io_bp
);
2219 ddt_entry_t
*dde
= zio
->io_private
;
2220 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2224 ASSERT(ddp
->ddp_refcnt
== 0);
2225 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2226 dde
->dde_lead_zio
[p
] = NULL
;
2228 if (zio
->io_error
== 0) {
2229 while (zio_walk_parents(zio
) != NULL
)
2230 ddt_phys_addref(ddp
);
2232 ddt_phys_clear(ddp
);
2239 zio_ddt_ditto_write_done(zio_t
*zio
)
2241 int p
= DDT_PHYS_DITTO
;
2242 blkptr_t
*bp
= zio
->io_bp
;
2243 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2244 ddt_entry_t
*dde
= zio
->io_private
;
2245 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2246 ddt_key_t
*ddk
= &dde
->dde_key
;
2247 ASSERTV(zio_prop_t
*zp
= &zio
->io_prop
);
2251 ASSERT(ddp
->ddp_refcnt
== 0);
2252 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2253 dde
->dde_lead_zio
[p
] = NULL
;
2255 if (zio
->io_error
== 0) {
2256 ASSERT(ZIO_CHECKSUM_EQUAL(bp
->blk_cksum
, ddk
->ddk_cksum
));
2257 ASSERT(zp
->zp_copies
< SPA_DVAS_PER_BP
);
2258 ASSERT(zp
->zp_copies
== BP_GET_NDVAS(bp
) - BP_IS_GANG(bp
));
2259 if (ddp
->ddp_phys_birth
!= 0)
2260 ddt_phys_free(ddt
, ddk
, ddp
, zio
->io_txg
);
2261 ddt_phys_fill(ddp
, bp
);
2268 zio_ddt_write(zio_t
*zio
)
2270 spa_t
*spa
= zio
->io_spa
;
2271 blkptr_t
*bp
= zio
->io_bp
;
2272 uint64_t txg
= zio
->io_txg
;
2273 zio_prop_t
*zp
= &zio
->io_prop
;
2274 int p
= zp
->zp_copies
;
2278 ddt_t
*ddt
= ddt_select(spa
, bp
);
2282 ASSERT(BP_GET_DEDUP(bp
));
2283 ASSERT(BP_GET_CHECKSUM(bp
) == zp
->zp_checksum
);
2284 ASSERT(BP_IS_HOLE(bp
) || zio
->io_bp_override
);
2287 dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
2288 ddp
= &dde
->dde_phys
[p
];
2290 if (zp
->zp_dedup_verify
&& zio_ddt_collision(zio
, ddt
, dde
)) {
2292 * If we're using a weak checksum, upgrade to a strong checksum
2293 * and try again. If we're already using a strong checksum,
2294 * we can't resolve it, so just convert to an ordinary write.
2295 * (And automatically e-mail a paper to Nature?)
2297 if (!zio_checksum_table
[zp
->zp_checksum
].ci_dedup
) {
2298 zp
->zp_checksum
= spa_dedup_checksum(spa
);
2299 zio_pop_transforms(zio
);
2300 zio
->io_stage
= ZIO_STAGE_OPEN
;
2303 zp
->zp_dedup
= B_FALSE
;
2305 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
2307 return (ZIO_PIPELINE_CONTINUE
);
2310 ditto_copies
= ddt_ditto_copies_needed(ddt
, dde
, ddp
);
2311 ASSERT(ditto_copies
< SPA_DVAS_PER_BP
);
2313 if (ditto_copies
> ddt_ditto_copies_present(dde
) &&
2314 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] == NULL
) {
2315 zio_prop_t czp
= *zp
;
2317 czp
.zp_copies
= ditto_copies
;
2320 * If we arrived here with an override bp, we won't have run
2321 * the transform stack, so we won't have the data we need to
2322 * generate a child i/o. So, toss the override bp and restart.
2323 * This is safe, because using the override bp is just an
2324 * optimization; and it's rare, so the cost doesn't matter.
2326 if (zio
->io_bp_override
) {
2327 zio_pop_transforms(zio
);
2328 zio
->io_stage
= ZIO_STAGE_OPEN
;
2329 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
2330 zio
->io_bp_override
= NULL
;
2333 return (ZIO_PIPELINE_CONTINUE
);
2336 dio
= zio_write(zio
, spa
, txg
, bp
, zio
->io_orig_data
,
2337 zio
->io_orig_size
, &czp
, NULL
,
2338 zio_ddt_ditto_write_done
, dde
, zio
->io_priority
,
2339 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
);
2341 zio_push_transform(dio
, zio
->io_data
, zio
->io_size
, 0, NULL
);
2342 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] = dio
;
2345 if (ddp
->ddp_phys_birth
!= 0 || dde
->dde_lead_zio
[p
] != NULL
) {
2346 if (ddp
->ddp_phys_birth
!= 0)
2347 ddt_bp_fill(ddp
, bp
, txg
);
2348 if (dde
->dde_lead_zio
[p
] != NULL
)
2349 zio_add_child(zio
, dde
->dde_lead_zio
[p
]);
2351 ddt_phys_addref(ddp
);
2352 } else if (zio
->io_bp_override
) {
2353 ASSERT(bp
->blk_birth
== txg
);
2354 ASSERT(BP_EQUAL(bp
, zio
->io_bp_override
));
2355 ddt_phys_fill(ddp
, bp
);
2356 ddt_phys_addref(ddp
);
2358 cio
= zio_write(zio
, spa
, txg
, bp
, zio
->io_orig_data
,
2359 zio
->io_orig_size
, zp
, zio_ddt_child_write_ready
,
2360 zio_ddt_child_write_done
, dde
, zio
->io_priority
,
2361 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
);
2363 zio_push_transform(cio
, zio
->io_data
, zio
->io_size
, 0, NULL
);
2364 dde
->dde_lead_zio
[p
] = cio
;
2374 return (ZIO_PIPELINE_CONTINUE
);
2377 ddt_entry_t
*freedde
; /* for debugging */
2380 zio_ddt_free(zio_t
*zio
)
2382 spa_t
*spa
= zio
->io_spa
;
2383 blkptr_t
*bp
= zio
->io_bp
;
2384 ddt_t
*ddt
= ddt_select(spa
, bp
);
2388 ASSERT(BP_GET_DEDUP(bp
));
2389 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2392 freedde
= dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
2394 ddp
= ddt_phys_select(dde
, bp
);
2396 ddt_phys_decref(ddp
);
2400 return (ZIO_PIPELINE_CONTINUE
);
2404 * ==========================================================================
2405 * Allocate and free blocks
2406 * ==========================================================================
2409 zio_dva_allocate(zio_t
*zio
)
2411 spa_t
*spa
= zio
->io_spa
;
2412 metaslab_class_t
*mc
= spa_normal_class(spa
);
2413 blkptr_t
*bp
= zio
->io_bp
;
2417 if (zio
->io_gang_leader
== NULL
) {
2418 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2419 zio
->io_gang_leader
= zio
;
2422 ASSERT(BP_IS_HOLE(bp
));
2423 ASSERT0(BP_GET_NDVAS(bp
));
2424 ASSERT3U(zio
->io_prop
.zp_copies
, >, 0);
2425 ASSERT3U(zio
->io_prop
.zp_copies
, <=, spa_max_replication(spa
));
2426 ASSERT3U(zio
->io_size
, ==, BP_GET_PSIZE(bp
));
2429 * The dump device does not support gang blocks so allocation on
2430 * behalf of the dump device (i.e. ZIO_FLAG_NODATA) must avoid
2431 * the "fast" gang feature.
2433 flags
|= (zio
->io_flags
& ZIO_FLAG_NODATA
) ? METASLAB_GANG_AVOID
: 0;
2434 flags
|= (zio
->io_flags
& ZIO_FLAG_GANG_CHILD
) ?
2435 METASLAB_GANG_CHILD
: 0;
2436 flags
|= (zio
->io_flags
& ZIO_FLAG_FASTWRITE
) ? METASLAB_FASTWRITE
: 0;
2437 error
= metaslab_alloc(spa
, mc
, zio
->io_size
, bp
,
2438 zio
->io_prop
.zp_copies
, zio
->io_txg
, NULL
, flags
);
2441 spa_dbgmsg(spa
, "%s: metaslab allocation failure: zio %p, "
2442 "size %llu, error %d", spa_name(spa
), zio
, zio
->io_size
,
2444 if (error
== ENOSPC
&& zio
->io_size
> SPA_MINBLOCKSIZE
)
2445 return (zio_write_gang_block(zio
));
2446 zio
->io_error
= error
;
2449 return (ZIO_PIPELINE_CONTINUE
);
2453 zio_dva_free(zio_t
*zio
)
2455 metaslab_free(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
, B_FALSE
);
2457 return (ZIO_PIPELINE_CONTINUE
);
2461 zio_dva_claim(zio_t
*zio
)
2465 error
= metaslab_claim(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
);
2467 zio
->io_error
= error
;
2469 return (ZIO_PIPELINE_CONTINUE
);
2473 * Undo an allocation. This is used by zio_done() when an I/O fails
2474 * and we want to give back the block we just allocated.
2475 * This handles both normal blocks and gang blocks.
2478 zio_dva_unallocate(zio_t
*zio
, zio_gang_node_t
*gn
, blkptr_t
*bp
)
2482 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
));
2483 ASSERT(zio
->io_bp_override
== NULL
);
2485 if (!BP_IS_HOLE(bp
))
2486 metaslab_free(zio
->io_spa
, bp
, bp
->blk_birth
, B_TRUE
);
2489 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
2490 zio_dva_unallocate(zio
, gn
->gn_child
[g
],
2491 &gn
->gn_gbh
->zg_blkptr
[g
]);
2497 * Try to allocate an intent log block. Return 0 on success, errno on failure.
2500 zio_alloc_zil(spa_t
*spa
, uint64_t txg
, blkptr_t
*new_bp
, uint64_t size
,
2505 ASSERT(txg
> spa_syncing_txg(spa
));
2508 * ZIL blocks are always contiguous (i.e. not gang blocks) so we
2509 * set the METASLAB_GANG_AVOID flag so that they don't "fast gang"
2510 * when allocating them.
2513 error
= metaslab_alloc(spa
, spa_log_class(spa
), size
,
2514 new_bp
, 1, txg
, NULL
,
2515 METASLAB_FASTWRITE
| METASLAB_GANG_AVOID
);
2519 error
= metaslab_alloc(spa
, spa_normal_class(spa
), size
,
2520 new_bp
, 1, txg
, NULL
,
2521 METASLAB_FASTWRITE
| METASLAB_GANG_AVOID
);
2525 BP_SET_LSIZE(new_bp
, size
);
2526 BP_SET_PSIZE(new_bp
, size
);
2527 BP_SET_COMPRESS(new_bp
, ZIO_COMPRESS_OFF
);
2528 BP_SET_CHECKSUM(new_bp
,
2529 spa_version(spa
) >= SPA_VERSION_SLIM_ZIL
2530 ? ZIO_CHECKSUM_ZILOG2
: ZIO_CHECKSUM_ZILOG
);
2531 BP_SET_TYPE(new_bp
, DMU_OT_INTENT_LOG
);
2532 BP_SET_LEVEL(new_bp
, 0);
2533 BP_SET_DEDUP(new_bp
, 0);
2534 BP_SET_BYTEORDER(new_bp
, ZFS_HOST_BYTEORDER
);
2541 * Free an intent log block.
2544 zio_free_zil(spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
)
2546 ASSERT(BP_GET_TYPE(bp
) == DMU_OT_INTENT_LOG
);
2547 ASSERT(!BP_IS_GANG(bp
));
2549 zio_free(spa
, txg
, bp
);
2553 * ==========================================================================
2554 * Read and write to physical devices
2555 * ==========================================================================
2558 zio_vdev_io_start(zio_t
*zio
)
2560 vdev_t
*vd
= zio
->io_vd
;
2562 spa_t
*spa
= zio
->io_spa
;
2564 ASSERT(zio
->io_error
== 0);
2565 ASSERT(zio
->io_child_error
[ZIO_CHILD_VDEV
] == 0);
2568 if (!(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
2569 spa_config_enter(spa
, SCL_ZIO
, zio
, RW_READER
);
2572 * The mirror_ops handle multiple DVAs in a single BP.
2574 return (vdev_mirror_ops
.vdev_op_io_start(zio
));
2578 * We keep track of time-sensitive I/Os so that the scan thread
2579 * can quickly react to certain workloads. In particular, we care
2580 * about non-scrubbing, top-level reads and writes with the following
2582 * - synchronous writes of user data to non-slog devices
2583 * - any reads of user data
2584 * When these conditions are met, adjust the timestamp of spa_last_io
2585 * which allows the scan thread to adjust its workload accordingly.
2587 if (!(zio
->io_flags
& ZIO_FLAG_SCAN_THREAD
) && zio
->io_bp
!= NULL
&&
2588 vd
== vd
->vdev_top
&& !vd
->vdev_islog
&&
2589 zio
->io_bookmark
.zb_objset
!= DMU_META_OBJSET
&&
2590 zio
->io_txg
!= spa_syncing_txg(spa
)) {
2591 uint64_t old
= spa
->spa_last_io
;
2592 uint64_t new = ddi_get_lbolt64();
2594 (void) atomic_cas_64(&spa
->spa_last_io
, old
, new);
2597 align
= 1ULL << vd
->vdev_top
->vdev_ashift
;
2599 if (P2PHASE(zio
->io_size
, align
) != 0) {
2600 uint64_t asize
= P2ROUNDUP(zio
->io_size
, align
);
2601 char *abuf
= zio_buf_alloc(asize
);
2602 ASSERT(vd
== vd
->vdev_top
);
2603 if (zio
->io_type
== ZIO_TYPE_WRITE
) {
2604 bcopy(zio
->io_data
, abuf
, zio
->io_size
);
2605 bzero(abuf
+ zio
->io_size
, asize
- zio
->io_size
);
2607 zio_push_transform(zio
, abuf
, asize
, asize
, zio_subblock
);
2610 ASSERT(P2PHASE(zio
->io_offset
, align
) == 0);
2611 ASSERT(P2PHASE(zio
->io_size
, align
) == 0);
2612 VERIFY(zio
->io_type
!= ZIO_TYPE_WRITE
|| spa_writeable(spa
));
2615 * If this is a repair I/O, and there's no self-healing involved --
2616 * that is, we're just resilvering what we expect to resilver --
2617 * then don't do the I/O unless zio's txg is actually in vd's DTL.
2618 * This prevents spurious resilvering with nested replication.
2619 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
2620 * A is out of date, we'll read from C+D, then use the data to
2621 * resilver A+B -- but we don't actually want to resilver B, just A.
2622 * The top-level mirror has no way to know this, so instead we just
2623 * discard unnecessary repairs as we work our way down the vdev tree.
2624 * The same logic applies to any form of nested replication:
2625 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
2627 if ((zio
->io_flags
& ZIO_FLAG_IO_REPAIR
) &&
2628 !(zio
->io_flags
& ZIO_FLAG_SELF_HEAL
) &&
2629 zio
->io_txg
!= 0 && /* not a delegated i/o */
2630 !vdev_dtl_contains(vd
, DTL_PARTIAL
, zio
->io_txg
, 1)) {
2631 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
2632 zio_vdev_io_bypass(zio
);
2633 return (ZIO_PIPELINE_CONTINUE
);
2636 if (vd
->vdev_ops
->vdev_op_leaf
&&
2637 (zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
)) {
2639 if (zio
->io_type
== ZIO_TYPE_READ
&& vdev_cache_read(zio
) == 0)
2640 return (ZIO_PIPELINE_CONTINUE
);
2642 if ((zio
= vdev_queue_io(zio
)) == NULL
)
2643 return (ZIO_PIPELINE_STOP
);
2645 if (!vdev_accessible(vd
, zio
)) {
2646 zio
->io_error
= SET_ERROR(ENXIO
);
2648 return (ZIO_PIPELINE_STOP
);
2652 return (vd
->vdev_ops
->vdev_op_io_start(zio
));
2656 zio_vdev_io_done(zio_t
*zio
)
2658 vdev_t
*vd
= zio
->io_vd
;
2659 vdev_ops_t
*ops
= vd
? vd
->vdev_ops
: &vdev_mirror_ops
;
2660 boolean_t unexpected_error
= B_FALSE
;
2662 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
))
2663 return (ZIO_PIPELINE_STOP
);
2665 ASSERT(zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
);
2667 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
) {
2669 vdev_queue_io_done(zio
);
2671 if (zio
->io_type
== ZIO_TYPE_WRITE
)
2672 vdev_cache_write(zio
);
2674 if (zio_injection_enabled
&& zio
->io_error
== 0)
2675 zio
->io_error
= zio_handle_device_injection(vd
,
2678 if (zio_injection_enabled
&& zio
->io_error
== 0)
2679 zio
->io_error
= zio_handle_label_injection(zio
, EIO
);
2681 if (zio
->io_error
) {
2682 if (!vdev_accessible(vd
, zio
)) {
2683 zio
->io_error
= SET_ERROR(ENXIO
);
2685 unexpected_error
= B_TRUE
;
2690 ops
->vdev_op_io_done(zio
);
2692 if (unexpected_error
)
2693 VERIFY(vdev_probe(vd
, zio
) == NULL
);
2695 return (ZIO_PIPELINE_CONTINUE
);
2699 * For non-raidz ZIOs, we can just copy aside the bad data read from the
2700 * disk, and use that to finish the checksum ereport later.
2703 zio_vsd_default_cksum_finish(zio_cksum_report_t
*zcr
,
2704 const void *good_buf
)
2706 /* no processing needed */
2707 zfs_ereport_finish_checksum(zcr
, good_buf
, zcr
->zcr_cbdata
, B_FALSE
);
2712 zio_vsd_default_cksum_report(zio_t
*zio
, zio_cksum_report_t
*zcr
, void *ignored
)
2714 void *buf
= zio_buf_alloc(zio
->io_size
);
2716 bcopy(zio
->io_data
, buf
, zio
->io_size
);
2718 zcr
->zcr_cbinfo
= zio
->io_size
;
2719 zcr
->zcr_cbdata
= buf
;
2720 zcr
->zcr_finish
= zio_vsd_default_cksum_finish
;
2721 zcr
->zcr_free
= zio_buf_free
;
2725 zio_vdev_io_assess(zio_t
*zio
)
2727 vdev_t
*vd
= zio
->io_vd
;
2729 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
))
2730 return (ZIO_PIPELINE_STOP
);
2732 if (vd
== NULL
&& !(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
2733 spa_config_exit(zio
->io_spa
, SCL_ZIO
, zio
);
2735 if (zio
->io_vsd
!= NULL
) {
2736 zio
->io_vsd_ops
->vsd_free(zio
);
2740 if (zio_injection_enabled
&& zio
->io_error
== 0)
2741 zio
->io_error
= zio_handle_fault_injection(zio
, EIO
);
2744 * If the I/O failed, determine whether we should attempt to retry it.
2746 * On retry, we cut in line in the issue queue, since we don't want
2747 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
2749 if (zio
->io_error
&& vd
== NULL
&&
2750 !(zio
->io_flags
& (ZIO_FLAG_DONT_RETRY
| ZIO_FLAG_IO_RETRY
))) {
2751 ASSERT(!(zio
->io_flags
& ZIO_FLAG_DONT_QUEUE
)); /* not a leaf */
2752 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_BYPASS
)); /* not a leaf */
2754 zio
->io_flags
|= ZIO_FLAG_IO_RETRY
|
2755 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
;
2756 zio
->io_stage
= ZIO_STAGE_VDEV_IO_START
>> 1;
2757 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
,
2758 zio_requeue_io_start_cut_in_line
);
2759 return (ZIO_PIPELINE_STOP
);
2763 * If we got an error on a leaf device, convert it to ENXIO
2764 * if the device is not accessible at all.
2766 if (zio
->io_error
&& vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
&&
2767 !vdev_accessible(vd
, zio
))
2768 zio
->io_error
= SET_ERROR(ENXIO
);
2771 * If we can't write to an interior vdev (mirror or RAID-Z),
2772 * set vdev_cant_write so that we stop trying to allocate from it.
2774 if (zio
->io_error
== ENXIO
&& zio
->io_type
== ZIO_TYPE_WRITE
&&
2775 vd
!= NULL
&& !vd
->vdev_ops
->vdev_op_leaf
) {
2776 vd
->vdev_cant_write
= B_TRUE
;
2780 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2782 return (ZIO_PIPELINE_CONTINUE
);
2786 zio_vdev_io_reissue(zio_t
*zio
)
2788 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
2789 ASSERT(zio
->io_error
== 0);
2791 zio
->io_stage
>>= 1;
2795 zio_vdev_io_redone(zio_t
*zio
)
2797 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_DONE
);
2799 zio
->io_stage
>>= 1;
2803 zio_vdev_io_bypass(zio_t
*zio
)
2805 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
2806 ASSERT(zio
->io_error
== 0);
2808 zio
->io_flags
|= ZIO_FLAG_IO_BYPASS
;
2809 zio
->io_stage
= ZIO_STAGE_VDEV_IO_ASSESS
>> 1;
2813 * ==========================================================================
2814 * Generate and verify checksums
2815 * ==========================================================================
2818 zio_checksum_generate(zio_t
*zio
)
2820 blkptr_t
*bp
= zio
->io_bp
;
2821 enum zio_checksum checksum
;
2825 * This is zio_write_phys().
2826 * We're either generating a label checksum, or none at all.
2828 checksum
= zio
->io_prop
.zp_checksum
;
2830 if (checksum
== ZIO_CHECKSUM_OFF
)
2831 return (ZIO_PIPELINE_CONTINUE
);
2833 ASSERT(checksum
== ZIO_CHECKSUM_LABEL
);
2835 if (BP_IS_GANG(bp
) && zio
->io_child_type
== ZIO_CHILD_GANG
) {
2836 ASSERT(!IO_IS_ALLOCATING(zio
));
2837 checksum
= ZIO_CHECKSUM_GANG_HEADER
;
2839 checksum
= BP_GET_CHECKSUM(bp
);
2843 zio_checksum_compute(zio
, checksum
, zio
->io_data
, zio
->io_size
);
2845 return (ZIO_PIPELINE_CONTINUE
);
2849 zio_checksum_verify(zio_t
*zio
)
2851 zio_bad_cksum_t info
;
2852 blkptr_t
*bp
= zio
->io_bp
;
2855 ASSERT(zio
->io_vd
!= NULL
);
2859 * This is zio_read_phys().
2860 * We're either verifying a label checksum, or nothing at all.
2862 if (zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_OFF
)
2863 return (ZIO_PIPELINE_CONTINUE
);
2865 ASSERT(zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_LABEL
);
2868 if ((error
= zio_checksum_error(zio
, &info
)) != 0) {
2869 zio
->io_error
= error
;
2870 if (!(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
)) {
2871 zfs_ereport_start_checksum(zio
->io_spa
,
2872 zio
->io_vd
, zio
, zio
->io_offset
,
2873 zio
->io_size
, NULL
, &info
);
2877 return (ZIO_PIPELINE_CONTINUE
);
2881 * Called by RAID-Z to ensure we don't compute the checksum twice.
2884 zio_checksum_verified(zio_t
*zio
)
2886 zio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
2890 * ==========================================================================
2891 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
2892 * An error of 0 indictes success. ENXIO indicates whole-device failure,
2893 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
2894 * indicate errors that are specific to one I/O, and most likely permanent.
2895 * Any other error is presumed to be worse because we weren't expecting it.
2896 * ==========================================================================
2899 zio_worst_error(int e1
, int e2
)
2901 static int zio_error_rank
[] = { 0, ENXIO
, ECKSUM
, EIO
};
2904 for (r1
= 0; r1
< sizeof (zio_error_rank
) / sizeof (int); r1
++)
2905 if (e1
== zio_error_rank
[r1
])
2908 for (r2
= 0; r2
< sizeof (zio_error_rank
) / sizeof (int); r2
++)
2909 if (e2
== zio_error_rank
[r2
])
2912 return (r1
> r2
? e1
: e2
);
2916 * ==========================================================================
2918 * ==========================================================================
2921 zio_ready(zio_t
*zio
)
2923 blkptr_t
*bp
= zio
->io_bp
;
2924 zio_t
*pio
, *pio_next
;
2926 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_READY
) ||
2927 zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_READY
))
2928 return (ZIO_PIPELINE_STOP
);
2930 if (zio
->io_ready
) {
2931 ASSERT(IO_IS_ALLOCATING(zio
));
2932 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
) ||
2933 (zio
->io_flags
& ZIO_FLAG_NOPWRITE
));
2934 ASSERT(zio
->io_children
[ZIO_CHILD_GANG
][ZIO_WAIT_READY
] == 0);
2939 if (bp
!= NULL
&& bp
!= &zio
->io_bp_copy
)
2940 zio
->io_bp_copy
= *bp
;
2943 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2945 mutex_enter(&zio
->io_lock
);
2946 zio
->io_state
[ZIO_WAIT_READY
] = 1;
2947 pio
= zio_walk_parents(zio
);
2948 mutex_exit(&zio
->io_lock
);
2951 * As we notify zio's parents, new parents could be added.
2952 * New parents go to the head of zio's io_parent_list, however,
2953 * so we will (correctly) not notify them. The remainder of zio's
2954 * io_parent_list, from 'pio_next' onward, cannot change because
2955 * all parents must wait for us to be done before they can be done.
2957 for (; pio
!= NULL
; pio
= pio_next
) {
2958 pio_next
= zio_walk_parents(zio
);
2959 zio_notify_parent(pio
, zio
, ZIO_WAIT_READY
);
2962 if (zio
->io_flags
& ZIO_FLAG_NODATA
) {
2963 if (BP_IS_GANG(bp
)) {
2964 zio
->io_flags
&= ~ZIO_FLAG_NODATA
;
2966 ASSERT((uintptr_t)zio
->io_data
< SPA_MAXBLOCKSIZE
);
2967 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
2971 if (zio_injection_enabled
&&
2972 zio
->io_spa
->spa_syncing_txg
== zio
->io_txg
)
2973 zio_handle_ignored_writes(zio
);
2975 return (ZIO_PIPELINE_CONTINUE
);
2979 zio_done(zio_t
*zio
)
2981 zio_t
*pio
, *pio_next
;
2985 * If our children haven't all completed,
2986 * wait for them and then repeat this pipeline stage.
2988 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
) ||
2989 zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_DONE
) ||
2990 zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_DONE
) ||
2991 zio_wait_for_children(zio
, ZIO_CHILD_LOGICAL
, ZIO_WAIT_DONE
))
2992 return (ZIO_PIPELINE_STOP
);
2994 for (c
= 0; c
< ZIO_CHILD_TYPES
; c
++)
2995 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
2996 ASSERT(zio
->io_children
[c
][w
] == 0);
2998 if (zio
->io_bp
!= NULL
) {
2999 ASSERT(zio
->io_bp
->blk_pad
[0] == 0);
3000 ASSERT(zio
->io_bp
->blk_pad
[1] == 0);
3001 ASSERT(bcmp(zio
->io_bp
, &zio
->io_bp_copy
, sizeof (blkptr_t
)) == 0 ||
3002 (zio
->io_bp
== zio_unique_parent(zio
)->io_bp
));
3003 if (zio
->io_type
== ZIO_TYPE_WRITE
&& !BP_IS_HOLE(zio
->io_bp
) &&
3004 zio
->io_bp_override
== NULL
&&
3005 !(zio
->io_flags
& ZIO_FLAG_IO_REPAIR
)) {
3006 ASSERT(!BP_SHOULD_BYTESWAP(zio
->io_bp
));
3007 ASSERT3U(zio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(zio
->io_bp
));
3008 ASSERT(BP_COUNT_GANG(zio
->io_bp
) == 0 ||
3009 (BP_COUNT_GANG(zio
->io_bp
) == BP_GET_NDVAS(zio
->io_bp
)));
3011 if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
)
3012 VERIFY(BP_EQUAL(zio
->io_bp
, &zio
->io_bp_orig
));
3016 * If there were child vdev/gang/ddt errors, they apply to us now.
3018 zio_inherit_child_errors(zio
, ZIO_CHILD_VDEV
);
3019 zio_inherit_child_errors(zio
, ZIO_CHILD_GANG
);
3020 zio_inherit_child_errors(zio
, ZIO_CHILD_DDT
);
3023 * If the I/O on the transformed data was successful, generate any
3024 * checksum reports now while we still have the transformed data.
3026 if (zio
->io_error
== 0) {
3027 while (zio
->io_cksum_report
!= NULL
) {
3028 zio_cksum_report_t
*zcr
= zio
->io_cksum_report
;
3029 uint64_t align
= zcr
->zcr_align
;
3030 uint64_t asize
= P2ROUNDUP(zio
->io_size
, align
);
3031 char *abuf
= zio
->io_data
;
3033 if (asize
!= zio
->io_size
) {
3034 abuf
= zio_buf_alloc(asize
);
3035 bcopy(zio
->io_data
, abuf
, zio
->io_size
);
3036 bzero(abuf
+ zio
->io_size
, asize
- zio
->io_size
);
3039 zio
->io_cksum_report
= zcr
->zcr_next
;
3040 zcr
->zcr_next
= NULL
;
3041 zcr
->zcr_finish(zcr
, abuf
);
3042 zfs_ereport_free_checksum(zcr
);
3044 if (asize
!= zio
->io_size
)
3045 zio_buf_free(abuf
, asize
);
3049 zio_pop_transforms(zio
); /* note: may set zio->io_error */
3051 vdev_stat_update(zio
, zio
->io_size
);
3054 * If this I/O is attached to a particular vdev is slow, exceeding
3055 * 30 seconds to complete, post an error described the I/O delay.
3056 * We ignore these errors if the device is currently unavailable.
3058 if (zio
->io_delay
>= MSEC_TO_TICK(zio_delay_max
)) {
3059 if (zio
->io_vd
!= NULL
&& !vdev_is_dead(zio
->io_vd
))
3060 zfs_ereport_post(FM_EREPORT_ZFS_DELAY
, zio
->io_spa
,
3061 zio
->io_vd
, zio
, 0, 0);
3064 if (zio
->io_error
) {
3066 * If this I/O is attached to a particular vdev,
3067 * generate an error message describing the I/O failure
3068 * at the block level. We ignore these errors if the
3069 * device is currently unavailable.
3071 if (zio
->io_error
!= ECKSUM
&& zio
->io_vd
!= NULL
&&
3072 !vdev_is_dead(zio
->io_vd
))
3073 zfs_ereport_post(FM_EREPORT_ZFS_IO
, zio
->io_spa
,
3074 zio
->io_vd
, zio
, 0, 0);
3076 if ((zio
->io_error
== EIO
|| !(zio
->io_flags
&
3077 (ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_DONT_PROPAGATE
))) &&
3078 zio
== zio
->io_logical
) {
3080 * For logical I/O requests, tell the SPA to log the
3081 * error and generate a logical data ereport.
3083 spa_log_error(zio
->io_spa
, zio
);
3084 zfs_ereport_post(FM_EREPORT_ZFS_DATA
, zio
->io_spa
, NULL
, zio
,
3089 if (zio
->io_error
&& zio
== zio
->io_logical
) {
3091 * Determine whether zio should be reexecuted. This will
3092 * propagate all the way to the root via zio_notify_parent().
3094 ASSERT(zio
->io_vd
== NULL
&& zio
->io_bp
!= NULL
);
3095 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
3097 if (IO_IS_ALLOCATING(zio
) &&
3098 !(zio
->io_flags
& ZIO_FLAG_CANFAIL
)) {
3099 if (zio
->io_error
!= ENOSPC
)
3100 zio
->io_reexecute
|= ZIO_REEXECUTE_NOW
;
3102 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3105 if ((zio
->io_type
== ZIO_TYPE_READ
||
3106 zio
->io_type
== ZIO_TYPE_FREE
) &&
3107 !(zio
->io_flags
& ZIO_FLAG_SCAN_THREAD
) &&
3108 zio
->io_error
== ENXIO
&&
3109 spa_load_state(zio
->io_spa
) == SPA_LOAD_NONE
&&
3110 spa_get_failmode(zio
->io_spa
) != ZIO_FAILURE_MODE_CONTINUE
)
3111 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3113 if (!(zio
->io_flags
& ZIO_FLAG_CANFAIL
) && !zio
->io_reexecute
)
3114 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3117 * Here is a possibly good place to attempt to do
3118 * either combinatorial reconstruction or error correction
3119 * based on checksums. It also might be a good place
3120 * to send out preliminary ereports before we suspend
3126 * If there were logical child errors, they apply to us now.
3127 * We defer this until now to avoid conflating logical child
3128 * errors with errors that happened to the zio itself when
3129 * updating vdev stats and reporting FMA events above.
3131 zio_inherit_child_errors(zio
, ZIO_CHILD_LOGICAL
);
3133 if ((zio
->io_error
|| zio
->io_reexecute
) &&
3134 IO_IS_ALLOCATING(zio
) && zio
->io_gang_leader
== zio
&&
3135 !(zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)))
3136 zio_dva_unallocate(zio
, zio
->io_gang_tree
, zio
->io_bp
);
3138 zio_gang_tree_free(&zio
->io_gang_tree
);
3141 * Godfather I/Os should never suspend.
3143 if ((zio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
3144 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
))
3145 zio
->io_reexecute
= 0;
3147 if (zio
->io_reexecute
) {
3149 * This is a logical I/O that wants to reexecute.
3151 * Reexecute is top-down. When an i/o fails, if it's not
3152 * the root, it simply notifies its parent and sticks around.
3153 * The parent, seeing that it still has children in zio_done(),
3154 * does the same. This percolates all the way up to the root.
3155 * The root i/o will reexecute or suspend the entire tree.
3157 * This approach ensures that zio_reexecute() honors
3158 * all the original i/o dependency relationships, e.g.
3159 * parents not executing until children are ready.
3161 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
3163 zio
->io_gang_leader
= NULL
;
3165 mutex_enter(&zio
->io_lock
);
3166 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
3167 mutex_exit(&zio
->io_lock
);
3170 * "The Godfather" I/O monitors its children but is
3171 * not a true parent to them. It will track them through
3172 * the pipeline but severs its ties whenever they get into
3173 * trouble (e.g. suspended). This allows "The Godfather"
3174 * I/O to return status without blocking.
3176 for (pio
= zio_walk_parents(zio
); pio
!= NULL
; pio
= pio_next
) {
3177 zio_link_t
*zl
= zio
->io_walk_link
;
3178 pio_next
= zio_walk_parents(zio
);
3180 if ((pio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
3181 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
)) {
3182 zio_remove_child(pio
, zio
, zl
);
3183 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3187 if ((pio
= zio_unique_parent(zio
)) != NULL
) {
3189 * We're not a root i/o, so there's nothing to do
3190 * but notify our parent. Don't propagate errors
3191 * upward since we haven't permanently failed yet.
3193 ASSERT(!(zio
->io_flags
& ZIO_FLAG_GODFATHER
));
3194 zio
->io_flags
|= ZIO_FLAG_DONT_PROPAGATE
;
3195 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3196 } else if (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
) {
3198 * We'd fail again if we reexecuted now, so suspend
3199 * until conditions improve (e.g. device comes online).
3201 zio_suspend(zio
->io_spa
, zio
);
3204 * Reexecution is potentially a huge amount of work.
3205 * Hand it off to the otherwise-unused claim taskq.
3207 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
3208 spa_taskq_dispatch_ent(zio
->io_spa
,
3209 ZIO_TYPE_CLAIM
, ZIO_TASKQ_ISSUE
,
3210 (task_func_t
*)zio_reexecute
, zio
, 0,
3213 return (ZIO_PIPELINE_STOP
);
3216 ASSERT(zio
->io_child_count
== 0);
3217 ASSERT(zio
->io_reexecute
== 0);
3218 ASSERT(zio
->io_error
== 0 || (zio
->io_flags
& ZIO_FLAG_CANFAIL
));
3221 * Report any checksum errors, since the I/O is complete.
3223 while (zio
->io_cksum_report
!= NULL
) {
3224 zio_cksum_report_t
*zcr
= zio
->io_cksum_report
;
3225 zio
->io_cksum_report
= zcr
->zcr_next
;
3226 zcr
->zcr_next
= NULL
;
3227 zcr
->zcr_finish(zcr
, NULL
);
3228 zfs_ereport_free_checksum(zcr
);
3231 if (zio
->io_flags
& ZIO_FLAG_FASTWRITE
&& zio
->io_bp
&&
3232 !BP_IS_HOLE(zio
->io_bp
) && !(zio
->io_flags
& ZIO_FLAG_NOPWRITE
)) {
3233 metaslab_fastwrite_unmark(zio
->io_spa
, zio
->io_bp
);
3237 * It is the responsibility of the done callback to ensure that this
3238 * particular zio is no longer discoverable for adoption, and as
3239 * such, cannot acquire any new parents.
3244 mutex_enter(&zio
->io_lock
);
3245 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
3246 mutex_exit(&zio
->io_lock
);
3248 for (pio
= zio_walk_parents(zio
); pio
!= NULL
; pio
= pio_next
) {
3249 zio_link_t
*zl
= zio
->io_walk_link
;
3250 pio_next
= zio_walk_parents(zio
);
3251 zio_remove_child(pio
, zio
, zl
);
3252 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3255 if (zio
->io_waiter
!= NULL
) {
3256 mutex_enter(&zio
->io_lock
);
3257 zio
->io_executor
= NULL
;
3258 cv_broadcast(&zio
->io_cv
);
3259 mutex_exit(&zio
->io_lock
);
3264 return (ZIO_PIPELINE_STOP
);
3268 * ==========================================================================
3269 * I/O pipeline definition
3270 * ==========================================================================
3272 static zio_pipe_stage_t
*zio_pipeline
[] = {
3278 zio_checksum_generate
,
3293 zio_checksum_verify
,
3297 /* dnp is the dnode for zb1->zb_object */
3299 zbookmark_is_before(const dnode_phys_t
*dnp
, const zbookmark_t
*zb1
,
3300 const zbookmark_t
*zb2
)
3302 uint64_t zb1nextL0
, zb2thisobj
;
3304 ASSERT(zb1
->zb_objset
== zb2
->zb_objset
);
3305 ASSERT(zb2
->zb_level
== 0);
3308 * A bookmark in the deadlist is considered to be after
3311 if (zb2
->zb_object
== DMU_DEADLIST_OBJECT
)
3314 /* The objset_phys_t isn't before anything. */
3318 zb1nextL0
= (zb1
->zb_blkid
+ 1) <<
3319 ((zb1
->zb_level
) * (dnp
->dn_indblkshift
- SPA_BLKPTRSHIFT
));
3321 zb2thisobj
= zb2
->zb_object
? zb2
->zb_object
:
3322 zb2
->zb_blkid
<< (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
);
3324 if (zb1
->zb_object
== DMU_META_DNODE_OBJECT
) {
3325 uint64_t nextobj
= zb1nextL0
*
3326 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
) >> DNODE_SHIFT
;
3327 return (nextobj
<= zb2thisobj
);
3330 if (zb1
->zb_object
< zb2thisobj
)
3332 if (zb1
->zb_object
> zb2thisobj
)
3334 if (zb2
->zb_object
== DMU_META_DNODE_OBJECT
)
3336 return (zb1nextL0
<= zb2
->zb_blkid
);
3339 #if defined(_KERNEL) && defined(HAVE_SPL)
3340 /* Fault injection */
3341 EXPORT_SYMBOL(zio_injection_enabled
);
3342 EXPORT_SYMBOL(zio_inject_fault
);
3343 EXPORT_SYMBOL(zio_inject_list_next
);
3344 EXPORT_SYMBOL(zio_clear_fault
);
3345 EXPORT_SYMBOL(zio_handle_fault_injection
);
3346 EXPORT_SYMBOL(zio_handle_device_injection
);
3347 EXPORT_SYMBOL(zio_handle_label_injection
);
3348 EXPORT_SYMBOL(zio_priority_table
);
3349 EXPORT_SYMBOL(zio_type_name
);
3351 module_param(zio_bulk_flags
, int, 0644);
3352 MODULE_PARM_DESC(zio_bulk_flags
, "Additional flags to pass to bulk buffers");
3354 module_param(zio_delay_max
, int, 0644);
3355 MODULE_PARM_DESC(zio_delay_max
, "Max zio millisec delay before posting event");
3357 module_param(zio_requeue_io_start_cut_in_line
, int, 0644);
3358 MODULE_PARM_DESC(zio_requeue_io_start_cut_in_line
, "Prioritize requeued I/O");
3360 module_param(zfs_sync_pass_deferred_free
, int, 0644);
3361 MODULE_PARM_DESC(zfs_sync_pass_deferred_free
,
3362 "defer frees starting in this pass");
3364 module_param(zfs_sync_pass_dont_compress
, int, 0644);
3365 MODULE_PARM_DESC(zfs_sync_pass_dont_compress
,
3366 "don't compress starting in this pass");
3368 module_param(zfs_sync_pass_rewrite
, int, 0644);
3369 MODULE_PARM_DESC(zfs_sync_pass_rewrite
,
3370 "rewrite new bps starting in this pass");