exec: make qemu_ram_ptr_length more similar to qemu_get_ram_ptr
[qemu.git] / migration / qemu-file-buf.c
blob49516b8643a35455a1f807dfe8c26110b6ca425f
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2014 IBM Corp.
7 * Authors:
8 * Stefan Berger <stefanb@linux.vnet.ibm.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
28 #include "qemu-common.h"
29 #include "qemu/error-report.h"
30 #include "qemu/iov.h"
31 #include "qemu/sockets.h"
32 #include "qemu/coroutine.h"
33 #include "migration/migration.h"
34 #include "migration/qemu-file.h"
35 #include "migration/qemu-file-internal.h"
36 #include "trace.h"
38 #define QSB_CHUNK_SIZE (1 << 10)
39 #define QSB_MAX_CHUNK_SIZE (16 * QSB_CHUNK_SIZE)
41 /**
42 * Create a QEMUSizedBuffer
43 * This type of buffer uses scatter-gather lists internally and
44 * can grow to any size. Any data array in the scatter-gather list
45 * can hold different amount of bytes.
47 * @buffer: Optional buffer to copy into the QSB
48 * @len: size of initial buffer; if @buffer is given, buffer must
49 * hold at least len bytes
51 * Returns a pointer to a QEMUSizedBuffer or NULL on allocation failure
53 QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len)
55 QEMUSizedBuffer *qsb;
56 size_t alloc_len, num_chunks, i, to_copy;
57 size_t chunk_size = (len > QSB_MAX_CHUNK_SIZE)
58 ? QSB_MAX_CHUNK_SIZE
59 : QSB_CHUNK_SIZE;
61 num_chunks = DIV_ROUND_UP(len ? len : QSB_CHUNK_SIZE, chunk_size);
62 alloc_len = num_chunks * chunk_size;
64 qsb = g_try_new0(QEMUSizedBuffer, 1);
65 if (!qsb) {
66 return NULL;
69 qsb->iov = g_try_new0(struct iovec, num_chunks);
70 if (!qsb->iov) {
71 g_free(qsb);
72 return NULL;
75 qsb->n_iov = num_chunks;
77 for (i = 0; i < num_chunks; i++) {
78 qsb->iov[i].iov_base = g_try_malloc0(chunk_size);
79 if (!qsb->iov[i].iov_base) {
80 /* qsb_free is safe since g_free can cope with NULL */
81 qsb_free(qsb);
82 return NULL;
85 qsb->iov[i].iov_len = chunk_size;
86 if (buffer) {
87 to_copy = (len - qsb->used) > chunk_size
88 ? chunk_size : (len - qsb->used);
89 memcpy(qsb->iov[i].iov_base, &buffer[qsb->used], to_copy);
90 qsb->used += to_copy;
94 qsb->size = alloc_len;
96 return qsb;
99 /**
100 * Free the QEMUSizedBuffer
102 * @qsb: The QEMUSizedBuffer to free
104 void qsb_free(QEMUSizedBuffer *qsb)
106 size_t i;
108 if (!qsb) {
109 return;
112 for (i = 0; i < qsb->n_iov; i++) {
113 g_free(qsb->iov[i].iov_base);
115 g_free(qsb->iov);
116 g_free(qsb);
120 * Get the number of used bytes in the QEMUSizedBuffer
122 * @qsb: A QEMUSizedBuffer
124 * Returns the number of bytes currently used in this buffer
126 size_t qsb_get_length(const QEMUSizedBuffer *qsb)
128 return qsb->used;
132 * Set the length of the buffer; the primary usage of this
133 * function is to truncate the number of used bytes in the buffer.
134 * The size will not be extended beyond the current number of
135 * allocated bytes in the QEMUSizedBuffer.
137 * @qsb: A QEMUSizedBuffer
138 * @new_len: The new length of bytes in the buffer
140 * Returns the number of bytes the buffer was truncated or extended
141 * to.
143 size_t qsb_set_length(QEMUSizedBuffer *qsb, size_t new_len)
145 if (new_len <= qsb->size) {
146 qsb->used = new_len;
147 } else {
148 qsb->used = qsb->size;
150 return qsb->used;
154 * Get the iovec that holds the data for a given position @pos.
156 * @qsb: A QEMUSizedBuffer
157 * @pos: The index of a byte in the buffer
158 * @d_off: Pointer to an offset that this function will indicate
159 * at what position within the returned iovec the byte
160 * is to be found
162 * Returns the index of the iovec that holds the byte at the given
163 * index @pos in the byte stream; a negative number if the iovec
164 * for the given position @pos does not exist.
166 static ssize_t qsb_get_iovec(const QEMUSizedBuffer *qsb,
167 off_t pos, off_t *d_off)
169 ssize_t i;
170 off_t curr = 0;
172 if (pos > qsb->used) {
173 return -1;
176 for (i = 0; i < qsb->n_iov; i++) {
177 if (curr + qsb->iov[i].iov_len > pos) {
178 *d_off = pos - curr;
179 return i;
181 curr += qsb->iov[i].iov_len;
183 return -1;
187 * Convert the QEMUSizedBuffer into a flat buffer.
189 * Note: If at all possible, try to avoid this function since it
190 * may unnecessarily copy memory around.
192 * @qsb: pointer to QEMUSizedBuffer
193 * @start: offset to start at
194 * @count: number of bytes to copy
195 * @buf: a pointer to a buffer to write into (at least @count bytes)
197 * Returns the number of bytes copied into the output buffer
199 ssize_t qsb_get_buffer(const QEMUSizedBuffer *qsb, off_t start,
200 size_t count, uint8_t *buffer)
202 const struct iovec *iov;
203 size_t to_copy, all_copy;
204 ssize_t index;
205 off_t s_off;
206 off_t d_off = 0;
207 char *s;
209 if (start > qsb->used) {
210 return 0;
213 all_copy = qsb->used - start;
214 if (all_copy > count) {
215 all_copy = count;
216 } else {
217 count = all_copy;
220 index = qsb_get_iovec(qsb, start, &s_off);
221 if (index < 0) {
222 return 0;
225 while (all_copy > 0) {
226 iov = &qsb->iov[index];
228 s = iov->iov_base;
230 to_copy = iov->iov_len - s_off;
231 if (to_copy > all_copy) {
232 to_copy = all_copy;
234 memcpy(&buffer[d_off], &s[s_off], to_copy);
236 d_off += to_copy;
237 all_copy -= to_copy;
239 s_off = 0;
240 index++;
243 return count;
247 * Grow the QEMUSizedBuffer to the given size and allocate
248 * memory for it.
250 * @qsb: A QEMUSizedBuffer
251 * @new_size: The new size of the buffer
253 * Return:
254 * a negative error code in case of memory allocation failure
255 * or
256 * the new size of the buffer. The returned size may be greater or equal
257 * to @new_size.
259 static ssize_t qsb_grow(QEMUSizedBuffer *qsb, size_t new_size)
261 size_t needed_chunks, i;
263 if (qsb->size < new_size) {
264 struct iovec *new_iov;
265 size_t size_diff = new_size - qsb->size;
266 size_t chunk_size = (size_diff > QSB_MAX_CHUNK_SIZE)
267 ? QSB_MAX_CHUNK_SIZE : QSB_CHUNK_SIZE;
269 needed_chunks = DIV_ROUND_UP(size_diff, chunk_size);
271 new_iov = g_try_new(struct iovec, qsb->n_iov + needed_chunks);
272 if (new_iov == NULL) {
273 return -ENOMEM;
276 /* Allocate new chunks as needed into new_iov */
277 for (i = qsb->n_iov; i < qsb->n_iov + needed_chunks; i++) {
278 new_iov[i].iov_base = g_try_malloc0(chunk_size);
279 new_iov[i].iov_len = chunk_size;
280 if (!new_iov[i].iov_base) {
281 size_t j;
283 /* Free previously allocated new chunks */
284 for (j = qsb->n_iov; j < i; j++) {
285 g_free(new_iov[j].iov_base);
287 g_free(new_iov);
289 return -ENOMEM;
294 * Now we can't get any allocation errors, copy over to new iov
295 * and switch.
297 for (i = 0; i < qsb->n_iov; i++) {
298 new_iov[i] = qsb->iov[i];
301 qsb->n_iov += needed_chunks;
302 g_free(qsb->iov);
303 qsb->iov = new_iov;
304 qsb->size += (needed_chunks * chunk_size);
307 return qsb->size;
311 * Write into the QEMUSizedBuffer at a given position and a given
312 * number of bytes. This function will automatically grow the
313 * QEMUSizedBuffer.
315 * @qsb: A QEMUSizedBuffer
316 * @source: A byte array to copy data from
317 * @pos: The position within the @qsb to write data to
318 * @size: The number of bytes to copy into the @qsb
320 * Returns @size or a negative error code in case of memory allocation failure,
321 * or with an invalid 'pos'
323 ssize_t qsb_write_at(QEMUSizedBuffer *qsb, const uint8_t *source,
324 off_t pos, size_t count)
326 ssize_t rc = qsb_grow(qsb, pos + count);
327 size_t to_copy;
328 size_t all_copy = count;
329 const struct iovec *iov;
330 ssize_t index;
331 char *dest;
332 off_t d_off, s_off = 0;
334 if (rc < 0) {
335 return rc;
338 if (pos + count > qsb->used) {
339 qsb->used = pos + count;
342 index = qsb_get_iovec(qsb, pos, &d_off);
343 if (index < 0) {
344 return -EINVAL;
347 while (all_copy > 0) {
348 iov = &qsb->iov[index];
350 dest = iov->iov_base;
352 to_copy = iov->iov_len - d_off;
353 if (to_copy > all_copy) {
354 to_copy = all_copy;
357 memcpy(&dest[d_off], &source[s_off], to_copy);
359 s_off += to_copy;
360 all_copy -= to_copy;
362 d_off = 0;
363 index++;
366 return count;
369 typedef struct QEMUBuffer {
370 QEMUSizedBuffer *qsb;
371 QEMUFile *file;
372 bool qsb_allocated;
373 } QEMUBuffer;
375 static ssize_t buf_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
376 size_t size)
378 QEMUBuffer *s = opaque;
379 ssize_t len = qsb_get_length(s->qsb) - pos;
381 if (len <= 0) {
382 return 0;
385 if (len > size) {
386 len = size;
388 return qsb_get_buffer(s->qsb, pos, len, buf);
391 static ssize_t buf_put_buffer(void *opaque, const uint8_t *buf,
392 int64_t pos, size_t size)
394 QEMUBuffer *s = opaque;
396 return qsb_write_at(s->qsb, buf, pos, size);
399 static int buf_close(void *opaque)
401 QEMUBuffer *s = opaque;
403 if (s->qsb_allocated) {
404 qsb_free(s->qsb);
407 g_free(s);
409 return 0;
412 const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f)
414 QEMUBuffer *p;
416 qemu_fflush(f);
418 p = f->opaque;
420 return p->qsb;
423 static const QEMUFileOps buf_read_ops = {
424 .get_buffer = buf_get_buffer,
425 .close = buf_close,
428 static const QEMUFileOps buf_write_ops = {
429 .put_buffer = buf_put_buffer,
430 .close = buf_close,
433 QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input)
435 QEMUBuffer *s;
437 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') ||
438 mode[1] != '\0') {
439 error_report("qemu_bufopen: Argument validity check failed");
440 return NULL;
443 s = g_new0(QEMUBuffer, 1);
444 s->qsb = input;
446 if (s->qsb == NULL) {
447 s->qsb = qsb_create(NULL, 0);
448 s->qsb_allocated = true;
450 if (!s->qsb) {
451 g_free(s);
452 error_report("qemu_bufopen: qsb_create failed");
453 return NULL;
457 if (mode[0] == 'r') {
458 s->file = qemu_fopen_ops(s, &buf_read_ops);
459 } else {
460 s->file = qemu_fopen_ops(s, &buf_write_ops);
462 return s->file;