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