xref: /freebsd/sys/contrib/openzfs/include/sys/abd.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2014 by Chunwei Chen. All rights reserved.
24  * Copyright (c) 2016, 2019 by Delphix. All rights reserved.
25  */
26 
27 #ifndef _ABD_H
28 #define	_ABD_H
29 
30 #include <sys/isa_defs.h>
31 #include <sys/debug.h>
32 #include <sys/zfs_refcount.h>
33 #include <sys/uio.h>
34 #include <sys/abd_os.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 typedef enum abd_flags {
41 	ABD_FLAG_LINEAR		= 1 << 0, /* is buffer linear (or scattered)? */
42 	ABD_FLAG_OWNER		= 1 << 1, /* does it own its data buffers? */
43 	ABD_FLAG_META		= 1 << 2, /* does this represent FS metadata? */
44 	ABD_FLAG_MULTI_ZONE  	= 1 << 3, /* pages split over memory zones */
45 	ABD_FLAG_MULTI_CHUNK 	= 1 << 4, /* pages split over multiple chunks */
46 	ABD_FLAG_LINEAR_PAGE 	= 1 << 5, /* linear but allocd from page */
47 	ABD_FLAG_GANG		= 1 << 6, /* mult ABDs chained together */
48 	ABD_FLAG_GANG_FREE	= 1 << 7, /* gang ABD is responsible for mem */
49 	ABD_FLAG_ALLOCD		= 1 << 8, /* we allocated the abd_t */
50 	ABD_FLAG_FROM_PAGES	= 1 << 9, /* does not own pages */
51 } abd_flags_t;
52 
53 typedef struct abd {
54 	abd_flags_t	abd_flags;
55 	uint_t		abd_size;	/* excludes scattered abd_offset */
56 	list_node_t	abd_gang_link;
57 #ifdef ZFS_DEBUG
58 	struct abd	*abd_parent;
59 	zfs_refcount_t	abd_children;
60 #endif
61 	kmutex_t	abd_mtx;
62 	union {
63 		struct abd_scatter	abd_scatter;
64 		struct abd_linear	abd_linear;
65 		struct abd_gang {
66 			list_t abd_gang_chain;
67 		} abd_gang;
68 	} abd_u;
69 } abd_t;
70 
71 typedef int abd_iter_func_t(void *buf, size_t len, void *priv);
72 typedef int abd_iter_func2_t(void *bufa, void *bufb, size_t len, void *priv);
73 
74 extern int zfs_abd_scatter_enabled;
75 
76 /*
77  * Allocations and deallocations
78  */
79 
80 __attribute__((malloc))
81 abd_t *abd_alloc(size_t, boolean_t);
82 __attribute__((malloc))
83 abd_t *abd_alloc_linear(size_t, boolean_t);
84 __attribute__((malloc))
85 abd_t *abd_alloc_gang(void);
86 __attribute__((malloc))
87 abd_t *abd_alloc_for_io(size_t, boolean_t);
88 __attribute__((malloc))
89 abd_t *abd_alloc_sametype(abd_t *, size_t);
90 boolean_t abd_size_alloc_linear(size_t);
91 void abd_gang_add(abd_t *, abd_t *, boolean_t);
92 void abd_free(abd_t *);
93 abd_t *abd_get_offset(abd_t *, size_t);
94 abd_t *abd_get_offset_size(abd_t *, size_t, size_t);
95 abd_t *abd_get_offset_struct(abd_t *, abd_t *, size_t, size_t);
96 abd_t *abd_get_zeros(size_t);
97 abd_t *abd_get_from_buf(void *, size_t);
98 abd_t *abd_get_from_buf_struct(abd_t *, void *, size_t);
99 void abd_cache_reap_now(void);
100 
101 /*
102  * Conversion to and from a normal buffer
103  */
104 
105 void *abd_to_buf(abd_t *);
106 void *abd_borrow_buf(abd_t *, size_t);
107 void *abd_borrow_buf_copy(abd_t *, size_t);
108 void abd_return_buf(abd_t *, void *, size_t);
109 void abd_return_buf_copy(abd_t *, void *, size_t);
110 void abd_take_ownership_of_buf(abd_t *, boolean_t);
111 void abd_release_ownership_of_buf(abd_t *);
112 
113 /*
114  * ABD operations
115  */
116 
117 int abd_iterate_func(abd_t *, size_t, size_t, abd_iter_func_t *, void *);
118 int abd_iterate_func2(abd_t *, abd_t *, size_t, size_t, size_t,
119     abd_iter_func2_t *, void *);
120 void abd_copy_off(abd_t *, abd_t *, size_t, size_t, size_t);
121 void abd_copy_from_buf_off(abd_t *, const void *, size_t, size_t);
122 void abd_copy_to_buf_off(void *, abd_t *, size_t, size_t);
123 int abd_cmp(abd_t *, abd_t *);
124 int abd_cmp_buf_off(abd_t *, const void *, size_t, size_t);
125 int abd_cmp_zero_off(abd_t *, size_t, size_t);
126 void abd_zero_off(abd_t *, size_t, size_t);
127 void abd_verify(abd_t *);
128 
129 void abd_raidz_gen_iterate(abd_t **cabds, abd_t *dabd, size_t off,
130 	size_t csize, size_t dsize, const unsigned parity,
131 	void (*func_raidz_gen)(void **, const void *, size_t, size_t));
132 void abd_raidz_rec_iterate(abd_t **cabds, abd_t **tabds,
133 	size_t tsize, const unsigned parity,
134 	void (*func_raidz_rec)(void **t, const size_t tsize, void **c,
135 	const unsigned *mul),
136 	const unsigned *mul);
137 
138 /*
139  * Wrappers for calls with offsets of 0
140  */
141 
142 static inline void
abd_copy(abd_t * dabd,abd_t * sabd,size_t size)143 abd_copy(abd_t *dabd, abd_t *sabd, size_t size)
144 {
145 	abd_copy_off(dabd, sabd, 0, 0, size);
146 }
147 
148 static inline void
abd_copy_from_buf(abd_t * abd,const void * buf,size_t size)149 abd_copy_from_buf(abd_t *abd, const void *buf, size_t size)
150 {
151 	abd_copy_from_buf_off(abd, buf, 0, size);
152 }
153 
154 static inline void
abd_copy_to_buf(void * buf,abd_t * abd,size_t size)155 abd_copy_to_buf(void* buf, abd_t *abd, size_t size)
156 {
157 	abd_copy_to_buf_off(buf, abd, 0, size);
158 }
159 
160 static inline int
abd_cmp_buf(abd_t * abd,const void * buf,size_t size)161 abd_cmp_buf(abd_t *abd, const void *buf, size_t size)
162 {
163 	return (abd_cmp_buf_off(abd, buf, 0, size));
164 }
165 
166 static inline void
abd_zero(abd_t * abd,size_t size)167 abd_zero(abd_t *abd, size_t size)
168 {
169 	abd_zero_off(abd, 0, size);
170 }
171 
172 static inline int
abd_cmp_zero(abd_t * abd,size_t size)173 abd_cmp_zero(abd_t *abd, size_t size)
174 {
175 	return (abd_cmp_zero_off(abd, 0, size));
176 }
177 
178 /*
179  * ABD type check functions
180  */
181 static inline boolean_t
abd_is_linear(abd_t * abd)182 abd_is_linear(abd_t *abd)
183 {
184 	return ((abd->abd_flags & ABD_FLAG_LINEAR) ? B_TRUE : B_FALSE);
185 }
186 
187 static inline boolean_t
abd_is_linear_page(abd_t * abd)188 abd_is_linear_page(abd_t *abd)
189 {
190 	return ((abd->abd_flags & ABD_FLAG_LINEAR_PAGE) ? B_TRUE : B_FALSE);
191 }
192 
193 static inline boolean_t
abd_is_gang(abd_t * abd)194 abd_is_gang(abd_t *abd)
195 {
196 	return ((abd->abd_flags & ABD_FLAG_GANG) ? B_TRUE : B_FALSE);
197 }
198 
199 static inline uint_t
abd_get_size(abd_t * abd)200 abd_get_size(abd_t *abd)
201 {
202 	return (abd->abd_size);
203 }
204 
205 static inline boolean_t
abd_is_from_pages(abd_t * abd)206 abd_is_from_pages(abd_t *abd)
207 {
208 	return ((abd->abd_flags & ABD_FLAG_FROM_PAGES) ? B_TRUE : B_FALSE);
209 }
210 
211 /*
212  * Module lifecycle
213  * Defined in each specific OS's abd_os.c
214  */
215 
216 void abd_init(void);
217 void abd_fini(void);
218 
219 #ifdef __cplusplus
220 }
221 #endif
222 
223 #endif	/* _ABD_H */
224