qemu-file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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/osdep.h"
  25. #include <zlib.h>
  26. #include "qemu/error-report.h"
  27. #include "qemu/iov.h"
  28. #include "migration.h"
  29. #include "qemu-file.h"
  30. #include "trace.h"
  31. #include "qapi/error.h"
  32. #define IO_BUF_SIZE 32768
  33. #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
  34. struct QEMUFile {
  35. const QEMUFileOps *ops;
  36. const QEMUFileHooks *hooks;
  37. void *opaque;
  38. int64_t bytes_xfer;
  39. int64_t xfer_limit;
  40. int64_t pos; /* start of buffer when writing, end of buffer
  41. when reading */
  42. int buf_index;
  43. int buf_size; /* 0 when writing */
  44. uint8_t buf[IO_BUF_SIZE];
  45. DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
  46. struct iovec iov[MAX_IOV_SIZE];
  47. unsigned int iovcnt;
  48. int last_error;
  49. Error *last_error_obj;
  50. };
  51. /*
  52. * Stop a file from being read/written - not all backing files can do this
  53. * typically only sockets can.
  54. */
  55. int qemu_file_shutdown(QEMUFile *f)
  56. {
  57. if (!f->ops->shut_down) {
  58. return -ENOSYS;
  59. }
  60. return f->ops->shut_down(f->opaque, true, true, NULL);
  61. }
  62. /*
  63. * Result: QEMUFile* for a 'return path' for comms in the opposite direction
  64. * NULL if not available
  65. */
  66. QEMUFile *qemu_file_get_return_path(QEMUFile *f)
  67. {
  68. if (!f->ops->get_return_path) {
  69. return NULL;
  70. }
  71. return f->ops->get_return_path(f->opaque);
  72. }
  73. bool qemu_file_mode_is_not_valid(const char *mode)
  74. {
  75. if (mode == NULL ||
  76. (mode[0] != 'r' && mode[0] != 'w') ||
  77. mode[1] != 'b' || mode[2] != 0) {
  78. fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
  79. return true;
  80. }
  81. return false;
  82. }
  83. QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
  84. {
  85. QEMUFile *f;
  86. f = g_new0(QEMUFile, 1);
  87. f->opaque = opaque;
  88. f->ops = ops;
  89. return f;
  90. }
  91. void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks)
  92. {
  93. f->hooks = hooks;
  94. }
  95. /*
  96. * Get last error for stream f with optional Error*
  97. *
  98. * Return negative error value if there has been an error on previous
  99. * operations, return 0 if no error happened.
  100. * Optional, it returns Error* in errp, but it may be NULL even if return value
  101. * is not 0.
  102. *
  103. */
  104. int qemu_file_get_error_obj(QEMUFile *f, Error **errp)
  105. {
  106. if (errp) {
  107. *errp = f->last_error_obj ? error_copy(f->last_error_obj) : NULL;
  108. }
  109. return f->last_error;
  110. }
  111. /*
  112. * Set the last error for stream f with optional Error*
  113. */
  114. void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err)
  115. {
  116. if (f->last_error == 0 && ret) {
  117. f->last_error = ret;
  118. error_propagate(&f->last_error_obj, err);
  119. } else if (err) {
  120. error_report_err(err);
  121. }
  122. }
  123. /*
  124. * Get last error for stream f
  125. *
  126. * Return negative error value if there has been an error on previous
  127. * operations, return 0 if no error happened.
  128. *
  129. */
  130. int qemu_file_get_error(QEMUFile *f)
  131. {
  132. return qemu_file_get_error_obj(f, NULL);
  133. }
  134. /*
  135. * Set the last error for stream f
  136. */
  137. void qemu_file_set_error(QEMUFile *f, int ret)
  138. {
  139. qemu_file_set_error_obj(f, ret, NULL);
  140. }
  141. bool qemu_file_is_writable(QEMUFile *f)
  142. {
  143. return f->ops->writev_buffer;
  144. }
  145. static void qemu_iovec_release_ram(QEMUFile *f)
  146. {
  147. struct iovec iov;
  148. unsigned long idx;
  149. /* Find and release all the contiguous memory ranges marked as may_free. */
  150. idx = find_next_bit(f->may_free, f->iovcnt, 0);
  151. if (idx >= f->iovcnt) {
  152. return;
  153. }
  154. iov = f->iov[idx];
  155. /* The madvise() in the loop is called for iov within a continuous range and
  156. * then reinitialize the iov. And in the end, madvise() is called for the
  157. * last iov.
  158. */
  159. while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) {
  160. /* check for adjacent buffer and coalesce them */
  161. if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) {
  162. iov.iov_len += f->iov[idx].iov_len;
  163. continue;
  164. }
  165. if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
  166. error_report("migrate: madvise DONTNEED failed %p %zd: %s",
  167. iov.iov_base, iov.iov_len, strerror(errno));
  168. }
  169. iov = f->iov[idx];
  170. }
  171. if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
  172. error_report("migrate: madvise DONTNEED failed %p %zd: %s",
  173. iov.iov_base, iov.iov_len, strerror(errno));
  174. }
  175. memset(f->may_free, 0, sizeof(f->may_free));
  176. }
  177. /**
  178. * Flushes QEMUFile buffer
  179. *
  180. * This will flush all pending data. If data was only partially flushed, it
  181. * will set an error state.
  182. */
  183. void qemu_fflush(QEMUFile *f)
  184. {
  185. ssize_t ret = 0;
  186. ssize_t expect = 0;
  187. Error *local_error = NULL;
  188. if (!qemu_file_is_writable(f)) {
  189. return;
  190. }
  191. if (f->iovcnt > 0) {
  192. expect = iov_size(f->iov, f->iovcnt);
  193. ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos,
  194. &local_error);
  195. qemu_iovec_release_ram(f);
  196. }
  197. if (ret >= 0) {
  198. f->pos += ret;
  199. }
  200. /* We expect the QEMUFile write impl to send the full
  201. * data set we requested, so sanity check that.
  202. */
  203. if (ret != expect) {
  204. qemu_file_set_error_obj(f, ret < 0 ? ret : -EIO, local_error);
  205. }
  206. f->buf_index = 0;
  207. f->iovcnt = 0;
  208. }
  209. void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
  210. {
  211. int ret = 0;
  212. if (f->hooks && f->hooks->before_ram_iterate) {
  213. ret = f->hooks->before_ram_iterate(f, f->opaque, flags, NULL);
  214. if (ret < 0) {
  215. qemu_file_set_error(f, ret);
  216. }
  217. }
  218. }
  219. void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
  220. {
  221. int ret = 0;
  222. if (f->hooks && f->hooks->after_ram_iterate) {
  223. ret = f->hooks->after_ram_iterate(f, f->opaque, flags, NULL);
  224. if (ret < 0) {
  225. qemu_file_set_error(f, ret);
  226. }
  227. }
  228. }
  229. void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data)
  230. {
  231. int ret = -EINVAL;
  232. if (f->hooks && f->hooks->hook_ram_load) {
  233. ret = f->hooks->hook_ram_load(f, f->opaque, flags, data);
  234. if (ret < 0) {
  235. qemu_file_set_error(f, ret);
  236. }
  237. } else {
  238. /*
  239. * Hook is a hook specifically requested by the source sending a flag
  240. * that expects there to be a hook on the destination.
  241. */
  242. if (flags == RAM_CONTROL_HOOK) {
  243. qemu_file_set_error(f, ret);
  244. }
  245. }
  246. }
  247. size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
  248. ram_addr_t offset, size_t size,
  249. uint64_t *bytes_sent)
  250. {
  251. if (f->hooks && f->hooks->save_page) {
  252. int ret = f->hooks->save_page(f, f->opaque, block_offset,
  253. offset, size, bytes_sent);
  254. if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
  255. f->bytes_xfer += size;
  256. }
  257. if (ret != RAM_SAVE_CONTROL_DELAYED &&
  258. ret != RAM_SAVE_CONTROL_NOT_SUPP) {
  259. if (bytes_sent && *bytes_sent > 0) {
  260. qemu_update_position(f, *bytes_sent);
  261. } else if (ret < 0) {
  262. qemu_file_set_error(f, ret);
  263. }
  264. }
  265. return ret;
  266. }
  267. return RAM_SAVE_CONTROL_NOT_SUPP;
  268. }
  269. /*
  270. * Attempt to fill the buffer from the underlying file
  271. * Returns the number of bytes read, or negative value for an error.
  272. *
  273. * Note that it can return a partially full buffer even in a not error/not EOF
  274. * case if the underlying file descriptor gives a short read, and that can
  275. * happen even on a blocking fd.
  276. */
  277. static ssize_t qemu_fill_buffer(QEMUFile *f)
  278. {
  279. int len;
  280. int pending;
  281. Error *local_error = NULL;
  282. assert(!qemu_file_is_writable(f));
  283. pending = f->buf_size - f->buf_index;
  284. if (pending > 0) {
  285. memmove(f->buf, f->buf + f->buf_index, pending);
  286. }
  287. f->buf_index = 0;
  288. f->buf_size = pending;
  289. len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
  290. IO_BUF_SIZE - pending, &local_error);
  291. if (len > 0) {
  292. f->buf_size += len;
  293. f->pos += len;
  294. } else if (len == 0) {
  295. qemu_file_set_error_obj(f, -EIO, local_error);
  296. } else if (len != -EAGAIN) {
  297. qemu_file_set_error_obj(f, len, local_error);
  298. } else {
  299. error_free(local_error);
  300. }
  301. return len;
  302. }
  303. void qemu_update_position(QEMUFile *f, size_t size)
  304. {
  305. f->pos += size;
  306. }
  307. /** Closes the file
  308. *
  309. * Returns negative error value if any error happened on previous operations or
  310. * while closing the file. Returns 0 or positive number on success.
  311. *
  312. * The meaning of return value on success depends on the specific backend
  313. * being used.
  314. */
  315. int qemu_fclose(QEMUFile *f)
  316. {
  317. int ret;
  318. qemu_fflush(f);
  319. ret = qemu_file_get_error(f);
  320. if (f->ops->close) {
  321. int ret2 = f->ops->close(f->opaque, NULL);
  322. if (ret >= 0) {
  323. ret = ret2;
  324. }
  325. }
  326. /* If any error was spotted before closing, we should report it
  327. * instead of the close() return value.
  328. */
  329. if (f->last_error) {
  330. ret = f->last_error;
  331. }
  332. error_free(f->last_error_obj);
  333. g_free(f);
  334. trace_qemu_file_fclose();
  335. return ret;
  336. }
  337. /*
  338. * Add buf to iovec. Do flush if iovec is full.
  339. *
  340. * Return values:
  341. * 1 iovec is full and flushed
  342. * 0 iovec is not flushed
  343. *
  344. */
  345. static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
  346. bool may_free)
  347. {
  348. /* check for adjacent buffer and coalesce them */
  349. if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
  350. f->iov[f->iovcnt - 1].iov_len &&
  351. may_free == test_bit(f->iovcnt - 1, f->may_free))
  352. {
  353. f->iov[f->iovcnt - 1].iov_len += size;
  354. } else {
  355. if (may_free) {
  356. set_bit(f->iovcnt, f->may_free);
  357. }
  358. f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
  359. f->iov[f->iovcnt++].iov_len = size;
  360. }
  361. if (f->iovcnt >= MAX_IOV_SIZE) {
  362. qemu_fflush(f);
  363. return 1;
  364. }
  365. return 0;
  366. }
  367. static void add_buf_to_iovec(QEMUFile *f, size_t len)
  368. {
  369. if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) {
  370. f->buf_index += len;
  371. if (f->buf_index == IO_BUF_SIZE) {
  372. qemu_fflush(f);
  373. }
  374. }
  375. }
  376. void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
  377. bool may_free)
  378. {
  379. if (f->last_error) {
  380. return;
  381. }
  382. f->bytes_xfer += size;
  383. add_to_iovec(f, buf, size, may_free);
  384. }
  385. void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
  386. {
  387. size_t l;
  388. if (f->last_error) {
  389. return;
  390. }
  391. while (size > 0) {
  392. l = IO_BUF_SIZE - f->buf_index;
  393. if (l > size) {
  394. l = size;
  395. }
  396. memcpy(f->buf + f->buf_index, buf, l);
  397. f->bytes_xfer += l;
  398. add_buf_to_iovec(f, l);
  399. if (qemu_file_get_error(f)) {
  400. break;
  401. }
  402. buf += l;
  403. size -= l;
  404. }
  405. }
  406. void qemu_put_byte(QEMUFile *f, int v)
  407. {
  408. if (f->last_error) {
  409. return;
  410. }
  411. f->buf[f->buf_index] = v;
  412. f->bytes_xfer++;
  413. add_buf_to_iovec(f, 1);
  414. }
  415. void qemu_file_skip(QEMUFile *f, int size)
  416. {
  417. if (f->buf_index + size <= f->buf_size) {
  418. f->buf_index += size;
  419. }
  420. }
  421. /*
  422. * Read 'size' bytes from file (at 'offset') without moving the
  423. * pointer and set 'buf' to point to that data.
  424. *
  425. * It will return size bytes unless there was an error, in which case it will
  426. * return as many as it managed to read (assuming blocking fd's which
  427. * all current QEMUFile are)
  428. */
  429. size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
  430. {
  431. ssize_t pending;
  432. size_t index;
  433. assert(!qemu_file_is_writable(f));
  434. assert(offset < IO_BUF_SIZE);
  435. assert(size <= IO_BUF_SIZE - offset);
  436. /* The 1st byte to read from */
  437. index = f->buf_index + offset;
  438. /* The number of available bytes starting at index */
  439. pending = f->buf_size - index;
  440. /*
  441. * qemu_fill_buffer might return just a few bytes, even when there isn't
  442. * an error, so loop collecting them until we get enough.
  443. */
  444. while (pending < size) {
  445. int received = qemu_fill_buffer(f);
  446. if (received <= 0) {
  447. break;
  448. }
  449. index = f->buf_index + offset;
  450. pending = f->buf_size - index;
  451. }
  452. if (pending <= 0) {
  453. return 0;
  454. }
  455. if (size > pending) {
  456. size = pending;
  457. }
  458. *buf = f->buf + index;
  459. return size;
  460. }
  461. /*
  462. * Read 'size' bytes of data from the file into buf.
  463. * 'size' can be larger than the internal buffer.
  464. *
  465. * It will return size bytes unless there was an error, in which case it will
  466. * return as many as it managed to read (assuming blocking fd's which
  467. * all current QEMUFile are)
  468. */
  469. size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
  470. {
  471. size_t pending = size;
  472. size_t done = 0;
  473. while (pending > 0) {
  474. size_t res;
  475. uint8_t *src;
  476. res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
  477. if (res == 0) {
  478. return done;
  479. }
  480. memcpy(buf, src, res);
  481. qemu_file_skip(f, res);
  482. buf += res;
  483. pending -= res;
  484. done += res;
  485. }
  486. return done;
  487. }
  488. /*
  489. * Read 'size' bytes of data from the file.
  490. * 'size' can be larger than the internal buffer.
  491. *
  492. * The data:
  493. * may be held on an internal buffer (in which case *buf is updated
  494. * to point to it) that is valid until the next qemu_file operation.
  495. * OR
  496. * will be copied to the *buf that was passed in.
  497. *
  498. * The code tries to avoid the copy if possible.
  499. *
  500. * It will return size bytes unless there was an error, in which case it will
  501. * return as many as it managed to read (assuming blocking fd's which
  502. * all current QEMUFile are)
  503. *
  504. * Note: Since **buf may get changed, the caller should take care to
  505. * keep a pointer to the original buffer if it needs to deallocate it.
  506. */
  507. size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
  508. {
  509. if (size < IO_BUF_SIZE) {
  510. size_t res;
  511. uint8_t *src;
  512. res = qemu_peek_buffer(f, &src, size, 0);
  513. if (res == size) {
  514. qemu_file_skip(f, res);
  515. *buf = src;
  516. return res;
  517. }
  518. }
  519. return qemu_get_buffer(f, *buf, size);
  520. }
  521. /*
  522. * Peeks a single byte from the buffer; this isn't guaranteed to work if
  523. * offset leaves a gap after the previous read/peeked data.
  524. */
  525. int qemu_peek_byte(QEMUFile *f, int offset)
  526. {
  527. int index = f->buf_index + offset;
  528. assert(!qemu_file_is_writable(f));
  529. assert(offset < IO_BUF_SIZE);
  530. if (index >= f->buf_size) {
  531. qemu_fill_buffer(f);
  532. index = f->buf_index + offset;
  533. if (index >= f->buf_size) {
  534. return 0;
  535. }
  536. }
  537. return f->buf[index];
  538. }
  539. int qemu_get_byte(QEMUFile *f)
  540. {
  541. int result;
  542. result = qemu_peek_byte(f, 0);
  543. qemu_file_skip(f, 1);
  544. return result;
  545. }
  546. int64_t qemu_ftell_fast(QEMUFile *f)
  547. {
  548. int64_t ret = f->pos;
  549. int i;
  550. for (i = 0; i < f->iovcnt; i++) {
  551. ret += f->iov[i].iov_len;
  552. }
  553. return ret;
  554. }
  555. int64_t qemu_ftell(QEMUFile *f)
  556. {
  557. qemu_fflush(f);
  558. return f->pos;
  559. }
  560. int qemu_file_rate_limit(QEMUFile *f)
  561. {
  562. if (qemu_file_get_error(f)) {
  563. return 1;
  564. }
  565. if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
  566. return 1;
  567. }
  568. return 0;
  569. }
  570. int64_t qemu_file_get_rate_limit(QEMUFile *f)
  571. {
  572. return f->xfer_limit;
  573. }
  574. void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
  575. {
  576. f->xfer_limit = limit;
  577. }
  578. void qemu_file_reset_rate_limit(QEMUFile *f)
  579. {
  580. f->bytes_xfer = 0;
  581. }
  582. void qemu_file_update_transfer(QEMUFile *f, int64_t len)
  583. {
  584. f->bytes_xfer += len;
  585. }
  586. void qemu_put_be16(QEMUFile *f, unsigned int v)
  587. {
  588. qemu_put_byte(f, v >> 8);
  589. qemu_put_byte(f, v);
  590. }
  591. void qemu_put_be32(QEMUFile *f, unsigned int v)
  592. {
  593. qemu_put_byte(f, v >> 24);
  594. qemu_put_byte(f, v >> 16);
  595. qemu_put_byte(f, v >> 8);
  596. qemu_put_byte(f, v);
  597. }
  598. void qemu_put_be64(QEMUFile *f, uint64_t v)
  599. {
  600. qemu_put_be32(f, v >> 32);
  601. qemu_put_be32(f, v);
  602. }
  603. unsigned int qemu_get_be16(QEMUFile *f)
  604. {
  605. unsigned int v;
  606. v = qemu_get_byte(f) << 8;
  607. v |= qemu_get_byte(f);
  608. return v;
  609. }
  610. unsigned int qemu_get_be32(QEMUFile *f)
  611. {
  612. unsigned int v;
  613. v = (unsigned int)qemu_get_byte(f) << 24;
  614. v |= qemu_get_byte(f) << 16;
  615. v |= qemu_get_byte(f) << 8;
  616. v |= qemu_get_byte(f);
  617. return v;
  618. }
  619. uint64_t qemu_get_be64(QEMUFile *f)
  620. {
  621. uint64_t v;
  622. v = (uint64_t)qemu_get_be32(f) << 32;
  623. v |= qemu_get_be32(f);
  624. return v;
  625. }
  626. /* return the size after compression, or negative value on error */
  627. static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
  628. const uint8_t *source, size_t source_len)
  629. {
  630. int err;
  631. err = deflateReset(stream);
  632. if (err != Z_OK) {
  633. return -1;
  634. }
  635. stream->avail_in = source_len;
  636. stream->next_in = (uint8_t *)source;
  637. stream->avail_out = dest_len;
  638. stream->next_out = dest;
  639. err = deflate(stream, Z_FINISH);
  640. if (err != Z_STREAM_END) {
  641. return -1;
  642. }
  643. return stream->next_out - dest;
  644. }
  645. /* Compress size bytes of data start at p and store the compressed
  646. * data to the buffer of f.
  647. *
  648. * When f is not writable, return -1 if f has no space to save the
  649. * compressed data.
  650. * When f is wirtable and it has no space to save the compressed data,
  651. * do fflush first, if f still has no space to save the compressed
  652. * data, return -1.
  653. */
  654. ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
  655. const uint8_t *p, size_t size)
  656. {
  657. ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
  658. if (blen < compressBound(size)) {
  659. if (!qemu_file_is_writable(f)) {
  660. return -1;
  661. }
  662. qemu_fflush(f);
  663. blen = IO_BUF_SIZE - sizeof(int32_t);
  664. if (blen < compressBound(size)) {
  665. return -1;
  666. }
  667. }
  668. blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t),
  669. blen, p, size);
  670. if (blen < 0) {
  671. return -1;
  672. }
  673. qemu_put_be32(f, blen);
  674. add_buf_to_iovec(f, blen);
  675. return blen + sizeof(int32_t);
  676. }
  677. /* Put the data in the buffer of f_src to the buffer of f_des, and
  678. * then reset the buf_index of f_src to 0.
  679. */
  680. int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
  681. {
  682. int len = 0;
  683. if (f_src->buf_index > 0) {
  684. len = f_src->buf_index;
  685. qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
  686. f_src->buf_index = 0;
  687. f_src->iovcnt = 0;
  688. }
  689. return len;
  690. }
  691. /*
  692. * Get a string whose length is determined by a single preceding byte
  693. * A preallocated 256 byte buffer must be passed in.
  694. * Returns: len on success and a 0 terminated string in the buffer
  695. * else 0
  696. * (Note a 0 length string will return 0 either way)
  697. */
  698. size_t qemu_get_counted_string(QEMUFile *f, char buf[256])
  699. {
  700. size_t len = qemu_get_byte(f);
  701. size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
  702. buf[res] = 0;
  703. return res == len ? res : 0;
  704. }
  705. /*
  706. * Put a string with one preceding byte containing its length. The length of
  707. * the string should be less than 256.
  708. */
  709. void qemu_put_counted_string(QEMUFile *f, const char *str)
  710. {
  711. size_t len = strlen(str);
  712. assert(len < 256);
  713. qemu_put_byte(f, len);
  714. qemu_put_buffer(f, (const uint8_t *)str, len);
  715. }
  716. /*
  717. * Set the blocking state of the QEMUFile.
  718. * Note: On some transports the OS only keeps a single blocking state for
  719. * both directions, and thus changing the blocking on the main
  720. * QEMUFile can also affect the return path.
  721. */
  722. void qemu_file_set_blocking(QEMUFile *f, bool block)
  723. {
  724. if (f->ops->set_blocking) {
  725. f->ops->set_blocking(f->opaque, block, NULL);
  726. }
  727. }