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