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 #include <sys/int_types.h>
22 #include <sys/debug.h>
23 #include <sys/refcount.h>
24 #ifdef _KERNEL
25 #include <sys/uio.h>
26 #endif
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 typedef enum abd_flags {
33 ABD_FLAG_LINEAR = 1 << 0, /* is buffer linear (or scattered)? */
34 ABD_FLAG_OWNER = 1 << 1, /* does it own its data buffers? */
35 ABD_FLAG_META = 1 << 2 /* does this represent FS metadata? */
36 } abd_flags_t;
37
38 typedef struct abd {
39 abd_flags_t abd_flags;
40 uint_t abd_size; /* excludes scattered abd_offset */
41 struct abd *abd_parent;
42 zfs_refcount_t abd_children;
43 union {
44 struct abd_scatter {
45 uint_t abd_offset;
46 uint_t abd_chunk_size;
47 void *abd_chunks[];
48 } abd_scatter;
49 struct abd_linear {
50 void *abd_buf;
51 } abd_linear;
52 } abd_u;
53 } abd_t;
54
55 typedef int abd_iter_func_t(void *, size_t, void *);
56 typedef int abd_iter_func2_t(void *, void *, size_t, void *);
57
58 extern boolean_t zfs_abd_scatter_enabled;
59
60 inline boolean_t
abd_is_linear(abd_t * abd)61 abd_is_linear(abd_t *abd)
62 {
63 return ((abd->abd_flags & ABD_FLAG_LINEAR) != 0 ? B_TRUE : B_FALSE);
64 }
65
66 /*
67 * Allocations and deallocations
68 */
69
70 abd_t *abd_alloc(size_t, boolean_t);
71 abd_t *abd_alloc_linear(size_t, boolean_t);
72 abd_t *abd_alloc_for_io(size_t, boolean_t);
73 abd_t *abd_alloc_sametype(abd_t *, size_t);
74 void abd_free(abd_t *);
75 abd_t *abd_get_offset(abd_t *, size_t);
76 abd_t *abd_get_offset_size(abd_t *, size_t, size_t);
77 abd_t *abd_get_from_buf(void *, size_t);
78 void abd_put(abd_t *);
79
80 /*
81 * Conversion to and from a normal buffer
82 */
83
84 void *abd_to_buf(abd_t *);
85 void *abd_borrow_buf(abd_t *, size_t);
86 void *abd_borrow_buf_copy(abd_t *, size_t);
87 void abd_return_buf(abd_t *, void *, size_t);
88 void abd_return_buf_copy(abd_t *, void *, size_t);
89 void abd_take_ownership_of_buf(abd_t *, boolean_t);
90 void abd_release_ownership_of_buf(abd_t *);
91
92 /*
93 * ABD operations
94 */
95
96 int abd_iterate_func(abd_t *, size_t, size_t, abd_iter_func_t *, void *);
97 int abd_iterate_func2(abd_t *, abd_t *, size_t, size_t, size_t,
98 abd_iter_func2_t *, void *);
99 void abd_copy_off(abd_t *, abd_t *, size_t, size_t, size_t);
100 void abd_copy_from_buf_off(abd_t *, const void *, size_t, size_t);
101 void abd_copy_to_buf_off(void *, abd_t *, size_t, size_t);
102 int abd_cmp(abd_t *, abd_t *, size_t);
103 int abd_cmp_buf_off(abd_t *, const void *, size_t, size_t);
104 void abd_zero_off(abd_t *, size_t, size_t);
105
106 void abd_raidz_gen_iterate(abd_t **cabds, abd_t *dabd,
107 ssize_t csize, ssize_t dsize, const unsigned parity,
108 void (*func_raidz_gen)(void **, const void *, size_t, size_t));
109 void abd_raidz_rec_iterate(abd_t **cabds, abd_t **tabds,
110 ssize_t tsize, const unsigned parity,
111 void (*func_raidz_rec)(void **t, const size_t tsize, void **c,
112 const unsigned *mul),
113 const unsigned *mul);
114
115 /*
116 * Wrappers for calls with offsets of 0
117 */
118
119 inline void
abd_copy(abd_t * dabd,abd_t * sabd,size_t size)120 abd_copy(abd_t *dabd, abd_t *sabd, size_t size)
121 {
122 abd_copy_off(dabd, sabd, 0, 0, size);
123 }
124
125 inline void
abd_copy_from_buf(abd_t * abd,const void * buf,size_t size)126 abd_copy_from_buf(abd_t *abd, const void *buf, size_t size)
127 {
128 abd_copy_from_buf_off(abd, buf, 0, size);
129 }
130
131 inline void
abd_copy_to_buf(void * buf,abd_t * abd,size_t size)132 abd_copy_to_buf(void* buf, abd_t *abd, size_t size)
133 {
134 abd_copy_to_buf_off(buf, abd, 0, size);
135 }
136
137 inline int
abd_cmp_buf(abd_t * abd,const void * buf,size_t size)138 abd_cmp_buf(abd_t *abd, const void *buf, size_t size)
139 {
140 return (abd_cmp_buf_off(abd, buf, 0, size));
141 }
142
143 inline void
abd_zero(abd_t * abd,size_t size)144 abd_zero(abd_t *abd, size_t size)
145 {
146 abd_zero_off(abd, 0, size);
147 }
148
149 /*
150 * Module lifecycle
151 */
152
153 void abd_init(void);
154 void abd_fini(void);
155
156 #ifdef __cplusplus
157 }
158 #endif
159
160 #endif /* _ABD_H */
161