xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/abd.h (revision e914ace2)
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_from_buf(void *, size_t);
77 void abd_put(abd_t *);
78 
79 /*
80  * Conversion to and from a normal buffer
81  */
82 
83 void *abd_to_buf(abd_t *);
84 void *abd_borrow_buf(abd_t *, size_t);
85 void *abd_borrow_buf_copy(abd_t *, size_t);
86 void abd_return_buf(abd_t *, void *, size_t);
87 void abd_return_buf_copy(abd_t *, void *, size_t);
88 void abd_take_ownership_of_buf(abd_t *, boolean_t);
89 void abd_release_ownership_of_buf(abd_t *);
90 
91 /*
92  * ABD operations
93  */
94 
95 int abd_iterate_func(abd_t *, size_t, size_t, abd_iter_func_t *, void *);
96 int abd_iterate_func2(abd_t *, abd_t *, size_t, size_t, size_t,
97     abd_iter_func2_t *, void *);
98 void abd_copy_off(abd_t *, abd_t *, size_t, size_t, size_t);
99 void abd_copy_from_buf_off(abd_t *, const void *, size_t, size_t);
100 void abd_copy_to_buf_off(void *, abd_t *, size_t, size_t);
101 int abd_cmp(abd_t *, abd_t *, size_t);
102 int abd_cmp_buf_off(abd_t *, const void *, size_t, size_t);
103 void abd_zero_off(abd_t *, size_t, size_t);
104 
105 /*
106  * Wrappers for calls with offsets of 0
107  */
108 
109 inline void
110 abd_copy(abd_t *dabd, abd_t *sabd, size_t size)
111 {
112 	abd_copy_off(dabd, sabd, 0, 0, size);
113 }
114 
115 inline void
116 abd_copy_from_buf(abd_t *abd, const void *buf, size_t size)
117 {
118 	abd_copy_from_buf_off(abd, buf, 0, size);
119 }
120 
121 inline void
122 abd_copy_to_buf(void* buf, abd_t *abd, size_t size)
123 {
124 	abd_copy_to_buf_off(buf, abd, 0, size);
125 }
126 
127 inline int
128 abd_cmp_buf(abd_t *abd, const void *buf, size_t size)
129 {
130 	return (abd_cmp_buf_off(abd, buf, 0, size));
131 }
132 
133 inline void
134 abd_zero(abd_t *abd, size_t size)
135 {
136 	abd_zero_off(abd, 0, size);
137 }
138 
139 /*
140  * Module lifecycle
141  */
142 
143 void abd_init(void);
144 void abd_fini(void);
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif	/* _ABD_H */
151