1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy /* 22eda14cbcSMatt Macy * Copyright (c) 2014 by Chunwei Chen. All rights reserved. 23eda14cbcSMatt Macy * Copyright (c) 2019 by Delphix. All rights reserved. 24eda14cbcSMatt Macy */ 25eda14cbcSMatt Macy 26eda14cbcSMatt Macy /* 27eda14cbcSMatt Macy * ARC buffer data (ABD). 28eda14cbcSMatt Macy * 29eda14cbcSMatt Macy * ABDs are an abstract data structure for the ARC which can use two 30eda14cbcSMatt Macy * different ways of storing the underlying data: 31eda14cbcSMatt Macy * 32eda14cbcSMatt Macy * (a) Linear buffer. In this case, all the data in the ABD is stored in one 33eda14cbcSMatt Macy * contiguous buffer in memory (from a zio_[data_]buf_* kmem cache). 34eda14cbcSMatt Macy * 35eda14cbcSMatt Macy * +-------------------+ 36eda14cbcSMatt Macy * | ABD (linear) | 37eda14cbcSMatt Macy * | abd_flags = ... | 38eda14cbcSMatt Macy * | abd_size = ... | +--------------------------------+ 39eda14cbcSMatt Macy * | abd_buf ------------->| raw buffer of size abd_size | 40eda14cbcSMatt Macy * +-------------------+ +--------------------------------+ 41eda14cbcSMatt Macy * no abd_chunks 42eda14cbcSMatt Macy * 43eda14cbcSMatt Macy * (b) Scattered buffer. In this case, the data in the ABD is split into 44eda14cbcSMatt Macy * equal-sized chunks (from the abd_chunk_cache kmem_cache), with pointers 45eda14cbcSMatt Macy * to the chunks recorded in an array at the end of the ABD structure. 46eda14cbcSMatt Macy * 47eda14cbcSMatt Macy * +-------------------+ 48eda14cbcSMatt Macy * | ABD (scattered) | 49eda14cbcSMatt Macy * | abd_flags = ... | 50eda14cbcSMatt Macy * | abd_size = ... | 51eda14cbcSMatt Macy * | abd_offset = 0 | +-----------+ 52eda14cbcSMatt Macy * | abd_chunks[0] ----------------------------->| chunk 0 | 53eda14cbcSMatt Macy * | abd_chunks[1] ---------------------+ +-----------+ 54eda14cbcSMatt Macy * | ... | | +-----------+ 55eda14cbcSMatt Macy * | abd_chunks[N-1] ---------+ +------->| chunk 1 | 56eda14cbcSMatt Macy * +-------------------+ | +-----------+ 57eda14cbcSMatt Macy * | ... 58eda14cbcSMatt Macy * | +-----------+ 59eda14cbcSMatt Macy * +----------------->| chunk N-1 | 60eda14cbcSMatt Macy * +-----------+ 61eda14cbcSMatt Macy * 62eda14cbcSMatt Macy * In addition to directly allocating a linear or scattered ABD, it is also 63eda14cbcSMatt Macy * possible to create an ABD by requesting the "sub-ABD" starting at an offset 64eda14cbcSMatt Macy * within an existing ABD. In linear buffers this is simple (set abd_buf of 65eda14cbcSMatt Macy * the new ABD to the starting point within the original raw buffer), but 66eda14cbcSMatt Macy * scattered ABDs are a little more complex. The new ABD makes a copy of the 67eda14cbcSMatt Macy * relevant abd_chunks pointers (but not the underlying data). However, to 68eda14cbcSMatt Macy * provide arbitrary rather than only chunk-aligned starting offsets, it also 69eda14cbcSMatt Macy * tracks an abd_offset field which represents the starting point of the data 70eda14cbcSMatt Macy * within the first chunk in abd_chunks. For both linear and scattered ABDs, 71eda14cbcSMatt Macy * creating an offset ABD marks the original ABD as the offset's parent, and the 72eda14cbcSMatt Macy * original ABD's abd_children refcount is incremented. This data allows us to 73eda14cbcSMatt Macy * ensure the root ABD isn't deleted before its children. 74eda14cbcSMatt Macy * 75eda14cbcSMatt Macy * Most consumers should never need to know what type of ABD they're using -- 76eda14cbcSMatt Macy * the ABD public API ensures that it's possible to transparently switch from 77eda14cbcSMatt Macy * using a linear ABD to a scattered one when doing so would be beneficial. 78eda14cbcSMatt Macy * 79eda14cbcSMatt Macy * If you need to use the data within an ABD directly, if you know it's linear 80eda14cbcSMatt Macy * (because you allocated it) you can use abd_to_buf() to access the underlying 81eda14cbcSMatt Macy * raw buffer. Otherwise, you should use one of the abd_borrow_buf* functions 82eda14cbcSMatt Macy * which will allocate a raw buffer if necessary. Use the abd_return_buf* 83eda14cbcSMatt Macy * functions to return any raw buffers that are no longer necessary when you're 84eda14cbcSMatt Macy * done using them. 85eda14cbcSMatt Macy * 86eda14cbcSMatt Macy * There are a variety of ABD APIs that implement basic buffer operations: 87eda14cbcSMatt Macy * compare, copy, read, write, and fill with zeroes. If you need a custom 88eda14cbcSMatt Macy * function which progressively accesses the whole ABD, use the abd_iterate_* 89eda14cbcSMatt Macy * functions. 90eda14cbcSMatt Macy * 91eda14cbcSMatt Macy * As an additional feature, linear and scatter ABD's can be stitched together 92eda14cbcSMatt Macy * by using the gang ABD type (abd_alloc_gang_abd()). This allows for 93eda14cbcSMatt Macy * multiple ABDs to be viewed as a singular ABD. 94eda14cbcSMatt Macy * 95eda14cbcSMatt Macy * It is possible to make all ABDs linear by setting zfs_abd_scatter_enabled to 96eda14cbcSMatt Macy * B_FALSE. 97eda14cbcSMatt Macy */ 98eda14cbcSMatt Macy 99eda14cbcSMatt Macy #include <sys/abd_impl.h> 100eda14cbcSMatt Macy #include <sys/param.h> 101eda14cbcSMatt Macy #include <sys/zio.h> 102eda14cbcSMatt Macy #include <sys/zfs_context.h> 103eda14cbcSMatt Macy #include <sys/zfs_znode.h> 104eda14cbcSMatt Macy 105eda14cbcSMatt Macy /* see block comment above for description */ 106eda14cbcSMatt Macy int zfs_abd_scatter_enabled = B_TRUE; 107eda14cbcSMatt Macy 108eda14cbcSMatt Macy void 109eda14cbcSMatt Macy abd_verify(abd_t *abd) 110eda14cbcSMatt Macy { 11133b8c039SMartin Matuska #ifdef ZFS_DEBUG 112eda14cbcSMatt Macy ASSERT3U(abd->abd_size, <=, SPA_MAXBLOCKSIZE); 113eda14cbcSMatt Macy ASSERT3U(abd->abd_flags, ==, abd->abd_flags & (ABD_FLAG_LINEAR | 114eda14cbcSMatt Macy ABD_FLAG_OWNER | ABD_FLAG_META | ABD_FLAG_MULTI_ZONE | 115eda14cbcSMatt Macy ABD_FLAG_MULTI_CHUNK | ABD_FLAG_LINEAR_PAGE | ABD_FLAG_GANG | 116184c1b94SMartin Matuska ABD_FLAG_GANG_FREE | ABD_FLAG_ZEROS | ABD_FLAG_ALLOCD)); 117eda14cbcSMatt Macy IMPLY(abd->abd_parent != NULL, !(abd->abd_flags & ABD_FLAG_OWNER)); 118eda14cbcSMatt Macy IMPLY(abd->abd_flags & ABD_FLAG_META, abd->abd_flags & ABD_FLAG_OWNER); 119eda14cbcSMatt Macy if (abd_is_linear(abd)) { 120d09a955aSMateusz Guzik ASSERT3U(abd->abd_size, >, 0); 121eda14cbcSMatt Macy ASSERT3P(ABD_LINEAR_BUF(abd), !=, NULL); 122eda14cbcSMatt Macy } else if (abd_is_gang(abd)) { 123eda14cbcSMatt Macy uint_t child_sizes = 0; 124eda14cbcSMatt Macy for (abd_t *cabd = list_head(&ABD_GANG(abd).abd_gang_chain); 125eda14cbcSMatt Macy cabd != NULL; 126eda14cbcSMatt Macy cabd = list_next(&ABD_GANG(abd).abd_gang_chain, cabd)) { 127eda14cbcSMatt Macy ASSERT(list_link_active(&cabd->abd_gang_link)); 128eda14cbcSMatt Macy child_sizes += cabd->abd_size; 129eda14cbcSMatt Macy abd_verify(cabd); 130eda14cbcSMatt Macy } 131eda14cbcSMatt Macy ASSERT3U(abd->abd_size, ==, child_sizes); 132eda14cbcSMatt Macy } else { 133d09a955aSMateusz Guzik ASSERT3U(abd->abd_size, >, 0); 134eda14cbcSMatt Macy abd_verify_scatter(abd); 135eda14cbcSMatt Macy } 13633b8c039SMartin Matuska #endif 137eda14cbcSMatt Macy } 138eda14cbcSMatt Macy 139184c1b94SMartin Matuska static void 140184c1b94SMartin Matuska abd_init_struct(abd_t *abd) 141eda14cbcSMatt Macy { 142184c1b94SMartin Matuska list_link_init(&abd->abd_gang_link); 143184c1b94SMartin Matuska mutex_init(&abd->abd_mtx, NULL, MUTEX_DEFAULT, NULL); 144184c1b94SMartin Matuska abd->abd_flags = 0; 145184c1b94SMartin Matuska #ifdef ZFS_DEBUG 146184c1b94SMartin Matuska zfs_refcount_create(&abd->abd_children); 147184c1b94SMartin Matuska abd->abd_parent = NULL; 148184c1b94SMartin Matuska #endif 149184c1b94SMartin Matuska abd->abd_size = 0; 150184c1b94SMartin Matuska } 151184c1b94SMartin Matuska 152184c1b94SMartin Matuska static void 153184c1b94SMartin Matuska abd_fini_struct(abd_t *abd) 154184c1b94SMartin Matuska { 155184c1b94SMartin Matuska mutex_destroy(&abd->abd_mtx); 156184c1b94SMartin Matuska ASSERT(!list_link_active(&abd->abd_gang_link)); 157184c1b94SMartin Matuska #ifdef ZFS_DEBUG 158184c1b94SMartin Matuska zfs_refcount_destroy(&abd->abd_children); 159184c1b94SMartin Matuska #endif 160184c1b94SMartin Matuska } 161184c1b94SMartin Matuska 162184c1b94SMartin Matuska abd_t * 163184c1b94SMartin Matuska abd_alloc_struct(size_t size) 164184c1b94SMartin Matuska { 165184c1b94SMartin Matuska abd_t *abd = abd_alloc_struct_impl(size); 166184c1b94SMartin Matuska abd_init_struct(abd); 167184c1b94SMartin Matuska abd->abd_flags |= ABD_FLAG_ALLOCD; 168184c1b94SMartin Matuska return (abd); 169184c1b94SMartin Matuska } 170184c1b94SMartin Matuska 171184c1b94SMartin Matuska void 172184c1b94SMartin Matuska abd_free_struct(abd_t *abd) 173184c1b94SMartin Matuska { 174184c1b94SMartin Matuska abd_fini_struct(abd); 175184c1b94SMartin Matuska abd_free_struct_impl(abd); 176eda14cbcSMatt Macy } 177eda14cbcSMatt Macy 178eda14cbcSMatt Macy /* 179eda14cbcSMatt Macy * Allocate an ABD, along with its own underlying data buffers. Use this if you 180eda14cbcSMatt Macy * don't care whether the ABD is linear or not. 181eda14cbcSMatt Macy */ 182eda14cbcSMatt Macy abd_t * 183eda14cbcSMatt Macy abd_alloc(size_t size, boolean_t is_metadata) 184eda14cbcSMatt Macy { 1851f88aa09SMartin Matuska if (abd_size_alloc_linear(size)) 186eda14cbcSMatt Macy return (abd_alloc_linear(size, is_metadata)); 187eda14cbcSMatt Macy 188eda14cbcSMatt Macy VERIFY3U(size, <=, SPA_MAXBLOCKSIZE); 189eda14cbcSMatt Macy 190eda14cbcSMatt Macy abd_t *abd = abd_alloc_struct(size); 191184c1b94SMartin Matuska abd->abd_flags |= ABD_FLAG_OWNER; 192eda14cbcSMatt Macy abd->abd_u.abd_scatter.abd_offset = 0; 193eda14cbcSMatt Macy abd_alloc_chunks(abd, size); 194eda14cbcSMatt Macy 195eda14cbcSMatt Macy if (is_metadata) { 196eda14cbcSMatt Macy abd->abd_flags |= ABD_FLAG_META; 197eda14cbcSMatt Macy } 198eda14cbcSMatt Macy abd->abd_size = size; 199eda14cbcSMatt Macy 200eda14cbcSMatt Macy abd_update_scatter_stats(abd, ABDSTAT_INCR); 201eda14cbcSMatt Macy 202eda14cbcSMatt Macy return (abd); 203eda14cbcSMatt Macy } 204eda14cbcSMatt Macy 205eda14cbcSMatt Macy /* 206eda14cbcSMatt Macy * Allocate an ABD that must be linear, along with its own underlying data 207eda14cbcSMatt Macy * buffer. Only use this when it would be very annoying to write your ABD 208eda14cbcSMatt Macy * consumer with a scattered ABD. 209eda14cbcSMatt Macy */ 210eda14cbcSMatt Macy abd_t * 211eda14cbcSMatt Macy abd_alloc_linear(size_t size, boolean_t is_metadata) 212eda14cbcSMatt Macy { 213eda14cbcSMatt Macy abd_t *abd = abd_alloc_struct(0); 214eda14cbcSMatt Macy 215eda14cbcSMatt Macy VERIFY3U(size, <=, SPA_MAXBLOCKSIZE); 216eda14cbcSMatt Macy 217184c1b94SMartin Matuska abd->abd_flags |= ABD_FLAG_LINEAR | ABD_FLAG_OWNER; 218eda14cbcSMatt Macy if (is_metadata) { 219eda14cbcSMatt Macy abd->abd_flags |= ABD_FLAG_META; 220eda14cbcSMatt Macy } 221eda14cbcSMatt Macy abd->abd_size = size; 222eda14cbcSMatt Macy 223eda14cbcSMatt Macy if (is_metadata) { 224eda14cbcSMatt Macy ABD_LINEAR_BUF(abd) = zio_buf_alloc(size); 225eda14cbcSMatt Macy } else { 226eda14cbcSMatt Macy ABD_LINEAR_BUF(abd) = zio_data_buf_alloc(size); 227eda14cbcSMatt Macy } 228eda14cbcSMatt Macy 229eda14cbcSMatt Macy abd_update_linear_stats(abd, ABDSTAT_INCR); 230eda14cbcSMatt Macy 231eda14cbcSMatt Macy return (abd); 232eda14cbcSMatt Macy } 233eda14cbcSMatt Macy 234eda14cbcSMatt Macy static void 235eda14cbcSMatt Macy abd_free_linear(abd_t *abd) 236eda14cbcSMatt Macy { 237eda14cbcSMatt Macy if (abd_is_linear_page(abd)) { 238eda14cbcSMatt Macy abd_free_linear_page(abd); 239eda14cbcSMatt Macy return; 240eda14cbcSMatt Macy } 241eda14cbcSMatt Macy if (abd->abd_flags & ABD_FLAG_META) { 242eda14cbcSMatt Macy zio_buf_free(ABD_LINEAR_BUF(abd), abd->abd_size); 243eda14cbcSMatt Macy } else { 244eda14cbcSMatt Macy zio_data_buf_free(ABD_LINEAR_BUF(abd), abd->abd_size); 245eda14cbcSMatt Macy } 246eda14cbcSMatt Macy 247eda14cbcSMatt Macy abd_update_linear_stats(abd, ABDSTAT_DECR); 248eda14cbcSMatt Macy } 249eda14cbcSMatt Macy 250eda14cbcSMatt Macy static void 251184c1b94SMartin Matuska abd_free_gang(abd_t *abd) 252eda14cbcSMatt Macy { 253eda14cbcSMatt Macy ASSERT(abd_is_gang(abd)); 254184c1b94SMartin Matuska abd_t *cabd; 255eda14cbcSMatt Macy 256184c1b94SMartin Matuska while ((cabd = list_head(&ABD_GANG(abd).abd_gang_chain)) != NULL) { 257eda14cbcSMatt Macy /* 258eda14cbcSMatt Macy * We must acquire the child ABDs mutex to ensure that if it 259eda14cbcSMatt Macy * is being added to another gang ABD we will set the link 260eda14cbcSMatt Macy * as inactive when removing it from this gang ABD and before 261eda14cbcSMatt Macy * adding it to the other gang ABD. 262eda14cbcSMatt Macy */ 263eda14cbcSMatt Macy mutex_enter(&cabd->abd_mtx); 264eda14cbcSMatt Macy ASSERT(list_link_active(&cabd->abd_gang_link)); 265eda14cbcSMatt Macy list_remove(&ABD_GANG(abd).abd_gang_chain, cabd); 266eda14cbcSMatt Macy mutex_exit(&cabd->abd_mtx); 267184c1b94SMartin Matuska if (cabd->abd_flags & ABD_FLAG_GANG_FREE) 268eda14cbcSMatt Macy abd_free(cabd); 269eda14cbcSMatt Macy } 270eda14cbcSMatt Macy list_destroy(&ABD_GANG(abd).abd_gang_chain); 271184c1b94SMartin Matuska } 272184c1b94SMartin Matuska 273184c1b94SMartin Matuska static void 274184c1b94SMartin Matuska abd_free_scatter(abd_t *abd) 275184c1b94SMartin Matuska { 276184c1b94SMartin Matuska abd_free_chunks(abd); 277184c1b94SMartin Matuska abd_update_scatter_stats(abd, ABDSTAT_DECR); 278eda14cbcSMatt Macy } 279eda14cbcSMatt Macy 280eda14cbcSMatt Macy /* 281184c1b94SMartin Matuska * Free an ABD. Use with any kind of abd: those created with abd_alloc_*() 282184c1b94SMartin Matuska * and abd_get_*(), including abd_get_offset_struct(). 283184c1b94SMartin Matuska * 284184c1b94SMartin Matuska * If the ABD was created with abd_alloc_*(), the underlying data 285184c1b94SMartin Matuska * (scatterlist or linear buffer) will also be freed. (Subject to ownership 286184c1b94SMartin Matuska * changes via abd_*_ownership_of_buf().) 287184c1b94SMartin Matuska * 288184c1b94SMartin Matuska * Unless the ABD was created with abd_get_offset_struct(), the abd_t will 289184c1b94SMartin Matuska * also be freed. 290eda14cbcSMatt Macy */ 291eda14cbcSMatt Macy void 292eda14cbcSMatt Macy abd_free(abd_t *abd) 293eda14cbcSMatt Macy { 294eda14cbcSMatt Macy if (abd == NULL) 295eda14cbcSMatt Macy return; 296eda14cbcSMatt Macy 297eda14cbcSMatt Macy abd_verify(abd); 298184c1b94SMartin Matuska #ifdef ZFS_DEBUG 299184c1b94SMartin Matuska IMPLY(abd->abd_flags & ABD_FLAG_OWNER, abd->abd_parent == NULL); 300184c1b94SMartin Matuska #endif 301184c1b94SMartin Matuska 302184c1b94SMartin Matuska if (abd_is_gang(abd)) { 303184c1b94SMartin Matuska abd_free_gang(abd); 304184c1b94SMartin Matuska } else if (abd_is_linear(abd)) { 305184c1b94SMartin Matuska if (abd->abd_flags & ABD_FLAG_OWNER) 306eda14cbcSMatt Macy abd_free_linear(abd); 307184c1b94SMartin Matuska } else { 308184c1b94SMartin Matuska if (abd->abd_flags & ABD_FLAG_OWNER) 309eda14cbcSMatt Macy abd_free_scatter(abd); 310eda14cbcSMatt Macy } 311eda14cbcSMatt Macy 312184c1b94SMartin Matuska #ifdef ZFS_DEBUG 313184c1b94SMartin Matuska if (abd->abd_parent != NULL) { 314184c1b94SMartin Matuska (void) zfs_refcount_remove_many(&abd->abd_parent->abd_children, 315184c1b94SMartin Matuska abd->abd_size, abd); 316184c1b94SMartin Matuska } 317184c1b94SMartin Matuska #endif 318184c1b94SMartin Matuska 319184c1b94SMartin Matuska abd_fini_struct(abd); 320184c1b94SMartin Matuska if (abd->abd_flags & ABD_FLAG_ALLOCD) 321184c1b94SMartin Matuska abd_free_struct_impl(abd); 322184c1b94SMartin Matuska } 323184c1b94SMartin Matuska 324eda14cbcSMatt Macy /* 325eda14cbcSMatt Macy * Allocate an ABD of the same format (same metadata flag, same scatterize 326eda14cbcSMatt Macy * setting) as another ABD. 327eda14cbcSMatt Macy */ 328eda14cbcSMatt Macy abd_t * 329eda14cbcSMatt Macy abd_alloc_sametype(abd_t *sabd, size_t size) 330eda14cbcSMatt Macy { 331eda14cbcSMatt Macy boolean_t is_metadata = (sabd->abd_flags & ABD_FLAG_META) != 0; 332eda14cbcSMatt Macy if (abd_is_linear(sabd) && 333eda14cbcSMatt Macy !abd_is_linear_page(sabd)) { 334eda14cbcSMatt Macy return (abd_alloc_linear(size, is_metadata)); 335eda14cbcSMatt Macy } else { 336eda14cbcSMatt Macy return (abd_alloc(size, is_metadata)); 337eda14cbcSMatt Macy } 338eda14cbcSMatt Macy } 339eda14cbcSMatt Macy 340eda14cbcSMatt Macy /* 341eda14cbcSMatt Macy * Create gang ABD that will be the head of a list of ABD's. This is used 342eda14cbcSMatt Macy * to "chain" scatter/gather lists together when constructing aggregated 343eda14cbcSMatt Macy * IO's. To free this abd, abd_free() must be called. 344eda14cbcSMatt Macy */ 345eda14cbcSMatt Macy abd_t * 346184c1b94SMartin Matuska abd_alloc_gang(void) 347eda14cbcSMatt Macy { 348184c1b94SMartin Matuska abd_t *abd = abd_alloc_struct(0); 349184c1b94SMartin Matuska abd->abd_flags |= ABD_FLAG_GANG | ABD_FLAG_OWNER; 350eda14cbcSMatt Macy list_create(&ABD_GANG(abd).abd_gang_chain, 351eda14cbcSMatt Macy sizeof (abd_t), offsetof(abd_t, abd_gang_link)); 352eda14cbcSMatt Macy return (abd); 353eda14cbcSMatt Macy } 354eda14cbcSMatt Macy 355eda14cbcSMatt Macy /* 356eda14cbcSMatt Macy * Add a child gang ABD to a parent gang ABDs chained list. 357eda14cbcSMatt Macy */ 358eda14cbcSMatt Macy static void 359eda14cbcSMatt Macy abd_gang_add_gang(abd_t *pabd, abd_t *cabd, boolean_t free_on_free) 360eda14cbcSMatt Macy { 361eda14cbcSMatt Macy ASSERT(abd_is_gang(pabd)); 362eda14cbcSMatt Macy ASSERT(abd_is_gang(cabd)); 363eda14cbcSMatt Macy 364eda14cbcSMatt Macy if (free_on_free) { 365eda14cbcSMatt Macy /* 366eda14cbcSMatt Macy * If the parent is responsible for freeing the child gang 367184c1b94SMartin Matuska * ABD we will just splice the child's children ABD list to 368184c1b94SMartin Matuska * the parent's list and immediately free the child gang ABD 369eda14cbcSMatt Macy * struct. The parent gang ABDs children from the child gang 370eda14cbcSMatt Macy * will retain all the free_on_free settings after being 371eda14cbcSMatt Macy * added to the parents list. 372eda14cbcSMatt Macy */ 373e639e0d2SMartin Matuska #ifdef ZFS_DEBUG 374e639e0d2SMartin Matuska /* 375e639e0d2SMartin Matuska * If cabd had abd_parent, we have to drop it here. We can't 376e639e0d2SMartin Matuska * transfer it to pabd, nor we can clear abd_size leaving it. 377e639e0d2SMartin Matuska */ 378e639e0d2SMartin Matuska if (cabd->abd_parent != NULL) { 379e639e0d2SMartin Matuska (void) zfs_refcount_remove_many( 380e639e0d2SMartin Matuska &cabd->abd_parent->abd_children, 381e639e0d2SMartin Matuska cabd->abd_size, cabd); 382e639e0d2SMartin Matuska cabd->abd_parent = NULL; 383e639e0d2SMartin Matuska } 384e639e0d2SMartin Matuska #endif 385eda14cbcSMatt Macy pabd->abd_size += cabd->abd_size; 386e639e0d2SMartin Matuska cabd->abd_size = 0; 387eda14cbcSMatt Macy list_move_tail(&ABD_GANG(pabd).abd_gang_chain, 388eda14cbcSMatt Macy &ABD_GANG(cabd).abd_gang_chain); 389eda14cbcSMatt Macy ASSERT(list_is_empty(&ABD_GANG(cabd).abd_gang_chain)); 390eda14cbcSMatt Macy abd_verify(pabd); 391184c1b94SMartin Matuska abd_free(cabd); 392eda14cbcSMatt Macy } else { 393eda14cbcSMatt Macy for (abd_t *child = list_head(&ABD_GANG(cabd).abd_gang_chain); 394eda14cbcSMatt Macy child != NULL; 395eda14cbcSMatt Macy child = list_next(&ABD_GANG(cabd).abd_gang_chain, child)) { 396eda14cbcSMatt Macy /* 397eda14cbcSMatt Macy * We always pass B_FALSE for free_on_free as it is the 39816038816SMartin Matuska * original child gang ABDs responsibility to determine 399eda14cbcSMatt Macy * if any of its child ABDs should be free'd on the call 400eda14cbcSMatt Macy * to abd_free(). 401eda14cbcSMatt Macy */ 402eda14cbcSMatt Macy abd_gang_add(pabd, child, B_FALSE); 403eda14cbcSMatt Macy } 404eda14cbcSMatt Macy abd_verify(pabd); 405eda14cbcSMatt Macy } 406eda14cbcSMatt Macy } 407eda14cbcSMatt Macy 408eda14cbcSMatt Macy /* 409eda14cbcSMatt Macy * Add a child ABD to a gang ABD's chained list. 410eda14cbcSMatt Macy */ 411eda14cbcSMatt Macy void 412eda14cbcSMatt Macy abd_gang_add(abd_t *pabd, abd_t *cabd, boolean_t free_on_free) 413eda14cbcSMatt Macy { 414eda14cbcSMatt Macy ASSERT(abd_is_gang(pabd)); 415eda14cbcSMatt Macy abd_t *child_abd = NULL; 416eda14cbcSMatt Macy 417eda14cbcSMatt Macy /* 418eda14cbcSMatt Macy * If the child being added is a gang ABD, we will add the 419184c1b94SMartin Matuska * child's ABDs to the parent gang ABD. This allows us to account 420eda14cbcSMatt Macy * for the offset correctly in the parent gang ABD. 421eda14cbcSMatt Macy */ 422eda14cbcSMatt Macy if (abd_is_gang(cabd)) { 423eda14cbcSMatt Macy ASSERT(!list_link_active(&cabd->abd_gang_link)); 424eda14cbcSMatt Macy return (abd_gang_add_gang(pabd, cabd, free_on_free)); 425eda14cbcSMatt Macy } 426eda14cbcSMatt Macy ASSERT(!abd_is_gang(cabd)); 427eda14cbcSMatt Macy 428eda14cbcSMatt Macy /* 429eda14cbcSMatt Macy * In order to verify that an ABD is not already part of 430eda14cbcSMatt Macy * another gang ABD, we must lock the child ABD's abd_mtx 431eda14cbcSMatt Macy * to check its abd_gang_link status. We unlock the abd_mtx 432eda14cbcSMatt Macy * only after it is has been added to a gang ABD, which 433eda14cbcSMatt Macy * will update the abd_gang_link's status. See comment below 434eda14cbcSMatt Macy * for how an ABD can be in multiple gang ABD's simultaneously. 435eda14cbcSMatt Macy */ 436eda14cbcSMatt Macy mutex_enter(&cabd->abd_mtx); 437eda14cbcSMatt Macy if (list_link_active(&cabd->abd_gang_link)) { 438eda14cbcSMatt Macy /* 439eda14cbcSMatt Macy * If the child ABD is already part of another 440eda14cbcSMatt Macy * gang ABD then we must allocate a new 441eda14cbcSMatt Macy * ABD to use a separate link. We mark the newly 442eda14cbcSMatt Macy * allocated ABD with ABD_FLAG_GANG_FREE, before 443eda14cbcSMatt Macy * adding it to the gang ABD's list, to make the 444eda14cbcSMatt Macy * gang ABD aware that it is responsible to call 445184c1b94SMartin Matuska * abd_free(). We use abd_get_offset() in order 446eda14cbcSMatt Macy * to just allocate a new ABD but avoid copying the 447eda14cbcSMatt Macy * data over into the newly allocated ABD. 448eda14cbcSMatt Macy * 449eda14cbcSMatt Macy * An ABD may become part of multiple gang ABD's. For 450eda14cbcSMatt Macy * example, when writing ditto bocks, the same ABD 451eda14cbcSMatt Macy * is used to write 2 or 3 locations with 2 or 3 452eda14cbcSMatt Macy * zio_t's. Each of the zio's may be aggregated with 453eda14cbcSMatt Macy * different adjacent zio's. zio aggregation uses gang 454eda14cbcSMatt Macy * zio's, so the single ABD can become part of multiple 455eda14cbcSMatt Macy * gang zio's. 456eda14cbcSMatt Macy * 457eda14cbcSMatt Macy * The ASSERT below is to make sure that if 458eda14cbcSMatt Macy * free_on_free is passed as B_TRUE, the ABD can 459eda14cbcSMatt Macy * not be in multiple gang ABD's. The gang ABD 460eda14cbcSMatt Macy * can not be responsible for cleaning up the child 461eda14cbcSMatt Macy * ABD memory allocation if the ABD can be in 462eda14cbcSMatt Macy * multiple gang ABD's at one time. 463eda14cbcSMatt Macy */ 464eda14cbcSMatt Macy ASSERT3B(free_on_free, ==, B_FALSE); 465eda14cbcSMatt Macy child_abd = abd_get_offset(cabd, 0); 466eda14cbcSMatt Macy child_abd->abd_flags |= ABD_FLAG_GANG_FREE; 467eda14cbcSMatt Macy } else { 468eda14cbcSMatt Macy child_abd = cabd; 469eda14cbcSMatt Macy if (free_on_free) 470eda14cbcSMatt Macy child_abd->abd_flags |= ABD_FLAG_GANG_FREE; 471eda14cbcSMatt Macy } 472eda14cbcSMatt Macy ASSERT3P(child_abd, !=, NULL); 473eda14cbcSMatt Macy 474eda14cbcSMatt Macy list_insert_tail(&ABD_GANG(pabd).abd_gang_chain, child_abd); 475eda14cbcSMatt Macy mutex_exit(&cabd->abd_mtx); 476eda14cbcSMatt Macy pabd->abd_size += child_abd->abd_size; 477eda14cbcSMatt Macy } 478eda14cbcSMatt Macy 479eda14cbcSMatt Macy /* 480eda14cbcSMatt Macy * Locate the ABD for the supplied offset in the gang ABD. 481eda14cbcSMatt Macy * Return a new offset relative to the returned ABD. 482eda14cbcSMatt Macy */ 483eda14cbcSMatt Macy abd_t * 484eda14cbcSMatt Macy abd_gang_get_offset(abd_t *abd, size_t *off) 485eda14cbcSMatt Macy { 486eda14cbcSMatt Macy abd_t *cabd; 487eda14cbcSMatt Macy 488eda14cbcSMatt Macy ASSERT(abd_is_gang(abd)); 489eda14cbcSMatt Macy ASSERT3U(*off, <, abd->abd_size); 490eda14cbcSMatt Macy for (cabd = list_head(&ABD_GANG(abd).abd_gang_chain); cabd != NULL; 491eda14cbcSMatt Macy cabd = list_next(&ABD_GANG(abd).abd_gang_chain, cabd)) { 492eda14cbcSMatt Macy if (*off >= cabd->abd_size) 493eda14cbcSMatt Macy *off -= cabd->abd_size; 494eda14cbcSMatt Macy else 495eda14cbcSMatt Macy return (cabd); 496eda14cbcSMatt Macy } 497eda14cbcSMatt Macy VERIFY3P(cabd, !=, NULL); 498eda14cbcSMatt Macy return (cabd); 499eda14cbcSMatt Macy } 500eda14cbcSMatt Macy 501eda14cbcSMatt Macy /* 502184c1b94SMartin Matuska * Allocate a new ABD, using the provided struct (if non-NULL, and if 503184c1b94SMartin Matuska * circumstances allow - otherwise allocate the struct). The returned ABD will 504184c1b94SMartin Matuska * point to offset off of sabd. It shares the underlying buffer data with sabd. 505184c1b94SMartin Matuska * Use abd_free() to free. sabd must not be freed while any derived ABDs exist. 506eda14cbcSMatt Macy */ 507eda14cbcSMatt Macy static abd_t * 508184c1b94SMartin Matuska abd_get_offset_impl(abd_t *abd, abd_t *sabd, size_t off, size_t size) 509eda14cbcSMatt Macy { 510eda14cbcSMatt Macy abd_verify(sabd); 511184c1b94SMartin Matuska ASSERT3U(off + size, <=, sabd->abd_size); 512eda14cbcSMatt Macy 513eda14cbcSMatt Macy if (abd_is_linear(sabd)) { 514184c1b94SMartin Matuska if (abd == NULL) 515eda14cbcSMatt Macy abd = abd_alloc_struct(0); 516eda14cbcSMatt Macy /* 517eda14cbcSMatt Macy * Even if this buf is filesystem metadata, we only track that 518eda14cbcSMatt Macy * if we own the underlying data buffer, which is not true in 519eda14cbcSMatt Macy * this case. Therefore, we don't ever use ABD_FLAG_META here. 520eda14cbcSMatt Macy */ 521184c1b94SMartin Matuska abd->abd_flags |= ABD_FLAG_LINEAR; 522eda14cbcSMatt Macy 523eda14cbcSMatt Macy ABD_LINEAR_BUF(abd) = (char *)ABD_LINEAR_BUF(sabd) + off; 524eda14cbcSMatt Macy } else if (abd_is_gang(sabd)) { 525eda14cbcSMatt Macy size_t left = size; 526184c1b94SMartin Matuska if (abd == NULL) { 527184c1b94SMartin Matuska abd = abd_alloc_gang(); 528184c1b94SMartin Matuska } else { 529184c1b94SMartin Matuska abd->abd_flags |= ABD_FLAG_GANG; 530184c1b94SMartin Matuska list_create(&ABD_GANG(abd).abd_gang_chain, 531184c1b94SMartin Matuska sizeof (abd_t), offsetof(abd_t, abd_gang_link)); 532184c1b94SMartin Matuska } 533184c1b94SMartin Matuska 534eda14cbcSMatt Macy abd->abd_flags &= ~ABD_FLAG_OWNER; 535eda14cbcSMatt Macy for (abd_t *cabd = abd_gang_get_offset(sabd, &off); 536eda14cbcSMatt Macy cabd != NULL && left > 0; 537eda14cbcSMatt Macy cabd = list_next(&ABD_GANG(sabd).abd_gang_chain, cabd)) { 538eda14cbcSMatt Macy int csize = MIN(left, cabd->abd_size - off); 539eda14cbcSMatt Macy 540184c1b94SMartin Matuska abd_t *nabd = abd_get_offset_size(cabd, off, csize); 541184c1b94SMartin Matuska abd_gang_add(abd, nabd, B_TRUE); 542eda14cbcSMatt Macy left -= csize; 543eda14cbcSMatt Macy off = 0; 544eda14cbcSMatt Macy } 545eda14cbcSMatt Macy ASSERT3U(left, ==, 0); 546eda14cbcSMatt Macy } else { 5477cd22ac4SMartin Matuska abd = abd_get_offset_scatter(abd, sabd, off, size); 548eda14cbcSMatt Macy } 549eda14cbcSMatt Macy 550184c1b94SMartin Matuska ASSERT3P(abd, !=, NULL); 551eda14cbcSMatt Macy abd->abd_size = size; 552184c1b94SMartin Matuska #ifdef ZFS_DEBUG 553eda14cbcSMatt Macy abd->abd_parent = sabd; 554eda14cbcSMatt Macy (void) zfs_refcount_add_many(&sabd->abd_children, abd->abd_size, abd); 555184c1b94SMartin Matuska #endif 556eda14cbcSMatt Macy return (abd); 557eda14cbcSMatt Macy } 558eda14cbcSMatt Macy 559184c1b94SMartin Matuska /* 560184c1b94SMartin Matuska * Like abd_get_offset_size(), but memory for the abd_t is provided by the 561184c1b94SMartin Matuska * caller. Using this routine can improve performance by avoiding the cost 562184c1b94SMartin Matuska * of allocating memory for the abd_t struct, and updating the abd stats. 563184c1b94SMartin Matuska * Usually, the provided abd is returned, but in some circumstances (FreeBSD, 564184c1b94SMartin Matuska * if sabd is scatter and size is more than 2 pages) a new abd_t may need to 565184c1b94SMartin Matuska * be allocated. Therefore callers should be careful to use the returned 566184c1b94SMartin Matuska * abd_t*. 567184c1b94SMartin Matuska */ 568184c1b94SMartin Matuska abd_t * 569184c1b94SMartin Matuska abd_get_offset_struct(abd_t *abd, abd_t *sabd, size_t off, size_t size) 570184c1b94SMartin Matuska { 5719db44a8eSMartin Matuska abd_t *result; 572184c1b94SMartin Matuska abd_init_struct(abd); 5739db44a8eSMartin Matuska result = abd_get_offset_impl(abd, sabd, off, size); 5749db44a8eSMartin Matuska if (result != abd) 5759db44a8eSMartin Matuska abd_fini_struct(abd); 5769db44a8eSMartin Matuska return (result); 577184c1b94SMartin Matuska } 578184c1b94SMartin Matuska 579eda14cbcSMatt Macy abd_t * 580eda14cbcSMatt Macy abd_get_offset(abd_t *sabd, size_t off) 581eda14cbcSMatt Macy { 582eda14cbcSMatt Macy size_t size = sabd->abd_size > off ? sabd->abd_size - off : 0; 583eda14cbcSMatt Macy VERIFY3U(size, >, 0); 584184c1b94SMartin Matuska return (abd_get_offset_impl(NULL, sabd, off, size)); 585eda14cbcSMatt Macy } 586eda14cbcSMatt Macy 587eda14cbcSMatt Macy abd_t * 588eda14cbcSMatt Macy abd_get_offset_size(abd_t *sabd, size_t off, size_t size) 589eda14cbcSMatt Macy { 590eda14cbcSMatt Macy ASSERT3U(off + size, <=, sabd->abd_size); 591184c1b94SMartin Matuska return (abd_get_offset_impl(NULL, sabd, off, size)); 592eda14cbcSMatt Macy } 593eda14cbcSMatt Macy 594eda14cbcSMatt Macy /* 595184c1b94SMartin Matuska * Return a size scatter ABD containing only zeros. 596eda14cbcSMatt Macy */ 597eda14cbcSMatt Macy abd_t * 598eda14cbcSMatt Macy abd_get_zeros(size_t size) 599eda14cbcSMatt Macy { 600eda14cbcSMatt Macy ASSERT3P(abd_zero_scatter, !=, NULL); 601eda14cbcSMatt Macy ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 602eda14cbcSMatt Macy return (abd_get_offset_size(abd_zero_scatter, 0, size)); 603eda14cbcSMatt Macy } 604eda14cbcSMatt Macy 605eda14cbcSMatt Macy /* 606184c1b94SMartin Matuska * Allocate a linear ABD structure for buf. 607eda14cbcSMatt Macy */ 608eda14cbcSMatt Macy abd_t * 609eda14cbcSMatt Macy abd_get_from_buf(void *buf, size_t size) 610eda14cbcSMatt Macy { 611eda14cbcSMatt Macy abd_t *abd = abd_alloc_struct(0); 612eda14cbcSMatt Macy 613eda14cbcSMatt Macy VERIFY3U(size, <=, SPA_MAXBLOCKSIZE); 614eda14cbcSMatt Macy 615eda14cbcSMatt Macy /* 616eda14cbcSMatt Macy * Even if this buf is filesystem metadata, we only track that if we 617eda14cbcSMatt Macy * own the underlying data buffer, which is not true in this case. 618eda14cbcSMatt Macy * Therefore, we don't ever use ABD_FLAG_META here. 619eda14cbcSMatt Macy */ 620184c1b94SMartin Matuska abd->abd_flags |= ABD_FLAG_LINEAR; 621eda14cbcSMatt Macy abd->abd_size = size; 622eda14cbcSMatt Macy 623eda14cbcSMatt Macy ABD_LINEAR_BUF(abd) = buf; 624eda14cbcSMatt Macy 625eda14cbcSMatt Macy return (abd); 626eda14cbcSMatt Macy } 627eda14cbcSMatt Macy 628eda14cbcSMatt Macy /* 629eda14cbcSMatt Macy * Get the raw buffer associated with a linear ABD. 630eda14cbcSMatt Macy */ 631eda14cbcSMatt Macy void * 632eda14cbcSMatt Macy abd_to_buf(abd_t *abd) 633eda14cbcSMatt Macy { 634eda14cbcSMatt Macy ASSERT(abd_is_linear(abd)); 635eda14cbcSMatt Macy abd_verify(abd); 636eda14cbcSMatt Macy return (ABD_LINEAR_BUF(abd)); 637eda14cbcSMatt Macy } 638eda14cbcSMatt Macy 639eda14cbcSMatt Macy /* 640eda14cbcSMatt Macy * Borrow a raw buffer from an ABD without copying the contents of the ABD 641eda14cbcSMatt Macy * into the buffer. If the ABD is scattered, this will allocate a raw buffer 642eda14cbcSMatt Macy * whose contents are undefined. To copy over the existing data in the ABD, use 643eda14cbcSMatt Macy * abd_borrow_buf_copy() instead. 644eda14cbcSMatt Macy */ 645eda14cbcSMatt Macy void * 646eda14cbcSMatt Macy abd_borrow_buf(abd_t *abd, size_t n) 647eda14cbcSMatt Macy { 648eda14cbcSMatt Macy void *buf; 649eda14cbcSMatt Macy abd_verify(abd); 650eda14cbcSMatt Macy ASSERT3U(abd->abd_size, >=, n); 651eda14cbcSMatt Macy if (abd_is_linear(abd)) { 652eda14cbcSMatt Macy buf = abd_to_buf(abd); 653eda14cbcSMatt Macy } else { 654eda14cbcSMatt Macy buf = zio_buf_alloc(n); 655eda14cbcSMatt Macy } 656184c1b94SMartin Matuska #ifdef ZFS_DEBUG 657eda14cbcSMatt Macy (void) zfs_refcount_add_many(&abd->abd_children, n, buf); 658184c1b94SMartin Matuska #endif 659eda14cbcSMatt Macy return (buf); 660eda14cbcSMatt Macy } 661eda14cbcSMatt Macy 662eda14cbcSMatt Macy void * 663eda14cbcSMatt Macy abd_borrow_buf_copy(abd_t *abd, size_t n) 664eda14cbcSMatt Macy { 665eda14cbcSMatt Macy void *buf = abd_borrow_buf(abd, n); 666eda14cbcSMatt Macy if (!abd_is_linear(abd)) { 667eda14cbcSMatt Macy abd_copy_to_buf(buf, abd, n); 668eda14cbcSMatt Macy } 669eda14cbcSMatt Macy return (buf); 670eda14cbcSMatt Macy } 671eda14cbcSMatt Macy 672eda14cbcSMatt Macy /* 673eda14cbcSMatt Macy * Return a borrowed raw buffer to an ABD. If the ABD is scattered, this will 674eda14cbcSMatt Macy * not change the contents of the ABD and will ASSERT that you didn't modify 675eda14cbcSMatt Macy * the buffer since it was borrowed. If you want any changes you made to buf to 676eda14cbcSMatt Macy * be copied back to abd, use abd_return_buf_copy() instead. 677eda14cbcSMatt Macy */ 678eda14cbcSMatt Macy void 679eda14cbcSMatt Macy abd_return_buf(abd_t *abd, void *buf, size_t n) 680eda14cbcSMatt Macy { 681eda14cbcSMatt Macy abd_verify(abd); 682eda14cbcSMatt Macy ASSERT3U(abd->abd_size, >=, n); 683dbd5678dSMartin Matuska #ifdef ZFS_DEBUG 684dbd5678dSMartin Matuska (void) zfs_refcount_remove_many(&abd->abd_children, n, buf); 685dbd5678dSMartin Matuska #endif 686eda14cbcSMatt Macy if (abd_is_linear(abd)) { 687eda14cbcSMatt Macy ASSERT3P(buf, ==, abd_to_buf(abd)); 688eda14cbcSMatt Macy } else { 689eda14cbcSMatt Macy ASSERT0(abd_cmp_buf(abd, buf, n)); 690eda14cbcSMatt Macy zio_buf_free(buf, n); 691eda14cbcSMatt Macy } 692eda14cbcSMatt Macy } 693eda14cbcSMatt Macy 694eda14cbcSMatt Macy void 695eda14cbcSMatt Macy abd_return_buf_copy(abd_t *abd, void *buf, size_t n) 696eda14cbcSMatt Macy { 697eda14cbcSMatt Macy if (!abd_is_linear(abd)) { 698eda14cbcSMatt Macy abd_copy_from_buf(abd, buf, n); 699eda14cbcSMatt Macy } 700eda14cbcSMatt Macy abd_return_buf(abd, buf, n); 701eda14cbcSMatt Macy } 702eda14cbcSMatt Macy 703eda14cbcSMatt Macy void 704eda14cbcSMatt Macy abd_release_ownership_of_buf(abd_t *abd) 705eda14cbcSMatt Macy { 706eda14cbcSMatt Macy ASSERT(abd_is_linear(abd)); 707eda14cbcSMatt Macy ASSERT(abd->abd_flags & ABD_FLAG_OWNER); 708eda14cbcSMatt Macy 709eda14cbcSMatt Macy /* 710eda14cbcSMatt Macy * abd_free() needs to handle LINEAR_PAGE ABD's specially. 711eda14cbcSMatt Macy * Since that flag does not survive the 712eda14cbcSMatt Macy * abd_release_ownership_of_buf() -> abd_get_from_buf() -> 713eda14cbcSMatt Macy * abd_take_ownership_of_buf() sequence, we don't allow releasing 714eda14cbcSMatt Macy * these "linear but not zio_[data_]buf_alloc()'ed" ABD's. 715eda14cbcSMatt Macy */ 716eda14cbcSMatt Macy ASSERT(!abd_is_linear_page(abd)); 717eda14cbcSMatt Macy 718eda14cbcSMatt Macy abd_verify(abd); 719eda14cbcSMatt Macy 720eda14cbcSMatt Macy abd->abd_flags &= ~ABD_FLAG_OWNER; 721eda14cbcSMatt Macy /* Disable this flag since we no longer own the data buffer */ 722eda14cbcSMatt Macy abd->abd_flags &= ~ABD_FLAG_META; 723eda14cbcSMatt Macy 724eda14cbcSMatt Macy abd_update_linear_stats(abd, ABDSTAT_DECR); 725eda14cbcSMatt Macy } 726eda14cbcSMatt Macy 727eda14cbcSMatt Macy 728eda14cbcSMatt Macy /* 729eda14cbcSMatt Macy * Give this ABD ownership of the buffer that it's storing. Can only be used on 730eda14cbcSMatt Macy * linear ABDs which were allocated via abd_get_from_buf(), or ones allocated 731eda14cbcSMatt Macy * with abd_alloc_linear() which subsequently released ownership of their buf 732eda14cbcSMatt Macy * with abd_release_ownership_of_buf(). 733eda14cbcSMatt Macy */ 734eda14cbcSMatt Macy void 735eda14cbcSMatt Macy abd_take_ownership_of_buf(abd_t *abd, boolean_t is_metadata) 736eda14cbcSMatt Macy { 737eda14cbcSMatt Macy ASSERT(abd_is_linear(abd)); 738eda14cbcSMatt Macy ASSERT(!(abd->abd_flags & ABD_FLAG_OWNER)); 739eda14cbcSMatt Macy abd_verify(abd); 740eda14cbcSMatt Macy 741eda14cbcSMatt Macy abd->abd_flags |= ABD_FLAG_OWNER; 742eda14cbcSMatt Macy if (is_metadata) { 743eda14cbcSMatt Macy abd->abd_flags |= ABD_FLAG_META; 744eda14cbcSMatt Macy } 745eda14cbcSMatt Macy 746eda14cbcSMatt Macy abd_update_linear_stats(abd, ABDSTAT_INCR); 747eda14cbcSMatt Macy } 748eda14cbcSMatt Macy 749eda14cbcSMatt Macy /* 750eda14cbcSMatt Macy * Initializes an abd_iter based on whether the abd is a gang ABD 751eda14cbcSMatt Macy * or just a single ABD. 752eda14cbcSMatt Macy */ 753eda14cbcSMatt Macy static inline abd_t * 754eda14cbcSMatt Macy abd_init_abd_iter(abd_t *abd, struct abd_iter *aiter, size_t off) 755eda14cbcSMatt Macy { 756eda14cbcSMatt Macy abd_t *cabd = NULL; 757eda14cbcSMatt Macy 758eda14cbcSMatt Macy if (abd_is_gang(abd)) { 759eda14cbcSMatt Macy cabd = abd_gang_get_offset(abd, &off); 760eda14cbcSMatt Macy if (cabd) { 761eda14cbcSMatt Macy abd_iter_init(aiter, cabd); 762eda14cbcSMatt Macy abd_iter_advance(aiter, off); 763eda14cbcSMatt Macy } 764eda14cbcSMatt Macy } else { 765eda14cbcSMatt Macy abd_iter_init(aiter, abd); 766eda14cbcSMatt Macy abd_iter_advance(aiter, off); 767eda14cbcSMatt Macy } 768eda14cbcSMatt Macy return (cabd); 769eda14cbcSMatt Macy } 770eda14cbcSMatt Macy 771eda14cbcSMatt Macy /* 772eda14cbcSMatt Macy * Advances an abd_iter. We have to be careful with gang ABD as 773eda14cbcSMatt Macy * advancing could mean that we are at the end of a particular ABD and 774eda14cbcSMatt Macy * must grab the ABD in the gang ABD's list. 775eda14cbcSMatt Macy */ 776eda14cbcSMatt Macy static inline abd_t * 777eda14cbcSMatt Macy abd_advance_abd_iter(abd_t *abd, abd_t *cabd, struct abd_iter *aiter, 778eda14cbcSMatt Macy size_t len) 779eda14cbcSMatt Macy { 780eda14cbcSMatt Macy abd_iter_advance(aiter, len); 781eda14cbcSMatt Macy if (abd_is_gang(abd) && abd_iter_at_end(aiter)) { 782eda14cbcSMatt Macy ASSERT3P(cabd, !=, NULL); 783eda14cbcSMatt Macy cabd = list_next(&ABD_GANG(abd).abd_gang_chain, cabd); 784eda14cbcSMatt Macy if (cabd) { 785eda14cbcSMatt Macy abd_iter_init(aiter, cabd); 786eda14cbcSMatt Macy abd_iter_advance(aiter, 0); 787eda14cbcSMatt Macy } 788eda14cbcSMatt Macy } 789eda14cbcSMatt Macy return (cabd); 790eda14cbcSMatt Macy } 791eda14cbcSMatt Macy 792eda14cbcSMatt Macy int 793eda14cbcSMatt Macy abd_iterate_func(abd_t *abd, size_t off, size_t size, 794eda14cbcSMatt Macy abd_iter_func_t *func, void *private) 795eda14cbcSMatt Macy { 796eda14cbcSMatt Macy struct abd_iter aiter; 7977877fdebSMatt Macy int ret = 0; 7987877fdebSMatt Macy 7997877fdebSMatt Macy if (size == 0) 8007877fdebSMatt Macy return (0); 801eda14cbcSMatt Macy 802eda14cbcSMatt Macy abd_verify(abd); 803eda14cbcSMatt Macy ASSERT3U(off + size, <=, abd->abd_size); 804eda14cbcSMatt Macy 8057877fdebSMatt Macy abd_t *c_abd = abd_init_abd_iter(abd, &aiter, off); 806eda14cbcSMatt Macy 807eda14cbcSMatt Macy while (size > 0) { 8086c1e79dfSMartin Matuska IMPLY(abd_is_gang(abd), c_abd != NULL); 809eda14cbcSMatt Macy 810eda14cbcSMatt Macy abd_iter_map(&aiter); 811eda14cbcSMatt Macy 812eda14cbcSMatt Macy size_t len = MIN(aiter.iter_mapsize, size); 813eda14cbcSMatt Macy ASSERT3U(len, >, 0); 814eda14cbcSMatt Macy 815eda14cbcSMatt Macy ret = func(aiter.iter_mapaddr, len, private); 816eda14cbcSMatt Macy 817eda14cbcSMatt Macy abd_iter_unmap(&aiter); 818eda14cbcSMatt Macy 819eda14cbcSMatt Macy if (ret != 0) 820eda14cbcSMatt Macy break; 821eda14cbcSMatt Macy 822eda14cbcSMatt Macy size -= len; 823eda14cbcSMatt Macy c_abd = abd_advance_abd_iter(abd, c_abd, &aiter, len); 824eda14cbcSMatt Macy } 825eda14cbcSMatt Macy 826eda14cbcSMatt Macy return (ret); 827eda14cbcSMatt Macy } 828eda14cbcSMatt Macy 829eda14cbcSMatt Macy struct buf_arg { 830eda14cbcSMatt Macy void *arg_buf; 831eda14cbcSMatt Macy }; 832eda14cbcSMatt Macy 833eda14cbcSMatt Macy static int 834eda14cbcSMatt Macy abd_copy_to_buf_off_cb(void *buf, size_t size, void *private) 835eda14cbcSMatt Macy { 836eda14cbcSMatt Macy struct buf_arg *ba_ptr = private; 837eda14cbcSMatt Macy 838eda14cbcSMatt Macy (void) memcpy(ba_ptr->arg_buf, buf, size); 839eda14cbcSMatt Macy ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size; 840eda14cbcSMatt Macy 841eda14cbcSMatt Macy return (0); 842eda14cbcSMatt Macy } 843eda14cbcSMatt Macy 844eda14cbcSMatt Macy /* 845eda14cbcSMatt Macy * Copy abd to buf. (off is the offset in abd.) 846eda14cbcSMatt Macy */ 847eda14cbcSMatt Macy void 848eda14cbcSMatt Macy abd_copy_to_buf_off(void *buf, abd_t *abd, size_t off, size_t size) 849eda14cbcSMatt Macy { 850eda14cbcSMatt Macy struct buf_arg ba_ptr = { buf }; 851eda14cbcSMatt Macy 852eda14cbcSMatt Macy (void) abd_iterate_func(abd, off, size, abd_copy_to_buf_off_cb, 853eda14cbcSMatt Macy &ba_ptr); 854eda14cbcSMatt Macy } 855eda14cbcSMatt Macy 856eda14cbcSMatt Macy static int 857eda14cbcSMatt Macy abd_cmp_buf_off_cb(void *buf, size_t size, void *private) 858eda14cbcSMatt Macy { 859eda14cbcSMatt Macy int ret; 860eda14cbcSMatt Macy struct buf_arg *ba_ptr = private; 861eda14cbcSMatt Macy 862eda14cbcSMatt Macy ret = memcmp(buf, ba_ptr->arg_buf, size); 863eda14cbcSMatt Macy ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size; 864eda14cbcSMatt Macy 865eda14cbcSMatt Macy return (ret); 866eda14cbcSMatt Macy } 867eda14cbcSMatt Macy 868eda14cbcSMatt Macy /* 869eda14cbcSMatt Macy * Compare the contents of abd to buf. (off is the offset in abd.) 870eda14cbcSMatt Macy */ 871eda14cbcSMatt Macy int 872eda14cbcSMatt Macy abd_cmp_buf_off(abd_t *abd, const void *buf, size_t off, size_t size) 873eda14cbcSMatt Macy { 874eda14cbcSMatt Macy struct buf_arg ba_ptr = { (void *) buf }; 875eda14cbcSMatt Macy 876eda14cbcSMatt Macy return (abd_iterate_func(abd, off, size, abd_cmp_buf_off_cb, &ba_ptr)); 877eda14cbcSMatt Macy } 878eda14cbcSMatt Macy 879eda14cbcSMatt Macy static int 880eda14cbcSMatt Macy abd_copy_from_buf_off_cb(void *buf, size_t size, void *private) 881eda14cbcSMatt Macy { 882eda14cbcSMatt Macy struct buf_arg *ba_ptr = private; 883eda14cbcSMatt Macy 884eda14cbcSMatt Macy (void) memcpy(buf, ba_ptr->arg_buf, size); 885eda14cbcSMatt Macy ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size; 886eda14cbcSMatt Macy 887eda14cbcSMatt Macy return (0); 888eda14cbcSMatt Macy } 889eda14cbcSMatt Macy 890eda14cbcSMatt Macy /* 891eda14cbcSMatt Macy * Copy from buf to abd. (off is the offset in abd.) 892eda14cbcSMatt Macy */ 893eda14cbcSMatt Macy void 894eda14cbcSMatt Macy abd_copy_from_buf_off(abd_t *abd, const void *buf, size_t off, size_t size) 895eda14cbcSMatt Macy { 896eda14cbcSMatt Macy struct buf_arg ba_ptr = { (void *) buf }; 897eda14cbcSMatt Macy 898eda14cbcSMatt Macy (void) abd_iterate_func(abd, off, size, abd_copy_from_buf_off_cb, 899eda14cbcSMatt Macy &ba_ptr); 900eda14cbcSMatt Macy } 901eda14cbcSMatt Macy 902eda14cbcSMatt Macy static int 903eda14cbcSMatt Macy abd_zero_off_cb(void *buf, size_t size, void *private) 904eda14cbcSMatt Macy { 905e92ffd9bSMartin Matuska (void) private; 906eda14cbcSMatt Macy (void) memset(buf, 0, size); 907eda14cbcSMatt Macy return (0); 908eda14cbcSMatt Macy } 909eda14cbcSMatt Macy 910eda14cbcSMatt Macy /* 911eda14cbcSMatt Macy * Zero out the abd from a particular offset to the end. 912eda14cbcSMatt Macy */ 913eda14cbcSMatt Macy void 914eda14cbcSMatt Macy abd_zero_off(abd_t *abd, size_t off, size_t size) 915eda14cbcSMatt Macy { 916eda14cbcSMatt Macy (void) abd_iterate_func(abd, off, size, abd_zero_off_cb, NULL); 917eda14cbcSMatt Macy } 918eda14cbcSMatt Macy 919eda14cbcSMatt Macy /* 920eda14cbcSMatt Macy * Iterate over two ABDs and call func incrementally on the two ABDs' data in 921eda14cbcSMatt Macy * equal-sized chunks (passed to func as raw buffers). func could be called many 922eda14cbcSMatt Macy * times during this iteration. 923eda14cbcSMatt Macy */ 924eda14cbcSMatt Macy int 925eda14cbcSMatt Macy abd_iterate_func2(abd_t *dabd, abd_t *sabd, size_t doff, size_t soff, 926eda14cbcSMatt Macy size_t size, abd_iter_func2_t *func, void *private) 927eda14cbcSMatt Macy { 928eda14cbcSMatt Macy int ret = 0; 929eda14cbcSMatt Macy struct abd_iter daiter, saiter; 930eda14cbcSMatt Macy abd_t *c_dabd, *c_sabd; 931eda14cbcSMatt Macy 9327877fdebSMatt Macy if (size == 0) 9337877fdebSMatt Macy return (0); 9347877fdebSMatt Macy 935eda14cbcSMatt Macy abd_verify(dabd); 936eda14cbcSMatt Macy abd_verify(sabd); 937eda14cbcSMatt Macy 938eda14cbcSMatt Macy ASSERT3U(doff + size, <=, dabd->abd_size); 939eda14cbcSMatt Macy ASSERT3U(soff + size, <=, sabd->abd_size); 940eda14cbcSMatt Macy 941eda14cbcSMatt Macy c_dabd = abd_init_abd_iter(dabd, &daiter, doff); 942eda14cbcSMatt Macy c_sabd = abd_init_abd_iter(sabd, &saiter, soff); 943eda14cbcSMatt Macy 944eda14cbcSMatt Macy while (size > 0) { 9456c1e79dfSMartin Matuska IMPLY(abd_is_gang(dabd), c_dabd != NULL); 9466c1e79dfSMartin Matuska IMPLY(abd_is_gang(sabd), c_sabd != NULL); 947eda14cbcSMatt Macy 948eda14cbcSMatt Macy abd_iter_map(&daiter); 949eda14cbcSMatt Macy abd_iter_map(&saiter); 950eda14cbcSMatt Macy 951eda14cbcSMatt Macy size_t dlen = MIN(daiter.iter_mapsize, size); 952eda14cbcSMatt Macy size_t slen = MIN(saiter.iter_mapsize, size); 953eda14cbcSMatt Macy size_t len = MIN(dlen, slen); 954eda14cbcSMatt Macy ASSERT(dlen > 0 || slen > 0); 955eda14cbcSMatt Macy 956eda14cbcSMatt Macy ret = func(daiter.iter_mapaddr, saiter.iter_mapaddr, len, 957eda14cbcSMatt Macy private); 958eda14cbcSMatt Macy 959eda14cbcSMatt Macy abd_iter_unmap(&saiter); 960eda14cbcSMatt Macy abd_iter_unmap(&daiter); 961eda14cbcSMatt Macy 962eda14cbcSMatt Macy if (ret != 0) 963eda14cbcSMatt Macy break; 964eda14cbcSMatt Macy 965eda14cbcSMatt Macy size -= len; 966eda14cbcSMatt Macy c_dabd = 967eda14cbcSMatt Macy abd_advance_abd_iter(dabd, c_dabd, &daiter, len); 968eda14cbcSMatt Macy c_sabd = 969eda14cbcSMatt Macy abd_advance_abd_iter(sabd, c_sabd, &saiter, len); 970eda14cbcSMatt Macy } 971eda14cbcSMatt Macy 972eda14cbcSMatt Macy return (ret); 973eda14cbcSMatt Macy } 974eda14cbcSMatt Macy 975eda14cbcSMatt Macy static int 976eda14cbcSMatt Macy abd_copy_off_cb(void *dbuf, void *sbuf, size_t size, void *private) 977eda14cbcSMatt Macy { 978e92ffd9bSMartin Matuska (void) private; 979eda14cbcSMatt Macy (void) memcpy(dbuf, sbuf, size); 980eda14cbcSMatt Macy return (0); 981eda14cbcSMatt Macy } 982eda14cbcSMatt Macy 983eda14cbcSMatt Macy /* 984eda14cbcSMatt Macy * Copy from sabd to dabd starting from soff and doff. 985eda14cbcSMatt Macy */ 986eda14cbcSMatt Macy void 987eda14cbcSMatt Macy abd_copy_off(abd_t *dabd, abd_t *sabd, size_t doff, size_t soff, size_t size) 988eda14cbcSMatt Macy { 989eda14cbcSMatt Macy (void) abd_iterate_func2(dabd, sabd, doff, soff, size, 990eda14cbcSMatt Macy abd_copy_off_cb, NULL); 991eda14cbcSMatt Macy } 992eda14cbcSMatt Macy 993eda14cbcSMatt Macy static int 994eda14cbcSMatt Macy abd_cmp_cb(void *bufa, void *bufb, size_t size, void *private) 995eda14cbcSMatt Macy { 996e92ffd9bSMartin Matuska (void) private; 997eda14cbcSMatt Macy return (memcmp(bufa, bufb, size)); 998eda14cbcSMatt Macy } 999eda14cbcSMatt Macy 1000eda14cbcSMatt Macy /* 1001eda14cbcSMatt Macy * Compares the contents of two ABDs. 1002eda14cbcSMatt Macy */ 1003eda14cbcSMatt Macy int 1004eda14cbcSMatt Macy abd_cmp(abd_t *dabd, abd_t *sabd) 1005eda14cbcSMatt Macy { 1006eda14cbcSMatt Macy ASSERT3U(dabd->abd_size, ==, sabd->abd_size); 1007eda14cbcSMatt Macy return (abd_iterate_func2(dabd, sabd, 0, 0, dabd->abd_size, 1008eda14cbcSMatt Macy abd_cmp_cb, NULL)); 1009eda14cbcSMatt Macy } 1010eda14cbcSMatt Macy 1011eda14cbcSMatt Macy /* 1012eda14cbcSMatt Macy * Iterate over code ABDs and a data ABD and call @func_raidz_gen. 1013eda14cbcSMatt Macy * 1014eda14cbcSMatt Macy * @cabds parity ABDs, must have equal size 1015eda14cbcSMatt Macy * @dabd data ABD. Can be NULL (in this case @dsize = 0) 1016eda14cbcSMatt Macy * @func_raidz_gen should be implemented so that its behaviour 1017eda14cbcSMatt Macy * is the same when taking linear and when taking scatter 1018eda14cbcSMatt Macy */ 1019eda14cbcSMatt Macy void 1020f8b1db88SMartin Matuska abd_raidz_gen_iterate(abd_t **cabds, abd_t *dabd, size_t off, 1021f8b1db88SMartin Matuska size_t csize, size_t dsize, const unsigned parity, 1022eda14cbcSMatt Macy void (*func_raidz_gen)(void **, const void *, size_t, size_t)) 1023eda14cbcSMatt Macy { 1024eda14cbcSMatt Macy int i; 1025f8b1db88SMartin Matuska size_t len, dlen; 1026eda14cbcSMatt Macy struct abd_iter caiters[3]; 10276c1e79dfSMartin Matuska struct abd_iter daiter; 1028*14c2e0a0SMartin Matuska void *caddrs[3], *daddr; 1029eda14cbcSMatt Macy unsigned long flags __maybe_unused = 0; 1030eda14cbcSMatt Macy abd_t *c_cabds[3]; 1031eda14cbcSMatt Macy abd_t *c_dabd = NULL; 1032eda14cbcSMatt Macy 1033eda14cbcSMatt Macy ASSERT3U(parity, <=, 3); 1034eda14cbcSMatt Macy for (i = 0; i < parity; i++) { 10356c1e79dfSMartin Matuska abd_verify(cabds[i]); 1036f8b1db88SMartin Matuska ASSERT3U(off + csize, <=, cabds[i]->abd_size); 1037f8b1db88SMartin Matuska c_cabds[i] = abd_init_abd_iter(cabds[i], &caiters[i], off); 1038eda14cbcSMatt Macy } 1039eda14cbcSMatt Macy 10406c1e79dfSMartin Matuska if (dsize > 0) { 10416c1e79dfSMartin Matuska ASSERT(dabd); 10426c1e79dfSMartin Matuska abd_verify(dabd); 1043f8b1db88SMartin Matuska ASSERT3U(off + dsize, <=, dabd->abd_size); 1044f8b1db88SMartin Matuska c_dabd = abd_init_abd_iter(dabd, &daiter, off); 1045eda14cbcSMatt Macy } 1046eda14cbcSMatt Macy 1047eda14cbcSMatt Macy abd_enter_critical(flags); 1048eda14cbcSMatt Macy while (csize > 0) { 10496c1e79dfSMartin Matuska len = csize; 1050eda14cbcSMatt Macy for (i = 0; i < parity; i++) { 10516c1e79dfSMartin Matuska IMPLY(abd_is_gang(cabds[i]), c_cabds[i] != NULL); 1052eda14cbcSMatt Macy abd_iter_map(&caiters[i]); 1053eda14cbcSMatt Macy caddrs[i] = caiters[i].iter_mapaddr; 10546c1e79dfSMartin Matuska len = MIN(caiters[i].iter_mapsize, len); 1055eda14cbcSMatt Macy } 1056eda14cbcSMatt Macy 10576c1e79dfSMartin Matuska if (dsize > 0) { 10586c1e79dfSMartin Matuska IMPLY(abd_is_gang(dabd), c_dabd != NULL); 1059eda14cbcSMatt Macy abd_iter_map(&daiter); 1060*14c2e0a0SMartin Matuska daddr = daiter.iter_mapaddr; 1061eda14cbcSMatt Macy len = MIN(daiter.iter_mapsize, len); 1062eda14cbcSMatt Macy dlen = len; 1063*14c2e0a0SMartin Matuska } else { 1064*14c2e0a0SMartin Matuska daddr = NULL; 1065eda14cbcSMatt Macy dlen = 0; 1066*14c2e0a0SMartin Matuska } 1067eda14cbcSMatt Macy 1068eda14cbcSMatt Macy /* must be progressive */ 1069f8b1db88SMartin Matuska ASSERT3U(len, >, 0); 1070eda14cbcSMatt Macy /* 1071eda14cbcSMatt Macy * The iterated function likely will not do well if each 1072eda14cbcSMatt Macy * segment except the last one is not multiple of 512 (raidz). 1073eda14cbcSMatt Macy */ 1074eda14cbcSMatt Macy ASSERT3U(((uint64_t)len & 511ULL), ==, 0); 1075eda14cbcSMatt Macy 1076*14c2e0a0SMartin Matuska func_raidz_gen(caddrs, daddr, len, dlen); 1077eda14cbcSMatt Macy 1078eda14cbcSMatt Macy for (i = parity-1; i >= 0; i--) { 1079eda14cbcSMatt Macy abd_iter_unmap(&caiters[i]); 1080eda14cbcSMatt Macy c_cabds[i] = 1081eda14cbcSMatt Macy abd_advance_abd_iter(cabds[i], c_cabds[i], 1082eda14cbcSMatt Macy &caiters[i], len); 1083eda14cbcSMatt Macy } 1084eda14cbcSMatt Macy 10856c1e79dfSMartin Matuska if (dsize > 0) { 1086eda14cbcSMatt Macy abd_iter_unmap(&daiter); 1087eda14cbcSMatt Macy c_dabd = 1088eda14cbcSMatt Macy abd_advance_abd_iter(dabd, c_dabd, &daiter, 1089eda14cbcSMatt Macy dlen); 1090eda14cbcSMatt Macy dsize -= dlen; 1091eda14cbcSMatt Macy } 1092eda14cbcSMatt Macy 1093eda14cbcSMatt Macy csize -= len; 1094eda14cbcSMatt Macy } 1095eda14cbcSMatt Macy abd_exit_critical(flags); 1096eda14cbcSMatt Macy } 1097eda14cbcSMatt Macy 1098eda14cbcSMatt Macy /* 1099eda14cbcSMatt Macy * Iterate over code ABDs and data reconstruction target ABDs and call 1100eda14cbcSMatt Macy * @func_raidz_rec. Function maps at most 6 pages atomically. 1101eda14cbcSMatt Macy * 1102eda14cbcSMatt Macy * @cabds parity ABDs, must have equal size 1103eda14cbcSMatt Macy * @tabds rec target ABDs, at most 3 1104eda14cbcSMatt Macy * @tsize size of data target columns 1105eda14cbcSMatt Macy * @func_raidz_rec expects syndrome data in target columns. Function 1106eda14cbcSMatt Macy * reconstructs data and overwrites target columns. 1107eda14cbcSMatt Macy */ 1108eda14cbcSMatt Macy void 1109eda14cbcSMatt Macy abd_raidz_rec_iterate(abd_t **cabds, abd_t **tabds, 1110f8b1db88SMartin Matuska size_t tsize, const unsigned parity, 1111eda14cbcSMatt Macy void (*func_raidz_rec)(void **t, const size_t tsize, void **c, 1112eda14cbcSMatt Macy const unsigned *mul), 1113eda14cbcSMatt Macy const unsigned *mul) 1114eda14cbcSMatt Macy { 1115eda14cbcSMatt Macy int i; 1116f8b1db88SMartin Matuska size_t len; 1117eda14cbcSMatt Macy struct abd_iter citers[3]; 1118eda14cbcSMatt Macy struct abd_iter xiters[3]; 1119eda14cbcSMatt Macy void *caddrs[3], *xaddrs[3]; 1120eda14cbcSMatt Macy unsigned long flags __maybe_unused = 0; 1121eda14cbcSMatt Macy abd_t *c_cabds[3]; 1122eda14cbcSMatt Macy abd_t *c_tabds[3]; 1123eda14cbcSMatt Macy 1124eda14cbcSMatt Macy ASSERT3U(parity, <=, 3); 1125eda14cbcSMatt Macy 1126eda14cbcSMatt Macy for (i = 0; i < parity; i++) { 11276c1e79dfSMartin Matuska abd_verify(cabds[i]); 11286c1e79dfSMartin Matuska abd_verify(tabds[i]); 11296c1e79dfSMartin Matuska ASSERT3U(tsize, <=, cabds[i]->abd_size); 11306c1e79dfSMartin Matuska ASSERT3U(tsize, <=, tabds[i]->abd_size); 1131eda14cbcSMatt Macy c_cabds[i] = 1132eda14cbcSMatt Macy abd_init_abd_iter(cabds[i], &citers[i], 0); 1133eda14cbcSMatt Macy c_tabds[i] = 1134eda14cbcSMatt Macy abd_init_abd_iter(tabds[i], &xiters[i], 0); 1135eda14cbcSMatt Macy } 1136eda14cbcSMatt Macy 1137eda14cbcSMatt Macy abd_enter_critical(flags); 1138eda14cbcSMatt Macy while (tsize > 0) { 11396c1e79dfSMartin Matuska len = tsize; 1140eda14cbcSMatt Macy for (i = 0; i < parity; i++) { 11416c1e79dfSMartin Matuska IMPLY(abd_is_gang(cabds[i]), c_cabds[i] != NULL); 11426c1e79dfSMartin Matuska IMPLY(abd_is_gang(tabds[i]), c_tabds[i] != NULL); 1143eda14cbcSMatt Macy abd_iter_map(&citers[i]); 1144eda14cbcSMatt Macy abd_iter_map(&xiters[i]); 1145eda14cbcSMatt Macy caddrs[i] = citers[i].iter_mapaddr; 1146eda14cbcSMatt Macy xaddrs[i] = xiters[i].iter_mapaddr; 11476c1e79dfSMartin Matuska len = MIN(citers[i].iter_mapsize, len); 11486c1e79dfSMartin Matuska len = MIN(xiters[i].iter_mapsize, len); 1149eda14cbcSMatt Macy } 1150eda14cbcSMatt Macy 1151eda14cbcSMatt Macy /* must be progressive */ 1152eda14cbcSMatt Macy ASSERT3S(len, >, 0); 1153eda14cbcSMatt Macy /* 1154eda14cbcSMatt Macy * The iterated function likely will not do well if each 1155eda14cbcSMatt Macy * segment except the last one is not multiple of 512 (raidz). 1156eda14cbcSMatt Macy */ 1157eda14cbcSMatt Macy ASSERT3U(((uint64_t)len & 511ULL), ==, 0); 1158eda14cbcSMatt Macy 1159eda14cbcSMatt Macy func_raidz_rec(xaddrs, len, caddrs, mul); 1160eda14cbcSMatt Macy 1161eda14cbcSMatt Macy for (i = parity-1; i >= 0; i--) { 1162eda14cbcSMatt Macy abd_iter_unmap(&xiters[i]); 1163eda14cbcSMatt Macy abd_iter_unmap(&citers[i]); 1164eda14cbcSMatt Macy c_tabds[i] = 1165eda14cbcSMatt Macy abd_advance_abd_iter(tabds[i], c_tabds[i], 1166eda14cbcSMatt Macy &xiters[i], len); 1167eda14cbcSMatt Macy c_cabds[i] = 1168eda14cbcSMatt Macy abd_advance_abd_iter(cabds[i], c_cabds[i], 1169eda14cbcSMatt Macy &citers[i], len); 1170eda14cbcSMatt Macy } 1171eda14cbcSMatt Macy 1172eda14cbcSMatt Macy tsize -= len; 1173eda14cbcSMatt Macy ASSERT3S(tsize, >=, 0); 1174eda14cbcSMatt Macy } 1175eda14cbcSMatt Macy abd_exit_critical(flags); 1176eda14cbcSMatt Macy } 1177