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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
22 * Copyright (c) 2014 by Chunwei Chen. All rights reserved.
23 * Copyright (c) 2019 by Delphix. All rights reserved.
24 * Copyright (c) 2023, 2024, Klara Inc.
27 #include <sys/abd_impl.h>
28 #include <sys/param.h>
31 #include <sys/zfs_context.h>
32 #include <sys/zfs_znode.h>
35 * We're simulating scatter/gather with 4K allocations, since that's more like
36 * what a typical kernel does.
38 #define ABD_PAGESIZE (4096)
39 #define ABD_PAGESHIFT (12)
40 #define ABD_PAGEMASK (ABD_PAGESIZE-1)
43 * See rationale in module/os/linux/zfs/abd_os.c, but in userspace this is
44 * mostly useful to get a mix of linear and scatter ABDs for testing.
46 #define ABD_SCATTER_MIN_SIZE (512 * 3)
48 abd_t
*abd_zero_scatter
= NULL
;
51 abd_iovcnt_for_bytes(size_t size
)
54 * Each iovec points to a 4K page. There's no real reason to do this
55 * in userspace, but our whole point here is to make it feel a bit
56 * more like a real paged memory model.
58 return (P2ROUNDUP(size
, ABD_PAGESIZE
) / ABD_PAGESIZE
);
62 abd_alloc_struct_impl(size_t size
)
65 * Zero-sized means it will be used for a linear or gang abd, so just
66 * allocate the abd itself and return.
69 return (umem_alloc(sizeof (abd_t
), UMEM_NOFAIL
));
72 * Allocating for a scatter abd, so compute how many ABD_PAGESIZE
73 * iovecs we will need to hold this size. Append that allocation to the
74 * end. Note that struct abd_scatter has includes abd_iov[1], so we
75 * allocate one less iovec than we need.
77 * Note we're not allocating the pages proper, just the iovec pointers.
78 * That's down in abd_alloc_chunks. We _could_ do it here in a single
79 * allocation, but it's fiddly and harder to read for no real gain.
81 uint_t n
= abd_iovcnt_for_bytes(size
);
82 abd_t
*abd
= umem_alloc(sizeof (abd_t
) + (n
-1) * sizeof (struct iovec
),
84 ABD_SCATTER(abd
).abd_offset
= 0;
85 ABD_SCATTER(abd
).abd_iovcnt
= n
;
90 abd_free_struct_impl(abd_t
*abd
)
92 /* For scatter, compute the extra amount we need to free */
94 abd_is_linear(abd
) || abd_is_gang(abd
) ?
95 0 : (ABD_SCATTER(abd
).abd_iovcnt
- 1);
96 umem_free(abd
, sizeof (abd_t
) + iovcnt
* sizeof (struct iovec
));
100 abd_alloc_chunks(abd_t
*abd
, size_t size
)
103 * We've already allocated the iovec array; ensure that the wanted size
104 * actually matches, otherwise the caller has made a mistake somewhere.
106 uint_t n
= ABD_SCATTER(abd
).abd_iovcnt
;
107 ASSERT3U(n
, ==, abd_iovcnt_for_bytes(size
));
110 * Allocate a ABD_PAGESIZE region for each iovec.
112 struct iovec
*iov
= ABD_SCATTER(abd
).abd_iov
;
113 for (int i
= 0; i
< n
; i
++) {
115 umem_alloc_aligned(ABD_PAGESIZE
, ABD_PAGESIZE
, UMEM_NOFAIL
);
116 iov
[i
].iov_len
= ABD_PAGESIZE
;
121 abd_free_chunks(abd_t
*abd
)
123 uint_t n
= ABD_SCATTER(abd
).abd_iovcnt
;
124 struct iovec
*iov
= ABD_SCATTER(abd
).abd_iov
;
125 for (int i
= 0; i
< n
; i
++)
126 umem_free_aligned(iov
[i
].iov_base
, ABD_PAGESIZE
);
130 abd_size_alloc_linear(size_t size
)
132 return (size
< ABD_SCATTER_MIN_SIZE
);
136 abd_update_scatter_stats(abd_t
*abd
, abd_stats_op_t op
)
138 ASSERT(op
== ABDSTAT_INCR
|| op
== ABDSTAT_DECR
);
139 int waste
= P2ROUNDUP(abd
->abd_size
, ABD_PAGESIZE
) - abd
->abd_size
;
140 if (op
== ABDSTAT_INCR
) {
141 arc_space_consume(waste
, ARC_SPACE_ABD_CHUNK_WASTE
);
143 arc_space_return(waste
, ARC_SPACE_ABD_CHUNK_WASTE
);
148 abd_update_linear_stats(abd_t
*abd
, abd_stats_op_t op
)
152 ASSERT(op
== ABDSTAT_INCR
|| op
== ABDSTAT_DECR
);
156 abd_verify_scatter(abd_t
*abd
)
160 * scatter abds shall have:
161 * - at least one iovec
162 * - all iov_base point somewhere
163 * - all iov_len are ABD_PAGESIZE
164 * - offset set within the abd pages somewhere
166 uint_t n
= ABD_SCATTER(abd
).abd_iovcnt
;
170 for (int i
= 0; i
< n
; i
++) {
171 ASSERT3P(ABD_SCATTER(abd
).abd_iov
[i
].iov_base
, !=, NULL
);
172 ASSERT3U(ABD_SCATTER(abd
).abd_iov
[i
].iov_len
, ==, ABD_PAGESIZE
);
176 ASSERT3U(ABD_SCATTER(abd
).abd_offset
, <, len
);
184 * Create the "zero" scatter abd. This is always the size of the
185 * largest possible block, but only actually has a single allocated
186 * page, which all iovecs in the abd point to.
188 abd_zero_scatter
= abd_alloc_struct(SPA_MAXBLOCKSIZE
);
189 abd_zero_scatter
->abd_flags
|= ABD_FLAG_OWNER
;
190 abd_zero_scatter
->abd_size
= SPA_MAXBLOCKSIZE
;
193 umem_alloc_aligned(ABD_PAGESIZE
, ABD_PAGESIZE
, UMEM_NOFAIL
);
194 memset(zero
, 0, ABD_PAGESIZE
);
196 uint_t n
= abd_iovcnt_for_bytes(SPA_MAXBLOCKSIZE
);
197 struct iovec
*iov
= ABD_SCATTER(abd_zero_scatter
).abd_iov
;
198 for (int i
= 0; i
< n
; i
++) {
199 iov
[i
].iov_base
= zero
;
200 iov
[i
].iov_len
= ABD_PAGESIZE
;
208 ABD_SCATTER(abd_zero_scatter
).abd_iov
[0].iov_base
, ABD_PAGESIZE
);
209 abd_free_struct(abd_zero_scatter
);
210 abd_zero_scatter
= NULL
;
214 abd_free_linear_page(abd_t
*abd
)
217 * LINEAR_PAGE is specific to the Linux kernel; we never set this
218 * flag, so this will never be called.
221 PANIC("unreachable");
225 abd_alloc_for_io(size_t size
, boolean_t is_metadata
)
227 return (abd_alloc(size
, is_metadata
));
231 abd_get_offset_scatter(abd_t
*dabd
, abd_t
*sabd
, size_t off
, size_t size
)
235 * Create a new scatter dabd by borrowing data pages from sabd to cover
238 * sabd is an existing scatter abd with a set of iovecs, each covering
239 * an ABD_PAGESIZE (4K) allocation. It's "zero" is at abd_offset.
241 * [........][........][........][........]
244 * We want to produce a new abd, referencing those allocations at the
247 * [........][........][........][........]
248 * ^- dabd_offset = sabd_offset + off
249 * ^- dabd_offset + size
251 * In this example, dabd needs three iovecs. The first iovec is offset
252 * 0, so the final dabd_offset is masked back into the first iovec.
254 * [........][........][........]
257 size_t soff
= ABD_SCATTER(sabd
).abd_offset
+ off
;
258 size_t doff
= soff
& ABD_PAGEMASK
;
259 size_t iovcnt
= abd_iovcnt_for_bytes(doff
+ size
);
262 * If the passed-in abd has enough allocated iovecs already, reuse it.
263 * Otherwise, make a new one. The caller will free the original if the
264 * one it gets back is not the same.
266 * Note that it's ok if we reuse an abd with more iovecs than we need.
267 * abd_size has the usable amount of data, and the abd does not own the
268 * pages referenced by the iovecs. At worst, they're holding dangling
269 * pointers that we'll never use anyway.
271 if (dabd
== NULL
|| ABD_SCATTER(dabd
).abd_iovcnt
< iovcnt
)
272 dabd
= abd_alloc_struct(iovcnt
<< ABD_PAGESHIFT
);
274 /* Set offset into first page in view */
275 ABD_SCATTER(dabd
).abd_offset
= doff
;
277 /* Copy the wanted iovecs from the source to the dest */
278 memcpy(&ABD_SCATTER(dabd
).abd_iov
[0],
279 &ABD_SCATTER(sabd
).abd_iov
[soff
>> ABD_PAGESHIFT
],
280 iovcnt
* sizeof (struct iovec
));
286 abd_iter_init(struct abd_iter
*aiter
, abd_t
*abd
)
288 ASSERT(!abd_is_gang(abd
));
290 memset(aiter
, 0, sizeof (struct abd_iter
));
291 aiter
->iter_abd
= abd
;
295 abd_iter_at_end(struct abd_iter
*aiter
)
297 ASSERT3U(aiter
->iter_pos
, <=, aiter
->iter_abd
->abd_size
);
298 return (aiter
->iter_pos
== aiter
->iter_abd
->abd_size
);
302 abd_iter_advance(struct abd_iter
*aiter
, size_t amount
)
304 ASSERT3P(aiter
->iter_mapaddr
, ==, NULL
);
305 ASSERT0(aiter
->iter_mapsize
);
307 if (abd_iter_at_end(aiter
))
310 aiter
->iter_pos
+= amount
;
311 ASSERT3U(aiter
->iter_pos
, <=, aiter
->iter_abd
->abd_size
);
315 abd_iter_map(struct abd_iter
*aiter
)
317 ASSERT3P(aiter
->iter_mapaddr
, ==, NULL
);
318 ASSERT0(aiter
->iter_mapsize
);
320 if (abd_iter_at_end(aiter
))
323 if (abd_is_linear(aiter
->iter_abd
)) {
324 aiter
->iter_mapaddr
=
325 ABD_LINEAR_BUF(aiter
->iter_abd
) + aiter
->iter_pos
;
326 aiter
->iter_mapsize
=
327 aiter
->iter_abd
->abd_size
- aiter
->iter_pos
;
332 * For scatter, we index into the appropriate iovec, and return the
333 * smaller of the amount requested, or up to the end of the page.
335 size_t poff
= aiter
->iter_pos
+ ABD_SCATTER(aiter
->iter_abd
).abd_offset
;
337 ASSERT3U(poff
>> ABD_PAGESHIFT
, <=,
338 ABD_SCATTER(aiter
->iter_abd
).abd_iovcnt
);
339 struct iovec
*iov
= &ABD_SCATTER(aiter
->iter_abd
).
340 abd_iov
[poff
>> ABD_PAGESHIFT
];
342 aiter
->iter_mapsize
= MIN(ABD_PAGESIZE
- (poff
& ABD_PAGEMASK
),
343 aiter
->iter_abd
->abd_size
- aiter
->iter_pos
);
344 ASSERT3U(aiter
->iter_mapsize
, <=, ABD_PAGESIZE
);
346 aiter
->iter_mapaddr
= iov
->iov_base
+ (poff
& ABD_PAGEMASK
);
350 abd_iter_unmap(struct abd_iter
*aiter
)
352 if (abd_iter_at_end(aiter
))
355 ASSERT3P(aiter
->iter_mapaddr
, !=, NULL
);
356 ASSERT3U(aiter
->iter_mapsize
, >, 0);
358 aiter
->iter_mapaddr
= NULL
;
359 aiter
->iter_mapsize
= 0;
363 abd_cache_reap_now(void)
368 * Borrow a raw buffer from an ABD without copying the contents of the ABD
369 * into the buffer. If the ABD is scattered, this will alloate a raw buffer
370 * whose contents are undefined. To copy over the existing data in the ABD, use
371 * abd_borrow_buf_copy() instead.
374 abd_borrow_buf(abd_t
*abd
, size_t n
)
378 ASSERT3U(abd
->abd_size
, >=, 0);
379 if (abd_is_linear(abd
)) {
380 buf
= abd_to_buf(abd
);
382 buf
= zio_buf_alloc(n
);
385 (void) zfs_refcount_add_many(&abd
->abd_children
, n
, buf
);
391 abd_borrow_buf_copy(abd_t
*abd
, size_t n
)
393 void *buf
= abd_borrow_buf(abd
, n
);
394 if (!abd_is_linear(abd
)) {
395 abd_copy_to_buf(buf
, abd
, n
);
401 * Return a borrowed raw buffer to an ABD. If the ABD is scattered, this will
402 * no change the contents of the ABD and will ASSERT that you didn't modify
403 * the buffer since it was borrowed. If you want any changes you made to buf to
404 * be copied back to abd, use abd_return_buf_copy() instead.
407 abd_return_buf(abd_t
*abd
, void *buf
, size_t n
)
410 ASSERT3U(abd
->abd_size
, >=, n
);
412 (void) zfs_refcount_remove_many(&abd
->abd_children
, n
, buf
);
414 if (abd_is_linear(abd
)) {
415 ASSERT3P(buf
, ==, abd_to_buf(abd
));
417 ASSERT0(abd_cmp_buf(abd
, buf
, n
));
418 zio_buf_free(buf
, n
);
423 abd_return_buf_copy(abd_t
*abd
, void *buf
, size_t n
)
425 if (!abd_is_linear(abd
)) {
426 abd_copy_from_buf(abd
, buf
, n
);
428 abd_return_buf(abd
, buf
, n
);