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 9eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 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 boolean_t 109eda14cbcSMatt Macy abd_is_linear(abd_t *abd) 110eda14cbcSMatt Macy { 111eda14cbcSMatt Macy return ((abd->abd_flags & ABD_FLAG_LINEAR) != 0 ? B_TRUE : B_FALSE); 112eda14cbcSMatt Macy } 113eda14cbcSMatt Macy 114eda14cbcSMatt Macy boolean_t 115eda14cbcSMatt Macy abd_is_linear_page(abd_t *abd) 116eda14cbcSMatt Macy { 117eda14cbcSMatt Macy return ((abd->abd_flags & ABD_FLAG_LINEAR_PAGE) != 0 ? 118eda14cbcSMatt Macy B_TRUE : B_FALSE); 119eda14cbcSMatt Macy } 120eda14cbcSMatt Macy 121eda14cbcSMatt Macy boolean_t 122eda14cbcSMatt Macy abd_is_gang(abd_t *abd) 123eda14cbcSMatt Macy { 124eda14cbcSMatt Macy return ((abd->abd_flags & ABD_FLAG_GANG) != 0 ? B_TRUE : 125eda14cbcSMatt Macy B_FALSE); 126eda14cbcSMatt Macy } 127eda14cbcSMatt Macy 128eda14cbcSMatt Macy void 129eda14cbcSMatt Macy abd_verify(abd_t *abd) 130eda14cbcSMatt Macy { 131eda14cbcSMatt Macy ASSERT3U(abd->abd_size, >, 0); 132eda14cbcSMatt Macy ASSERT3U(abd->abd_size, <=, SPA_MAXBLOCKSIZE); 133eda14cbcSMatt Macy ASSERT3U(abd->abd_flags, ==, abd->abd_flags & (ABD_FLAG_LINEAR | 134eda14cbcSMatt Macy ABD_FLAG_OWNER | ABD_FLAG_META | ABD_FLAG_MULTI_ZONE | 135eda14cbcSMatt Macy ABD_FLAG_MULTI_CHUNK | ABD_FLAG_LINEAR_PAGE | ABD_FLAG_GANG | 136eda14cbcSMatt Macy ABD_FLAG_GANG_FREE | ABD_FLAG_ZEROS)); 137eda14cbcSMatt Macy IMPLY(abd->abd_parent != NULL, !(abd->abd_flags & ABD_FLAG_OWNER)); 138eda14cbcSMatt Macy IMPLY(abd->abd_flags & ABD_FLAG_META, abd->abd_flags & ABD_FLAG_OWNER); 139eda14cbcSMatt Macy if (abd_is_linear(abd)) { 140eda14cbcSMatt Macy ASSERT3P(ABD_LINEAR_BUF(abd), !=, NULL); 141eda14cbcSMatt Macy } else if (abd_is_gang(abd)) { 142eda14cbcSMatt Macy uint_t child_sizes = 0; 143eda14cbcSMatt Macy for (abd_t *cabd = list_head(&ABD_GANG(abd).abd_gang_chain); 144eda14cbcSMatt Macy cabd != NULL; 145eda14cbcSMatt Macy cabd = list_next(&ABD_GANG(abd).abd_gang_chain, cabd)) { 146eda14cbcSMatt Macy ASSERT(list_link_active(&cabd->abd_gang_link)); 147eda14cbcSMatt Macy child_sizes += cabd->abd_size; 148eda14cbcSMatt Macy abd_verify(cabd); 149eda14cbcSMatt Macy } 150eda14cbcSMatt Macy ASSERT3U(abd->abd_size, ==, child_sizes); 151eda14cbcSMatt Macy } else { 152eda14cbcSMatt Macy abd_verify_scatter(abd); 153eda14cbcSMatt Macy } 154eda14cbcSMatt Macy } 155eda14cbcSMatt Macy 156eda14cbcSMatt Macy uint_t 157eda14cbcSMatt Macy abd_get_size(abd_t *abd) 158eda14cbcSMatt Macy { 159eda14cbcSMatt Macy abd_verify(abd); 160eda14cbcSMatt Macy return (abd->abd_size); 161eda14cbcSMatt Macy } 162eda14cbcSMatt Macy 163eda14cbcSMatt Macy /* 164eda14cbcSMatt Macy * Allocate an ABD, along with its own underlying data buffers. Use this if you 165eda14cbcSMatt Macy * don't care whether the ABD is linear or not. 166eda14cbcSMatt Macy */ 167eda14cbcSMatt Macy abd_t * 168eda14cbcSMatt Macy abd_alloc(size_t size, boolean_t is_metadata) 169eda14cbcSMatt Macy { 170eda14cbcSMatt Macy if (!zfs_abd_scatter_enabled || abd_size_alloc_linear(size)) 171eda14cbcSMatt Macy return (abd_alloc_linear(size, is_metadata)); 172eda14cbcSMatt Macy 173eda14cbcSMatt Macy VERIFY3U(size, <=, SPA_MAXBLOCKSIZE); 174eda14cbcSMatt Macy 175eda14cbcSMatt Macy abd_t *abd = abd_alloc_struct(size); 176eda14cbcSMatt Macy abd->abd_flags = ABD_FLAG_OWNER; 177eda14cbcSMatt Macy abd->abd_u.abd_scatter.abd_offset = 0; 178eda14cbcSMatt Macy abd_alloc_chunks(abd, size); 179eda14cbcSMatt Macy 180eda14cbcSMatt Macy if (is_metadata) { 181eda14cbcSMatt Macy abd->abd_flags |= ABD_FLAG_META; 182eda14cbcSMatt Macy } 183eda14cbcSMatt Macy abd->abd_size = size; 184eda14cbcSMatt Macy abd->abd_parent = NULL; 185eda14cbcSMatt Macy zfs_refcount_create(&abd->abd_children); 186eda14cbcSMatt Macy 187eda14cbcSMatt Macy abd_update_scatter_stats(abd, ABDSTAT_INCR); 188eda14cbcSMatt Macy 189eda14cbcSMatt Macy return (abd); 190eda14cbcSMatt Macy } 191eda14cbcSMatt Macy 192eda14cbcSMatt Macy static void 193eda14cbcSMatt Macy abd_free_scatter(abd_t *abd) 194eda14cbcSMatt Macy { 195eda14cbcSMatt Macy abd_free_chunks(abd); 196eda14cbcSMatt Macy 197eda14cbcSMatt Macy zfs_refcount_destroy(&abd->abd_children); 198eda14cbcSMatt Macy abd_update_scatter_stats(abd, ABDSTAT_DECR); 199eda14cbcSMatt Macy abd_free_struct(abd); 200eda14cbcSMatt Macy } 201eda14cbcSMatt Macy 202eda14cbcSMatt Macy static void 203eda14cbcSMatt Macy abd_put_gang_abd(abd_t *abd) 204eda14cbcSMatt Macy { 205eda14cbcSMatt Macy ASSERT(abd_is_gang(abd)); 206eda14cbcSMatt Macy abd_t *cabd; 207eda14cbcSMatt Macy 208eda14cbcSMatt Macy while ((cabd = list_remove_head(&ABD_GANG(abd).abd_gang_chain)) 209eda14cbcSMatt Macy != NULL) { 210eda14cbcSMatt Macy ASSERT0(cabd->abd_flags & ABD_FLAG_GANG_FREE); 211eda14cbcSMatt Macy abd->abd_size -= cabd->abd_size; 212eda14cbcSMatt Macy abd_put(cabd); 213eda14cbcSMatt Macy } 214eda14cbcSMatt Macy ASSERT0(abd->abd_size); 215eda14cbcSMatt Macy list_destroy(&ABD_GANG(abd).abd_gang_chain); 216eda14cbcSMatt Macy } 217eda14cbcSMatt Macy 218eda14cbcSMatt Macy /* 219eda14cbcSMatt Macy * Free an ABD allocated from abd_get_offset() or abd_get_from_buf(). Will not 220eda14cbcSMatt Macy * free the underlying scatterlist or buffer. 221eda14cbcSMatt Macy */ 222eda14cbcSMatt Macy void 223eda14cbcSMatt Macy abd_put(abd_t *abd) 224eda14cbcSMatt Macy { 225eda14cbcSMatt Macy if (abd == NULL) 226eda14cbcSMatt Macy return; 227eda14cbcSMatt Macy 228eda14cbcSMatt Macy abd_verify(abd); 229eda14cbcSMatt Macy ASSERT(!(abd->abd_flags & ABD_FLAG_OWNER)); 230eda14cbcSMatt Macy 231eda14cbcSMatt Macy if (abd->abd_parent != NULL) { 232eda14cbcSMatt Macy (void) zfs_refcount_remove_many(&abd->abd_parent->abd_children, 233eda14cbcSMatt Macy abd->abd_size, abd); 234eda14cbcSMatt Macy } 235eda14cbcSMatt Macy 236eda14cbcSMatt Macy if (abd_is_gang(abd)) 237eda14cbcSMatt Macy abd_put_gang_abd(abd); 238eda14cbcSMatt Macy 239eda14cbcSMatt Macy zfs_refcount_destroy(&abd->abd_children); 240eda14cbcSMatt Macy abd_free_struct(abd); 241eda14cbcSMatt Macy } 242eda14cbcSMatt Macy 243eda14cbcSMatt Macy /* 244eda14cbcSMatt Macy * Allocate an ABD that must be linear, along with its own underlying data 245eda14cbcSMatt Macy * buffer. Only use this when it would be very annoying to write your ABD 246eda14cbcSMatt Macy * consumer with a scattered ABD. 247eda14cbcSMatt Macy */ 248eda14cbcSMatt Macy abd_t * 249eda14cbcSMatt Macy abd_alloc_linear(size_t size, boolean_t is_metadata) 250eda14cbcSMatt Macy { 251eda14cbcSMatt Macy abd_t *abd = abd_alloc_struct(0); 252eda14cbcSMatt Macy 253eda14cbcSMatt Macy VERIFY3U(size, <=, SPA_MAXBLOCKSIZE); 254eda14cbcSMatt Macy 255eda14cbcSMatt Macy abd->abd_flags = ABD_FLAG_LINEAR | ABD_FLAG_OWNER; 256eda14cbcSMatt Macy if (is_metadata) { 257eda14cbcSMatt Macy abd->abd_flags |= ABD_FLAG_META; 258eda14cbcSMatt Macy } 259eda14cbcSMatt Macy abd->abd_size = size; 260eda14cbcSMatt Macy abd->abd_parent = NULL; 261eda14cbcSMatt Macy zfs_refcount_create(&abd->abd_children); 262eda14cbcSMatt Macy 263eda14cbcSMatt Macy if (is_metadata) { 264eda14cbcSMatt Macy ABD_LINEAR_BUF(abd) = zio_buf_alloc(size); 265eda14cbcSMatt Macy } else { 266eda14cbcSMatt Macy ABD_LINEAR_BUF(abd) = zio_data_buf_alloc(size); 267eda14cbcSMatt Macy } 268eda14cbcSMatt Macy 269eda14cbcSMatt Macy abd_update_linear_stats(abd, ABDSTAT_INCR); 270eda14cbcSMatt Macy 271eda14cbcSMatt Macy return (abd); 272eda14cbcSMatt Macy } 273eda14cbcSMatt Macy 274eda14cbcSMatt Macy static void 275eda14cbcSMatt Macy abd_free_linear(abd_t *abd) 276eda14cbcSMatt Macy { 277eda14cbcSMatt Macy if (abd_is_linear_page(abd)) { 278eda14cbcSMatt Macy abd_free_linear_page(abd); 279eda14cbcSMatt Macy return; 280eda14cbcSMatt Macy } 281eda14cbcSMatt Macy if (abd->abd_flags & ABD_FLAG_META) { 282eda14cbcSMatt Macy zio_buf_free(ABD_LINEAR_BUF(abd), abd->abd_size); 283eda14cbcSMatt Macy } else { 284eda14cbcSMatt Macy zio_data_buf_free(ABD_LINEAR_BUF(abd), abd->abd_size); 285eda14cbcSMatt Macy } 286eda14cbcSMatt Macy 287eda14cbcSMatt Macy zfs_refcount_destroy(&abd->abd_children); 288eda14cbcSMatt Macy abd_update_linear_stats(abd, ABDSTAT_DECR); 289eda14cbcSMatt Macy 290eda14cbcSMatt Macy abd_free_struct(abd); 291eda14cbcSMatt Macy } 292eda14cbcSMatt Macy 293eda14cbcSMatt Macy static void 294eda14cbcSMatt Macy abd_free_gang_abd(abd_t *abd) 295eda14cbcSMatt Macy { 296eda14cbcSMatt Macy ASSERT(abd_is_gang(abd)); 297eda14cbcSMatt Macy abd_t *cabd = list_head(&ABD_GANG(abd).abd_gang_chain); 298eda14cbcSMatt Macy 299eda14cbcSMatt Macy while (cabd != NULL) { 300eda14cbcSMatt Macy /* 301eda14cbcSMatt Macy * We must acquire the child ABDs mutex to ensure that if it 302eda14cbcSMatt Macy * is being added to another gang ABD we will set the link 303eda14cbcSMatt Macy * as inactive when removing it from this gang ABD and before 304eda14cbcSMatt Macy * adding it to the other gang ABD. 305eda14cbcSMatt Macy */ 306eda14cbcSMatt Macy mutex_enter(&cabd->abd_mtx); 307eda14cbcSMatt Macy ASSERT(list_link_active(&cabd->abd_gang_link)); 308eda14cbcSMatt Macy list_remove(&ABD_GANG(abd).abd_gang_chain, cabd); 309eda14cbcSMatt Macy mutex_exit(&cabd->abd_mtx); 310eda14cbcSMatt Macy abd->abd_size -= cabd->abd_size; 311eda14cbcSMatt Macy if (cabd->abd_flags & ABD_FLAG_GANG_FREE) { 312eda14cbcSMatt Macy if (cabd->abd_flags & ABD_FLAG_OWNER) 313eda14cbcSMatt Macy abd_free(cabd); 314eda14cbcSMatt Macy else 315eda14cbcSMatt Macy abd_put(cabd); 316eda14cbcSMatt Macy } 317eda14cbcSMatt Macy cabd = list_head(&ABD_GANG(abd).abd_gang_chain); 318eda14cbcSMatt Macy } 319eda14cbcSMatt Macy ASSERT0(abd->abd_size); 320eda14cbcSMatt Macy list_destroy(&ABD_GANG(abd).abd_gang_chain); 321eda14cbcSMatt Macy zfs_refcount_destroy(&abd->abd_children); 322eda14cbcSMatt Macy abd_free_struct(abd); 323eda14cbcSMatt Macy } 324eda14cbcSMatt Macy 325eda14cbcSMatt Macy /* 326eda14cbcSMatt Macy * Free an ABD. Only use this on ABDs allocated with abd_alloc(), 327eda14cbcSMatt Macy * abd_alloc_linear(), or abd_alloc_gang_abd(). 328eda14cbcSMatt Macy */ 329eda14cbcSMatt Macy void 330eda14cbcSMatt Macy abd_free(abd_t *abd) 331eda14cbcSMatt Macy { 332eda14cbcSMatt Macy if (abd == NULL) 333eda14cbcSMatt Macy return; 334eda14cbcSMatt Macy 335eda14cbcSMatt Macy abd_verify(abd); 336eda14cbcSMatt Macy ASSERT3P(abd->abd_parent, ==, NULL); 337eda14cbcSMatt Macy ASSERT(abd->abd_flags & ABD_FLAG_OWNER); 338eda14cbcSMatt Macy if (abd_is_linear(abd)) 339eda14cbcSMatt Macy abd_free_linear(abd); 340eda14cbcSMatt Macy else if (abd_is_gang(abd)) 341eda14cbcSMatt Macy abd_free_gang_abd(abd); 342eda14cbcSMatt Macy else 343eda14cbcSMatt Macy abd_free_scatter(abd); 344eda14cbcSMatt Macy } 345eda14cbcSMatt Macy 346eda14cbcSMatt Macy /* 347eda14cbcSMatt Macy * Allocate an ABD of the same format (same metadata flag, same scatterize 348eda14cbcSMatt Macy * setting) as another ABD. 349eda14cbcSMatt Macy */ 350eda14cbcSMatt Macy abd_t * 351eda14cbcSMatt Macy abd_alloc_sametype(abd_t *sabd, size_t size) 352eda14cbcSMatt Macy { 353eda14cbcSMatt Macy boolean_t is_metadata = (sabd->abd_flags & ABD_FLAG_META) != 0; 354eda14cbcSMatt Macy if (abd_is_linear(sabd) && 355eda14cbcSMatt Macy !abd_is_linear_page(sabd)) { 356eda14cbcSMatt Macy return (abd_alloc_linear(size, is_metadata)); 357eda14cbcSMatt Macy } else { 358eda14cbcSMatt Macy return (abd_alloc(size, is_metadata)); 359eda14cbcSMatt Macy } 360eda14cbcSMatt Macy } 361eda14cbcSMatt Macy 362eda14cbcSMatt Macy 363eda14cbcSMatt Macy /* 364eda14cbcSMatt Macy * Create gang ABD that will be the head of a list of ABD's. This is used 365eda14cbcSMatt Macy * to "chain" scatter/gather lists together when constructing aggregated 366eda14cbcSMatt Macy * IO's. To free this abd, abd_free() must be called. 367eda14cbcSMatt Macy */ 368eda14cbcSMatt Macy abd_t * 369eda14cbcSMatt Macy abd_alloc_gang_abd(void) 370eda14cbcSMatt Macy { 371eda14cbcSMatt Macy abd_t *abd; 372eda14cbcSMatt Macy 373eda14cbcSMatt Macy abd = abd_alloc_struct(0); 374eda14cbcSMatt Macy abd->abd_flags = ABD_FLAG_GANG | ABD_FLAG_OWNER; 375eda14cbcSMatt Macy abd->abd_size = 0; 376eda14cbcSMatt Macy abd->abd_parent = NULL; 377eda14cbcSMatt Macy list_create(&ABD_GANG(abd).abd_gang_chain, 378eda14cbcSMatt Macy sizeof (abd_t), offsetof(abd_t, abd_gang_link)); 379eda14cbcSMatt Macy zfs_refcount_create(&abd->abd_children); 380eda14cbcSMatt Macy return (abd); 381eda14cbcSMatt Macy } 382eda14cbcSMatt Macy 383eda14cbcSMatt Macy /* 384eda14cbcSMatt Macy * Add a child gang ABD to a parent gang ABDs chained list. 385eda14cbcSMatt Macy */ 386eda14cbcSMatt Macy static void 387eda14cbcSMatt Macy abd_gang_add_gang(abd_t *pabd, abd_t *cabd, boolean_t free_on_free) 388eda14cbcSMatt Macy { 389eda14cbcSMatt Macy ASSERT(abd_is_gang(pabd)); 390eda14cbcSMatt Macy ASSERT(abd_is_gang(cabd)); 391eda14cbcSMatt Macy 392eda14cbcSMatt Macy if (free_on_free) { 393eda14cbcSMatt Macy /* 394eda14cbcSMatt Macy * If the parent is responsible for freeing the child gang 395eda14cbcSMatt Macy * ABD we will just splice the childs children ABD list to 396eda14cbcSMatt Macy * the parents list and immediately free the child gang ABD 397eda14cbcSMatt Macy * struct. The parent gang ABDs children from the child gang 398eda14cbcSMatt Macy * will retain all the free_on_free settings after being 399eda14cbcSMatt Macy * added to the parents list. 400eda14cbcSMatt Macy */ 401eda14cbcSMatt Macy pabd->abd_size += cabd->abd_size; 402eda14cbcSMatt Macy list_move_tail(&ABD_GANG(pabd).abd_gang_chain, 403eda14cbcSMatt Macy &ABD_GANG(cabd).abd_gang_chain); 404eda14cbcSMatt Macy ASSERT(list_is_empty(&ABD_GANG(cabd).abd_gang_chain)); 405eda14cbcSMatt Macy abd_verify(pabd); 406eda14cbcSMatt Macy abd_free_struct(cabd); 407eda14cbcSMatt Macy } else { 408eda14cbcSMatt Macy for (abd_t *child = list_head(&ABD_GANG(cabd).abd_gang_chain); 409eda14cbcSMatt Macy child != NULL; 410eda14cbcSMatt Macy child = list_next(&ABD_GANG(cabd).abd_gang_chain, child)) { 411eda14cbcSMatt Macy /* 412eda14cbcSMatt Macy * We always pass B_FALSE for free_on_free as it is the 413eda14cbcSMatt Macy * original child gang ABDs responsibilty to determine 414eda14cbcSMatt Macy * if any of its child ABDs should be free'd on the call 415eda14cbcSMatt Macy * to abd_free(). 416eda14cbcSMatt Macy */ 417eda14cbcSMatt Macy abd_gang_add(pabd, child, B_FALSE); 418eda14cbcSMatt Macy } 419eda14cbcSMatt Macy abd_verify(pabd); 420eda14cbcSMatt Macy } 421eda14cbcSMatt Macy } 422eda14cbcSMatt Macy 423eda14cbcSMatt Macy /* 424eda14cbcSMatt Macy * Add a child ABD to a gang ABD's chained list. 425eda14cbcSMatt Macy */ 426eda14cbcSMatt Macy void 427eda14cbcSMatt Macy abd_gang_add(abd_t *pabd, abd_t *cabd, boolean_t free_on_free) 428eda14cbcSMatt Macy { 429eda14cbcSMatt Macy ASSERT(abd_is_gang(pabd)); 430eda14cbcSMatt Macy abd_t *child_abd = NULL; 431eda14cbcSMatt Macy 432eda14cbcSMatt Macy /* 433eda14cbcSMatt Macy * If the child being added is a gang ABD, we will add the 434eda14cbcSMatt Macy * childs ABDs to the parent gang ABD. This alllows us to account 435eda14cbcSMatt Macy * for the offset correctly in the parent gang ABD. 436eda14cbcSMatt Macy */ 437eda14cbcSMatt Macy if (abd_is_gang(cabd)) { 438eda14cbcSMatt Macy ASSERT(!list_link_active(&cabd->abd_gang_link)); 439eda14cbcSMatt Macy ASSERT(!list_is_empty(&ABD_GANG(cabd).abd_gang_chain)); 440eda14cbcSMatt Macy return (abd_gang_add_gang(pabd, cabd, free_on_free)); 441eda14cbcSMatt Macy } 442eda14cbcSMatt Macy ASSERT(!abd_is_gang(cabd)); 443eda14cbcSMatt Macy 444eda14cbcSMatt Macy /* 445eda14cbcSMatt Macy * In order to verify that an ABD is not already part of 446eda14cbcSMatt Macy * another gang ABD, we must lock the child ABD's abd_mtx 447eda14cbcSMatt Macy * to check its abd_gang_link status. We unlock the abd_mtx 448eda14cbcSMatt Macy * only after it is has been added to a gang ABD, which 449eda14cbcSMatt Macy * will update the abd_gang_link's status. See comment below 450eda14cbcSMatt Macy * for how an ABD can be in multiple gang ABD's simultaneously. 451eda14cbcSMatt Macy */ 452eda14cbcSMatt Macy mutex_enter(&cabd->abd_mtx); 453eda14cbcSMatt Macy if (list_link_active(&cabd->abd_gang_link)) { 454eda14cbcSMatt Macy /* 455eda14cbcSMatt Macy * If the child ABD is already part of another 456eda14cbcSMatt Macy * gang ABD then we must allocate a new 457eda14cbcSMatt Macy * ABD to use a separate link. We mark the newly 458eda14cbcSMatt Macy * allocated ABD with ABD_FLAG_GANG_FREE, before 459eda14cbcSMatt Macy * adding it to the gang ABD's list, to make the 460eda14cbcSMatt Macy * gang ABD aware that it is responsible to call 461eda14cbcSMatt Macy * abd_put(). We use abd_get_offset() in order 462eda14cbcSMatt Macy * to just allocate a new ABD but avoid copying the 463eda14cbcSMatt Macy * data over into the newly allocated ABD. 464eda14cbcSMatt Macy * 465eda14cbcSMatt Macy * An ABD may become part of multiple gang ABD's. For 466eda14cbcSMatt Macy * example, when writing ditto bocks, the same ABD 467eda14cbcSMatt Macy * is used to write 2 or 3 locations with 2 or 3 468eda14cbcSMatt Macy * zio_t's. Each of the zio's may be aggregated with 469eda14cbcSMatt Macy * different adjacent zio's. zio aggregation uses gang 470eda14cbcSMatt Macy * zio's, so the single ABD can become part of multiple 471eda14cbcSMatt Macy * gang zio's. 472eda14cbcSMatt Macy * 473eda14cbcSMatt Macy * The ASSERT below is to make sure that if 474eda14cbcSMatt Macy * free_on_free is passed as B_TRUE, the ABD can 475eda14cbcSMatt Macy * not be in multiple gang ABD's. The gang ABD 476eda14cbcSMatt Macy * can not be responsible for cleaning up the child 477eda14cbcSMatt Macy * ABD memory allocation if the ABD can be in 478eda14cbcSMatt Macy * multiple gang ABD's at one time. 479eda14cbcSMatt Macy */ 480eda14cbcSMatt Macy ASSERT3B(free_on_free, ==, B_FALSE); 481eda14cbcSMatt Macy child_abd = abd_get_offset(cabd, 0); 482eda14cbcSMatt Macy child_abd->abd_flags |= ABD_FLAG_GANG_FREE; 483eda14cbcSMatt Macy } else { 484eda14cbcSMatt Macy child_abd = cabd; 485eda14cbcSMatt Macy if (free_on_free) 486eda14cbcSMatt Macy child_abd->abd_flags |= ABD_FLAG_GANG_FREE; 487eda14cbcSMatt Macy } 488eda14cbcSMatt Macy ASSERT3P(child_abd, !=, NULL); 489eda14cbcSMatt Macy 490eda14cbcSMatt Macy list_insert_tail(&ABD_GANG(pabd).abd_gang_chain, child_abd); 491eda14cbcSMatt Macy mutex_exit(&cabd->abd_mtx); 492eda14cbcSMatt Macy pabd->abd_size += child_abd->abd_size; 493eda14cbcSMatt Macy } 494eda14cbcSMatt Macy 495eda14cbcSMatt Macy /* 496eda14cbcSMatt Macy * Locate the ABD for the supplied offset in the gang ABD. 497eda14cbcSMatt Macy * Return a new offset relative to the returned ABD. 498eda14cbcSMatt Macy */ 499eda14cbcSMatt Macy abd_t * 500eda14cbcSMatt Macy abd_gang_get_offset(abd_t *abd, size_t *off) 501eda14cbcSMatt Macy { 502eda14cbcSMatt Macy abd_t *cabd; 503eda14cbcSMatt Macy 504eda14cbcSMatt Macy ASSERT(abd_is_gang(abd)); 505eda14cbcSMatt Macy ASSERT3U(*off, <, abd->abd_size); 506eda14cbcSMatt Macy for (cabd = list_head(&ABD_GANG(abd).abd_gang_chain); cabd != NULL; 507eda14cbcSMatt Macy cabd = list_next(&ABD_GANG(abd).abd_gang_chain, cabd)) { 508eda14cbcSMatt Macy if (*off >= cabd->abd_size) 509eda14cbcSMatt Macy *off -= cabd->abd_size; 510eda14cbcSMatt Macy else 511eda14cbcSMatt Macy return (cabd); 512eda14cbcSMatt Macy } 513eda14cbcSMatt Macy VERIFY3P(cabd, !=, NULL); 514eda14cbcSMatt Macy return (cabd); 515eda14cbcSMatt Macy } 516eda14cbcSMatt Macy 517eda14cbcSMatt Macy /* 518eda14cbcSMatt Macy * Allocate a new ABD to point to offset off of sabd. It shares the underlying 519eda14cbcSMatt Macy * buffer data with sabd. Use abd_put() to free. sabd must not be freed while 520eda14cbcSMatt Macy * any derived ABDs exist. 521eda14cbcSMatt Macy */ 522eda14cbcSMatt Macy static abd_t * 523eda14cbcSMatt Macy abd_get_offset_impl(abd_t *sabd, size_t off, size_t size) 524eda14cbcSMatt Macy { 525eda14cbcSMatt Macy abd_t *abd = NULL; 526eda14cbcSMatt Macy 527eda14cbcSMatt Macy abd_verify(sabd); 528eda14cbcSMatt Macy ASSERT3U(off, <=, sabd->abd_size); 529eda14cbcSMatt Macy 530eda14cbcSMatt Macy if (abd_is_linear(sabd)) { 531eda14cbcSMatt Macy abd = abd_alloc_struct(0); 532eda14cbcSMatt Macy 533eda14cbcSMatt Macy /* 534eda14cbcSMatt Macy * Even if this buf is filesystem metadata, we only track that 535eda14cbcSMatt Macy * if we own the underlying data buffer, which is not true in 536eda14cbcSMatt Macy * this case. Therefore, we don't ever use ABD_FLAG_META here. 537eda14cbcSMatt Macy */ 538eda14cbcSMatt Macy abd->abd_flags = ABD_FLAG_LINEAR; 539eda14cbcSMatt Macy 540eda14cbcSMatt Macy ABD_LINEAR_BUF(abd) = (char *)ABD_LINEAR_BUF(sabd) + off; 541eda14cbcSMatt Macy } else if (abd_is_gang(sabd)) { 542eda14cbcSMatt Macy size_t left = size; 543eda14cbcSMatt Macy abd = abd_alloc_gang_abd(); 544eda14cbcSMatt Macy abd->abd_flags &= ~ABD_FLAG_OWNER; 545eda14cbcSMatt Macy for (abd_t *cabd = abd_gang_get_offset(sabd, &off); 546eda14cbcSMatt Macy cabd != NULL && left > 0; 547eda14cbcSMatt Macy cabd = list_next(&ABD_GANG(sabd).abd_gang_chain, cabd)) { 548eda14cbcSMatt Macy int csize = MIN(left, cabd->abd_size - off); 549eda14cbcSMatt Macy 550eda14cbcSMatt Macy abd_t *nabd = abd_get_offset_impl(cabd, off, csize); 551eda14cbcSMatt Macy abd_gang_add(abd, nabd, B_FALSE); 552eda14cbcSMatt Macy left -= csize; 553eda14cbcSMatt Macy off = 0; 554eda14cbcSMatt Macy } 555eda14cbcSMatt Macy ASSERT3U(left, ==, 0); 556eda14cbcSMatt Macy } else { 557eda14cbcSMatt Macy abd = abd_get_offset_scatter(sabd, off); 558eda14cbcSMatt Macy } 559eda14cbcSMatt Macy 560eda14cbcSMatt Macy abd->abd_size = size; 561eda14cbcSMatt Macy abd->abd_parent = sabd; 562eda14cbcSMatt Macy zfs_refcount_create(&abd->abd_children); 563eda14cbcSMatt Macy (void) zfs_refcount_add_many(&sabd->abd_children, abd->abd_size, abd); 564eda14cbcSMatt Macy return (abd); 565eda14cbcSMatt Macy } 566eda14cbcSMatt Macy 567eda14cbcSMatt Macy abd_t * 568eda14cbcSMatt Macy abd_get_offset(abd_t *sabd, size_t off) 569eda14cbcSMatt Macy { 570eda14cbcSMatt Macy size_t size = sabd->abd_size > off ? sabd->abd_size - off : 0; 571eda14cbcSMatt Macy VERIFY3U(size, >, 0); 572eda14cbcSMatt Macy return (abd_get_offset_impl(sabd, off, size)); 573eda14cbcSMatt Macy } 574eda14cbcSMatt Macy 575eda14cbcSMatt Macy abd_t * 576eda14cbcSMatt Macy abd_get_offset_size(abd_t *sabd, size_t off, size_t size) 577eda14cbcSMatt Macy { 578eda14cbcSMatt Macy ASSERT3U(off + size, <=, sabd->abd_size); 579eda14cbcSMatt Macy return (abd_get_offset_impl(sabd, off, size)); 580eda14cbcSMatt Macy } 581eda14cbcSMatt Macy 582eda14cbcSMatt Macy /* 583eda14cbcSMatt Macy * Return a size scatter ABD. In order to free the returned 584eda14cbcSMatt Macy * ABD abd_put() must be called. 585eda14cbcSMatt Macy */ 586eda14cbcSMatt Macy abd_t * 587eda14cbcSMatt Macy abd_get_zeros(size_t size) 588eda14cbcSMatt Macy { 589eda14cbcSMatt Macy ASSERT3P(abd_zero_scatter, !=, NULL); 590eda14cbcSMatt Macy ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 591eda14cbcSMatt Macy return (abd_get_offset_size(abd_zero_scatter, 0, size)); 592eda14cbcSMatt Macy } 593eda14cbcSMatt Macy 594eda14cbcSMatt Macy /* 595eda14cbcSMatt Macy * Allocate a linear ABD structure for buf. You must free this with abd_put() 596eda14cbcSMatt Macy * since the resulting ABD doesn't own its own buffer. 597eda14cbcSMatt Macy */ 598eda14cbcSMatt Macy abd_t * 599eda14cbcSMatt Macy abd_get_from_buf(void *buf, size_t size) 600eda14cbcSMatt Macy { 601eda14cbcSMatt Macy abd_t *abd = abd_alloc_struct(0); 602eda14cbcSMatt Macy 603eda14cbcSMatt Macy VERIFY3U(size, <=, SPA_MAXBLOCKSIZE); 604eda14cbcSMatt Macy 605eda14cbcSMatt Macy /* 606eda14cbcSMatt Macy * Even if this buf is filesystem metadata, we only track that if we 607eda14cbcSMatt Macy * own the underlying data buffer, which is not true in this case. 608eda14cbcSMatt Macy * Therefore, we don't ever use ABD_FLAG_META here. 609eda14cbcSMatt Macy */ 610eda14cbcSMatt Macy abd->abd_flags = ABD_FLAG_LINEAR; 611eda14cbcSMatt Macy abd->abd_size = size; 612eda14cbcSMatt Macy abd->abd_parent = NULL; 613eda14cbcSMatt Macy zfs_refcount_create(&abd->abd_children); 614eda14cbcSMatt Macy 615eda14cbcSMatt Macy ABD_LINEAR_BUF(abd) = buf; 616eda14cbcSMatt Macy 617eda14cbcSMatt Macy return (abd); 618eda14cbcSMatt Macy } 619eda14cbcSMatt Macy 620eda14cbcSMatt Macy /* 621eda14cbcSMatt Macy * Get the raw buffer associated with a linear ABD. 622eda14cbcSMatt Macy */ 623eda14cbcSMatt Macy void * 624eda14cbcSMatt Macy abd_to_buf(abd_t *abd) 625eda14cbcSMatt Macy { 626eda14cbcSMatt Macy ASSERT(abd_is_linear(abd)); 627eda14cbcSMatt Macy abd_verify(abd); 628eda14cbcSMatt Macy return (ABD_LINEAR_BUF(abd)); 629eda14cbcSMatt Macy } 630eda14cbcSMatt Macy 631eda14cbcSMatt Macy /* 632eda14cbcSMatt Macy * Borrow a raw buffer from an ABD without copying the contents of the ABD 633eda14cbcSMatt Macy * into the buffer. If the ABD is scattered, this will allocate a raw buffer 634eda14cbcSMatt Macy * whose contents are undefined. To copy over the existing data in the ABD, use 635eda14cbcSMatt Macy * abd_borrow_buf_copy() instead. 636eda14cbcSMatt Macy */ 637eda14cbcSMatt Macy void * 638eda14cbcSMatt Macy abd_borrow_buf(abd_t *abd, size_t n) 639eda14cbcSMatt Macy { 640eda14cbcSMatt Macy void *buf; 641eda14cbcSMatt Macy abd_verify(abd); 642eda14cbcSMatt Macy ASSERT3U(abd->abd_size, >=, n); 643eda14cbcSMatt Macy if (abd_is_linear(abd)) { 644eda14cbcSMatt Macy buf = abd_to_buf(abd); 645eda14cbcSMatt Macy } else { 646eda14cbcSMatt Macy buf = zio_buf_alloc(n); 647eda14cbcSMatt Macy } 648eda14cbcSMatt Macy (void) zfs_refcount_add_many(&abd->abd_children, n, buf); 649eda14cbcSMatt Macy return (buf); 650eda14cbcSMatt Macy } 651eda14cbcSMatt Macy 652eda14cbcSMatt Macy void * 653eda14cbcSMatt Macy abd_borrow_buf_copy(abd_t *abd, size_t n) 654eda14cbcSMatt Macy { 655eda14cbcSMatt Macy void *buf = abd_borrow_buf(abd, n); 656eda14cbcSMatt Macy if (!abd_is_linear(abd)) { 657eda14cbcSMatt Macy abd_copy_to_buf(buf, abd, n); 658eda14cbcSMatt Macy } 659eda14cbcSMatt Macy return (buf); 660eda14cbcSMatt Macy } 661eda14cbcSMatt Macy 662eda14cbcSMatt Macy /* 663eda14cbcSMatt Macy * Return a borrowed raw buffer to an ABD. If the ABD is scattered, this will 664eda14cbcSMatt Macy * not change the contents of the ABD and will ASSERT that you didn't modify 665eda14cbcSMatt Macy * the buffer since it was borrowed. If you want any changes you made to buf to 666eda14cbcSMatt Macy * be copied back to abd, use abd_return_buf_copy() instead. 667eda14cbcSMatt Macy */ 668eda14cbcSMatt Macy void 669eda14cbcSMatt Macy abd_return_buf(abd_t *abd, void *buf, size_t n) 670eda14cbcSMatt Macy { 671eda14cbcSMatt Macy abd_verify(abd); 672eda14cbcSMatt Macy ASSERT3U(abd->abd_size, >=, n); 673eda14cbcSMatt Macy if (abd_is_linear(abd)) { 674eda14cbcSMatt Macy ASSERT3P(buf, ==, abd_to_buf(abd)); 675eda14cbcSMatt Macy } else { 676eda14cbcSMatt Macy ASSERT0(abd_cmp_buf(abd, buf, n)); 677eda14cbcSMatt Macy zio_buf_free(buf, n); 678eda14cbcSMatt Macy } 679eda14cbcSMatt Macy (void) zfs_refcount_remove_many(&abd->abd_children, n, buf); 680eda14cbcSMatt Macy } 681eda14cbcSMatt Macy 682eda14cbcSMatt Macy void 683eda14cbcSMatt Macy abd_return_buf_copy(abd_t *abd, void *buf, size_t n) 684eda14cbcSMatt Macy { 685eda14cbcSMatt Macy if (!abd_is_linear(abd)) { 686eda14cbcSMatt Macy abd_copy_from_buf(abd, buf, n); 687eda14cbcSMatt Macy } 688eda14cbcSMatt Macy abd_return_buf(abd, buf, n); 689eda14cbcSMatt Macy } 690eda14cbcSMatt Macy 691eda14cbcSMatt Macy void 692eda14cbcSMatt Macy abd_release_ownership_of_buf(abd_t *abd) 693eda14cbcSMatt Macy { 694eda14cbcSMatt Macy ASSERT(abd_is_linear(abd)); 695eda14cbcSMatt Macy ASSERT(abd->abd_flags & ABD_FLAG_OWNER); 696eda14cbcSMatt Macy 697eda14cbcSMatt Macy /* 698eda14cbcSMatt Macy * abd_free() needs to handle LINEAR_PAGE ABD's specially. 699eda14cbcSMatt Macy * Since that flag does not survive the 700eda14cbcSMatt Macy * abd_release_ownership_of_buf() -> abd_get_from_buf() -> 701eda14cbcSMatt Macy * abd_take_ownership_of_buf() sequence, we don't allow releasing 702eda14cbcSMatt Macy * these "linear but not zio_[data_]buf_alloc()'ed" ABD's. 703eda14cbcSMatt Macy */ 704eda14cbcSMatt Macy ASSERT(!abd_is_linear_page(abd)); 705eda14cbcSMatt Macy 706eda14cbcSMatt Macy abd_verify(abd); 707eda14cbcSMatt Macy 708eda14cbcSMatt Macy abd->abd_flags &= ~ABD_FLAG_OWNER; 709eda14cbcSMatt Macy /* Disable this flag since we no longer own the data buffer */ 710eda14cbcSMatt Macy abd->abd_flags &= ~ABD_FLAG_META; 711eda14cbcSMatt Macy 712eda14cbcSMatt Macy abd_update_linear_stats(abd, ABDSTAT_DECR); 713eda14cbcSMatt Macy } 714eda14cbcSMatt Macy 715eda14cbcSMatt Macy 716eda14cbcSMatt Macy /* 717eda14cbcSMatt Macy * Give this ABD ownership of the buffer that it's storing. Can only be used on 718eda14cbcSMatt Macy * linear ABDs which were allocated via abd_get_from_buf(), or ones allocated 719eda14cbcSMatt Macy * with abd_alloc_linear() which subsequently released ownership of their buf 720eda14cbcSMatt Macy * with abd_release_ownership_of_buf(). 721eda14cbcSMatt Macy */ 722eda14cbcSMatt Macy void 723eda14cbcSMatt Macy abd_take_ownership_of_buf(abd_t *abd, boolean_t is_metadata) 724eda14cbcSMatt Macy { 725eda14cbcSMatt Macy ASSERT(abd_is_linear(abd)); 726eda14cbcSMatt Macy ASSERT(!(abd->abd_flags & ABD_FLAG_OWNER)); 727eda14cbcSMatt Macy abd_verify(abd); 728eda14cbcSMatt Macy 729eda14cbcSMatt Macy abd->abd_flags |= ABD_FLAG_OWNER; 730eda14cbcSMatt Macy if (is_metadata) { 731eda14cbcSMatt Macy abd->abd_flags |= ABD_FLAG_META; 732eda14cbcSMatt Macy } 733eda14cbcSMatt Macy 734eda14cbcSMatt Macy abd_update_linear_stats(abd, ABDSTAT_INCR); 735eda14cbcSMatt Macy } 736eda14cbcSMatt Macy 737eda14cbcSMatt Macy /* 738eda14cbcSMatt Macy * Initializes an abd_iter based on whether the abd is a gang ABD 739eda14cbcSMatt Macy * or just a single ABD. 740eda14cbcSMatt Macy */ 741eda14cbcSMatt Macy static inline abd_t * 742eda14cbcSMatt Macy abd_init_abd_iter(abd_t *abd, struct abd_iter *aiter, size_t off) 743eda14cbcSMatt Macy { 744eda14cbcSMatt Macy abd_t *cabd = NULL; 745eda14cbcSMatt Macy 746eda14cbcSMatt Macy if (abd_is_gang(abd)) { 747eda14cbcSMatt Macy cabd = abd_gang_get_offset(abd, &off); 748eda14cbcSMatt Macy if (cabd) { 749eda14cbcSMatt Macy abd_iter_init(aiter, cabd); 750eda14cbcSMatt Macy abd_iter_advance(aiter, off); 751eda14cbcSMatt Macy } 752eda14cbcSMatt Macy } else { 753eda14cbcSMatt Macy abd_iter_init(aiter, abd); 754eda14cbcSMatt Macy abd_iter_advance(aiter, off); 755eda14cbcSMatt Macy } 756eda14cbcSMatt Macy return (cabd); 757eda14cbcSMatt Macy } 758eda14cbcSMatt Macy 759eda14cbcSMatt Macy /* 760eda14cbcSMatt Macy * Advances an abd_iter. We have to be careful with gang ABD as 761eda14cbcSMatt Macy * advancing could mean that we are at the end of a particular ABD and 762eda14cbcSMatt Macy * must grab the ABD in the gang ABD's list. 763eda14cbcSMatt Macy */ 764eda14cbcSMatt Macy static inline abd_t * 765eda14cbcSMatt Macy abd_advance_abd_iter(abd_t *abd, abd_t *cabd, struct abd_iter *aiter, 766eda14cbcSMatt Macy size_t len) 767eda14cbcSMatt Macy { 768eda14cbcSMatt Macy abd_iter_advance(aiter, len); 769eda14cbcSMatt Macy if (abd_is_gang(abd) && abd_iter_at_end(aiter)) { 770eda14cbcSMatt Macy ASSERT3P(cabd, !=, NULL); 771eda14cbcSMatt Macy cabd = list_next(&ABD_GANG(abd).abd_gang_chain, cabd); 772eda14cbcSMatt Macy if (cabd) { 773eda14cbcSMatt Macy abd_iter_init(aiter, cabd); 774eda14cbcSMatt Macy abd_iter_advance(aiter, 0); 775eda14cbcSMatt Macy } 776eda14cbcSMatt Macy } 777eda14cbcSMatt Macy return (cabd); 778eda14cbcSMatt Macy } 779eda14cbcSMatt Macy 780eda14cbcSMatt Macy int 781eda14cbcSMatt Macy abd_iterate_func(abd_t *abd, size_t off, size_t size, 782eda14cbcSMatt Macy abd_iter_func_t *func, void *private) 783eda14cbcSMatt Macy { 784eda14cbcSMatt Macy struct abd_iter aiter; 785*7877fdebSMatt Macy int ret = 0; 786*7877fdebSMatt Macy 787*7877fdebSMatt Macy if (size == 0) 788*7877fdebSMatt Macy return (0); 789eda14cbcSMatt Macy 790eda14cbcSMatt Macy abd_verify(abd); 791eda14cbcSMatt Macy ASSERT3U(off + size, <=, abd->abd_size); 792eda14cbcSMatt Macy 793*7877fdebSMatt Macy boolean_t abd_multi = abd_is_gang(abd); 794*7877fdebSMatt Macy abd_t *c_abd = abd_init_abd_iter(abd, &aiter, off); 795eda14cbcSMatt Macy 796eda14cbcSMatt Macy while (size > 0) { 797eda14cbcSMatt Macy /* If we are at the end of the gang ABD we are done */ 798eda14cbcSMatt Macy if (abd_multi && !c_abd) 799eda14cbcSMatt Macy break; 800eda14cbcSMatt Macy 801eda14cbcSMatt Macy abd_iter_map(&aiter); 802eda14cbcSMatt Macy 803eda14cbcSMatt Macy size_t len = MIN(aiter.iter_mapsize, size); 804eda14cbcSMatt Macy ASSERT3U(len, >, 0); 805eda14cbcSMatt Macy 806eda14cbcSMatt Macy ret = func(aiter.iter_mapaddr, len, private); 807eda14cbcSMatt Macy 808eda14cbcSMatt Macy abd_iter_unmap(&aiter); 809eda14cbcSMatt Macy 810eda14cbcSMatt Macy if (ret != 0) 811eda14cbcSMatt Macy break; 812eda14cbcSMatt Macy 813eda14cbcSMatt Macy size -= len; 814eda14cbcSMatt Macy c_abd = abd_advance_abd_iter(abd, c_abd, &aiter, len); 815eda14cbcSMatt Macy } 816eda14cbcSMatt Macy 817eda14cbcSMatt Macy return (ret); 818eda14cbcSMatt Macy } 819eda14cbcSMatt Macy 820eda14cbcSMatt Macy struct buf_arg { 821eda14cbcSMatt Macy void *arg_buf; 822eda14cbcSMatt Macy }; 823eda14cbcSMatt Macy 824eda14cbcSMatt Macy static int 825eda14cbcSMatt Macy abd_copy_to_buf_off_cb(void *buf, size_t size, void *private) 826eda14cbcSMatt Macy { 827eda14cbcSMatt Macy struct buf_arg *ba_ptr = private; 828eda14cbcSMatt Macy 829eda14cbcSMatt Macy (void) memcpy(ba_ptr->arg_buf, buf, size); 830eda14cbcSMatt Macy ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size; 831eda14cbcSMatt Macy 832eda14cbcSMatt Macy return (0); 833eda14cbcSMatt Macy } 834eda14cbcSMatt Macy 835eda14cbcSMatt Macy /* 836eda14cbcSMatt Macy * Copy abd to buf. (off is the offset in abd.) 837eda14cbcSMatt Macy */ 838eda14cbcSMatt Macy void 839eda14cbcSMatt Macy abd_copy_to_buf_off(void *buf, abd_t *abd, size_t off, size_t size) 840eda14cbcSMatt Macy { 841eda14cbcSMatt Macy struct buf_arg ba_ptr = { buf }; 842eda14cbcSMatt Macy 843eda14cbcSMatt Macy (void) abd_iterate_func(abd, off, size, abd_copy_to_buf_off_cb, 844eda14cbcSMatt Macy &ba_ptr); 845eda14cbcSMatt Macy } 846eda14cbcSMatt Macy 847eda14cbcSMatt Macy static int 848eda14cbcSMatt Macy abd_cmp_buf_off_cb(void *buf, size_t size, void *private) 849eda14cbcSMatt Macy { 850eda14cbcSMatt Macy int ret; 851eda14cbcSMatt Macy struct buf_arg *ba_ptr = private; 852eda14cbcSMatt Macy 853eda14cbcSMatt Macy ret = memcmp(buf, ba_ptr->arg_buf, size); 854eda14cbcSMatt Macy ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size; 855eda14cbcSMatt Macy 856eda14cbcSMatt Macy return (ret); 857eda14cbcSMatt Macy } 858eda14cbcSMatt Macy 859eda14cbcSMatt Macy /* 860eda14cbcSMatt Macy * Compare the contents of abd to buf. (off is the offset in abd.) 861eda14cbcSMatt Macy */ 862eda14cbcSMatt Macy int 863eda14cbcSMatt Macy abd_cmp_buf_off(abd_t *abd, const void *buf, size_t off, size_t size) 864eda14cbcSMatt Macy { 865eda14cbcSMatt Macy struct buf_arg ba_ptr = { (void *) buf }; 866eda14cbcSMatt Macy 867eda14cbcSMatt Macy return (abd_iterate_func(abd, off, size, abd_cmp_buf_off_cb, &ba_ptr)); 868eda14cbcSMatt Macy } 869eda14cbcSMatt Macy 870eda14cbcSMatt Macy static int 871eda14cbcSMatt Macy abd_copy_from_buf_off_cb(void *buf, size_t size, void *private) 872eda14cbcSMatt Macy { 873eda14cbcSMatt Macy struct buf_arg *ba_ptr = private; 874eda14cbcSMatt Macy 875eda14cbcSMatt Macy (void) memcpy(buf, ba_ptr->arg_buf, size); 876eda14cbcSMatt Macy ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size; 877eda14cbcSMatt Macy 878eda14cbcSMatt Macy return (0); 879eda14cbcSMatt Macy } 880eda14cbcSMatt Macy 881eda14cbcSMatt Macy /* 882eda14cbcSMatt Macy * Copy from buf to abd. (off is the offset in abd.) 883eda14cbcSMatt Macy */ 884eda14cbcSMatt Macy void 885eda14cbcSMatt Macy abd_copy_from_buf_off(abd_t *abd, const void *buf, size_t off, size_t size) 886eda14cbcSMatt Macy { 887eda14cbcSMatt Macy struct buf_arg ba_ptr = { (void *) buf }; 888eda14cbcSMatt Macy 889eda14cbcSMatt Macy (void) abd_iterate_func(abd, off, size, abd_copy_from_buf_off_cb, 890eda14cbcSMatt Macy &ba_ptr); 891eda14cbcSMatt Macy } 892eda14cbcSMatt Macy 893eda14cbcSMatt Macy /*ARGSUSED*/ 894eda14cbcSMatt Macy static int 895eda14cbcSMatt Macy abd_zero_off_cb(void *buf, size_t size, void *private) 896eda14cbcSMatt Macy { 897eda14cbcSMatt Macy (void) memset(buf, 0, size); 898eda14cbcSMatt Macy return (0); 899eda14cbcSMatt Macy } 900eda14cbcSMatt Macy 901eda14cbcSMatt Macy /* 902eda14cbcSMatt Macy * Zero out the abd from a particular offset to the end. 903eda14cbcSMatt Macy */ 904eda14cbcSMatt Macy void 905eda14cbcSMatt Macy abd_zero_off(abd_t *abd, size_t off, size_t size) 906eda14cbcSMatt Macy { 907eda14cbcSMatt Macy (void) abd_iterate_func(abd, off, size, abd_zero_off_cb, NULL); 908eda14cbcSMatt Macy } 909eda14cbcSMatt Macy 910eda14cbcSMatt Macy /* 911eda14cbcSMatt Macy * Iterate over two ABDs and call func incrementally on the two ABDs' data in 912eda14cbcSMatt Macy * equal-sized chunks (passed to func as raw buffers). func could be called many 913eda14cbcSMatt Macy * times during this iteration. 914eda14cbcSMatt Macy */ 915eda14cbcSMatt Macy int 916eda14cbcSMatt Macy abd_iterate_func2(abd_t *dabd, abd_t *sabd, size_t doff, size_t soff, 917eda14cbcSMatt Macy size_t size, abd_iter_func2_t *func, void *private) 918eda14cbcSMatt Macy { 919eda14cbcSMatt Macy int ret = 0; 920eda14cbcSMatt Macy struct abd_iter daiter, saiter; 921eda14cbcSMatt Macy boolean_t dabd_is_gang_abd, sabd_is_gang_abd; 922eda14cbcSMatt Macy abd_t *c_dabd, *c_sabd; 923eda14cbcSMatt Macy 924*7877fdebSMatt Macy if (size == 0) 925*7877fdebSMatt Macy return (0); 926*7877fdebSMatt Macy 927eda14cbcSMatt Macy abd_verify(dabd); 928eda14cbcSMatt Macy abd_verify(sabd); 929eda14cbcSMatt Macy 930eda14cbcSMatt Macy ASSERT3U(doff + size, <=, dabd->abd_size); 931eda14cbcSMatt Macy ASSERT3U(soff + size, <=, sabd->abd_size); 932eda14cbcSMatt Macy 933eda14cbcSMatt Macy dabd_is_gang_abd = abd_is_gang(dabd); 934eda14cbcSMatt Macy sabd_is_gang_abd = abd_is_gang(sabd); 935eda14cbcSMatt Macy c_dabd = abd_init_abd_iter(dabd, &daiter, doff); 936eda14cbcSMatt Macy c_sabd = abd_init_abd_iter(sabd, &saiter, soff); 937eda14cbcSMatt Macy 938eda14cbcSMatt Macy while (size > 0) { 939eda14cbcSMatt Macy /* if we are at the end of the gang ABD we are done */ 940eda14cbcSMatt Macy if ((dabd_is_gang_abd && !c_dabd) || 941eda14cbcSMatt Macy (sabd_is_gang_abd && !c_sabd)) 942eda14cbcSMatt Macy break; 943eda14cbcSMatt Macy 944eda14cbcSMatt Macy abd_iter_map(&daiter); 945eda14cbcSMatt Macy abd_iter_map(&saiter); 946eda14cbcSMatt Macy 947eda14cbcSMatt Macy size_t dlen = MIN(daiter.iter_mapsize, size); 948eda14cbcSMatt Macy size_t slen = MIN(saiter.iter_mapsize, size); 949eda14cbcSMatt Macy size_t len = MIN(dlen, slen); 950eda14cbcSMatt Macy ASSERT(dlen > 0 || slen > 0); 951eda14cbcSMatt Macy 952eda14cbcSMatt Macy ret = func(daiter.iter_mapaddr, saiter.iter_mapaddr, len, 953eda14cbcSMatt Macy private); 954eda14cbcSMatt Macy 955eda14cbcSMatt Macy abd_iter_unmap(&saiter); 956eda14cbcSMatt Macy abd_iter_unmap(&daiter); 957eda14cbcSMatt Macy 958eda14cbcSMatt Macy if (ret != 0) 959eda14cbcSMatt Macy break; 960eda14cbcSMatt Macy 961eda14cbcSMatt Macy size -= len; 962eda14cbcSMatt Macy c_dabd = 963eda14cbcSMatt Macy abd_advance_abd_iter(dabd, c_dabd, &daiter, len); 964eda14cbcSMatt Macy c_sabd = 965eda14cbcSMatt Macy abd_advance_abd_iter(sabd, c_sabd, &saiter, len); 966eda14cbcSMatt Macy } 967eda14cbcSMatt Macy 968eda14cbcSMatt Macy return (ret); 969eda14cbcSMatt Macy } 970eda14cbcSMatt Macy 971eda14cbcSMatt Macy /*ARGSUSED*/ 972eda14cbcSMatt Macy static int 973eda14cbcSMatt Macy abd_copy_off_cb(void *dbuf, void *sbuf, size_t size, void *private) 974eda14cbcSMatt Macy { 975eda14cbcSMatt Macy (void) memcpy(dbuf, sbuf, size); 976eda14cbcSMatt Macy return (0); 977eda14cbcSMatt Macy } 978eda14cbcSMatt Macy 979eda14cbcSMatt Macy /* 980eda14cbcSMatt Macy * Copy from sabd to dabd starting from soff and doff. 981eda14cbcSMatt Macy */ 982eda14cbcSMatt Macy void 983eda14cbcSMatt Macy abd_copy_off(abd_t *dabd, abd_t *sabd, size_t doff, size_t soff, size_t size) 984eda14cbcSMatt Macy { 985eda14cbcSMatt Macy (void) abd_iterate_func2(dabd, sabd, doff, soff, size, 986eda14cbcSMatt Macy abd_copy_off_cb, NULL); 987eda14cbcSMatt Macy } 988eda14cbcSMatt Macy 989eda14cbcSMatt Macy /*ARGSUSED*/ 990eda14cbcSMatt Macy static int 991eda14cbcSMatt Macy abd_cmp_cb(void *bufa, void *bufb, size_t size, void *private) 992eda14cbcSMatt Macy { 993eda14cbcSMatt Macy return (memcmp(bufa, bufb, size)); 994eda14cbcSMatt Macy } 995eda14cbcSMatt Macy 996eda14cbcSMatt Macy /* 997eda14cbcSMatt Macy * Compares the contents of two ABDs. 998eda14cbcSMatt Macy */ 999eda14cbcSMatt Macy int 1000eda14cbcSMatt Macy abd_cmp(abd_t *dabd, abd_t *sabd) 1001eda14cbcSMatt Macy { 1002eda14cbcSMatt Macy ASSERT3U(dabd->abd_size, ==, sabd->abd_size); 1003eda14cbcSMatt Macy return (abd_iterate_func2(dabd, sabd, 0, 0, dabd->abd_size, 1004eda14cbcSMatt Macy abd_cmp_cb, NULL)); 1005eda14cbcSMatt Macy } 1006eda14cbcSMatt Macy 1007eda14cbcSMatt Macy /* 1008eda14cbcSMatt Macy * Iterate over code ABDs and a data ABD and call @func_raidz_gen. 1009eda14cbcSMatt Macy * 1010eda14cbcSMatt Macy * @cabds parity ABDs, must have equal size 1011eda14cbcSMatt Macy * @dabd data ABD. Can be NULL (in this case @dsize = 0) 1012eda14cbcSMatt Macy * @func_raidz_gen should be implemented so that its behaviour 1013eda14cbcSMatt Macy * is the same when taking linear and when taking scatter 1014eda14cbcSMatt Macy */ 1015eda14cbcSMatt Macy void 1016eda14cbcSMatt Macy abd_raidz_gen_iterate(abd_t **cabds, abd_t *dabd, 1017eda14cbcSMatt Macy ssize_t csize, ssize_t dsize, const unsigned parity, 1018eda14cbcSMatt Macy void (*func_raidz_gen)(void **, const void *, size_t, size_t)) 1019eda14cbcSMatt Macy { 1020eda14cbcSMatt Macy int i; 1021eda14cbcSMatt Macy ssize_t len, dlen; 1022eda14cbcSMatt Macy struct abd_iter caiters[3]; 1023eda14cbcSMatt Macy struct abd_iter daiter = {0}; 1024eda14cbcSMatt Macy void *caddrs[3]; 1025eda14cbcSMatt Macy unsigned long flags __maybe_unused = 0; 1026eda14cbcSMatt Macy abd_t *c_cabds[3]; 1027eda14cbcSMatt Macy abd_t *c_dabd = NULL; 1028eda14cbcSMatt Macy boolean_t cabds_is_gang_abd[3]; 1029eda14cbcSMatt Macy boolean_t dabd_is_gang_abd = B_FALSE; 1030eda14cbcSMatt Macy 1031eda14cbcSMatt Macy ASSERT3U(parity, <=, 3); 1032eda14cbcSMatt Macy 1033eda14cbcSMatt Macy for (i = 0; i < parity; i++) { 1034eda14cbcSMatt Macy cabds_is_gang_abd[i] = abd_is_gang(cabds[i]); 1035eda14cbcSMatt Macy c_cabds[i] = abd_init_abd_iter(cabds[i], &caiters[i], 0); 1036eda14cbcSMatt Macy } 1037eda14cbcSMatt Macy 1038eda14cbcSMatt Macy if (dabd) { 1039eda14cbcSMatt Macy dabd_is_gang_abd = abd_is_gang(dabd); 1040eda14cbcSMatt Macy c_dabd = abd_init_abd_iter(dabd, &daiter, 0); 1041eda14cbcSMatt Macy } 1042eda14cbcSMatt Macy 1043eda14cbcSMatt Macy ASSERT3S(dsize, >=, 0); 1044eda14cbcSMatt Macy 1045eda14cbcSMatt Macy abd_enter_critical(flags); 1046eda14cbcSMatt Macy while (csize > 0) { 1047eda14cbcSMatt Macy /* if we are at the end of the gang ABD we are done */ 1048eda14cbcSMatt Macy if (dabd_is_gang_abd && !c_dabd) 1049eda14cbcSMatt Macy break; 1050eda14cbcSMatt Macy 1051eda14cbcSMatt Macy for (i = 0; i < parity; i++) { 1052eda14cbcSMatt Macy /* 1053eda14cbcSMatt Macy * If we are at the end of the gang ABD we are 1054eda14cbcSMatt Macy * done. 1055eda14cbcSMatt Macy */ 1056eda14cbcSMatt Macy if (cabds_is_gang_abd[i] && !c_cabds[i]) 1057eda14cbcSMatt Macy break; 1058eda14cbcSMatt Macy abd_iter_map(&caiters[i]); 1059eda14cbcSMatt Macy caddrs[i] = caiters[i].iter_mapaddr; 1060eda14cbcSMatt Macy } 1061eda14cbcSMatt Macy 1062eda14cbcSMatt Macy len = csize; 1063eda14cbcSMatt Macy 1064eda14cbcSMatt Macy if (dabd && dsize > 0) 1065eda14cbcSMatt Macy abd_iter_map(&daiter); 1066eda14cbcSMatt Macy 1067eda14cbcSMatt Macy switch (parity) { 1068eda14cbcSMatt Macy case 3: 1069eda14cbcSMatt Macy len = MIN(caiters[2].iter_mapsize, len); 1070eda14cbcSMatt Macy /* falls through */ 1071eda14cbcSMatt Macy case 2: 1072eda14cbcSMatt Macy len = MIN(caiters[1].iter_mapsize, len); 1073eda14cbcSMatt Macy /* falls through */ 1074eda14cbcSMatt Macy case 1: 1075eda14cbcSMatt Macy len = MIN(caiters[0].iter_mapsize, len); 1076eda14cbcSMatt Macy } 1077eda14cbcSMatt Macy 1078eda14cbcSMatt Macy /* must be progressive */ 1079eda14cbcSMatt Macy ASSERT3S(len, >, 0); 1080eda14cbcSMatt Macy 1081eda14cbcSMatt Macy if (dabd && dsize > 0) { 1082eda14cbcSMatt Macy /* this needs precise iter.length */ 1083eda14cbcSMatt Macy len = MIN(daiter.iter_mapsize, len); 1084eda14cbcSMatt Macy dlen = len; 1085eda14cbcSMatt Macy } else 1086eda14cbcSMatt Macy dlen = 0; 1087eda14cbcSMatt Macy 1088eda14cbcSMatt Macy /* must be progressive */ 1089eda14cbcSMatt Macy ASSERT3S(len, >, 0); 1090eda14cbcSMatt Macy /* 1091eda14cbcSMatt Macy * The iterated function likely will not do well if each 1092eda14cbcSMatt Macy * segment except the last one is not multiple of 512 (raidz). 1093eda14cbcSMatt Macy */ 1094eda14cbcSMatt Macy ASSERT3U(((uint64_t)len & 511ULL), ==, 0); 1095eda14cbcSMatt Macy 1096eda14cbcSMatt Macy func_raidz_gen(caddrs, daiter.iter_mapaddr, len, dlen); 1097eda14cbcSMatt Macy 1098eda14cbcSMatt Macy for (i = parity-1; i >= 0; i--) { 1099eda14cbcSMatt Macy abd_iter_unmap(&caiters[i]); 1100eda14cbcSMatt Macy c_cabds[i] = 1101eda14cbcSMatt Macy abd_advance_abd_iter(cabds[i], c_cabds[i], 1102eda14cbcSMatt Macy &caiters[i], len); 1103eda14cbcSMatt Macy } 1104eda14cbcSMatt Macy 1105eda14cbcSMatt Macy if (dabd && dsize > 0) { 1106eda14cbcSMatt Macy abd_iter_unmap(&daiter); 1107eda14cbcSMatt Macy c_dabd = 1108eda14cbcSMatt Macy abd_advance_abd_iter(dabd, c_dabd, &daiter, 1109eda14cbcSMatt Macy dlen); 1110eda14cbcSMatt Macy dsize -= dlen; 1111eda14cbcSMatt Macy } 1112eda14cbcSMatt Macy 1113eda14cbcSMatt Macy csize -= len; 1114eda14cbcSMatt Macy 1115eda14cbcSMatt Macy ASSERT3S(dsize, >=, 0); 1116eda14cbcSMatt Macy ASSERT3S(csize, >=, 0); 1117eda14cbcSMatt Macy } 1118eda14cbcSMatt Macy abd_exit_critical(flags); 1119eda14cbcSMatt Macy } 1120eda14cbcSMatt Macy 1121eda14cbcSMatt Macy /* 1122eda14cbcSMatt Macy * Iterate over code ABDs and data reconstruction target ABDs and call 1123eda14cbcSMatt Macy * @func_raidz_rec. Function maps at most 6 pages atomically. 1124eda14cbcSMatt Macy * 1125eda14cbcSMatt Macy * @cabds parity ABDs, must have equal size 1126eda14cbcSMatt Macy * @tabds rec target ABDs, at most 3 1127eda14cbcSMatt Macy * @tsize size of data target columns 1128eda14cbcSMatt Macy * @func_raidz_rec expects syndrome data in target columns. Function 1129eda14cbcSMatt Macy * reconstructs data and overwrites target columns. 1130eda14cbcSMatt Macy */ 1131eda14cbcSMatt Macy void 1132eda14cbcSMatt Macy abd_raidz_rec_iterate(abd_t **cabds, abd_t **tabds, 1133eda14cbcSMatt Macy ssize_t tsize, const unsigned parity, 1134eda14cbcSMatt Macy void (*func_raidz_rec)(void **t, const size_t tsize, void **c, 1135eda14cbcSMatt Macy const unsigned *mul), 1136eda14cbcSMatt Macy const unsigned *mul) 1137eda14cbcSMatt Macy { 1138eda14cbcSMatt Macy int i; 1139eda14cbcSMatt Macy ssize_t len; 1140eda14cbcSMatt Macy struct abd_iter citers[3]; 1141eda14cbcSMatt Macy struct abd_iter xiters[3]; 1142eda14cbcSMatt Macy void *caddrs[3], *xaddrs[3]; 1143eda14cbcSMatt Macy unsigned long flags __maybe_unused = 0; 1144eda14cbcSMatt Macy boolean_t cabds_is_gang_abd[3]; 1145eda14cbcSMatt Macy boolean_t tabds_is_gang_abd[3]; 1146eda14cbcSMatt Macy abd_t *c_cabds[3]; 1147eda14cbcSMatt Macy abd_t *c_tabds[3]; 1148eda14cbcSMatt Macy 1149eda14cbcSMatt Macy ASSERT3U(parity, <=, 3); 1150eda14cbcSMatt Macy 1151eda14cbcSMatt Macy for (i = 0; i < parity; i++) { 1152eda14cbcSMatt Macy cabds_is_gang_abd[i] = abd_is_gang(cabds[i]); 1153eda14cbcSMatt Macy tabds_is_gang_abd[i] = abd_is_gang(tabds[i]); 1154eda14cbcSMatt Macy c_cabds[i] = 1155eda14cbcSMatt Macy abd_init_abd_iter(cabds[i], &citers[i], 0); 1156eda14cbcSMatt Macy c_tabds[i] = 1157eda14cbcSMatt Macy abd_init_abd_iter(tabds[i], &xiters[i], 0); 1158eda14cbcSMatt Macy } 1159eda14cbcSMatt Macy 1160eda14cbcSMatt Macy abd_enter_critical(flags); 1161eda14cbcSMatt Macy while (tsize > 0) { 1162eda14cbcSMatt Macy 1163eda14cbcSMatt Macy for (i = 0; i < parity; i++) { 1164eda14cbcSMatt Macy /* 1165eda14cbcSMatt Macy * If we are at the end of the gang ABD we 1166eda14cbcSMatt Macy * are done. 1167eda14cbcSMatt Macy */ 1168eda14cbcSMatt Macy if (cabds_is_gang_abd[i] && !c_cabds[i]) 1169eda14cbcSMatt Macy break; 1170eda14cbcSMatt Macy if (tabds_is_gang_abd[i] && !c_tabds[i]) 1171eda14cbcSMatt Macy break; 1172eda14cbcSMatt Macy abd_iter_map(&citers[i]); 1173eda14cbcSMatt Macy abd_iter_map(&xiters[i]); 1174eda14cbcSMatt Macy caddrs[i] = citers[i].iter_mapaddr; 1175eda14cbcSMatt Macy xaddrs[i] = xiters[i].iter_mapaddr; 1176eda14cbcSMatt Macy } 1177eda14cbcSMatt Macy 1178eda14cbcSMatt Macy len = tsize; 1179eda14cbcSMatt Macy switch (parity) { 1180eda14cbcSMatt Macy case 3: 1181eda14cbcSMatt Macy len = MIN(xiters[2].iter_mapsize, len); 1182eda14cbcSMatt Macy len = MIN(citers[2].iter_mapsize, len); 1183eda14cbcSMatt Macy /* falls through */ 1184eda14cbcSMatt Macy case 2: 1185eda14cbcSMatt Macy len = MIN(xiters[1].iter_mapsize, len); 1186eda14cbcSMatt Macy len = MIN(citers[1].iter_mapsize, len); 1187eda14cbcSMatt Macy /* falls through */ 1188eda14cbcSMatt Macy case 1: 1189eda14cbcSMatt Macy len = MIN(xiters[0].iter_mapsize, len); 1190eda14cbcSMatt Macy len = MIN(citers[0].iter_mapsize, len); 1191eda14cbcSMatt Macy } 1192eda14cbcSMatt Macy /* must be progressive */ 1193eda14cbcSMatt Macy ASSERT3S(len, >, 0); 1194eda14cbcSMatt Macy /* 1195eda14cbcSMatt Macy * The iterated function likely will not do well if each 1196eda14cbcSMatt Macy * segment except the last one is not multiple of 512 (raidz). 1197eda14cbcSMatt Macy */ 1198eda14cbcSMatt Macy ASSERT3U(((uint64_t)len & 511ULL), ==, 0); 1199eda14cbcSMatt Macy 1200eda14cbcSMatt Macy func_raidz_rec(xaddrs, len, caddrs, mul); 1201eda14cbcSMatt Macy 1202eda14cbcSMatt Macy for (i = parity-1; i >= 0; i--) { 1203eda14cbcSMatt Macy abd_iter_unmap(&xiters[i]); 1204eda14cbcSMatt Macy abd_iter_unmap(&citers[i]); 1205eda14cbcSMatt Macy c_tabds[i] = 1206eda14cbcSMatt Macy abd_advance_abd_iter(tabds[i], c_tabds[i], 1207eda14cbcSMatt Macy &xiters[i], len); 1208eda14cbcSMatt Macy c_cabds[i] = 1209eda14cbcSMatt Macy abd_advance_abd_iter(cabds[i], c_cabds[i], 1210eda14cbcSMatt Macy &citers[i], len); 1211eda14cbcSMatt Macy } 1212eda14cbcSMatt Macy 1213eda14cbcSMatt Macy tsize -= len; 1214eda14cbcSMatt Macy ASSERT3S(tsize, >=, 0); 1215eda14cbcSMatt Macy } 1216eda14cbcSMatt Macy abd_exit_critical(flags); 1217eda14cbcSMatt Macy } 1218