]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/abd.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306956, and update
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / abd.c
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 /*
18  * ARC buffer data (ABD).
19  *
20  * ABDs are an abstract data structure for the ARC which can use two
21  * different ways of storing the underlying data:
22  *
23  * (a) Linear buffer. In this case, all the data in the ABD is stored in one
24  *     contiguous buffer in memory (from a zio_[data_]buf_* kmem cache).
25  *
26  *         +-------------------+
27  *         | ABD (linear)      |
28  *         |   abd_flags = ... |
29  *         |   abd_size = ...  |     +--------------------------------+
30  *         |   abd_buf ------------->| raw buffer of size abd_size    |
31  *         +-------------------+     +--------------------------------+
32  *              no abd_chunks
33  *
34  * (b) Scattered buffer. In this case, the data in the ABD is split into
35  *     equal-sized chunks (from the abd_chunk_cache kmem_cache), with pointers
36  *     to the chunks recorded in an array at the end of the ABD structure.
37  *
38  *         +-------------------+
39  *         | ABD (scattered)   |
40  *         |   abd_flags = ... |
41  *         |   abd_size = ...  |
42  *         |   abd_offset = 0  |                           +-----------+
43  *         |   abd_chunks[0] ----------------------------->| chunk 0   |
44  *         |   abd_chunks[1] ---------------------+        +-----------+
45  *         |   ...             |                  |        +-----------+
46  *         |   abd_chunks[N-1] ---------+         +------->| chunk 1   |
47  *         +-------------------+        |                  +-----------+
48  *                                      |                      ...
49  *                                      |                  +-----------+
50  *                                      +----------------->| chunk N-1 |
51  *                                                         +-----------+
52  *
53  * Using a large proportion of scattered ABDs decreases ARC fragmentation since
54  * when we are at the limit of allocatable space, using equal-size chunks will
55  * allow us to quickly reclaim enough space for a new large allocation (assuming
56  * it is also scattered).
57  *
58  * In addition to directly allocating a linear or scattered ABD, it is also
59  * possible to create an ABD by requesting the "sub-ABD" starting at an offset
60  * within an existing ABD. In linear buffers this is simple (set abd_buf of
61  * the new ABD to the starting point within the original raw buffer), but
62  * scattered ABDs are a little more complex. The new ABD makes a copy of the
63  * relevant abd_chunks pointers (but not the underlying data). However, to
64  * provide arbitrary rather than only chunk-aligned starting offsets, it also
65  * tracks an abd_offset field which represents the starting point of the data
66  * within the first chunk in abd_chunks. For both linear and scattered ABDs,
67  * creating an offset ABD marks the original ABD as the offset's parent, and the
68  * original ABD's abd_children refcount is incremented. This data allows us to
69  * ensure the root ABD isn't deleted before its children.
70  *
71  * Most consumers should never need to know what type of ABD they're using --
72  * the ABD public API ensures that it's possible to transparently switch from
73  * using a linear ABD to a scattered one when doing so would be beneficial.
74  *
75  * If you need to use the data within an ABD directly, if you know it's linear
76  * (because you allocated it) you can use abd_to_buf() to access the underlying
77  * raw buffer. Otherwise, you should use one of the abd_borrow_buf* functions
78  * which will allocate a raw buffer if necessary. Use the abd_return_buf*
79  * functions to return any raw buffers that are no longer necessary when you're
80  * done using them.
81  *
82  * There are a variety of ABD APIs that implement basic buffer operations:
83  * compare, copy, read, write, and fill with zeroes. If you need a custom
84  * function which progressively accesses the whole ABD, use the abd_iterate_*
85  * functions.
86  */
87
88 #include <sys/abd.h>
89 #include <sys/param.h>
90 #include <sys/zio.h>
91 #include <sys/zfs_context.h>
92 #include <sys/zfs_znode.h>
93
94 typedef struct abd_stats {
95         kstat_named_t abdstat_struct_size;
96         kstat_named_t abdstat_scatter_cnt;
97         kstat_named_t abdstat_scatter_data_size;
98         kstat_named_t abdstat_scatter_chunk_waste;
99         kstat_named_t abdstat_linear_cnt;
100         kstat_named_t abdstat_linear_data_size;
101 } abd_stats_t;
102
103 static abd_stats_t abd_stats = {
104         /* Amount of memory occupied by all of the abd_t struct allocations */
105         { "struct_size",                        KSTAT_DATA_UINT64 },
106         /*
107          * The number of scatter ABDs which are currently allocated, excluding
108          * ABDs which don't own their data (for instance the ones which were
109          * allocated through abd_get_offset()).
110          */
111         { "scatter_cnt",                        KSTAT_DATA_UINT64 },
112         /* Amount of data stored in all scatter ABDs tracked by scatter_cnt */
113         { "scatter_data_size",                  KSTAT_DATA_UINT64 },
114         /*
115          * The amount of space wasted at the end of the last chunk across all
116          * scatter ABDs tracked by scatter_cnt.
117          */
118         { "scatter_chunk_waste",                KSTAT_DATA_UINT64 },
119         /*
120          * The number of linear ABDs which are currently allocated, excluding
121          * ABDs which don't own their data (for instance the ones which were
122          * allocated through abd_get_offset() and abd_get_from_buf()). If an
123          * ABD takes ownership of its buf then it will become tracked.
124          */
125         { "linear_cnt",                         KSTAT_DATA_UINT64 },
126         /* Amount of data stored in all linear ABDs tracked by linear_cnt */
127         { "linear_data_size",                   KSTAT_DATA_UINT64 },
128 };
129
130 #define ABDSTAT(stat)           (abd_stats.stat.value.ui64)
131 #define ABDSTAT_INCR(stat, val) \
132         atomic_add_64(&abd_stats.stat.value.ui64, (val))
133 #define ABDSTAT_BUMP(stat)      ABDSTAT_INCR(stat, 1)
134 #define ABDSTAT_BUMPDOWN(stat)  ABDSTAT_INCR(stat, -1)
135
136 /*
137  * It is possible to make all future ABDs be linear by setting this to B_FALSE.
138  * Otherwise, ABDs are allocated scattered by default unless the caller uses
139  * abd_alloc_linear().
140  */
141 boolean_t zfs_abd_scatter_enabled = B_TRUE;
142
143 /*
144  * The size of the chunks ABD allocates. Because the sizes allocated from the
145  * kmem_cache can't change, this tunable can only be modified at boot. Changing
146  * it at runtime would cause ABD iteration to work incorrectly for ABDs which
147  * were allocated with the old size, so a safeguard has been put in place which
148  * will cause the machine to panic if you change it and try to access the data
149  * within a scattered ABD.
150  */
151 size_t zfs_abd_chunk_size = 4096;
152
153 #ifdef _KERNEL
154 extern vmem_t *zio_alloc_arena;
155 #endif
156
157 kmem_cache_t *abd_chunk_cache;
158 static kstat_t *abd_ksp;
159
160 static void *
161 abd_alloc_chunk()
162 {
163         void *c = kmem_cache_alloc(abd_chunk_cache, KM_PUSHPAGE);
164         ASSERT3P(c, !=, NULL);
165         return (c);
166 }
167
168 static void
169 abd_free_chunk(void *c)
170 {
171         kmem_cache_free(abd_chunk_cache, c);
172 }
173
174 void
175 abd_init(void)
176 {
177 #ifdef illumos
178         vmem_t *data_alloc_arena = NULL;
179
180 #ifdef _KERNEL
181         data_alloc_arena = zio_alloc_arena;
182 #endif
183
184         /*
185          * Since ABD chunks do not appear in crash dumps, we pass KMC_NOTOUCH
186          * so that no allocator metadata is stored with the buffers.
187          */
188         abd_chunk_cache = kmem_cache_create("abd_chunk", zfs_abd_chunk_size, 0,
189             NULL, NULL, NULL, NULL, data_alloc_arena, KMC_NOTOUCH);
190 #else
191         abd_chunk_cache = kmem_cache_create("abd_chunk", zfs_abd_chunk_size, 0,
192             NULL, NULL, NULL, NULL, 0, KMC_NOTOUCH | KMC_NODEBUG);
193 #endif
194         abd_ksp = kstat_create("zfs", 0, "abdstats", "misc", KSTAT_TYPE_NAMED,
195             sizeof (abd_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
196         if (abd_ksp != NULL) {
197                 abd_ksp->ks_data = &abd_stats;
198                 kstat_install(abd_ksp);
199         }
200 }
201
202 void
203 abd_fini(void)
204 {
205         if (abd_ksp != NULL) {
206                 kstat_delete(abd_ksp);
207                 abd_ksp = NULL;
208         }
209
210         kmem_cache_destroy(abd_chunk_cache);
211         abd_chunk_cache = NULL;
212 }
213
214 static inline size_t
215 abd_chunkcnt_for_bytes(size_t size)
216 {
217         return (P2ROUNDUP(size, zfs_abd_chunk_size) / zfs_abd_chunk_size);
218 }
219
220 static inline size_t
221 abd_scatter_chunkcnt(abd_t *abd)
222 {
223         ASSERT(!abd_is_linear(abd));
224         return (abd_chunkcnt_for_bytes(
225             abd->abd_u.abd_scatter.abd_offset + abd->abd_size));
226 }
227
228 static inline void
229 abd_verify(abd_t *abd)
230 {
231         ASSERT3U(abd->abd_size, >, 0);
232         ASSERT3U(abd->abd_size, <=, SPA_MAXBLOCKSIZE);
233         ASSERT3U(abd->abd_flags, ==, abd->abd_flags & (ABD_FLAG_LINEAR |
234             ABD_FLAG_OWNER | ABD_FLAG_META));
235         IMPLY(abd->abd_parent != NULL, !(abd->abd_flags & ABD_FLAG_OWNER));
236         IMPLY(abd->abd_flags & ABD_FLAG_META, abd->abd_flags & ABD_FLAG_OWNER);
237         if (abd_is_linear(abd)) {
238                 ASSERT3P(abd->abd_u.abd_linear.abd_buf, !=, NULL);
239         } else {
240                 ASSERT3U(abd->abd_u.abd_scatter.abd_offset, <,
241                     zfs_abd_chunk_size);
242                 size_t n = abd_scatter_chunkcnt(abd);
243                 for (int i = 0; i < n; i++) {
244                         ASSERT3P(
245                             abd->abd_u.abd_scatter.abd_chunks[i], !=, NULL);
246                 }
247         }
248 }
249
250 static inline abd_t *
251 abd_alloc_struct(size_t chunkcnt)
252 {
253         size_t size = offsetof(abd_t, abd_u.abd_scatter.abd_chunks[chunkcnt]);
254         abd_t *abd = kmem_alloc(size, KM_PUSHPAGE);
255         ASSERT3P(abd, !=, NULL);
256         ABDSTAT_INCR(abdstat_struct_size, size);
257
258         return (abd);
259 }
260
261 static inline void
262 abd_free_struct(abd_t *abd)
263 {
264         size_t chunkcnt = abd_is_linear(abd) ? 0 : abd_scatter_chunkcnt(abd);
265         int size = offsetof(abd_t, abd_u.abd_scatter.abd_chunks[chunkcnt]);
266         kmem_free(abd, size);
267         ABDSTAT_INCR(abdstat_struct_size, -size);
268 }
269
270 /*
271  * Allocate an ABD, along with its own underlying data buffers. Use this if you
272  * don't care whether the ABD is linear or not.
273  */
274 abd_t *
275 abd_alloc(size_t size, boolean_t is_metadata)
276 {
277         if (!zfs_abd_scatter_enabled)
278                 return (abd_alloc_linear(size, is_metadata));
279
280         VERIFY3U(size, <=, SPA_MAXBLOCKSIZE);
281
282         size_t n = abd_chunkcnt_for_bytes(size);
283         abd_t *abd = abd_alloc_struct(n);
284
285         abd->abd_flags = ABD_FLAG_OWNER;
286         if (is_metadata) {
287                 abd->abd_flags |= ABD_FLAG_META;
288         }
289         abd->abd_size = size;
290         abd->abd_parent = NULL;
291         refcount_create(&abd->abd_children);
292
293         abd->abd_u.abd_scatter.abd_offset = 0;
294         abd->abd_u.abd_scatter.abd_chunk_size = zfs_abd_chunk_size;
295
296         for (int i = 0; i < n; i++) {
297                 void *c = abd_alloc_chunk();
298                 ASSERT3P(c, !=, NULL);
299                 abd->abd_u.abd_scatter.abd_chunks[i] = c;
300         }
301
302         ABDSTAT_BUMP(abdstat_scatter_cnt);
303         ABDSTAT_INCR(abdstat_scatter_data_size, size);
304         ABDSTAT_INCR(abdstat_scatter_chunk_waste,
305             n * zfs_abd_chunk_size - size);
306
307         return (abd);
308 }
309
310 static void
311 abd_free_scatter(abd_t *abd)
312 {
313         size_t n = abd_scatter_chunkcnt(abd);
314         for (int i = 0; i < n; i++) {
315                 abd_free_chunk(abd->abd_u.abd_scatter.abd_chunks[i]);
316         }
317
318         refcount_destroy(&abd->abd_children);
319         ABDSTAT_BUMPDOWN(abdstat_scatter_cnt);
320         ABDSTAT_INCR(abdstat_scatter_data_size, -(int)abd->abd_size);
321         ABDSTAT_INCR(abdstat_scatter_chunk_waste,
322             abd->abd_size - n * zfs_abd_chunk_size);
323
324         abd_free_struct(abd);
325 }
326
327 /*
328  * Allocate an ABD that must be linear, along with its own underlying data
329  * buffer. Only use this when it would be very annoying to write your ABD
330  * consumer with a scattered ABD.
331  */
332 abd_t *
333 abd_alloc_linear(size_t size, boolean_t is_metadata)
334 {
335         abd_t *abd = abd_alloc_struct(0);
336
337         VERIFY3U(size, <=, SPA_MAXBLOCKSIZE);
338
339         abd->abd_flags = ABD_FLAG_LINEAR | ABD_FLAG_OWNER;
340         if (is_metadata) {
341                 abd->abd_flags |= ABD_FLAG_META;
342         }
343         abd->abd_size = size;
344         abd->abd_parent = NULL;
345         refcount_create(&abd->abd_children);
346
347         if (is_metadata) {
348                 abd->abd_u.abd_linear.abd_buf = zio_buf_alloc(size);
349         } else {
350                 abd->abd_u.abd_linear.abd_buf = zio_data_buf_alloc(size);
351         }
352
353         ABDSTAT_BUMP(abdstat_linear_cnt);
354         ABDSTAT_INCR(abdstat_linear_data_size, size);
355
356         return (abd);
357 }
358
359 static void
360 abd_free_linear(abd_t *abd)
361 {
362         if (abd->abd_flags & ABD_FLAG_META) {
363                 zio_buf_free(abd->abd_u.abd_linear.abd_buf, abd->abd_size);
364         } else {
365                 zio_data_buf_free(abd->abd_u.abd_linear.abd_buf, abd->abd_size);
366         }
367
368         refcount_destroy(&abd->abd_children);
369         ABDSTAT_BUMPDOWN(abdstat_linear_cnt);
370         ABDSTAT_INCR(abdstat_linear_data_size, -(int)abd->abd_size);
371
372         abd_free_struct(abd);
373 }
374
375 /*
376  * Free an ABD. Only use this on ABDs allocated with abd_alloc() or
377  * abd_alloc_linear().
378  */
379 void
380 abd_free(abd_t *abd)
381 {
382         abd_verify(abd);
383         ASSERT3P(abd->abd_parent, ==, NULL);
384         ASSERT(abd->abd_flags & ABD_FLAG_OWNER);
385         if (abd_is_linear(abd))
386                 abd_free_linear(abd);
387         else
388                 abd_free_scatter(abd);
389 }
390
391 /*
392  * Allocate an ABD of the same format (same metadata flag, same scatterize
393  * setting) as another ABD.
394  */
395 abd_t *
396 abd_alloc_sametype(abd_t *sabd, size_t size)
397 {
398         boolean_t is_metadata = (sabd->abd_flags & ABD_FLAG_META) != 0;
399         if (abd_is_linear(sabd)) {
400                 return (abd_alloc_linear(size, is_metadata));
401         } else {
402                 return (abd_alloc(size, is_metadata));
403         }
404 }
405
406 /*
407  * If we're going to use this ABD for doing I/O using the block layer, the
408  * consumer of the ABD data doesn't care if it's scattered or not, and we don't
409  * plan to store this ABD in memory for a long period of time, we should
410  * allocate the ABD type that requires the least data copying to do the I/O.
411  *
412  * Currently this is linear ABDs, however if ldi_strategy() can ever issue I/Os
413  * using a scatter/gather list we should switch to that and replace this call
414  * with vanilla abd_alloc().
415  */
416 abd_t *
417 abd_alloc_for_io(size_t size, boolean_t is_metadata)
418 {
419         return (abd_alloc_linear(size, is_metadata));
420 }
421
422 /*
423  * Allocate a new ABD to point to offset off of sabd. It shares the underlying
424  * buffer data with sabd. Use abd_put() to free. sabd must not be freed while
425  * any derived ABDs exist.
426  */
427 abd_t *
428 abd_get_offset(abd_t *sabd, size_t off)
429 {
430         abd_t *abd;
431
432         abd_verify(sabd);
433         ASSERT3U(off, <=, sabd->abd_size);
434
435         if (abd_is_linear(sabd)) {
436                 abd = abd_alloc_struct(0);
437
438                 /*
439                  * Even if this buf is filesystem metadata, we only track that
440                  * if we own the underlying data buffer, which is not true in
441                  * this case. Therefore, we don't ever use ABD_FLAG_META here.
442                  */
443                 abd->abd_flags = ABD_FLAG_LINEAR;
444
445                 abd->abd_u.abd_linear.abd_buf =
446                     (char *)sabd->abd_u.abd_linear.abd_buf + off;
447         } else {
448                 size_t new_offset = sabd->abd_u.abd_scatter.abd_offset + off;
449                 size_t chunkcnt = abd_scatter_chunkcnt(sabd) -
450                     (new_offset / zfs_abd_chunk_size);
451
452                 abd = abd_alloc_struct(chunkcnt);
453
454                 /*
455                  * Even if this buf is filesystem metadata, we only track that
456                  * if we own the underlying data buffer, which is not true in
457                  * this case. Therefore, we don't ever use ABD_FLAG_META here.
458                  */
459                 abd->abd_flags = 0;
460
461                 abd->abd_u.abd_scatter.abd_offset =
462                     new_offset % zfs_abd_chunk_size;
463                 abd->abd_u.abd_scatter.abd_chunk_size = zfs_abd_chunk_size;
464
465                 /* Copy the scatterlist starting at the correct offset */
466                 (void) memcpy(&abd->abd_u.abd_scatter.abd_chunks,
467                     &sabd->abd_u.abd_scatter.abd_chunks[new_offset /
468                     zfs_abd_chunk_size],
469                     chunkcnt * sizeof (void *));
470         }
471
472         abd->abd_size = sabd->abd_size - off;
473         abd->abd_parent = sabd;
474         refcount_create(&abd->abd_children);
475         (void) refcount_add_many(&sabd->abd_children, abd->abd_size, abd);
476
477         return (abd);
478 }
479
480 /*
481  * Allocate a linear ABD structure for buf. You must free this with abd_put()
482  * since the resulting ABD doesn't own its own buffer.
483  */
484 abd_t *
485 abd_get_from_buf(void *buf, size_t size)
486 {
487         abd_t *abd = abd_alloc_struct(0);
488
489         VERIFY3U(size, <=, SPA_MAXBLOCKSIZE);
490
491         /*
492          * Even if this buf is filesystem metadata, we only track that if we
493          * own the underlying data buffer, which is not true in this case.
494          * Therefore, we don't ever use ABD_FLAG_META here.
495          */
496         abd->abd_flags = ABD_FLAG_LINEAR;
497         abd->abd_size = size;
498         abd->abd_parent = NULL;
499         refcount_create(&abd->abd_children);
500
501         abd->abd_u.abd_linear.abd_buf = buf;
502
503         return (abd);
504 }
505
506 /*
507  * Free an ABD allocated from abd_get_offset() or abd_get_from_buf(). Will not
508  * free the underlying scatterlist or buffer.
509  */
510 void
511 abd_put(abd_t *abd)
512 {
513         abd_verify(abd);
514         ASSERT(!(abd->abd_flags & ABD_FLAG_OWNER));
515
516         if (abd->abd_parent != NULL) {
517                 (void) refcount_remove_many(&abd->abd_parent->abd_children,
518                     abd->abd_size, abd);
519         }
520
521         refcount_destroy(&abd->abd_children);
522         abd_free_struct(abd);
523 }
524
525 /*
526  * Get the raw buffer associated with a linear ABD.
527  */
528 void *
529 abd_to_buf(abd_t *abd)
530 {
531         ASSERT(abd_is_linear(abd));
532         abd_verify(abd);
533         return (abd->abd_u.abd_linear.abd_buf);
534 }
535
536 /*
537  * Borrow a raw buffer from an ABD without copying the contents of the ABD
538  * into the buffer. If the ABD is scattered, this will allocate a raw buffer
539  * whose contents are undefined. To copy over the existing data in the ABD, use
540  * abd_borrow_buf_copy() instead.
541  */
542 void *
543 abd_borrow_buf(abd_t *abd, size_t n)
544 {
545         void *buf;
546         abd_verify(abd);
547         ASSERT3U(abd->abd_size, >=, n);
548         if (abd_is_linear(abd)) {
549                 buf = abd_to_buf(abd);
550         } else {
551                 buf = zio_buf_alloc(n);
552         }
553         (void) refcount_add_many(&abd->abd_children, n, buf);
554
555         return (buf);
556 }
557
558 void *
559 abd_borrow_buf_copy(abd_t *abd, size_t n)
560 {
561         void *buf = abd_borrow_buf(abd, n);
562         if (!abd_is_linear(abd)) {
563                 abd_copy_to_buf(buf, abd, n);
564         }
565         return (buf);
566 }
567
568 /*
569  * Return a borrowed raw buffer to an ABD. If the ABD is scattered, this will
570  * not change the contents of the ABD and will ASSERT that you didn't modify
571  * the buffer since it was borrowed. If you want any changes you made to buf to
572  * be copied back to abd, use abd_return_buf_copy() instead.
573  */
574 void
575 abd_return_buf(abd_t *abd, void *buf, size_t n)
576 {
577         abd_verify(abd);
578         ASSERT3U(abd->abd_size, >=, n);
579         if (abd_is_linear(abd)) {
580                 ASSERT3P(buf, ==, abd_to_buf(abd));
581         } else {
582                 ASSERT0(abd_cmp_buf(abd, buf, n));
583                 zio_buf_free(buf, n);
584         }
585         (void) refcount_remove_many(&abd->abd_children, n, buf);
586 }
587
588 void
589 abd_return_buf_copy(abd_t *abd, void *buf, size_t n)
590 {
591         if (!abd_is_linear(abd)) {
592                 abd_copy_from_buf(abd, buf, n);
593         }
594         abd_return_buf(abd, buf, n);
595 }
596
597 /*
598  * Give this ABD ownership of the buffer that it's storing. Can only be used on
599  * linear ABDs which were allocated via abd_get_from_buf(), or ones allocated
600  * with abd_alloc_linear() which subsequently released ownership of their buf
601  * with abd_release_ownership_of_buf().
602  */
603 void
604 abd_take_ownership_of_buf(abd_t *abd, boolean_t is_metadata)
605 {
606         ASSERT(abd_is_linear(abd));
607         ASSERT(!(abd->abd_flags & ABD_FLAG_OWNER));
608         abd_verify(abd);
609
610         abd->abd_flags |= ABD_FLAG_OWNER;
611         if (is_metadata) {
612                 abd->abd_flags |= ABD_FLAG_META;
613         }
614
615         ABDSTAT_BUMP(abdstat_linear_cnt);
616         ABDSTAT_INCR(abdstat_linear_data_size, abd->abd_size);
617 }
618
619 void
620 abd_release_ownership_of_buf(abd_t *abd)
621 {
622         ASSERT(abd_is_linear(abd));
623         ASSERT(abd->abd_flags & ABD_FLAG_OWNER);
624         abd_verify(abd);
625
626         abd->abd_flags &= ~ABD_FLAG_OWNER;
627         /* Disable this flag since we no longer own the data buffer */
628         abd->abd_flags &= ~ABD_FLAG_META;
629
630         ABDSTAT_BUMPDOWN(abdstat_linear_cnt);
631         ABDSTAT_INCR(abdstat_linear_data_size, -(int)abd->abd_size);
632 }
633
634 struct abd_iter {
635         abd_t           *iter_abd;      /* ABD being iterated through */
636         size_t          iter_pos;       /* position (relative to abd_offset) */
637         void            *iter_mapaddr;  /* addr corresponding to iter_pos */
638         size_t          iter_mapsize;   /* length of data valid at mapaddr */
639 };
640
641 static inline size_t
642 abd_iter_scatter_chunk_offset(struct abd_iter *aiter)
643 {
644         ASSERT(!abd_is_linear(aiter->iter_abd));
645         return ((aiter->iter_abd->abd_u.abd_scatter.abd_offset +
646             aiter->iter_pos) % zfs_abd_chunk_size);
647 }
648
649 static inline size_t
650 abd_iter_scatter_chunk_index(struct abd_iter *aiter)
651 {
652         ASSERT(!abd_is_linear(aiter->iter_abd));
653         return ((aiter->iter_abd->abd_u.abd_scatter.abd_offset +
654             aiter->iter_pos) / zfs_abd_chunk_size);
655 }
656
657 /*
658  * Initialize the abd_iter.
659  */
660 static void
661 abd_iter_init(struct abd_iter *aiter, abd_t *abd)
662 {
663         abd_verify(abd);
664         aiter->iter_abd = abd;
665         aiter->iter_pos = 0;
666         aiter->iter_mapaddr = NULL;
667         aiter->iter_mapsize = 0;
668 }
669
670 /*
671  * Advance the iterator by a certain amount. Cannot be called when a chunk is
672  * in use. This can be safely called when the aiter has already exhausted, in
673  * which case this does nothing.
674  */
675 static void
676 abd_iter_advance(struct abd_iter *aiter, size_t amount)
677 {
678         ASSERT3P(aiter->iter_mapaddr, ==, NULL);
679         ASSERT0(aiter->iter_mapsize);
680
681         /* There's nothing left to advance to, so do nothing */
682         if (aiter->iter_pos == aiter->iter_abd->abd_size)
683                 return;
684
685         aiter->iter_pos += amount;
686 }
687
688 /*
689  * Map the current chunk into aiter. This can be safely called when the aiter
690  * has already exhausted, in which case this does nothing.
691  */
692 static void
693 abd_iter_map(struct abd_iter *aiter)
694 {
695         void *paddr;
696         size_t offset = 0;
697
698         ASSERT3P(aiter->iter_mapaddr, ==, NULL);
699         ASSERT0(aiter->iter_mapsize);
700
701         /* Panic if someone has changed zfs_abd_chunk_size */
702         IMPLY(!abd_is_linear(aiter->iter_abd), zfs_abd_chunk_size ==
703             aiter->iter_abd->abd_u.abd_scatter.abd_chunk_size);
704
705         /* There's nothing left to iterate over, so do nothing */
706         if (aiter->iter_pos == aiter->iter_abd->abd_size)
707                 return;
708
709         if (abd_is_linear(aiter->iter_abd)) {
710                 offset = aiter->iter_pos;
711                 aiter->iter_mapsize = aiter->iter_abd->abd_size - offset;
712                 paddr = aiter->iter_abd->abd_u.abd_linear.abd_buf;
713         } else {
714                 size_t index = abd_iter_scatter_chunk_index(aiter);
715                 offset = abd_iter_scatter_chunk_offset(aiter);
716                 aiter->iter_mapsize = zfs_abd_chunk_size - offset;
717                 paddr = aiter->iter_abd->abd_u.abd_scatter.abd_chunks[index];
718         }
719         aiter->iter_mapaddr = (char *)paddr + offset;
720 }
721
722 /*
723  * Unmap the current chunk from aiter. This can be safely called when the aiter
724  * has already exhausted, in which case this does nothing.
725  */
726 static void
727 abd_iter_unmap(struct abd_iter *aiter)
728 {
729         /* There's nothing left to unmap, so do nothing */
730         if (aiter->iter_pos == aiter->iter_abd->abd_size)
731                 return;
732
733         ASSERT3P(aiter->iter_mapaddr, !=, NULL);
734         ASSERT3U(aiter->iter_mapsize, >, 0);
735
736         aiter->iter_mapaddr = NULL;
737         aiter->iter_mapsize = 0;
738 }
739
740 int
741 abd_iterate_func(abd_t *abd, size_t off, size_t size,
742     abd_iter_func_t *func, void *private)
743 {
744         int ret = 0;
745         struct abd_iter aiter;
746
747         abd_verify(abd);
748         ASSERT3U(off + size, <=, abd->abd_size);
749
750         abd_iter_init(&aiter, abd);
751         abd_iter_advance(&aiter, off);
752
753         while (size > 0) {
754                 abd_iter_map(&aiter);
755
756                 size_t len = MIN(aiter.iter_mapsize, size);
757                 ASSERT3U(len, >, 0);
758
759                 ret = func(aiter.iter_mapaddr, len, private);
760
761                 abd_iter_unmap(&aiter);
762
763                 if (ret != 0)
764                         break;
765
766                 size -= len;
767                 abd_iter_advance(&aiter, len);
768         }
769
770         return (ret);
771 }
772
773 struct buf_arg {
774         void *arg_buf;
775 };
776
777 static int
778 abd_copy_to_buf_off_cb(void *buf, size_t size, void *private)
779 {
780         struct buf_arg *ba_ptr = private;
781
782         (void) memcpy(ba_ptr->arg_buf, buf, size);
783         ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size;
784
785         return (0);
786 }
787
788 /*
789  * Copy abd to buf. (off is the offset in abd.)
790  */
791 void
792 abd_copy_to_buf_off(void *buf, abd_t *abd, size_t off, size_t size)
793 {
794         struct buf_arg ba_ptr = { buf };
795
796         (void) abd_iterate_func(abd, off, size, abd_copy_to_buf_off_cb,
797             &ba_ptr);
798 }
799
800 static int
801 abd_cmp_buf_off_cb(void *buf, size_t size, void *private)
802 {
803         int ret;
804         struct buf_arg *ba_ptr = private;
805
806         ret = memcmp(buf, ba_ptr->arg_buf, size);
807         ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size;
808
809         return (ret);
810 }
811
812 /*
813  * Compare the contents of abd to buf. (off is the offset in abd.)
814  */
815 int
816 abd_cmp_buf_off(abd_t *abd, const void *buf, size_t off, size_t size)
817 {
818         struct buf_arg ba_ptr = { (void *) buf };
819
820         return (abd_iterate_func(abd, off, size, abd_cmp_buf_off_cb, &ba_ptr));
821 }
822
823 static int
824 abd_copy_from_buf_off_cb(void *buf, size_t size, void *private)
825 {
826         struct buf_arg *ba_ptr = private;
827
828         (void) memcpy(buf, ba_ptr->arg_buf, size);
829         ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size;
830
831         return (0);
832 }
833
834 /*
835  * Copy from buf to abd. (off is the offset in abd.)
836  */
837 void
838 abd_copy_from_buf_off(abd_t *abd, const void *buf, size_t off, size_t size)
839 {
840         struct buf_arg ba_ptr = { (void *) buf };
841
842         (void) abd_iterate_func(abd, off, size, abd_copy_from_buf_off_cb,
843             &ba_ptr);
844 }
845
846 /*ARGSUSED*/
847 static int
848 abd_zero_off_cb(void *buf, size_t size, void *private)
849 {
850         (void) memset(buf, 0, size);
851         return (0);
852 }
853
854 /*
855  * Zero out the abd from a particular offset to the end.
856  */
857 void
858 abd_zero_off(abd_t *abd, size_t off, size_t size)
859 {
860         (void) abd_iterate_func(abd, off, size, abd_zero_off_cb, NULL);
861 }
862
863 /*
864  * Iterate over two ABDs and call func incrementally on the two ABDs' data in
865  * equal-sized chunks (passed to func as raw buffers). func could be called many
866  * times during this iteration.
867  */
868 int
869 abd_iterate_func2(abd_t *dabd, abd_t *sabd, size_t doff, size_t soff,
870     size_t size, abd_iter_func2_t *func, void *private)
871 {
872         int ret = 0;
873         struct abd_iter daiter, saiter;
874
875         abd_verify(dabd);
876         abd_verify(sabd);
877
878         ASSERT3U(doff + size, <=, dabd->abd_size);
879         ASSERT3U(soff + size, <=, sabd->abd_size);
880
881         abd_iter_init(&daiter, dabd);
882         abd_iter_init(&saiter, sabd);
883         abd_iter_advance(&daiter, doff);
884         abd_iter_advance(&saiter, soff);
885
886         while (size > 0) {
887                 abd_iter_map(&daiter);
888                 abd_iter_map(&saiter);
889
890                 size_t dlen = MIN(daiter.iter_mapsize, size);
891                 size_t slen = MIN(saiter.iter_mapsize, size);
892                 size_t len = MIN(dlen, slen);
893                 ASSERT(dlen > 0 || slen > 0);
894
895                 ret = func(daiter.iter_mapaddr, saiter.iter_mapaddr, len,
896                     private);
897
898                 abd_iter_unmap(&saiter);
899                 abd_iter_unmap(&daiter);
900
901                 if (ret != 0)
902                         break;
903
904                 size -= len;
905                 abd_iter_advance(&daiter, len);
906                 abd_iter_advance(&saiter, len);
907         }
908
909         return (ret);
910 }
911
912 /*ARGSUSED*/
913 static int
914 abd_copy_off_cb(void *dbuf, void *sbuf, size_t size, void *private)
915 {
916         (void) memcpy(dbuf, sbuf, size);
917         return (0);
918 }
919
920 /*
921  * Copy from sabd to dabd starting from soff and doff.
922  */
923 void
924 abd_copy_off(abd_t *dabd, abd_t *sabd, size_t doff, size_t soff, size_t size)
925 {
926         (void) abd_iterate_func2(dabd, sabd, doff, soff, size,
927             abd_copy_off_cb, NULL);
928 }
929
930 /*ARGSUSED*/
931 static int
932 abd_cmp_cb(void *bufa, void *bufb, size_t size, void *private)
933 {
934         return (memcmp(bufa, bufb, size));
935 }
936
937 /*
938  * Compares the first size bytes of two ABDs.
939  */
940 int
941 abd_cmp(abd_t *dabd, abd_t *sabd, size_t size)
942 {
943         return (abd_iterate_func2(dabd, sabd, 0, 0, size, abd_cmp_cb, NULL));
944 }