qemu-file.c 22 KB

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