4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
27 #include <sys/zfs_context.h>
28 #include <sys/fm/fs/zfs.h>
31 #include <sys/spa_impl.h>
32 #include <sys/vdev_impl.h>
33 #include <sys/zio_impl.h>
34 #include <sys/zio_compress.h>
35 #include <sys/zio_checksum.h>
36 #include <sys/dmu_objset.h>
39 #include <sys/blkptr.h>
40 #include <sys/zfeature.h>
43 * ==========================================================================
44 * I/O type descriptions
45 * ==========================================================================
47 const char *zio_type_name
[ZIO_TYPES
] = {
48 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl"
52 * ==========================================================================
54 * ==========================================================================
56 kmem_cache_t
*zio_cache
;
57 kmem_cache_t
*zio_link_cache
;
58 kmem_cache_t
*zio_buf_cache
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
59 kmem_cache_t
*zio_data_buf_cache
[SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
];
60 int zio_bulk_flags
= 0;
61 int zio_delay_max
= ZIO_DELAY_MAX
;
64 * The following actions directly effect the spa's sync-to-convergence logic.
65 * The values below define the sync pass when we start performing the action.
66 * Care should be taken when changing these values as they directly impact
67 * spa_sync() performance. Tuning these values may introduce subtle performance
68 * pathologies and should only be done in the context of performance analysis.
69 * These tunables will eventually be removed and replaced with #defines once
70 * enough analysis has been done to determine optimal values.
72 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
73 * regular blocks are not deferred.
75 int zfs_sync_pass_deferred_free
= 2; /* defer frees starting in this pass */
76 int zfs_sync_pass_dont_compress
= 5; /* don't compress starting in this pass */
77 int zfs_sync_pass_rewrite
= 2; /* rewrite new bps starting in this pass */
80 * An allocating zio is one that either currently has the DVA allocate
81 * stage set or will have it later in its lifetime.
83 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
85 int zio_requeue_io_start_cut_in_line
= 1;
88 int zio_buf_debug_limit
= 16384;
90 int zio_buf_debug_limit
= 0;
93 static inline void __zio_execute(zio_t
*zio
);
96 zio_cons(void *arg
, void *unused
, int kmflag
)
100 bzero(zio
, sizeof (zio_t
));
102 mutex_init(&zio
->io_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
103 cv_init(&zio
->io_cv
, NULL
, CV_DEFAULT
, NULL
);
105 list_create(&zio
->io_parent_list
, sizeof (zio_link_t
),
106 offsetof(zio_link_t
, zl_parent_node
));
107 list_create(&zio
->io_child_list
, sizeof (zio_link_t
),
108 offsetof(zio_link_t
, zl_child_node
));
114 zio_dest(void *arg
, void *unused
)
118 mutex_destroy(&zio
->io_lock
);
119 cv_destroy(&zio
->io_cv
);
120 list_destroy(&zio
->io_parent_list
);
121 list_destroy(&zio
->io_child_list
);
128 vmem_t
*data_alloc_arena
= NULL
;
130 zio_cache
= kmem_cache_create("zio_cache", sizeof (zio_t
), 0,
131 zio_cons
, zio_dest
, NULL
, NULL
, NULL
, 0);
132 zio_link_cache
= kmem_cache_create("zio_link_cache",
133 sizeof (zio_link_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
136 * For small buffers, we want a cache for each multiple of
137 * SPA_MINBLOCKSIZE. For medium-size buffers, we want a cache
138 * for each quarter-power of 2. For large buffers, we want
139 * a cache for each multiple of PAGESIZE.
141 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
142 size_t size
= (c
+ 1) << SPA_MINBLOCKSHIFT
;
146 while (p2
& (p2
- 1))
151 * If we are using watchpoints, put each buffer on its own page,
152 * to eliminate the performance overhead of trapping to the
153 * kernel when modifying a non-watched buffer that shares the
154 * page with a watched buffer.
156 if (arc_watch
&& !IS_P2ALIGNED(size
, PAGESIZE
))
159 if (size
<= 4 * SPA_MINBLOCKSIZE
) {
160 align
= SPA_MINBLOCKSIZE
;
161 } else if (IS_P2ALIGNED(size
, PAGESIZE
)) {
163 } else if (IS_P2ALIGNED(size
, p2
>> 2)) {
169 int flags
= zio_bulk_flags
;
171 (void) sprintf(name
, "zio_buf_%lu", (ulong_t
)size
);
172 zio_buf_cache
[c
] = kmem_cache_create(name
, size
,
173 align
, NULL
, NULL
, NULL
, NULL
, NULL
, flags
);
175 (void) sprintf(name
, "zio_data_buf_%lu", (ulong_t
)size
);
176 zio_data_buf_cache
[c
] = kmem_cache_create(name
, size
,
177 align
, NULL
, NULL
, NULL
, NULL
,
178 data_alloc_arena
, flags
);
183 ASSERT(zio_buf_cache
[c
] != NULL
);
184 if (zio_buf_cache
[c
- 1] == NULL
)
185 zio_buf_cache
[c
- 1] = zio_buf_cache
[c
];
187 ASSERT(zio_data_buf_cache
[c
] != NULL
);
188 if (zio_data_buf_cache
[c
- 1] == NULL
)
189 zio_data_buf_cache
[c
- 1] = zio_data_buf_cache
[c
];
201 kmem_cache_t
*last_cache
= NULL
;
202 kmem_cache_t
*last_data_cache
= NULL
;
204 for (c
= 0; c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; c
++) {
205 if (zio_buf_cache
[c
] != last_cache
) {
206 last_cache
= zio_buf_cache
[c
];
207 kmem_cache_destroy(zio_buf_cache
[c
]);
209 zio_buf_cache
[c
] = NULL
;
211 if (zio_data_buf_cache
[c
] != last_data_cache
) {
212 last_data_cache
= zio_data_buf_cache
[c
];
213 kmem_cache_destroy(zio_data_buf_cache
[c
]);
215 zio_data_buf_cache
[c
] = NULL
;
218 kmem_cache_destroy(zio_link_cache
);
219 kmem_cache_destroy(zio_cache
);
227 * ==========================================================================
228 * Allocate and free I/O buffers
229 * ==========================================================================
233 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
234 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
235 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
236 * excess / transient data in-core during a crashdump.
239 zio_buf_alloc(size_t size
)
241 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
243 ASSERT3U(c
, <, SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
245 return (kmem_cache_alloc(zio_buf_cache
[c
], KM_PUSHPAGE
));
249 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
250 * crashdump if the kernel panics. This exists so that we will limit the amount
251 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
252 * of kernel heap dumped to disk when the kernel panics)
255 zio_data_buf_alloc(size_t size
)
257 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
259 ASSERT(c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
261 return (kmem_cache_alloc(zio_data_buf_cache
[c
], KM_PUSHPAGE
));
265 zio_buf_free(void *buf
, size_t size
)
267 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
269 ASSERT(c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
271 kmem_cache_free(zio_buf_cache
[c
], buf
);
275 zio_data_buf_free(void *buf
, size_t size
)
277 size_t c
= (size
- 1) >> SPA_MINBLOCKSHIFT
;
279 ASSERT(c
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
);
281 kmem_cache_free(zio_data_buf_cache
[c
], buf
);
285 * ==========================================================================
286 * Push and pop I/O transform buffers
287 * ==========================================================================
290 zio_push_transform(zio_t
*zio
, void *data
, uint64_t size
, uint64_t bufsize
,
291 zio_transform_func_t
*transform
)
293 zio_transform_t
*zt
= kmem_alloc(sizeof (zio_transform_t
), KM_SLEEP
);
295 zt
->zt_orig_data
= zio
->io_data
;
296 zt
->zt_orig_size
= zio
->io_size
;
297 zt
->zt_bufsize
= bufsize
;
298 zt
->zt_transform
= transform
;
300 zt
->zt_next
= zio
->io_transform_stack
;
301 zio
->io_transform_stack
= zt
;
308 zio_pop_transforms(zio_t
*zio
)
312 while ((zt
= zio
->io_transform_stack
) != NULL
) {
313 if (zt
->zt_transform
!= NULL
)
314 zt
->zt_transform(zio
,
315 zt
->zt_orig_data
, zt
->zt_orig_size
);
317 if (zt
->zt_bufsize
!= 0)
318 zio_buf_free(zio
->io_data
, zt
->zt_bufsize
);
320 zio
->io_data
= zt
->zt_orig_data
;
321 zio
->io_size
= zt
->zt_orig_size
;
322 zio
->io_transform_stack
= zt
->zt_next
;
324 kmem_free(zt
, sizeof (zio_transform_t
));
329 * ==========================================================================
330 * I/O transform callbacks for subblocks and decompression
331 * ==========================================================================
334 zio_subblock(zio_t
*zio
, void *data
, uint64_t size
)
336 ASSERT(zio
->io_size
> size
);
338 if (zio
->io_type
== ZIO_TYPE_READ
)
339 bcopy(zio
->io_data
, data
, size
);
343 zio_decompress(zio_t
*zio
, void *data
, uint64_t size
)
345 if (zio
->io_error
== 0 &&
346 zio_decompress_data(BP_GET_COMPRESS(zio
->io_bp
),
347 zio
->io_data
, data
, zio
->io_size
, size
) != 0)
348 zio
->io_error
= SET_ERROR(EIO
);
352 * ==========================================================================
353 * I/O parent/child relationships and pipeline interlocks
354 * ==========================================================================
357 * NOTE - Callers to zio_walk_parents() and zio_walk_children must
358 * continue calling these functions until they return NULL.
359 * Otherwise, the next caller will pick up the list walk in
360 * some indeterminate state. (Otherwise every caller would
361 * have to pass in a cookie to keep the state represented by
362 * io_walk_link, which gets annoying.)
365 zio_walk_parents(zio_t
*cio
)
367 zio_link_t
*zl
= cio
->io_walk_link
;
368 list_t
*pl
= &cio
->io_parent_list
;
370 zl
= (zl
== NULL
) ? list_head(pl
) : list_next(pl
, zl
);
371 cio
->io_walk_link
= zl
;
376 ASSERT(zl
->zl_child
== cio
);
377 return (zl
->zl_parent
);
381 zio_walk_children(zio_t
*pio
)
383 zio_link_t
*zl
= pio
->io_walk_link
;
384 list_t
*cl
= &pio
->io_child_list
;
386 zl
= (zl
== NULL
) ? list_head(cl
) : list_next(cl
, zl
);
387 pio
->io_walk_link
= zl
;
392 ASSERT(zl
->zl_parent
== pio
);
393 return (zl
->zl_child
);
397 zio_unique_parent(zio_t
*cio
)
399 zio_t
*pio
= zio_walk_parents(cio
);
401 VERIFY(zio_walk_parents(cio
) == NULL
);
406 zio_add_child(zio_t
*pio
, zio_t
*cio
)
408 zio_link_t
*zl
= kmem_cache_alloc(zio_link_cache
, KM_SLEEP
);
412 * Logical I/Os can have logical, gang, or vdev children.
413 * Gang I/Os can have gang or vdev children.
414 * Vdev I/Os can only have vdev children.
415 * The following ASSERT captures all of these constraints.
417 ASSERT(cio
->io_child_type
<= pio
->io_child_type
);
422 mutex_enter(&cio
->io_lock
);
423 mutex_enter(&pio
->io_lock
);
425 ASSERT(pio
->io_state
[ZIO_WAIT_DONE
] == 0);
427 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
428 pio
->io_children
[cio
->io_child_type
][w
] += !cio
->io_state
[w
];
430 list_insert_head(&pio
->io_child_list
, zl
);
431 list_insert_head(&cio
->io_parent_list
, zl
);
433 pio
->io_child_count
++;
434 cio
->io_parent_count
++;
436 mutex_exit(&pio
->io_lock
);
437 mutex_exit(&cio
->io_lock
);
441 zio_remove_child(zio_t
*pio
, zio_t
*cio
, zio_link_t
*zl
)
443 ASSERT(zl
->zl_parent
== pio
);
444 ASSERT(zl
->zl_child
== cio
);
446 mutex_enter(&cio
->io_lock
);
447 mutex_enter(&pio
->io_lock
);
449 list_remove(&pio
->io_child_list
, zl
);
450 list_remove(&cio
->io_parent_list
, zl
);
452 pio
->io_child_count
--;
453 cio
->io_parent_count
--;
455 mutex_exit(&pio
->io_lock
);
456 mutex_exit(&cio
->io_lock
);
458 kmem_cache_free(zio_link_cache
, zl
);
462 zio_wait_for_children(zio_t
*zio
, enum zio_child child
, enum zio_wait_type wait
)
464 uint64_t *countp
= &zio
->io_children
[child
][wait
];
465 boolean_t waiting
= B_FALSE
;
467 mutex_enter(&zio
->io_lock
);
468 ASSERT(zio
->io_stall
== NULL
);
471 zio
->io_stall
= countp
;
474 mutex_exit(&zio
->io_lock
);
479 __attribute__((always_inline
))
481 zio_notify_parent(zio_t
*pio
, zio_t
*zio
, enum zio_wait_type wait
)
483 uint64_t *countp
= &pio
->io_children
[zio
->io_child_type
][wait
];
484 int *errorp
= &pio
->io_child_error
[zio
->io_child_type
];
486 mutex_enter(&pio
->io_lock
);
487 if (zio
->io_error
&& !(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
488 *errorp
= zio_worst_error(*errorp
, zio
->io_error
);
489 pio
->io_reexecute
|= zio
->io_reexecute
;
490 ASSERT3U(*countp
, >, 0);
494 if (*countp
== 0 && pio
->io_stall
== countp
) {
495 pio
->io_stall
= NULL
;
496 mutex_exit(&pio
->io_lock
);
499 mutex_exit(&pio
->io_lock
);
504 zio_inherit_child_errors(zio_t
*zio
, enum zio_child c
)
506 if (zio
->io_child_error
[c
] != 0 && zio
->io_error
== 0)
507 zio
->io_error
= zio
->io_child_error
[c
];
511 * ==========================================================================
512 * Create the various types of I/O (read, write, free, etc)
513 * ==========================================================================
516 zio_create(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
517 void *data
, uint64_t size
, zio_done_func_t
*done
, void *private,
518 zio_type_t type
, zio_priority_t priority
, enum zio_flag flags
,
519 vdev_t
*vd
, uint64_t offset
, const zbookmark_phys_t
*zb
,
520 enum zio_stage stage
, enum zio_stage pipeline
)
524 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
525 ASSERT(P2PHASE(size
, SPA_MINBLOCKSIZE
) == 0);
526 ASSERT(P2PHASE(offset
, SPA_MINBLOCKSIZE
) == 0);
528 ASSERT(!vd
|| spa_config_held(spa
, SCL_STATE_ALL
, RW_READER
));
529 ASSERT(!bp
|| !(flags
& ZIO_FLAG_CONFIG_WRITER
));
530 ASSERT(vd
|| stage
== ZIO_STAGE_OPEN
);
532 zio
= kmem_cache_alloc(zio_cache
, KM_SLEEP
);
535 zio
->io_child_type
= ZIO_CHILD_VDEV
;
536 else if (flags
& ZIO_FLAG_GANG_CHILD
)
537 zio
->io_child_type
= ZIO_CHILD_GANG
;
538 else if (flags
& ZIO_FLAG_DDT_CHILD
)
539 zio
->io_child_type
= ZIO_CHILD_DDT
;
541 zio
->io_child_type
= ZIO_CHILD_LOGICAL
;
544 zio
->io_logical
= NULL
;
545 zio
->io_bp
= (blkptr_t
*)bp
;
546 zio
->io_bp_copy
= *bp
;
547 zio
->io_bp_orig
= *bp
;
548 if (type
!= ZIO_TYPE_WRITE
||
549 zio
->io_child_type
== ZIO_CHILD_DDT
)
550 zio
->io_bp
= &zio
->io_bp_copy
; /* so caller can free */
551 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
552 zio
->io_logical
= zio
;
553 if (zio
->io_child_type
> ZIO_CHILD_GANG
&& BP_IS_GANG(bp
))
554 pipeline
|= ZIO_GANG_STAGES
;
556 zio
->io_logical
= NULL
;
558 bzero(&zio
->io_bp_copy
, sizeof (blkptr_t
));
559 bzero(&zio
->io_bp_orig
, sizeof (blkptr_t
));
564 zio
->io_ready
= NULL
;
565 zio
->io_physdone
= NULL
;
567 zio
->io_private
= private;
568 zio
->io_prev_space_delta
= 0;
570 zio
->io_priority
= priority
;
573 zio
->io_vsd_ops
= NULL
;
574 zio
->io_offset
= offset
;
575 zio
->io_timestamp
= 0;
578 zio
->io_orig_data
= zio
->io_data
= data
;
579 zio
->io_orig_size
= zio
->io_size
= size
;
580 zio
->io_orig_flags
= zio
->io_flags
= flags
;
581 zio
->io_orig_stage
= zio
->io_stage
= stage
;
582 zio
->io_orig_pipeline
= zio
->io_pipeline
= pipeline
;
583 bzero(&zio
->io_prop
, sizeof (zio_prop_t
));
585 zio
->io_reexecute
= 0;
586 zio
->io_bp_override
= NULL
;
587 zio
->io_walk_link
= NULL
;
588 zio
->io_transform_stack
= NULL
;
590 zio
->io_child_count
= 0;
591 zio
->io_phys_children
= 0;
592 zio
->io_parent_count
= 0;
593 zio
->io_stall
= NULL
;
594 zio
->io_gang_leader
= NULL
;
595 zio
->io_gang_tree
= NULL
;
596 zio
->io_executor
= NULL
;
597 zio
->io_waiter
= NULL
;
598 zio
->io_cksum_report
= NULL
;
600 bzero(zio
->io_child_error
, sizeof (int) * ZIO_CHILD_TYPES
);
601 bzero(zio
->io_children
,
602 sizeof (uint64_t) * ZIO_CHILD_TYPES
* ZIO_WAIT_TYPES
);
603 bzero(&zio
->io_bookmark
, sizeof (zbookmark_phys_t
));
605 zio
->io_state
[ZIO_WAIT_READY
] = (stage
>= ZIO_STAGE_READY
);
606 zio
->io_state
[ZIO_WAIT_DONE
] = (stage
>= ZIO_STAGE_DONE
);
609 zio
->io_bookmark
= *zb
;
612 if (zio
->io_logical
== NULL
)
613 zio
->io_logical
= pio
->io_logical
;
614 if (zio
->io_child_type
== ZIO_CHILD_GANG
)
615 zio
->io_gang_leader
= pio
->io_gang_leader
;
616 zio_add_child(pio
, zio
);
619 taskq_init_ent(&zio
->io_tqent
);
625 zio_destroy(zio_t
*zio
)
627 kmem_cache_free(zio_cache
, zio
);
631 zio_null(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, zio_done_func_t
*done
,
632 void *private, enum zio_flag flags
)
636 zio
= zio_create(pio
, spa
, 0, NULL
, NULL
, 0, done
, private,
637 ZIO_TYPE_NULL
, ZIO_PRIORITY_NOW
, flags
, vd
, 0, NULL
,
638 ZIO_STAGE_OPEN
, ZIO_INTERLOCK_PIPELINE
);
644 zio_root(spa_t
*spa
, zio_done_func_t
*done
, void *private, enum zio_flag flags
)
646 return (zio_null(NULL
, spa
, NULL
, done
, private, flags
));
650 zio_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
651 void *data
, uint64_t size
, zio_done_func_t
*done
, void *private,
652 zio_priority_t priority
, enum zio_flag flags
, const zbookmark_phys_t
*zb
)
656 zio
= zio_create(pio
, spa
, BP_PHYSICAL_BIRTH(bp
), bp
,
657 data
, size
, done
, private,
658 ZIO_TYPE_READ
, priority
, flags
, NULL
, 0, zb
,
659 ZIO_STAGE_OPEN
, (flags
& ZIO_FLAG_DDT_CHILD
) ?
660 ZIO_DDT_CHILD_READ_PIPELINE
: ZIO_READ_PIPELINE
);
666 zio_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
,
667 void *data
, uint64_t size
, const zio_prop_t
*zp
,
668 zio_done_func_t
*ready
, zio_done_func_t
*physdone
, zio_done_func_t
*done
,
670 zio_priority_t priority
, enum zio_flag flags
, const zbookmark_phys_t
*zb
)
674 ASSERT(zp
->zp_checksum
>= ZIO_CHECKSUM_OFF
&&
675 zp
->zp_checksum
< ZIO_CHECKSUM_FUNCTIONS
&&
676 zp
->zp_compress
>= ZIO_COMPRESS_OFF
&&
677 zp
->zp_compress
< ZIO_COMPRESS_FUNCTIONS
&&
678 DMU_OT_IS_VALID(zp
->zp_type
) &&
681 zp
->zp_copies
<= spa_max_replication(spa
));
683 zio
= zio_create(pio
, spa
, txg
, bp
, data
, size
, done
, private,
684 ZIO_TYPE_WRITE
, priority
, flags
, NULL
, 0, zb
,
685 ZIO_STAGE_OPEN
, (flags
& ZIO_FLAG_DDT_CHILD
) ?
686 ZIO_DDT_CHILD_WRITE_PIPELINE
: ZIO_WRITE_PIPELINE
);
688 zio
->io_ready
= ready
;
689 zio
->io_physdone
= physdone
;
693 * Data can be NULL if we are going to call zio_write_override() to
694 * provide the already-allocated BP. But we may need the data to
695 * verify a dedup hit (if requested). In this case, don't try to
696 * dedup (just take the already-allocated BP verbatim).
698 if (data
== NULL
&& zio
->io_prop
.zp_dedup_verify
) {
699 zio
->io_prop
.zp_dedup
= zio
->io_prop
.zp_dedup_verify
= B_FALSE
;
706 zio_rewrite(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
, void *data
,
707 uint64_t size
, zio_done_func_t
*done
, void *private,
708 zio_priority_t priority
, enum zio_flag flags
, zbookmark_phys_t
*zb
)
712 zio
= zio_create(pio
, spa
, txg
, bp
, data
, size
, done
, private,
713 ZIO_TYPE_WRITE
, priority
, flags
, NULL
, 0, zb
,
714 ZIO_STAGE_OPEN
, ZIO_REWRITE_PIPELINE
);
720 zio_write_override(zio_t
*zio
, blkptr_t
*bp
, int copies
, boolean_t nopwrite
)
722 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
723 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
724 ASSERT(zio
->io_stage
== ZIO_STAGE_OPEN
);
725 ASSERT(zio
->io_txg
== spa_syncing_txg(zio
->io_spa
));
728 * We must reset the io_prop to match the values that existed
729 * when the bp was first written by dmu_sync() keeping in mind
730 * that nopwrite and dedup are mutually exclusive.
732 zio
->io_prop
.zp_dedup
= nopwrite
? B_FALSE
: zio
->io_prop
.zp_dedup
;
733 zio
->io_prop
.zp_nopwrite
= nopwrite
;
734 zio
->io_prop
.zp_copies
= copies
;
735 zio
->io_bp_override
= bp
;
739 zio_free(spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
)
743 * The check for EMBEDDED is a performance optimization. We
744 * process the free here (by ignoring it) rather than
745 * putting it on the list and then processing it in zio_free_sync().
747 if (BP_IS_EMBEDDED(bp
))
749 metaslab_check_free(spa
, bp
);
752 * Frees that are for the currently-syncing txg, are not going to be
753 * deferred, and which will not need to do a read (i.e. not GANG or
754 * DEDUP), can be processed immediately. Otherwise, put them on the
755 * in-memory list for later processing.
757 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
) ||
758 txg
!= spa
->spa_syncing_txg
||
759 spa_sync_pass(spa
) >= zfs_sync_pass_deferred_free
) {
760 bplist_append(&spa
->spa_free_bplist
[txg
& TXG_MASK
], bp
);
762 VERIFY0(zio_wait(zio_free_sync(NULL
, spa
, txg
, bp
, 0)));
767 zio_free_sync(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
771 enum zio_stage stage
= ZIO_FREE_PIPELINE
;
773 ASSERT(!BP_IS_HOLE(bp
));
774 ASSERT(spa_syncing_txg(spa
) == txg
);
775 ASSERT(spa_sync_pass(spa
) < zfs_sync_pass_deferred_free
);
777 if (BP_IS_EMBEDDED(bp
))
778 return (zio_null(pio
, spa
, NULL
, NULL
, NULL
, 0));
780 metaslab_check_free(spa
, bp
);
784 * GANG and DEDUP blocks can induce a read (for the gang block header,
785 * or the DDT), so issue them asynchronously so that this thread is
788 if (BP_IS_GANG(bp
) || BP_GET_DEDUP(bp
))
789 stage
|= ZIO_STAGE_ISSUE_ASYNC
;
791 zio
= zio_create(pio
, spa
, txg
, bp
, NULL
, BP_GET_PSIZE(bp
),
792 NULL
, NULL
, ZIO_TYPE_FREE
, ZIO_PRIORITY_NOW
, flags
,
793 NULL
, 0, NULL
, ZIO_STAGE_OPEN
, stage
);
799 zio_claim(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, const blkptr_t
*bp
,
800 zio_done_func_t
*done
, void *private, enum zio_flag flags
)
804 dprintf_bp(bp
, "claiming in txg %llu", txg
);
806 if (BP_IS_EMBEDDED(bp
))
807 return (zio_null(pio
, spa
, NULL
, NULL
, NULL
, 0));
810 * A claim is an allocation of a specific block. Claims are needed
811 * to support immediate writes in the intent log. The issue is that
812 * immediate writes contain committed data, but in a txg that was
813 * *not* committed. Upon opening the pool after an unclean shutdown,
814 * the intent log claims all blocks that contain immediate write data
815 * so that the SPA knows they're in use.
817 * All claims *must* be resolved in the first txg -- before the SPA
818 * starts allocating blocks -- so that nothing is allocated twice.
819 * If txg == 0 we just verify that the block is claimable.
821 ASSERT3U(spa
->spa_uberblock
.ub_rootbp
.blk_birth
, <, spa_first_txg(spa
));
822 ASSERT(txg
== spa_first_txg(spa
) || txg
== 0);
823 ASSERT(!BP_GET_DEDUP(bp
) || !spa_writeable(spa
)); /* zdb(1M) */
825 zio
= zio_create(pio
, spa
, txg
, bp
, NULL
, BP_GET_PSIZE(bp
),
826 done
, private, ZIO_TYPE_CLAIM
, ZIO_PRIORITY_NOW
, flags
,
827 NULL
, 0, NULL
, ZIO_STAGE_OPEN
, ZIO_CLAIM_PIPELINE
);
833 zio_ioctl(zio_t
*pio
, spa_t
*spa
, vdev_t
*vd
, int cmd
,
834 zio_done_func_t
*done
, void *private, enum zio_flag flags
)
839 if (vd
->vdev_children
== 0) {
840 zio
= zio_create(pio
, spa
, 0, NULL
, NULL
, 0, done
, private,
841 ZIO_TYPE_IOCTL
, ZIO_PRIORITY_NOW
, flags
, vd
, 0, NULL
,
842 ZIO_STAGE_OPEN
, ZIO_IOCTL_PIPELINE
);
846 zio
= zio_null(pio
, spa
, NULL
, NULL
, NULL
, flags
);
848 for (c
= 0; c
< vd
->vdev_children
; c
++)
849 zio_nowait(zio_ioctl(zio
, spa
, vd
->vdev_child
[c
], cmd
,
850 done
, private, flags
));
857 zio_read_phys(zio_t
*pio
, vdev_t
*vd
, uint64_t offset
, uint64_t size
,
858 void *data
, int checksum
, zio_done_func_t
*done
, void *private,
859 zio_priority_t priority
, enum zio_flag flags
, boolean_t labels
)
863 ASSERT(vd
->vdev_children
== 0);
864 ASSERT(!labels
|| offset
+ size
<= VDEV_LABEL_START_SIZE
||
865 offset
>= vd
->vdev_psize
- VDEV_LABEL_END_SIZE
);
866 ASSERT3U(offset
+ size
, <=, vd
->vdev_psize
);
868 zio
= zio_create(pio
, vd
->vdev_spa
, 0, NULL
, data
, size
, done
, private,
869 ZIO_TYPE_READ
, priority
, flags
| ZIO_FLAG_PHYSICAL
, vd
, offset
,
870 NULL
, ZIO_STAGE_OPEN
, ZIO_READ_PHYS_PIPELINE
);
872 zio
->io_prop
.zp_checksum
= checksum
;
878 zio_write_phys(zio_t
*pio
, vdev_t
*vd
, uint64_t offset
, uint64_t size
,
879 void *data
, int checksum
, zio_done_func_t
*done
, void *private,
880 zio_priority_t priority
, enum zio_flag flags
, boolean_t labels
)
884 ASSERT(vd
->vdev_children
== 0);
885 ASSERT(!labels
|| offset
+ size
<= VDEV_LABEL_START_SIZE
||
886 offset
>= vd
->vdev_psize
- VDEV_LABEL_END_SIZE
);
887 ASSERT3U(offset
+ size
, <=, vd
->vdev_psize
);
889 zio
= zio_create(pio
, vd
->vdev_spa
, 0, NULL
, data
, size
, done
, private,
890 ZIO_TYPE_WRITE
, priority
, flags
| ZIO_FLAG_PHYSICAL
, vd
, offset
,
891 NULL
, ZIO_STAGE_OPEN
, ZIO_WRITE_PHYS_PIPELINE
);
893 zio
->io_prop
.zp_checksum
= checksum
;
895 if (zio_checksum_table
[checksum
].ci_eck
) {
897 * zec checksums are necessarily destructive -- they modify
898 * the end of the write buffer to hold the verifier/checksum.
899 * Therefore, we must make a local copy in case the data is
900 * being written to multiple places in parallel.
902 void *wbuf
= zio_buf_alloc(size
);
903 bcopy(data
, wbuf
, size
);
904 zio_push_transform(zio
, wbuf
, size
, size
, NULL
);
911 * Create a child I/O to do some work for us.
914 zio_vdev_child_io(zio_t
*pio
, blkptr_t
*bp
, vdev_t
*vd
, uint64_t offset
,
915 void *data
, uint64_t size
, int type
, zio_priority_t priority
,
916 enum zio_flag flags
, zio_done_func_t
*done
, void *private)
918 enum zio_stage pipeline
= ZIO_VDEV_CHILD_PIPELINE
;
921 ASSERT(vd
->vdev_parent
==
922 (pio
->io_vd
? pio
->io_vd
: pio
->io_spa
->spa_root_vdev
));
924 if (type
== ZIO_TYPE_READ
&& bp
!= NULL
) {
926 * If we have the bp, then the child should perform the
927 * checksum and the parent need not. This pushes error
928 * detection as close to the leaves as possible and
929 * eliminates redundant checksums in the interior nodes.
931 pipeline
|= ZIO_STAGE_CHECKSUM_VERIFY
;
932 pio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
935 if (vd
->vdev_children
== 0)
936 offset
+= VDEV_LABEL_START_SIZE
;
938 flags
|= ZIO_VDEV_CHILD_FLAGS(pio
) | ZIO_FLAG_DONT_PROPAGATE
;
941 * If we've decided to do a repair, the write is not speculative --
942 * even if the original read was.
944 if (flags
& ZIO_FLAG_IO_REPAIR
)
945 flags
&= ~ZIO_FLAG_SPECULATIVE
;
947 zio
= zio_create(pio
, pio
->io_spa
, pio
->io_txg
, bp
, data
, size
,
948 done
, private, type
, priority
, flags
, vd
, offset
, &pio
->io_bookmark
,
949 ZIO_STAGE_VDEV_IO_START
>> 1, pipeline
);
951 zio
->io_physdone
= pio
->io_physdone
;
952 if (vd
->vdev_ops
->vdev_op_leaf
&& zio
->io_logical
!= NULL
)
953 zio
->io_logical
->io_phys_children
++;
959 zio_vdev_delegated_io(vdev_t
*vd
, uint64_t offset
, void *data
, uint64_t size
,
960 int type
, zio_priority_t priority
, enum zio_flag flags
,
961 zio_done_func_t
*done
, void *private)
965 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
967 zio
= zio_create(NULL
, vd
->vdev_spa
, 0, NULL
,
968 data
, size
, done
, private, type
, priority
,
969 flags
| ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_RETRY
| ZIO_FLAG_DELEGATED
,
971 ZIO_STAGE_VDEV_IO_START
>> 1, ZIO_VDEV_CHILD_PIPELINE
);
977 zio_flush(zio_t
*zio
, vdev_t
*vd
)
979 zio_nowait(zio_ioctl(zio
, zio
->io_spa
, vd
, DKIOCFLUSHWRITECACHE
,
981 ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
));
985 zio_shrink(zio_t
*zio
, uint64_t size
)
987 ASSERT(zio
->io_executor
== NULL
);
988 ASSERT(zio
->io_orig_size
== zio
->io_size
);
989 ASSERT(size
<= zio
->io_size
);
992 * We don't shrink for raidz because of problems with the
993 * reconstruction when reading back less than the block size.
994 * Note, BP_IS_RAIDZ() assumes no compression.
996 ASSERT(BP_GET_COMPRESS(zio
->io_bp
) == ZIO_COMPRESS_OFF
);
997 if (!BP_IS_RAIDZ(zio
->io_bp
))
998 zio
->io_orig_size
= zio
->io_size
= size
;
1002 * ==========================================================================
1003 * Prepare to read and write logical blocks
1004 * ==========================================================================
1008 zio_read_bp_init(zio_t
*zio
)
1010 blkptr_t
*bp
= zio
->io_bp
;
1012 if (BP_GET_COMPRESS(bp
) != ZIO_COMPRESS_OFF
&&
1013 zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1014 !(zio
->io_flags
& ZIO_FLAG_RAW
)) {
1016 BP_IS_EMBEDDED(bp
) ? BPE_GET_PSIZE(bp
) : BP_GET_PSIZE(bp
);
1017 void *cbuf
= zio_buf_alloc(psize
);
1019 zio_push_transform(zio
, cbuf
, psize
, psize
, zio_decompress
);
1022 if (BP_IS_EMBEDDED(bp
) && BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
) {
1023 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1024 decode_embedded_bp_compressed(bp
, zio
->io_data
);
1026 ASSERT(!BP_IS_EMBEDDED(bp
));
1029 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) && BP_GET_LEVEL(bp
) == 0)
1030 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1032 if (BP_GET_TYPE(bp
) == DMU_OT_DDT_ZAP
)
1033 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
;
1035 if (BP_GET_DEDUP(bp
) && zio
->io_child_type
== ZIO_CHILD_LOGICAL
)
1036 zio
->io_pipeline
= ZIO_DDT_READ_PIPELINE
;
1038 return (ZIO_PIPELINE_CONTINUE
);
1042 zio_write_bp_init(zio_t
*zio
)
1044 spa_t
*spa
= zio
->io_spa
;
1045 zio_prop_t
*zp
= &zio
->io_prop
;
1046 enum zio_compress compress
= zp
->zp_compress
;
1047 blkptr_t
*bp
= zio
->io_bp
;
1048 uint64_t lsize
= zio
->io_size
;
1049 uint64_t psize
= lsize
;
1053 * If our children haven't all reached the ready stage,
1054 * wait for them and then repeat this pipeline stage.
1056 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_READY
) ||
1057 zio_wait_for_children(zio
, ZIO_CHILD_LOGICAL
, ZIO_WAIT_READY
))
1058 return (ZIO_PIPELINE_STOP
);
1060 if (!IO_IS_ALLOCATING(zio
))
1061 return (ZIO_PIPELINE_CONTINUE
);
1063 ASSERT(zio
->io_child_type
!= ZIO_CHILD_DDT
);
1065 if (zio
->io_bp_override
) {
1066 ASSERT(bp
->blk_birth
!= zio
->io_txg
);
1067 ASSERT(BP_GET_DEDUP(zio
->io_bp_override
) == 0);
1069 *bp
= *zio
->io_bp_override
;
1070 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1072 if (BP_IS_EMBEDDED(bp
))
1073 return (ZIO_PIPELINE_CONTINUE
);
1076 * If we've been overridden and nopwrite is set then
1077 * set the flag accordingly to indicate that a nopwrite
1078 * has already occurred.
1080 if (!BP_IS_HOLE(bp
) && zp
->zp_nopwrite
) {
1081 ASSERT(!zp
->zp_dedup
);
1082 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
1083 return (ZIO_PIPELINE_CONTINUE
);
1086 ASSERT(!zp
->zp_nopwrite
);
1088 if (BP_IS_HOLE(bp
) || !zp
->zp_dedup
)
1089 return (ZIO_PIPELINE_CONTINUE
);
1091 ASSERT(zio_checksum_table
[zp
->zp_checksum
].ci_dedup
||
1092 zp
->zp_dedup_verify
);
1094 if (BP_GET_CHECKSUM(bp
) == zp
->zp_checksum
) {
1095 BP_SET_DEDUP(bp
, 1);
1096 zio
->io_pipeline
|= ZIO_STAGE_DDT_WRITE
;
1097 return (ZIO_PIPELINE_CONTINUE
);
1099 zio
->io_bp_override
= NULL
;
1103 if (!BP_IS_HOLE(bp
) && bp
->blk_birth
== zio
->io_txg
) {
1105 * We're rewriting an existing block, which means we're
1106 * working on behalf of spa_sync(). For spa_sync() to
1107 * converge, it must eventually be the case that we don't
1108 * have to allocate new blocks. But compression changes
1109 * the blocksize, which forces a reallocate, and makes
1110 * convergence take longer. Therefore, after the first
1111 * few passes, stop compressing to ensure convergence.
1113 pass
= spa_sync_pass(spa
);
1115 ASSERT(zio
->io_txg
== spa_syncing_txg(spa
));
1116 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1117 ASSERT(!BP_GET_DEDUP(bp
));
1119 if (pass
>= zfs_sync_pass_dont_compress
)
1120 compress
= ZIO_COMPRESS_OFF
;
1122 /* Make sure someone doesn't change their mind on overwrites */
1123 ASSERT(BP_IS_EMBEDDED(bp
) || MIN(zp
->zp_copies
+ BP_IS_GANG(bp
),
1124 spa_max_replication(spa
)) == BP_GET_NDVAS(bp
));
1127 if (compress
!= ZIO_COMPRESS_OFF
) {
1128 void *cbuf
= zio_buf_alloc(lsize
);
1129 psize
= zio_compress_data(compress
, zio
->io_data
, cbuf
, lsize
);
1130 if (psize
== 0 || psize
== lsize
) {
1131 compress
= ZIO_COMPRESS_OFF
;
1132 zio_buf_free(cbuf
, lsize
);
1133 } else if (!zp
->zp_dedup
&& psize
<= BPE_PAYLOAD_SIZE
&&
1134 zp
->zp_level
== 0 && !DMU_OT_HAS_FILL(zp
->zp_type
) &&
1135 spa_feature_is_enabled(spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
1136 encode_embedded_bp_compressed(bp
,
1137 cbuf
, compress
, lsize
, psize
);
1138 BPE_SET_ETYPE(bp
, BP_EMBEDDED_TYPE_DATA
);
1139 BP_SET_TYPE(bp
, zio
->io_prop
.zp_type
);
1140 BP_SET_LEVEL(bp
, zio
->io_prop
.zp_level
);
1141 zio_buf_free(cbuf
, lsize
);
1142 bp
->blk_birth
= zio
->io_txg
;
1143 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1144 ASSERT(spa_feature_is_active(spa
,
1145 SPA_FEATURE_EMBEDDED_DATA
));
1146 return (ZIO_PIPELINE_CONTINUE
);
1149 * Round up compressed size to MINBLOCKSIZE and
1153 P2ROUNDUP(psize
, (size_t)SPA_MINBLOCKSIZE
);
1154 if (rounded
> psize
) {
1155 bzero((char *)cbuf
+ psize
, rounded
- psize
);
1158 if (psize
== lsize
) {
1159 compress
= ZIO_COMPRESS_OFF
;
1160 zio_buf_free(cbuf
, lsize
);
1162 zio_push_transform(zio
, cbuf
,
1163 psize
, lsize
, NULL
);
1169 * The final pass of spa_sync() must be all rewrites, but the first
1170 * few passes offer a trade-off: allocating blocks defers convergence,
1171 * but newly allocated blocks are sequential, so they can be written
1172 * to disk faster. Therefore, we allow the first few passes of
1173 * spa_sync() to allocate new blocks, but force rewrites after that.
1174 * There should only be a handful of blocks after pass 1 in any case.
1176 if (!BP_IS_HOLE(bp
) && bp
->blk_birth
== zio
->io_txg
&&
1177 BP_GET_PSIZE(bp
) == psize
&&
1178 pass
>= zfs_sync_pass_rewrite
) {
1179 enum zio_stage gang_stages
= zio
->io_pipeline
& ZIO_GANG_STAGES
;
1181 zio
->io_pipeline
= ZIO_REWRITE_PIPELINE
| gang_stages
;
1182 zio
->io_flags
|= ZIO_FLAG_IO_REWRITE
;
1185 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
1189 if (zio
->io_bp_orig
.blk_birth
!= 0 &&
1190 spa_feature_is_active(spa
, SPA_FEATURE_HOLE_BIRTH
)) {
1191 BP_SET_LSIZE(bp
, lsize
);
1192 BP_SET_TYPE(bp
, zp
->zp_type
);
1193 BP_SET_LEVEL(bp
, zp
->zp_level
);
1194 BP_SET_BIRTH(bp
, zio
->io_txg
, 0);
1196 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1198 ASSERT(zp
->zp_checksum
!= ZIO_CHECKSUM_GANG_HEADER
);
1199 BP_SET_LSIZE(bp
, lsize
);
1200 BP_SET_TYPE(bp
, zp
->zp_type
);
1201 BP_SET_LEVEL(bp
, zp
->zp_level
);
1202 BP_SET_PSIZE(bp
, psize
);
1203 BP_SET_COMPRESS(bp
, compress
);
1204 BP_SET_CHECKSUM(bp
, zp
->zp_checksum
);
1205 BP_SET_DEDUP(bp
, zp
->zp_dedup
);
1206 BP_SET_BYTEORDER(bp
, ZFS_HOST_BYTEORDER
);
1208 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1209 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1210 zio
->io_pipeline
= ZIO_DDT_WRITE_PIPELINE
;
1212 if (zp
->zp_nopwrite
) {
1213 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1214 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
1215 zio
->io_pipeline
|= ZIO_STAGE_NOP_WRITE
;
1219 return (ZIO_PIPELINE_CONTINUE
);
1223 zio_free_bp_init(zio_t
*zio
)
1225 blkptr_t
*bp
= zio
->io_bp
;
1227 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
) {
1228 if (BP_GET_DEDUP(bp
))
1229 zio
->io_pipeline
= ZIO_DDT_FREE_PIPELINE
;
1232 return (ZIO_PIPELINE_CONTINUE
);
1236 * ==========================================================================
1237 * Execute the I/O pipeline
1238 * ==========================================================================
1242 zio_taskq_dispatch(zio_t
*zio
, zio_taskq_type_t q
, boolean_t cutinline
)
1244 spa_t
*spa
= zio
->io_spa
;
1245 zio_type_t t
= zio
->io_type
;
1246 int flags
= (cutinline
? TQ_FRONT
: 0);
1249 * If we're a config writer or a probe, the normal issue and
1250 * interrupt threads may all be blocked waiting for the config lock.
1251 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1253 if (zio
->io_flags
& (ZIO_FLAG_CONFIG_WRITER
| ZIO_FLAG_PROBE
))
1257 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1259 if (t
== ZIO_TYPE_WRITE
&& zio
->io_vd
&& zio
->io_vd
->vdev_aux
)
1263 * If this is a high priority I/O, then use the high priority taskq if
1266 if (zio
->io_priority
== ZIO_PRIORITY_NOW
&&
1267 spa
->spa_zio_taskq
[t
][q
+ 1].stqs_count
!= 0)
1270 ASSERT3U(q
, <, ZIO_TASKQ_TYPES
);
1273 * NB: We are assuming that the zio can only be dispatched
1274 * to a single taskq at a time. It would be a grievous error
1275 * to dispatch the zio to another taskq at the same time.
1277 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
1278 spa_taskq_dispatch_ent(spa
, t
, q
, (task_func_t
*)zio_execute
, zio
,
1279 flags
, &zio
->io_tqent
);
1283 zio_taskq_member(zio_t
*zio
, zio_taskq_type_t q
)
1285 kthread_t
*executor
= zio
->io_executor
;
1286 spa_t
*spa
= zio
->io_spa
;
1289 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1290 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1292 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
1293 if (taskq_member(tqs
->stqs_taskq
[i
], executor
))
1302 zio_issue_async(zio_t
*zio
)
1304 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
1306 return (ZIO_PIPELINE_STOP
);
1310 zio_interrupt(zio_t
*zio
)
1312 zio_taskq_dispatch(zio
, ZIO_TASKQ_INTERRUPT
, B_FALSE
);
1316 * Execute the I/O pipeline until one of the following occurs:
1317 * (1) the I/O completes; (2) the pipeline stalls waiting for
1318 * dependent child I/Os; (3) the I/O issues, so we're waiting
1319 * for an I/O completion interrupt; (4) the I/O is delegated by
1320 * vdev-level caching or aggregation; (5) the I/O is deferred
1321 * due to vdev-level queueing; (6) the I/O is handed off to
1322 * another thread. In all cases, the pipeline stops whenever
1323 * there's no CPU work; it never burns a thread in cv_wait_io().
1325 * There's no locking on io_stage because there's no legitimate way
1326 * for multiple threads to be attempting to process the same I/O.
1328 static zio_pipe_stage_t
*zio_pipeline
[];
1331 * zio_execute() is a wrapper around the static function
1332 * __zio_execute() so that we can force __zio_execute() to be
1333 * inlined. This reduces stack overhead which is important
1334 * because __zio_execute() is called recursively in several zio
1335 * code paths. zio_execute() itself cannot be inlined because
1336 * it is externally visible.
1339 zio_execute(zio_t
*zio
)
1341 fstrans_cookie_t cookie
;
1343 cookie
= spl_fstrans_mark();
1345 spl_fstrans_unmark(cookie
);
1348 __attribute__((always_inline
))
1350 __zio_execute(zio_t
*zio
)
1352 zio
->io_executor
= curthread
;
1354 while (zio
->io_stage
< ZIO_STAGE_DONE
) {
1355 enum zio_stage pipeline
= zio
->io_pipeline
;
1356 enum zio_stage stage
= zio
->io_stage
;
1361 ASSERT(!MUTEX_HELD(&zio
->io_lock
));
1362 ASSERT(ISP2(stage
));
1363 ASSERT(zio
->io_stall
== NULL
);
1367 } while ((stage
& pipeline
) == 0);
1369 ASSERT(stage
<= ZIO_STAGE_DONE
);
1371 dp
= spa_get_dsl(zio
->io_spa
);
1372 cut
= (stage
== ZIO_STAGE_VDEV_IO_START
) ?
1373 zio_requeue_io_start_cut_in_line
: B_FALSE
;
1376 * If we are in interrupt context and this pipeline stage
1377 * will grab a config lock that is held across I/O,
1378 * or may wait for an I/O that needs an interrupt thread
1379 * to complete, issue async to avoid deadlock.
1381 * For VDEV_IO_START, we cut in line so that the io will
1382 * be sent to disk promptly.
1384 if ((stage
& ZIO_BLOCKING_STAGES
) && zio
->io_vd
== NULL
&&
1385 zio_taskq_member(zio
, ZIO_TASKQ_INTERRUPT
)) {
1386 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, cut
);
1391 * If we executing in the context of the tx_sync_thread,
1392 * or we are performing pool initialization outside of a
1393 * zio_taskq[ZIO_TASKQ_ISSUE|ZIO_TASKQ_ISSUE_HIGH] context.
1394 * Then issue the zio asynchronously to minimize stack usage
1395 * for these deep call paths.
1397 if ((dp
&& curthread
== dp
->dp_tx
.tx_sync_thread
) ||
1398 (dp
&& spa_is_initializing(dp
->dp_spa
) &&
1399 !zio_taskq_member(zio
, ZIO_TASKQ_ISSUE
) &&
1400 !zio_taskq_member(zio
, ZIO_TASKQ_ISSUE_HIGH
))) {
1401 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, cut
);
1405 zio
->io_stage
= stage
;
1406 rv
= zio_pipeline
[highbit64(stage
) - 1](zio
);
1408 if (rv
== ZIO_PIPELINE_STOP
)
1411 ASSERT(rv
== ZIO_PIPELINE_CONTINUE
);
1417 * ==========================================================================
1418 * Initiate I/O, either sync or async
1419 * ==========================================================================
1422 zio_wait(zio_t
*zio
)
1426 ASSERT(zio
->io_stage
== ZIO_STAGE_OPEN
);
1427 ASSERT(zio
->io_executor
== NULL
);
1429 zio
->io_waiter
= curthread
;
1433 mutex_enter(&zio
->io_lock
);
1434 while (zio
->io_executor
!= NULL
)
1435 cv_wait_io(&zio
->io_cv
, &zio
->io_lock
);
1436 mutex_exit(&zio
->io_lock
);
1438 error
= zio
->io_error
;
1445 zio_nowait(zio_t
*zio
)
1447 ASSERT(zio
->io_executor
== NULL
);
1449 if (zio
->io_child_type
== ZIO_CHILD_LOGICAL
&&
1450 zio_unique_parent(zio
) == NULL
) {
1454 * This is a logical async I/O with no parent to wait for it.
1455 * We add it to the spa_async_root_zio "Godfather" I/O which
1456 * will ensure they complete prior to unloading the pool.
1458 spa_t
*spa
= zio
->io_spa
;
1460 pio
= spa
->spa_async_zio_root
[CPU_SEQID
];
1463 zio_add_child(pio
, zio
);
1470 * ==========================================================================
1471 * Reexecute or suspend/resume failed I/O
1472 * ==========================================================================
1476 zio_reexecute(zio_t
*pio
)
1478 zio_t
*cio
, *cio_next
;
1481 ASSERT(pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1482 ASSERT(pio
->io_orig_stage
== ZIO_STAGE_OPEN
);
1483 ASSERT(pio
->io_gang_leader
== NULL
);
1484 ASSERT(pio
->io_gang_tree
== NULL
);
1486 pio
->io_flags
= pio
->io_orig_flags
;
1487 pio
->io_stage
= pio
->io_orig_stage
;
1488 pio
->io_pipeline
= pio
->io_orig_pipeline
;
1489 pio
->io_reexecute
= 0;
1490 pio
->io_flags
|= ZIO_FLAG_REEXECUTED
;
1492 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
1493 pio
->io_state
[w
] = 0;
1494 for (c
= 0; c
< ZIO_CHILD_TYPES
; c
++)
1495 pio
->io_child_error
[c
] = 0;
1497 if (IO_IS_ALLOCATING(pio
))
1498 BP_ZERO(pio
->io_bp
);
1501 * As we reexecute pio's children, new children could be created.
1502 * New children go to the head of pio's io_child_list, however,
1503 * so we will (correctly) not reexecute them. The key is that
1504 * the remainder of pio's io_child_list, from 'cio_next' onward,
1505 * cannot be affected by any side effects of reexecuting 'cio'.
1507 for (cio
= zio_walk_children(pio
); cio
!= NULL
; cio
= cio_next
) {
1508 cio_next
= zio_walk_children(pio
);
1509 mutex_enter(&pio
->io_lock
);
1510 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
1511 pio
->io_children
[cio
->io_child_type
][w
]++;
1512 mutex_exit(&pio
->io_lock
);
1517 * Now that all children have been reexecuted, execute the parent.
1518 * We don't reexecute "The Godfather" I/O here as it's the
1519 * responsibility of the caller to wait on him.
1521 if (!(pio
->io_flags
& ZIO_FLAG_GODFATHER
))
1526 zio_suspend(spa_t
*spa
, zio_t
*zio
)
1528 if (spa_get_failmode(spa
) == ZIO_FAILURE_MODE_PANIC
)
1529 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1530 "failure and the failure mode property for this pool "
1531 "is set to panic.", spa_name(spa
));
1533 cmn_err(CE_WARN
, "Pool '%s' has encountered an uncorrectable I/O "
1534 "failure and has been suspended.\n", spa_name(spa
));
1536 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE
, spa
, NULL
, NULL
, 0, 0);
1538 mutex_enter(&spa
->spa_suspend_lock
);
1540 if (spa
->spa_suspend_zio_root
== NULL
)
1541 spa
->spa_suspend_zio_root
= zio_root(spa
, NULL
, NULL
,
1542 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
1543 ZIO_FLAG_GODFATHER
);
1545 spa
->spa_suspended
= B_TRUE
;
1548 ASSERT(!(zio
->io_flags
& ZIO_FLAG_GODFATHER
));
1549 ASSERT(zio
!= spa
->spa_suspend_zio_root
);
1550 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
1551 ASSERT(zio_unique_parent(zio
) == NULL
);
1552 ASSERT(zio
->io_stage
== ZIO_STAGE_DONE
);
1553 zio_add_child(spa
->spa_suspend_zio_root
, zio
);
1556 mutex_exit(&spa
->spa_suspend_lock
);
1560 zio_resume(spa_t
*spa
)
1565 * Reexecute all previously suspended i/o.
1567 mutex_enter(&spa
->spa_suspend_lock
);
1568 spa
->spa_suspended
= B_FALSE
;
1569 cv_broadcast(&spa
->spa_suspend_cv
);
1570 pio
= spa
->spa_suspend_zio_root
;
1571 spa
->spa_suspend_zio_root
= NULL
;
1572 mutex_exit(&spa
->spa_suspend_lock
);
1578 return (zio_wait(pio
));
1582 zio_resume_wait(spa_t
*spa
)
1584 mutex_enter(&spa
->spa_suspend_lock
);
1585 while (spa_suspended(spa
))
1586 cv_wait(&spa
->spa_suspend_cv
, &spa
->spa_suspend_lock
);
1587 mutex_exit(&spa
->spa_suspend_lock
);
1591 * ==========================================================================
1594 * A gang block is a collection of small blocks that looks to the DMU
1595 * like one large block. When zio_dva_allocate() cannot find a block
1596 * of the requested size, due to either severe fragmentation or the pool
1597 * being nearly full, it calls zio_write_gang_block() to construct the
1598 * block from smaller fragments.
1600 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1601 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1602 * an indirect block: it's an array of block pointers. It consumes
1603 * only one sector and hence is allocatable regardless of fragmentation.
1604 * The gang header's bps point to its gang members, which hold the data.
1606 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1607 * as the verifier to ensure uniqueness of the SHA256 checksum.
1608 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1609 * not the gang header. This ensures that data block signatures (needed for
1610 * deduplication) are independent of how the block is physically stored.
1612 * Gang blocks can be nested: a gang member may itself be a gang block.
1613 * Thus every gang block is a tree in which root and all interior nodes are
1614 * gang headers, and the leaves are normal blocks that contain user data.
1615 * The root of the gang tree is called the gang leader.
1617 * To perform any operation (read, rewrite, free, claim) on a gang block,
1618 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1619 * in the io_gang_tree field of the original logical i/o by recursively
1620 * reading the gang leader and all gang headers below it. This yields
1621 * an in-core tree containing the contents of every gang header and the
1622 * bps for every constituent of the gang block.
1624 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1625 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1626 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1627 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1628 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1629 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1630 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1631 * of the gang header plus zio_checksum_compute() of the data to update the
1632 * gang header's blk_cksum as described above.
1634 * The two-phase assemble/issue model solves the problem of partial failure --
1635 * what if you'd freed part of a gang block but then couldn't read the
1636 * gang header for another part? Assembling the entire gang tree first
1637 * ensures that all the necessary gang header I/O has succeeded before
1638 * starting the actual work of free, claim, or write. Once the gang tree
1639 * is assembled, free and claim are in-memory operations that cannot fail.
1641 * In the event that a gang write fails, zio_dva_unallocate() walks the
1642 * gang tree to immediately free (i.e. insert back into the space map)
1643 * everything we've allocated. This ensures that we don't get ENOSPC
1644 * errors during repeated suspend/resume cycles due to a flaky device.
1646 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1647 * the gang tree, we won't modify the block, so we can safely defer the free
1648 * (knowing that the block is still intact). If we *can* assemble the gang
1649 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1650 * each constituent bp and we can allocate a new block on the next sync pass.
1652 * In all cases, the gang tree allows complete recovery from partial failure.
1653 * ==========================================================================
1657 zio_read_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1662 return (zio_read(pio
, pio
->io_spa
, bp
, data
, BP_GET_PSIZE(bp
),
1663 NULL
, NULL
, pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
),
1664 &pio
->io_bookmark
));
1668 zio_rewrite_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1673 zio
= zio_rewrite(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1674 gn
->gn_gbh
, SPA_GANGBLOCKSIZE
, NULL
, NULL
, pio
->io_priority
,
1675 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
1677 * As we rewrite each gang header, the pipeline will compute
1678 * a new gang block header checksum for it; but no one will
1679 * compute a new data checksum, so we do that here. The one
1680 * exception is the gang leader: the pipeline already computed
1681 * its data checksum because that stage precedes gang assembly.
1682 * (Presently, nothing actually uses interior data checksums;
1683 * this is just good hygiene.)
1685 if (gn
!= pio
->io_gang_leader
->io_gang_tree
) {
1686 zio_checksum_compute(zio
, BP_GET_CHECKSUM(bp
),
1687 data
, BP_GET_PSIZE(bp
));
1690 * If we are here to damage data for testing purposes,
1691 * leave the GBH alone so that we can detect the damage.
1693 if (pio
->io_gang_leader
->io_flags
& ZIO_FLAG_INDUCE_DAMAGE
)
1694 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
1696 zio
= zio_rewrite(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1697 data
, BP_GET_PSIZE(bp
), NULL
, NULL
, pio
->io_priority
,
1698 ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
1706 zio_free_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1708 return (zio_free_sync(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1709 ZIO_GANG_CHILD_FLAGS(pio
)));
1714 zio_claim_gang(zio_t
*pio
, blkptr_t
*bp
, zio_gang_node_t
*gn
, void *data
)
1716 return (zio_claim(pio
, pio
->io_spa
, pio
->io_txg
, bp
,
1717 NULL
, NULL
, ZIO_GANG_CHILD_FLAGS(pio
)));
1720 static zio_gang_issue_func_t
*zio_gang_issue_func
[ZIO_TYPES
] = {
1729 static void zio_gang_tree_assemble_done(zio_t
*zio
);
1731 static zio_gang_node_t
*
1732 zio_gang_node_alloc(zio_gang_node_t
**gnpp
)
1734 zio_gang_node_t
*gn
;
1736 ASSERT(*gnpp
== NULL
);
1738 gn
= kmem_zalloc(sizeof (*gn
), KM_SLEEP
);
1739 gn
->gn_gbh
= zio_buf_alloc(SPA_GANGBLOCKSIZE
);
1746 zio_gang_node_free(zio_gang_node_t
**gnpp
)
1748 zio_gang_node_t
*gn
= *gnpp
;
1751 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
1752 ASSERT(gn
->gn_child
[g
] == NULL
);
1754 zio_buf_free(gn
->gn_gbh
, SPA_GANGBLOCKSIZE
);
1755 kmem_free(gn
, sizeof (*gn
));
1760 zio_gang_tree_free(zio_gang_node_t
**gnpp
)
1762 zio_gang_node_t
*gn
= *gnpp
;
1768 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++)
1769 zio_gang_tree_free(&gn
->gn_child
[g
]);
1771 zio_gang_node_free(gnpp
);
1775 zio_gang_tree_assemble(zio_t
*gio
, blkptr_t
*bp
, zio_gang_node_t
**gnpp
)
1777 zio_gang_node_t
*gn
= zio_gang_node_alloc(gnpp
);
1779 ASSERT(gio
->io_gang_leader
== gio
);
1780 ASSERT(BP_IS_GANG(bp
));
1782 zio_nowait(zio_read(gio
, gio
->io_spa
, bp
, gn
->gn_gbh
,
1783 SPA_GANGBLOCKSIZE
, zio_gang_tree_assemble_done
, gn
,
1784 gio
->io_priority
, ZIO_GANG_CHILD_FLAGS(gio
), &gio
->io_bookmark
));
1788 zio_gang_tree_assemble_done(zio_t
*zio
)
1790 zio_t
*gio
= zio
->io_gang_leader
;
1791 zio_gang_node_t
*gn
= zio
->io_private
;
1792 blkptr_t
*bp
= zio
->io_bp
;
1795 ASSERT(gio
== zio_unique_parent(zio
));
1796 ASSERT(zio
->io_child_count
== 0);
1801 if (BP_SHOULD_BYTESWAP(bp
))
1802 byteswap_uint64_array(zio
->io_data
, zio
->io_size
);
1804 ASSERT(zio
->io_data
== gn
->gn_gbh
);
1805 ASSERT(zio
->io_size
== SPA_GANGBLOCKSIZE
);
1806 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
1808 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
1809 blkptr_t
*gbp
= &gn
->gn_gbh
->zg_blkptr
[g
];
1810 if (!BP_IS_GANG(gbp
))
1812 zio_gang_tree_assemble(gio
, gbp
, &gn
->gn_child
[g
]);
1817 zio_gang_tree_issue(zio_t
*pio
, zio_gang_node_t
*gn
, blkptr_t
*bp
, void *data
)
1819 zio_t
*gio
= pio
->io_gang_leader
;
1823 ASSERT(BP_IS_GANG(bp
) == !!gn
);
1824 ASSERT(BP_GET_CHECKSUM(bp
) == BP_GET_CHECKSUM(gio
->io_bp
));
1825 ASSERT(BP_GET_LSIZE(bp
) == BP_GET_PSIZE(bp
) || gn
== gio
->io_gang_tree
);
1828 * If you're a gang header, your data is in gn->gn_gbh.
1829 * If you're a gang member, your data is in 'data' and gn == NULL.
1831 zio
= zio_gang_issue_func
[gio
->io_type
](pio
, bp
, gn
, data
);
1834 ASSERT(gn
->gn_gbh
->zg_tail
.zec_magic
== ZEC_MAGIC
);
1836 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
1837 blkptr_t
*gbp
= &gn
->gn_gbh
->zg_blkptr
[g
];
1838 if (BP_IS_HOLE(gbp
))
1840 zio_gang_tree_issue(zio
, gn
->gn_child
[g
], gbp
, data
);
1841 data
= (char *)data
+ BP_GET_PSIZE(gbp
);
1845 if (gn
== gio
->io_gang_tree
)
1846 ASSERT3P((char *)gio
->io_data
+ gio
->io_size
, ==, data
);
1853 zio_gang_assemble(zio_t
*zio
)
1855 blkptr_t
*bp
= zio
->io_bp
;
1857 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== NULL
);
1858 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
1860 zio
->io_gang_leader
= zio
;
1862 zio_gang_tree_assemble(zio
, bp
, &zio
->io_gang_tree
);
1864 return (ZIO_PIPELINE_CONTINUE
);
1868 zio_gang_issue(zio_t
*zio
)
1870 blkptr_t
*bp
= zio
->io_bp
;
1872 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_DONE
))
1873 return (ZIO_PIPELINE_STOP
);
1875 ASSERT(BP_IS_GANG(bp
) && zio
->io_gang_leader
== zio
);
1876 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
1878 if (zio
->io_child_error
[ZIO_CHILD_GANG
] == 0)
1879 zio_gang_tree_issue(zio
, zio
->io_gang_tree
, bp
, zio
->io_data
);
1881 zio_gang_tree_free(&zio
->io_gang_tree
);
1883 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1885 return (ZIO_PIPELINE_CONTINUE
);
1889 zio_write_gang_member_ready(zio_t
*zio
)
1891 zio_t
*pio
= zio_unique_parent(zio
);
1892 dva_t
*cdva
= zio
->io_bp
->blk_dva
;
1893 dva_t
*pdva
= pio
->io_bp
->blk_dva
;
1896 ASSERTV(zio_t
*gio
= zio
->io_gang_leader
);
1898 if (BP_IS_HOLE(zio
->io_bp
))
1901 ASSERT(BP_IS_HOLE(&zio
->io_bp_orig
));
1903 ASSERT(zio
->io_child_type
== ZIO_CHILD_GANG
);
1904 ASSERT3U(zio
->io_prop
.zp_copies
, ==, gio
->io_prop
.zp_copies
);
1905 ASSERT3U(zio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(zio
->io_bp
));
1906 ASSERT3U(pio
->io_prop
.zp_copies
, <=, BP_GET_NDVAS(pio
->io_bp
));
1907 ASSERT3U(BP_GET_NDVAS(zio
->io_bp
), <=, BP_GET_NDVAS(pio
->io_bp
));
1909 mutex_enter(&pio
->io_lock
);
1910 for (d
= 0; d
< BP_GET_NDVAS(zio
->io_bp
); d
++) {
1911 ASSERT(DVA_GET_GANG(&pdva
[d
]));
1912 asize
= DVA_GET_ASIZE(&pdva
[d
]);
1913 asize
+= DVA_GET_ASIZE(&cdva
[d
]);
1914 DVA_SET_ASIZE(&pdva
[d
], asize
);
1916 mutex_exit(&pio
->io_lock
);
1920 zio_write_gang_block(zio_t
*pio
)
1922 spa_t
*spa
= pio
->io_spa
;
1923 blkptr_t
*bp
= pio
->io_bp
;
1924 zio_t
*gio
= pio
->io_gang_leader
;
1926 zio_gang_node_t
*gn
, **gnpp
;
1927 zio_gbh_phys_t
*gbh
;
1928 uint64_t txg
= pio
->io_txg
;
1929 uint64_t resid
= pio
->io_size
;
1931 int copies
= gio
->io_prop
.zp_copies
;
1932 int gbh_copies
= MIN(copies
+ 1, spa_max_replication(spa
));
1936 error
= metaslab_alloc(spa
, spa_normal_class(spa
), SPA_GANGBLOCKSIZE
,
1937 bp
, gbh_copies
, txg
, pio
== gio
? NULL
: gio
->io_bp
,
1938 METASLAB_HINTBP_FAVOR
| METASLAB_GANG_HEADER
);
1940 pio
->io_error
= error
;
1941 return (ZIO_PIPELINE_CONTINUE
);
1945 gnpp
= &gio
->io_gang_tree
;
1947 gnpp
= pio
->io_private
;
1948 ASSERT(pio
->io_ready
== zio_write_gang_member_ready
);
1951 gn
= zio_gang_node_alloc(gnpp
);
1953 bzero(gbh
, SPA_GANGBLOCKSIZE
);
1956 * Create the gang header.
1958 zio
= zio_rewrite(pio
, spa
, txg
, bp
, gbh
, SPA_GANGBLOCKSIZE
, NULL
, NULL
,
1959 pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
), &pio
->io_bookmark
);
1962 * Create and nowait the gang children.
1964 for (g
= 0; resid
!= 0; resid
-= lsize
, g
++) {
1965 lsize
= P2ROUNDUP(resid
/ (SPA_GBH_NBLKPTRS
- g
),
1967 ASSERT(lsize
>= SPA_MINBLOCKSIZE
&& lsize
<= resid
);
1969 zp
.zp_checksum
= gio
->io_prop
.zp_checksum
;
1970 zp
.zp_compress
= ZIO_COMPRESS_OFF
;
1971 zp
.zp_type
= DMU_OT_NONE
;
1973 zp
.zp_copies
= gio
->io_prop
.zp_copies
;
1974 zp
.zp_dedup
= B_FALSE
;
1975 zp
.zp_dedup_verify
= B_FALSE
;
1976 zp
.zp_nopwrite
= B_FALSE
;
1978 zio_nowait(zio_write(zio
, spa
, txg
, &gbh
->zg_blkptr
[g
],
1979 (char *)pio
->io_data
+ (pio
->io_size
- resid
), lsize
, &zp
,
1980 zio_write_gang_member_ready
, NULL
, NULL
, &gn
->gn_child
[g
],
1981 pio
->io_priority
, ZIO_GANG_CHILD_FLAGS(pio
),
1982 &pio
->io_bookmark
));
1986 * Set pio's pipeline to just wait for zio to finish.
1988 pio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
1991 * We didn't allocate this bp, so make sure it doesn't get unmarked.
1993 pio
->io_flags
&= ~ZIO_FLAG_FASTWRITE
;
1997 return (ZIO_PIPELINE_CONTINUE
);
2001 * The zio_nop_write stage in the pipeline determines if allocating
2002 * a new bp is necessary. By leveraging a cryptographically secure checksum,
2003 * such as SHA256, we can compare the checksums of the new data and the old
2004 * to determine if allocating a new block is required. The nopwrite
2005 * feature can handle writes in either syncing or open context (i.e. zil
2006 * writes) and as a result is mutually exclusive with dedup.
2009 zio_nop_write(zio_t
*zio
)
2011 blkptr_t
*bp
= zio
->io_bp
;
2012 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
2013 zio_prop_t
*zp
= &zio
->io_prop
;
2015 ASSERT(BP_GET_LEVEL(bp
) == 0);
2016 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
));
2017 ASSERT(zp
->zp_nopwrite
);
2018 ASSERT(!zp
->zp_dedup
);
2019 ASSERT(zio
->io_bp_override
== NULL
);
2020 ASSERT(IO_IS_ALLOCATING(zio
));
2023 * Check to see if the original bp and the new bp have matching
2024 * characteristics (i.e. same checksum, compression algorithms, etc).
2025 * If they don't then just continue with the pipeline which will
2026 * allocate a new bp.
2028 if (BP_IS_HOLE(bp_orig
) ||
2029 !zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_dedup
||
2030 BP_GET_CHECKSUM(bp
) != BP_GET_CHECKSUM(bp_orig
) ||
2031 BP_GET_COMPRESS(bp
) != BP_GET_COMPRESS(bp_orig
) ||
2032 BP_GET_DEDUP(bp
) != BP_GET_DEDUP(bp_orig
) ||
2033 zp
->zp_copies
!= BP_GET_NDVAS(bp_orig
))
2034 return (ZIO_PIPELINE_CONTINUE
);
2037 * If the checksums match then reset the pipeline so that we
2038 * avoid allocating a new bp and issuing any I/O.
2040 if (ZIO_CHECKSUM_EQUAL(bp
->blk_cksum
, bp_orig
->blk_cksum
)) {
2041 ASSERT(zio_checksum_table
[zp
->zp_checksum
].ci_dedup
);
2042 ASSERT3U(BP_GET_PSIZE(bp
), ==, BP_GET_PSIZE(bp_orig
));
2043 ASSERT3U(BP_GET_LSIZE(bp
), ==, BP_GET_LSIZE(bp_orig
));
2044 ASSERT(zp
->zp_compress
!= ZIO_COMPRESS_OFF
);
2045 ASSERT(bcmp(&bp
->blk_prop
, &bp_orig
->blk_prop
,
2046 sizeof (uint64_t)) == 0);
2049 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2050 zio
->io_flags
|= ZIO_FLAG_NOPWRITE
;
2053 return (ZIO_PIPELINE_CONTINUE
);
2057 * ==========================================================================
2059 * ==========================================================================
2062 zio_ddt_child_read_done(zio_t
*zio
)
2064 blkptr_t
*bp
= zio
->io_bp
;
2065 ddt_entry_t
*dde
= zio
->io_private
;
2067 zio_t
*pio
= zio_unique_parent(zio
);
2069 mutex_enter(&pio
->io_lock
);
2070 ddp
= ddt_phys_select(dde
, bp
);
2071 if (zio
->io_error
== 0)
2072 ddt_phys_clear(ddp
); /* this ddp doesn't need repair */
2073 if (zio
->io_error
== 0 && dde
->dde_repair_data
== NULL
)
2074 dde
->dde_repair_data
= zio
->io_data
;
2076 zio_buf_free(zio
->io_data
, zio
->io_size
);
2077 mutex_exit(&pio
->io_lock
);
2081 zio_ddt_read_start(zio_t
*zio
)
2083 blkptr_t
*bp
= zio
->io_bp
;
2086 ASSERT(BP_GET_DEDUP(bp
));
2087 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2088 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2090 if (zio
->io_child_error
[ZIO_CHILD_DDT
]) {
2091 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2092 ddt_entry_t
*dde
= ddt_repair_start(ddt
, bp
);
2093 ddt_phys_t
*ddp
= dde
->dde_phys
;
2094 ddt_phys_t
*ddp_self
= ddt_phys_select(dde
, bp
);
2097 ASSERT(zio
->io_vsd
== NULL
);
2100 if (ddp_self
== NULL
)
2101 return (ZIO_PIPELINE_CONTINUE
);
2103 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
2104 if (ddp
->ddp_phys_birth
== 0 || ddp
== ddp_self
)
2106 ddt_bp_create(ddt
->ddt_checksum
, &dde
->dde_key
, ddp
,
2108 zio_nowait(zio_read(zio
, zio
->io_spa
, &blk
,
2109 zio_buf_alloc(zio
->io_size
), zio
->io_size
,
2110 zio_ddt_child_read_done
, dde
, zio
->io_priority
,
2111 ZIO_DDT_CHILD_FLAGS(zio
) | ZIO_FLAG_DONT_PROPAGATE
,
2112 &zio
->io_bookmark
));
2114 return (ZIO_PIPELINE_CONTINUE
);
2117 zio_nowait(zio_read(zio
, zio
->io_spa
, bp
,
2118 zio
->io_data
, zio
->io_size
, NULL
, NULL
, zio
->io_priority
,
2119 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
));
2121 return (ZIO_PIPELINE_CONTINUE
);
2125 zio_ddt_read_done(zio_t
*zio
)
2127 blkptr_t
*bp
= zio
->io_bp
;
2129 if (zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_DONE
))
2130 return (ZIO_PIPELINE_STOP
);
2132 ASSERT(BP_GET_DEDUP(bp
));
2133 ASSERT(BP_GET_PSIZE(bp
) == zio
->io_size
);
2134 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2136 if (zio
->io_child_error
[ZIO_CHILD_DDT
]) {
2137 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2138 ddt_entry_t
*dde
= zio
->io_vsd
;
2140 ASSERT(spa_load_state(zio
->io_spa
) != SPA_LOAD_NONE
);
2141 return (ZIO_PIPELINE_CONTINUE
);
2144 zio
->io_stage
= ZIO_STAGE_DDT_READ_START
>> 1;
2145 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
, B_FALSE
);
2146 return (ZIO_PIPELINE_STOP
);
2148 if (dde
->dde_repair_data
!= NULL
) {
2149 bcopy(dde
->dde_repair_data
, zio
->io_data
, zio
->io_size
);
2150 zio
->io_child_error
[ZIO_CHILD_DDT
] = 0;
2152 ddt_repair_done(ddt
, dde
);
2156 ASSERT(zio
->io_vsd
== NULL
);
2158 return (ZIO_PIPELINE_CONTINUE
);
2162 zio_ddt_collision(zio_t
*zio
, ddt_t
*ddt
, ddt_entry_t
*dde
)
2164 spa_t
*spa
= zio
->io_spa
;
2168 * Note: we compare the original data, not the transformed data,
2169 * because when zio->io_bp is an override bp, we will not have
2170 * pushed the I/O transforms. That's an important optimization
2171 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2173 for (p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2174 zio_t
*lio
= dde
->dde_lead_zio
[p
];
2177 return (lio
->io_orig_size
!= zio
->io_orig_size
||
2178 bcmp(zio
->io_orig_data
, lio
->io_orig_data
,
2179 zio
->io_orig_size
) != 0);
2183 for (p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++) {
2184 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2186 if (ddp
->ddp_phys_birth
!= 0) {
2187 arc_buf_t
*abuf
= NULL
;
2188 uint32_t aflags
= ARC_WAIT
;
2189 blkptr_t blk
= *zio
->io_bp
;
2192 ddt_bp_fill(ddp
, &blk
, ddp
->ddp_phys_birth
);
2196 error
= arc_read(NULL
, spa
, &blk
,
2197 arc_getbuf_func
, &abuf
, ZIO_PRIORITY_SYNC_READ
,
2198 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2199 &aflags
, &zio
->io_bookmark
);
2202 if (arc_buf_size(abuf
) != zio
->io_orig_size
||
2203 bcmp(abuf
->b_data
, zio
->io_orig_data
,
2204 zio
->io_orig_size
) != 0)
2205 error
= SET_ERROR(EEXIST
);
2206 VERIFY(arc_buf_remove_ref(abuf
, &abuf
));
2210 return (error
!= 0);
2218 zio_ddt_child_write_ready(zio_t
*zio
)
2220 int p
= zio
->io_prop
.zp_copies
;
2221 ddt_t
*ddt
= ddt_select(zio
->io_spa
, zio
->io_bp
);
2222 ddt_entry_t
*dde
= zio
->io_private
;
2223 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2231 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2233 ddt_phys_fill(ddp
, zio
->io_bp
);
2235 while ((pio
= zio_walk_parents(zio
)) != NULL
)
2236 ddt_bp_fill(ddp
, pio
->io_bp
, zio
->io_txg
);
2242 zio_ddt_child_write_done(zio_t
*zio
)
2244 int p
= zio
->io_prop
.zp_copies
;
2245 ddt_t
*ddt
= ddt_select(zio
->io_spa
, zio
->io_bp
);
2246 ddt_entry_t
*dde
= zio
->io_private
;
2247 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
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 while (zio_walk_parents(zio
) != NULL
)
2257 ddt_phys_addref(ddp
);
2259 ddt_phys_clear(ddp
);
2266 zio_ddt_ditto_write_done(zio_t
*zio
)
2268 int p
= DDT_PHYS_DITTO
;
2269 blkptr_t
*bp
= zio
->io_bp
;
2270 ddt_t
*ddt
= ddt_select(zio
->io_spa
, bp
);
2271 ddt_entry_t
*dde
= zio
->io_private
;
2272 ddt_phys_t
*ddp
= &dde
->dde_phys
[p
];
2273 ddt_key_t
*ddk
= &dde
->dde_key
;
2274 ASSERTV(zio_prop_t
*zp
= &zio
->io_prop
);
2278 ASSERT(ddp
->ddp_refcnt
== 0);
2279 ASSERT(dde
->dde_lead_zio
[p
] == zio
);
2280 dde
->dde_lead_zio
[p
] = NULL
;
2282 if (zio
->io_error
== 0) {
2283 ASSERT(ZIO_CHECKSUM_EQUAL(bp
->blk_cksum
, ddk
->ddk_cksum
));
2284 ASSERT(zp
->zp_copies
< SPA_DVAS_PER_BP
);
2285 ASSERT(zp
->zp_copies
== BP_GET_NDVAS(bp
) - BP_IS_GANG(bp
));
2286 if (ddp
->ddp_phys_birth
!= 0)
2287 ddt_phys_free(ddt
, ddk
, ddp
, zio
->io_txg
);
2288 ddt_phys_fill(ddp
, bp
);
2295 zio_ddt_write(zio_t
*zio
)
2297 spa_t
*spa
= zio
->io_spa
;
2298 blkptr_t
*bp
= zio
->io_bp
;
2299 uint64_t txg
= zio
->io_txg
;
2300 zio_prop_t
*zp
= &zio
->io_prop
;
2301 int p
= zp
->zp_copies
;
2305 ddt_t
*ddt
= ddt_select(spa
, bp
);
2309 ASSERT(BP_GET_DEDUP(bp
));
2310 ASSERT(BP_GET_CHECKSUM(bp
) == zp
->zp_checksum
);
2311 ASSERT(BP_IS_HOLE(bp
) || zio
->io_bp_override
);
2314 dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
2315 ddp
= &dde
->dde_phys
[p
];
2317 if (zp
->zp_dedup_verify
&& zio_ddt_collision(zio
, ddt
, dde
)) {
2319 * If we're using a weak checksum, upgrade to a strong checksum
2320 * and try again. If we're already using a strong checksum,
2321 * we can't resolve it, so just convert to an ordinary write.
2322 * (And automatically e-mail a paper to Nature?)
2324 if (!zio_checksum_table
[zp
->zp_checksum
].ci_dedup
) {
2325 zp
->zp_checksum
= spa_dedup_checksum(spa
);
2326 zio_pop_transforms(zio
);
2327 zio
->io_stage
= ZIO_STAGE_OPEN
;
2330 zp
->zp_dedup
= B_FALSE
;
2332 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
2334 return (ZIO_PIPELINE_CONTINUE
);
2337 ditto_copies
= ddt_ditto_copies_needed(ddt
, dde
, ddp
);
2338 ASSERT(ditto_copies
< SPA_DVAS_PER_BP
);
2340 if (ditto_copies
> ddt_ditto_copies_present(dde
) &&
2341 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] == NULL
) {
2342 zio_prop_t czp
= *zp
;
2344 czp
.zp_copies
= ditto_copies
;
2347 * If we arrived here with an override bp, we won't have run
2348 * the transform stack, so we won't have the data we need to
2349 * generate a child i/o. So, toss the override bp and restart.
2350 * This is safe, because using the override bp is just an
2351 * optimization; and it's rare, so the cost doesn't matter.
2353 if (zio
->io_bp_override
) {
2354 zio_pop_transforms(zio
);
2355 zio
->io_stage
= ZIO_STAGE_OPEN
;
2356 zio
->io_pipeline
= ZIO_WRITE_PIPELINE
;
2357 zio
->io_bp_override
= NULL
;
2360 return (ZIO_PIPELINE_CONTINUE
);
2363 dio
= zio_write(zio
, spa
, txg
, bp
, zio
->io_orig_data
,
2364 zio
->io_orig_size
, &czp
, NULL
, NULL
,
2365 zio_ddt_ditto_write_done
, dde
, zio
->io_priority
,
2366 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
);
2368 zio_push_transform(dio
, zio
->io_data
, zio
->io_size
, 0, NULL
);
2369 dde
->dde_lead_zio
[DDT_PHYS_DITTO
] = dio
;
2372 if (ddp
->ddp_phys_birth
!= 0 || dde
->dde_lead_zio
[p
] != NULL
) {
2373 if (ddp
->ddp_phys_birth
!= 0)
2374 ddt_bp_fill(ddp
, bp
, txg
);
2375 if (dde
->dde_lead_zio
[p
] != NULL
)
2376 zio_add_child(zio
, dde
->dde_lead_zio
[p
]);
2378 ddt_phys_addref(ddp
);
2379 } else if (zio
->io_bp_override
) {
2380 ASSERT(bp
->blk_birth
== txg
);
2381 ASSERT(BP_EQUAL(bp
, zio
->io_bp_override
));
2382 ddt_phys_fill(ddp
, bp
);
2383 ddt_phys_addref(ddp
);
2385 cio
= zio_write(zio
, spa
, txg
, bp
, zio
->io_orig_data
,
2386 zio
->io_orig_size
, zp
, zio_ddt_child_write_ready
, NULL
,
2387 zio_ddt_child_write_done
, dde
, zio
->io_priority
,
2388 ZIO_DDT_CHILD_FLAGS(zio
), &zio
->io_bookmark
);
2390 zio_push_transform(cio
, zio
->io_data
, zio
->io_size
, 0, NULL
);
2391 dde
->dde_lead_zio
[p
] = cio
;
2401 return (ZIO_PIPELINE_CONTINUE
);
2404 ddt_entry_t
*freedde
; /* for debugging */
2407 zio_ddt_free(zio_t
*zio
)
2409 spa_t
*spa
= zio
->io_spa
;
2410 blkptr_t
*bp
= zio
->io_bp
;
2411 ddt_t
*ddt
= ddt_select(spa
, bp
);
2415 ASSERT(BP_GET_DEDUP(bp
));
2416 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
2419 freedde
= dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
2421 ddp
= ddt_phys_select(dde
, bp
);
2423 ddt_phys_decref(ddp
);
2427 return (ZIO_PIPELINE_CONTINUE
);
2431 * ==========================================================================
2432 * Allocate and free blocks
2433 * ==========================================================================
2436 zio_dva_allocate(zio_t
*zio
)
2438 spa_t
*spa
= zio
->io_spa
;
2439 metaslab_class_t
*mc
= spa_normal_class(spa
);
2440 blkptr_t
*bp
= zio
->io_bp
;
2444 if (zio
->io_gang_leader
== NULL
) {
2445 ASSERT(zio
->io_child_type
> ZIO_CHILD_GANG
);
2446 zio
->io_gang_leader
= zio
;
2449 ASSERT(BP_IS_HOLE(bp
));
2450 ASSERT0(BP_GET_NDVAS(bp
));
2451 ASSERT3U(zio
->io_prop
.zp_copies
, >, 0);
2452 ASSERT3U(zio
->io_prop
.zp_copies
, <=, spa_max_replication(spa
));
2453 ASSERT3U(zio
->io_size
, ==, BP_GET_PSIZE(bp
));
2456 * The dump device does not support gang blocks so allocation on
2457 * behalf of the dump device (i.e. ZIO_FLAG_NODATA) must avoid
2458 * the "fast" gang feature.
2460 flags
|= (zio
->io_flags
& ZIO_FLAG_NODATA
) ? METASLAB_GANG_AVOID
: 0;
2461 flags
|= (zio
->io_flags
& ZIO_FLAG_GANG_CHILD
) ?
2462 METASLAB_GANG_CHILD
: 0;
2463 flags
|= (zio
->io_flags
& ZIO_FLAG_FASTWRITE
) ? METASLAB_FASTWRITE
: 0;
2464 error
= metaslab_alloc(spa
, mc
, zio
->io_size
, bp
,
2465 zio
->io_prop
.zp_copies
, zio
->io_txg
, NULL
, flags
);
2468 spa_dbgmsg(spa
, "%s: metaslab allocation failure: zio %p, "
2469 "size %llu, error %d", spa_name(spa
), zio
, zio
->io_size
,
2471 if (error
== ENOSPC
&& zio
->io_size
> SPA_MINBLOCKSIZE
)
2472 return (zio_write_gang_block(zio
));
2473 zio
->io_error
= error
;
2476 return (ZIO_PIPELINE_CONTINUE
);
2480 zio_dva_free(zio_t
*zio
)
2482 metaslab_free(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
, B_FALSE
);
2484 return (ZIO_PIPELINE_CONTINUE
);
2488 zio_dva_claim(zio_t
*zio
)
2492 error
= metaslab_claim(zio
->io_spa
, zio
->io_bp
, zio
->io_txg
);
2494 zio
->io_error
= error
;
2496 return (ZIO_PIPELINE_CONTINUE
);
2500 * Undo an allocation. This is used by zio_done() when an I/O fails
2501 * and we want to give back the block we just allocated.
2502 * This handles both normal blocks and gang blocks.
2505 zio_dva_unallocate(zio_t
*zio
, zio_gang_node_t
*gn
, blkptr_t
*bp
)
2509 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
));
2510 ASSERT(zio
->io_bp_override
== NULL
);
2512 if (!BP_IS_HOLE(bp
))
2513 metaslab_free(zio
->io_spa
, bp
, bp
->blk_birth
, B_TRUE
);
2516 for (g
= 0; g
< SPA_GBH_NBLKPTRS
; g
++) {
2517 zio_dva_unallocate(zio
, gn
->gn_child
[g
],
2518 &gn
->gn_gbh
->zg_blkptr
[g
]);
2524 * Try to allocate an intent log block. Return 0 on success, errno on failure.
2527 zio_alloc_zil(spa_t
*spa
, uint64_t txg
, blkptr_t
*new_bp
, uint64_t size
,
2532 ASSERT(txg
> spa_syncing_txg(spa
));
2535 * ZIL blocks are always contiguous (i.e. not gang blocks) so we
2536 * set the METASLAB_GANG_AVOID flag so that they don't "fast gang"
2537 * when allocating them.
2540 error
= metaslab_alloc(spa
, spa_log_class(spa
), size
,
2541 new_bp
, 1, txg
, NULL
,
2542 METASLAB_FASTWRITE
| METASLAB_GANG_AVOID
);
2546 error
= metaslab_alloc(spa
, spa_normal_class(spa
), size
,
2547 new_bp
, 1, txg
, NULL
,
2548 METASLAB_FASTWRITE
);
2552 BP_SET_LSIZE(new_bp
, size
);
2553 BP_SET_PSIZE(new_bp
, size
);
2554 BP_SET_COMPRESS(new_bp
, ZIO_COMPRESS_OFF
);
2555 BP_SET_CHECKSUM(new_bp
,
2556 spa_version(spa
) >= SPA_VERSION_SLIM_ZIL
2557 ? ZIO_CHECKSUM_ZILOG2
: ZIO_CHECKSUM_ZILOG
);
2558 BP_SET_TYPE(new_bp
, DMU_OT_INTENT_LOG
);
2559 BP_SET_LEVEL(new_bp
, 0);
2560 BP_SET_DEDUP(new_bp
, 0);
2561 BP_SET_BYTEORDER(new_bp
, ZFS_HOST_BYTEORDER
);
2568 * Free an intent log block.
2571 zio_free_zil(spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
)
2573 ASSERT(BP_GET_TYPE(bp
) == DMU_OT_INTENT_LOG
);
2574 ASSERT(!BP_IS_GANG(bp
));
2576 zio_free(spa
, txg
, bp
);
2580 * ==========================================================================
2581 * Read and write to physical devices
2582 * ==========================================================================
2585 zio_vdev_io_start(zio_t
*zio
)
2587 vdev_t
*vd
= zio
->io_vd
;
2589 spa_t
*spa
= zio
->io_spa
;
2591 ASSERT(zio
->io_error
== 0);
2592 ASSERT(zio
->io_child_error
[ZIO_CHILD_VDEV
] == 0);
2595 if (!(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
2596 spa_config_enter(spa
, SCL_ZIO
, zio
, RW_READER
);
2599 * The mirror_ops handle multiple DVAs in a single BP.
2601 return (vdev_mirror_ops
.vdev_op_io_start(zio
));
2605 * We keep track of time-sensitive I/Os so that the scan thread
2606 * can quickly react to certain workloads. In particular, we care
2607 * about non-scrubbing, top-level reads and writes with the following
2609 * - synchronous writes of user data to non-slog devices
2610 * - any reads of user data
2611 * When these conditions are met, adjust the timestamp of spa_last_io
2612 * which allows the scan thread to adjust its workload accordingly.
2614 if (!(zio
->io_flags
& ZIO_FLAG_SCAN_THREAD
) && zio
->io_bp
!= NULL
&&
2615 vd
== vd
->vdev_top
&& !vd
->vdev_islog
&&
2616 zio
->io_bookmark
.zb_objset
!= DMU_META_OBJSET
&&
2617 zio
->io_txg
!= spa_syncing_txg(spa
)) {
2618 uint64_t old
= spa
->spa_last_io
;
2619 uint64_t new = ddi_get_lbolt64();
2621 (void) atomic_cas_64(&spa
->spa_last_io
, old
, new);
2624 align
= 1ULL << vd
->vdev_top
->vdev_ashift
;
2626 if (!(zio
->io_flags
& ZIO_FLAG_PHYSICAL
) &&
2627 P2PHASE(zio
->io_size
, align
) != 0) {
2628 /* Transform logical writes to be a full physical block size. */
2629 uint64_t asize
= P2ROUNDUP(zio
->io_size
, align
);
2630 char *abuf
= zio_buf_alloc(asize
);
2631 ASSERT(vd
== vd
->vdev_top
);
2632 if (zio
->io_type
== ZIO_TYPE_WRITE
) {
2633 bcopy(zio
->io_data
, abuf
, zio
->io_size
);
2634 bzero(abuf
+ zio
->io_size
, asize
- zio
->io_size
);
2636 zio_push_transform(zio
, abuf
, asize
, asize
, zio_subblock
);
2640 * If this is not a physical io, make sure that it is properly aligned
2641 * before proceeding.
2643 if (!(zio
->io_flags
& ZIO_FLAG_PHYSICAL
)) {
2644 ASSERT0(P2PHASE(zio
->io_offset
, align
));
2645 ASSERT0(P2PHASE(zio
->io_size
, align
));
2648 * For physical writes, we allow 512b aligned writes and assume
2649 * the device will perform a read-modify-write as necessary.
2651 ASSERT0(P2PHASE(zio
->io_offset
, SPA_MINBLOCKSIZE
));
2652 ASSERT0(P2PHASE(zio
->io_size
, SPA_MINBLOCKSIZE
));
2655 VERIFY(zio
->io_type
!= ZIO_TYPE_WRITE
|| spa_writeable(spa
));
2658 * If this is a repair I/O, and there's no self-healing involved --
2659 * that is, we're just resilvering what we expect to resilver --
2660 * then don't do the I/O unless zio's txg is actually in vd's DTL.
2661 * This prevents spurious resilvering with nested replication.
2662 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
2663 * A is out of date, we'll read from C+D, then use the data to
2664 * resilver A+B -- but we don't actually want to resilver B, just A.
2665 * The top-level mirror has no way to know this, so instead we just
2666 * discard unnecessary repairs as we work our way down the vdev tree.
2667 * The same logic applies to any form of nested replication:
2668 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
2670 if ((zio
->io_flags
& ZIO_FLAG_IO_REPAIR
) &&
2671 !(zio
->io_flags
& ZIO_FLAG_SELF_HEAL
) &&
2672 zio
->io_txg
!= 0 && /* not a delegated i/o */
2673 !vdev_dtl_contains(vd
, DTL_PARTIAL
, zio
->io_txg
, 1)) {
2674 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
2675 zio_vdev_io_bypass(zio
);
2676 return (ZIO_PIPELINE_CONTINUE
);
2679 if (vd
->vdev_ops
->vdev_op_leaf
&&
2680 (zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
)) {
2682 if (zio
->io_type
== ZIO_TYPE_READ
&& vdev_cache_read(zio
))
2683 return (ZIO_PIPELINE_CONTINUE
);
2685 if ((zio
= vdev_queue_io(zio
)) == NULL
)
2686 return (ZIO_PIPELINE_STOP
);
2688 if (!vdev_accessible(vd
, zio
)) {
2689 zio
->io_error
= SET_ERROR(ENXIO
);
2691 return (ZIO_PIPELINE_STOP
);
2695 return (vd
->vdev_ops
->vdev_op_io_start(zio
));
2699 zio_vdev_io_done(zio_t
*zio
)
2701 vdev_t
*vd
= zio
->io_vd
;
2702 vdev_ops_t
*ops
= vd
? vd
->vdev_ops
: &vdev_mirror_ops
;
2703 boolean_t unexpected_error
= B_FALSE
;
2705 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
))
2706 return (ZIO_PIPELINE_STOP
);
2708 ASSERT(zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
);
2710 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
) {
2712 vdev_queue_io_done(zio
);
2714 if (zio
->io_type
== ZIO_TYPE_WRITE
)
2715 vdev_cache_write(zio
);
2717 if (zio_injection_enabled
&& zio
->io_error
== 0)
2718 zio
->io_error
= zio_handle_device_injection(vd
,
2721 if (zio_injection_enabled
&& zio
->io_error
== 0)
2722 zio
->io_error
= zio_handle_label_injection(zio
, EIO
);
2724 if (zio
->io_error
) {
2725 if (!vdev_accessible(vd
, zio
)) {
2726 zio
->io_error
= SET_ERROR(ENXIO
);
2728 unexpected_error
= B_TRUE
;
2733 ops
->vdev_op_io_done(zio
);
2735 if (unexpected_error
)
2736 VERIFY(vdev_probe(vd
, zio
) == NULL
);
2738 return (ZIO_PIPELINE_CONTINUE
);
2742 * For non-raidz ZIOs, we can just copy aside the bad data read from the
2743 * disk, and use that to finish the checksum ereport later.
2746 zio_vsd_default_cksum_finish(zio_cksum_report_t
*zcr
,
2747 const void *good_buf
)
2749 /* no processing needed */
2750 zfs_ereport_finish_checksum(zcr
, good_buf
, zcr
->zcr_cbdata
, B_FALSE
);
2755 zio_vsd_default_cksum_report(zio_t
*zio
, zio_cksum_report_t
*zcr
, void *ignored
)
2757 void *buf
= zio_buf_alloc(zio
->io_size
);
2759 bcopy(zio
->io_data
, buf
, zio
->io_size
);
2761 zcr
->zcr_cbinfo
= zio
->io_size
;
2762 zcr
->zcr_cbdata
= buf
;
2763 zcr
->zcr_finish
= zio_vsd_default_cksum_finish
;
2764 zcr
->zcr_free
= zio_buf_free
;
2768 zio_vdev_io_assess(zio_t
*zio
)
2770 vdev_t
*vd
= zio
->io_vd
;
2772 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
))
2773 return (ZIO_PIPELINE_STOP
);
2775 if (vd
== NULL
&& !(zio
->io_flags
& ZIO_FLAG_CONFIG_WRITER
))
2776 spa_config_exit(zio
->io_spa
, SCL_ZIO
, zio
);
2778 if (zio
->io_vsd
!= NULL
) {
2779 zio
->io_vsd_ops
->vsd_free(zio
);
2783 if (zio_injection_enabled
&& zio
->io_error
== 0)
2784 zio
->io_error
= zio_handle_fault_injection(zio
, EIO
);
2787 * If the I/O failed, determine whether we should attempt to retry it.
2789 * On retry, we cut in line in the issue queue, since we don't want
2790 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
2792 if (zio
->io_error
&& vd
== NULL
&&
2793 !(zio
->io_flags
& (ZIO_FLAG_DONT_RETRY
| ZIO_FLAG_IO_RETRY
))) {
2794 ASSERT(!(zio
->io_flags
& ZIO_FLAG_DONT_QUEUE
)); /* not a leaf */
2795 ASSERT(!(zio
->io_flags
& ZIO_FLAG_IO_BYPASS
)); /* not a leaf */
2797 zio
->io_flags
|= ZIO_FLAG_IO_RETRY
|
2798 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
;
2799 zio
->io_stage
= ZIO_STAGE_VDEV_IO_START
>> 1;
2800 zio_taskq_dispatch(zio
, ZIO_TASKQ_ISSUE
,
2801 zio_requeue_io_start_cut_in_line
);
2802 return (ZIO_PIPELINE_STOP
);
2806 * If we got an error on a leaf device, convert it to ENXIO
2807 * if the device is not accessible at all.
2809 if (zio
->io_error
&& vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
&&
2810 !vdev_accessible(vd
, zio
))
2811 zio
->io_error
= SET_ERROR(ENXIO
);
2814 * If we can't write to an interior vdev (mirror or RAID-Z),
2815 * set vdev_cant_write so that we stop trying to allocate from it.
2817 if (zio
->io_error
== ENXIO
&& zio
->io_type
== ZIO_TYPE_WRITE
&&
2818 vd
!= NULL
&& !vd
->vdev_ops
->vdev_op_leaf
) {
2819 vd
->vdev_cant_write
= B_TRUE
;
2823 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2825 if (vd
!= NULL
&& vd
->vdev_ops
->vdev_op_leaf
&&
2826 zio
->io_physdone
!= NULL
) {
2827 ASSERT(!(zio
->io_flags
& ZIO_FLAG_DELEGATED
));
2828 ASSERT(zio
->io_child_type
== ZIO_CHILD_VDEV
);
2829 zio
->io_physdone(zio
->io_logical
);
2832 return (ZIO_PIPELINE_CONTINUE
);
2836 zio_vdev_io_reissue(zio_t
*zio
)
2838 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
2839 ASSERT(zio
->io_error
== 0);
2841 zio
->io_stage
>>= 1;
2845 zio_vdev_io_redone(zio_t
*zio
)
2847 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_DONE
);
2849 zio
->io_stage
>>= 1;
2853 zio_vdev_io_bypass(zio_t
*zio
)
2855 ASSERT(zio
->io_stage
== ZIO_STAGE_VDEV_IO_START
);
2856 ASSERT(zio
->io_error
== 0);
2858 zio
->io_flags
|= ZIO_FLAG_IO_BYPASS
;
2859 zio
->io_stage
= ZIO_STAGE_VDEV_IO_ASSESS
>> 1;
2863 * ==========================================================================
2864 * Generate and verify checksums
2865 * ==========================================================================
2868 zio_checksum_generate(zio_t
*zio
)
2870 blkptr_t
*bp
= zio
->io_bp
;
2871 enum zio_checksum checksum
;
2875 * This is zio_write_phys().
2876 * We're either generating a label checksum, or none at all.
2878 checksum
= zio
->io_prop
.zp_checksum
;
2880 if (checksum
== ZIO_CHECKSUM_OFF
)
2881 return (ZIO_PIPELINE_CONTINUE
);
2883 ASSERT(checksum
== ZIO_CHECKSUM_LABEL
);
2885 if (BP_IS_GANG(bp
) && zio
->io_child_type
== ZIO_CHILD_GANG
) {
2886 ASSERT(!IO_IS_ALLOCATING(zio
));
2887 checksum
= ZIO_CHECKSUM_GANG_HEADER
;
2889 checksum
= BP_GET_CHECKSUM(bp
);
2893 zio_checksum_compute(zio
, checksum
, zio
->io_data
, zio
->io_size
);
2895 return (ZIO_PIPELINE_CONTINUE
);
2899 zio_checksum_verify(zio_t
*zio
)
2901 zio_bad_cksum_t info
;
2902 blkptr_t
*bp
= zio
->io_bp
;
2905 ASSERT(zio
->io_vd
!= NULL
);
2909 * This is zio_read_phys().
2910 * We're either verifying a label checksum, or nothing at all.
2912 if (zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_OFF
)
2913 return (ZIO_PIPELINE_CONTINUE
);
2915 ASSERT(zio
->io_prop
.zp_checksum
== ZIO_CHECKSUM_LABEL
);
2918 if ((error
= zio_checksum_error(zio
, &info
)) != 0) {
2919 zio
->io_error
= error
;
2920 if (!(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
)) {
2921 zfs_ereport_start_checksum(zio
->io_spa
,
2922 zio
->io_vd
, zio
, zio
->io_offset
,
2923 zio
->io_size
, NULL
, &info
);
2927 return (ZIO_PIPELINE_CONTINUE
);
2931 * Called by RAID-Z to ensure we don't compute the checksum twice.
2934 zio_checksum_verified(zio_t
*zio
)
2936 zio
->io_pipeline
&= ~ZIO_STAGE_CHECKSUM_VERIFY
;
2940 * ==========================================================================
2941 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
2942 * An error of 0 indicates success. ENXIO indicates whole-device failure,
2943 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
2944 * indicate errors that are specific to one I/O, and most likely permanent.
2945 * Any other error is presumed to be worse because we weren't expecting it.
2946 * ==========================================================================
2949 zio_worst_error(int e1
, int e2
)
2951 static int zio_error_rank
[] = { 0, ENXIO
, ECKSUM
, EIO
};
2954 for (r1
= 0; r1
< sizeof (zio_error_rank
) / sizeof (int); r1
++)
2955 if (e1
== zio_error_rank
[r1
])
2958 for (r2
= 0; r2
< sizeof (zio_error_rank
) / sizeof (int); r2
++)
2959 if (e2
== zio_error_rank
[r2
])
2962 return (r1
> r2
? e1
: e2
);
2966 * ==========================================================================
2968 * ==========================================================================
2971 zio_ready(zio_t
*zio
)
2973 blkptr_t
*bp
= zio
->io_bp
;
2974 zio_t
*pio
, *pio_next
;
2976 if (zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_READY
) ||
2977 zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_READY
))
2978 return (ZIO_PIPELINE_STOP
);
2980 if (zio
->io_ready
) {
2981 ASSERT(IO_IS_ALLOCATING(zio
));
2982 ASSERT(bp
->blk_birth
== zio
->io_txg
|| BP_IS_HOLE(bp
) ||
2983 (zio
->io_flags
& ZIO_FLAG_NOPWRITE
));
2984 ASSERT(zio
->io_children
[ZIO_CHILD_GANG
][ZIO_WAIT_READY
] == 0);
2989 if (bp
!= NULL
&& bp
!= &zio
->io_bp_copy
)
2990 zio
->io_bp_copy
= *bp
;
2993 zio
->io_pipeline
= ZIO_INTERLOCK_PIPELINE
;
2995 mutex_enter(&zio
->io_lock
);
2996 zio
->io_state
[ZIO_WAIT_READY
] = 1;
2997 pio
= zio_walk_parents(zio
);
2998 mutex_exit(&zio
->io_lock
);
3001 * As we notify zio's parents, new parents could be added.
3002 * New parents go to the head of zio's io_parent_list, however,
3003 * so we will (correctly) not notify them. The remainder of zio's
3004 * io_parent_list, from 'pio_next' onward, cannot change because
3005 * all parents must wait for us to be done before they can be done.
3007 for (; pio
!= NULL
; pio
= pio_next
) {
3008 pio_next
= zio_walk_parents(zio
);
3009 zio_notify_parent(pio
, zio
, ZIO_WAIT_READY
);
3012 if (zio
->io_flags
& ZIO_FLAG_NODATA
) {
3013 if (BP_IS_GANG(bp
)) {
3014 zio
->io_flags
&= ~ZIO_FLAG_NODATA
;
3016 ASSERT((uintptr_t)zio
->io_data
< SPA_MAXBLOCKSIZE
);
3017 zio
->io_pipeline
&= ~ZIO_VDEV_IO_STAGES
;
3021 if (zio_injection_enabled
&&
3022 zio
->io_spa
->spa_syncing_txg
== zio
->io_txg
)
3023 zio_handle_ignored_writes(zio
);
3025 return (ZIO_PIPELINE_CONTINUE
);
3029 zio_done(zio_t
*zio
)
3031 zio_t
*pio
, *pio_next
;
3035 * If our children haven't all completed,
3036 * wait for them and then repeat this pipeline stage.
3038 if (zio_wait_for_children(zio
, ZIO_CHILD_VDEV
, ZIO_WAIT_DONE
) ||
3039 zio_wait_for_children(zio
, ZIO_CHILD_GANG
, ZIO_WAIT_DONE
) ||
3040 zio_wait_for_children(zio
, ZIO_CHILD_DDT
, ZIO_WAIT_DONE
) ||
3041 zio_wait_for_children(zio
, ZIO_CHILD_LOGICAL
, ZIO_WAIT_DONE
))
3042 return (ZIO_PIPELINE_STOP
);
3044 for (c
= 0; c
< ZIO_CHILD_TYPES
; c
++)
3045 for (w
= 0; w
< ZIO_WAIT_TYPES
; w
++)
3046 ASSERT(zio
->io_children
[c
][w
] == 0);
3048 if (zio
->io_bp
!= NULL
&& !BP_IS_EMBEDDED(zio
->io_bp
)) {
3049 ASSERT(zio
->io_bp
->blk_pad
[0] == 0);
3050 ASSERT(zio
->io_bp
->blk_pad
[1] == 0);
3051 ASSERT(bcmp(zio
->io_bp
, &zio
->io_bp_copy
,
3052 sizeof (blkptr_t
)) == 0 ||
3053 (zio
->io_bp
== zio_unique_parent(zio
)->io_bp
));
3054 if (zio
->io_type
== ZIO_TYPE_WRITE
&& !BP_IS_HOLE(zio
->io_bp
) &&
3055 zio
->io_bp_override
== NULL
&&
3056 !(zio
->io_flags
& ZIO_FLAG_IO_REPAIR
)) {
3057 ASSERT(!BP_SHOULD_BYTESWAP(zio
->io_bp
));
3058 ASSERT3U(zio
->io_prop
.zp_copies
, <=,
3059 BP_GET_NDVAS(zio
->io_bp
));
3060 ASSERT(BP_COUNT_GANG(zio
->io_bp
) == 0 ||
3061 (BP_COUNT_GANG(zio
->io_bp
) ==
3062 BP_GET_NDVAS(zio
->io_bp
)));
3064 if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
)
3065 VERIFY(BP_EQUAL(zio
->io_bp
, &zio
->io_bp_orig
));
3069 * If there were child vdev/gang/ddt errors, they apply to us now.
3071 zio_inherit_child_errors(zio
, ZIO_CHILD_VDEV
);
3072 zio_inherit_child_errors(zio
, ZIO_CHILD_GANG
);
3073 zio_inherit_child_errors(zio
, ZIO_CHILD_DDT
);
3076 * If the I/O on the transformed data was successful, generate any
3077 * checksum reports now while we still have the transformed data.
3079 if (zio
->io_error
== 0) {
3080 while (zio
->io_cksum_report
!= NULL
) {
3081 zio_cksum_report_t
*zcr
= zio
->io_cksum_report
;
3082 uint64_t align
= zcr
->zcr_align
;
3083 uint64_t asize
= P2ROUNDUP(zio
->io_size
, align
);
3084 char *abuf
= zio
->io_data
;
3086 if (asize
!= zio
->io_size
) {
3087 abuf
= zio_buf_alloc(asize
);
3088 bcopy(zio
->io_data
, abuf
, zio
->io_size
);
3089 bzero(abuf
+zio
->io_size
, asize
-zio
->io_size
);
3092 zio
->io_cksum_report
= zcr
->zcr_next
;
3093 zcr
->zcr_next
= NULL
;
3094 zcr
->zcr_finish(zcr
, abuf
);
3095 zfs_ereport_free_checksum(zcr
);
3097 if (asize
!= zio
->io_size
)
3098 zio_buf_free(abuf
, asize
);
3102 zio_pop_transforms(zio
); /* note: may set zio->io_error */
3104 vdev_stat_update(zio
, zio
->io_size
);
3107 * If this I/O is attached to a particular vdev is slow, exceeding
3108 * 30 seconds to complete, post an error described the I/O delay.
3109 * We ignore these errors if the device is currently unavailable.
3111 if (zio
->io_delay
>= MSEC_TO_TICK(zio_delay_max
)) {
3112 if (zio
->io_vd
!= NULL
&& !vdev_is_dead(zio
->io_vd
))
3113 zfs_ereport_post(FM_EREPORT_ZFS_DELAY
, zio
->io_spa
,
3114 zio
->io_vd
, zio
, 0, 0);
3117 if (zio
->io_error
) {
3119 * If this I/O is attached to a particular vdev,
3120 * generate an error message describing the I/O failure
3121 * at the block level. We ignore these errors if the
3122 * device is currently unavailable.
3124 if (zio
->io_error
!= ECKSUM
&& zio
->io_vd
!= NULL
&&
3125 !vdev_is_dead(zio
->io_vd
))
3126 zfs_ereport_post(FM_EREPORT_ZFS_IO
, zio
->io_spa
,
3127 zio
->io_vd
, zio
, 0, 0);
3129 if ((zio
->io_error
== EIO
|| !(zio
->io_flags
&
3130 (ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_DONT_PROPAGATE
))) &&
3131 zio
== zio
->io_logical
) {
3133 * For logical I/O requests, tell the SPA to log the
3134 * error and generate a logical data ereport.
3136 spa_log_error(zio
->io_spa
, zio
);
3137 zfs_ereport_post(FM_EREPORT_ZFS_DATA
, zio
->io_spa
,
3142 if (zio
->io_error
&& zio
== zio
->io_logical
) {
3144 * Determine whether zio should be reexecuted. This will
3145 * propagate all the way to the root via zio_notify_parent().
3147 ASSERT(zio
->io_vd
== NULL
&& zio
->io_bp
!= NULL
);
3148 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
3150 if (IO_IS_ALLOCATING(zio
) &&
3151 !(zio
->io_flags
& ZIO_FLAG_CANFAIL
)) {
3152 if (zio
->io_error
!= ENOSPC
)
3153 zio
->io_reexecute
|= ZIO_REEXECUTE_NOW
;
3155 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3158 if ((zio
->io_type
== ZIO_TYPE_READ
||
3159 zio
->io_type
== ZIO_TYPE_FREE
) &&
3160 !(zio
->io_flags
& ZIO_FLAG_SCAN_THREAD
) &&
3161 zio
->io_error
== ENXIO
&&
3162 spa_load_state(zio
->io_spa
) == SPA_LOAD_NONE
&&
3163 spa_get_failmode(zio
->io_spa
) != ZIO_FAILURE_MODE_CONTINUE
)
3164 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3166 if (!(zio
->io_flags
& ZIO_FLAG_CANFAIL
) && !zio
->io_reexecute
)
3167 zio
->io_reexecute
|= ZIO_REEXECUTE_SUSPEND
;
3170 * Here is a possibly good place to attempt to do
3171 * either combinatorial reconstruction or error correction
3172 * based on checksums. It also might be a good place
3173 * to send out preliminary ereports before we suspend
3179 * If there were logical child errors, they apply to us now.
3180 * We defer this until now to avoid conflating logical child
3181 * errors with errors that happened to the zio itself when
3182 * updating vdev stats and reporting FMA events above.
3184 zio_inherit_child_errors(zio
, ZIO_CHILD_LOGICAL
);
3186 if ((zio
->io_error
|| zio
->io_reexecute
) &&
3187 IO_IS_ALLOCATING(zio
) && zio
->io_gang_leader
== zio
&&
3188 !(zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)))
3189 zio_dva_unallocate(zio
, zio
->io_gang_tree
, zio
->io_bp
);
3191 zio_gang_tree_free(&zio
->io_gang_tree
);
3194 * Godfather I/Os should never suspend.
3196 if ((zio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
3197 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
))
3198 zio
->io_reexecute
= 0;
3200 if (zio
->io_reexecute
) {
3202 * This is a logical I/O that wants to reexecute.
3204 * Reexecute is top-down. When an i/o fails, if it's not
3205 * the root, it simply notifies its parent and sticks around.
3206 * The parent, seeing that it still has children in zio_done(),
3207 * does the same. This percolates all the way up to the root.
3208 * The root i/o will reexecute or suspend the entire tree.
3210 * This approach ensures that zio_reexecute() honors
3211 * all the original i/o dependency relationships, e.g.
3212 * parents not executing until children are ready.
3214 ASSERT(zio
->io_child_type
== ZIO_CHILD_LOGICAL
);
3216 zio
->io_gang_leader
= NULL
;
3218 mutex_enter(&zio
->io_lock
);
3219 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
3220 mutex_exit(&zio
->io_lock
);
3223 * "The Godfather" I/O monitors its children but is
3224 * not a true parent to them. It will track them through
3225 * the pipeline but severs its ties whenever they get into
3226 * trouble (e.g. suspended). This allows "The Godfather"
3227 * I/O to return status without blocking.
3229 for (pio
= zio_walk_parents(zio
); pio
!= NULL
; pio
= pio_next
) {
3230 zio_link_t
*zl
= zio
->io_walk_link
;
3231 pio_next
= zio_walk_parents(zio
);
3233 if ((pio
->io_flags
& ZIO_FLAG_GODFATHER
) &&
3234 (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
)) {
3235 zio_remove_child(pio
, zio
, zl
);
3236 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3240 if ((pio
= zio_unique_parent(zio
)) != NULL
) {
3242 * We're not a root i/o, so there's nothing to do
3243 * but notify our parent. Don't propagate errors
3244 * upward since we haven't permanently failed yet.
3246 ASSERT(!(zio
->io_flags
& ZIO_FLAG_GODFATHER
));
3247 zio
->io_flags
|= ZIO_FLAG_DONT_PROPAGATE
;
3248 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3249 } else if (zio
->io_reexecute
& ZIO_REEXECUTE_SUSPEND
) {
3251 * We'd fail again if we reexecuted now, so suspend
3252 * until conditions improve (e.g. device comes online).
3254 zio_suspend(zio
->io_spa
, zio
);
3257 * Reexecution is potentially a huge amount of work.
3258 * Hand it off to the otherwise-unused claim taskq.
3260 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
3261 spa_taskq_dispatch_ent(zio
->io_spa
,
3262 ZIO_TYPE_CLAIM
, ZIO_TASKQ_ISSUE
,
3263 (task_func_t
*)zio_reexecute
, zio
, 0,
3266 return (ZIO_PIPELINE_STOP
);
3269 ASSERT(zio
->io_child_count
== 0);
3270 ASSERT(zio
->io_reexecute
== 0);
3271 ASSERT(zio
->io_error
== 0 || (zio
->io_flags
& ZIO_FLAG_CANFAIL
));
3274 * Report any checksum errors, since the I/O is complete.
3276 while (zio
->io_cksum_report
!= NULL
) {
3277 zio_cksum_report_t
*zcr
= zio
->io_cksum_report
;
3278 zio
->io_cksum_report
= zcr
->zcr_next
;
3279 zcr
->zcr_next
= NULL
;
3280 zcr
->zcr_finish(zcr
, NULL
);
3281 zfs_ereport_free_checksum(zcr
);
3284 if (zio
->io_flags
& ZIO_FLAG_FASTWRITE
&& zio
->io_bp
&&
3285 !BP_IS_HOLE(zio
->io_bp
) && !BP_IS_EMBEDDED(zio
->io_bp
) &&
3286 !(zio
->io_flags
& ZIO_FLAG_NOPWRITE
)) {
3287 metaslab_fastwrite_unmark(zio
->io_spa
, zio
->io_bp
);
3291 * It is the responsibility of the done callback to ensure that this
3292 * particular zio is no longer discoverable for adoption, and as
3293 * such, cannot acquire any new parents.
3298 mutex_enter(&zio
->io_lock
);
3299 zio
->io_state
[ZIO_WAIT_DONE
] = 1;
3300 mutex_exit(&zio
->io_lock
);
3302 for (pio
= zio_walk_parents(zio
); pio
!= NULL
; pio
= pio_next
) {
3303 zio_link_t
*zl
= zio
->io_walk_link
;
3304 pio_next
= zio_walk_parents(zio
);
3305 zio_remove_child(pio
, zio
, zl
);
3306 zio_notify_parent(pio
, zio
, ZIO_WAIT_DONE
);
3309 if (zio
->io_waiter
!= NULL
) {
3310 mutex_enter(&zio
->io_lock
);
3311 zio
->io_executor
= NULL
;
3312 cv_broadcast(&zio
->io_cv
);
3313 mutex_exit(&zio
->io_lock
);
3318 return (ZIO_PIPELINE_STOP
);
3322 * ==========================================================================
3323 * I/O pipeline definition
3324 * ==========================================================================
3326 static zio_pipe_stage_t
*zio_pipeline
[] = {
3332 zio_checksum_generate
,
3347 zio_checksum_verify
,
3351 /* dnp is the dnode for zb1->zb_object */
3353 zbookmark_is_before(const dnode_phys_t
*dnp
, const zbookmark_phys_t
*zb1
,
3354 const zbookmark_phys_t
*zb2
)
3356 uint64_t zb1nextL0
, zb2thisobj
;
3358 ASSERT(zb1
->zb_objset
== zb2
->zb_objset
);
3359 ASSERT(zb2
->zb_level
== 0);
3361 /* The objset_phys_t isn't before anything. */
3365 zb1nextL0
= (zb1
->zb_blkid
+ 1) <<
3366 ((zb1
->zb_level
) * (dnp
->dn_indblkshift
- SPA_BLKPTRSHIFT
));
3368 zb2thisobj
= zb2
->zb_object
? zb2
->zb_object
:
3369 zb2
->zb_blkid
<< (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
);
3371 if (zb1
->zb_object
== DMU_META_DNODE_OBJECT
) {
3372 uint64_t nextobj
= zb1nextL0
*
3373 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
) >> DNODE_SHIFT
;
3374 return (nextobj
<= zb2thisobj
);
3377 if (zb1
->zb_object
< zb2thisobj
)
3379 if (zb1
->zb_object
> zb2thisobj
)
3381 if (zb2
->zb_object
== DMU_META_DNODE_OBJECT
)
3383 return (zb1nextL0
<= zb2
->zb_blkid
);
3386 #if defined(_KERNEL) && defined(HAVE_SPL)
3387 /* Fault injection */
3388 EXPORT_SYMBOL(zio_injection_enabled
);
3389 EXPORT_SYMBOL(zio_inject_fault
);
3390 EXPORT_SYMBOL(zio_inject_list_next
);
3391 EXPORT_SYMBOL(zio_clear_fault
);
3392 EXPORT_SYMBOL(zio_handle_fault_injection
);
3393 EXPORT_SYMBOL(zio_handle_device_injection
);
3394 EXPORT_SYMBOL(zio_handle_label_injection
);
3395 EXPORT_SYMBOL(zio_type_name
);
3396 EXPORT_SYMBOL(zio_buf_alloc
);
3397 EXPORT_SYMBOL(zio_data_buf_alloc
);
3398 EXPORT_SYMBOL(zio_buf_free
);
3399 EXPORT_SYMBOL(zio_data_buf_free
);
3401 module_param(zio_bulk_flags
, int, 0644);
3402 MODULE_PARM_DESC(zio_bulk_flags
, "Additional flags to pass to bulk buffers");
3404 module_param(zio_delay_max
, int, 0644);
3405 MODULE_PARM_DESC(zio_delay_max
, "Max zio millisec delay before posting event");
3407 module_param(zio_requeue_io_start_cut_in_line
, int, 0644);
3408 MODULE_PARM_DESC(zio_requeue_io_start_cut_in_line
, "Prioritize requeued I/O");
3410 module_param(zfs_sync_pass_deferred_free
, int, 0644);
3411 MODULE_PARM_DESC(zfs_sync_pass_deferred_free
,
3412 "Defer frees starting in this pass");
3414 module_param(zfs_sync_pass_dont_compress
, int, 0644);
3415 MODULE_PARM_DESC(zfs_sync_pass_dont_compress
,
3416 "Don't compress starting in this pass");
3418 module_param(zfs_sync_pass_rewrite
, int, 0644);
3419 MODULE_PARM_DESC(zfs_sync_pass_rewrite
,
3420 "Rewrite new bps starting in this pass");