iov.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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>
  10. * Michael Tokarev <mjt@tls.msk.ru>
  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.
  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.
  17. */
  18. #include "qemu/osdep.h"
  19. #include "qemu-common.h"
  20. #include "qemu/iov.h"
  21. #include "qemu/sockets.h"
  22. #include "qemu/cutils.h"
  23. size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
  24. size_t offset, const void *buf, size_t bytes)
  25. {
  26. size_t done;
  27. unsigned int i;
  28. for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
  29. if (offset < iov[i].iov_len) {
  30. size_t len = MIN(iov[i].iov_len - offset, bytes - done);
  31. memcpy(iov[i].iov_base + offset, buf + done, len);
  32. done += len;
  33. offset = 0;
  34. } else {
  35. offset -= iov[i].iov_len;
  36. }
  37. }
  38. assert(offset == 0);
  39. return done;
  40. }
  41. size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
  42. size_t offset, void *buf, size_t bytes)
  43. {
  44. size_t done;
  45. unsigned int i;
  46. for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
  47. if (offset < iov[i].iov_len) {
  48. size_t len = MIN(iov[i].iov_len - offset, bytes - done);
  49. memcpy(buf + done, iov[i].iov_base + offset, len);
  50. done += len;
  51. offset = 0;
  52. } else {
  53. offset -= iov[i].iov_len;
  54. }
  55. }
  56. assert(offset == 0);
  57. return done;
  58. }
  59. size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
  60. size_t offset, int fillc, size_t bytes)
  61. {
  62. size_t done;
  63. unsigned int i;
  64. for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
  65. if (offset < iov[i].iov_len) {
  66. size_t len = MIN(iov[i].iov_len - offset, bytes - done);
  67. memset(iov[i].iov_base + offset, fillc, len);
  68. done += len;
  69. offset = 0;
  70. } else {
  71. offset -= iov[i].iov_len;
  72. }
  73. }
  74. assert(offset == 0);
  75. return done;
  76. }
  77. size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
  78. {
  79. size_t len;
  80. unsigned int i;
  81. len = 0;
  82. for (i = 0; i < iov_cnt; i++) {
  83. len += iov[i].iov_len;
  84. }
  85. return len;
  86. }
  87. /* helper function for iov_send_recv() */
  88. static ssize_t
  89. do_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send)
  90. {
  91. #ifdef CONFIG_POSIX
  92. ssize_t ret;
  93. struct msghdr msg;
  94. memset(&msg, 0, sizeof(msg));
  95. msg.msg_iov = iov;
  96. msg.msg_iovlen = iov_cnt;
  97. do {
  98. ret = do_send
  99. ? sendmsg(sockfd, &msg, 0)
  100. : recvmsg(sockfd, &msg, 0);
  101. } while (ret < 0 && errno == EINTR);
  102. return ret;
  103. #else
  104. /* else send piece-by-piece */
  105. /*XXX Note: windows has WSASend() and WSARecv() */
  106. unsigned i = 0;
  107. ssize_t ret = 0;
  108. while (i < iov_cnt) {
  109. ssize_t r = do_send
  110. ? send(sockfd, iov[i].iov_base, iov[i].iov_len, 0)
  111. : recv(sockfd, iov[i].iov_base, iov[i].iov_len, 0);
  112. if (r > 0) {
  113. ret += r;
  114. } else if (!r) {
  115. break;
  116. } else if (errno == EINTR) {
  117. continue;
  118. } else {
  119. /* else it is some "other" error,
  120. * only return if there was no data processed. */
  121. if (ret == 0) {
  122. ret = -1;
  123. }
  124. break;
  125. }
  126. i++;
  127. }
  128. return ret;
  129. #endif
  130. }
  131. ssize_t iov_send_recv(int sockfd, const struct iovec *_iov, unsigned iov_cnt,
  132. size_t offset, size_t bytes,
  133. bool do_send)
  134. {
  135. ssize_t total = 0;
  136. ssize_t ret;
  137. size_t orig_len, tail;
  138. unsigned niov;
  139. struct iovec *local_iov, *iov;
  140. if (bytes <= 0) {
  141. return 0;
  142. }
  143. local_iov = g_new0(struct iovec, iov_cnt);
  144. iov_copy(local_iov, iov_cnt, _iov, iov_cnt, offset, bytes);
  145. offset = 0;
  146. iov = local_iov;
  147. while (bytes > 0) {
  148. /* Find the start position, skipping `offset' bytes:
  149. * first, skip all full-sized vector elements, */
  150. for (niov = 0; niov < iov_cnt && offset >= iov[niov].iov_len; ++niov) {
  151. offset -= iov[niov].iov_len;
  152. }
  153. /* niov == iov_cnt would only be valid if bytes == 0, which
  154. * we already ruled out in the loop condition. */
  155. assert(niov < iov_cnt);
  156. iov += niov;
  157. iov_cnt -= niov;
  158. if (offset) {
  159. /* second, skip `offset' bytes from the (now) first element,
  160. * undo it on exit */
  161. iov[0].iov_base += offset;
  162. iov[0].iov_len -= offset;
  163. }
  164. /* Find the end position skipping `bytes' bytes: */
  165. /* first, skip all full-sized elements */
  166. tail = bytes;
  167. for (niov = 0; niov < iov_cnt && iov[niov].iov_len <= tail; ++niov) {
  168. tail -= iov[niov].iov_len;
  169. }
  170. if (tail) {
  171. /* second, fixup the last element, and remember the original
  172. * length */
  173. assert(niov < iov_cnt);
  174. assert(iov[niov].iov_len > tail);
  175. orig_len = iov[niov].iov_len;
  176. iov[niov++].iov_len = tail;
  177. ret = do_send_recv(sockfd, iov, niov, do_send);
  178. /* Undo the changes above before checking for errors */
  179. iov[niov-1].iov_len = orig_len;
  180. } else {
  181. ret = do_send_recv(sockfd, iov, niov, do_send);
  182. }
  183. if (offset) {
  184. iov[0].iov_base -= offset;
  185. iov[0].iov_len += offset;
  186. }
  187. if (ret < 0) {
  188. assert(errno != EINTR);
  189. g_free(local_iov);
  190. if (errno == EAGAIN && total > 0) {
  191. return total;
  192. }
  193. return -1;
  194. }
  195. if (ret == 0 && !do_send) {
  196. /* recv returns 0 when the peer has performed an orderly
  197. * shutdown. */
  198. break;
  199. }
  200. /* Prepare for the next iteration */
  201. offset += ret;
  202. total += ret;
  203. bytes -= ret;
  204. }
  205. g_free(local_iov);
  206. return total;
  207. }
  208. void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
  209. FILE *fp, const char *prefix, size_t limit)
  210. {
  211. int v;
  212. size_t size = 0;
  213. char *buf;
  214. for (v = 0; v < iov_cnt; v++) {
  215. size += iov[v].iov_len;
  216. }
  217. size = size > limit ? limit : size;
  218. buf = g_malloc(size);
  219. iov_to_buf(iov, iov_cnt, 0, buf, size);
  220. qemu_hexdump(buf, fp, prefix, size);
  221. g_free(buf);
  222. }
  223. unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
  224. const struct iovec *iov, unsigned int iov_cnt,
  225. size_t offset, size_t bytes)
  226. {
  227. size_t len;
  228. unsigned int i, j;
  229. for (i = 0, j = 0;
  230. i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) {
  231. if (offset >= iov[i].iov_len) {
  232. offset -= iov[i].iov_len;
  233. continue;
  234. }
  235. len = MIN(bytes, iov[i].iov_len - offset);
  236. dst_iov[j].iov_base = iov[i].iov_base + offset;
  237. dst_iov[j].iov_len = len;
  238. j++;
  239. bytes -= len;
  240. offset = 0;
  241. }
  242. assert(offset == 0);
  243. return j;
  244. }
  245. /* io vectors */
  246. void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
  247. {
  248. qiov->iov = g_new(struct iovec, alloc_hint);
  249. qiov->niov = 0;
  250. qiov->nalloc = alloc_hint;
  251. qiov->size = 0;
  252. }
  253. void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
  254. {
  255. int i;
  256. qiov->iov = iov;
  257. qiov->niov = niov;
  258. qiov->nalloc = -1;
  259. qiov->size = 0;
  260. for (i = 0; i < niov; i++)
  261. qiov->size += iov[i].iov_len;
  262. }
  263. void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
  264. {
  265. assert(qiov->nalloc != -1);
  266. if (qiov->niov == qiov->nalloc) {
  267. qiov->nalloc = 2 * qiov->nalloc + 1;
  268. qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
  269. }
  270. qiov->iov[qiov->niov].iov_base = base;
  271. qiov->iov[qiov->niov].iov_len = len;
  272. qiov->size += len;
  273. ++qiov->niov;
  274. }
  275. /*
  276. * Concatenates (partial) iovecs from src_iov to the end of dst.
  277. * It starts copying after skipping `soffset' bytes at the
  278. * beginning of src and adds individual vectors from src to
  279. * dst copies up to `sbytes' bytes total, or up to the end
  280. * of src_iov if it comes first. This way, it is okay to specify
  281. * very large value for `sbytes' to indicate "up to the end
  282. * of src".
  283. * Only vector pointers are processed, not the actual data buffers.
  284. */
  285. size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
  286. struct iovec *src_iov, unsigned int src_cnt,
  287. size_t soffset, size_t sbytes)
  288. {
  289. int i;
  290. size_t done;
  291. if (!sbytes) {
  292. return 0;
  293. }
  294. assert(dst->nalloc != -1);
  295. for (i = 0, done = 0; done < sbytes && i < src_cnt; i++) {
  296. if (soffset < src_iov[i].iov_len) {
  297. size_t len = MIN(src_iov[i].iov_len - soffset, sbytes - done);
  298. qemu_iovec_add(dst, src_iov[i].iov_base + soffset, len);
  299. done += len;
  300. soffset = 0;
  301. } else {
  302. soffset -= src_iov[i].iov_len;
  303. }
  304. }
  305. assert(soffset == 0); /* offset beyond end of src */
  306. return done;
  307. }
  308. /*
  309. * Concatenates (partial) iovecs from src to the end of dst.
  310. * It starts copying after skipping `soffset' bytes at the
  311. * beginning of src and adds individual vectors from src to
  312. * dst copies up to `sbytes' bytes total, or up to the end
  313. * of src if it comes first. This way, it is okay to specify
  314. * very large value for `sbytes' to indicate "up to the end
  315. * of src".
  316. * Only vector pointers are processed, not the actual data buffers.
  317. */
  318. void qemu_iovec_concat(QEMUIOVector *dst,
  319. QEMUIOVector *src, size_t soffset, size_t sbytes)
  320. {
  321. qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes);
  322. }
  323. /*
  324. * qiov_find_iov
  325. *
  326. * Return pointer to iovec structure, where byte at @offset in original vector
  327. * @iov exactly is.
  328. * Set @remaining_offset to be offset inside that iovec to the same byte.
  329. */
  330. static struct iovec *iov_skip_offset(struct iovec *iov, size_t offset,
  331. size_t *remaining_offset)
  332. {
  333. while (offset > 0 && offset >= iov->iov_len) {
  334. offset -= iov->iov_len;
  335. iov++;
  336. }
  337. *remaining_offset = offset;
  338. return iov;
  339. }
  340. /*
  341. * qiov_slice
  342. *
  343. * Find subarray of iovec's, containing requested range. @head would
  344. * be offset in first iov (returned by the function), @tail would be
  345. * count of extra bytes in last iovec (returned iov + @niov - 1).
  346. */
  347. static struct iovec *qiov_slice(QEMUIOVector *qiov,
  348. size_t offset, size_t len,
  349. size_t *head, size_t *tail, int *niov)
  350. {
  351. struct iovec *iov, *end_iov;
  352. assert(offset + len <= qiov->size);
  353. iov = iov_skip_offset(qiov->iov, offset, head);
  354. end_iov = iov_skip_offset(iov, *head + len, tail);
  355. if (*tail > 0) {
  356. assert(*tail < end_iov->iov_len);
  357. *tail = end_iov->iov_len - *tail;
  358. end_iov++;
  359. }
  360. *niov = end_iov - iov;
  361. return iov;
  362. }
  363. int qemu_iovec_subvec_niov(QEMUIOVector *qiov, size_t offset, size_t len)
  364. {
  365. size_t head, tail;
  366. int niov;
  367. qiov_slice(qiov, offset, len, &head, &tail, &niov);
  368. return niov;
  369. }
  370. /*
  371. * Compile new iovec, combining @head_buf buffer, sub-qiov of @mid_qiov,
  372. * and @tail_buf buffer into new qiov.
  373. */
  374. void qemu_iovec_init_extended(
  375. QEMUIOVector *qiov,
  376. void *head_buf, size_t head_len,
  377. QEMUIOVector *mid_qiov, size_t mid_offset, size_t mid_len,
  378. void *tail_buf, size_t tail_len)
  379. {
  380. size_t mid_head, mid_tail;
  381. int total_niov, mid_niov = 0;
  382. struct iovec *p, *mid_iov = NULL;
  383. if (mid_len) {
  384. mid_iov = qiov_slice(mid_qiov, mid_offset, mid_len,
  385. &mid_head, &mid_tail, &mid_niov);
  386. }
  387. total_niov = !!head_len + mid_niov + !!tail_len;
  388. if (total_niov == 1) {
  389. qemu_iovec_init_buf(qiov, NULL, 0);
  390. p = &qiov->local_iov;
  391. } else {
  392. qiov->niov = qiov->nalloc = total_niov;
  393. qiov->size = head_len + mid_len + tail_len;
  394. p = qiov->iov = g_new(struct iovec, qiov->niov);
  395. }
  396. if (head_len) {
  397. p->iov_base = head_buf;
  398. p->iov_len = head_len;
  399. p++;
  400. }
  401. assert(!mid_niov == !mid_len);
  402. if (mid_niov) {
  403. memcpy(p, mid_iov, mid_niov * sizeof(*p));
  404. p[0].iov_base = (uint8_t *)p[0].iov_base + mid_head;
  405. p[0].iov_len -= mid_head;
  406. p[mid_niov - 1].iov_len -= mid_tail;
  407. p += mid_niov;
  408. }
  409. if (tail_len) {
  410. p->iov_base = tail_buf;
  411. p->iov_len = tail_len;
  412. }
  413. }
  414. /*
  415. * Check if the contents of subrange of qiov data is all zeroes.
  416. */
  417. bool qemu_iovec_is_zero(QEMUIOVector *qiov, size_t offset, size_t bytes)
  418. {
  419. struct iovec *iov;
  420. size_t current_offset;
  421. assert(offset + bytes <= qiov->size);
  422. iov = iov_skip_offset(qiov->iov, offset, &current_offset);
  423. while (bytes) {
  424. uint8_t *base = (uint8_t *)iov->iov_base + current_offset;
  425. size_t len = MIN(iov->iov_len - current_offset, bytes);
  426. if (!buffer_is_zero(base, len)) {
  427. return false;
  428. }
  429. current_offset = 0;
  430. bytes -= len;
  431. iov++;
  432. }
  433. return true;
  434. }
  435. void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source,
  436. size_t offset, size_t len)
  437. {
  438. qemu_iovec_init_extended(qiov, NULL, 0, source, offset, len, NULL, 0);
  439. }
  440. void qemu_iovec_destroy(QEMUIOVector *qiov)
  441. {
  442. if (qiov->nalloc != -1) {
  443. g_free(qiov->iov);
  444. }
  445. memset(qiov, 0, sizeof(*qiov));
  446. }
  447. void qemu_iovec_reset(QEMUIOVector *qiov)
  448. {
  449. assert(qiov->nalloc != -1);
  450. qiov->niov = 0;
  451. qiov->size = 0;
  452. }
  453. size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
  454. void *buf, size_t bytes)
  455. {
  456. return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
  457. }
  458. size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
  459. const void *buf, size_t bytes)
  460. {
  461. return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
  462. }
  463. size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
  464. int fillc, size_t bytes)
  465. {
  466. return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
  467. }
  468. /**
  469. * Check that I/O vector contents are identical
  470. *
  471. * The IO vectors must have the same structure (same length of all parts).
  472. * A typical usage is to compare vectors created with qemu_iovec_clone().
  473. *
  474. * @a: I/O vector
  475. * @b: I/O vector
  476. * @ret: Offset to first mismatching byte or -1 if match
  477. */
  478. ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
  479. {
  480. int i;
  481. ssize_t offset = 0;
  482. assert(a->niov == b->niov);
  483. for (i = 0; i < a->niov; i++) {
  484. size_t len = 0;
  485. uint8_t *p = (uint8_t *)a->iov[i].iov_base;
  486. uint8_t *q = (uint8_t *)b->iov[i].iov_base;
  487. assert(a->iov[i].iov_len == b->iov[i].iov_len);
  488. while (len < a->iov[i].iov_len && *p++ == *q++) {
  489. len++;
  490. }
  491. offset += len;
  492. if (len != a->iov[i].iov_len) {
  493. return offset;
  494. }
  495. }
  496. return -1;
  497. }
  498. typedef struct {
  499. int src_index;
  500. struct iovec *src_iov;
  501. void *dest_base;
  502. } IOVectorSortElem;
  503. static int sortelem_cmp_src_base(const void *a, const void *b)
  504. {
  505. const IOVectorSortElem *elem_a = a;
  506. const IOVectorSortElem *elem_b = b;
  507. /* Don't overflow */
  508. if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
  509. return -1;
  510. } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
  511. return 1;
  512. } else {
  513. return 0;
  514. }
  515. }
  516. static int sortelem_cmp_src_index(const void *a, const void *b)
  517. {
  518. const IOVectorSortElem *elem_a = a;
  519. const IOVectorSortElem *elem_b = b;
  520. return elem_a->src_index - elem_b->src_index;
  521. }
  522. /**
  523. * Copy contents of I/O vector
  524. *
  525. * The relative relationships of overlapping iovecs are preserved. This is
  526. * necessary to ensure identical semantics in the cloned I/O vector.
  527. */
  528. void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
  529. {
  530. IOVectorSortElem sortelems[src->niov];
  531. void *last_end;
  532. int i;
  533. /* Sort by source iovecs by base address */
  534. for (i = 0; i < src->niov; i++) {
  535. sortelems[i].src_index = i;
  536. sortelems[i].src_iov = &src->iov[i];
  537. }
  538. qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
  539. /* Allocate buffer space taking into account overlapping iovecs */
  540. last_end = NULL;
  541. for (i = 0; i < src->niov; i++) {
  542. struct iovec *cur = sortelems[i].src_iov;
  543. ptrdiff_t rewind = 0;
  544. /* Detect overlap */
  545. if (last_end && last_end > cur->iov_base) {
  546. rewind = last_end - cur->iov_base;
  547. }
  548. sortelems[i].dest_base = buf - rewind;
  549. buf += cur->iov_len - MIN(rewind, cur->iov_len);
  550. last_end = MAX(cur->iov_base + cur->iov_len, last_end);
  551. }
  552. /* Sort by source iovec index and build destination iovec */
  553. qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
  554. for (i = 0; i < src->niov; i++) {
  555. qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
  556. }
  557. }
  558. size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
  559. size_t bytes)
  560. {
  561. size_t total = 0;
  562. struct iovec *cur;
  563. for (cur = *iov; *iov_cnt > 0; cur++) {
  564. if (cur->iov_len > bytes) {
  565. cur->iov_base += bytes;
  566. cur->iov_len -= bytes;
  567. total += bytes;
  568. break;
  569. }
  570. bytes -= cur->iov_len;
  571. total += cur->iov_len;
  572. *iov_cnt -= 1;
  573. }
  574. *iov = cur;
  575. return total;
  576. }
  577. size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
  578. size_t bytes)
  579. {
  580. size_t total = 0;
  581. struct iovec *cur;
  582. if (*iov_cnt == 0) {
  583. return 0;
  584. }
  585. cur = iov + (*iov_cnt - 1);
  586. while (*iov_cnt > 0) {
  587. if (cur->iov_len > bytes) {
  588. cur->iov_len -= bytes;
  589. total += bytes;
  590. break;
  591. }
  592. bytes -= cur->iov_len;
  593. total += cur->iov_len;
  594. cur--;
  595. *iov_cnt -= 1;
  596. }
  597. return total;
  598. }
  599. void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes)
  600. {
  601. size_t total;
  602. unsigned int niov = qiov->niov;
  603. assert(qiov->size >= bytes);
  604. total = iov_discard_back(qiov->iov, &niov, bytes);
  605. assert(total == bytes);
  606. qiov->niov = niov;
  607. qiov->size -= bytes;
  608. }