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