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