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