block: bdrv_img_create(): add Error ** argument
[qemu.git] / buffered_file.c
blobbd0f61d8c9684ca8347be8dfdc9d0619219613a5
1 /*
2 * QEMU buffered QEMUFile
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu-common.h"
17 #include "hw/hw.h"
18 #include "qemu-timer.h"
19 #include "qemu-char.h"
20 #include "buffered_file.h"
22 //#define DEBUG_BUFFERED_FILE
24 typedef struct QEMUFileBuffered
26 MigrationState *migration_state;
27 QEMUFile *file;
28 int freeze_output;
29 size_t bytes_xfer;
30 size_t xfer_limit;
31 uint8_t *buffer;
32 size_t buffer_size;
33 size_t buffer_capacity;
34 QEMUTimer *timer;
35 } QEMUFileBuffered;
37 #ifdef DEBUG_BUFFERED_FILE
38 #define DPRINTF(fmt, ...) \
39 do { printf("buffered-file: " fmt, ## __VA_ARGS__); } while (0)
40 #else
41 #define DPRINTF(fmt, ...) \
42 do { } while (0)
43 #endif
45 static void buffered_append(QEMUFileBuffered *s,
46 const uint8_t *buf, size_t size)
48 if (size > (s->buffer_capacity - s->buffer_size)) {
49 DPRINTF("increasing buffer capacity from %zu by %zu\n",
50 s->buffer_capacity, size + 1024);
52 s->buffer_capacity += size + 1024;
54 s->buffer = g_realloc(s->buffer, s->buffer_capacity);
57 memcpy(s->buffer + s->buffer_size, buf, size);
58 s->buffer_size += size;
61 static ssize_t buffered_flush(QEMUFileBuffered *s)
63 size_t offset = 0;
64 ssize_t ret = 0;
66 DPRINTF("flushing %zu byte(s) of data\n", s->buffer_size);
68 while (s->bytes_xfer < s->xfer_limit && offset < s->buffer_size) {
70 ret = migrate_fd_put_buffer(s->migration_state, s->buffer + offset,
71 s->buffer_size - offset);
72 if (ret == -EAGAIN) {
73 DPRINTF("backend not ready, freezing\n");
74 ret = 0;
75 s->freeze_output = 1;
76 break;
79 if (ret <= 0) {
80 DPRINTF("error flushing data, %zd\n", ret);
81 break;
82 } else {
83 DPRINTF("flushed %zd byte(s)\n", ret);
84 offset += ret;
85 s->bytes_xfer += ret;
89 DPRINTF("flushed %zu of %zu byte(s)\n", offset, s->buffer_size);
90 memmove(s->buffer, s->buffer + offset, s->buffer_size - offset);
91 s->buffer_size -= offset;
93 if (ret < 0) {
94 return ret;
96 return offset;
99 static int buffered_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
101 QEMUFileBuffered *s = opaque;
102 ssize_t error;
104 DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);
106 error = qemu_file_get_error(s->file);
107 if (error) {
108 DPRINTF("flush when error, bailing: %s\n", strerror(-error));
109 return error;
112 DPRINTF("unfreezing output\n");
113 s->freeze_output = 0;
115 if (size > 0) {
116 DPRINTF("buffering %d bytes\n", size - offset);
117 buffered_append(s, buf, size);
120 error = buffered_flush(s);
121 if (error < 0) {
122 DPRINTF("buffered flush error. bailing: %s\n", strerror(-error));
123 return error;
126 if (pos == 0 && size == 0) {
127 DPRINTF("file is ready\n");
128 if (!s->freeze_output && s->bytes_xfer < s->xfer_limit) {
129 DPRINTF("notifying client\n");
130 migrate_fd_put_ready(s->migration_state);
134 return size;
137 static int buffered_close(void *opaque)
139 QEMUFileBuffered *s = opaque;
140 ssize_t ret = 0;
141 int ret2;
143 DPRINTF("closing\n");
145 s->xfer_limit = INT_MAX;
146 while (!qemu_file_get_error(s->file) && s->buffer_size) {
147 ret = buffered_flush(s);
148 if (ret < 0) {
149 break;
151 if (s->freeze_output) {
152 ret = migrate_fd_wait_for_unfreeze(s->migration_state);
153 if (ret < 0) {
154 break;
159 ret2 = migrate_fd_close(s->migration_state);
160 if (ret >= 0) {
161 ret = ret2;
163 qemu_del_timer(s->timer);
164 qemu_free_timer(s->timer);
165 g_free(s->buffer);
166 g_free(s);
168 return ret;
172 * The meaning of the return values is:
173 * 0: We can continue sending
174 * 1: Time to stop
175 * negative: There has been an error
177 static int buffered_get_fd(void *opaque)
179 QEMUFileBuffered *s = opaque;
181 return qemu_get_fd(s->file);
184 static int buffered_rate_limit(void *opaque)
186 QEMUFileBuffered *s = opaque;
187 int ret;
189 ret = qemu_file_get_error(s->file);
190 if (ret) {
191 return ret;
193 if (s->freeze_output)
194 return 1;
196 if (s->bytes_xfer > s->xfer_limit)
197 return 1;
199 return 0;
202 static int64_t buffered_set_rate_limit(void *opaque, int64_t new_rate)
204 QEMUFileBuffered *s = opaque;
205 if (qemu_file_get_error(s->file)) {
206 goto out;
208 if (new_rate > SIZE_MAX) {
209 new_rate = SIZE_MAX;
212 s->xfer_limit = new_rate / 10;
214 out:
215 return s->xfer_limit;
218 static int64_t buffered_get_rate_limit(void *opaque)
220 QEMUFileBuffered *s = opaque;
222 return s->xfer_limit;
225 static void buffered_rate_tick(void *opaque)
227 QEMUFileBuffered *s = opaque;
229 if (qemu_file_get_error(s->file)) {
230 buffered_close(s);
231 return;
234 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 100);
236 if (s->freeze_output)
237 return;
239 s->bytes_xfer = 0;
241 buffered_put_buffer(s, NULL, 0, 0);
244 static const QEMUFileOps buffered_file_ops = {
245 .get_fd = buffered_get_fd,
246 .put_buffer = buffered_put_buffer,
247 .close = buffered_close,
248 .rate_limit = buffered_rate_limit,
249 .get_rate_limit = buffered_get_rate_limit,
250 .set_rate_limit = buffered_set_rate_limit,
253 QEMUFile *qemu_fopen_ops_buffered(MigrationState *migration_state)
255 QEMUFileBuffered *s;
257 s = g_malloc0(sizeof(*s));
259 s->migration_state = migration_state;
260 s->xfer_limit = migration_state->bandwidth_limit / 10;
262 s->file = qemu_fopen_ops(s, &buffered_file_ops);
264 s->timer = qemu_new_timer_ms(rt_clock, buffered_rate_tick, s);
266 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 100);
268 return s->file;