1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2014 by Chunwei Chen. All rights reserved.
14  * Copyright (c) 2016 by Delphix. All rights reserved.
15  */
16 
17 #ifndef _ABD_H
18 #define	_ABD_H
19 
20 #include <sys/isa_defs.h>
21 #ifdef illumos
22 #include <sys/int_types.h>
23 #else
24 #include <sys/stdint.h>
25 #endif
26 #include <sys/debug.h>
27 #include <sys/refcount.h>
28 #ifdef _KERNEL
29 #include <sys/uio.h>
30 #endif
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 typedef enum abd_flags {
37 	ABD_FLAG_LINEAR	= 1 << 0,	/* is buffer linear (or scattered)? */
38 	ABD_FLAG_OWNER	= 1 << 1,	/* does it own its data buffers? */
39 	ABD_FLAG_META	= 1 << 2	/* does this represent FS metadata? */
40 } abd_flags_t;
41 
42 typedef struct abd {
43 	abd_flags_t	abd_flags;
44 	uint_t		abd_size;	/* excludes scattered abd_offset */
45 	struct abd	*abd_parent;
46 	refcount_t	abd_children;
47 	union {
48 		struct abd_scatter {
49 			uint_t	abd_offset;
50 			uint_t	abd_chunk_size;
51 			void	*abd_chunks[];
52 		} abd_scatter;
53 		struct abd_linear {
54 			void	*abd_buf;
55 		} abd_linear;
56 	} abd_u;
57 } abd_t;
58 
59 typedef int abd_iter_func_t(void *, size_t, void *);
60 typedef int abd_iter_func2_t(void *, void *, size_t, void *);
61 
62 extern boolean_t zfs_abd_scatter_enabled;
63 
64 inline boolean_t
abd_is_linear(abd_t * abd)65 abd_is_linear(abd_t *abd)
66 {
67 	return ((abd->abd_flags & ABD_FLAG_LINEAR) != 0 ? B_TRUE : B_FALSE);
68 }
69 
70 /*
71  * Allocations and deallocations
72  */
73 
74 abd_t *abd_alloc(size_t, boolean_t);
75 abd_t *abd_alloc_linear(size_t, boolean_t);
76 abd_t *abd_alloc_for_io(size_t, boolean_t);
77 abd_t *abd_alloc_sametype(abd_t *, size_t);
78 void abd_free(abd_t *);
79 abd_t *abd_get_offset(abd_t *, size_t);
80 abd_t *abd_get_from_buf(void *, size_t);
81 void abd_put(abd_t *);
82 
83 /*
84  * Conversion to and from a normal buffer
85  */
86 
87 void *abd_to_buf(abd_t *);
88 void *abd_borrow_buf(abd_t *, size_t);
89 void *abd_borrow_buf_copy(abd_t *, size_t);
90 void abd_return_buf(abd_t *, void *, size_t);
91 void abd_return_buf_copy(abd_t *, void *, size_t);
92 void abd_take_ownership_of_buf(abd_t *, boolean_t);
93 void abd_release_ownership_of_buf(abd_t *);
94 
95 /*
96  * ABD operations
97  */
98 
99 int abd_iterate_func(abd_t *, size_t, size_t, abd_iter_func_t *, void *);
100 int abd_iterate_func2(abd_t *, abd_t *, size_t, size_t, size_t,
101     abd_iter_func2_t *, void *);
102 void abd_copy_off(abd_t *, abd_t *, size_t, size_t, size_t);
103 void abd_copy_from_buf_off(abd_t *, const void *, size_t, size_t);
104 void abd_copy_to_buf_off(void *, abd_t *, size_t, size_t);
105 int abd_cmp(abd_t *, abd_t *, size_t);
106 int abd_cmp_buf_off(abd_t *, const void *, size_t, size_t);
107 void abd_zero_off(abd_t *, size_t, size_t);
108 
109 /*
110  * Wrappers for calls with offsets of 0
111  */
112 
113 inline void
abd_copy(abd_t * dabd,abd_t * sabd,size_t size)114 abd_copy(abd_t *dabd, abd_t *sabd, size_t size)
115 {
116 	abd_copy_off(dabd, sabd, 0, 0, size);
117 }
118 
119 inline void
abd_copy_from_buf(abd_t * abd,const void * buf,size_t size)120 abd_copy_from_buf(abd_t *abd, const void *buf, size_t size)
121 {
122 	abd_copy_from_buf_off(abd, buf, 0, size);
123 }
124 
125 inline void
abd_copy_to_buf(void * buf,abd_t * abd,size_t size)126 abd_copy_to_buf(void* buf, abd_t *abd, size_t size)
127 {
128 	abd_copy_to_buf_off(buf, abd, 0, size);
129 }
130 
131 inline int
abd_cmp_buf(abd_t * abd,const void * buf,size_t size)132 abd_cmp_buf(abd_t *abd, const void *buf, size_t size)
133 {
134 	return (abd_cmp_buf_off(abd, buf, 0, size));
135 }
136 
137 inline void
abd_zero(abd_t * abd,size_t size)138 abd_zero(abd_t *abd, size_t size)
139 {
140 	abd_zero_off(abd, 0, size);
141 }
142 
143 /*
144  * Module lifecycle
145  */
146 
147 void abd_init(void);
148 void abd_fini(void);
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #endif	/* _ABD_H */
155