]> git.proxmox.com Git - mirror_qemu.git/blame - buffered_file.c
migration: set error if select return one error
[mirror_qemu.git] / buffered_file.c
CommitLineData
39b65c2e
AL
1/*
2 * QEMU buffered QEMUFile
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#include "qemu-common.h"
15#include "hw/hw.h"
16#include "qemu-timer.h"
39b65c2e
AL
17#include "qemu-char.h"
18#include "buffered_file.h"
19
20//#define DEBUG_BUFFERED_FILE
21
22typedef struct QEMUFileBuffered
23{
24 BufferedPutFunc *put_buffer;
25 BufferedPutReadyFunc *put_ready;
26 BufferedWaitForUnfreezeFunc *wait_for_unfreeze;
27 BufferedCloseFunc *close;
28 void *opaque;
29 QEMUFile *file;
39b65c2e
AL
30 int freeze_output;
31 size_t bytes_xfer;
32 size_t xfer_limit;
33 uint8_t *buffer;
34 size_t buffer_size;
35 size_t buffer_capacity;
36 QEMUTimer *timer;
37} QEMUFileBuffered;
38
39#ifdef DEBUG_BUFFERED_FILE
d0f2c4c6 40#define DPRINTF(fmt, ...) \
39b65c2e
AL
41 do { printf("buffered-file: " fmt, ## __VA_ARGS__); } while (0)
42#else
d0f2c4c6 43#define DPRINTF(fmt, ...) \
39b65c2e
AL
44 do { } while (0)
45#endif
46
47static void buffered_append(QEMUFileBuffered *s,
48 const uint8_t *buf, size_t size)
49{
50 if (size > (s->buffer_capacity - s->buffer_size)) {
51 void *tmp;
52
d0f2c4c6 53 DPRINTF("increasing buffer capacity from %zu by %zu\n",
39b65c2e
AL
54 s->buffer_capacity, size + 1024);
55
56 s->buffer_capacity += size + 1024;
57
7267c094 58 tmp = g_realloc(s->buffer, s->buffer_capacity);
39b65c2e
AL
59 if (tmp == NULL) {
60 fprintf(stderr, "qemu file buffer expansion failed\n");
61 exit(1);
62 }
63
64 s->buffer = tmp;
65 }
66
67 memcpy(s->buffer + s->buffer_size, buf, size);
68 s->buffer_size += size;
69}
70
71static void buffered_flush(QEMUFileBuffered *s)
72{
73 size_t offset = 0;
74
eff28a5c 75 if (qemu_file_has_error(s->file)) {
d0f2c4c6 76 DPRINTF("flush when error, bailing\n");
39b65c2e
AL
77 return;
78 }
79
d0f2c4c6 80 DPRINTF("flushing %zu byte(s) of data\n", s->buffer_size);
39b65c2e
AL
81
82 while (offset < s->buffer_size) {
83 ssize_t ret;
84
85 ret = s->put_buffer(s->opaque, s->buffer + offset,
86 s->buffer_size - offset);
87 if (ret == -EAGAIN) {
d0f2c4c6 88 DPRINTF("backend not ready, freezing\n");
39b65c2e
AL
89 s->freeze_output = 1;
90 break;
91 }
92
93 if (ret <= 0) {
d0f2c4c6 94 DPRINTF("error flushing data, %zd\n", ret);
eff28a5c 95 qemu_file_set_error(s->file);
39b65c2e
AL
96 break;
97 } else {
d0f2c4c6 98 DPRINTF("flushed %zd byte(s)\n", ret);
39b65c2e
AL
99 offset += ret;
100 }
101 }
102
d0f2c4c6 103 DPRINTF("flushed %zu of %zu byte(s)\n", offset, s->buffer_size);
39b65c2e
AL
104 memmove(s->buffer, s->buffer + offset, s->buffer_size - offset);
105 s->buffer_size -= offset;
106}
107
108static int buffered_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
109{
110 QEMUFileBuffered *s = opaque;
111 int offset = 0;
112 ssize_t ret;
113
d0f2c4c6 114 DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);
39b65c2e 115
eff28a5c 116 if (qemu_file_has_error(s->file)) {
d0f2c4c6 117 DPRINTF("flush when error, bailing\n");
39b65c2e
AL
118 return -EINVAL;
119 }
120
d0f2c4c6 121 DPRINTF("unfreezing output\n");
39b65c2e
AL
122 s->freeze_output = 0;
123
124 buffered_flush(s);
125
126 while (!s->freeze_output && offset < size) {
127 if (s->bytes_xfer > s->xfer_limit) {
d0f2c4c6 128 DPRINTF("transfer limit exceeded when putting\n");
39b65c2e
AL
129 break;
130 }
131
132 ret = s->put_buffer(s->opaque, buf + offset, size - offset);
133 if (ret == -EAGAIN) {
d0f2c4c6 134 DPRINTF("backend not ready, freezing\n");
39b65c2e
AL
135 s->freeze_output = 1;
136 break;
137 }
138
139 if (ret <= 0) {
d0f2c4c6 140 DPRINTF("error putting\n");
eff28a5c 141 qemu_file_set_error(s->file);
39b65c2e
AL
142 offset = -EINVAL;
143 break;
144 }
145
d0f2c4c6 146 DPRINTF("put %zd byte(s)\n", ret);
39b65c2e
AL
147 offset += ret;
148 s->bytes_xfer += ret;
149 }
150
151 if (offset >= 0) {
d0f2c4c6 152 DPRINTF("buffering %d bytes\n", size - offset);
39b65c2e
AL
153 buffered_append(s, buf + offset, size - offset);
154 offset = size;
155 }
156
5e77aaa0
AK
157 if (pos == 0 && size == 0) {
158 DPRINTF("file is ready\n");
159 if (s->bytes_xfer <= s->xfer_limit) {
160 DPRINTF("notifying client\n");
161 s->put_ready(s->opaque);
162 }
163 }
164
39b65c2e
AL
165 return offset;
166}
167
168static int buffered_close(void *opaque)
169{
170 QEMUFileBuffered *s = opaque;
171 int ret;
172
d0f2c4c6 173 DPRINTF("closing\n");
39b65c2e 174
eff28a5c 175 while (!qemu_file_has_error(s->file) && s->buffer_size) {
39b65c2e
AL
176 buffered_flush(s);
177 if (s->freeze_output)
db448a08 178 s->wait_for_unfreeze(s->opaque);
39b65c2e
AL
179 }
180
181 ret = s->close(s->opaque);
182
183 qemu_del_timer(s->timer);
184 qemu_free_timer(s->timer);
7267c094
AL
185 g_free(s->buffer);
186 g_free(s);
39b65c2e
AL
187
188 return ret;
189}
190
4fc7d819
JQ
191/*
192 * The meaning of the return values is:
193 * 0: We can continue sending
194 * 1: Time to stop
195 * -1: There has been an error
196 */
39b65c2e
AL
197static int buffered_rate_limit(void *opaque)
198{
199 QEMUFileBuffered *s = opaque;
200
eff28a5c 201 if (qemu_file_has_error(s->file)) {
4fc7d819
JQ
202 return -1;
203 }
39b65c2e
AL
204 if (s->freeze_output)
205 return 1;
206
207 if (s->bytes_xfer > s->xfer_limit)
208 return 1;
209
210 return 0;
211}
212
3d002df3 213static int64_t buffered_set_rate_limit(void *opaque, int64_t new_rate)
19629537
GC
214{
215 QEMUFileBuffered *s = opaque;
eff28a5c 216 if (qemu_file_has_error(s->file))
19629537
GC
217 goto out;
218
3d002df3
MT
219 if (new_rate > SIZE_MAX) {
220 new_rate = SIZE_MAX;
221 }
222
19629537
GC
223 s->xfer_limit = new_rate / 10;
224
225out:
226 return s->xfer_limit;
227}
228
3d002df3 229static int64_t buffered_get_rate_limit(void *opaque)
c163b5ca
LS
230{
231 QEMUFileBuffered *s = opaque;
232
233 return s->xfer_limit;
234}
235
39b65c2e
AL
236static void buffered_rate_tick(void *opaque)
237{
238 QEMUFileBuffered *s = opaque;
239
eff28a5c 240 if (qemu_file_has_error(s->file)) {
e447b1a6 241 buffered_close(s);
39b65c2e 242 return;
e447b1a6 243 }
39b65c2e 244
7bd427d8 245 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 100);
39b65c2e
AL
246
247 if (s->freeze_output)
248 return;
249
250 s->bytes_xfer = 0;
251
252 buffered_flush(s);
253
254 /* Add some checks around this */
255 s->put_ready(s->opaque);
256}
257
258QEMUFile *qemu_fopen_ops_buffered(void *opaque,
259 size_t bytes_per_sec,
260 BufferedPutFunc *put_buffer,
261 BufferedPutReadyFunc *put_ready,
262 BufferedWaitForUnfreezeFunc *wait_for_unfreeze,
263 BufferedCloseFunc *close)
264{
265 QEMUFileBuffered *s;
266
7267c094 267 s = g_malloc0(sizeof(*s));
39b65c2e
AL
268
269 s->opaque = opaque;
270 s->xfer_limit = bytes_per_sec / 10;
271 s->put_buffer = put_buffer;
272 s->put_ready = put_ready;
273 s->wait_for_unfreeze = wait_for_unfreeze;
274 s->close = close;
275
276 s->file = qemu_fopen_ops(s, buffered_put_buffer, NULL,
19629537 277 buffered_close, buffered_rate_limit,
c163b5ca
LS
278 buffered_set_rate_limit,
279 buffered_get_rate_limit);
39b65c2e 280
7bd427d8 281 s->timer = qemu_new_timer_ms(rt_clock, buffered_rate_tick, s);
39b65c2e 282
7bd427d8 283 qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 100);
39b65c2e
AL
284
285 return s->file;
286}