xref: /qemu/util/iov.c (revision 3f08376c)
1baacf047SPaolo Bonzini /*
2baacf047SPaolo Bonzini  * Helpers for getting linearized buffers from iov / filling buffers into iovs
3baacf047SPaolo Bonzini  *
4baacf047SPaolo Bonzini  * Copyright IBM, Corp. 2007, 2008
5baacf047SPaolo Bonzini  * Copyright (C) 2010 Red Hat, Inc.
6baacf047SPaolo Bonzini  *
7baacf047SPaolo Bonzini  * Author(s):
8baacf047SPaolo Bonzini  *  Anthony Liguori <aliguori@us.ibm.com>
9baacf047SPaolo Bonzini  *  Amit Shah <amit.shah@redhat.com>
10baacf047SPaolo Bonzini  *  Michael Tokarev <mjt@tls.msk.ru>
11baacf047SPaolo Bonzini  *
12baacf047SPaolo Bonzini  * This work is licensed under the terms of the GNU GPL, version 2.  See
13baacf047SPaolo Bonzini  * the COPYING file in the top-level directory.
14baacf047SPaolo Bonzini  *
15baacf047SPaolo Bonzini  * Contributions after 2012-01-13 are licensed under the terms of the
16baacf047SPaolo Bonzini  * GNU GPL, version 2 or (at your option) any later version.
17baacf047SPaolo Bonzini  */
18baacf047SPaolo Bonzini 
19aafd7584SPeter Maydell #include "qemu/osdep.h"
20baacf047SPaolo Bonzini #include "qemu/iov.h"
21cc99c6f5SStefan Weil #include "qemu/sockets.h"
22f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
23baacf047SPaolo Bonzini 
24ad523bcaSPaolo Bonzini size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
25baacf047SPaolo Bonzini                          size_t offset, const void *buf, size_t bytes)
26baacf047SPaolo Bonzini {
27baacf047SPaolo Bonzini     size_t done;
28baacf047SPaolo Bonzini     unsigned int i;
29baacf047SPaolo Bonzini     for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
30baacf047SPaolo Bonzini         if (offset < iov[i].iov_len) {
31baacf047SPaolo Bonzini             size_t len = MIN(iov[i].iov_len - offset, bytes - done);
32baacf047SPaolo Bonzini             memcpy(iov[i].iov_base + offset, buf + done, len);
33baacf047SPaolo Bonzini             done += len;
34baacf047SPaolo Bonzini             offset = 0;
35baacf047SPaolo Bonzini         } else {
36baacf047SPaolo Bonzini             offset -= iov[i].iov_len;
37baacf047SPaolo Bonzini         }
38baacf047SPaolo Bonzini     }
39baacf047SPaolo Bonzini     assert(offset == 0);
40baacf047SPaolo Bonzini     return done;
41baacf047SPaolo Bonzini }
42baacf047SPaolo Bonzini 
43ad523bcaSPaolo Bonzini size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
44baacf047SPaolo Bonzini                        size_t offset, void *buf, size_t bytes)
45baacf047SPaolo Bonzini {
46baacf047SPaolo Bonzini     size_t done;
47baacf047SPaolo Bonzini     unsigned int i;
48baacf047SPaolo Bonzini     for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
49baacf047SPaolo Bonzini         if (offset < iov[i].iov_len) {
50baacf047SPaolo Bonzini             size_t len = MIN(iov[i].iov_len - offset, bytes - done);
51baacf047SPaolo Bonzini             memcpy(buf + done, iov[i].iov_base + offset, len);
52baacf047SPaolo Bonzini             done += len;
53baacf047SPaolo Bonzini             offset = 0;
54baacf047SPaolo Bonzini         } else {
55baacf047SPaolo Bonzini             offset -= iov[i].iov_len;
56baacf047SPaolo Bonzini         }
57baacf047SPaolo Bonzini     }
58baacf047SPaolo Bonzini     assert(offset == 0);
59baacf047SPaolo Bonzini     return done;
60baacf047SPaolo Bonzini }
61baacf047SPaolo Bonzini 
62baacf047SPaolo Bonzini size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
63baacf047SPaolo Bonzini                   size_t offset, int fillc, size_t bytes)
64baacf047SPaolo Bonzini {
65baacf047SPaolo Bonzini     size_t done;
66baacf047SPaolo Bonzini     unsigned int i;
67baacf047SPaolo Bonzini     for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
68baacf047SPaolo Bonzini         if (offset < iov[i].iov_len) {
69baacf047SPaolo Bonzini             size_t len = MIN(iov[i].iov_len - offset, bytes - done);
70baacf047SPaolo Bonzini             memset(iov[i].iov_base + offset, fillc, len);
71baacf047SPaolo Bonzini             done += len;
72baacf047SPaolo Bonzini             offset = 0;
73baacf047SPaolo Bonzini         } else {
74baacf047SPaolo Bonzini             offset -= iov[i].iov_len;
75baacf047SPaolo Bonzini         }
76baacf047SPaolo Bonzini     }
77baacf047SPaolo Bonzini     assert(offset == 0);
78baacf047SPaolo Bonzini     return done;
79baacf047SPaolo Bonzini }
80baacf047SPaolo Bonzini 
81baacf047SPaolo Bonzini size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
82baacf047SPaolo Bonzini {
83baacf047SPaolo Bonzini     size_t len;
84baacf047SPaolo Bonzini     unsigned int i;
85baacf047SPaolo Bonzini 
86baacf047SPaolo Bonzini     len = 0;
87baacf047SPaolo Bonzini     for (i = 0; i < iov_cnt; i++) {
88baacf047SPaolo Bonzini         len += iov[i].iov_len;
89baacf047SPaolo Bonzini     }
90baacf047SPaolo Bonzini     return len;
91baacf047SPaolo Bonzini }
92baacf047SPaolo Bonzini 
93baacf047SPaolo Bonzini /* helper function for iov_send_recv() */
94baacf047SPaolo Bonzini static ssize_t
95baacf047SPaolo Bonzini do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
96baacf047SPaolo Bonzini {
979adea5f7SPaolo Bonzini #ifdef CONFIG_POSIX
98baacf047SPaolo Bonzini     ssize_t ret;
99baacf047SPaolo Bonzini     struct msghdr msg;
100baacf047SPaolo Bonzini     memset(&msg, 0, sizeof(msg));
101baacf047SPaolo Bonzini     msg.msg_iov = iov;
102baacf047SPaolo Bonzini     msg.msg_iovlen = iov_cnt;
103baacf047SPaolo Bonzini     do {
104baacf047SPaolo Bonzini         ret = do_send
105baacf047SPaolo Bonzini             ? sendmsg(sockfd, &msg, 0)
106baacf047SPaolo Bonzini             : recvmsg(sockfd, &msg, 0);
107baacf047SPaolo Bonzini     } while (ret < 0 && errno == EINTR);
108baacf047SPaolo Bonzini     return ret;
109baacf047SPaolo Bonzini #else
110baacf047SPaolo Bonzini     /* else send piece-by-piece */
111baacf047SPaolo Bonzini     /*XXX Note: windows has WSASend() and WSARecv() */
112baacf047SPaolo Bonzini     unsigned i = 0;
113baacf047SPaolo Bonzini     ssize_t ret = 0;
1143f08376cSMarc-André Lureau     ssize_t off = 0;
115baacf047SPaolo Bonzini     while (i < iov_cnt) {
116baacf047SPaolo Bonzini         ssize_t r = do_send
1173f08376cSMarc-André Lureau             ? send(sockfd, iov[i].iov_base + off, iov[i].iov_len - off, 0)
1183f08376cSMarc-André Lureau             : recv(sockfd, iov[i].iov_base + off, iov[i].iov_len - off, 0);
119baacf047SPaolo Bonzini         if (r > 0) {
120baacf047SPaolo Bonzini             ret += r;
1213f08376cSMarc-André Lureau             off += r;
1223f08376cSMarc-André Lureau             if (off < iov[i].iov_len) {
1233f08376cSMarc-André Lureau                 continue;
1243f08376cSMarc-André Lureau             }
125baacf047SPaolo Bonzini         } else if (!r) {
126baacf047SPaolo Bonzini             break;
127baacf047SPaolo Bonzini         } else if (errno == EINTR) {
128baacf047SPaolo Bonzini             continue;
129baacf047SPaolo Bonzini         } else {
130baacf047SPaolo Bonzini             /* else it is some "other" error,
131baacf047SPaolo Bonzini              * only return if there was no data processed. */
132baacf047SPaolo Bonzini             if (ret == 0) {
133baacf047SPaolo Bonzini                 ret = -1;
134baacf047SPaolo Bonzini             }
135baacf047SPaolo Bonzini             break;
136baacf047SPaolo Bonzini         }
1373f08376cSMarc-André Lureau         off = 0;
138baacf047SPaolo Bonzini         i++;
139baacf047SPaolo Bonzini     }
140baacf047SPaolo Bonzini     return ret;
141baacf047SPaolo Bonzini #endif
142baacf047SPaolo Bonzini }
143baacf047SPaolo Bonzini 
1446b64640dSWen Congyang ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
145baacf047SPaolo Bonzini                       size_t offset, size_t bytes,
146baacf047SPaolo Bonzini                       bool do_send)
147baacf047SPaolo Bonzini {
14883f75c26SPaolo Bonzini     ssize_t total = 0;
149baacf047SPaolo Bonzini     ssize_t ret;
1505209d675SPaolo Bonzini     size_t orig_len, tail;
151f48869adSPaolo Bonzini     unsigned niov;
1526b64640dSWen Congyang     struct iovec *local_iov, *iov;
1536b64640dSWen Congyang 
1546b64640dSWen Congyang     if (bytes <= 0) {
1556b64640dSWen Congyang         return 0;
1566b64640dSWen Congyang     }
1576b64640dSWen Congyang 
1586b64640dSWen Congyang     local_iov = g_new0(struct iovec, iov_cnt);
1596b64640dSWen Congyang     iov_copy(local_iov, iov_cnt, _iov, iov_cnt, offset, bytes);
1606b64640dSWen Congyang     offset = 0;
1616b64640dSWen Congyang     iov = local_iov;
1625209d675SPaolo Bonzini 
16383f75c26SPaolo Bonzini     while (bytes > 0) {
164baacf047SPaolo Bonzini         /* Find the start position, skipping `offset' bytes:
165baacf047SPaolo Bonzini          * first, skip all full-sized vector elements, */
166f48869adSPaolo Bonzini         for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
167f48869adSPaolo Bonzini             offset -= iov[niov].iov_len;
168baacf047SPaolo Bonzini         }
169cb6247a7SPaolo Bonzini 
170f48869adSPaolo Bonzini         /* niov == iov_cnt would only be valid if bytes == 0, which
17183f75c26SPaolo Bonzini          * we already ruled out in the loop condition.  */
172f48869adSPaolo Bonzini         assert(niov < iov_cnt);
173f48869adSPaolo Bonzini         iov += niov;
174f48869adSPaolo Bonzini         iov_cnt -= niov;
175cb6247a7SPaolo Bonzini 
176cb6247a7SPaolo Bonzini         if (offset) {
177baacf047SPaolo Bonzini             /* second, skip `offset' bytes from the (now) first element,
178baacf047SPaolo Bonzini              * undo it on exit */
179cb6247a7SPaolo Bonzini             iov[0].iov_base += offset;
180cb6247a7SPaolo Bonzini             iov[0].iov_len -= offset;
181baacf047SPaolo Bonzini         }
182baacf047SPaolo Bonzini         /* Find the end position skipping `bytes' bytes: */
183baacf047SPaolo Bonzini         /* first, skip all full-sized elements */
1845209d675SPaolo Bonzini         tail = bytes;
185f48869adSPaolo Bonzini         for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
186f48869adSPaolo Bonzini             tail -= iov[niov].iov_len;
187baacf047SPaolo Bonzini         }
1885209d675SPaolo Bonzini         if (tail) {
1895209d675SPaolo Bonzini             /* second, fixup the last element, and remember the original
1905209d675SPaolo Bonzini              * length */
191f48869adSPaolo Bonzini             assert(niov < iov_cnt);
192f48869adSPaolo Bonzini             assert(iov[niov].iov_len > tail);
193f48869adSPaolo Bonzini             orig_len = iov[niov].iov_len;
194f48869adSPaolo Bonzini             iov[niov++].iov_len = tail;
195f48869adSPaolo Bonzini             ret = do_send_recv(sockfd, iov, niov, do_send);
19683f75c26SPaolo Bonzini             /* Undo the changes above before checking for errors */
197f48869adSPaolo Bonzini             iov[niov-1].iov_len = orig_len;
1982be178a4SMichael Tokarev         } else {
1992be178a4SMichael Tokarev             ret = do_send_recv(sockfd, iov, niov, do_send);
2005209d675SPaolo Bonzini         }
201baacf047SPaolo Bonzini         if (offset) {
202cb6247a7SPaolo Bonzini             iov[0].iov_base -= offset;
203cb6247a7SPaolo Bonzini             iov[0].iov_len += offset;
204baacf047SPaolo Bonzini         }
205baacf047SPaolo Bonzini 
20683f75c26SPaolo Bonzini         if (ret < 0) {
20783f75c26SPaolo Bonzini             assert(errno != EINTR);
2086b64640dSWen Congyang             g_free(local_iov);
20983f75c26SPaolo Bonzini             if (errno == EAGAIN && total > 0) {
21083f75c26SPaolo Bonzini                 return total;
21183f75c26SPaolo Bonzini             }
21283f75c26SPaolo Bonzini             return -1;
21383f75c26SPaolo Bonzini         }
21483f75c26SPaolo Bonzini 
21584004290SMORITA Kazutaka         if (ret == 0 && !do_send) {
21684004290SMORITA Kazutaka             /* recv returns 0 when the peer has performed an orderly
21784004290SMORITA Kazutaka              * shutdown. */
21884004290SMORITA Kazutaka             break;
21984004290SMORITA Kazutaka         }
22084004290SMORITA Kazutaka 
22183f75c26SPaolo Bonzini         /* Prepare for the next iteration */
22283f75c26SPaolo Bonzini         offset += ret;
22383f75c26SPaolo Bonzini         total += ret;
22483f75c26SPaolo Bonzini         bytes -= ret;
22583f75c26SPaolo Bonzini     }
22683f75c26SPaolo Bonzini 
2276b64640dSWen Congyang     g_free(local_iov);
22883f75c26SPaolo Bonzini     return total;
229baacf047SPaolo Bonzini }
230baacf047SPaolo Bonzini 
231baacf047SPaolo Bonzini 
232baacf047SPaolo Bonzini void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
233baacf047SPaolo Bonzini                  FILE *fp, const char *prefix, size_t limit)
234baacf047SPaolo Bonzini {
2356ff66f50SPeter Crosthwaite     int v;
2366ff66f50SPeter Crosthwaite     size_t size = 0;
2376ff66f50SPeter Crosthwaite     char *buf;
238baacf047SPaolo Bonzini 
2396ff66f50SPeter Crosthwaite     for (v = 0; v < iov_cnt; v++) {
2406ff66f50SPeter Crosthwaite         size += iov[v].iov_len;
241baacf047SPaolo Bonzini     }
2426ff66f50SPeter Crosthwaite     size = size > limit ? limit : size;
2436ff66f50SPeter Crosthwaite     buf = g_malloc(size);
2446ff66f50SPeter Crosthwaite     iov_to_buf(iov, iov_cnt, 0, buf, size);
245b42581f5SPhilippe Mathieu-Daudé     qemu_hexdump(fp, prefix, buf, size);
2466ff66f50SPeter Crosthwaite     g_free(buf);
247baacf047SPaolo Bonzini }
248baacf047SPaolo Bonzini 
249baacf047SPaolo Bonzini unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
250baacf047SPaolo Bonzini                  const struct iovec *iov, unsigned int iov_cnt,
251baacf047SPaolo Bonzini                  size_t offset, size_t bytes)
252baacf047SPaolo Bonzini {
253baacf047SPaolo Bonzini     size_t len;
254baacf047SPaolo Bonzini     unsigned int i, j;
255e911765cSShmulik Ladkani     for (i = 0, j = 0;
256e911765cSShmulik Ladkani          i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) {
257baacf047SPaolo Bonzini         if (offset >= iov[i].iov_len) {
258baacf047SPaolo Bonzini             offset -= iov[i].iov_len;
259baacf047SPaolo Bonzini             continue;
260baacf047SPaolo Bonzini         }
261baacf047SPaolo Bonzini         len = MIN(bytes, iov[i].iov_len - offset);
262baacf047SPaolo Bonzini 
263baacf047SPaolo Bonzini         dst_iov[j].iov_base = iov[i].iov_base + offset;
264baacf047SPaolo Bonzini         dst_iov[j].iov_len = len;
265baacf047SPaolo Bonzini         j++;
266baacf047SPaolo Bonzini         bytes -= len;
267baacf047SPaolo Bonzini         offset = 0;
268baacf047SPaolo Bonzini     }
269baacf047SPaolo Bonzini     assert(offset == 0);
270baacf047SPaolo Bonzini     return j;
271baacf047SPaolo Bonzini }
272baacf047SPaolo Bonzini 
273baacf047SPaolo Bonzini /* io vectors */
274baacf047SPaolo Bonzini 
275baacf047SPaolo Bonzini void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
276baacf047SPaolo Bonzini {
277e1cf5582SMarkus Armbruster     qiov->iov = g_new(struct iovec, alloc_hint);
278baacf047SPaolo Bonzini     qiov->niov = 0;
279baacf047SPaolo Bonzini     qiov->nalloc = alloc_hint;
280baacf047SPaolo Bonzini     qiov->size = 0;
281baacf047SPaolo Bonzini }
282baacf047SPaolo Bonzini 
283baacf047SPaolo Bonzini void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
284baacf047SPaolo Bonzini {
285baacf047SPaolo Bonzini     int i;
286baacf047SPaolo Bonzini 
287baacf047SPaolo Bonzini     qiov->iov = iov;
288baacf047SPaolo Bonzini     qiov->niov = niov;
289baacf047SPaolo Bonzini     qiov->nalloc = -1;
290baacf047SPaolo Bonzini     qiov->size = 0;
291baacf047SPaolo Bonzini     for (i = 0; i < niov; i++)
292baacf047SPaolo Bonzini         qiov->size += iov[i].iov_len;
293baacf047SPaolo Bonzini }
294baacf047SPaolo Bonzini 
295baacf047SPaolo Bonzini void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
296baacf047SPaolo Bonzini {
297baacf047SPaolo Bonzini     assert(qiov->nalloc != -1);
298baacf047SPaolo Bonzini 
299baacf047SPaolo Bonzini     if (qiov->niov == qiov->nalloc) {
300baacf047SPaolo Bonzini         qiov->nalloc = 2 * qiov->nalloc + 1;
301e1cf5582SMarkus Armbruster         qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
302baacf047SPaolo Bonzini     }
303baacf047SPaolo Bonzini     qiov->iov[qiov->niov].iov_base = base;
304baacf047SPaolo Bonzini     qiov->iov[qiov->niov].iov_len = len;
305baacf047SPaolo Bonzini     qiov->size += len;
306baacf047SPaolo Bonzini     ++qiov->niov;
307baacf047SPaolo Bonzini }
308baacf047SPaolo Bonzini 
309baacf047SPaolo Bonzini /*
310baacf047SPaolo Bonzini  * Concatenates (partial) iovecs from src_iov to the end of dst.
311baacf047SPaolo Bonzini  * It starts copying after skipping `soffset' bytes at the
312baacf047SPaolo Bonzini  * beginning of src and adds individual vectors from src to
313baacf047SPaolo Bonzini  * dst copies up to `sbytes' bytes total, or up to the end
314baacf047SPaolo Bonzini  * of src_iov if it comes first.  This way, it is okay to specify
315baacf047SPaolo Bonzini  * very large value for `sbytes' to indicate "up to the end
316baacf047SPaolo Bonzini  * of src".
317baacf047SPaolo Bonzini  * Only vector pointers are processed, not the actual data buffers.
318baacf047SPaolo Bonzini  */
319519661eeSPaolo Bonzini size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
320baacf047SPaolo Bonzini                              struct iovec *src_iov, unsigned int src_cnt,
321baacf047SPaolo Bonzini                              size_t soffset, size_t sbytes)
322baacf047SPaolo Bonzini {
323baacf047SPaolo Bonzini     int i;
324baacf047SPaolo Bonzini     size_t done;
325facf98adSAneesh Kumar K.V 
326facf98adSAneesh Kumar K.V     if (!sbytes) {
327519661eeSPaolo Bonzini         return 0;
328facf98adSAneesh Kumar K.V     }
329baacf047SPaolo Bonzini     assert(dst->nalloc != -1);
330baacf047SPaolo Bonzini     for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
331baacf047SPaolo Bonzini         if (soffset < src_iov[i].iov_len) {
332baacf047SPaolo Bonzini             size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
333baacf047SPaolo Bonzini             qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
334baacf047SPaolo Bonzini             done += len;
335baacf047SPaolo Bonzini             soffset = 0;
336baacf047SPaolo Bonzini         } else {
337baacf047SPaolo Bonzini             soffset -= src_iov[i].iov_len;
338baacf047SPaolo Bonzini         }
339baacf047SPaolo Bonzini     }
340baacf047SPaolo Bonzini     assert(soffset == 0); /* offset beyond end of src */
341519661eeSPaolo Bonzini 
342519661eeSPaolo Bonzini     return done;
343baacf047SPaolo Bonzini }
344baacf047SPaolo Bonzini 
345baacf047SPaolo Bonzini /*
346baacf047SPaolo Bonzini  * Concatenates (partial) iovecs from src to the end of dst.
347baacf047SPaolo Bonzini  * It starts copying after skipping `soffset' bytes at the
348baacf047SPaolo Bonzini  * beginning of src and adds individual vectors from src to
349baacf047SPaolo Bonzini  * dst copies up to `sbytes' bytes total, or up to the end
350baacf047SPaolo Bonzini  * of src if it comes first.  This way, it is okay to specify
351baacf047SPaolo Bonzini  * very large value for `sbytes' to indicate "up to the end
352baacf047SPaolo Bonzini  * of src".
353baacf047SPaolo Bonzini  * Only vector pointers are processed, not the actual data buffers.
354baacf047SPaolo Bonzini  */
355baacf047SPaolo Bonzini void qemu_iovec_concat(QEMUIOVector *dst,
356baacf047SPaolo Bonzini                        QEMUIOVector *src, size_t soffset, size_t sbytes)
357baacf047SPaolo Bonzini {
358baacf047SPaolo Bonzini     qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
359baacf047SPaolo Bonzini }
360baacf047SPaolo Bonzini 
36143f35cb5SPeter Lieven /*
362d953169dSVladimir Sementsov-Ogievskiy  * qiov_find_iov
363d953169dSVladimir Sementsov-Ogievskiy  *
364d953169dSVladimir Sementsov-Ogievskiy  * Return pointer to iovec structure, where byte at @offset in original vector
365d953169dSVladimir Sementsov-Ogievskiy  * @iov exactly is.
366d953169dSVladimir Sementsov-Ogievskiy  * Set @remaining_offset to be offset inside that iovec to the same byte.
367d953169dSVladimir Sementsov-Ogievskiy  */
368d953169dSVladimir Sementsov-Ogievskiy static struct iovec *iov_skip_offset(struct iovec *iov, size_t offset,
369d953169dSVladimir Sementsov-Ogievskiy                                      size_t *remaining_offset)
370d953169dSVladimir Sementsov-Ogievskiy {
371d953169dSVladimir Sementsov-Ogievskiy     while (offset > 0 && offset >= iov->iov_len) {
372d953169dSVladimir Sementsov-Ogievskiy         offset -= iov->iov_len;
373d953169dSVladimir Sementsov-Ogievskiy         iov++;
374d953169dSVladimir Sementsov-Ogievskiy     }
375d953169dSVladimir Sementsov-Ogievskiy     *remaining_offset = offset;
376d953169dSVladimir Sementsov-Ogievskiy 
377d953169dSVladimir Sementsov-Ogievskiy     return iov;
378d953169dSVladimir Sementsov-Ogievskiy }
379d953169dSVladimir Sementsov-Ogievskiy 
380d953169dSVladimir Sementsov-Ogievskiy /*
381d953169dSVladimir Sementsov-Ogievskiy  * qiov_slice
382d953169dSVladimir Sementsov-Ogievskiy  *
383d953169dSVladimir Sementsov-Ogievskiy  * Find subarray of iovec's, containing requested range. @head would
384d953169dSVladimir Sementsov-Ogievskiy  * be offset in first iov (returned by the function), @tail would be
385d953169dSVladimir Sementsov-Ogievskiy  * count of extra bytes in last iovec (returned iov + @niov - 1).
386d953169dSVladimir Sementsov-Ogievskiy  */
387d953169dSVladimir Sementsov-Ogievskiy static struct iovec *qiov_slice(QEMUIOVector *qiov,
388d953169dSVladimir Sementsov-Ogievskiy                                 size_t offset, size_t len,
389d953169dSVladimir Sementsov-Ogievskiy                                 size_t *head, size_t *tail, int *niov)
390d953169dSVladimir Sementsov-Ogievskiy {
391d953169dSVladimir Sementsov-Ogievskiy     struct iovec *iov, *end_iov;
392d953169dSVladimir Sementsov-Ogievskiy 
393d953169dSVladimir Sementsov-Ogievskiy     assert(offset + len <= qiov->size);
394d953169dSVladimir Sementsov-Ogievskiy 
395d953169dSVladimir Sementsov-Ogievskiy     iov = iov_skip_offset(qiov->iov, offset, head);
396d953169dSVladimir Sementsov-Ogievskiy     end_iov = iov_skip_offset(iov, *head + len, tail);
397d953169dSVladimir Sementsov-Ogievskiy 
398d953169dSVladimir Sementsov-Ogievskiy     if (*tail > 0) {
399d953169dSVladimir Sementsov-Ogievskiy         assert(*tail < end_iov->iov_len);
400d953169dSVladimir Sementsov-Ogievskiy         *tail = end_iov->iov_len - *tail;
401d953169dSVladimir Sementsov-Ogievskiy         end_iov++;
402d953169dSVladimir Sementsov-Ogievskiy     }
403d953169dSVladimir Sementsov-Ogievskiy 
404d953169dSVladimir Sementsov-Ogievskiy     *niov = end_iov - iov;
405d953169dSVladimir Sementsov-Ogievskiy 
406d953169dSVladimir Sementsov-Ogievskiy     return iov;
407d953169dSVladimir Sementsov-Ogievskiy }
408d953169dSVladimir Sementsov-Ogievskiy 
4095396234bSVladimir Sementsov-Ogievskiy int qemu_iovec_subvec_niov(QEMUIOVector *qiov, size_t offset, size_t len)
4105396234bSVladimir Sementsov-Ogievskiy {
4115396234bSVladimir Sementsov-Ogievskiy     size_t head, tail;
4125396234bSVladimir Sementsov-Ogievskiy     int niov;
4135396234bSVladimir Sementsov-Ogievskiy 
4145396234bSVladimir Sementsov-Ogievskiy     qiov_slice(qiov, offset, len, &head, &tail, &niov);
4155396234bSVladimir Sementsov-Ogievskiy 
4165396234bSVladimir Sementsov-Ogievskiy     return niov;
4175396234bSVladimir Sementsov-Ogievskiy }
4185396234bSVladimir Sementsov-Ogievskiy 
419d953169dSVladimir Sementsov-Ogievskiy /*
420d953169dSVladimir Sementsov-Ogievskiy  * Compile new iovec, combining @head_buf buffer, sub-qiov of @mid_qiov,
421d953169dSVladimir Sementsov-Ogievskiy  * and @tail_buf buffer into new qiov.
422d953169dSVladimir Sementsov-Ogievskiy  */
4234c002cefSVladimir Sementsov-Ogievskiy int qemu_iovec_init_extended(
424d953169dSVladimir Sementsov-Ogievskiy         QEMUIOVector *qiov,
425d953169dSVladimir Sementsov-Ogievskiy         void *head_buf, size_t head_len,
426d953169dSVladimir Sementsov-Ogievskiy         QEMUIOVector *mid_qiov, size_t mid_offset, size_t mid_len,
427d953169dSVladimir Sementsov-Ogievskiy         void *tail_buf, size_t tail_len)
428d953169dSVladimir Sementsov-Ogievskiy {
429d953169dSVladimir Sementsov-Ogievskiy     size_t mid_head, mid_tail;
430d953169dSVladimir Sementsov-Ogievskiy     int total_niov, mid_niov = 0;
431d38d6de2SVladimir Sementsov-Ogievskiy     struct iovec *p, *mid_iov = NULL;
432d953169dSVladimir Sementsov-Ogievskiy 
4334c002cefSVladimir Sementsov-Ogievskiy     assert(mid_qiov->niov <= IOV_MAX);
4344c002cefSVladimir Sementsov-Ogievskiy 
4354c002cefSVladimir Sementsov-Ogievskiy     if (SIZE_MAX - head_len < mid_len ||
4364c002cefSVladimir Sementsov-Ogievskiy         SIZE_MAX - head_len - mid_len < tail_len)
4374c002cefSVladimir Sementsov-Ogievskiy     {
4384c002cefSVladimir Sementsov-Ogievskiy         return -EINVAL;
4394c002cefSVladimir Sementsov-Ogievskiy     }
4404c002cefSVladimir Sementsov-Ogievskiy 
441d953169dSVladimir Sementsov-Ogievskiy     if (mid_len) {
442d953169dSVladimir Sementsov-Ogievskiy         mid_iov = qiov_slice(mid_qiov, mid_offset, mid_len,
443d953169dSVladimir Sementsov-Ogievskiy                              &mid_head, &mid_tail, &mid_niov);
444d953169dSVladimir Sementsov-Ogievskiy     }
445d953169dSVladimir Sementsov-Ogievskiy 
446d953169dSVladimir Sementsov-Ogievskiy     total_niov = !!head_len + mid_niov + !!tail_len;
4474c002cefSVladimir Sementsov-Ogievskiy     if (total_niov > IOV_MAX) {
4484c002cefSVladimir Sementsov-Ogievskiy         return -EINVAL;
4494c002cefSVladimir Sementsov-Ogievskiy     }
4504c002cefSVladimir Sementsov-Ogievskiy 
451d953169dSVladimir Sementsov-Ogievskiy     if (total_niov == 1) {
452d953169dSVladimir Sementsov-Ogievskiy         qemu_iovec_init_buf(qiov, NULL, 0);
453d953169dSVladimir Sementsov-Ogievskiy         p = &qiov->local_iov;
454d953169dSVladimir Sementsov-Ogievskiy     } else {
455d953169dSVladimir Sementsov-Ogievskiy         qiov->niov = qiov->nalloc = total_niov;
456d953169dSVladimir Sementsov-Ogievskiy         qiov->size = head_len + mid_len + tail_len;
457d953169dSVladimir Sementsov-Ogievskiy         p = qiov->iov = g_new(struct iovec, qiov->niov);
458d953169dSVladimir Sementsov-Ogievskiy     }
459d953169dSVladimir Sementsov-Ogievskiy 
460d953169dSVladimir Sementsov-Ogievskiy     if (head_len) {
461d953169dSVladimir Sementsov-Ogievskiy         p->iov_base = head_buf;
462d953169dSVladimir Sementsov-Ogievskiy         p->iov_len = head_len;
463d953169dSVladimir Sementsov-Ogievskiy         p++;
464d953169dSVladimir Sementsov-Ogievskiy     }
465d953169dSVladimir Sementsov-Ogievskiy 
466d38d6de2SVladimir Sementsov-Ogievskiy     assert(!mid_niov == !mid_len);
467d38d6de2SVladimir Sementsov-Ogievskiy     if (mid_niov) {
468d953169dSVladimir Sementsov-Ogievskiy         memcpy(p, mid_iov, mid_niov * sizeof(*p));
469d953169dSVladimir Sementsov-Ogievskiy         p[0].iov_base = (uint8_t *)p[0].iov_base + mid_head;
470d953169dSVladimir Sementsov-Ogievskiy         p[0].iov_len -= mid_head;
471d953169dSVladimir Sementsov-Ogievskiy         p[mid_niov - 1].iov_len -= mid_tail;
472d953169dSVladimir Sementsov-Ogievskiy         p += mid_niov;
473d953169dSVladimir Sementsov-Ogievskiy     }
474d953169dSVladimir Sementsov-Ogievskiy 
475d953169dSVladimir Sementsov-Ogievskiy     if (tail_len) {
476d953169dSVladimir Sementsov-Ogievskiy         p->iov_base = tail_buf;
477d953169dSVladimir Sementsov-Ogievskiy         p->iov_len = tail_len;
478d953169dSVladimir Sementsov-Ogievskiy     }
4794c002cefSVladimir Sementsov-Ogievskiy 
4804c002cefSVladimir Sementsov-Ogievskiy     return 0;
481d953169dSVladimir Sementsov-Ogievskiy }
482d953169dSVladimir Sementsov-Ogievskiy 
483d953169dSVladimir Sementsov-Ogievskiy /*
484f76889e7SVladimir Sementsov-Ogievskiy  * Check if the contents of subrange of qiov data is all zeroes.
48543f35cb5SPeter Lieven  */
486f76889e7SVladimir Sementsov-Ogievskiy bool qemu_iovec_is_zero(QEMUIOVector *qiov, size_t offset, size_t bytes)
48743f35cb5SPeter Lieven {
488f76889e7SVladimir Sementsov-Ogievskiy     struct iovec *iov;
489f76889e7SVladimir Sementsov-Ogievskiy     size_t current_offset;
490f76889e7SVladimir Sementsov-Ogievskiy 
491f76889e7SVladimir Sementsov-Ogievskiy     assert(offset + bytes <= qiov->size);
492f76889e7SVladimir Sementsov-Ogievskiy 
493f76889e7SVladimir Sementsov-Ogievskiy     iov = iov_skip_offset(qiov->iov, offset, &current_offset);
494f76889e7SVladimir Sementsov-Ogievskiy 
495f76889e7SVladimir Sementsov-Ogievskiy     while (bytes) {
496f76889e7SVladimir Sementsov-Ogievskiy         uint8_t *base = (uint8_t *)iov->iov_base + current_offset;
497f76889e7SVladimir Sementsov-Ogievskiy         size_t len = MIN(iov->iov_len - current_offset, bytes);
498f76889e7SVladimir Sementsov-Ogievskiy 
499f76889e7SVladimir Sementsov-Ogievskiy         if (!buffer_is_zero(base, len)) {
50043f35cb5SPeter Lieven             return false;
50143f35cb5SPeter Lieven         }
502f76889e7SVladimir Sementsov-Ogievskiy 
503f76889e7SVladimir Sementsov-Ogievskiy         current_offset = 0;
504f76889e7SVladimir Sementsov-Ogievskiy         bytes -= len;
505f76889e7SVladimir Sementsov-Ogievskiy         iov++;
50643f35cb5SPeter Lieven     }
507f76889e7SVladimir Sementsov-Ogievskiy 
50843f35cb5SPeter Lieven     return true;
50943f35cb5SPeter Lieven }
51043f35cb5SPeter Lieven 
511d953169dSVladimir Sementsov-Ogievskiy void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source,
512d953169dSVladimir Sementsov-Ogievskiy                            size_t offset, size_t len)
513d953169dSVladimir Sementsov-Ogievskiy {
5144c002cefSVladimir Sementsov-Ogievskiy     int ret;
5154c002cefSVladimir Sementsov-Ogievskiy 
5164c002cefSVladimir Sementsov-Ogievskiy     assert(source->size >= len);
5174c002cefSVladimir Sementsov-Ogievskiy     assert(source->size - len >= offset);
5184c002cefSVladimir Sementsov-Ogievskiy 
5194c002cefSVladimir Sementsov-Ogievskiy     /* We shrink the request, so we can't overflow neither size_t nor MAX_IOV */
5204c002cefSVladimir Sementsov-Ogievskiy     ret = qemu_iovec_init_extended(qiov, NULL, 0, source, offset, len, NULL, 0);
5214c002cefSVladimir Sementsov-Ogievskiy     assert(ret == 0);
522d953169dSVladimir Sementsov-Ogievskiy }
523d953169dSVladimir Sementsov-Ogievskiy 
524baacf047SPaolo Bonzini void qemu_iovec_destroy(QEMUIOVector *qiov)
525baacf047SPaolo Bonzini {
526d953169dSVladimir Sementsov-Ogievskiy     if (qiov->nalloc != -1) {
527baacf047SPaolo Bonzini         g_free(qiov->iov);
528d953169dSVladimir Sementsov-Ogievskiy     }
529d953169dSVladimir Sementsov-Ogievskiy 
530d953169dSVladimir Sementsov-Ogievskiy     memset(qiov, 0, sizeof(*qiov));
531baacf047SPaolo Bonzini }
532baacf047SPaolo Bonzini 
533baacf047SPaolo Bonzini void qemu_iovec_reset(QEMUIOVector *qiov)
534baacf047SPaolo Bonzini {
535baacf047SPaolo Bonzini     assert(qiov->nalloc != -1);
536baacf047SPaolo Bonzini 
537baacf047SPaolo Bonzini     qiov->niov = 0;
538baacf047SPaolo Bonzini     qiov->size = 0;
539baacf047SPaolo Bonzini }
540baacf047SPaolo Bonzini 
541baacf047SPaolo Bonzini size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
542baacf047SPaolo Bonzini                          void *buf, size_t bytes)
543baacf047SPaolo Bonzini {
544baacf047SPaolo Bonzini     return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
545baacf047SPaolo Bonzini }
546baacf047SPaolo Bonzini 
547baacf047SPaolo Bonzini size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
548baacf047SPaolo Bonzini                            const void *buf, size_t bytes)
549baacf047SPaolo Bonzini {
550baacf047SPaolo Bonzini     return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
551baacf047SPaolo Bonzini }
552baacf047SPaolo Bonzini 
553baacf047SPaolo Bonzini size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
554baacf047SPaolo Bonzini                          int fillc, size_t bytes)
555baacf047SPaolo Bonzini {
556baacf047SPaolo Bonzini     return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
557baacf047SPaolo Bonzini }
558baacf047SPaolo Bonzini 
559f70d7f7eSBenoît Canet /**
560f70d7f7eSBenoît Canet  * Check that I/O vector contents are identical
561f70d7f7eSBenoît Canet  *
562f70d7f7eSBenoît Canet  * The IO vectors must have the same structure (same length of all parts).
563f70d7f7eSBenoît Canet  * A typical usage is to compare vectors created with qemu_iovec_clone().
564f70d7f7eSBenoît Canet  *
565f70d7f7eSBenoît Canet  * @a:          I/O vector
566f70d7f7eSBenoît Canet  * @b:          I/O vector
567f70d7f7eSBenoît Canet  * @ret:        Offset to first mismatching byte or -1 if match
568f70d7f7eSBenoît Canet  */
569f70d7f7eSBenoît Canet ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
570f70d7f7eSBenoît Canet {
571f70d7f7eSBenoît Canet     int i;
572f70d7f7eSBenoît Canet     ssize_t offset = 0;
573f70d7f7eSBenoît Canet 
574f70d7f7eSBenoît Canet     assert(a->niov == b->niov);
575f70d7f7eSBenoît Canet     for (i = 0; i < a->niov; i++) {
576f70d7f7eSBenoît Canet         size_t len = 0;
577f70d7f7eSBenoît Canet         uint8_t *p = (uint8_t *)a->iov[i].iov_base;
578f70d7f7eSBenoît Canet         uint8_t *q = (uint8_t *)b->iov[i].iov_base;
579f70d7f7eSBenoît Canet 
580f70d7f7eSBenoît Canet         assert(a->iov[i].iov_len == b->iov[i].iov_len);
581f70d7f7eSBenoît Canet         while (len < a->iov[i].iov_len && *p++ == *q++) {
582f70d7f7eSBenoît Canet             len++;
583f70d7f7eSBenoît Canet         }
584f70d7f7eSBenoît Canet 
585f70d7f7eSBenoît Canet         offset += len;
586f70d7f7eSBenoît Canet 
587f70d7f7eSBenoît Canet         if (len != a->iov[i].iov_len) {
588f70d7f7eSBenoît Canet             return offset;
589f70d7f7eSBenoît Canet         }
590f70d7f7eSBenoît Canet     }
591f70d7f7eSBenoît Canet     return -1;
592f70d7f7eSBenoît Canet }
593f70d7f7eSBenoît Canet 
594f70d7f7eSBenoît Canet typedef struct {
595f70d7f7eSBenoît Canet     int src_index;
596f70d7f7eSBenoît Canet     struct iovec *src_iov;
597f70d7f7eSBenoît Canet     void *dest_base;
598f70d7f7eSBenoît Canet } IOVectorSortElem;
599f70d7f7eSBenoît Canet 
600f70d7f7eSBenoît Canet static int sortelem_cmp_src_base(const void *a, const void *b)
601f70d7f7eSBenoît Canet {
602f70d7f7eSBenoît Canet     const IOVectorSortElem *elem_a = a;
603f70d7f7eSBenoît Canet     const IOVectorSortElem *elem_b = b;
604f70d7f7eSBenoît Canet 
605f70d7f7eSBenoît Canet     /* Don't overflow */
606f70d7f7eSBenoît Canet     if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
607f70d7f7eSBenoît Canet         return -1;
608f70d7f7eSBenoît Canet     } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
609f70d7f7eSBenoît Canet         return 1;
610f70d7f7eSBenoît Canet     } else {
611f70d7f7eSBenoît Canet         return 0;
612f70d7f7eSBenoît Canet     }
613f70d7f7eSBenoît Canet }
614f70d7f7eSBenoît Canet 
615f70d7f7eSBenoît Canet static int sortelem_cmp_src_index(const void *a, const void *b)
616f70d7f7eSBenoît Canet {
617f70d7f7eSBenoît Canet     const IOVectorSortElem *elem_a = a;
618f70d7f7eSBenoît Canet     const IOVectorSortElem *elem_b = b;
619f70d7f7eSBenoît Canet 
620f70d7f7eSBenoît Canet     return elem_a->src_index - elem_b->src_index;
621f70d7f7eSBenoît Canet }
622f70d7f7eSBenoît Canet 
623f70d7f7eSBenoît Canet /**
624f70d7f7eSBenoît Canet  * Copy contents of I/O vector
625f70d7f7eSBenoît Canet  *
626f70d7f7eSBenoît Canet  * The relative relationships of overlapping iovecs are preserved.  This is
627f70d7f7eSBenoît Canet  * necessary to ensure identical semantics in the cloned I/O vector.
628f70d7f7eSBenoît Canet  */
629f70d7f7eSBenoît Canet void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
630f70d7f7eSBenoît Canet {
631f70d7f7eSBenoît Canet     IOVectorSortElem sortelems[src->niov];
632f70d7f7eSBenoît Canet     void *last_end;
633f70d7f7eSBenoît Canet     int i;
634f70d7f7eSBenoît Canet 
635f70d7f7eSBenoît Canet     /* Sort by source iovecs by base address */
636f70d7f7eSBenoît Canet     for (i = 0; i < src->niov; i++) {
637f70d7f7eSBenoît Canet         sortelems[i].src_index = i;
638f70d7f7eSBenoît Canet         sortelems[i].src_iov = &src->iov[i];
639f70d7f7eSBenoît Canet     }
640f70d7f7eSBenoît Canet     qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
641f70d7f7eSBenoît Canet 
642f70d7f7eSBenoît Canet     /* Allocate buffer space taking into account overlapping iovecs */
643f70d7f7eSBenoît Canet     last_end = NULL;
644f70d7f7eSBenoît Canet     for (i = 0; i < src->niov; i++) {
645f70d7f7eSBenoît Canet         struct iovec *cur = sortelems[i].src_iov;
646f70d7f7eSBenoît Canet         ptrdiff_t rewind = 0;
647f70d7f7eSBenoît Canet 
648f70d7f7eSBenoît Canet         /* Detect overlap */
649f70d7f7eSBenoît Canet         if (last_end && last_end > cur->iov_base) {
650f70d7f7eSBenoît Canet             rewind = last_end - cur->iov_base;
651f70d7f7eSBenoît Canet         }
652f70d7f7eSBenoît Canet 
653f70d7f7eSBenoît Canet         sortelems[i].dest_base = buf - rewind;
654f70d7f7eSBenoît Canet         buf += cur->iov_len - MIN(rewind, cur->iov_len);
655f70d7f7eSBenoît Canet         last_end = MAX(cur->iov_base + cur->iov_len, last_end);
656f70d7f7eSBenoît Canet     }
657f70d7f7eSBenoît Canet 
658f70d7f7eSBenoît Canet     /* Sort by source iovec index and build destination iovec */
659f70d7f7eSBenoît Canet     qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
660f70d7f7eSBenoît Canet     for (i = 0; i < src->niov; i++) {
661f70d7f7eSBenoît Canet         qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
662f70d7f7eSBenoît Canet     }
663f70d7f7eSBenoît Canet }
664f70d7f7eSBenoît Canet 
6659dd6f7c2SStefan Hajnoczi void iov_discard_undo(IOVDiscardUndo *undo)
6669dd6f7c2SStefan Hajnoczi {
6679dd6f7c2SStefan Hajnoczi     /* Restore original iovec if it was modified */
6689dd6f7c2SStefan Hajnoczi     if (undo->modified_iov) {
6699dd6f7c2SStefan Hajnoczi         *undo->modified_iov = undo->orig;
6709dd6f7c2SStefan Hajnoczi     }
6719dd6f7c2SStefan Hajnoczi }
6729dd6f7c2SStefan Hajnoczi 
6739dd6f7c2SStefan Hajnoczi size_t iov_discard_front_undoable(struct iovec **iov,
6749dd6f7c2SStefan Hajnoczi                                   unsigned int *iov_cnt,
6759dd6f7c2SStefan Hajnoczi                                   size_t bytes,
6769dd6f7c2SStefan Hajnoczi                                   IOVDiscardUndo *undo)
677baacf047SPaolo Bonzini {
678baacf047SPaolo Bonzini     size_t total = 0;
679baacf047SPaolo Bonzini     struct iovec *cur;
680baacf047SPaolo Bonzini 
6819dd6f7c2SStefan Hajnoczi     if (undo) {
6829dd6f7c2SStefan Hajnoczi         undo->modified_iov = NULL;
6839dd6f7c2SStefan Hajnoczi     }
6849dd6f7c2SStefan Hajnoczi 
685baacf047SPaolo Bonzini     for (cur = *iov; *iov_cnt > 0; cur++) {
686baacf047SPaolo Bonzini         if (cur->iov_len > bytes) {
6879dd6f7c2SStefan Hajnoczi             if (undo) {
6889dd6f7c2SStefan Hajnoczi                 undo->modified_iov = cur;
6899dd6f7c2SStefan Hajnoczi                 undo->orig = *cur;
6909dd6f7c2SStefan Hajnoczi             }
6919dd6f7c2SStefan Hajnoczi 
692baacf047SPaolo Bonzini             cur->iov_base += bytes;
693baacf047SPaolo Bonzini             cur->iov_len -= bytes;
694baacf047SPaolo Bonzini             total += bytes;
695baacf047SPaolo Bonzini             break;
696baacf047SPaolo Bonzini         }
697baacf047SPaolo Bonzini 
698baacf047SPaolo Bonzini         bytes -= cur->iov_len;
699baacf047SPaolo Bonzini         total += cur->iov_len;
700baacf047SPaolo Bonzini         *iov_cnt -= 1;
701baacf047SPaolo Bonzini     }
702baacf047SPaolo Bonzini 
703baacf047SPaolo Bonzini     *iov = cur;
704baacf047SPaolo Bonzini     return total;
705baacf047SPaolo Bonzini }
706baacf047SPaolo Bonzini 
7079dd6f7c2SStefan Hajnoczi size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
708baacf047SPaolo Bonzini                          size_t bytes)
709baacf047SPaolo Bonzini {
7109dd6f7c2SStefan Hajnoczi     return iov_discard_front_undoable(iov, iov_cnt, bytes, NULL);
7119dd6f7c2SStefan Hajnoczi }
7129dd6f7c2SStefan Hajnoczi 
7139dd6f7c2SStefan Hajnoczi size_t iov_discard_back_undoable(struct iovec *iov,
7149dd6f7c2SStefan Hajnoczi                                  unsigned int *iov_cnt,
7159dd6f7c2SStefan Hajnoczi                                  size_t bytes,
7169dd6f7c2SStefan Hajnoczi                                  IOVDiscardUndo *undo)
7179dd6f7c2SStefan Hajnoczi {
718baacf047SPaolo Bonzini     size_t total = 0;
719baacf047SPaolo Bonzini     struct iovec *cur;
720baacf047SPaolo Bonzini 
7219dd6f7c2SStefan Hajnoczi     if (undo) {
7229dd6f7c2SStefan Hajnoczi         undo->modified_iov = NULL;
7239dd6f7c2SStefan Hajnoczi     }
7249dd6f7c2SStefan Hajnoczi 
725baacf047SPaolo Bonzini     if (*iov_cnt == 0) {
726baacf047SPaolo Bonzini         return 0;
727baacf047SPaolo Bonzini     }
728baacf047SPaolo Bonzini 
729baacf047SPaolo Bonzini     cur = iov + (*iov_cnt - 1);
730baacf047SPaolo Bonzini 
731baacf047SPaolo Bonzini     while (*iov_cnt > 0) {
732baacf047SPaolo Bonzini         if (cur->iov_len > bytes) {
7339dd6f7c2SStefan Hajnoczi             if (undo) {
7349dd6f7c2SStefan Hajnoczi                 undo->modified_iov = cur;
7359dd6f7c2SStefan Hajnoczi                 undo->orig = *cur;
7369dd6f7c2SStefan Hajnoczi             }
7379dd6f7c2SStefan Hajnoczi 
738baacf047SPaolo Bonzini             cur->iov_len -= bytes;
739baacf047SPaolo Bonzini             total += bytes;
740baacf047SPaolo Bonzini             break;
741baacf047SPaolo Bonzini         }
742baacf047SPaolo Bonzini 
743baacf047SPaolo Bonzini         bytes -= cur->iov_len;
744baacf047SPaolo Bonzini         total += cur->iov_len;
745baacf047SPaolo Bonzini         cur--;
746baacf047SPaolo Bonzini         *iov_cnt -= 1;
747baacf047SPaolo Bonzini     }
748baacf047SPaolo Bonzini 
749baacf047SPaolo Bonzini     return total;
750baacf047SPaolo Bonzini }
75158f423fbSKevin Wolf 
7529dd6f7c2SStefan Hajnoczi size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
7539dd6f7c2SStefan Hajnoczi                         size_t bytes)
7549dd6f7c2SStefan Hajnoczi {
7559dd6f7c2SStefan Hajnoczi     return iov_discard_back_undoable(iov, iov_cnt, bytes, NULL);
7569dd6f7c2SStefan Hajnoczi }
7579dd6f7c2SStefan Hajnoczi 
75858f423fbSKevin Wolf void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
75958f423fbSKevin Wolf {
76058f423fbSKevin Wolf     size_t total;
76158f423fbSKevin Wolf     unsigned int niov = qiov->niov;
76258f423fbSKevin Wolf 
76358f423fbSKevin Wolf     assert(qiov->size >= bytes);
76458f423fbSKevin Wolf     total = iov_discard_back(qiov->iov, &niov, bytes);
76558f423fbSKevin Wolf     assert(total == bytes);
76658f423fbSKevin Wolf 
76758f423fbSKevin Wolf     qiov->niov = niov;
76858f423fbSKevin Wolf     qiov->size -= bytes;
76958f423fbSKevin Wolf }
770