qcow2: add missing coroutine_fn annotations
[qemu.git] / util / iov.c
blob22d6996ccec037e66d6757fd1879ca78a3490077
1 /*
2 * Helpers for getting linearized buffers from iov / filling buffers into iovs
4 * Copyright IBM, Corp. 2007, 2008
5 * Copyright (C) 2010 Red Hat, Inc.
7 * Author(s):
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
10 * Michael Tokarev <mjt@tls.msk.ru>
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
15 * Contributions after 2012-01-13 are licensed under the terms of the
16 * GNU GPL, version 2 or (at your option) any later version.
19 #include "qemu/osdep.h"
20 #include "qemu/iov.h"
21 #include "qemu/sockets.h"
22 #include "qemu/cutils.h"
24 size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
25 size_t offset, const void *buf, size_t bytes)
27 size_t done;
28 unsigned int i;
29 for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
30 if (offset < iov[i].iov_len) {
31 size_t len = MIN(iov[i].iov_len - offset, bytes - done);
32 memcpy(iov[i].iov_base + offset, buf + done, len);
33 done += len;
34 offset = 0;
35 } else {
36 offset -= iov[i].iov_len;
39 assert(offset == 0);
40 return done;
43 size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
44 size_t offset, void *buf, size_t bytes)
46 size_t done;
47 unsigned int i;
48 for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
49 if (offset < iov[i].iov_len) {
50 size_t len = MIN(iov[i].iov_len - offset, bytes - done);
51 memcpy(buf + done, iov[i].iov_base + offset, len);
52 done += len;
53 offset = 0;
54 } else {
55 offset -= iov[i].iov_len;
58 assert(offset == 0);
59 return done;
62 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
63 size_t offset, int fillc, size_t bytes)
65 size_t done;
66 unsigned int i;
67 for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
68 if (offset < iov[i].iov_len) {
69 size_t len = MIN(iov[i].iov_len - offset, bytes - done);
70 memset(iov[i].iov_base + offset, fillc, len);
71 done += len;
72 offset = 0;
73 } else {
74 offset -= iov[i].iov_len;
77 assert(offset == 0);
78 return done;
81 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
83 size_t len;
84 unsigned int i;
86 len = 0;
87 for (i = 0; i < iov_cnt; i++) {
88 len += iov[i].iov_len;
90 return len;
93 /* helper function for iov_send_recv() */
94 static ssize_t
95 do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
97 #ifdef CONFIG_POSIX
98 ssize_t ret;
99 struct msghdr msg;
100 memset(&msg, 0, sizeof(msg));
101 msg.msg_iov = iov;
102 msg.msg_iovlen = iov_cnt;
103 do {
104 ret = do_send
105 ? sendmsg(sockfd, &msg, 0)
106 : recvmsg(sockfd, &msg, 0);
107 } while (ret < 0 && errno == EINTR);
108 return ret;
109 #else
110 /* else send piece-by-piece */
111 /*XXX Note: windows has WSASend() and WSARecv() */
112 unsigned i = 0;
113 ssize_t ret = 0;
114 while (i < iov_cnt) {
115 ssize_t r = do_send
116 ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
117 : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
118 if (r > 0) {
119 ret += r;
120 } else if (!r) {
121 break;
122 } else if (errno == EINTR) {
123 continue;
124 } else {
125 /* else it is some "other" error,
126 * only return if there was no data processed. */
127 if (ret == 0) {
128 ret = -1;
130 break;
132 i++;
134 return ret;
135 #endif
138 ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
139 size_t offset, size_t bytes,
140 bool do_send)
142 ssize_t total = 0;
143 ssize_t ret;
144 size_t orig_len, tail;
145 unsigned niov;
146 struct iovec *local_iov, *iov;
148 if (bytes <= 0) {
149 return 0;
152 local_iov = g_new0(struct iovec, iov_cnt);
153 iov_copy(local_iov, iov_cnt, _iov, iov_cnt, offset, bytes);
154 offset = 0;
155 iov = local_iov;
157 while (bytes > 0) {
158 /* Find the start position, skipping `offset' bytes:
159 * first, skip all full-sized vector elements, */
160 for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
161 offset -= iov[niov].iov_len;
164 /* niov == iov_cnt would only be valid if bytes == 0, which
165 * we already ruled out in the loop condition. */
166 assert(niov < iov_cnt);
167 iov += niov;
168 iov_cnt -= niov;
170 if (offset) {
171 /* second, skip `offset' bytes from the (now) first element,
172 * undo it on exit */
173 iov[0].iov_base += offset;
174 iov[0].iov_len -= offset;
176 /* Find the end position skipping `bytes' bytes: */
177 /* first, skip all full-sized elements */
178 tail = bytes;
179 for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
180 tail -= iov[niov].iov_len;
182 if (tail) {
183 /* second, fixup the last element, and remember the original
184 * length */
185 assert(niov < iov_cnt);
186 assert(iov[niov].iov_len > tail);
187 orig_len = iov[niov].iov_len;
188 iov[niov++].iov_len = tail;
189 ret = do_send_recv(sockfd, iov, niov, do_send);
190 /* Undo the changes above before checking for errors */
191 iov[niov-1].iov_len = orig_len;
192 } else {
193 ret = do_send_recv(sockfd, iov, niov, do_send);
195 if (offset) {
196 iov[0].iov_base -= offset;
197 iov[0].iov_len += offset;
200 if (ret < 0) {
201 assert(errno != EINTR);
202 g_free(local_iov);
203 if (errno == EAGAIN && total > 0) {
204 return total;
206 return -1;
209 if (ret == 0 && !do_send) {
210 /* recv returns 0 when the peer has performed an orderly
211 * shutdown. */
212 break;
215 /* Prepare for the next iteration */
216 offset += ret;
217 total += ret;
218 bytes -= ret;
221 g_free(local_iov);
222 return total;
226 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
227 FILE *fp, const char *prefix, size_t limit)
229 int v;
230 size_t size = 0;
231 char *buf;
233 for (v = 0; v < iov_cnt; v++) {
234 size += iov[v].iov_len;
236 size = size > limit ? limit : size;
237 buf = g_malloc(size);
238 iov_to_buf(iov, iov_cnt, 0, buf, size);
239 qemu_hexdump(fp, prefix, buf, size);
240 g_free(buf);
243 unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
244 const struct iovec *iov, unsigned int iov_cnt,
245 size_t offset, size_t bytes)
247 size_t len;
248 unsigned int i, j;
249 for (i = 0, j = 0;
250 i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) {
251 if (offset >= iov[i].iov_len) {
252 offset -= iov[i].iov_len;
253 continue;
255 len = MIN(bytes, iov[i].iov_len - offset);
257 dst_iov[j].iov_base = iov[i].iov_base + offset;
258 dst_iov[j].iov_len = len;
259 j++;
260 bytes -= len;
261 offset = 0;
263 assert(offset == 0);
264 return j;
267 /* io vectors */
269 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
271 qiov->iov = g_new(struct iovec, alloc_hint);
272 qiov->niov = 0;
273 qiov->nalloc = alloc_hint;
274 qiov->size = 0;
277 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
279 int i;
281 qiov->iov = iov;
282 qiov->niov = niov;
283 qiov->nalloc = -1;
284 qiov->size = 0;
285 for (i = 0; i < niov; i++)
286 qiov->size += iov[i].iov_len;
289 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
291 assert(qiov->nalloc != -1);
293 if (qiov->niov == qiov->nalloc) {
294 qiov->nalloc = 2 * qiov->nalloc + 1;
295 qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
297 qiov->iov[qiov->niov].iov_base = base;
298 qiov->iov[qiov->niov].iov_len = len;
299 qiov->size += len;
300 ++qiov->niov;
304 * Concatenates (partial) iovecs from src_iov to the end of dst.
305 * It starts copying after skipping `soffset' bytes at the
306 * beginning of src and adds individual vectors from src to
307 * dst copies up to `sbytes' bytes total, or up to the end
308 * of src_iov if it comes first. This way, it is okay to specify
309 * very large value for `sbytes' to indicate "up to the end
310 * of src".
311 * Only vector pointers are processed, not the actual data buffers.
313 size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
314 struct iovec *src_iov, unsigned int src_cnt,
315 size_t soffset, size_t sbytes)
317 int i;
318 size_t done;
320 if (!sbytes) {
321 return 0;
323 assert(dst->nalloc != -1);
324 for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
325 if (soffset < src_iov[i].iov_len) {
326 size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
327 qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
328 done += len;
329 soffset = 0;
330 } else {
331 soffset -= src_iov[i].iov_len;
334 assert(soffset == 0); /* offset beyond end of src */
336 return done;
340 * Concatenates (partial) iovecs from src to the end of dst.
341 * It starts copying after skipping `soffset' bytes at the
342 * beginning of src and adds individual vectors from src to
343 * dst copies up to `sbytes' bytes total, or up to the end
344 * of src if it comes first. This way, it is okay to specify
345 * very large value for `sbytes' to indicate "up to the end
346 * of src".
347 * Only vector pointers are processed, not the actual data buffers.
349 void qemu_iovec_concat(QEMUIOVector *dst,
350 QEMUIOVector *src, size_t soffset, size_t sbytes)
352 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
356 * qiov_find_iov
358 * Return pointer to iovec structure, where byte at @offset in original vector
359 * @iov exactly is.
360 * Set @remaining_offset to be offset inside that iovec to the same byte.
362 static struct iovec *iov_skip_offset(struct iovec *iov, size_t offset,
363 size_t *remaining_offset)
365 while (offset > 0 && offset >= iov->iov_len) {
366 offset -= iov->iov_len;
367 iov++;
369 *remaining_offset = offset;
371 return iov;
375 * qiov_slice
377 * Find subarray of iovec's, containing requested range. @head would
378 * be offset in first iov (returned by the function), @tail would be
379 * count of extra bytes in last iovec (returned iov + @niov - 1).
381 static struct iovec *qiov_slice(QEMUIOVector *qiov,
382 size_t offset, size_t len,
383 size_t *head, size_t *tail, int *niov)
385 struct iovec *iov, *end_iov;
387 assert(offset + len <= qiov->size);
389 iov = iov_skip_offset(qiov->iov, offset, head);
390 end_iov = iov_skip_offset(iov, *head + len, tail);
392 if (*tail > 0) {
393 assert(*tail < end_iov->iov_len);
394 *tail = end_iov->iov_len - *tail;
395 end_iov++;
398 *niov = end_iov - iov;
400 return iov;
403 int qemu_iovec_subvec_niov(QEMUIOVector *qiov, size_t offset, size_t len)
405 size_t head, tail;
406 int niov;
408 qiov_slice(qiov, offset, len, &head, &tail, &niov);
410 return niov;
414 * Compile new iovec, combining @head_buf buffer, sub-qiov of @mid_qiov,
415 * and @tail_buf buffer into new qiov.
417 int qemu_iovec_init_extended(
418 QEMUIOVector *qiov,
419 void *head_buf, size_t head_len,
420 QEMUIOVector *mid_qiov, size_t mid_offset, size_t mid_len,
421 void *tail_buf, size_t tail_len)
423 size_t mid_head, mid_tail;
424 int total_niov, mid_niov = 0;
425 struct iovec *p, *mid_iov = NULL;
427 assert(mid_qiov->niov <= IOV_MAX);
429 if (SIZE_MAX - head_len < mid_len ||
430 SIZE_MAX - head_len - mid_len < tail_len)
432 return -EINVAL;
435 if (mid_len) {
436 mid_iov = qiov_slice(mid_qiov, mid_offset, mid_len,
437 &mid_head, &mid_tail, &mid_niov);
440 total_niov = !!head_len + mid_niov + !!tail_len;
441 if (total_niov > IOV_MAX) {
442 return -EINVAL;
445 if (total_niov == 1) {
446 qemu_iovec_init_buf(qiov, NULL, 0);
447 p = &qiov->local_iov;
448 } else {
449 qiov->niov = qiov->nalloc = total_niov;
450 qiov->size = head_len + mid_len + tail_len;
451 p = qiov->iov = g_new(struct iovec, qiov->niov);
454 if (head_len) {
455 p->iov_base = head_buf;
456 p->iov_len = head_len;
457 p++;
460 assert(!mid_niov == !mid_len);
461 if (mid_niov) {
462 memcpy(p, mid_iov, mid_niov * sizeof(*p));
463 p[0].iov_base = (uint8_t *)p[0].iov_base + mid_head;
464 p[0].iov_len -= mid_head;
465 p[mid_niov - 1].iov_len -= mid_tail;
466 p += mid_niov;
469 if (tail_len) {
470 p->iov_base = tail_buf;
471 p->iov_len = tail_len;
474 return 0;
478 * Check if the contents of subrange of qiov data is all zeroes.
480 bool qemu_iovec_is_zero(QEMUIOVector *qiov, size_t offset, size_t bytes)
482 struct iovec *iov;
483 size_t current_offset;
485 assert(offset + bytes <= qiov->size);
487 iov = iov_skip_offset(qiov->iov, offset, &current_offset);
489 while (bytes) {
490 uint8_t *base = (uint8_t *)iov->iov_base + current_offset;
491 size_t len = MIN(iov->iov_len - current_offset, bytes);
493 if (!buffer_is_zero(base, len)) {
494 return false;
497 current_offset = 0;
498 bytes -= len;
499 iov++;
502 return true;
505 void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source,
506 size_t offset, size_t len)
508 int ret;
510 assert(source->size >= len);
511 assert(source->size - len >= offset);
513 /* We shrink the request, so we can't overflow neither size_t nor MAX_IOV */
514 ret = qemu_iovec_init_extended(qiov, NULL, 0, source, offset, len, NULL, 0);
515 assert(ret == 0);
518 void qemu_iovec_destroy(QEMUIOVector *qiov)
520 if (qiov->nalloc != -1) {
521 g_free(qiov->iov);
524 memset(qiov, 0, sizeof(*qiov));
527 void qemu_iovec_reset(QEMUIOVector *qiov)
529 assert(qiov->nalloc != -1);
531 qiov->niov = 0;
532 qiov->size = 0;
535 size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
536 void *buf, size_t bytes)
538 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
541 size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
542 const void *buf, size_t bytes)
544 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
547 size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
548 int fillc, size_t bytes)
550 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
554 * Check that I/O vector contents are identical
556 * The IO vectors must have the same structure (same length of all parts).
557 * A typical usage is to compare vectors created with qemu_iovec_clone().
559 * @a: I/O vector
560 * @b: I/O vector
561 * @ret: Offset to first mismatching byte or -1 if match
563 ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
565 int i;
566 ssize_t offset = 0;
568 assert(a->niov == b->niov);
569 for (i = 0; i < a->niov; i++) {
570 size_t len = 0;
571 uint8_t *p = (uint8_t *)a->iov[i].iov_base;
572 uint8_t *q = (uint8_t *)b->iov[i].iov_base;
574 assert(a->iov[i].iov_len == b->iov[i].iov_len);
575 while (len < a->iov[i].iov_len && *p++ == *q++) {
576 len++;
579 offset += len;
581 if (len != a->iov[i].iov_len) {
582 return offset;
585 return -1;
588 typedef struct {
589 int src_index;
590 struct iovec *src_iov;
591 void *dest_base;
592 } IOVectorSortElem;
594 static int sortelem_cmp_src_base(const void *a, const void *b)
596 const IOVectorSortElem *elem_a = a;
597 const IOVectorSortElem *elem_b = b;
599 /* Don't overflow */
600 if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
601 return -1;
602 } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
603 return 1;
604 } else {
605 return 0;
609 static int sortelem_cmp_src_index(const void *a, const void *b)
611 const IOVectorSortElem *elem_a = a;
612 const IOVectorSortElem *elem_b = b;
614 return elem_a->src_index - elem_b->src_index;
618 * Copy contents of I/O vector
620 * The relative relationships of overlapping iovecs are preserved. This is
621 * necessary to ensure identical semantics in the cloned I/O vector.
623 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
625 IOVectorSortElem sortelems[src->niov];
626 void *last_end;
627 int i;
629 /* Sort by source iovecs by base address */
630 for (i = 0; i < src->niov; i++) {
631 sortelems[i].src_index = i;
632 sortelems[i].src_iov = &src->iov[i];
634 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
636 /* Allocate buffer space taking into account overlapping iovecs */
637 last_end = NULL;
638 for (i = 0; i < src->niov; i++) {
639 struct iovec *cur = sortelems[i].src_iov;
640 ptrdiff_t rewind = 0;
642 /* Detect overlap */
643 if (last_end && last_end > cur->iov_base) {
644 rewind = last_end - cur->iov_base;
647 sortelems[i].dest_base = buf - rewind;
648 buf += cur->iov_len - MIN(rewind, cur->iov_len);
649 last_end = MAX(cur->iov_base + cur->iov_len, last_end);
652 /* Sort by source iovec index and build destination iovec */
653 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
654 for (i = 0; i < src->niov; i++) {
655 qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
659 void iov_discard_undo(IOVDiscardUndo *undo)
661 /* Restore original iovec if it was modified */
662 if (undo->modified_iov) {
663 *undo->modified_iov = undo->orig;
667 size_t iov_discard_front_undoable(struct iovec **iov,
668 unsigned int *iov_cnt,
669 size_t bytes,
670 IOVDiscardUndo *undo)
672 size_t total = 0;
673 struct iovec *cur;
675 if (undo) {
676 undo->modified_iov = NULL;
679 for (cur = *iov; *iov_cnt > 0; cur++) {
680 if (cur->iov_len > bytes) {
681 if (undo) {
682 undo->modified_iov = cur;
683 undo->orig = *cur;
686 cur->iov_base += bytes;
687 cur->iov_len -= bytes;
688 total += bytes;
689 break;
692 bytes -= cur->iov_len;
693 total += cur->iov_len;
694 *iov_cnt -= 1;
697 *iov = cur;
698 return total;
701 size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
702 size_t bytes)
704 return iov_discard_front_undoable(iov, iov_cnt, bytes, NULL);
707 size_t iov_discard_back_undoable(struct iovec *iov,
708 unsigned int *iov_cnt,
709 size_t bytes,
710 IOVDiscardUndo *undo)
712 size_t total = 0;
713 struct iovec *cur;
715 if (undo) {
716 undo->modified_iov = NULL;
719 if (*iov_cnt == 0) {
720 return 0;
723 cur = iov + (*iov_cnt - 1);
725 while (*iov_cnt > 0) {
726 if (cur->iov_len > bytes) {
727 if (undo) {
728 undo->modified_iov = cur;
729 undo->orig = *cur;
732 cur->iov_len -= bytes;
733 total += bytes;
734 break;
737 bytes -= cur->iov_len;
738 total += cur->iov_len;
739 cur--;
740 *iov_cnt -= 1;
743 return total;
746 size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
747 size_t bytes)
749 return iov_discard_back_undoable(iov, iov_cnt, bytes, NULL);
752 void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
754 size_t total;
755 unsigned int niov = qiov->niov;
757 assert(qiov->size >= bytes);
758 total = iov_discard_back(qiov->iov, &niov, bytes);
759 assert(total == bytes);
761 qiov->niov = niov;
762 qiov->size -= bytes;