2
0

qemu-file.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu-common.h"
  25. #include "qemu/iov.h"
  26. #include "qemu/sockets.h"
  27. #include "block/coroutine.h"
  28. #include "migration/migration.h"
  29. #include "migration/qemu-file.h"
  30. #include "trace.h"
  31. #define IO_BUF_SIZE 32768
  32. #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
  33. struct QEMUFile {
  34. const QEMUFileOps *ops;
  35. void *opaque;
  36. int64_t bytes_xfer;
  37. int64_t xfer_limit;
  38. int64_t pos; /* start of buffer when writing, end of buffer
  39. when reading */
  40. int buf_index;
  41. int buf_size; /* 0 when writing */
  42. uint8_t buf[IO_BUF_SIZE];
  43. struct iovec iov[MAX_IOV_SIZE];
  44. unsigned int iovcnt;
  45. int last_error;
  46. };
  47. bool qemu_file_mode_is_not_valid(const char *mode)
  48. {
  49. if (mode == NULL ||
  50. (mode[0] != 'r' && mode[0] != 'w') ||
  51. mode[1] != 'b' || mode[2] != 0) {
  52. fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
  53. return true;
  54. }
  55. return false;
  56. }
  57. QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
  58. {
  59. QEMUFile *f;
  60. f = g_malloc0(sizeof(QEMUFile));
  61. f->opaque = opaque;
  62. f->ops = ops;
  63. return f;
  64. }
  65. /*
  66. * Get last error for stream f
  67. *
  68. * Return negative error value if there has been an error on previous
  69. * operations, return 0 if no error happened.
  70. *
  71. */
  72. int qemu_file_get_error(QEMUFile *f)
  73. {
  74. return f->last_error;
  75. }
  76. void qemu_file_set_error(QEMUFile *f, int ret)
  77. {
  78. if (f->last_error == 0) {
  79. f->last_error = ret;
  80. }
  81. }
  82. bool qemu_file_is_writable(QEMUFile *f)
  83. {
  84. return f->ops->writev_buffer || f->ops->put_buffer;
  85. }
  86. /**
  87. * Flushes QEMUFile buffer
  88. *
  89. * If there is writev_buffer QEMUFileOps it uses it otherwise uses
  90. * put_buffer ops.
  91. */
  92. void qemu_fflush(QEMUFile *f)
  93. {
  94. ssize_t ret = 0;
  95. if (!qemu_file_is_writable(f)) {
  96. return;
  97. }
  98. if (f->ops->writev_buffer) {
  99. if (f->iovcnt > 0) {
  100. ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
  101. }
  102. } else {
  103. if (f->buf_index > 0) {
  104. ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
  105. }
  106. }
  107. if (ret >= 0) {
  108. f->pos += ret;
  109. }
  110. f->buf_index = 0;
  111. f->iovcnt = 0;
  112. if (ret < 0) {
  113. qemu_file_set_error(f, ret);
  114. }
  115. }
  116. void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
  117. {
  118. int ret = 0;
  119. if (f->ops->before_ram_iterate) {
  120. ret = f->ops->before_ram_iterate(f, f->opaque, flags);
  121. if (ret < 0) {
  122. qemu_file_set_error(f, ret);
  123. }
  124. }
  125. }
  126. void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
  127. {
  128. int ret = 0;
  129. if (f->ops->after_ram_iterate) {
  130. ret = f->ops->after_ram_iterate(f, f->opaque, flags);
  131. if (ret < 0) {
  132. qemu_file_set_error(f, ret);
  133. }
  134. }
  135. }
  136. void ram_control_load_hook(QEMUFile *f, uint64_t flags)
  137. {
  138. int ret = -EINVAL;
  139. if (f->ops->hook_ram_load) {
  140. ret = f->ops->hook_ram_load(f, f->opaque, flags);
  141. if (ret < 0) {
  142. qemu_file_set_error(f, ret);
  143. }
  144. } else {
  145. qemu_file_set_error(f, ret);
  146. }
  147. }
  148. size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
  149. ram_addr_t offset, size_t size, int *bytes_sent)
  150. {
  151. if (f->ops->save_page) {
  152. int ret = f->ops->save_page(f, f->opaque, block_offset,
  153. offset, size, bytes_sent);
  154. if (ret != RAM_SAVE_CONTROL_DELAYED) {
  155. if (bytes_sent && *bytes_sent > 0) {
  156. qemu_update_position(f, *bytes_sent);
  157. } else if (ret < 0) {
  158. qemu_file_set_error(f, ret);
  159. }
  160. }
  161. return ret;
  162. }
  163. return RAM_SAVE_CONTROL_NOT_SUPP;
  164. }
  165. /*
  166. * Attempt to fill the buffer from the underlying file
  167. * Returns the number of bytes read, or negative value for an error.
  168. *
  169. * Note that it can return a partially full buffer even in a not error/not EOF
  170. * case if the underlying file descriptor gives a short read, and that can
  171. * happen even on a blocking fd.
  172. */
  173. static ssize_t qemu_fill_buffer(QEMUFile *f)
  174. {
  175. int len;
  176. int pending;
  177. assert(!qemu_file_is_writable(f));
  178. pending = f->buf_size - f->buf_index;
  179. if (pending > 0) {
  180. memmove(f->buf, f->buf + f->buf_index, pending);
  181. }
  182. f->buf_index = 0;
  183. f->buf_size = pending;
  184. len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
  185. IO_BUF_SIZE - pending);
  186. if (len > 0) {
  187. f->buf_size += len;
  188. f->pos += len;
  189. } else if (len == 0) {
  190. qemu_file_set_error(f, -EIO);
  191. } else if (len != -EAGAIN) {
  192. qemu_file_set_error(f, len);
  193. }
  194. return len;
  195. }
  196. int qemu_get_fd(QEMUFile *f)
  197. {
  198. if (f->ops->get_fd) {
  199. return f->ops->get_fd(f->opaque);
  200. }
  201. return -1;
  202. }
  203. void qemu_update_position(QEMUFile *f, size_t size)
  204. {
  205. f->pos += size;
  206. }
  207. /** Closes the file
  208. *
  209. * Returns negative error value if any error happened on previous operations or
  210. * while closing the file. Returns 0 or positive number on success.
  211. *
  212. * The meaning of return value on success depends on the specific backend
  213. * being used.
  214. */
  215. int qemu_fclose(QEMUFile *f)
  216. {
  217. int ret;
  218. qemu_fflush(f);
  219. ret = qemu_file_get_error(f);
  220. if (f->ops->close) {
  221. int ret2 = f->ops->close(f->opaque);
  222. if (ret >= 0) {
  223. ret = ret2;
  224. }
  225. }
  226. /* If any error was spotted before closing, we should report it
  227. * instead of the close() return value.
  228. */
  229. if (f->last_error) {
  230. ret = f->last_error;
  231. }
  232. g_free(f);
  233. trace_qemu_file_fclose();
  234. return ret;
  235. }
  236. static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
  237. {
  238. /* check for adjacent buffer and coalesce them */
  239. if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
  240. f->iov[f->iovcnt - 1].iov_len) {
  241. f->iov[f->iovcnt - 1].iov_len += size;
  242. } else {
  243. f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
  244. f->iov[f->iovcnt++].iov_len = size;
  245. }
  246. if (f->iovcnt >= MAX_IOV_SIZE) {
  247. qemu_fflush(f);
  248. }
  249. }
  250. void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
  251. {
  252. if (!f->ops->writev_buffer) {
  253. qemu_put_buffer(f, buf, size);
  254. return;
  255. }
  256. if (f->last_error) {
  257. return;
  258. }
  259. f->bytes_xfer += size;
  260. add_to_iovec(f, buf, size);
  261. }
  262. void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
  263. {
  264. int l;
  265. if (f->last_error) {
  266. return;
  267. }
  268. while (size > 0) {
  269. l = IO_BUF_SIZE - f->buf_index;
  270. if (l > size) {
  271. l = size;
  272. }
  273. memcpy(f->buf + f->buf_index, buf, l);
  274. f->bytes_xfer += l;
  275. if (f->ops->writev_buffer) {
  276. add_to_iovec(f, f->buf + f->buf_index, l);
  277. }
  278. f->buf_index += l;
  279. if (f->buf_index == IO_BUF_SIZE) {
  280. qemu_fflush(f);
  281. }
  282. if (qemu_file_get_error(f)) {
  283. break;
  284. }
  285. buf += l;
  286. size -= l;
  287. }
  288. }
  289. void qemu_put_byte(QEMUFile *f, int v)
  290. {
  291. if (f->last_error) {
  292. return;
  293. }
  294. f->buf[f->buf_index] = v;
  295. f->bytes_xfer++;
  296. if (f->ops->writev_buffer) {
  297. add_to_iovec(f, f->buf + f->buf_index, 1);
  298. }
  299. f->buf_index++;
  300. if (f->buf_index == IO_BUF_SIZE) {
  301. qemu_fflush(f);
  302. }
  303. }
  304. void qemu_file_skip(QEMUFile *f, int size)
  305. {
  306. if (f->buf_index + size <= f->buf_size) {
  307. f->buf_index += size;
  308. }
  309. }
  310. /*
  311. * Read 'size' bytes from file (at 'offset') into buf without moving the
  312. * pointer.
  313. *
  314. * It will return size bytes unless there was an error, in which case it will
  315. * return as many as it managed to read (assuming blocking fd's which
  316. * all current QEMUFile are)
  317. */
  318. int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
  319. {
  320. int pending;
  321. int index;
  322. assert(!qemu_file_is_writable(f));
  323. assert(offset < IO_BUF_SIZE);
  324. assert(size <= IO_BUF_SIZE - offset);
  325. /* The 1st byte to read from */
  326. index = f->buf_index + offset;
  327. /* The number of available bytes starting at index */
  328. pending = f->buf_size - index;
  329. /*
  330. * qemu_fill_buffer might return just a few bytes, even when there isn't
  331. * an error, so loop collecting them until we get enough.
  332. */
  333. while (pending < size) {
  334. int received = qemu_fill_buffer(f);
  335. if (received <= 0) {
  336. break;
  337. }
  338. index = f->buf_index + offset;
  339. pending = f->buf_size - index;
  340. }
  341. if (pending <= 0) {
  342. return 0;
  343. }
  344. if (size > pending) {
  345. size = pending;
  346. }
  347. memcpy(buf, f->buf + index, size);
  348. return size;
  349. }
  350. /*
  351. * Read 'size' bytes of data from the file into buf.
  352. * 'size' can be larger than the internal buffer.
  353. *
  354. * It will return size bytes unless there was an error, in which case it will
  355. * return as many as it managed to read (assuming blocking fd's which
  356. * all current QEMUFile are)
  357. */
  358. int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
  359. {
  360. int pending = size;
  361. int done = 0;
  362. while (pending > 0) {
  363. int res;
  364. res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
  365. if (res == 0) {
  366. return done;
  367. }
  368. qemu_file_skip(f, res);
  369. buf += res;
  370. pending -= res;
  371. done += res;
  372. }
  373. return done;
  374. }
  375. /*
  376. * Peeks a single byte from the buffer; this isn't guaranteed to work if
  377. * offset leaves a gap after the previous read/peeked data.
  378. */
  379. int qemu_peek_byte(QEMUFile *f, int offset)
  380. {
  381. int index = f->buf_index + offset;
  382. assert(!qemu_file_is_writable(f));
  383. assert(offset < IO_BUF_SIZE);
  384. if (index >= f->buf_size) {
  385. qemu_fill_buffer(f);
  386. index = f->buf_index + offset;
  387. if (index >= f->buf_size) {
  388. return 0;
  389. }
  390. }
  391. return f->buf[index];
  392. }
  393. int qemu_get_byte(QEMUFile *f)
  394. {
  395. int result;
  396. result = qemu_peek_byte(f, 0);
  397. qemu_file_skip(f, 1);
  398. return result;
  399. }
  400. int64_t qemu_ftell(QEMUFile *f)
  401. {
  402. qemu_fflush(f);
  403. return f->pos;
  404. }
  405. int qemu_file_rate_limit(QEMUFile *f)
  406. {
  407. if (qemu_file_get_error(f)) {
  408. return 1;
  409. }
  410. if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
  411. return 1;
  412. }
  413. return 0;
  414. }
  415. int64_t qemu_file_get_rate_limit(QEMUFile *f)
  416. {
  417. return f->xfer_limit;
  418. }
  419. void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
  420. {
  421. f->xfer_limit = limit;
  422. }
  423. void qemu_file_reset_rate_limit(QEMUFile *f)
  424. {
  425. f->bytes_xfer = 0;
  426. }
  427. void qemu_put_be16(QEMUFile *f, unsigned int v)
  428. {
  429. qemu_put_byte(f, v >> 8);
  430. qemu_put_byte(f, v);
  431. }
  432. void qemu_put_be32(QEMUFile *f, unsigned int v)
  433. {
  434. qemu_put_byte(f, v >> 24);
  435. qemu_put_byte(f, v >> 16);
  436. qemu_put_byte(f, v >> 8);
  437. qemu_put_byte(f, v);
  438. }
  439. void qemu_put_be64(QEMUFile *f, uint64_t v)
  440. {
  441. qemu_put_be32(f, v >> 32);
  442. qemu_put_be32(f, v);
  443. }
  444. unsigned int qemu_get_be16(QEMUFile *f)
  445. {
  446. unsigned int v;
  447. v = qemu_get_byte(f) << 8;
  448. v |= qemu_get_byte(f);
  449. return v;
  450. }
  451. unsigned int qemu_get_be32(QEMUFile *f)
  452. {
  453. unsigned int v;
  454. v = qemu_get_byte(f) << 24;
  455. v |= qemu_get_byte(f) << 16;
  456. v |= qemu_get_byte(f) << 8;
  457. v |= qemu_get_byte(f);
  458. return v;
  459. }
  460. uint64_t qemu_get_be64(QEMUFile *f)
  461. {
  462. uint64_t v;
  463. v = (uint64_t)qemu_get_be32(f) << 32;
  464. v |= qemu_get_be32(f);
  465. return v;
  466. }
  467. #define QSB_CHUNK_SIZE (1 << 10)
  468. #define QSB_MAX_CHUNK_SIZE (16 * QSB_CHUNK_SIZE)
  469. /**
  470. * Create a QEMUSizedBuffer
  471. * This type of buffer uses scatter-gather lists internally and
  472. * can grow to any size. Any data array in the scatter-gather list
  473. * can hold different amount of bytes.
  474. *
  475. * @buffer: Optional buffer to copy into the QSB
  476. * @len: size of initial buffer; if @buffer is given, buffer must
  477. * hold at least len bytes
  478. *
  479. * Returns a pointer to a QEMUSizedBuffer or NULL on allocation failure
  480. */
  481. QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len)
  482. {
  483. QEMUSizedBuffer *qsb;
  484. size_t alloc_len, num_chunks, i, to_copy;
  485. size_t chunk_size = (len > QSB_MAX_CHUNK_SIZE)
  486. ? QSB_MAX_CHUNK_SIZE
  487. : QSB_CHUNK_SIZE;
  488. num_chunks = DIV_ROUND_UP(len ? len : QSB_CHUNK_SIZE, chunk_size);
  489. alloc_len = num_chunks * chunk_size;
  490. qsb = g_try_new0(QEMUSizedBuffer, 1);
  491. if (!qsb) {
  492. return NULL;
  493. }
  494. qsb->iov = g_try_new0(struct iovec, num_chunks);
  495. if (!qsb->iov) {
  496. g_free(qsb);
  497. return NULL;
  498. }
  499. qsb->n_iov = num_chunks;
  500. for (i = 0; i < num_chunks; i++) {
  501. qsb->iov[i].iov_base = g_try_malloc0(chunk_size);
  502. if (!qsb->iov[i].iov_base) {
  503. /* qsb_free is safe since g_free can cope with NULL */
  504. qsb_free(qsb);
  505. return NULL;
  506. }
  507. qsb->iov[i].iov_len = chunk_size;
  508. if (buffer) {
  509. to_copy = (len - qsb->used) > chunk_size
  510. ? chunk_size : (len - qsb->used);
  511. memcpy(qsb->iov[i].iov_base, &buffer[qsb->used], to_copy);
  512. qsb->used += to_copy;
  513. }
  514. }
  515. qsb->size = alloc_len;
  516. return qsb;
  517. }
  518. /**
  519. * Free the QEMUSizedBuffer
  520. *
  521. * @qsb: The QEMUSizedBuffer to free
  522. */
  523. void qsb_free(QEMUSizedBuffer *qsb)
  524. {
  525. size_t i;
  526. if (!qsb) {
  527. return;
  528. }
  529. for (i = 0; i < qsb->n_iov; i++) {
  530. g_free(qsb->iov[i].iov_base);
  531. }
  532. g_free(qsb->iov);
  533. g_free(qsb);
  534. }
  535. /**
  536. * Get the number of used bytes in the QEMUSizedBuffer
  537. *
  538. * @qsb: A QEMUSizedBuffer
  539. *
  540. * Returns the number of bytes currently used in this buffer
  541. */
  542. size_t qsb_get_length(const QEMUSizedBuffer *qsb)
  543. {
  544. return qsb->used;
  545. }
  546. /**
  547. * Set the length of the buffer; the primary usage of this
  548. * function is to truncate the number of used bytes in the buffer.
  549. * The size will not be extended beyond the current number of
  550. * allocated bytes in the QEMUSizedBuffer.
  551. *
  552. * @qsb: A QEMUSizedBuffer
  553. * @new_len: The new length of bytes in the buffer
  554. *
  555. * Returns the number of bytes the buffer was truncated or extended
  556. * to.
  557. */
  558. size_t qsb_set_length(QEMUSizedBuffer *qsb, size_t new_len)
  559. {
  560. if (new_len <= qsb->size) {
  561. qsb->used = new_len;
  562. } else {
  563. qsb->used = qsb->size;
  564. }
  565. return qsb->used;
  566. }
  567. /**
  568. * Get the iovec that holds the data for a given position @pos.
  569. *
  570. * @qsb: A QEMUSizedBuffer
  571. * @pos: The index of a byte in the buffer
  572. * @d_off: Pointer to an offset that this function will indicate
  573. * at what position within the returned iovec the byte
  574. * is to be found
  575. *
  576. * Returns the index of the iovec that holds the byte at the given
  577. * index @pos in the byte stream; a negative number if the iovec
  578. * for the given position @pos does not exist.
  579. */
  580. static ssize_t qsb_get_iovec(const QEMUSizedBuffer *qsb,
  581. off_t pos, off_t *d_off)
  582. {
  583. ssize_t i;
  584. off_t curr = 0;
  585. if (pos > qsb->used) {
  586. return -1;
  587. }
  588. for (i = 0; i < qsb->n_iov; i++) {
  589. if (curr + qsb->iov[i].iov_len > pos) {
  590. *d_off = pos - curr;
  591. return i;
  592. }
  593. curr += qsb->iov[i].iov_len;
  594. }
  595. return -1;
  596. }
  597. /*
  598. * Convert the QEMUSizedBuffer into a flat buffer.
  599. *
  600. * Note: If at all possible, try to avoid this function since it
  601. * may unnecessarily copy memory around.
  602. *
  603. * @qsb: pointer to QEMUSizedBuffer
  604. * @start: offset to start at
  605. * @count: number of bytes to copy
  606. * @buf: a pointer to a buffer to write into (at least @count bytes)
  607. *
  608. * Returns the number of bytes copied into the output buffer
  609. */
  610. ssize_t qsb_get_buffer(const QEMUSizedBuffer *qsb, off_t start,
  611. size_t count, uint8_t *buffer)
  612. {
  613. const struct iovec *iov;
  614. size_t to_copy, all_copy;
  615. ssize_t index;
  616. off_t s_off;
  617. off_t d_off = 0;
  618. char *s;
  619. if (start > qsb->used) {
  620. return 0;
  621. }
  622. all_copy = qsb->used - start;
  623. if (all_copy > count) {
  624. all_copy = count;
  625. } else {
  626. count = all_copy;
  627. }
  628. index = qsb_get_iovec(qsb, start, &s_off);
  629. if (index < 0) {
  630. return 0;
  631. }
  632. while (all_copy > 0) {
  633. iov = &qsb->iov[index];
  634. s = iov->iov_base;
  635. to_copy = iov->iov_len - s_off;
  636. if (to_copy > all_copy) {
  637. to_copy = all_copy;
  638. }
  639. memcpy(&buffer[d_off], &s[s_off], to_copy);
  640. d_off += to_copy;
  641. all_copy -= to_copy;
  642. s_off = 0;
  643. index++;
  644. }
  645. return count;
  646. }
  647. /**
  648. * Grow the QEMUSizedBuffer to the given size and allocate
  649. * memory for it.
  650. *
  651. * @qsb: A QEMUSizedBuffer
  652. * @new_size: The new size of the buffer
  653. *
  654. * Return:
  655. * a negative error code in case of memory allocation failure
  656. * or
  657. * the new size of the buffer. The returned size may be greater or equal
  658. * to @new_size.
  659. */
  660. static ssize_t qsb_grow(QEMUSizedBuffer *qsb, size_t new_size)
  661. {
  662. size_t needed_chunks, i;
  663. if (qsb->size < new_size) {
  664. struct iovec *new_iov;
  665. size_t size_diff = new_size - qsb->size;
  666. size_t chunk_size = (size_diff > QSB_MAX_CHUNK_SIZE)
  667. ? QSB_MAX_CHUNK_SIZE : QSB_CHUNK_SIZE;
  668. needed_chunks = DIV_ROUND_UP(size_diff, chunk_size);
  669. new_iov = g_try_new(struct iovec, qsb->n_iov + needed_chunks);
  670. if (new_iov == NULL) {
  671. return -ENOMEM;
  672. }
  673. /* Allocate new chunks as needed into new_iov */
  674. for (i = qsb->n_iov; i < qsb->n_iov + needed_chunks; i++) {
  675. new_iov[i].iov_base = g_try_malloc0(chunk_size);
  676. new_iov[i].iov_len = chunk_size;
  677. if (!new_iov[i].iov_base) {
  678. size_t j;
  679. /* Free previously allocated new chunks */
  680. for (j = qsb->n_iov; j < i; j++) {
  681. g_free(new_iov[j].iov_base);
  682. }
  683. g_free(new_iov);
  684. return -ENOMEM;
  685. }
  686. }
  687. /*
  688. * Now we can't get any allocation errors, copy over to new iov
  689. * and switch.
  690. */
  691. for (i = 0; i < qsb->n_iov; i++) {
  692. new_iov[i] = qsb->iov[i];
  693. }
  694. qsb->n_iov += needed_chunks;
  695. g_free(qsb->iov);
  696. qsb->iov = new_iov;
  697. qsb->size += (needed_chunks * chunk_size);
  698. }
  699. return qsb->size;
  700. }
  701. /**
  702. * Write into the QEMUSizedBuffer at a given position and a given
  703. * number of bytes. This function will automatically grow the
  704. * QEMUSizedBuffer.
  705. *
  706. * @qsb: A QEMUSizedBuffer
  707. * @source: A byte array to copy data from
  708. * @pos: The position within the @qsb to write data to
  709. * @size: The number of bytes to copy into the @qsb
  710. *
  711. * Returns @size or a negative error code in case of memory allocation failure,
  712. * or with an invalid 'pos'
  713. */
  714. ssize_t qsb_write_at(QEMUSizedBuffer *qsb, const uint8_t *source,
  715. off_t pos, size_t count)
  716. {
  717. ssize_t rc = qsb_grow(qsb, pos + count);
  718. size_t to_copy;
  719. size_t all_copy = count;
  720. const struct iovec *iov;
  721. ssize_t index;
  722. char *dest;
  723. off_t d_off, s_off = 0;
  724. if (rc < 0) {
  725. return rc;
  726. }
  727. if (pos + count > qsb->used) {
  728. qsb->used = pos + count;
  729. }
  730. index = qsb_get_iovec(qsb, pos, &d_off);
  731. if (index < 0) {
  732. return -EINVAL;
  733. }
  734. while (all_copy > 0) {
  735. iov = &qsb->iov[index];
  736. dest = iov->iov_base;
  737. to_copy = iov->iov_len - d_off;
  738. if (to_copy > all_copy) {
  739. to_copy = all_copy;
  740. }
  741. memcpy(&dest[d_off], &source[s_off], to_copy);
  742. s_off += to_copy;
  743. all_copy -= to_copy;
  744. d_off = 0;
  745. index++;
  746. }
  747. return count;
  748. }
  749. /**
  750. * Create a deep copy of the given QEMUSizedBuffer.
  751. *
  752. * @qsb: A QEMUSizedBuffer
  753. *
  754. * Returns a clone of @qsb or NULL on allocation failure
  755. */
  756. QEMUSizedBuffer *qsb_clone(const QEMUSizedBuffer *qsb)
  757. {
  758. QEMUSizedBuffer *out = qsb_create(NULL, qsb_get_length(qsb));
  759. size_t i;
  760. ssize_t res;
  761. off_t pos = 0;
  762. if (!out) {
  763. return NULL;
  764. }
  765. for (i = 0; i < qsb->n_iov; i++) {
  766. res = qsb_write_at(out, qsb->iov[i].iov_base,
  767. pos, qsb->iov[i].iov_len);
  768. if (res < 0) {
  769. qsb_free(out);
  770. return NULL;
  771. }
  772. pos += res;
  773. }
  774. return out;
  775. }
  776. typedef struct QEMUBuffer {
  777. QEMUSizedBuffer *qsb;
  778. QEMUFile *file;
  779. } QEMUBuffer;
  780. static int buf_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  781. {
  782. QEMUBuffer *s = opaque;
  783. ssize_t len = qsb_get_length(s->qsb) - pos;
  784. if (len <= 0) {
  785. return 0;
  786. }
  787. if (len > size) {
  788. len = size;
  789. }
  790. return qsb_get_buffer(s->qsb, pos, len, buf);
  791. }
  792. static int buf_put_buffer(void *opaque, const uint8_t *buf,
  793. int64_t pos, int size)
  794. {
  795. QEMUBuffer *s = opaque;
  796. return qsb_write_at(s->qsb, buf, pos, size);
  797. }
  798. static int buf_close(void *opaque)
  799. {
  800. QEMUBuffer *s = opaque;
  801. qsb_free(s->qsb);
  802. g_free(s);
  803. return 0;
  804. }
  805. const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f)
  806. {
  807. QEMUBuffer *p;
  808. qemu_fflush(f);
  809. p = f->opaque;
  810. return p->qsb;
  811. }
  812. static const QEMUFileOps buf_read_ops = {
  813. .get_buffer = buf_get_buffer,
  814. .close = buf_close,
  815. };
  816. static const QEMUFileOps buf_write_ops = {
  817. .put_buffer = buf_put_buffer,
  818. .close = buf_close,
  819. };
  820. QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input)
  821. {
  822. QEMUBuffer *s;
  823. if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') ||
  824. mode[1] != '\0') {
  825. error_report("qemu_bufopen: Argument validity check failed");
  826. return NULL;
  827. }
  828. s = g_malloc0(sizeof(QEMUBuffer));
  829. if (mode[0] == 'r') {
  830. s->qsb = input;
  831. }
  832. if (s->qsb == NULL) {
  833. s->qsb = qsb_create(NULL, 0);
  834. }
  835. if (!s->qsb) {
  836. g_free(s);
  837. error_report("qemu_bufopen: qsb_create failed");
  838. return NULL;
  839. }
  840. if (mode[0] == 'r') {
  841. s->file = qemu_fopen_ops(s, &buf_read_ops);
  842. } else {
  843. s->file = qemu_fopen_ops(s, &buf_write_ops);
  844. }
  845. return s->file;
  846. }