qemu-file.c 23 KB

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