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 http://www.opensolaris.org/os/licensing.
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
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38typedef enum abd_flags {
39	ABD_FLAG_LINEAR		= 1 << 0, /* is buffer linear (or scattered)? */
40	ABD_FLAG_OWNER		= 1 << 1, /* does it own its data buffers? */
41	ABD_FLAG_META		= 1 << 2, /* does this represent FS metadata? */
42	ABD_FLAG_MULTI_ZONE  	= 1 << 3, /* pages split over memory zones */
43	ABD_FLAG_MULTI_CHUNK 	= 1 << 4, /* pages split over multiple chunks */
44	ABD_FLAG_LINEAR_PAGE 	= 1 << 5, /* linear but allocd from page */
45	ABD_FLAG_GANG		= 1 << 6, /* mult ABDs chained together */
46	ABD_FLAG_GANG_FREE	= 1 << 7, /* gang ABD is responsible for mem */
47	ABD_FLAG_ZEROS		= 1 << 8, /* ABD for zero-filled buffer */
48	ABD_FLAG_ALLOCD		= 1 << 9, /* we allocated the abd_t */
49} abd_flags_t;
50
51typedef struct abd {
52	abd_flags_t	abd_flags;
53	uint_t		abd_size;	/* excludes scattered abd_offset */
54	list_node_t	abd_gang_link;
55#ifdef ZFS_DEBUG
56	struct abd	*abd_parent;
57	zfs_refcount_t	abd_children;
58#endif
59	kmutex_t	abd_mtx;
60	union {
61		struct abd_scatter {
62			uint_t		abd_offset;
63#if defined(__FreeBSD__) && defined(_KERNEL)
64			uint_t  abd_chunk_size;
65			void    *abd_chunks[1]; /* actually variable-length */
66#else
67			uint_t		abd_nents;
68			struct scatterlist *abd_sgl;
69#endif
70		} abd_scatter;
71		struct abd_linear {
72			void		*abd_buf;
73			struct scatterlist *abd_sgl; /* for LINEAR_PAGE */
74		} abd_linear;
75		struct abd_gang {
76			list_t abd_gang_chain;
77		} abd_gang;
78	} abd_u;
79} abd_t;
80
81typedef int abd_iter_func_t(void *buf, size_t len, void *priv);
82typedef int abd_iter_func2_t(void *bufa, void *bufb, size_t len, void *priv);
83
84extern int zfs_abd_scatter_enabled;
85
86/*
87 * Allocations and deallocations
88 */
89
90abd_t *abd_alloc(size_t, boolean_t);
91abd_t *abd_alloc_linear(size_t, boolean_t);
92abd_t *abd_alloc_gang(void);
93abd_t *abd_alloc_for_io(size_t, boolean_t);
94abd_t *abd_alloc_sametype(abd_t *, size_t);
95void abd_gang_add(abd_t *, abd_t *, boolean_t);
96void abd_free(abd_t *);
97abd_t *abd_get_offset(abd_t *, size_t);
98abd_t *abd_get_offset_size(abd_t *, size_t, size_t);
99abd_t *abd_get_offset_struct(abd_t *, abd_t *, size_t, size_t);
100abd_t *abd_get_zeros(size_t);
101abd_t *abd_get_from_buf(void *, size_t);
102void abd_cache_reap_now(void);
103
104/*
105 * Conversion to and from a normal buffer
106 */
107
108void *abd_to_buf(abd_t *);
109void *abd_borrow_buf(abd_t *, size_t);
110void *abd_borrow_buf_copy(abd_t *, size_t);
111void abd_return_buf(abd_t *, void *, size_t);
112void abd_return_buf_copy(abd_t *, void *, size_t);
113void abd_take_ownership_of_buf(abd_t *, boolean_t);
114void abd_release_ownership_of_buf(abd_t *);
115
116/*
117 * ABD operations
118 */
119
120int abd_iterate_func(abd_t *, size_t, size_t, abd_iter_func_t *, void *);
121int abd_iterate_func2(abd_t *, abd_t *, size_t, size_t, size_t,
122    abd_iter_func2_t *, void *);
123void abd_copy_off(abd_t *, abd_t *, size_t, size_t, size_t);
124void abd_copy_from_buf_off(abd_t *, const void *, size_t, size_t);
125void abd_copy_to_buf_off(void *, abd_t *, size_t, size_t);
126int abd_cmp(abd_t *, abd_t *);
127int abd_cmp_buf_off(abd_t *, const void *, size_t, size_t);
128void abd_zero_off(abd_t *, size_t, size_t);
129void abd_verify(abd_t *);
130
131void abd_raidz_gen_iterate(abd_t **cabds, abd_t *dabd,
132	ssize_t csize, ssize_t dsize, const unsigned parity,
133	void (*func_raidz_gen)(void **, const void *, size_t, size_t));
134void abd_raidz_rec_iterate(abd_t **cabds, abd_t **tabds,
135	ssize_t tsize, const unsigned parity,
136	void (*func_raidz_rec)(void **t, const size_t tsize, void **c,
137	const unsigned *mul),
138	const unsigned *mul);
139
140/*
141 * Wrappers for calls with offsets of 0
142 */
143
144static inline void
145abd_copy(abd_t *dabd, abd_t *sabd, size_t size)
146{
147	abd_copy_off(dabd, sabd, 0, 0, size);
148}
149
150static inline void
151abd_copy_from_buf(abd_t *abd, const void *buf, size_t size)
152{
153	abd_copy_from_buf_off(abd, buf, 0, size);
154}
155
156static inline void
157abd_copy_to_buf(void* buf, abd_t *abd, size_t size)
158{
159	abd_copy_to_buf_off(buf, abd, 0, size);
160}
161
162static inline int
163abd_cmp_buf(abd_t *abd, const void *buf, size_t size)
164{
165	return (abd_cmp_buf_off(abd, buf, 0, size));
166}
167
168static inline void
169abd_zero(abd_t *abd, size_t size)
170{
171	abd_zero_off(abd, 0, size);
172}
173
174/*
175 * ABD type check functions
176 */
177static inline boolean_t
178abd_is_linear(abd_t *abd)
179{
180	return ((abd->abd_flags & ABD_FLAG_LINEAR) ? B_TRUE : B_FALSE);
181}
182
183static inline boolean_t
184abd_is_linear_page(abd_t *abd)
185{
186	return ((abd->abd_flags & ABD_FLAG_LINEAR_PAGE) ? B_TRUE : B_FALSE);
187}
188
189static inline boolean_t
190abd_is_gang(abd_t *abd)
191{
192	return ((abd->abd_flags & ABD_FLAG_GANG) ? B_TRUE : B_FALSE);
193}
194
195static inline uint_t
196abd_get_size(abd_t *abd)
197{
198	return (abd->abd_size);
199}
200
201/*
202 * Module lifecycle
203 * Defined in each specific OS's abd_os.c
204 */
205
206void abd_init(void);
207void abd_fini(void);
208
209/*
210 * Linux ABD bio functions
211 */
212#if defined(__linux__) && defined(_KERNEL)
213unsigned int abd_bio_map_off(struct bio *, abd_t *, unsigned int, size_t);
214unsigned long abd_nr_pages_off(abd_t *, unsigned int, size_t);
215#endif
216
217#ifdef __cplusplus
218}
219#endif
220
221#endif	/* _ABD_H */
222