]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/abd.c
MFV r323790: 8567 Inconsistent return value in zpool_read_label
[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 extern inline boolean_t abd_is_linear(abd_t *abd);
161 extern inline void abd_copy(abd_t *dabd, abd_t *sabd, size_t size);
162 extern inline void abd_copy_from_buf(abd_t *abd, const void *buf, size_t size);
163 extern inline void abd_copy_to_buf(void* buf, abd_t *abd, size_t size);
164 extern inline int abd_cmp_buf(abd_t *abd, const void *buf, size_t size);
165 extern inline void abd_zero(abd_t *abd, size_t size);
166
167 static void *
168 abd_alloc_chunk()
169 {
170         void *c = kmem_cache_alloc(abd_chunk_cache, KM_PUSHPAGE);
171         ASSERT3P(c, !=, NULL);
172         return (c);
173 }
174
175 static void
176 abd_free_chunk(void *c)
177 {
178         kmem_cache_free(abd_chunk_cache, c);
179 }
180
181 void
182 abd_init(void)
183 {
184 #ifdef illumos
185         vmem_t *data_alloc_arena = NULL;
186
187 #ifdef _KERNEL
188         data_alloc_arena = zio_alloc_arena;
189 #endif
190
191         /*
192          * Since ABD chunks do not appear in crash dumps, we pass KMC_NOTOUCH
193          * so that no allocator metadata is stored with the buffers.
194          */
195         abd_chunk_cache = kmem_cache_create("abd_chunk", zfs_abd_chunk_size, 0,
196             NULL, NULL, NULL, NULL, data_alloc_arena, KMC_NOTOUCH);
197 #else
198         abd_chunk_cache = kmem_cache_create("abd_chunk", zfs_abd_chunk_size, 0,
199             NULL, NULL, NULL, NULL, 0, KMC_NOTOUCH | KMC_NODEBUG);
200 #endif
201         abd_ksp = kstat_create("zfs", 0, "abdstats", "misc", KSTAT_TYPE_NAMED,
202             sizeof (abd_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
203         if (abd_ksp != NULL) {
204                 abd_ksp->ks_data = &abd_stats;
205                 kstat_install(abd_ksp);
206         }
207 }
208
209 void
210 abd_fini(void)
211 {
212         if (abd_ksp != NULL) {
213                 kstat_delete(abd_ksp);
214                 abd_ksp = NULL;
215         }
216
217         kmem_cache_destroy(abd_chunk_cache);
218         abd_chunk_cache = NULL;
219 }
220
221 static inline size_t
222 abd_chunkcnt_for_bytes(size_t size)
223 {
224         return (P2ROUNDUP(size, zfs_abd_chunk_size) / zfs_abd_chunk_size);
225 }
226
227 static inline size_t
228 abd_scatter_chunkcnt(abd_t *abd)
229 {
230         ASSERT(!abd_is_linear(abd));
231         return (abd_chunkcnt_for_bytes(
232             abd->abd_u.abd_scatter.abd_offset + abd->abd_size));
233 }
234
235 static inline void
236 abd_verify(abd_t *abd)
237 {
238         ASSERT3U(abd->abd_size, >, 0);
239         ASSERT3U(abd->abd_size, <=, SPA_MAXBLOCKSIZE);
240         ASSERT3U(abd->abd_flags, ==, abd->abd_flags & (ABD_FLAG_LINEAR |
241             ABD_FLAG_OWNER | ABD_FLAG_META));
242         IMPLY(abd->abd_parent != NULL, !(abd->abd_flags & ABD_FLAG_OWNER));
243         IMPLY(abd->abd_flags & ABD_FLAG_META, abd->abd_flags & ABD_FLAG_OWNER);
244         if (abd_is_linear(abd)) {
245                 ASSERT3P(abd->abd_u.abd_linear.abd_buf, !=, NULL);
246         } else {
247                 ASSERT3U(abd->abd_u.abd_scatter.abd_offset, <,
248                     zfs_abd_chunk_size);
249                 size_t n = abd_scatter_chunkcnt(abd);
250                 for (int i = 0; i < n; i++) {
251                         ASSERT3P(
252                             abd->abd_u.abd_scatter.abd_chunks[i], !=, NULL);
253                 }
254         }
255 }
256
257 static inline abd_t *
258 abd_alloc_struct(size_t chunkcnt)
259 {
260         size_t size = offsetof(abd_t, abd_u.abd_scatter.abd_chunks[chunkcnt]);
261         abd_t *abd = kmem_alloc(size, KM_PUSHPAGE);
262         ASSERT3P(abd, !=, NULL);
263         ABDSTAT_INCR(abdstat_struct_size, size);
264
265         return (abd);
266 }
267
268 static inline void
269 abd_free_struct(abd_t *abd)
270 {
271         size_t chunkcnt = abd_is_linear(abd) ? 0 : abd_scatter_chunkcnt(abd);
272         int size = offsetof(abd_t, abd_u.abd_scatter.abd_chunks[chunkcnt]);
273         kmem_free(abd, size);
274         ABDSTAT_INCR(abdstat_struct_size, -size);
275 }
276
277 /*
278  * Allocate an ABD, along with its own underlying data buffers. Use this if you
279  * don't care whether the ABD is linear or not.
280  */
281 abd_t *
282 abd_alloc(size_t size, boolean_t is_metadata)
283 {
284         if (!zfs_abd_scatter_enabled)
285                 return (abd_alloc_linear(size, is_metadata));
286
287         VERIFY3U(size, <=, SPA_MAXBLOCKSIZE);
288
289         size_t n = abd_chunkcnt_for_bytes(size);
290         abd_t *abd = abd_alloc_struct(n);
291
292         abd->abd_flags = ABD_FLAG_OWNER;
293         if (is_metadata) {
294                 abd->abd_flags |= ABD_FLAG_META;
295         }
296         abd->abd_size = size;
297         abd->abd_parent = NULL;
298         refcount_create(&abd->abd_children);
299
300         abd->abd_u.abd_scatter.abd_offset = 0;
301         abd->abd_u.abd_scatter.abd_chunk_size = zfs_abd_chunk_size;
302
303         for (int i = 0; i < n; i++) {
304                 void *c = abd_alloc_chunk();
305                 ASSERT3P(c, !=, NULL);
306                 abd->abd_u.abd_scatter.abd_chunks[i] = c;
307         }
308
309         ABDSTAT_BUMP(abdstat_scatter_cnt);
310         ABDSTAT_INCR(abdstat_scatter_data_size, size);
311         ABDSTAT_INCR(abdstat_scatter_chunk_waste,
312             n * zfs_abd_chunk_size - size);
313
314         return (abd);
315 }
316
317 static void
318 abd_free_scatter(abd_t *abd)
319 {
320         size_t n = abd_scatter_chunkcnt(abd);
321         for (int i = 0; i < n; i++) {
322                 abd_free_chunk(abd->abd_u.abd_scatter.abd_chunks[i]);
323         }
324
325         refcount_destroy(&abd->abd_children);
326         ABDSTAT_BUMPDOWN(abdstat_scatter_cnt);
327         ABDSTAT_INCR(abdstat_scatter_data_size, -(int)abd->abd_size);
328         ABDSTAT_INCR(abdstat_scatter_chunk_waste,
329             abd->abd_size - n * zfs_abd_chunk_size);
330
331         abd_free_struct(abd);
332 }
333
334 /*
335  * Allocate an ABD that must be linear, along with its own underlying data
336  * buffer. Only use this when it would be very annoying to write your ABD
337  * consumer with a scattered ABD.
338  */
339 abd_t *
340 abd_alloc_linear(size_t size, boolean_t is_metadata)
341 {
342         abd_t *abd = abd_alloc_struct(0);
343
344         VERIFY3U(size, <=, SPA_MAXBLOCKSIZE);
345
346         abd->abd_flags = ABD_FLAG_LINEAR | ABD_FLAG_OWNER;
347         if (is_metadata) {
348                 abd->abd_flags |= ABD_FLAG_META;
349         }
350         abd->abd_size = size;
351         abd->abd_parent = NULL;
352         refcount_create(&abd->abd_children);
353
354         if (is_metadata) {
355                 abd->abd_u.abd_linear.abd_buf = zio_buf_alloc(size);
356         } else {
357                 abd->abd_u.abd_linear.abd_buf = zio_data_buf_alloc(size);
358         }
359
360         ABDSTAT_BUMP(abdstat_linear_cnt);
361         ABDSTAT_INCR(abdstat_linear_data_size, size);
362
363         return (abd);
364 }
365
366 static void
367 abd_free_linear(abd_t *abd)
368 {
369         if (abd->abd_flags & ABD_FLAG_META) {
370                 zio_buf_free(abd->abd_u.abd_linear.abd_buf, abd->abd_size);
371         } else {
372                 zio_data_buf_free(abd->abd_u.abd_linear.abd_buf, abd->abd_size);
373         }
374
375         refcount_destroy(&abd->abd_children);
376         ABDSTAT_BUMPDOWN(abdstat_linear_cnt);
377         ABDSTAT_INCR(abdstat_linear_data_size, -(int)abd->abd_size);
378
379         abd_free_struct(abd);
380 }
381
382 /*
383  * Free an ABD. Only use this on ABDs allocated with abd_alloc() or
384  * abd_alloc_linear().
385  */
386 void
387 abd_free(abd_t *abd)
388 {
389         abd_verify(abd);
390         ASSERT3P(abd->abd_parent, ==, NULL);
391         ASSERT(abd->abd_flags & ABD_FLAG_OWNER);
392         if (abd_is_linear(abd))
393                 abd_free_linear(abd);
394         else
395                 abd_free_scatter(abd);
396 }
397
398 /*
399  * Allocate an ABD of the same format (same metadata flag, same scatterize
400  * setting) as another ABD.
401  */
402 abd_t *
403 abd_alloc_sametype(abd_t *sabd, size_t size)
404 {
405         boolean_t is_metadata = (sabd->abd_flags & ABD_FLAG_META) != 0;
406         if (abd_is_linear(sabd)) {
407                 return (abd_alloc_linear(size, is_metadata));
408         } else {
409                 return (abd_alloc(size, is_metadata));
410         }
411 }
412
413 /*
414  * If we're going to use this ABD for doing I/O using the block layer, the
415  * consumer of the ABD data doesn't care if it's scattered or not, and we don't
416  * plan to store this ABD in memory for a long period of time, we should
417  * allocate the ABD type that requires the least data copying to do the I/O.
418  *
419  * Currently this is linear ABDs, however if ldi_strategy() can ever issue I/Os
420  * using a scatter/gather list we should switch to that and replace this call
421  * with vanilla abd_alloc().
422  */
423 abd_t *
424 abd_alloc_for_io(size_t size, boolean_t is_metadata)
425 {
426         return (abd_alloc_linear(size, is_metadata));
427 }
428
429 /*
430  * Allocate a new ABD to point to offset off of sabd. It shares the underlying
431  * buffer data with sabd. Use abd_put() to free. sabd must not be freed while
432  * any derived ABDs exist.
433  */
434 abd_t *
435 abd_get_offset(abd_t *sabd, size_t off)
436 {
437         abd_t *abd;
438
439         abd_verify(sabd);
440         ASSERT3U(off, <=, sabd->abd_size);
441
442         if (abd_is_linear(sabd)) {
443                 abd = abd_alloc_struct(0);
444
445                 /*
446                  * Even if this buf is filesystem metadata, we only track that
447                  * if we own the underlying data buffer, which is not true in
448                  * this case. Therefore, we don't ever use ABD_FLAG_META here.
449                  */
450                 abd->abd_flags = ABD_FLAG_LINEAR;
451
452                 abd->abd_u.abd_linear.abd_buf =
453                     (char *)sabd->abd_u.abd_linear.abd_buf + off;
454         } else {
455                 size_t new_offset = sabd->abd_u.abd_scatter.abd_offset + off;
456                 size_t chunkcnt = abd_scatter_chunkcnt(sabd) -
457                     (new_offset / zfs_abd_chunk_size);
458
459                 abd = abd_alloc_struct(chunkcnt);
460
461                 /*
462                  * Even if this buf is filesystem metadata, we only track that
463                  * if we own the underlying data buffer, which is not true in
464                  * this case. Therefore, we don't ever use ABD_FLAG_META here.
465                  */
466                 abd->abd_flags = 0;
467
468                 abd->abd_u.abd_scatter.abd_offset =
469                     new_offset % zfs_abd_chunk_size;
470                 abd->abd_u.abd_scatter.abd_chunk_size = zfs_abd_chunk_size;
471
472                 /* Copy the scatterlist starting at the correct offset */
473                 (void) memcpy(&abd->abd_u.abd_scatter.abd_chunks,
474                     &sabd->abd_u.abd_scatter.abd_chunks[new_offset /
475                     zfs_abd_chunk_size],
476                     chunkcnt * sizeof (void *));
477         }
478
479         abd->abd_size = sabd->abd_size - off;
480         abd->abd_parent = sabd;
481         refcount_create(&abd->abd_children);
482         (void) refcount_add_many(&sabd->abd_children, abd->abd_size, abd);
483
484         return (abd);
485 }
486
487 /*
488  * Allocate a linear ABD structure for buf. You must free this with abd_put()
489  * since the resulting ABD doesn't own its own buffer.
490  */
491 abd_t *
492 abd_get_from_buf(void *buf, size_t size)
493 {
494         abd_t *abd = abd_alloc_struct(0);
495
496         VERIFY3U(size, <=, SPA_MAXBLOCKSIZE);
497
498         /*
499          * Even if this buf is filesystem metadata, we only track that if we
500          * own the underlying data buffer, which is not true in this case.
501          * Therefore, we don't ever use ABD_FLAG_META here.
502          */
503         abd->abd_flags = ABD_FLAG_LINEAR;
504         abd->abd_size = size;
505         abd->abd_parent = NULL;
506         refcount_create(&abd->abd_children);
507
508         abd->abd_u.abd_linear.abd_buf = buf;
509
510         return (abd);
511 }
512
513 /*
514  * Free an ABD allocated from abd_get_offset() or abd_get_from_buf(). Will not
515  * free the underlying scatterlist or buffer.
516  */
517 void
518 abd_put(abd_t *abd)
519 {
520         abd_verify(abd);
521         ASSERT(!(abd->abd_flags & ABD_FLAG_OWNER));
522
523         if (abd->abd_parent != NULL) {
524                 (void) refcount_remove_many(&abd->abd_parent->abd_children,
525                     abd->abd_size, abd);
526         }
527
528         refcount_destroy(&abd->abd_children);
529         abd_free_struct(abd);
530 }
531
532 /*
533  * Get the raw buffer associated with a linear ABD.
534  */
535 void *
536 abd_to_buf(abd_t *abd)
537 {
538         ASSERT(abd_is_linear(abd));
539         abd_verify(abd);
540         return (abd->abd_u.abd_linear.abd_buf);
541 }
542
543 /*
544  * Borrow a raw buffer from an ABD without copying the contents of the ABD
545  * into the buffer. If the ABD is scattered, this will allocate a raw buffer
546  * whose contents are undefined. To copy over the existing data in the ABD, use
547  * abd_borrow_buf_copy() instead.
548  */
549 void *
550 abd_borrow_buf(abd_t *abd, size_t n)
551 {
552         void *buf;
553         abd_verify(abd);
554         ASSERT3U(abd->abd_size, >=, n);
555         if (abd_is_linear(abd)) {
556                 buf = abd_to_buf(abd);
557         } else {
558                 buf = zio_buf_alloc(n);
559         }
560         (void) refcount_add_many(&abd->abd_children, n, buf);
561
562         return (buf);
563 }
564
565 void *
566 abd_borrow_buf_copy(abd_t *abd, size_t n)
567 {
568         void *buf = abd_borrow_buf(abd, n);
569         if (!abd_is_linear(abd)) {
570                 abd_copy_to_buf(buf, abd, n);
571         }
572         return (buf);
573 }
574
575 /*
576  * Return a borrowed raw buffer to an ABD. If the ABD is scattered, this will
577  * not change the contents of the ABD and will ASSERT that you didn't modify
578  * the buffer since it was borrowed. If you want any changes you made to buf to
579  * be copied back to abd, use abd_return_buf_copy() instead.
580  */
581 void
582 abd_return_buf(abd_t *abd, void *buf, size_t n)
583 {
584         abd_verify(abd);
585         ASSERT3U(abd->abd_size, >=, n);
586         if (abd_is_linear(abd)) {
587                 ASSERT3P(buf, ==, abd_to_buf(abd));
588         } else {
589                 ASSERT0(abd_cmp_buf(abd, buf, n));
590                 zio_buf_free(buf, n);
591         }
592         (void) refcount_remove_many(&abd->abd_children, n, buf);
593 }
594
595 void
596 abd_return_buf_copy(abd_t *abd, void *buf, size_t n)
597 {
598         if (!abd_is_linear(abd)) {
599                 abd_copy_from_buf(abd, buf, n);
600         }
601         abd_return_buf(abd, buf, n);
602 }
603
604 /*
605  * Give this ABD ownership of the buffer that it's storing. Can only be used on
606  * linear ABDs which were allocated via abd_get_from_buf(), or ones allocated
607  * with abd_alloc_linear() which subsequently released ownership of their buf
608  * with abd_release_ownership_of_buf().
609  */
610 void
611 abd_take_ownership_of_buf(abd_t *abd, boolean_t is_metadata)
612 {
613         ASSERT(abd_is_linear(abd));
614         ASSERT(!(abd->abd_flags & ABD_FLAG_OWNER));
615         abd_verify(abd);
616
617         abd->abd_flags |= ABD_FLAG_OWNER;
618         if (is_metadata) {
619                 abd->abd_flags |= ABD_FLAG_META;
620         }
621
622         ABDSTAT_BUMP(abdstat_linear_cnt);
623         ABDSTAT_INCR(abdstat_linear_data_size, abd->abd_size);
624 }
625
626 void
627 abd_release_ownership_of_buf(abd_t *abd)
628 {
629         ASSERT(abd_is_linear(abd));
630         ASSERT(abd->abd_flags & ABD_FLAG_OWNER);
631         abd_verify(abd);
632
633         abd->abd_flags &= ~ABD_FLAG_OWNER;
634         /* Disable this flag since we no longer own the data buffer */
635         abd->abd_flags &= ~ABD_FLAG_META;
636
637         ABDSTAT_BUMPDOWN(abdstat_linear_cnt);
638         ABDSTAT_INCR(abdstat_linear_data_size, -(int)abd->abd_size);
639 }
640
641 struct abd_iter {
642         abd_t           *iter_abd;      /* ABD being iterated through */
643         size_t          iter_pos;       /* position (relative to abd_offset) */
644         void            *iter_mapaddr;  /* addr corresponding to iter_pos */
645         size_t          iter_mapsize;   /* length of data valid at mapaddr */
646 };
647
648 static inline size_t
649 abd_iter_scatter_chunk_offset(struct abd_iter *aiter)
650 {
651         ASSERT(!abd_is_linear(aiter->iter_abd));
652         return ((aiter->iter_abd->abd_u.abd_scatter.abd_offset +
653             aiter->iter_pos) % zfs_abd_chunk_size);
654 }
655
656 static inline size_t
657 abd_iter_scatter_chunk_index(struct abd_iter *aiter)
658 {
659         ASSERT(!abd_is_linear(aiter->iter_abd));
660         return ((aiter->iter_abd->abd_u.abd_scatter.abd_offset +
661             aiter->iter_pos) / zfs_abd_chunk_size);
662 }
663
664 /*
665  * Initialize the abd_iter.
666  */
667 static void
668 abd_iter_init(struct abd_iter *aiter, abd_t *abd)
669 {
670         abd_verify(abd);
671         aiter->iter_abd = abd;
672         aiter->iter_pos = 0;
673         aiter->iter_mapaddr = NULL;
674         aiter->iter_mapsize = 0;
675 }
676
677 /*
678  * Advance the iterator by a certain amount. Cannot be called when a chunk is
679  * in use. This can be safely called when the aiter has already exhausted, in
680  * which case this does nothing.
681  */
682 static void
683 abd_iter_advance(struct abd_iter *aiter, size_t amount)
684 {
685         ASSERT3P(aiter->iter_mapaddr, ==, NULL);
686         ASSERT0(aiter->iter_mapsize);
687
688         /* There's nothing left to advance to, so do nothing */
689         if (aiter->iter_pos == aiter->iter_abd->abd_size)
690                 return;
691
692         aiter->iter_pos += amount;
693 }
694
695 /*
696  * Map the current chunk into aiter. This can be safely called when the aiter
697  * has already exhausted, in which case this does nothing.
698  */
699 static void
700 abd_iter_map(struct abd_iter *aiter)
701 {
702         void *paddr;
703         size_t offset = 0;
704
705         ASSERT3P(aiter->iter_mapaddr, ==, NULL);
706         ASSERT0(aiter->iter_mapsize);
707
708         /* Panic if someone has changed zfs_abd_chunk_size */
709         IMPLY(!abd_is_linear(aiter->iter_abd), zfs_abd_chunk_size ==
710             aiter->iter_abd->abd_u.abd_scatter.abd_chunk_size);
711
712         /* There's nothing left to iterate over, so do nothing */
713         if (aiter->iter_pos == aiter->iter_abd->abd_size)
714                 return;
715
716         if (abd_is_linear(aiter->iter_abd)) {
717                 offset = aiter->iter_pos;
718                 aiter->iter_mapsize = aiter->iter_abd->abd_size - offset;
719                 paddr = aiter->iter_abd->abd_u.abd_linear.abd_buf;
720         } else {
721                 size_t index = abd_iter_scatter_chunk_index(aiter);
722                 offset = abd_iter_scatter_chunk_offset(aiter);
723                 aiter->iter_mapsize = zfs_abd_chunk_size - offset;
724                 paddr = aiter->iter_abd->abd_u.abd_scatter.abd_chunks[index];
725         }
726         aiter->iter_mapaddr = (char *)paddr + offset;
727 }
728
729 /*
730  * Unmap the current chunk from aiter. This can be safely called when the aiter
731  * has already exhausted, in which case this does nothing.
732  */
733 static void
734 abd_iter_unmap(struct abd_iter *aiter)
735 {
736         /* There's nothing left to unmap, so do nothing */
737         if (aiter->iter_pos == aiter->iter_abd->abd_size)
738                 return;
739
740         ASSERT3P(aiter->iter_mapaddr, !=, NULL);
741         ASSERT3U(aiter->iter_mapsize, >, 0);
742
743         aiter->iter_mapaddr = NULL;
744         aiter->iter_mapsize = 0;
745 }
746
747 int
748 abd_iterate_func(abd_t *abd, size_t off, size_t size,
749     abd_iter_func_t *func, void *private)
750 {
751         int ret = 0;
752         struct abd_iter aiter;
753
754         abd_verify(abd);
755         ASSERT3U(off + size, <=, abd->abd_size);
756
757         abd_iter_init(&aiter, abd);
758         abd_iter_advance(&aiter, off);
759
760         while (size > 0) {
761                 abd_iter_map(&aiter);
762
763                 size_t len = MIN(aiter.iter_mapsize, size);
764                 ASSERT3U(len, >, 0);
765
766                 ret = func(aiter.iter_mapaddr, len, private);
767
768                 abd_iter_unmap(&aiter);
769
770                 if (ret != 0)
771                         break;
772
773                 size -= len;
774                 abd_iter_advance(&aiter, len);
775         }
776
777         return (ret);
778 }
779
780 struct buf_arg {
781         void *arg_buf;
782 };
783
784 static int
785 abd_copy_to_buf_off_cb(void *buf, size_t size, void *private)
786 {
787         struct buf_arg *ba_ptr = private;
788
789         (void) memcpy(ba_ptr->arg_buf, buf, size);
790         ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size;
791
792         return (0);
793 }
794
795 /*
796  * Copy abd to buf. (off is the offset in abd.)
797  */
798 void
799 abd_copy_to_buf_off(void *buf, abd_t *abd, size_t off, size_t size)
800 {
801         struct buf_arg ba_ptr = { buf };
802
803         (void) abd_iterate_func(abd, off, size, abd_copy_to_buf_off_cb,
804             &ba_ptr);
805 }
806
807 static int
808 abd_cmp_buf_off_cb(void *buf, size_t size, void *private)
809 {
810         int ret;
811         struct buf_arg *ba_ptr = private;
812
813         ret = memcmp(buf, ba_ptr->arg_buf, size);
814         ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size;
815
816         return (ret);
817 }
818
819 /*
820  * Compare the contents of abd to buf. (off is the offset in abd.)
821  */
822 int
823 abd_cmp_buf_off(abd_t *abd, const void *buf, size_t off, size_t size)
824 {
825         struct buf_arg ba_ptr = { (void *) buf };
826
827         return (abd_iterate_func(abd, off, size, abd_cmp_buf_off_cb, &ba_ptr));
828 }
829
830 static int
831 abd_copy_from_buf_off_cb(void *buf, size_t size, void *private)
832 {
833         struct buf_arg *ba_ptr = private;
834
835         (void) memcpy(buf, ba_ptr->arg_buf, size);
836         ba_ptr->arg_buf = (char *)ba_ptr->arg_buf + size;
837
838         return (0);
839 }
840
841 /*
842  * Copy from buf to abd. (off is the offset in abd.)
843  */
844 void
845 abd_copy_from_buf_off(abd_t *abd, const void *buf, size_t off, size_t size)
846 {
847         struct buf_arg ba_ptr = { (void *) buf };
848
849         (void) abd_iterate_func(abd, off, size, abd_copy_from_buf_off_cb,
850             &ba_ptr);
851 }
852
853 /*ARGSUSED*/
854 static int
855 abd_zero_off_cb(void *buf, size_t size, void *private)
856 {
857         (void) memset(buf, 0, size);
858         return (0);
859 }
860
861 /*
862  * Zero out the abd from a particular offset to the end.
863  */
864 void
865 abd_zero_off(abd_t *abd, size_t off, size_t size)
866 {
867         (void) abd_iterate_func(abd, off, size, abd_zero_off_cb, NULL);
868 }
869
870 /*
871  * Iterate over two ABDs and call func incrementally on the two ABDs' data in
872  * equal-sized chunks (passed to func as raw buffers). func could be called many
873  * times during this iteration.
874  */
875 int
876 abd_iterate_func2(abd_t *dabd, abd_t *sabd, size_t doff, size_t soff,
877     size_t size, abd_iter_func2_t *func, void *private)
878 {
879         int ret = 0;
880         struct abd_iter daiter, saiter;
881
882         abd_verify(dabd);
883         abd_verify(sabd);
884
885         ASSERT3U(doff + size, <=, dabd->abd_size);
886         ASSERT3U(soff + size, <=, sabd->abd_size);
887
888         abd_iter_init(&daiter, dabd);
889         abd_iter_init(&saiter, sabd);
890         abd_iter_advance(&daiter, doff);
891         abd_iter_advance(&saiter, soff);
892
893         while (size > 0) {
894                 abd_iter_map(&daiter);
895                 abd_iter_map(&saiter);
896
897                 size_t dlen = MIN(daiter.iter_mapsize, size);
898                 size_t slen = MIN(saiter.iter_mapsize, size);
899                 size_t len = MIN(dlen, slen);
900                 ASSERT(dlen > 0 || slen > 0);
901
902                 ret = func(daiter.iter_mapaddr, saiter.iter_mapaddr, len,
903                     private);
904
905                 abd_iter_unmap(&saiter);
906                 abd_iter_unmap(&daiter);
907
908                 if (ret != 0)
909                         break;
910
911                 size -= len;
912                 abd_iter_advance(&daiter, len);
913                 abd_iter_advance(&saiter, len);
914         }
915
916         return (ret);
917 }
918
919 /*ARGSUSED*/
920 static int
921 abd_copy_off_cb(void *dbuf, void *sbuf, size_t size, void *private)
922 {
923         (void) memcpy(dbuf, sbuf, size);
924         return (0);
925 }
926
927 /*
928  * Copy from sabd to dabd starting from soff and doff.
929  */
930 void
931 abd_copy_off(abd_t *dabd, abd_t *sabd, size_t doff, size_t soff, size_t size)
932 {
933         (void) abd_iterate_func2(dabd, sabd, doff, soff, size,
934             abd_copy_off_cb, NULL);
935 }
936
937 /*ARGSUSED*/
938 static int
939 abd_cmp_cb(void *bufa, void *bufb, size_t size, void *private)
940 {
941         return (memcmp(bufa, bufb, size));
942 }
943
944 /*
945  * Compares the first size bytes of two ABDs.
946  */
947 int
948 abd_cmp(abd_t *dabd, abd_t *sabd, size_t size)
949 {
950         return (abd_iterate_func2(dabd, sabd, 0, 0, size, abd_cmp_cb, NULL));
951 }