2
0

qemu-file.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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 "qemu/madvise.h"
  26. #include "qemu/error-report.h"
  27. #include "qemu/iov.h"
  28. #include "migration.h"
  29. #include "migration-stats.h"
  30. #include "qemu-file.h"
  31. #include "trace.h"
  32. #include "options.h"
  33. #include "qapi/error.h"
  34. #include "rdma.h"
  35. #include "io/channel-file.h"
  36. #define IO_BUF_SIZE 32768
  37. #define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
  38. typedef struct FdEntry {
  39. QTAILQ_ENTRY(FdEntry) entry;
  40. int fd;
  41. } FdEntry;
  42. struct QEMUFile {
  43. QIOChannel *ioc;
  44. bool is_writable;
  45. int buf_index;
  46. int buf_size; /* 0 when writing */
  47. uint8_t buf[IO_BUF_SIZE];
  48. DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
  49. struct iovec iov[MAX_IOV_SIZE];
  50. unsigned int iovcnt;
  51. int last_error;
  52. Error *last_error_obj;
  53. bool can_pass_fd;
  54. QTAILQ_HEAD(, FdEntry) fds;
  55. };
  56. /*
  57. * Stop a file from being read/written - not all backing files can do this
  58. * typically only sockets can.
  59. *
  60. * TODO: convert to propagate Error objects instead of squashing
  61. * to a fixed errno value
  62. */
  63. int qemu_file_shutdown(QEMUFile *f)
  64. {
  65. Error *err = NULL;
  66. /*
  67. * We must set qemufile error before the real shutdown(), otherwise
  68. * there can be a race window where we thought IO all went though
  69. * (because last_error==NULL) but actually IO has already stopped.
  70. *
  71. * If without correct ordering, the race can happen like this:
  72. *
  73. * page receiver other thread
  74. * ------------- ------------
  75. * qemu_get_buffer()
  76. * do shutdown()
  77. * returns 0 (buffer all zero)
  78. * (we didn't check this retcode)
  79. * try to detect IO error
  80. * last_error==NULL, IO okay
  81. * install ALL-ZERO page
  82. * set last_error
  83. * --> guest crash!
  84. */
  85. if (!f->last_error) {
  86. qemu_file_set_error(f, -EIO);
  87. }
  88. if (!qio_channel_has_feature(f->ioc,
  89. QIO_CHANNEL_FEATURE_SHUTDOWN)) {
  90. return -ENOSYS;
  91. }
  92. if (qio_channel_shutdown(f->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, &err) < 0) {
  93. error_report_err(err);
  94. return -EIO;
  95. }
  96. return 0;
  97. }
  98. static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable)
  99. {
  100. QEMUFile *f;
  101. f = g_new0(QEMUFile, 1);
  102. object_ref(ioc);
  103. f->ioc = ioc;
  104. f->is_writable = is_writable;
  105. f->can_pass_fd = qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS);
  106. QTAILQ_INIT(&f->fds);
  107. return f;
  108. }
  109. /*
  110. * Result: QEMUFile* for a 'return path' for comms in the opposite direction
  111. * NULL if not available
  112. */
  113. QEMUFile *qemu_file_get_return_path(QEMUFile *f)
  114. {
  115. return qemu_file_new_impl(f->ioc, !f->is_writable);
  116. }
  117. QEMUFile *qemu_file_new_output(QIOChannel *ioc)
  118. {
  119. return qemu_file_new_impl(ioc, true);
  120. }
  121. QEMUFile *qemu_file_new_input(QIOChannel *ioc)
  122. {
  123. return qemu_file_new_impl(ioc, false);
  124. }
  125. /*
  126. * Get last error for stream f with optional Error*
  127. *
  128. * Return negative error value if there has been an error on previous
  129. * operations, return 0 if no error happened.
  130. *
  131. * If errp is specified, a verbose error message will be copied over.
  132. */
  133. int qemu_file_get_error_obj(QEMUFile *f, Error **errp)
  134. {
  135. if (!f->last_error) {
  136. return 0;
  137. }
  138. /* There is an error */
  139. if (errp) {
  140. if (f->last_error_obj) {
  141. *errp = error_copy(f->last_error_obj);
  142. } else {
  143. error_setg_errno(errp, -f->last_error, "Channel error");
  144. }
  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 f->last_error;
  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. static 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. bool qemu_file_is_seekable(QEMUFile *f)
  238. {
  239. return qio_channel_has_feature(f->ioc, QIO_CHANNEL_FEATURE_SEEKABLE);
  240. }
  241. /**
  242. * Flushes QEMUFile buffer
  243. *
  244. * This will flush all pending data. If data was only partially flushed, it
  245. * will set an error state.
  246. */
  247. int qemu_fflush(QEMUFile *f)
  248. {
  249. if (!qemu_file_is_writable(f)) {
  250. return f->last_error;
  251. }
  252. if (f->last_error) {
  253. return f->last_error;
  254. }
  255. if (f->iovcnt > 0) {
  256. Error *local_error = NULL;
  257. if (qio_channel_writev_all(f->ioc,
  258. f->iov, f->iovcnt,
  259. &local_error) < 0) {
  260. qemu_file_set_error_obj(f, -EIO, local_error);
  261. } else {
  262. uint64_t size = iov_size(f->iov, f->iovcnt);
  263. stat64_add(&mig_stats.qemu_file_transferred, size);
  264. }
  265. qemu_iovec_release_ram(f);
  266. }
  267. f->buf_index = 0;
  268. f->iovcnt = 0;
  269. return f->last_error;
  270. }
  271. /*
  272. * Attempt to fill the buffer from the underlying file
  273. * Returns the number of bytes read, or negative value for an error.
  274. *
  275. * Note that it can return a partially full buffer even in a not error/not EOF
  276. * case if the underlying file descriptor gives a short read, and that can
  277. * happen even on a blocking fd.
  278. */
  279. static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEMUFile *f)
  280. {
  281. int len;
  282. int pending;
  283. Error *local_error = NULL;
  284. g_autofree int *fds = NULL;
  285. size_t nfd = 0;
  286. int **pfds = f->can_pass_fd ? &fds : NULL;
  287. size_t *pnfd = f->can_pass_fd ? &nfd : NULL;
  288. assert(!qemu_file_is_writable(f));
  289. pending = f->buf_size - f->buf_index;
  290. if (pending > 0) {
  291. memmove(f->buf, f->buf + f->buf_index, pending);
  292. }
  293. f->buf_index = 0;
  294. f->buf_size = pending;
  295. if (qemu_file_get_error(f)) {
  296. return 0;
  297. }
  298. do {
  299. struct iovec iov = { f->buf + pending, IO_BUF_SIZE - pending };
  300. len = qio_channel_readv_full(f->ioc, &iov, 1, pfds, pnfd, 0,
  301. &local_error);
  302. if (len == QIO_CHANNEL_ERR_BLOCK) {
  303. if (qemu_in_coroutine()) {
  304. qio_channel_yield(f->ioc, G_IO_IN);
  305. } else {
  306. qio_channel_wait(f->ioc, G_IO_IN);
  307. }
  308. } else if (len < 0) {
  309. len = -EIO;
  310. }
  311. } while (len == QIO_CHANNEL_ERR_BLOCK);
  312. if (len > 0) {
  313. f->buf_size += len;
  314. } else if (len == 0) {
  315. qemu_file_set_error_obj(f, -EIO, local_error);
  316. } else {
  317. qemu_file_set_error_obj(f, len, local_error);
  318. }
  319. for (int i = 0; i < nfd; i++) {
  320. FdEntry *fde = g_new0(FdEntry, 1);
  321. fde->fd = fds[i];
  322. QTAILQ_INSERT_TAIL(&f->fds, fde, entry);
  323. }
  324. return len;
  325. }
  326. int qemu_file_put_fd(QEMUFile *f, int fd)
  327. {
  328. int ret = 0;
  329. QIOChannel *ioc = qemu_file_get_ioc(f);
  330. Error *err = NULL;
  331. struct iovec iov = { (void *)" ", 1 };
  332. /*
  333. * Send a dummy byte so qemu_fill_buffer on the receiving side does not
  334. * fail with a len=0 error. Flush first to maintain ordering wrt other
  335. * data.
  336. */
  337. qemu_fflush(f);
  338. if (qio_channel_writev_full(ioc, &iov, 1, &fd, 1, 0, &err) < 1) {
  339. error_report_err(error_copy(err));
  340. qemu_file_set_error_obj(f, -EIO, err);
  341. ret = -1;
  342. }
  343. trace_qemu_file_put_fd(f->ioc->name, fd, ret);
  344. return ret;
  345. }
  346. int qemu_file_get_fd(QEMUFile *f)
  347. {
  348. int fd = -1;
  349. FdEntry *fde;
  350. if (!f->can_pass_fd) {
  351. Error *err = NULL;
  352. error_setg(&err, "%s does not support fd passing", f->ioc->name);
  353. error_report_err(error_copy(err));
  354. qemu_file_set_error_obj(f, -EIO, err);
  355. goto out;
  356. }
  357. /* Force the dummy byte and its fd passenger to appear. */
  358. qemu_peek_byte(f, 0);
  359. fde = QTAILQ_FIRST(&f->fds);
  360. if (fde) {
  361. qemu_get_byte(f); /* Drop the dummy byte */
  362. fd = fde->fd;
  363. QTAILQ_REMOVE(&f->fds, fde, entry);
  364. g_free(fde);
  365. }
  366. out:
  367. trace_qemu_file_get_fd(f->ioc->name, fd);
  368. return fd;
  369. }
  370. /** Closes the file
  371. *
  372. * Returns negative error value if any error happened on previous operations or
  373. * while closing the file. Returns 0 or positive number on success.
  374. *
  375. * The meaning of return value on success depends on the specific backend
  376. * being used.
  377. */
  378. int qemu_fclose(QEMUFile *f)
  379. {
  380. FdEntry *fde, *next;
  381. int ret = qemu_fflush(f);
  382. int ret2 = qio_channel_close(f->ioc, NULL);
  383. if (ret >= 0) {
  384. ret = ret2;
  385. }
  386. QTAILQ_FOREACH_SAFE(fde, &f->fds, entry, next) {
  387. warn_report("qemu_fclose: received fd %d was never claimed", fde->fd);
  388. close(fde->fd);
  389. g_free(fde);
  390. }
  391. g_clear_pointer(&f->ioc, object_unref);
  392. error_free(f->last_error_obj);
  393. g_free(f);
  394. trace_qemu_file_fclose();
  395. return ret;
  396. }
  397. /*
  398. * Add buf to iovec. Do flush if iovec is full.
  399. *
  400. * Return values:
  401. * 1 iovec is full and flushed
  402. * 0 iovec is not flushed
  403. *
  404. */
  405. static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
  406. bool may_free)
  407. {
  408. /* check for adjacent buffer and coalesce them */
  409. if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
  410. f->iov[f->iovcnt - 1].iov_len &&
  411. may_free == test_bit(f->iovcnt - 1, f->may_free))
  412. {
  413. f->iov[f->iovcnt - 1].iov_len += size;
  414. } else {
  415. if (f->iovcnt >= MAX_IOV_SIZE) {
  416. /* Should only happen if a previous fflush failed */
  417. assert(qemu_file_get_error(f) || !qemu_file_is_writable(f));
  418. return 1;
  419. }
  420. if (may_free) {
  421. set_bit(f->iovcnt, f->may_free);
  422. }
  423. f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
  424. f->iov[f->iovcnt++].iov_len = size;
  425. }
  426. if (f->iovcnt >= MAX_IOV_SIZE) {
  427. qemu_fflush(f);
  428. return 1;
  429. }
  430. return 0;
  431. }
  432. static void add_buf_to_iovec(QEMUFile *f, size_t len)
  433. {
  434. if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) {
  435. f->buf_index += len;
  436. if (f->buf_index == IO_BUF_SIZE) {
  437. qemu_fflush(f);
  438. }
  439. }
  440. }
  441. void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
  442. bool may_free)
  443. {
  444. if (f->last_error) {
  445. return;
  446. }
  447. add_to_iovec(f, buf, size, may_free);
  448. }
  449. void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
  450. {
  451. size_t l;
  452. if (f->last_error) {
  453. return;
  454. }
  455. while (size > 0) {
  456. l = IO_BUF_SIZE - f->buf_index;
  457. if (l > size) {
  458. l = size;
  459. }
  460. memcpy(f->buf + f->buf_index, buf, l);
  461. add_buf_to_iovec(f, l);
  462. if (qemu_file_get_error(f)) {
  463. break;
  464. }
  465. buf += l;
  466. size -= l;
  467. }
  468. }
  469. void qemu_put_buffer_at(QEMUFile *f, const uint8_t *buf, size_t buflen,
  470. off_t pos)
  471. {
  472. Error *err = NULL;
  473. size_t ret;
  474. if (f->last_error) {
  475. return;
  476. }
  477. qemu_fflush(f);
  478. ret = qio_channel_pwrite(f->ioc, (char *)buf, buflen, pos, &err);
  479. if (err) {
  480. qemu_file_set_error_obj(f, -EIO, err);
  481. return;
  482. }
  483. if ((ssize_t)ret == QIO_CHANNEL_ERR_BLOCK) {
  484. qemu_file_set_error_obj(f, -EAGAIN, NULL);
  485. return;
  486. }
  487. if (ret != buflen) {
  488. error_setg(&err, "Partial write of size %zu, expected %zu", ret,
  489. buflen);
  490. qemu_file_set_error_obj(f, -EIO, err);
  491. return;
  492. }
  493. stat64_add(&mig_stats.qemu_file_transferred, buflen);
  494. return;
  495. }
  496. size_t qemu_get_buffer_at(QEMUFile *f, const uint8_t *buf, size_t buflen,
  497. off_t pos)
  498. {
  499. Error *err = NULL;
  500. size_t ret;
  501. if (f->last_error) {
  502. return 0;
  503. }
  504. ret = qio_channel_pread(f->ioc, (char *)buf, buflen, pos, &err);
  505. if ((ssize_t)ret == -1 || err) {
  506. qemu_file_set_error_obj(f, -EIO, err);
  507. return 0;
  508. }
  509. if ((ssize_t)ret == QIO_CHANNEL_ERR_BLOCK) {
  510. qemu_file_set_error_obj(f, -EAGAIN, NULL);
  511. return 0;
  512. }
  513. if (ret != buflen) {
  514. error_setg(&err, "Partial read of size %zu, expected %zu", ret, buflen);
  515. qemu_file_set_error_obj(f, -EIO, err);
  516. return 0;
  517. }
  518. return ret;
  519. }
  520. void qemu_set_offset(QEMUFile *f, off_t off, int whence)
  521. {
  522. Error *err = NULL;
  523. off_t ret;
  524. if (qemu_file_is_writable(f)) {
  525. qemu_fflush(f);
  526. } else {
  527. /* Drop all cached buffers if existed; will trigger a re-fill later */
  528. f->buf_index = 0;
  529. f->buf_size = 0;
  530. }
  531. ret = qio_channel_io_seek(f->ioc, off, whence, &err);
  532. if (ret == (off_t)-1) {
  533. qemu_file_set_error_obj(f, -EIO, err);
  534. }
  535. }
  536. off_t qemu_get_offset(QEMUFile *f)
  537. {
  538. Error *err = NULL;
  539. off_t ret;
  540. qemu_fflush(f);
  541. ret = qio_channel_io_seek(f->ioc, 0, SEEK_CUR, &err);
  542. if (ret == (off_t)-1) {
  543. qemu_file_set_error_obj(f, -EIO, err);
  544. }
  545. return ret;
  546. }
  547. void qemu_put_byte(QEMUFile *f, int v)
  548. {
  549. if (f->last_error) {
  550. return;
  551. }
  552. f->buf[f->buf_index] = v;
  553. add_buf_to_iovec(f, 1);
  554. }
  555. void qemu_file_skip(QEMUFile *f, int size)
  556. {
  557. if (f->buf_index + size <= f->buf_size) {
  558. f->buf_index += size;
  559. }
  560. }
  561. /*
  562. * Read 'size' bytes from file (at 'offset') without moving the
  563. * pointer and set 'buf' to point to that data.
  564. *
  565. * It will return size bytes unless there was an error, in which case it will
  566. * return as many as it managed to read (assuming blocking fd's which
  567. * all current QEMUFile are)
  568. */
  569. size_t coroutine_mixed_fn qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
  570. {
  571. ssize_t pending;
  572. size_t index;
  573. assert(!qemu_file_is_writable(f));
  574. assert(offset < IO_BUF_SIZE);
  575. assert(size <= IO_BUF_SIZE - offset);
  576. /* The 1st byte to read from */
  577. index = f->buf_index + offset;
  578. /* The number of available bytes starting at index */
  579. pending = f->buf_size - index;
  580. /*
  581. * qemu_fill_buffer might return just a few bytes, even when there isn't
  582. * an error, so loop collecting them until we get enough.
  583. */
  584. while (pending < size) {
  585. int received = qemu_fill_buffer(f);
  586. if (received <= 0) {
  587. break;
  588. }
  589. index = f->buf_index + offset;
  590. pending = f->buf_size - index;
  591. }
  592. if (pending <= 0) {
  593. return 0;
  594. }
  595. if (size > pending) {
  596. size = pending;
  597. }
  598. *buf = f->buf + index;
  599. return size;
  600. }
  601. /*
  602. * Read 'size' bytes of data from the file into buf.
  603. * 'size' can be larger than the internal buffer.
  604. *
  605. * It will return size bytes unless there was an error, in which case it will
  606. * return as many as it managed to read (assuming blocking fd's which
  607. * all current QEMUFile are)
  608. */
  609. size_t coroutine_mixed_fn qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
  610. {
  611. size_t pending = size;
  612. size_t done = 0;
  613. while (pending > 0) {
  614. size_t res;
  615. uint8_t *src;
  616. res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
  617. if (res == 0) {
  618. return done;
  619. }
  620. memcpy(buf, src, res);
  621. qemu_file_skip(f, res);
  622. buf += res;
  623. pending -= res;
  624. done += res;
  625. }
  626. return done;
  627. }
  628. /*
  629. * Read 'size' bytes of data from the file.
  630. * 'size' can be larger than the internal buffer.
  631. *
  632. * The data:
  633. * may be held on an internal buffer (in which case *buf is updated
  634. * to point to it) that is valid until the next qemu_file operation.
  635. * OR
  636. * will be copied to the *buf that was passed in.
  637. *
  638. * The code tries to avoid the copy if possible.
  639. *
  640. * It will return size bytes unless there was an error, in which case it will
  641. * return as many as it managed to read (assuming blocking fd's which
  642. * all current QEMUFile are)
  643. *
  644. * Note: Since **buf may get changed, the caller should take care to
  645. * keep a pointer to the original buffer if it needs to deallocate it.
  646. */
  647. size_t coroutine_mixed_fn qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
  648. {
  649. if (size < IO_BUF_SIZE) {
  650. size_t res;
  651. uint8_t *src = NULL;
  652. res = qemu_peek_buffer(f, &src, size, 0);
  653. if (res == size) {
  654. qemu_file_skip(f, res);
  655. *buf = src;
  656. return res;
  657. }
  658. }
  659. return qemu_get_buffer(f, *buf, size);
  660. }
  661. /*
  662. * Peeks a single byte from the buffer; this isn't guaranteed to work if
  663. * offset leaves a gap after the previous read/peeked data.
  664. */
  665. int coroutine_mixed_fn qemu_peek_byte(QEMUFile *f, int offset)
  666. {
  667. int index = f->buf_index + offset;
  668. assert(!qemu_file_is_writable(f));
  669. assert(offset < IO_BUF_SIZE);
  670. if (index >= f->buf_size) {
  671. qemu_fill_buffer(f);
  672. index = f->buf_index + offset;
  673. if (index >= f->buf_size) {
  674. return 0;
  675. }
  676. }
  677. return f->buf[index];
  678. }
  679. int coroutine_mixed_fn qemu_get_byte(QEMUFile *f)
  680. {
  681. int result;
  682. result = qemu_peek_byte(f, 0);
  683. qemu_file_skip(f, 1);
  684. return result;
  685. }
  686. uint64_t qemu_file_transferred(QEMUFile *f)
  687. {
  688. uint64_t ret = stat64_get(&mig_stats.qemu_file_transferred);
  689. int i;
  690. g_assert(qemu_file_is_writable(f));
  691. for (i = 0; i < f->iovcnt; i++) {
  692. ret += f->iov[i].iov_len;
  693. }
  694. return ret;
  695. }
  696. void qemu_put_be16(QEMUFile *f, unsigned int v)
  697. {
  698. qemu_put_byte(f, v >> 8);
  699. qemu_put_byte(f, v);
  700. }
  701. void qemu_put_be32(QEMUFile *f, unsigned int v)
  702. {
  703. qemu_put_byte(f, v >> 24);
  704. qemu_put_byte(f, v >> 16);
  705. qemu_put_byte(f, v >> 8);
  706. qemu_put_byte(f, v);
  707. }
  708. void qemu_put_be64(QEMUFile *f, uint64_t v)
  709. {
  710. qemu_put_be32(f, v >> 32);
  711. qemu_put_be32(f, v);
  712. }
  713. unsigned int qemu_get_be16(QEMUFile *f)
  714. {
  715. unsigned int v;
  716. v = qemu_get_byte(f) << 8;
  717. v |= qemu_get_byte(f);
  718. return v;
  719. }
  720. unsigned int qemu_get_be32(QEMUFile *f)
  721. {
  722. unsigned int v;
  723. v = (unsigned int)qemu_get_byte(f) << 24;
  724. v |= qemu_get_byte(f) << 16;
  725. v |= qemu_get_byte(f) << 8;
  726. v |= qemu_get_byte(f);
  727. return v;
  728. }
  729. uint64_t qemu_get_be64(QEMUFile *f)
  730. {
  731. uint64_t v;
  732. v = (uint64_t)qemu_get_be32(f) << 32;
  733. v |= qemu_get_be32(f);
  734. return v;
  735. }
  736. /*
  737. * Get a string whose length is determined by a single preceding byte
  738. * A preallocated 256 byte buffer must be passed in.
  739. * Returns: len on success and a 0 terminated string in the buffer
  740. * else 0
  741. * (Note a 0 length string will return 0 either way)
  742. */
  743. size_t coroutine_fn qemu_get_counted_string(QEMUFile *f, char buf[256])
  744. {
  745. size_t len = qemu_get_byte(f);
  746. size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
  747. buf[res] = 0;
  748. return res == len ? res : 0;
  749. }
  750. /*
  751. * Put a string with one preceding byte containing its length. The length of
  752. * the string should be less than 256.
  753. */
  754. void qemu_put_counted_string(QEMUFile *f, const char *str)
  755. {
  756. size_t len = strlen(str);
  757. assert(len < 256);
  758. qemu_put_byte(f, len);
  759. qemu_put_buffer(f, (const uint8_t *)str, len);
  760. }
  761. /*
  762. * Set the blocking state of the QEMUFile.
  763. * Note: On some transports the OS only keeps a single blocking state for
  764. * both directions, and thus changing the blocking on the main
  765. * QEMUFile can also affect the return path.
  766. */
  767. void qemu_file_set_blocking(QEMUFile *f, bool block)
  768. {
  769. qio_channel_set_blocking(f->ioc, block, NULL);
  770. }
  771. /*
  772. * qemu_file_get_ioc:
  773. *
  774. * Get the ioc object for the file, without incrementing
  775. * the reference count.
  776. *
  777. * Returns: the ioc object
  778. */
  779. QIOChannel *qemu_file_get_ioc(QEMUFile *file)
  780. {
  781. return file->ioc;
  782. }
  783. /*
  784. * Read size bytes from QEMUFile f and write them to fd.
  785. */
  786. int qemu_file_get_to_fd(QEMUFile *f, int fd, size_t size)
  787. {
  788. while (size) {
  789. size_t pending = f->buf_size - f->buf_index;
  790. ssize_t rc;
  791. if (!pending) {
  792. rc = qemu_fill_buffer(f);
  793. if (rc < 0) {
  794. return rc;
  795. }
  796. if (rc == 0) {
  797. return -EIO;
  798. }
  799. continue;
  800. }
  801. rc = write(fd, f->buf + f->buf_index, MIN(pending, size));
  802. if (rc < 0) {
  803. return -errno;
  804. }
  805. if (rc == 0) {
  806. return -EIO;
  807. }
  808. f->buf_index += rc;
  809. size -= rc;
  810. }
  811. return 0;
  812. }