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