xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/abd.h (revision eb633035)
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
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 /*
107  * Wrappers for calls with offsets of 0
108  */
109 
110 inline void
111 abd_copy(abd_t *dabd, abd_t *sabd, size_t size)
112 {
113 	abd_copy_off(dabd, sabd, 0, 0, size);
114 }
115 
116 inline void
117 abd_copy_from_buf(abd_t *abd, const void *buf, size_t size)
118 {
119 	abd_copy_from_buf_off(abd, buf, 0, size);
120 }
121 
122 inline void
123 abd_copy_to_buf(void* buf, abd_t *abd, size_t size)
124 {
125 	abd_copy_to_buf_off(buf, abd, 0, size);
126 }
127 
128 inline int
129 abd_cmp_buf(abd_t *abd, const void *buf, size_t size)
130 {
131 	return (abd_cmp_buf_off(abd, buf, 0, size));
132 }
133 
134 inline void
135 abd_zero(abd_t *abd, size_t size)
136 {
137 	abd_zero_off(abd, 0, size);
138 }
139 
140 /*
141  * Module lifecycle
142  */
143 
144 void abd_init(void);
145 void abd_fini(void);
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif	/* _ABD_H */
152