]> Git Repo - qemu.git/blob - migration/qemu-file-buf.c
Merge remote-tracking branch 'remotes/borntraeger/tags/s390x-20150310' into staging
[qemu.git] / migration / qemu-file-buf.c
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2014 IBM Corp.
6  *
7  * Authors:
8  *  Stefan Berger <[email protected]>
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
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.
27  */
28 #include "qemu-common.h"
29 #include "qemu/iov.h"
30 #include "qemu/sockets.h"
31 #include "block/coroutine.h"
32 #include "migration/migration.h"
33 #include "migration/qemu-file.h"
34 #include "migration/qemu-file-internal.h"
35 #include "trace.h"
36
37 #define QSB_CHUNK_SIZE      (1 << 10)
38 #define QSB_MAX_CHUNK_SIZE  (16 * QSB_CHUNK_SIZE)
39
40 /**
41  * Create a QEMUSizedBuffer
42  * This type of buffer uses scatter-gather lists internally and
43  * can grow to any size. Any data array in the scatter-gather list
44  * can hold different amount of bytes.
45  *
46  * @buffer: Optional buffer to copy into the QSB
47  * @len: size of initial buffer; if @buffer is given, buffer must
48  *       hold at least len bytes
49  *
50  * Returns a pointer to a QEMUSizedBuffer or NULL on allocation failure
51  */
52 QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len)
53 {
54     QEMUSizedBuffer *qsb;
55     size_t alloc_len, num_chunks, i, to_copy;
56     size_t chunk_size = (len > QSB_MAX_CHUNK_SIZE)
57                         ? QSB_MAX_CHUNK_SIZE
58                         : QSB_CHUNK_SIZE;
59
60     num_chunks = DIV_ROUND_UP(len ? len : QSB_CHUNK_SIZE, chunk_size);
61     alloc_len = num_chunks * chunk_size;
62
63     qsb = g_try_new0(QEMUSizedBuffer, 1);
64     if (!qsb) {
65         return NULL;
66     }
67
68     qsb->iov = g_try_new0(struct iovec, num_chunks);
69     if (!qsb->iov) {
70         g_free(qsb);
71         return NULL;
72     }
73
74     qsb->n_iov = num_chunks;
75
76     for (i = 0; i < num_chunks; i++) {
77         qsb->iov[i].iov_base = g_try_malloc0(chunk_size);
78         if (!qsb->iov[i].iov_base) {
79             /* qsb_free is safe since g_free can cope with NULL */
80             qsb_free(qsb);
81             return NULL;
82         }
83
84         qsb->iov[i].iov_len = chunk_size;
85         if (buffer) {
86             to_copy = (len - qsb->used) > chunk_size
87                       ? chunk_size : (len - qsb->used);
88             memcpy(qsb->iov[i].iov_base, &buffer[qsb->used], to_copy);
89             qsb->used += to_copy;
90         }
91     }
92
93     qsb->size = alloc_len;
94
95     return qsb;
96 }
97
98 /**
99  * Free the QEMUSizedBuffer
100  *
101  * @qsb: The QEMUSizedBuffer to free
102  */
103 void qsb_free(QEMUSizedBuffer *qsb)
104 {
105     size_t i;
106
107     if (!qsb) {
108         return;
109     }
110
111     for (i = 0; i < qsb->n_iov; i++) {
112         g_free(qsb->iov[i].iov_base);
113     }
114     g_free(qsb->iov);
115     g_free(qsb);
116 }
117
118 /**
119  * Get the number of used bytes in the QEMUSizedBuffer
120  *
121  * @qsb: A QEMUSizedBuffer
122  *
123  * Returns the number of bytes currently used in this buffer
124  */
125 size_t qsb_get_length(const QEMUSizedBuffer *qsb)
126 {
127     return qsb->used;
128 }
129
130 /**
131  * Set the length of the buffer; the primary usage of this
132  * function is to truncate the number of used bytes in the buffer.
133  * The size will not be extended beyond the current number of
134  * allocated bytes in the QEMUSizedBuffer.
135  *
136  * @qsb: A QEMUSizedBuffer
137  * @new_len: The new length of bytes in the buffer
138  *
139  * Returns the number of bytes the buffer was truncated or extended
140  * to.
141  */
142 size_t qsb_set_length(QEMUSizedBuffer *qsb, size_t new_len)
143 {
144     if (new_len <= qsb->size) {
145         qsb->used = new_len;
146     } else {
147         qsb->used = qsb->size;
148     }
149     return qsb->used;
150 }
151
152 /**
153  * Get the iovec that holds the data for a given position @pos.
154  *
155  * @qsb: A QEMUSizedBuffer
156  * @pos: The index of a byte in the buffer
157  * @d_off: Pointer to an offset that this function will indicate
158  *         at what position within the returned iovec the byte
159  *         is to be found
160  *
161  * Returns the index of the iovec that holds the byte at the given
162  * index @pos in the byte stream; a negative number if the iovec
163  * for the given position @pos does not exist.
164  */
165 static ssize_t qsb_get_iovec(const QEMUSizedBuffer *qsb,
166                              off_t pos, off_t *d_off)
167 {
168     ssize_t i;
169     off_t curr = 0;
170
171     if (pos > qsb->used) {
172         return -1;
173     }
174
175     for (i = 0; i < qsb->n_iov; i++) {
176         if (curr + qsb->iov[i].iov_len > pos) {
177             *d_off = pos - curr;
178             return i;
179         }
180         curr += qsb->iov[i].iov_len;
181     }
182     return -1;
183 }
184
185 /*
186  * Convert the QEMUSizedBuffer into a flat buffer.
187  *
188  * Note: If at all possible, try to avoid this function since it
189  *       may unnecessarily copy memory around.
190  *
191  * @qsb: pointer to QEMUSizedBuffer
192  * @start: offset to start at
193  * @count: number of bytes to copy
194  * @buf: a pointer to a buffer to write into (at least @count bytes)
195  *
196  * Returns the number of bytes copied into the output buffer
197  */
198 ssize_t qsb_get_buffer(const QEMUSizedBuffer *qsb, off_t start,
199                        size_t count, uint8_t *buffer)
200 {
201     const struct iovec *iov;
202     size_t to_copy, all_copy;
203     ssize_t index;
204     off_t s_off;
205     off_t d_off = 0;
206     char *s;
207
208     if (start > qsb->used) {
209         return 0;
210     }
211
212     all_copy = qsb->used - start;
213     if (all_copy > count) {
214         all_copy = count;
215     } else {
216         count = all_copy;
217     }
218
219     index = qsb_get_iovec(qsb, start, &s_off);
220     if (index < 0) {
221         return 0;
222     }
223
224     while (all_copy > 0) {
225         iov = &qsb->iov[index];
226
227         s = iov->iov_base;
228
229         to_copy = iov->iov_len - s_off;
230         if (to_copy > all_copy) {
231             to_copy = all_copy;
232         }
233         memcpy(&buffer[d_off], &s[s_off], to_copy);
234
235         d_off += to_copy;
236         all_copy -= to_copy;
237
238         s_off = 0;
239         index++;
240     }
241
242     return count;
243 }
244
245 /**
246  * Grow the QEMUSizedBuffer to the given size and allocate
247  * memory for it.
248  *
249  * @qsb: A QEMUSizedBuffer
250  * @new_size: The new size of the buffer
251  *
252  * Return:
253  *    a negative error code in case of memory allocation failure
254  * or
255  *    the new size of the buffer. The returned size may be greater or equal
256  *    to @new_size.
257  */
258 static ssize_t qsb_grow(QEMUSizedBuffer *qsb, size_t new_size)
259 {
260     size_t needed_chunks, i;
261
262     if (qsb->size < new_size) {
263         struct iovec *new_iov;
264         size_t size_diff = new_size - qsb->size;
265         size_t chunk_size = (size_diff > QSB_MAX_CHUNK_SIZE)
266                              ? QSB_MAX_CHUNK_SIZE : QSB_CHUNK_SIZE;
267
268         needed_chunks = DIV_ROUND_UP(size_diff, chunk_size);
269
270         new_iov = g_try_new(struct iovec, qsb->n_iov + needed_chunks);
271         if (new_iov == NULL) {
272             return -ENOMEM;
273         }
274
275         /* Allocate new chunks as needed into new_iov */
276         for (i = qsb->n_iov; i < qsb->n_iov + needed_chunks; i++) {
277             new_iov[i].iov_base = g_try_malloc0(chunk_size);
278             new_iov[i].iov_len = chunk_size;
279             if (!new_iov[i].iov_base) {
280                 size_t j;
281
282                 /* Free previously allocated new chunks */
283                 for (j = qsb->n_iov; j < i; j++) {
284                     g_free(new_iov[j].iov_base);
285                 }
286                 g_free(new_iov);
287
288                 return -ENOMEM;
289             }
290         }
291
292         /*
293          * Now we can't get any allocation errors, copy over to new iov
294          * and switch.
295          */
296         for (i = 0; i < qsb->n_iov; i++) {
297             new_iov[i] = qsb->iov[i];
298         }
299
300         qsb->n_iov += needed_chunks;
301         g_free(qsb->iov);
302         qsb->iov = new_iov;
303         qsb->size += (needed_chunks * chunk_size);
304     }
305
306     return qsb->size;
307 }
308
309 /**
310  * Write into the QEMUSizedBuffer at a given position and a given
311  * number of bytes. This function will automatically grow the
312  * QEMUSizedBuffer.
313  *
314  * @qsb: A QEMUSizedBuffer
315  * @source: A byte array to copy data from
316  * @pos: The position within the @qsb to write data to
317  * @size: The number of bytes to copy into the @qsb
318  *
319  * Returns @size or a negative error code in case of memory allocation failure,
320  *           or with an invalid 'pos'
321  */
322 ssize_t qsb_write_at(QEMUSizedBuffer *qsb, const uint8_t *source,
323                      off_t pos, size_t count)
324 {
325     ssize_t rc = qsb_grow(qsb, pos + count);
326     size_t to_copy;
327     size_t all_copy = count;
328     const struct iovec *iov;
329     ssize_t index;
330     char *dest;
331     off_t d_off, s_off = 0;
332
333     if (rc < 0) {
334         return rc;
335     }
336
337     if (pos + count > qsb->used) {
338         qsb->used = pos + count;
339     }
340
341     index = qsb_get_iovec(qsb, pos, &d_off);
342     if (index < 0) {
343         return -EINVAL;
344     }
345
346     while (all_copy > 0) {
347         iov = &qsb->iov[index];
348
349         dest = iov->iov_base;
350
351         to_copy = iov->iov_len - d_off;
352         if (to_copy > all_copy) {
353             to_copy = all_copy;
354         }
355
356         memcpy(&dest[d_off], &source[s_off], to_copy);
357
358         s_off += to_copy;
359         all_copy -= to_copy;
360
361         d_off = 0;
362         index++;
363     }
364
365     return count;
366 }
367
368 /**
369  * Create a deep copy of the given QEMUSizedBuffer.
370  *
371  * @qsb: A QEMUSizedBuffer
372  *
373  * Returns a clone of @qsb or NULL on allocation failure
374  */
375 QEMUSizedBuffer *qsb_clone(const QEMUSizedBuffer *qsb)
376 {
377     QEMUSizedBuffer *out = qsb_create(NULL, qsb_get_length(qsb));
378     size_t i;
379     ssize_t res;
380     off_t pos = 0;
381
382     if (!out) {
383         return NULL;
384     }
385
386     for (i = 0; i < qsb->n_iov; i++) {
387         res =  qsb_write_at(out, qsb->iov[i].iov_base,
388                             pos, qsb->iov[i].iov_len);
389         if (res < 0) {
390             qsb_free(out);
391             return NULL;
392         }
393         pos += res;
394     }
395
396     return out;
397 }
398
399 typedef struct QEMUBuffer {
400     QEMUSizedBuffer *qsb;
401     QEMUFile *file;
402     bool qsb_allocated;
403 } QEMUBuffer;
404
405 static int buf_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
406 {
407     QEMUBuffer *s = opaque;
408     ssize_t len = qsb_get_length(s->qsb) - pos;
409
410     if (len <= 0) {
411         return 0;
412     }
413
414     if (len > size) {
415         len = size;
416     }
417     return qsb_get_buffer(s->qsb, pos, len, buf);
418 }
419
420 static int buf_put_buffer(void *opaque, const uint8_t *buf,
421                           int64_t pos, int size)
422 {
423     QEMUBuffer *s = opaque;
424
425     return qsb_write_at(s->qsb, buf, pos, size);
426 }
427
428 static int buf_close(void *opaque)
429 {
430     QEMUBuffer *s = opaque;
431
432     if (s->qsb_allocated) {
433         qsb_free(s->qsb);
434     }
435
436     g_free(s);
437
438     return 0;
439 }
440
441 const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f)
442 {
443     QEMUBuffer *p;
444
445     qemu_fflush(f);
446
447     p = f->opaque;
448
449     return p->qsb;
450 }
451
452 static const QEMUFileOps buf_read_ops = {
453     .get_buffer = buf_get_buffer,
454     .close =      buf_close,
455 };
456
457 static const QEMUFileOps buf_write_ops = {
458     .put_buffer = buf_put_buffer,
459     .close =      buf_close,
460 };
461
462 QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input)
463 {
464     QEMUBuffer *s;
465
466     if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') ||
467         mode[1] != '\0') {
468         error_report("qemu_bufopen: Argument validity check failed");
469         return NULL;
470     }
471
472     s = g_malloc0(sizeof(QEMUBuffer));
473     s->qsb = input;
474
475     if (s->qsb == NULL) {
476         s->qsb = qsb_create(NULL, 0);
477         s->qsb_allocated = true;
478     }
479     if (!s->qsb) {
480         g_free(s);
481         error_report("qemu_bufopen: qsb_create failed");
482         return NULL;
483     }
484
485
486     if (mode[0] == 'r') {
487         s->file = qemu_fopen_ops(s, &buf_read_ops);
488     } else {
489         s->file = qemu_fopen_ops(s, &buf_write_ops);
490     }
491     return s->file;
492 }
This page took 0.05097 seconds and 4 git commands to generate.