xref: /freebsd/sys/contrib/openzfs/module/zfs/abd.c (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
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
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
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
92*7a7741afSMartin Matuska  * by using the gang ABD type (abd_alloc_gang()). This allows for multiple ABDs
93*7a7741afSMartin Matuska  * 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 {
11133b8c039SMartin Matuska #ifdef ZFS_DEBUG
112*7a7741afSMartin Matuska 	if (abd_is_from_pages(abd)) {
113*7a7741afSMartin Matuska 		ASSERT3U(abd->abd_size, <=, DMU_MAX_ACCESS);
114*7a7741afSMartin Matuska 	} else {
115eda14cbcSMatt Macy 		ASSERT3U(abd->abd_size, <=, SPA_MAXBLOCKSIZE);
116*7a7741afSMartin Matuska 	}
117eda14cbcSMatt Macy 	ASSERT3U(abd->abd_flags, ==, abd->abd_flags & (ABD_FLAG_LINEAR |
118eda14cbcSMatt Macy 	    ABD_FLAG_OWNER | ABD_FLAG_META | ABD_FLAG_MULTI_ZONE |
119eda14cbcSMatt Macy 	    ABD_FLAG_MULTI_CHUNK | ABD_FLAG_LINEAR_PAGE | ABD_FLAG_GANG |
120*7a7741afSMartin Matuska 	    ABD_FLAG_GANG_FREE | ABD_FLAG_ALLOCD | ABD_FLAG_FROM_PAGES));
121eda14cbcSMatt Macy 	IMPLY(abd->abd_parent != NULL, !(abd->abd_flags & ABD_FLAG_OWNER));
122eda14cbcSMatt Macy 	IMPLY(abd->abd_flags & ABD_FLAG_META, abd->abd_flags & ABD_FLAG_OWNER);
123eda14cbcSMatt Macy 	if (abd_is_linear(abd)) {
124d09a955aSMateusz Guzik 		ASSERT3U(abd->abd_size, >, 0);
125eda14cbcSMatt Macy 		ASSERT3P(ABD_LINEAR_BUF(abd), !=, NULL);
126eda14cbcSMatt Macy 	} else if (abd_is_gang(abd)) {
127eda14cbcSMatt Macy 		uint_t child_sizes = 0;
128eda14cbcSMatt Macy 		for (abd_t *cabd = list_head(&ABD_GANG(abd).abd_gang_chain);
129eda14cbcSMatt Macy 		    cabd != NULL;
130eda14cbcSMatt Macy 		    cabd = list_next(&ABD_GANG(abd).abd_gang_chain, cabd)) {
131eda14cbcSMatt Macy 			ASSERT(list_link_active(&cabd->abd_gang_link));
132eda14cbcSMatt Macy 			child_sizes += cabd->abd_size;
133eda14cbcSMatt Macy 			abd_verify(cabd);
134eda14cbcSMatt Macy 		}
135eda14cbcSMatt Macy 		ASSERT3U(abd->abd_size, ==, child_sizes);
136eda14cbcSMatt Macy 	} else {
137d09a955aSMateusz Guzik 		ASSERT3U(abd->abd_size, >, 0);
138eda14cbcSMatt Macy 		abd_verify_scatter(abd);
139eda14cbcSMatt Macy 	}
14033b8c039SMartin Matuska #endif
141eda14cbcSMatt Macy }
142eda14cbcSMatt Macy 
143*7a7741afSMartin Matuska void
144184c1b94SMartin Matuska abd_init_struct(abd_t *abd)
145eda14cbcSMatt Macy {
146184c1b94SMartin Matuska 	list_link_init(&abd->abd_gang_link);
147184c1b94SMartin Matuska 	mutex_init(&abd->abd_mtx, NULL, MUTEX_DEFAULT, NULL);
148184c1b94SMartin Matuska 	abd->abd_flags = 0;
149184c1b94SMartin Matuska #ifdef ZFS_DEBUG
150184c1b94SMartin Matuska 	zfs_refcount_create(&abd->abd_children);
151184c1b94SMartin Matuska 	abd->abd_parent = NULL;
152184c1b94SMartin Matuska #endif
153184c1b94SMartin Matuska 	abd->abd_size = 0;
154184c1b94SMartin Matuska }
155184c1b94SMartin Matuska 
156184c1b94SMartin Matuska static void
157184c1b94SMartin Matuska abd_fini_struct(abd_t *abd)
158184c1b94SMartin Matuska {
159184c1b94SMartin Matuska 	mutex_destroy(&abd->abd_mtx);
160184c1b94SMartin Matuska 	ASSERT(!list_link_active(&abd->abd_gang_link));
161184c1b94SMartin Matuska #ifdef ZFS_DEBUG
162184c1b94SMartin Matuska 	zfs_refcount_destroy(&abd->abd_children);
163184c1b94SMartin Matuska #endif
164184c1b94SMartin Matuska }
165184c1b94SMartin Matuska 
166184c1b94SMartin Matuska abd_t *
167184c1b94SMartin Matuska abd_alloc_struct(size_t size)
168184c1b94SMartin Matuska {
169184c1b94SMartin Matuska 	abd_t *abd = abd_alloc_struct_impl(size);
170184c1b94SMartin Matuska 	abd_init_struct(abd);
171184c1b94SMartin Matuska 	abd->abd_flags |= ABD_FLAG_ALLOCD;
172184c1b94SMartin Matuska 	return (abd);
173184c1b94SMartin Matuska }
174184c1b94SMartin Matuska 
175184c1b94SMartin Matuska void
176184c1b94SMartin Matuska abd_free_struct(abd_t *abd)
177184c1b94SMartin Matuska {
178184c1b94SMartin Matuska 	abd_fini_struct(abd);
179184c1b94SMartin Matuska 	abd_free_struct_impl(abd);
180eda14cbcSMatt Macy }
181eda14cbcSMatt Macy 
182eda14cbcSMatt Macy /*
183eda14cbcSMatt Macy  * Allocate an ABD, along with its own underlying data buffers. Use this if you
184eda14cbcSMatt Macy  * don't care whether the ABD is linear or not.
185eda14cbcSMatt Macy  */
186eda14cbcSMatt Macy abd_t *
187eda14cbcSMatt Macy abd_alloc(size_t size, boolean_t is_metadata)
188eda14cbcSMatt Macy {
1891f88aa09SMartin Matuska 	if (abd_size_alloc_linear(size))
190eda14cbcSMatt Macy 		return (abd_alloc_linear(size, is_metadata));
191eda14cbcSMatt Macy 
192eda14cbcSMatt Macy 	VERIFY3U(size, <=, SPA_MAXBLOCKSIZE);
193eda14cbcSMatt Macy 
194eda14cbcSMatt Macy 	abd_t *abd = abd_alloc_struct(size);
195184c1b94SMartin Matuska 	abd->abd_flags |= ABD_FLAG_OWNER;
196eda14cbcSMatt Macy 	abd->abd_u.abd_scatter.abd_offset = 0;
197eda14cbcSMatt Macy 	abd_alloc_chunks(abd, size);
198eda14cbcSMatt Macy 
199eda14cbcSMatt Macy 	if (is_metadata) {
200eda14cbcSMatt Macy 		abd->abd_flags |= ABD_FLAG_META;
201eda14cbcSMatt Macy 	}
202eda14cbcSMatt Macy 	abd->abd_size = size;
203eda14cbcSMatt Macy 
204eda14cbcSMatt Macy 	abd_update_scatter_stats(abd, ABDSTAT_INCR);
205eda14cbcSMatt Macy 
206eda14cbcSMatt Macy 	return (abd);
207eda14cbcSMatt Macy }
208eda14cbcSMatt Macy 
209eda14cbcSMatt Macy /*
210eda14cbcSMatt Macy  * Allocate an ABD that must be linear, along with its own underlying data
211eda14cbcSMatt Macy  * buffer. Only use this when it would be very annoying to write your ABD
212eda14cbcSMatt Macy  * consumer with a scattered ABD.
213eda14cbcSMatt Macy  */
214eda14cbcSMatt Macy abd_t *
215eda14cbcSMatt Macy abd_alloc_linear(size_t size, boolean_t is_metadata)
216eda14cbcSMatt Macy {
217eda14cbcSMatt Macy 	abd_t *abd = abd_alloc_struct(0);
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 	VERIFY3U(size, <=, SPA_MAXBLOCKSIZE);
220eda14cbcSMatt Macy 
221184c1b94SMartin Matuska 	abd->abd_flags |= ABD_FLAG_LINEAR | ABD_FLAG_OWNER;
222eda14cbcSMatt Macy 	if (is_metadata) {
223eda14cbcSMatt Macy 		abd->abd_flags |= ABD_FLAG_META;
224eda14cbcSMatt Macy 	}
225eda14cbcSMatt Macy 	abd->abd_size = size;
226eda14cbcSMatt Macy 
227eda14cbcSMatt Macy 	if (is_metadata) {
228eda14cbcSMatt Macy 		ABD_LINEAR_BUF(abd) = zio_buf_alloc(size);
229eda14cbcSMatt Macy 	} else {
230eda14cbcSMatt Macy 		ABD_LINEAR_BUF(abd) = zio_data_buf_alloc(size);
231eda14cbcSMatt Macy 	}
232eda14cbcSMatt Macy 
233eda14cbcSMatt Macy 	abd_update_linear_stats(abd, ABDSTAT_INCR);
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy 	return (abd);
236eda14cbcSMatt Macy }
237eda14cbcSMatt Macy 
238eda14cbcSMatt Macy static void
239eda14cbcSMatt Macy abd_free_linear(abd_t *abd)
240eda14cbcSMatt Macy {
241eda14cbcSMatt Macy 	if (abd_is_linear_page(abd)) {
242eda14cbcSMatt Macy 		abd_free_linear_page(abd);
243eda14cbcSMatt Macy 		return;
244eda14cbcSMatt Macy 	}
245*7a7741afSMartin Matuska 
246eda14cbcSMatt Macy 	if (abd->abd_flags & ABD_FLAG_META) {
247eda14cbcSMatt Macy 		zio_buf_free(ABD_LINEAR_BUF(abd), abd->abd_size);
248eda14cbcSMatt Macy 	} else {
249eda14cbcSMatt Macy 		zio_data_buf_free(ABD_LINEAR_BUF(abd), abd->abd_size);
250eda14cbcSMatt Macy 	}
251eda14cbcSMatt Macy 
252eda14cbcSMatt Macy 	abd_update_linear_stats(abd, ABDSTAT_DECR);
253eda14cbcSMatt Macy }
254eda14cbcSMatt Macy 
255eda14cbcSMatt Macy static void
256184c1b94SMartin Matuska abd_free_gang(abd_t *abd)
257eda14cbcSMatt Macy {
258eda14cbcSMatt Macy 	ASSERT(abd_is_gang(abd));
259184c1b94SMartin Matuska 	abd_t *cabd;
260eda14cbcSMatt Macy 
261184c1b94SMartin Matuska 	while ((cabd = list_head(&ABD_GANG(abd).abd_gang_chain)) != NULL) {
262eda14cbcSMatt Macy 		/*
263eda14cbcSMatt Macy 		 * We must acquire the child ABDs mutex to ensure that if it
264eda14cbcSMatt Macy 		 * is being added to another gang ABD we will set the link
265eda14cbcSMatt Macy 		 * as inactive when removing it from this gang ABD and before
266eda14cbcSMatt Macy 		 * adding it to the other gang ABD.
267eda14cbcSMatt Macy 		 */
268eda14cbcSMatt Macy 		mutex_enter(&cabd->abd_mtx);
269eda14cbcSMatt Macy 		ASSERT(list_link_active(&cabd->abd_gang_link));
270eda14cbcSMatt Macy 		list_remove(&ABD_GANG(abd).abd_gang_chain, cabd);
271eda14cbcSMatt Macy 		mutex_exit(&cabd->abd_mtx);
272184c1b94SMartin Matuska 		if (cabd->abd_flags & ABD_FLAG_GANG_FREE)
273eda14cbcSMatt Macy 			abd_free(cabd);
274eda14cbcSMatt Macy 	}
275eda14cbcSMatt Macy 	list_destroy(&ABD_GANG(abd).abd_gang_chain);
276184c1b94SMartin Matuska }
277184c1b94SMartin Matuska 
278184c1b94SMartin Matuska static void
279184c1b94SMartin Matuska abd_free_scatter(abd_t *abd)
280184c1b94SMartin Matuska {
281184c1b94SMartin Matuska 	abd_free_chunks(abd);
282184c1b94SMartin Matuska 	abd_update_scatter_stats(abd, ABDSTAT_DECR);
283eda14cbcSMatt Macy }
284eda14cbcSMatt Macy 
285eda14cbcSMatt Macy /*
286184c1b94SMartin Matuska  * Free an ABD.  Use with any kind of abd: those created with abd_alloc_*()
287184c1b94SMartin Matuska  * and abd_get_*(), including abd_get_offset_struct().
288184c1b94SMartin Matuska  *
289184c1b94SMartin Matuska  * If the ABD was created with abd_alloc_*(), the underlying data
290184c1b94SMartin Matuska  * (scatterlist or linear buffer) will also be freed.  (Subject to ownership
291184c1b94SMartin Matuska  * changes via abd_*_ownership_of_buf().)
292184c1b94SMartin Matuska  *
293184c1b94SMartin Matuska  * Unless the ABD was created with abd_get_offset_struct(), the abd_t will
294184c1b94SMartin Matuska  * also be freed.
295eda14cbcSMatt Macy  */
296eda14cbcSMatt Macy void
297eda14cbcSMatt Macy abd_free(abd_t *abd)
298eda14cbcSMatt Macy {
299eda14cbcSMatt Macy 	if (abd == NULL)
300eda14cbcSMatt Macy 		return;
301eda14cbcSMatt Macy 
302eda14cbcSMatt Macy 	abd_verify(abd);
303184c1b94SMartin Matuska #ifdef ZFS_DEBUG
304184c1b94SMartin Matuska 	IMPLY(abd->abd_flags & ABD_FLAG_OWNER, abd->abd_parent == NULL);
305184c1b94SMartin Matuska #endif
306184c1b94SMartin Matuska 
307184c1b94SMartin Matuska 	if (abd_is_gang(abd)) {
308184c1b94SMartin Matuska 		abd_free_gang(abd);
309184c1b94SMartin Matuska 	} else if (abd_is_linear(abd)) {
310184c1b94SMartin Matuska 		if (abd->abd_flags & ABD_FLAG_OWNER)
311eda14cbcSMatt Macy 			abd_free_linear(abd);
312184c1b94SMartin Matuska 	} else {
313184c1b94SMartin Matuska 		if (abd->abd_flags & ABD_FLAG_OWNER)
314eda14cbcSMatt Macy 			abd_free_scatter(abd);
315eda14cbcSMatt Macy 	}
316eda14cbcSMatt Macy 
317184c1b94SMartin Matuska #ifdef ZFS_DEBUG
318184c1b94SMartin Matuska 	if (abd->abd_parent != NULL) {
319184c1b94SMartin Matuska 		(void) zfs_refcount_remove_many(&abd->abd_parent->abd_children,
320184c1b94SMartin Matuska 		    abd->abd_size, abd);
321184c1b94SMartin Matuska 	}
322184c1b94SMartin Matuska #endif
323184c1b94SMartin Matuska 
324184c1b94SMartin Matuska 	abd_fini_struct(abd);
325184c1b94SMartin Matuska 	if (abd->abd_flags & ABD_FLAG_ALLOCD)
326184c1b94SMartin Matuska 		abd_free_struct_impl(abd);
327184c1b94SMartin Matuska }
328184c1b94SMartin Matuska 
329eda14cbcSMatt Macy /*
330eda14cbcSMatt Macy  * Allocate an ABD of the same format (same metadata flag, same scatterize
331eda14cbcSMatt Macy  * setting) as another ABD.
332eda14cbcSMatt Macy  */
333eda14cbcSMatt Macy abd_t *
334eda14cbcSMatt Macy abd_alloc_sametype(abd_t *sabd, size_t size)
335eda14cbcSMatt Macy {
336eda14cbcSMatt Macy 	boolean_t is_metadata = (sabd->abd_flags & ABD_FLAG_META) != 0;
337eda14cbcSMatt Macy 	if (abd_is_linear(sabd) &&
338eda14cbcSMatt Macy 	    !abd_is_linear_page(sabd)) {
339eda14cbcSMatt Macy 		return (abd_alloc_linear(size, is_metadata));
340eda14cbcSMatt Macy 	} else {
341eda14cbcSMatt Macy 		return (abd_alloc(size, is_metadata));
342eda14cbcSMatt Macy 	}
343eda14cbcSMatt Macy }
344eda14cbcSMatt Macy 
345eda14cbcSMatt Macy /*
346eda14cbcSMatt Macy  * Create gang ABD that will be the head of a list of ABD's. This is used
347eda14cbcSMatt Macy  * to "chain" scatter/gather lists together when constructing aggregated
348eda14cbcSMatt Macy  * IO's. To free this abd, abd_free() must be called.
349eda14cbcSMatt Macy  */
350eda14cbcSMatt Macy abd_t *
351184c1b94SMartin Matuska abd_alloc_gang(void)
352eda14cbcSMatt Macy {
353184c1b94SMartin Matuska 	abd_t *abd = abd_alloc_struct(0);
354184c1b94SMartin Matuska 	abd->abd_flags |= ABD_FLAG_GANG | ABD_FLAG_OWNER;
355eda14cbcSMatt Macy 	list_create(&ABD_GANG(abd).abd_gang_chain,
356eda14cbcSMatt Macy 	    sizeof (abd_t), offsetof(abd_t, abd_gang_link));
357eda14cbcSMatt Macy 	return (abd);
358eda14cbcSMatt Macy }
359eda14cbcSMatt Macy 
360eda14cbcSMatt Macy /*
361eda14cbcSMatt Macy  * Add a child gang ABD to a parent gang ABDs chained list.
362eda14cbcSMatt Macy  */
363eda14cbcSMatt Macy static void
364eda14cbcSMatt Macy abd_gang_add_gang(abd_t *pabd, abd_t *cabd, boolean_t free_on_free)
365eda14cbcSMatt Macy {
366eda14cbcSMatt Macy 	ASSERT(abd_is_gang(pabd));
367eda14cbcSMatt Macy 	ASSERT(abd_is_gang(cabd));
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy 	if (free_on_free) {
370eda14cbcSMatt Macy 		/*
371eda14cbcSMatt Macy 		 * If the parent is responsible for freeing the child gang
372184c1b94SMartin Matuska 		 * ABD we will just splice the child's children ABD list to
373184c1b94SMartin Matuska 		 * the parent's list and immediately free the child gang ABD
374eda14cbcSMatt Macy 		 * struct. The parent gang ABDs children from the child gang
375eda14cbcSMatt Macy 		 * will retain all the free_on_free settings after being
376eda14cbcSMatt Macy 		 * added to the parents list.
377eda14cbcSMatt Macy 		 */
378e639e0d2SMartin Matuska #ifdef ZFS_DEBUG
379e639e0d2SMartin Matuska 		/*
380e639e0d2SMartin Matuska 		 * If cabd had abd_parent, we have to drop it here.  We can't
381e639e0d2SMartin Matuska 		 * transfer it to pabd, nor we can clear abd_size leaving it.
382e639e0d2SMartin Matuska 		 */
383e639e0d2SMartin Matuska 		if (cabd->abd_parent != NULL) {
384e639e0d2SMartin Matuska 			(void) zfs_refcount_remove_many(
385e639e0d2SMartin Matuska 			    &cabd->abd_parent->abd_children,
386e639e0d2SMartin Matuska 			    cabd->abd_size, cabd);
387e639e0d2SMartin Matuska 			cabd->abd_parent = NULL;
388e639e0d2SMartin Matuska 		}
389e639e0d2SMartin Matuska #endif
390eda14cbcSMatt Macy 		pabd->abd_size += cabd->abd_size;
391e639e0d2SMartin Matuska 		cabd->abd_size = 0;
392eda14cbcSMatt Macy 		list_move_tail(&ABD_GANG(pabd).abd_gang_chain,
393eda14cbcSMatt Macy 		    &ABD_GANG(cabd).abd_gang_chain);
394eda14cbcSMatt Macy 		ASSERT(list_is_empty(&ABD_GANG(cabd).abd_gang_chain));
395eda14cbcSMatt Macy 		abd_verify(pabd);
396184c1b94SMartin Matuska 		abd_free(cabd);
397eda14cbcSMatt Macy 	} else {
398eda14cbcSMatt Macy 		for (abd_t *child = list_head(&ABD_GANG(cabd).abd_gang_chain);
399eda14cbcSMatt Macy 		    child != NULL;
400eda14cbcSMatt Macy 		    child = list_next(&ABD_GANG(cabd).abd_gang_chain, child)) {
401eda14cbcSMatt Macy 			/*
402eda14cbcSMatt Macy 			 * We always pass B_FALSE for free_on_free as it is the
40316038816SMartin Matuska 			 * original child gang ABDs responsibility to determine
404eda14cbcSMatt Macy 			 * if any of its child ABDs should be free'd on the call
405eda14cbcSMatt Macy 			 * to abd_free().
406eda14cbcSMatt Macy 			 */
407eda14cbcSMatt Macy 			abd_gang_add(pabd, child, B_FALSE);
408eda14cbcSMatt Macy 		}
409eda14cbcSMatt Macy 		abd_verify(pabd);
410eda14cbcSMatt Macy 	}
411eda14cbcSMatt Macy }
412eda14cbcSMatt Macy 
413eda14cbcSMatt Macy /*
414eda14cbcSMatt Macy  * Add a child ABD to a gang ABD's chained list.
415eda14cbcSMatt Macy  */
416eda14cbcSMatt Macy void
417eda14cbcSMatt Macy abd_gang_add(abd_t *pabd, abd_t *cabd, boolean_t free_on_free)
418eda14cbcSMatt Macy {
419eda14cbcSMatt Macy 	ASSERT(abd_is_gang(pabd));
420eda14cbcSMatt Macy 	abd_t *child_abd = NULL;
421eda14cbcSMatt Macy 
422eda14cbcSMatt Macy 	/*
423eda14cbcSMatt Macy 	 * If the child being added is a gang ABD, we will add the
424184c1b94SMartin Matuska 	 * child's ABDs to the parent gang ABD. This allows us to account
425eda14cbcSMatt Macy 	 * for the offset correctly in the parent gang ABD.
426eda14cbcSMatt Macy 	 */
427eda14cbcSMatt Macy 	if (abd_is_gang(cabd)) {
428eda14cbcSMatt Macy 		ASSERT(!list_link_active(&cabd->abd_gang_link));
429eda14cbcSMatt Macy 		return (abd_gang_add_gang(pabd, cabd, free_on_free));
430eda14cbcSMatt Macy 	}
431eda14cbcSMatt Macy 	ASSERT(!abd_is_gang(cabd));
432eda14cbcSMatt Macy 
433eda14cbcSMatt Macy 	/*
434eda14cbcSMatt Macy 	 * In order to verify that an ABD is not already part of
435eda14cbcSMatt Macy 	 * another gang ABD, we must lock the child ABD's abd_mtx
436eda14cbcSMatt Macy 	 * to check its abd_gang_link status. We unlock the abd_mtx
437eda14cbcSMatt Macy 	 * only after it is has been added to a gang ABD, which
438eda14cbcSMatt Macy 	 * will update the abd_gang_link's status. See comment below
439eda14cbcSMatt Macy 	 * for how an ABD can be in multiple gang ABD's simultaneously.
440eda14cbcSMatt Macy 	 */
441eda14cbcSMatt Macy 	mutex_enter(&cabd->abd_mtx);
442eda14cbcSMatt Macy 	if (list_link_active(&cabd->abd_gang_link)) {
443eda14cbcSMatt Macy 		/*
444eda14cbcSMatt Macy 		 * If the child ABD is already part of another
445eda14cbcSMatt Macy 		 * gang ABD then we must allocate a new
446eda14cbcSMatt Macy 		 * ABD to use a separate link. We mark the newly
447eda14cbcSMatt Macy 		 * allocated ABD with ABD_FLAG_GANG_FREE, before
448eda14cbcSMatt Macy 		 * adding it to the gang ABD's list, to make the
449eda14cbcSMatt Macy 		 * gang ABD aware that it is responsible to call
450184c1b94SMartin Matuska 		 * abd_free(). We use abd_get_offset() in order
451eda14cbcSMatt Macy 		 * to just allocate a new ABD but avoid copying the
452eda14cbcSMatt Macy 		 * data over into the newly allocated ABD.
453eda14cbcSMatt Macy 		 *
454eda14cbcSMatt Macy 		 * An ABD may become part of multiple gang ABD's. For
455eda14cbcSMatt Macy 		 * example, when writing ditto bocks, the same ABD
456eda14cbcSMatt Macy 		 * is used to write 2 or 3 locations with 2 or 3
457eda14cbcSMatt Macy 		 * zio_t's. Each of the zio's may be aggregated with
458eda14cbcSMatt Macy 		 * different adjacent zio's. zio aggregation uses gang
459eda14cbcSMatt Macy 		 * zio's, so the single ABD can become part of multiple
460eda14cbcSMatt Macy 		 * gang zio's.
461eda14cbcSMatt Macy 		 *
462eda14cbcSMatt Macy 		 * The ASSERT below is to make sure that if
463eda14cbcSMatt Macy 		 * free_on_free is passed as B_TRUE, the ABD can
464eda14cbcSMatt Macy 		 * not be in multiple gang ABD's. The gang ABD
465eda14cbcSMatt Macy 		 * can not be responsible for cleaning up the child
466eda14cbcSMatt Macy 		 * ABD memory allocation if the ABD can be in
467eda14cbcSMatt Macy 		 * multiple gang ABD's at one time.
468eda14cbcSMatt Macy 		 */
469eda14cbcSMatt Macy 		ASSERT3B(free_on_free, ==, B_FALSE);
470eda14cbcSMatt Macy 		child_abd = abd_get_offset(cabd, 0);
471eda14cbcSMatt Macy 		child_abd->abd_flags |= ABD_FLAG_GANG_FREE;
472eda14cbcSMatt Macy 	} else {
473eda14cbcSMatt Macy 		child_abd = cabd;
474eda14cbcSMatt Macy 		if (free_on_free)
475eda14cbcSMatt Macy 			child_abd->abd_flags |= ABD_FLAG_GANG_FREE;
476eda14cbcSMatt Macy 	}
477eda14cbcSMatt Macy 	ASSERT3P(child_abd, !=, NULL);
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy 	list_insert_tail(&ABD_GANG(pabd).abd_gang_chain, child_abd);
480eda14cbcSMatt Macy 	mutex_exit(&cabd->abd_mtx);
481eda14cbcSMatt Macy 	pabd->abd_size += child_abd->abd_size;
482eda14cbcSMatt Macy }
483eda14cbcSMatt Macy 
484eda14cbcSMatt Macy /*
485eda14cbcSMatt Macy  * Locate the ABD for the supplied offset in the gang ABD.
486eda14cbcSMatt Macy  * Return a new offset relative to the returned ABD.
487eda14cbcSMatt Macy  */
488eda14cbcSMatt Macy abd_t *
489eda14cbcSMatt Macy abd_gang_get_offset(abd_t *abd, size_t *off)
490eda14cbcSMatt Macy {
491eda14cbcSMatt Macy 	abd_t *cabd;
492eda14cbcSMatt Macy 
493eda14cbcSMatt Macy 	ASSERT(abd_is_gang(abd));
494eda14cbcSMatt Macy 	ASSERT3U(*off, <, abd->abd_size);
495eda14cbcSMatt Macy 	for (cabd = list_head(&ABD_GANG(abd).abd_gang_chain); cabd != NULL;
496eda14cbcSMatt Macy 	    cabd = list_next(&ABD_GANG(abd).abd_gang_chain, cabd)) {
497eda14cbcSMatt Macy 		if (*off >= cabd->abd_size)
498eda14cbcSMatt Macy 			*off -= cabd->abd_size;
499eda14cbcSMatt Macy 		else
500eda14cbcSMatt Macy 			return (cabd);
501eda14cbcSMatt Macy 	}
502eda14cbcSMatt Macy 	VERIFY3P(cabd, !=, NULL);
503eda14cbcSMatt Macy 	return (cabd);
504eda14cbcSMatt Macy }
505eda14cbcSMatt Macy 
506eda14cbcSMatt Macy /*
507184c1b94SMartin Matuska  * Allocate a new ABD, using the provided struct (if non-NULL, and if
508184c1b94SMartin Matuska  * circumstances allow - otherwise allocate the struct).  The returned ABD will
509184c1b94SMartin Matuska  * point to offset off of sabd. It shares the underlying buffer data with sabd.
510184c1b94SMartin Matuska  * Use abd_free() to free.  sabd must not be freed while any derived ABDs exist.
511eda14cbcSMatt Macy  */
512eda14cbcSMatt Macy static abd_t *
513184c1b94SMartin Matuska abd_get_offset_impl(abd_t *abd, abd_t *sabd, size_t off, size_t size)
514eda14cbcSMatt Macy {
515eda14cbcSMatt Macy 	abd_verify(sabd);
516184c1b94SMartin Matuska 	ASSERT3U(off + size, <=, sabd->abd_size);
517eda14cbcSMatt Macy 
518eda14cbcSMatt Macy 	if (abd_is_linear(sabd)) {
519184c1b94SMartin Matuska 		if (abd == NULL)
520eda14cbcSMatt Macy 			abd = abd_alloc_struct(0);
521eda14cbcSMatt Macy 		/*
522eda14cbcSMatt Macy 		 * Even if this buf is filesystem metadata, we only track that
523eda14cbcSMatt Macy 		 * if we own the underlying data buffer, which is not true in
524eda14cbcSMatt Macy 		 * this case. Therefore, we don't ever use ABD_FLAG_META here.
525eda14cbcSMatt Macy 		 */
526184c1b94SMartin Matuska 		abd->abd_flags |= ABD_FLAG_LINEAR;
527eda14cbcSMatt Macy 
528*7a7741afSMartin Matuska 		/*
529*7a7741afSMartin Matuska 		 * User pages from Direct I/O requests may be in a single page
530*7a7741afSMartin Matuska 		 * (ABD_FLAG_LINEAR_PAGE), and we must make sure to still flag
531*7a7741afSMartin Matuska 		 * that here for abd. This is required because we have to be
532*7a7741afSMartin Matuska 		 * careful when borrowing the buffer from the ABD because we
533*7a7741afSMartin Matuska 		 * can not place user pages under write protection on Linux.
534*7a7741afSMartin Matuska 		 * See the comments in abd_os.c for abd_borrow_buf(),
535*7a7741afSMartin Matuska 		 * abd_borrow_buf_copy(), abd_return_buf() and
536*7a7741afSMartin Matuska 		 * abd_return_buf_copy().
537*7a7741afSMartin Matuska 		 */
538*7a7741afSMartin Matuska 		if (abd_is_from_pages(sabd)) {
539*7a7741afSMartin Matuska 			abd->abd_flags |= ABD_FLAG_FROM_PAGES |
540*7a7741afSMartin Matuska 			    ABD_FLAG_LINEAR_PAGE;
541*7a7741afSMartin Matuska 		}
542*7a7741afSMartin Matuska 
543eda14cbcSMatt Macy 		ABD_LINEAR_BUF(abd) = (char *)ABD_LINEAR_BUF(sabd) + off;
544eda14cbcSMatt Macy 	} else if (abd_is_gang(sabd)) {
545eda14cbcSMatt Macy 		size_t left = size;
546184c1b94SMartin Matuska 		if (abd == NULL) {
547184c1b94SMartin Matuska 			abd = abd_alloc_gang();
548184c1b94SMartin Matuska 		} else {
549184c1b94SMartin Matuska 			abd->abd_flags |= ABD_FLAG_GANG;
550184c1b94SMartin Matuska 			list_create(&ABD_GANG(abd).abd_gang_chain,
551184c1b94SMartin Matuska 			    sizeof (abd_t), offsetof(abd_t, abd_gang_link));
552184c1b94SMartin Matuska 		}
553184c1b94SMartin Matuska 
554eda14cbcSMatt Macy 		abd->abd_flags &= ~ABD_FLAG_OWNER;
555eda14cbcSMatt Macy 		for (abd_t *cabd = abd_gang_get_offset(sabd, &off);
556eda14cbcSMatt Macy 		    cabd != NULL && left > 0;
557eda14cbcSMatt Macy 		    cabd = list_next(&ABD_GANG(sabd).abd_gang_chain, cabd)) {
558eda14cbcSMatt Macy 			int csize = MIN(left, cabd->abd_size - off);
559eda14cbcSMatt Macy 
560184c1b94SMartin Matuska 			abd_t *nabd = abd_get_offset_size(cabd, off, csize);
561184c1b94SMartin Matuska 			abd_gang_add(abd, nabd, B_TRUE);
562eda14cbcSMatt Macy 			left -= csize;
563eda14cbcSMatt Macy 			off = 0;
564eda14cbcSMatt Macy 		}
565eda14cbcSMatt Macy 		ASSERT3U(left, ==, 0);
566eda14cbcSMatt Macy 	} else {
5677cd22ac4SMartin Matuska 		abd = abd_get_offset_scatter(abd, sabd, off, size);
568eda14cbcSMatt Macy 	}
569eda14cbcSMatt Macy 
570184c1b94SMartin Matuska 	ASSERT3P(abd, !=, NULL);
571eda14cbcSMatt Macy 	abd->abd_size = size;
572184c1b94SMartin Matuska #ifdef ZFS_DEBUG
573eda14cbcSMatt Macy 	abd->abd_parent = sabd;
574eda14cbcSMatt Macy 	(void) zfs_refcount_add_many(&sabd->abd_children, abd->abd_size, abd);
575184c1b94SMartin Matuska #endif
576eda14cbcSMatt Macy 	return (abd);
577eda14cbcSMatt Macy }
578eda14cbcSMatt Macy 
579184c1b94SMartin Matuska /*
580184c1b94SMartin Matuska  * Like abd_get_offset_size(), but memory for the abd_t is provided by the
581184c1b94SMartin Matuska  * caller.  Using this routine can improve performance by avoiding the cost
582184c1b94SMartin Matuska  * of allocating memory for the abd_t struct, and updating the abd stats.
583184c1b94SMartin Matuska  * Usually, the provided abd is returned, but in some circumstances (FreeBSD,
584184c1b94SMartin Matuska  * if sabd is scatter and size is more than 2 pages) a new abd_t may need to
585184c1b94SMartin Matuska  * be allocated.  Therefore callers should be careful to use the returned
586184c1b94SMartin Matuska  * abd_t*.
587184c1b94SMartin Matuska  */
588184c1b94SMartin Matuska abd_t *
589184c1b94SMartin Matuska abd_get_offset_struct(abd_t *abd, abd_t *sabd, size_t off, size_t size)
590184c1b94SMartin Matuska {
5919db44a8eSMartin Matuska 	abd_t *result;
592184c1b94SMartin Matuska 	abd_init_struct(abd);
5939db44a8eSMartin Matuska 	result = abd_get_offset_impl(abd, sabd, off, size);
5949db44a8eSMartin Matuska 	if (result != abd)
5959db44a8eSMartin Matuska 		abd_fini_struct(abd);
5969db44a8eSMartin Matuska 	return (result);
597184c1b94SMartin Matuska }
598184c1b94SMartin Matuska 
599eda14cbcSMatt Macy abd_t *
600eda14cbcSMatt Macy abd_get_offset(abd_t *sabd, size_t off)
601eda14cbcSMatt Macy {
602eda14cbcSMatt Macy 	size_t size = sabd->abd_size > off ? sabd->abd_size - off : 0;
603eda14cbcSMatt Macy 	VERIFY3U(size, >, 0);
604184c1b94SMartin Matuska 	return (abd_get_offset_impl(NULL, sabd, off, size));
605eda14cbcSMatt Macy }
606eda14cbcSMatt Macy 
607eda14cbcSMatt Macy abd_t *
608eda14cbcSMatt Macy abd_get_offset_size(abd_t *sabd, size_t off, size_t size)
609eda14cbcSMatt Macy {
610eda14cbcSMatt Macy 	ASSERT3U(off + size, <=, sabd->abd_size);
611184c1b94SMartin Matuska 	return (abd_get_offset_impl(NULL, sabd, off, size));
612eda14cbcSMatt Macy }
613eda14cbcSMatt Macy 
614eda14cbcSMatt Macy /*
615184c1b94SMartin Matuska  * Return a size scatter ABD containing only zeros.
616eda14cbcSMatt Macy  */
617eda14cbcSMatt Macy abd_t *
618eda14cbcSMatt Macy abd_get_zeros(size_t size)
619eda14cbcSMatt Macy {
620eda14cbcSMatt Macy 	ASSERT3P(abd_zero_scatter, !=, NULL);
621eda14cbcSMatt Macy 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
622eda14cbcSMatt Macy 	return (abd_get_offset_size(abd_zero_scatter, 0, size));
623eda14cbcSMatt Macy }
624eda14cbcSMatt Macy 
625eda14cbcSMatt Macy /*
626e2df9bb4SMartin Matuska  * Create a linear ABD for an existing buf.
627eda14cbcSMatt Macy  */
628e2df9bb4SMartin Matuska static abd_t *
629e2df9bb4SMartin Matuska abd_get_from_buf_impl(abd_t *abd, void *buf, size_t size)
630eda14cbcSMatt Macy {
631eda14cbcSMatt Macy 	VERIFY3U(size, <=, SPA_MAXBLOCKSIZE);
632eda14cbcSMatt Macy 
633eda14cbcSMatt Macy 	/*
634eda14cbcSMatt Macy 	 * Even if this buf is filesystem metadata, we only track that if we
635eda14cbcSMatt Macy 	 * own the underlying data buffer, which is not true in this case.
636eda14cbcSMatt Macy 	 * Therefore, we don't ever use ABD_FLAG_META here.
637eda14cbcSMatt Macy 	 */
638184c1b94SMartin Matuska 	abd->abd_flags |= ABD_FLAG_LINEAR;
639eda14cbcSMatt Macy 	abd->abd_size = size;
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy 	ABD_LINEAR_BUF(abd) = buf;
642eda14cbcSMatt Macy 
643eda14cbcSMatt Macy 	return (abd);
644eda14cbcSMatt Macy }
645eda14cbcSMatt Macy 
646e2df9bb4SMartin Matuska abd_t *
647e2df9bb4SMartin Matuska abd_get_from_buf(void *buf, size_t size)
648e2df9bb4SMartin Matuska {
649e2df9bb4SMartin Matuska 	abd_t *abd = abd_alloc_struct(0);
650e2df9bb4SMartin Matuska 	return (abd_get_from_buf_impl(abd, buf, size));
651e2df9bb4SMartin Matuska }
652e2df9bb4SMartin Matuska 
653e2df9bb4SMartin Matuska abd_t *
654e2df9bb4SMartin Matuska abd_get_from_buf_struct(abd_t *abd, void *buf, size_t size)
655e2df9bb4SMartin Matuska {
656e2df9bb4SMartin Matuska 	abd_init_struct(abd);
657e2df9bb4SMartin Matuska 	return (abd_get_from_buf_impl(abd, buf, size));
658e2df9bb4SMartin Matuska }
659e2df9bb4SMartin Matuska 
660eda14cbcSMatt Macy /*
661eda14cbcSMatt Macy  * Get the raw buffer associated with a linear ABD.
662eda14cbcSMatt Macy  */
663eda14cbcSMatt Macy void *
664eda14cbcSMatt Macy abd_to_buf(abd_t *abd)
665eda14cbcSMatt Macy {
666eda14cbcSMatt Macy 	ASSERT(abd_is_linear(abd));
667eda14cbcSMatt Macy 	abd_verify(abd);
668eda14cbcSMatt Macy 	return (ABD_LINEAR_BUF(abd));
669eda14cbcSMatt Macy }
670eda14cbcSMatt Macy 
671eda14cbcSMatt Macy void
672eda14cbcSMatt Macy abd_release_ownership_of_buf(abd_t *abd)
673eda14cbcSMatt Macy {
674eda14cbcSMatt Macy 	ASSERT(abd_is_linear(abd));
675eda14cbcSMatt Macy 	ASSERT(abd->abd_flags & ABD_FLAG_OWNER);
676eda14cbcSMatt Macy 
677eda14cbcSMatt Macy 	/*
678eda14cbcSMatt Macy 	 * abd_free() needs to handle LINEAR_PAGE ABD's specially.
679eda14cbcSMatt Macy 	 * Since that flag does not survive the
680eda14cbcSMatt Macy 	 * abd_release_ownership_of_buf() -> abd_get_from_buf() ->
681eda14cbcSMatt Macy 	 * abd_take_ownership_of_buf() sequence, we don't allow releasing
682eda14cbcSMatt Macy 	 * these "linear but not zio_[data_]buf_alloc()'ed" ABD's.
683eda14cbcSMatt Macy 	 */
684eda14cbcSMatt Macy 	ASSERT(!abd_is_linear_page(abd));
685eda14cbcSMatt Macy 
686eda14cbcSMatt Macy 	abd_verify(abd);
687eda14cbcSMatt Macy 
688eda14cbcSMatt Macy 	abd->abd_flags &= ~ABD_FLAG_OWNER;
689eda14cbcSMatt Macy 	/* Disable this flag since we no longer own the data buffer */
690eda14cbcSMatt Macy 	abd->abd_flags &= ~ABD_FLAG_META;
691eda14cbcSMatt Macy 
692eda14cbcSMatt Macy 	abd_update_linear_stats(abd, ABDSTAT_DECR);
693eda14cbcSMatt Macy }
694eda14cbcSMatt Macy 
695eda14cbcSMatt Macy 
696eda14cbcSMatt Macy /*
697eda14cbcSMatt Macy  * Give this ABD ownership of the buffer that it's storing. Can only be used on
698eda14cbcSMatt Macy  * linear ABDs which were allocated via abd_get_from_buf(), or ones allocated
699eda14cbcSMatt Macy  * with abd_alloc_linear() which subsequently released ownership of their buf
700eda14cbcSMatt Macy  * with abd_release_ownership_of_buf().
701eda14cbcSMatt Macy  */
702eda14cbcSMatt Macy void
703eda14cbcSMatt Macy abd_take_ownership_of_buf(abd_t *abd, boolean_t is_metadata)
704eda14cbcSMatt Macy {
705eda14cbcSMatt Macy 	ASSERT(abd_is_linear(abd));
706eda14cbcSMatt Macy 	ASSERT(!(abd->abd_flags & ABD_FLAG_OWNER));
707eda14cbcSMatt Macy 	abd_verify(abd);
708eda14cbcSMatt Macy 
709eda14cbcSMatt Macy 	abd->abd_flags |= ABD_FLAG_OWNER;
710eda14cbcSMatt Macy 	if (is_metadata) {
711eda14cbcSMatt Macy 		abd->abd_flags |= ABD_FLAG_META;
712eda14cbcSMatt Macy 	}
713eda14cbcSMatt Macy 
714eda14cbcSMatt Macy 	abd_update_linear_stats(abd, ABDSTAT_INCR);
715eda14cbcSMatt Macy }
716eda14cbcSMatt Macy 
717eda14cbcSMatt Macy /*
718eda14cbcSMatt Macy  * Initializes an abd_iter based on whether the abd is a gang ABD
719eda14cbcSMatt Macy  * or just a single ABD.
720eda14cbcSMatt Macy  */
721eda14cbcSMatt Macy static inline abd_t *
722eda14cbcSMatt Macy abd_init_abd_iter(abd_t *abd, struct abd_iter *aiter, size_t off)
723eda14cbcSMatt Macy {
724eda14cbcSMatt Macy 	abd_t *cabd = NULL;
725eda14cbcSMatt Macy 
726eda14cbcSMatt Macy 	if (abd_is_gang(abd)) {
727eda14cbcSMatt Macy 		cabd = abd_gang_get_offset(abd, &off);
728eda14cbcSMatt Macy 		if (cabd) {
729eda14cbcSMatt Macy 			abd_iter_init(aiter, cabd);
730eda14cbcSMatt Macy 			abd_iter_advance(aiter, off);
731eda14cbcSMatt Macy 		}
732eda14cbcSMatt Macy 	} else {
733eda14cbcSMatt Macy 		abd_iter_init(aiter, abd);
734eda14cbcSMatt Macy 		abd_iter_advance(aiter, off);
735eda14cbcSMatt Macy 	}
736eda14cbcSMatt Macy 	return (cabd);
737eda14cbcSMatt Macy }
738eda14cbcSMatt Macy 
739eda14cbcSMatt Macy /*
740eda14cbcSMatt Macy  * Advances an abd_iter. We have to be careful with gang ABD as
741eda14cbcSMatt Macy  * advancing could mean that we are at the end of a particular ABD and
742eda14cbcSMatt Macy  * must grab the ABD in the gang ABD's list.
743eda14cbcSMatt Macy  */
744eda14cbcSMatt Macy static inline abd_t *
745eda14cbcSMatt Macy abd_advance_abd_iter(abd_t *abd, abd_t *cabd, struct abd_iter *aiter,
746eda14cbcSMatt Macy     size_t len)
747eda14cbcSMatt Macy {
748eda14cbcSMatt Macy 	abd_iter_advance(aiter, len);
749eda14cbcSMatt Macy 	if (abd_is_gang(abd) && abd_iter_at_end(aiter)) {
750eda14cbcSMatt Macy 		ASSERT3P(cabd, !=, NULL);
751eda14cbcSMatt Macy 		cabd = list_next(&ABD_GANG(abd).abd_gang_chain, cabd);
752eda14cbcSMatt Macy 		if (cabd) {
753eda14cbcSMatt Macy 			abd_iter_init(aiter, cabd);
754eda14cbcSMatt Macy 			abd_iter_advance(aiter, 0);
755eda14cbcSMatt Macy 		}
756eda14cbcSMatt Macy 	}
757eda14cbcSMatt Macy 	return (cabd);
758eda14cbcSMatt Macy }
759eda14cbcSMatt Macy 
760eda14cbcSMatt Macy int
761eda14cbcSMatt Macy abd_iterate_func(abd_t *abd, size_t off, size_t size,
762eda14cbcSMatt Macy     abd_iter_func_t *func, void *private)
763eda14cbcSMatt Macy {
764eda14cbcSMatt Macy 	struct abd_iter aiter;
7657877fdebSMatt Macy 	int ret = 0;
7667877fdebSMatt Macy 
7677877fdebSMatt Macy 	if (size == 0)
7687877fdebSMatt Macy 		return (0);
769eda14cbcSMatt Macy 
770eda14cbcSMatt Macy 	abd_verify(abd);
771eda14cbcSMatt Macy 	ASSERT3U(off + size, <=, abd->abd_size);
772eda14cbcSMatt Macy 
7737877fdebSMatt Macy 	abd_t *c_abd = abd_init_abd_iter(abd, &aiter, off);
774eda14cbcSMatt Macy 
775eda14cbcSMatt Macy 	while (size > 0) {
7766c1e79dfSMartin Matuska 		IMPLY(abd_is_gang(abd), c_abd != NULL);
777eda14cbcSMatt Macy 
778eda14cbcSMatt Macy 		abd_iter_map(&aiter);
779eda14cbcSMatt Macy 
780eda14cbcSMatt Macy 		size_t len = MIN(aiter.iter_mapsize, size);
781eda14cbcSMatt Macy 		ASSERT3U(len, >, 0);
782eda14cbcSMatt Macy 
783eda14cbcSMatt Macy 		ret = func(aiter.iter_mapaddr, len, private);
784eda14cbcSMatt Macy 
785eda14cbcSMatt Macy 		abd_iter_unmap(&aiter);
786eda14cbcSMatt Macy 
787eda14cbcSMatt Macy 		if (ret != 0)
788eda14cbcSMatt Macy 			break;
789eda14cbcSMatt Macy 
790eda14cbcSMatt Macy 		size -= len;
791eda14cbcSMatt Macy 		c_abd = abd_advance_abd_iter(abd, c_abd, &aiter, len);
792eda14cbcSMatt Macy 	}
793eda14cbcSMatt Macy 
794eda14cbcSMatt Macy 	return (ret);
795eda14cbcSMatt Macy }
796eda14cbcSMatt Macy 
797783d3ff6SMartin Matuska #if defined(__linux__) && defined(_KERNEL)
798783d3ff6SMartin Matuska int
799783d3ff6SMartin Matuska abd_iterate_page_func(abd_t *abd, size_t off, size_t size,
800783d3ff6SMartin Matuska     abd_iter_page_func_t *func, void *private)
801783d3ff6SMartin Matuska {
802783d3ff6SMartin Matuska 	struct abd_iter aiter;
803783d3ff6SMartin Matuska 	int ret = 0;
804783d3ff6SMartin Matuska 
805783d3ff6SMartin Matuska 	if (size == 0)
806783d3ff6SMartin Matuska 		return (0);
807783d3ff6SMartin Matuska 
808783d3ff6SMartin Matuska 	abd_verify(abd);
809783d3ff6SMartin Matuska 	ASSERT3U(off + size, <=, abd->abd_size);
810783d3ff6SMartin Matuska 
811783d3ff6SMartin Matuska 	abd_t *c_abd = abd_init_abd_iter(abd, &aiter, off);
812783d3ff6SMartin Matuska 
813783d3ff6SMartin Matuska 	while (size > 0) {
814783d3ff6SMartin Matuska 		IMPLY(abd_is_gang(abd), c_abd != NULL);
815783d3ff6SMartin Matuska 
816783d3ff6SMartin Matuska 		abd_iter_page(&aiter);
817783d3ff6SMartin Matuska 
818783d3ff6SMartin Matuska 		size_t len = MIN(aiter.iter_page_dsize, size);
819783d3ff6SMartin Matuska 		ASSERT3U(len, >, 0);
820783d3ff6SMartin Matuska 
821783d3ff6SMartin Matuska 		ret = func(aiter.iter_page, aiter.iter_page_doff,
822783d3ff6SMartin Matuska 		    len, private);
823783d3ff6SMartin Matuska 
824783d3ff6SMartin Matuska 		aiter.iter_page = NULL;
825783d3ff6SMartin Matuska 		aiter.iter_page_doff = 0;
826783d3ff6SMartin Matuska 		aiter.iter_page_dsize = 0;
827783d3ff6SMartin Matuska 
828783d3ff6SMartin Matuska 		if (ret != 0)
829783d3ff6SMartin Matuska 			break;
830783d3ff6SMartin Matuska 
831783d3ff6SMartin Matuska 		size -= len;
832783d3ff6SMartin Matuska 		c_abd = abd_advance_abd_iter(abd, c_abd, &aiter, len);
833783d3ff6SMartin Matuska 	}
834783d3ff6SMartin Matuska 
835783d3ff6SMartin Matuska 	return (ret);
836783d3ff6SMartin Matuska }
837783d3ff6SMartin Matuska #endif
838783d3ff6SMartin Matuska 
839eda14cbcSMatt Macy struct buf_arg {
840eda14cbcSMatt Macy 	void *arg_buf;
841eda14cbcSMatt Macy };
842eda14cbcSMatt Macy 
843eda14cbcSMatt Macy static int
844eda14cbcSMatt Macy abd_copy_to_buf_off_cb(void *buf, size_t size, void *private)
845eda14cbcSMatt Macy {
846eda14cbcSMatt Macy 	struct buf_arg *ba_ptr = private;
847eda14cbcSMatt Macy 
848eda14cbcSMatt Macy 	(void) memcpy(ba_ptr->arg_buf, buf, size);
849eda14cbcSMatt Macy 	ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size;
850eda14cbcSMatt Macy 
851eda14cbcSMatt Macy 	return (0);
852eda14cbcSMatt Macy }
853eda14cbcSMatt Macy 
854eda14cbcSMatt Macy /*
855eda14cbcSMatt Macy  * Copy abd to buf. (off is the offset in abd.)
856eda14cbcSMatt Macy  */
857eda14cbcSMatt Macy void
858eda14cbcSMatt Macy abd_copy_to_buf_off(void *buf, abd_t *abd, size_t off, size_t size)
859eda14cbcSMatt Macy {
860eda14cbcSMatt Macy 	struct buf_arg ba_ptr = { buf };
861eda14cbcSMatt Macy 
862eda14cbcSMatt Macy 	(void) abd_iterate_func(abd, off, size, abd_copy_to_buf_off_cb,
863eda14cbcSMatt Macy 	    &ba_ptr);
864eda14cbcSMatt Macy }
865eda14cbcSMatt Macy 
866eda14cbcSMatt Macy static int
867eda14cbcSMatt Macy abd_cmp_buf_off_cb(void *buf, size_t size, void *private)
868eda14cbcSMatt Macy {
869eda14cbcSMatt Macy 	int ret;
870eda14cbcSMatt Macy 	struct buf_arg *ba_ptr = private;
871eda14cbcSMatt Macy 
872eda14cbcSMatt Macy 	ret = memcmp(buf, ba_ptr->arg_buf, size);
873eda14cbcSMatt Macy 	ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size;
874eda14cbcSMatt Macy 
875eda14cbcSMatt Macy 	return (ret);
876eda14cbcSMatt Macy }
877eda14cbcSMatt Macy 
878eda14cbcSMatt Macy /*
879eda14cbcSMatt Macy  * Compare the contents of abd to buf. (off is the offset in abd.)
880eda14cbcSMatt Macy  */
881eda14cbcSMatt Macy int
882eda14cbcSMatt Macy abd_cmp_buf_off(abd_t *abd, const void *buf, size_t off, size_t size)
883eda14cbcSMatt Macy {
884eda14cbcSMatt Macy 	struct buf_arg ba_ptr = { (void *) buf };
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy 	return (abd_iterate_func(abd, off, size, abd_cmp_buf_off_cb, &ba_ptr));
887eda14cbcSMatt Macy }
888eda14cbcSMatt Macy 
889eda14cbcSMatt Macy static int
890eda14cbcSMatt Macy abd_copy_from_buf_off_cb(void *buf, size_t size, void *private)
891eda14cbcSMatt Macy {
892eda14cbcSMatt Macy 	struct buf_arg *ba_ptr = private;
893eda14cbcSMatt Macy 
894eda14cbcSMatt Macy 	(void) memcpy(buf, ba_ptr->arg_buf, size);
895eda14cbcSMatt Macy 	ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size;
896eda14cbcSMatt Macy 
897eda14cbcSMatt Macy 	return (0);
898eda14cbcSMatt Macy }
899eda14cbcSMatt Macy 
900eda14cbcSMatt Macy /*
901eda14cbcSMatt Macy  * Copy from buf to abd. (off is the offset in abd.)
902eda14cbcSMatt Macy  */
903eda14cbcSMatt Macy void
904eda14cbcSMatt Macy abd_copy_from_buf_off(abd_t *abd, const void *buf, size_t off, size_t size)
905eda14cbcSMatt Macy {
906eda14cbcSMatt Macy 	struct buf_arg ba_ptr = { (void *) buf };
907eda14cbcSMatt Macy 
908eda14cbcSMatt Macy 	(void) abd_iterate_func(abd, off, size, abd_copy_from_buf_off_cb,
909eda14cbcSMatt Macy 	    &ba_ptr);
910eda14cbcSMatt Macy }
911eda14cbcSMatt Macy 
912eda14cbcSMatt Macy static int
913eda14cbcSMatt Macy abd_zero_off_cb(void *buf, size_t size, void *private)
914eda14cbcSMatt Macy {
915e92ffd9bSMartin Matuska 	(void) private;
916eda14cbcSMatt Macy 	(void) memset(buf, 0, size);
917eda14cbcSMatt Macy 	return (0);
918eda14cbcSMatt Macy }
919eda14cbcSMatt Macy 
920eda14cbcSMatt Macy /*
921eda14cbcSMatt Macy  * Zero out the abd from a particular offset to the end.
922eda14cbcSMatt Macy  */
923eda14cbcSMatt Macy void
924eda14cbcSMatt Macy abd_zero_off(abd_t *abd, size_t off, size_t size)
925eda14cbcSMatt Macy {
926eda14cbcSMatt Macy 	(void) abd_iterate_func(abd, off, size, abd_zero_off_cb, NULL);
927eda14cbcSMatt Macy }
928eda14cbcSMatt Macy 
929eda14cbcSMatt Macy /*
930eda14cbcSMatt Macy  * Iterate over two ABDs and call func incrementally on the two ABDs' data in
931eda14cbcSMatt Macy  * equal-sized chunks (passed to func as raw buffers). func could be called many
932eda14cbcSMatt Macy  * times during this iteration.
933eda14cbcSMatt Macy  */
934eda14cbcSMatt Macy int
935eda14cbcSMatt Macy abd_iterate_func2(abd_t *dabd, abd_t *sabd, size_t doff, size_t soff,
936eda14cbcSMatt Macy     size_t size, abd_iter_func2_t *func, void *private)
937eda14cbcSMatt Macy {
938eda14cbcSMatt Macy 	int ret = 0;
939eda14cbcSMatt Macy 	struct abd_iter daiter, saiter;
940eda14cbcSMatt Macy 	abd_t *c_dabd, *c_sabd;
941eda14cbcSMatt Macy 
9427877fdebSMatt Macy 	if (size == 0)
9437877fdebSMatt Macy 		return (0);
9447877fdebSMatt Macy 
945eda14cbcSMatt Macy 	abd_verify(dabd);
946eda14cbcSMatt Macy 	abd_verify(sabd);
947eda14cbcSMatt Macy 
948eda14cbcSMatt Macy 	ASSERT3U(doff + size, <=, dabd->abd_size);
949eda14cbcSMatt Macy 	ASSERT3U(soff + size, <=, sabd->abd_size);
950eda14cbcSMatt Macy 
951eda14cbcSMatt Macy 	c_dabd = abd_init_abd_iter(dabd, &daiter, doff);
952eda14cbcSMatt Macy 	c_sabd = abd_init_abd_iter(sabd, &saiter, soff);
953eda14cbcSMatt Macy 
954eda14cbcSMatt Macy 	while (size > 0) {
9556c1e79dfSMartin Matuska 		IMPLY(abd_is_gang(dabd), c_dabd != NULL);
9566c1e79dfSMartin Matuska 		IMPLY(abd_is_gang(sabd), c_sabd != NULL);
957eda14cbcSMatt Macy 
958eda14cbcSMatt Macy 		abd_iter_map(&daiter);
959eda14cbcSMatt Macy 		abd_iter_map(&saiter);
960eda14cbcSMatt Macy 
961eda14cbcSMatt Macy 		size_t dlen = MIN(daiter.iter_mapsize, size);
962eda14cbcSMatt Macy 		size_t slen = MIN(saiter.iter_mapsize, size);
963eda14cbcSMatt Macy 		size_t len = MIN(dlen, slen);
964eda14cbcSMatt Macy 		ASSERT(dlen > 0 || slen > 0);
965eda14cbcSMatt Macy 
966eda14cbcSMatt Macy 		ret = func(daiter.iter_mapaddr, saiter.iter_mapaddr, len,
967eda14cbcSMatt Macy 		    private);
968eda14cbcSMatt Macy 
969eda14cbcSMatt Macy 		abd_iter_unmap(&saiter);
970eda14cbcSMatt Macy 		abd_iter_unmap(&daiter);
971eda14cbcSMatt Macy 
972eda14cbcSMatt Macy 		if (ret != 0)
973eda14cbcSMatt Macy 			break;
974eda14cbcSMatt Macy 
975eda14cbcSMatt Macy 		size -= len;
976eda14cbcSMatt Macy 		c_dabd =
977eda14cbcSMatt Macy 		    abd_advance_abd_iter(dabd, c_dabd, &daiter, len);
978eda14cbcSMatt Macy 		c_sabd =
979eda14cbcSMatt Macy 		    abd_advance_abd_iter(sabd, c_sabd, &saiter, len);
980eda14cbcSMatt Macy 	}
981eda14cbcSMatt Macy 
982eda14cbcSMatt Macy 	return (ret);
983eda14cbcSMatt Macy }
984eda14cbcSMatt Macy 
985eda14cbcSMatt Macy static int
986eda14cbcSMatt Macy abd_copy_off_cb(void *dbuf, void *sbuf, size_t size, void *private)
987eda14cbcSMatt Macy {
988e92ffd9bSMartin Matuska 	(void) private;
989eda14cbcSMatt Macy 	(void) memcpy(dbuf, sbuf, size);
990eda14cbcSMatt Macy 	return (0);
991eda14cbcSMatt Macy }
992eda14cbcSMatt Macy 
993eda14cbcSMatt Macy /*
994eda14cbcSMatt Macy  * Copy from sabd to dabd starting from soff and doff.
995eda14cbcSMatt Macy  */
996eda14cbcSMatt Macy void
997eda14cbcSMatt Macy abd_copy_off(abd_t *dabd, abd_t *sabd, size_t doff, size_t soff, size_t size)
998eda14cbcSMatt Macy {
999eda14cbcSMatt Macy 	(void) abd_iterate_func2(dabd, sabd, doff, soff, size,
1000eda14cbcSMatt Macy 	    abd_copy_off_cb, NULL);
1001eda14cbcSMatt Macy }
1002eda14cbcSMatt Macy 
1003eda14cbcSMatt Macy static int
1004eda14cbcSMatt Macy abd_cmp_cb(void *bufa, void *bufb, size_t size, void *private)
1005eda14cbcSMatt Macy {
1006e92ffd9bSMartin Matuska 	(void) private;
1007eda14cbcSMatt Macy 	return (memcmp(bufa, bufb, size));
1008eda14cbcSMatt Macy }
1009eda14cbcSMatt Macy 
1010eda14cbcSMatt Macy /*
1011eda14cbcSMatt Macy  * Compares the contents of two ABDs.
1012eda14cbcSMatt Macy  */
1013eda14cbcSMatt Macy int
1014eda14cbcSMatt Macy abd_cmp(abd_t *dabd, abd_t *sabd)
1015eda14cbcSMatt Macy {
1016eda14cbcSMatt Macy 	ASSERT3U(dabd->abd_size, ==, sabd->abd_size);
1017eda14cbcSMatt Macy 	return (abd_iterate_func2(dabd, sabd, 0, 0, dabd->abd_size,
1018eda14cbcSMatt Macy 	    abd_cmp_cb, NULL));
1019eda14cbcSMatt Macy }
1020eda14cbcSMatt Macy 
1021eda14cbcSMatt Macy /*
1022ce4dcb97SMartin Matuska  * Check if ABD content is all-zeroes.
1023ce4dcb97SMartin Matuska  */
1024ce4dcb97SMartin Matuska static int
1025ce4dcb97SMartin Matuska abd_cmp_zero_off_cb(void *data, size_t len, void *private)
1026ce4dcb97SMartin Matuska {
1027ce4dcb97SMartin Matuska 	(void) private;
1028ce4dcb97SMartin Matuska 
1029ce4dcb97SMartin Matuska 	/* This function can only check whole uint64s. Enforce that. */
1030ce4dcb97SMartin Matuska 	ASSERT0(P2PHASE(len, 8));
1031ce4dcb97SMartin Matuska 
1032ce4dcb97SMartin Matuska 	uint64_t *end = (uint64_t *)((char *)data + len);
1033ce4dcb97SMartin Matuska 	for (uint64_t *word = (uint64_t *)data; word < end; word++)
1034ce4dcb97SMartin Matuska 		if (*word != 0)
1035ce4dcb97SMartin Matuska 			return (1);
1036ce4dcb97SMartin Matuska 
1037ce4dcb97SMartin Matuska 	return (0);
1038ce4dcb97SMartin Matuska }
1039ce4dcb97SMartin Matuska 
1040ce4dcb97SMartin Matuska int
1041ce4dcb97SMartin Matuska abd_cmp_zero_off(abd_t *abd, size_t off, size_t size)
1042ce4dcb97SMartin Matuska {
1043ce4dcb97SMartin Matuska 	return (abd_iterate_func(abd, off, size, abd_cmp_zero_off_cb, NULL));
1044ce4dcb97SMartin Matuska }
1045ce4dcb97SMartin Matuska 
1046ce4dcb97SMartin Matuska /*
1047eda14cbcSMatt Macy  * Iterate over code ABDs and a data ABD and call @func_raidz_gen.
1048eda14cbcSMatt Macy  *
1049eda14cbcSMatt Macy  * @cabds          parity ABDs, must have equal size
1050eda14cbcSMatt Macy  * @dabd           data ABD. Can be NULL (in this case @dsize = 0)
1051eda14cbcSMatt Macy  * @func_raidz_gen should be implemented so that its behaviour
1052eda14cbcSMatt Macy  *                 is the same when taking linear and when taking scatter
1053eda14cbcSMatt Macy  */
1054eda14cbcSMatt Macy void
1055f8b1db88SMartin Matuska abd_raidz_gen_iterate(abd_t **cabds, abd_t *dabd, size_t off,
1056f8b1db88SMartin Matuska     size_t csize, size_t dsize, const unsigned parity,
1057eda14cbcSMatt Macy     void (*func_raidz_gen)(void **, const void *, size_t, size_t))
1058eda14cbcSMatt Macy {
1059eda14cbcSMatt Macy 	int i;
1060f8b1db88SMartin Matuska 	size_t len, dlen;
1061eda14cbcSMatt Macy 	struct abd_iter caiters[3];
10626c1e79dfSMartin Matuska 	struct abd_iter daiter;
106314c2e0a0SMartin Matuska 	void *caddrs[3], *daddr;
1064eda14cbcSMatt Macy 	unsigned long flags __maybe_unused = 0;
1065eda14cbcSMatt Macy 	abd_t *c_cabds[3];
1066eda14cbcSMatt Macy 	abd_t *c_dabd = NULL;
1067eda14cbcSMatt Macy 
1068eda14cbcSMatt Macy 	ASSERT3U(parity, <=, 3);
1069eda14cbcSMatt Macy 	for (i = 0; i < parity; i++) {
10706c1e79dfSMartin Matuska 		abd_verify(cabds[i]);
1071f8b1db88SMartin Matuska 		ASSERT3U(off + csize, <=, cabds[i]->abd_size);
1072f8b1db88SMartin Matuska 		c_cabds[i] = abd_init_abd_iter(cabds[i], &caiters[i], off);
1073eda14cbcSMatt Macy 	}
1074eda14cbcSMatt Macy 
10756c1e79dfSMartin Matuska 	if (dsize > 0) {
10766c1e79dfSMartin Matuska 		ASSERT(dabd);
10776c1e79dfSMartin Matuska 		abd_verify(dabd);
1078f8b1db88SMartin Matuska 		ASSERT3U(off + dsize, <=, dabd->abd_size);
1079f8b1db88SMartin Matuska 		c_dabd = abd_init_abd_iter(dabd, &daiter, off);
1080eda14cbcSMatt Macy 	}
1081eda14cbcSMatt Macy 
1082eda14cbcSMatt Macy 	abd_enter_critical(flags);
1083eda14cbcSMatt Macy 	while (csize > 0) {
10846c1e79dfSMartin Matuska 		len = csize;
1085eda14cbcSMatt Macy 		for (i = 0; i < parity; i++) {
10866c1e79dfSMartin Matuska 			IMPLY(abd_is_gang(cabds[i]), c_cabds[i] != NULL);
1087eda14cbcSMatt Macy 			abd_iter_map(&caiters[i]);
1088eda14cbcSMatt Macy 			caddrs[i] = caiters[i].iter_mapaddr;
10896c1e79dfSMartin Matuska 			len = MIN(caiters[i].iter_mapsize, len);
1090eda14cbcSMatt Macy 		}
1091eda14cbcSMatt Macy 
10926c1e79dfSMartin Matuska 		if (dsize > 0) {
10936c1e79dfSMartin Matuska 			IMPLY(abd_is_gang(dabd), c_dabd != NULL);
1094eda14cbcSMatt Macy 			abd_iter_map(&daiter);
109514c2e0a0SMartin Matuska 			daddr = daiter.iter_mapaddr;
1096eda14cbcSMatt Macy 			len = MIN(daiter.iter_mapsize, len);
1097eda14cbcSMatt Macy 			dlen = len;
109814c2e0a0SMartin Matuska 		} else {
109914c2e0a0SMartin Matuska 			daddr = NULL;
1100eda14cbcSMatt Macy 			dlen = 0;
110114c2e0a0SMartin Matuska 		}
1102eda14cbcSMatt Macy 
1103eda14cbcSMatt Macy 		/* must be progressive */
1104f8b1db88SMartin Matuska 		ASSERT3U(len, >, 0);
1105eda14cbcSMatt Macy 		/*
1106eda14cbcSMatt Macy 		 * The iterated function likely will not do well if each
1107eda14cbcSMatt Macy 		 * segment except the last one is not multiple of 512 (raidz).
1108eda14cbcSMatt Macy 		 */
1109eda14cbcSMatt Macy 		ASSERT3U(((uint64_t)len & 511ULL), ==, 0);
1110eda14cbcSMatt Macy 
111114c2e0a0SMartin Matuska 		func_raidz_gen(caddrs, daddr, len, dlen);
1112eda14cbcSMatt Macy 
1113eda14cbcSMatt Macy 		for (i = parity-1; i >= 0; i--) {
1114eda14cbcSMatt Macy 			abd_iter_unmap(&caiters[i]);
1115eda14cbcSMatt Macy 			c_cabds[i] =
1116eda14cbcSMatt Macy 			    abd_advance_abd_iter(cabds[i], c_cabds[i],
1117eda14cbcSMatt Macy 			    &caiters[i], len);
1118eda14cbcSMatt Macy 		}
1119eda14cbcSMatt Macy 
11206c1e79dfSMartin Matuska 		if (dsize > 0) {
1121eda14cbcSMatt Macy 			abd_iter_unmap(&daiter);
1122eda14cbcSMatt Macy 			c_dabd =
1123eda14cbcSMatt Macy 			    abd_advance_abd_iter(dabd, c_dabd, &daiter,
1124eda14cbcSMatt Macy 			    dlen);
1125eda14cbcSMatt Macy 			dsize -= dlen;
1126eda14cbcSMatt Macy 		}
1127eda14cbcSMatt Macy 
1128eda14cbcSMatt Macy 		csize -= len;
1129eda14cbcSMatt Macy 	}
1130eda14cbcSMatt Macy 	abd_exit_critical(flags);
1131eda14cbcSMatt Macy }
1132eda14cbcSMatt Macy 
1133eda14cbcSMatt Macy /*
1134eda14cbcSMatt Macy  * Iterate over code ABDs and data reconstruction target ABDs and call
1135eda14cbcSMatt Macy  * @func_raidz_rec. Function maps at most 6 pages atomically.
1136eda14cbcSMatt Macy  *
1137eda14cbcSMatt Macy  * @cabds           parity ABDs, must have equal size
1138eda14cbcSMatt Macy  * @tabds           rec target ABDs, at most 3
1139eda14cbcSMatt Macy  * @tsize           size of data target columns
1140eda14cbcSMatt Macy  * @func_raidz_rec  expects syndrome data in target columns. Function
1141eda14cbcSMatt Macy  *                  reconstructs data and overwrites target columns.
1142eda14cbcSMatt Macy  */
1143eda14cbcSMatt Macy void
1144eda14cbcSMatt Macy abd_raidz_rec_iterate(abd_t **cabds, abd_t **tabds,
1145f8b1db88SMartin Matuska     size_t tsize, const unsigned parity,
1146eda14cbcSMatt Macy     void (*func_raidz_rec)(void **t, const size_t tsize, void **c,
1147eda14cbcSMatt Macy     const unsigned *mul),
1148eda14cbcSMatt Macy     const unsigned *mul)
1149eda14cbcSMatt Macy {
1150eda14cbcSMatt Macy 	int i;
1151f8b1db88SMartin Matuska 	size_t len;
1152eda14cbcSMatt Macy 	struct abd_iter citers[3];
1153eda14cbcSMatt Macy 	struct abd_iter xiters[3];
1154eda14cbcSMatt Macy 	void *caddrs[3], *xaddrs[3];
1155eda14cbcSMatt Macy 	unsigned long flags __maybe_unused = 0;
1156eda14cbcSMatt Macy 	abd_t *c_cabds[3];
1157eda14cbcSMatt Macy 	abd_t *c_tabds[3];
1158eda14cbcSMatt Macy 
1159eda14cbcSMatt Macy 	ASSERT3U(parity, <=, 3);
1160eda14cbcSMatt Macy 
1161eda14cbcSMatt Macy 	for (i = 0; i < parity; i++) {
11626c1e79dfSMartin Matuska 		abd_verify(cabds[i]);
11636c1e79dfSMartin Matuska 		abd_verify(tabds[i]);
11646c1e79dfSMartin Matuska 		ASSERT3U(tsize, <=, cabds[i]->abd_size);
11656c1e79dfSMartin Matuska 		ASSERT3U(tsize, <=, tabds[i]->abd_size);
1166eda14cbcSMatt Macy 		c_cabds[i] =
1167eda14cbcSMatt Macy 		    abd_init_abd_iter(cabds[i], &citers[i], 0);
1168eda14cbcSMatt Macy 		c_tabds[i] =
1169eda14cbcSMatt Macy 		    abd_init_abd_iter(tabds[i], &xiters[i], 0);
1170eda14cbcSMatt Macy 	}
1171eda14cbcSMatt Macy 
1172eda14cbcSMatt Macy 	abd_enter_critical(flags);
1173eda14cbcSMatt Macy 	while (tsize > 0) {
11746c1e79dfSMartin Matuska 		len = tsize;
1175eda14cbcSMatt Macy 		for (i = 0; i < parity; i++) {
11766c1e79dfSMartin Matuska 			IMPLY(abd_is_gang(cabds[i]), c_cabds[i] != NULL);
11776c1e79dfSMartin Matuska 			IMPLY(abd_is_gang(tabds[i]), c_tabds[i] != NULL);
1178eda14cbcSMatt Macy 			abd_iter_map(&citers[i]);
1179eda14cbcSMatt Macy 			abd_iter_map(&xiters[i]);
1180eda14cbcSMatt Macy 			caddrs[i] = citers[i].iter_mapaddr;
1181eda14cbcSMatt Macy 			xaddrs[i] = xiters[i].iter_mapaddr;
11826c1e79dfSMartin Matuska 			len = MIN(citers[i].iter_mapsize, len);
11836c1e79dfSMartin Matuska 			len = MIN(xiters[i].iter_mapsize, len);
1184eda14cbcSMatt Macy 		}
1185eda14cbcSMatt Macy 
1186eda14cbcSMatt Macy 		/* must be progressive */
1187eda14cbcSMatt Macy 		ASSERT3S(len, >, 0);
1188eda14cbcSMatt Macy 		/*
1189eda14cbcSMatt Macy 		 * The iterated function likely will not do well if each
1190eda14cbcSMatt Macy 		 * segment except the last one is not multiple of 512 (raidz).
1191eda14cbcSMatt Macy 		 */
1192eda14cbcSMatt Macy 		ASSERT3U(((uint64_t)len & 511ULL), ==, 0);
1193eda14cbcSMatt Macy 
1194eda14cbcSMatt Macy 		func_raidz_rec(xaddrs, len, caddrs, mul);
1195eda14cbcSMatt Macy 
1196eda14cbcSMatt Macy 		for (i = parity-1; i >= 0; i--) {
1197eda14cbcSMatt Macy 			abd_iter_unmap(&xiters[i]);
1198eda14cbcSMatt Macy 			abd_iter_unmap(&citers[i]);
1199eda14cbcSMatt Macy 			c_tabds[i] =
1200eda14cbcSMatt Macy 			    abd_advance_abd_iter(tabds[i], c_tabds[i],
1201eda14cbcSMatt Macy 			    &xiters[i], len);
1202eda14cbcSMatt Macy 			c_cabds[i] =
1203eda14cbcSMatt Macy 			    abd_advance_abd_iter(cabds[i], c_cabds[i],
1204eda14cbcSMatt Macy 			    &citers[i], len);
1205eda14cbcSMatt Macy 		}
1206eda14cbcSMatt Macy 
1207eda14cbcSMatt Macy 		tsize -= len;
1208eda14cbcSMatt Macy 		ASSERT3S(tsize, >=, 0);
1209eda14cbcSMatt Macy 	}
1210eda14cbcSMatt Macy 	abd_exit_critical(flags);
1211eda14cbcSMatt Macy }
1212