]> git.proxmox.com Git - mirror_qemu.git/blame - util/iov.c
tests/tcg/s390x: Add rxsbg.c
[mirror_qemu.git] / util / iov.c
CommitLineData
e4d5639d
AS
1/*
2 * Helpers for getting linearized buffers from iov / filling buffers into iovs
3 *
4 * Copyright IBM, Corp. 2007, 2008
5 * Copyright (C) 2010 Red Hat, Inc.
6 *
7 * Author(s):
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
2278a69e 10 * Michael Tokarev <mjt@tls.msk.ru>
e4d5639d
AS
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
6b620ca3
PB
14 *
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.
e4d5639d
AS
17 */
18
aafd7584 19#include "qemu/osdep.h"
1de7afc9 20#include "qemu/iov.h"
cc99c6f5 21#include "qemu/sockets.h"
f348b6d1 22#include "qemu/cutils.h"
25e5e4c7 23
ad523bca
PB
24size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
25 size_t offset, const void *buf, size_t bytes)
e4d5639d 26{
2278a69e 27 size_t done;
e4d5639d 28 unsigned int i;
2278a69e
MT
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;
348e7b8d 37 }
e4d5639d 38 }
2278a69e
MT
39 assert(offset == 0);
40 return done;
e4d5639d 41}
fa6111f2 42
ad523bca
PB
43size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
44 size_t offset, void *buf, size_t bytes)
fa6111f2 45{
2278a69e 46 size_t done;
fa6111f2 47 unsigned int i;
2278a69e
MT
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;
fa6111f2 56 }
8d15028e 57 }
2278a69e
MT
58 assert(offset == 0);
59 return done;
8d15028e
GH
60}
61
dcf6f5e1 62size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
2278a69e 63 size_t offset, int fillc, size_t bytes)
8d15028e 64{
2278a69e 65 size_t done;
8d15028e 66 unsigned int i;
2278a69e
MT
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;
8d15028e 75 }
fa6111f2 76 }
2278a69e
MT
77 assert(offset == 0);
78 return done;
fa6111f2
AS
79}
80
348e7b8d 81size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
fa6111f2
AS
82{
83 size_t len;
84 unsigned int i;
85
86 len = 0;
348e7b8d 87 for (i = 0; i < iov_cnt; i++) {
fa6111f2
AS
88 len += iov[i].iov_len;
89 }
90 return len;
91}
3a1dca94 92
25e5e4c7
MT
93/* helper function for iov_send_recv() */
94static ssize_t
95do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
96{
9adea5f7 97#ifdef CONFIG_POSIX
25e5e4c7
MT
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() */
c0958559
SW
112 unsigned i = 0;
113 ssize_t ret = 0;
3f08376c 114 ssize_t off = 0;
c0958559 115 while (i < iov_cnt) {
25e5e4c7 116 ssize_t r = do_send
3f08376c
MAL
117 ? send(sockfd, iov[i].iov_base + off, iov[i].iov_len - off, 0)
118 : recv(sockfd, iov[i].iov_base + off, iov[i].iov_len - off, 0);
25e5e4c7
MT
119 if (r > 0) {
120 ret += r;
3f08376c
MAL
121 off += r;
122 if (off < iov[i].iov_len) {
123 continue;
124 }
25e5e4c7
MT
125 } else if (!r) {
126 break;
127 } else if (errno == EINTR) {
128 continue;
129 } else {
130 /* else it is some "other" error,
131 * only return if there was no data processed. */
132 if (ret == 0) {
c0958559 133 ret = -1;
25e5e4c7
MT
134 }
135 break;
136 }
3f08376c 137 off = 0;
c0958559 138 i++;
25e5e4c7 139 }
c0958559 140 return ret;
25e5e4c7
MT
141#endif
142}
143
6b64640d 144ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
25e5e4c7
MT
145 size_t offset, size_t bytes,
146 bool do_send)
147{
83f75c26 148 ssize_t total = 0;
25e5e4c7 149 ssize_t ret;
5209d675 150 size_t orig_len, tail;
f48869ad 151 unsigned niov;
6b64640d
WC
152 struct iovec *local_iov, *iov;
153
154 if (bytes <= 0) {
155 return 0;
156 }
157
158 local_iov = g_new0(struct iovec, iov_cnt);
159 iov_copy(local_iov, iov_cnt, _iov, iov_cnt, offset, bytes);
160 offset = 0;
161 iov = local_iov;
5209d675 162
83f75c26
PB
163 while (bytes > 0) {
164 /* Find the start position, skipping `offset' bytes:
165 * first, skip all full-sized vector elements, */
166 for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
167 offset -= iov[niov].iov_len;
168 }
cb6247a7 169
83f75c26
PB
170 /* niov == iov_cnt would only be valid if bytes == 0, which
171 * we already ruled out in the loop condition. */
f48869ad 172 assert(niov < iov_cnt);
83f75c26
PB
173 iov += niov;
174 iov_cnt -= niov;
175
176 if (offset) {
177 /* second, skip `offset' bytes from the (now) first element,
178 * undo it on exit */
179 iov[0].iov_base += offset;
180 iov[0].iov_len -= offset;
181 }
182 /* Find the end position skipping `bytes' bytes: */
183 /* first, skip all full-sized elements */
184 tail = bytes;
185 for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
186 tail -= iov[niov].iov_len;
187 }
188 if (tail) {
189 /* second, fixup the last element, and remember the original
190 * length */
191 assert(niov < iov_cnt);
192 assert(iov[niov].iov_len > tail);
193 orig_len = iov[niov].iov_len;
194 iov[niov++].iov_len = tail;
2be178a4
MT
195 ret = do_send_recv(sockfd, iov, niov, do_send);
196 /* Undo the changes above before checking for errors */
83f75c26 197 iov[niov-1].iov_len = orig_len;
2be178a4
MT
198 } else {
199 ret = do_send_recv(sockfd, iov, niov, do_send);
83f75c26
PB
200 }
201 if (offset) {
202 iov[0].iov_base -= offset;
203 iov[0].iov_len += offset;
204 }
205
206 if (ret < 0) {
207 assert(errno != EINTR);
6b64640d 208 g_free(local_iov);
83f75c26
PB
209 if (errno == EAGAIN && total > 0) {
210 return total;
211 }
212 return -1;
213 }
214
84004290
MK
215 if (ret == 0 && !do_send) {
216 /* recv returns 0 when the peer has performed an orderly
217 * shutdown. */
218 break;
219 }
220
83f75c26
PB
221 /* Prepare for the next iteration */
222 offset += ret;
223 total += ret;
224 bytes -= ret;
25e5e4c7 225 }
25e5e4c7 226
6b64640d 227 g_free(local_iov);
83f75c26 228 return total;
25e5e4c7
MT
229}
230
231
3a1dca94
GH
232void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
233 FILE *fp, const char *prefix, size_t limit)
234{
6ff66f50
PC
235 int v;
236 size_t size = 0;
237 char *buf;
238
239 for (v = 0; v < iov_cnt; v++) {
240 size += iov[v].iov_len;
3a1dca94 241 }
6ff66f50
PC
242 size = size > limit ? limit : size;
243 buf = g_malloc(size);
244 iov_to_buf(iov, iov_cnt, 0, buf, size);
b42581f5 245 qemu_hexdump(fp, prefix, buf, size);
6ff66f50 246 g_free(buf);
3a1dca94 247}
0191253c 248
d336336c
MT
249unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
250 const struct iovec *iov, unsigned int iov_cnt,
251 size_t offset, size_t bytes)
252{
253 size_t len;
254 unsigned int i, j;
e911765c
SL
255 for (i = 0, j = 0;
256 i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) {
d336336c
MT
257 if (offset >= iov[i].iov_len) {
258 offset -= iov[i].iov_len;
259 continue;
260 }
261 len = MIN(bytes, iov[i].iov_len - offset);
262
263 dst_iov[j].iov_base = iov[i].iov_base + offset;
264 dst_iov[j].iov_len = len;
265 j++;
266 bytes -= len;
267 offset = 0;
268 }
269 assert(offset == 0);
270 return j;
271}
f563a5d7 272
0191253c
PB
273/* io vectors */
274
275void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
276{
e1cf5582 277 qiov->iov = g_new(struct iovec, alloc_hint);
0191253c
PB
278 qiov->niov = 0;
279 qiov->nalloc = alloc_hint;
280 qiov->size = 0;
281}
282
283void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
284{
285 int i;
286
287 qiov->iov = iov;
288 qiov->niov = niov;
289 qiov->nalloc = -1;
290 qiov->size = 0;
291 for (i = 0; i < niov; i++)
292 qiov->size += iov[i].iov_len;
293}
294
295void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
296{
297 assert(qiov->nalloc != -1);
298
299 if (qiov->niov == qiov->nalloc) {
300 qiov->nalloc = 2 * qiov->nalloc + 1;
e1cf5582 301 qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
0191253c
PB
302 }
303 qiov->iov[qiov->niov].iov_base = base;
304 qiov->iov[qiov->niov].iov_len = len;
305 qiov->size += len;
306 ++qiov->niov;
307}
308
309/*
530c0bbd 310 * Concatenates (partial) iovecs from src_iov to the end of dst.
0191253c
PB
311 * It starts copying after skipping `soffset' bytes at the
312 * beginning of src and adds individual vectors from src to
313 * dst copies up to `sbytes' bytes total, or up to the end
530c0bbd 314 * of src_iov if it comes first. This way, it is okay to specify
0191253c
PB
315 * very large value for `sbytes' to indicate "up to the end
316 * of src".
317 * Only vector pointers are processed, not the actual data buffers.
318 */
519661ee
PB
319size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
320 struct iovec *src_iov, unsigned int src_cnt,
321 size_t soffset, size_t sbytes)
0191253c
PB
322{
323 int i;
324 size_t done;
facf98ad
AK
325
326 if (!sbytes) {
519661ee 327 return 0;
facf98ad 328 }
0191253c 329 assert(dst->nalloc != -1);
530c0bbd
SH
330 for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
331 if (soffset < src_iov[i].iov_len) {
332 size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
333 qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
0191253c
PB
334 done += len;
335 soffset = 0;
336 } else {
530c0bbd 337 soffset -= src_iov[i].iov_len;
0191253c
PB
338 }
339 }
530c0bbd 340 assert(soffset == 0); /* offset beyond end of src */
519661ee
PB
341
342 return done;
530c0bbd
SH
343}
344
345/*
346 * Concatenates (partial) iovecs from src to the end of dst.
347 * It starts copying after skipping `soffset' bytes at the
348 * beginning of src and adds individual vectors from src to
349 * dst copies up to `sbytes' bytes total, or up to the end
350 * of src if it comes first. This way, it is okay to specify
351 * very large value for `sbytes' to indicate "up to the end
352 * of src".
353 * Only vector pointers are processed, not the actual data buffers.
354 */
355void qemu_iovec_concat(QEMUIOVector *dst,
356 QEMUIOVector *src, size_t soffset, size_t sbytes)
357{
358 qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
0191253c
PB
359}
360
d953169d
VSO
361/*
362 * qiov_find_iov
363 *
364 * Return pointer to iovec structure, where byte at @offset in original vector
365 * @iov exactly is.
366 * Set @remaining_offset to be offset inside that iovec to the same byte.
367 */
368static struct iovec *iov_skip_offset(struct iovec *iov, size_t offset,
369 size_t *remaining_offset)
370{
371 while (offset > 0 && offset >= iov->iov_len) {
372 offset -= iov->iov_len;
373 iov++;
374 }
375 *remaining_offset = offset;
376
377 return iov;
378}
379
380/*
381 * qiov_slice
382 *
383 * Find subarray of iovec's, containing requested range. @head would
384 * be offset in first iov (returned by the function), @tail would be
385 * count of extra bytes in last iovec (returned iov + @niov - 1).
386 */
387static struct iovec *qiov_slice(QEMUIOVector *qiov,
388 size_t offset, size_t len,
389 size_t *head, size_t *tail, int *niov)
390{
391 struct iovec *iov, *end_iov;
392
393 assert(offset + len <= qiov->size);
394
395 iov = iov_skip_offset(qiov->iov, offset, head);
396 end_iov = iov_skip_offset(iov, *head + len, tail);
397
398 if (*tail > 0) {
399 assert(*tail < end_iov->iov_len);
400 *tail = end_iov->iov_len - *tail;
401 end_iov++;
402 }
403
404 *niov = end_iov - iov;
405
406 return iov;
407}
408
5396234b
VSO
409int qemu_iovec_subvec_niov(QEMUIOVector *qiov, size_t offset, size_t len)
410{
411 size_t head, tail;
412 int niov;
413
414 qiov_slice(qiov, offset, len, &head, &tail, &niov);
415
416 return niov;
417}
418
d953169d
VSO
419/*
420 * Compile new iovec, combining @head_buf buffer, sub-qiov of @mid_qiov,
421 * and @tail_buf buffer into new qiov.
422 */
4c002cef 423int qemu_iovec_init_extended(
d953169d
VSO
424 QEMUIOVector *qiov,
425 void *head_buf, size_t head_len,
426 QEMUIOVector *mid_qiov, size_t mid_offset, size_t mid_len,
427 void *tail_buf, size_t tail_len)
428{
429 size_t mid_head, mid_tail;
430 int total_niov, mid_niov = 0;
d38d6de2 431 struct iovec *p, *mid_iov = NULL;
d953169d 432
4c002cef
VSO
433 assert(mid_qiov->niov <= IOV_MAX);
434
435 if (SIZE_MAX - head_len < mid_len ||
436 SIZE_MAX - head_len - mid_len < tail_len)
437 {
438 return -EINVAL;
439 }
440
d953169d
VSO
441 if (mid_len) {
442 mid_iov = qiov_slice(mid_qiov, mid_offset, mid_len,
443 &mid_head, &mid_tail, &mid_niov);
444 }
445
446 total_niov = !!head_len + mid_niov + !!tail_len;
4c002cef
VSO
447 if (total_niov > IOV_MAX) {
448 return -EINVAL;
449 }
450
d953169d
VSO
451 if (total_niov == 1) {
452 qemu_iovec_init_buf(qiov, NULL, 0);
453 p = &qiov->local_iov;
454 } else {
455 qiov->niov = qiov->nalloc = total_niov;
456 qiov->size = head_len + mid_len + tail_len;
457 p = qiov->iov = g_new(struct iovec, qiov->niov);
458 }
459
460 if (head_len) {
461 p->iov_base = head_buf;
462 p->iov_len = head_len;
463 p++;
464 }
465
d38d6de2
VSO
466 assert(!mid_niov == !mid_len);
467 if (mid_niov) {
d953169d
VSO
468 memcpy(p, mid_iov, mid_niov * sizeof(*p));
469 p[0].iov_base = (uint8_t *)p[0].iov_base + mid_head;
470 p[0].iov_len -= mid_head;
471 p[mid_niov - 1].iov_len -= mid_tail;
472 p += mid_niov;
473 }
474
475 if (tail_len) {
476 p->iov_base = tail_buf;
477 p->iov_len = tail_len;
478 }
4c002cef
VSO
479
480 return 0;
d953169d
VSO
481}
482
43f35cb5 483/*
f76889e7 484 * Check if the contents of subrange of qiov data is all zeroes.
43f35cb5 485 */
f76889e7 486bool qemu_iovec_is_zero(QEMUIOVector *qiov, size_t offset, size_t bytes)
43f35cb5 487{
f76889e7
VSO
488 struct iovec *iov;
489 size_t current_offset;
490
491 assert(offset + bytes <= qiov->size);
492
493 iov = iov_skip_offset(qiov->iov, offset, &current_offset);
494
495 while (bytes) {
496 uint8_t *base = (uint8_t *)iov->iov_base + current_offset;
497 size_t len = MIN(iov->iov_len - current_offset, bytes);
498
499 if (!buffer_is_zero(base, len)) {
43f35cb5
PL
500 return false;
501 }
f76889e7
VSO
502
503 current_offset = 0;
504 bytes -= len;
505 iov++;
43f35cb5 506 }
f76889e7 507
43f35cb5
PL
508 return true;
509}
510
d953169d
VSO
511void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source,
512 size_t offset, size_t len)
513{
4c002cef
VSO
514 int ret;
515
516 assert(source->size >= len);
517 assert(source->size - len >= offset);
518
519 /* We shrink the request, so we can't overflow neither size_t nor MAX_IOV */
520 ret = qemu_iovec_init_extended(qiov, NULL, 0, source, offset, len, NULL, 0);
521 assert(ret == 0);
d953169d
VSO
522}
523
0191253c
PB
524void qemu_iovec_destroy(QEMUIOVector *qiov)
525{
d953169d
VSO
526 if (qiov->nalloc != -1) {
527 g_free(qiov->iov);
528 }
0191253c 529
d953169d 530 memset(qiov, 0, sizeof(*qiov));
0191253c
PB
531}
532
533void qemu_iovec_reset(QEMUIOVector *qiov)
534{
535 assert(qiov->nalloc != -1);
536
537 qiov->niov = 0;
538 qiov->size = 0;
539}
540
541size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
542 void *buf, size_t bytes)
543{
544 return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
545}
546
547size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
548 const void *buf, size_t bytes)
549{
550 return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
551}
552
553size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
554 int fillc, size_t bytes)
555{
556 return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
557}
d0277635 558
f70d7f7e
BC
559/**
560 * Check that I/O vector contents are identical
561 *
562 * The IO vectors must have the same structure (same length of all parts).
563 * A typical usage is to compare vectors created with qemu_iovec_clone().
564 *
565 * @a: I/O vector
566 * @b: I/O vector
567 * @ret: Offset to first mismatching byte or -1 if match
568 */
569ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
570{
571 int i;
572 ssize_t offset = 0;
573
574 assert(a->niov == b->niov);
575 for (i = 0; i < a->niov; i++) {
576 size_t len = 0;
577 uint8_t *p = (uint8_t *)a->iov[i].iov_base;
578 uint8_t *q = (uint8_t *)b->iov[i].iov_base;
579
580 assert(a->iov[i].iov_len == b->iov[i].iov_len);
581 while (len < a->iov[i].iov_len && *p++ == *q++) {
582 len++;
583 }
584
585 offset += len;
586
587 if (len != a->iov[i].iov_len) {
588 return offset;
589 }
590 }
591 return -1;
592}
593
594typedef struct {
595 int src_index;
596 struct iovec *src_iov;
597 void *dest_base;
598} IOVectorSortElem;
599
600static int sortelem_cmp_src_base(const void *a, const void *b)
601{
602 const IOVectorSortElem *elem_a = a;
603 const IOVectorSortElem *elem_b = b;
604
605 /* Don't overflow */
606 if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
607 return -1;
608 } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
609 return 1;
610 } else {
611 return 0;
612 }
613}
614
615static int sortelem_cmp_src_index(const void *a, const void *b)
616{
617 const IOVectorSortElem *elem_a = a;
618 const IOVectorSortElem *elem_b = b;
619
620 return elem_a->src_index - elem_b->src_index;
621}
622
623/**
624 * Copy contents of I/O vector
625 *
626 * The relative relationships of overlapping iovecs are preserved. This is
627 * necessary to ensure identical semantics in the cloned I/O vector.
628 */
629void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
630{
631 IOVectorSortElem sortelems[src->niov];
632 void *last_end;
633 int i;
634
635 /* Sort by source iovecs by base address */
636 for (i = 0; i < src->niov; i++) {
637 sortelems[i].src_index = i;
638 sortelems[i].src_iov = &src->iov[i];
639 }
640 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
641
642 /* Allocate buffer space taking into account overlapping iovecs */
643 last_end = NULL;
644 for (i = 0; i < src->niov; i++) {
645 struct iovec *cur = sortelems[i].src_iov;
646 ptrdiff_t rewind = 0;
647
648 /* Detect overlap */
649 if (last_end && last_end > cur->iov_base) {
650 rewind = last_end - cur->iov_base;
651 }
652
653 sortelems[i].dest_base = buf - rewind;
654 buf += cur->iov_len - MIN(rewind, cur->iov_len);
655 last_end = MAX(cur->iov_base + cur->iov_len, last_end);
656 }
657
658 /* Sort by source iovec index and build destination iovec */
659 qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
660 for (i = 0; i < src->niov; i++) {
661 qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
662 }
663}
664
9dd6f7c2
SH
665void iov_discard_undo(IOVDiscardUndo *undo)
666{
667 /* Restore original iovec if it was modified */
668 if (undo->modified_iov) {
669 *undo->modified_iov = undo->orig;
670 }
671}
672
673size_t iov_discard_front_undoable(struct iovec **iov,
674 unsigned int *iov_cnt,
675 size_t bytes,
676 IOVDiscardUndo *undo)
d0277635
SH
677{
678 size_t total = 0;
679 struct iovec *cur;
680
9dd6f7c2
SH
681 if (undo) {
682 undo->modified_iov = NULL;
683 }
684
d0277635
SH
685 for (cur = *iov; *iov_cnt > 0; cur++) {
686 if (cur->iov_len > bytes) {
9dd6f7c2
SH
687 if (undo) {
688 undo->modified_iov = cur;
689 undo->orig = *cur;
690 }
691
d0277635
SH
692 cur->iov_base += bytes;
693 cur->iov_len -= bytes;
694 total += bytes;
695 break;
696 }
697
698 bytes -= cur->iov_len;
699 total += cur->iov_len;
700 *iov_cnt -= 1;
701 }
702
703 *iov = cur;
704 return total;
705}
706
9dd6f7c2
SH
707size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
708 size_t bytes)
709{
710 return iov_discard_front_undoable(iov, iov_cnt, bytes, NULL);
711}
712
713size_t iov_discard_back_undoable(struct iovec *iov,
714 unsigned int *iov_cnt,
715 size_t bytes,
716 IOVDiscardUndo *undo)
d0277635
SH
717{
718 size_t total = 0;
719 struct iovec *cur;
720
9dd6f7c2
SH
721 if (undo) {
722 undo->modified_iov = NULL;
723 }
724
d0277635
SH
725 if (*iov_cnt == 0) {
726 return 0;
727 }
728
729 cur = iov + (*iov_cnt - 1);
730
731 while (*iov_cnt > 0) {
732 if (cur->iov_len > bytes) {
9dd6f7c2
SH
733 if (undo) {
734 undo->modified_iov = cur;
735 undo->orig = *cur;
736 }
737
d0277635
SH
738 cur->iov_len -= bytes;
739 total += bytes;
740 break;
741 }
742
743 bytes -= cur->iov_len;
744 total += cur->iov_len;
745 cur--;
746 *iov_cnt -= 1;
747 }
748
749 return total;
750}
58f423fb 751
9dd6f7c2
SH
752size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
753 size_t bytes)
754{
755 return iov_discard_back_undoable(iov, iov_cnt, bytes, NULL);
756}
757
58f423fb
KW
758void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
759{
760 size_t total;
761 unsigned int niov = qiov->niov;
762
763 assert(qiov->size >= bytes);
764 total = iov_discard_back(qiov->iov, &niov, bytes);
765 assert(total == bytes);
766
767 qiov->niov = niov;
768 qiov->size -= bytes;
769}