]>
Commit | Line | Data |
---|---|---|
769ce76d AG |
1 | /* |
2 | * QEMU Block driver for CURL images | |
3 | * | |
4 | * Copyright (c) 2009 Alexander Graf <[email protected]> | |
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-common.h" | |
737e150e | 25 | #include "block/block_int.h" |
769ce76d AG |
26 | #include <curl/curl.h> |
27 | ||
28 | // #define DEBUG | |
29 | // #define DEBUG_VERBOSE | |
30 | ||
31 | #ifdef DEBUG_CURL | |
d0f2c4c6 | 32 | #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0) |
769ce76d | 33 | #else |
d0f2c4c6 | 34 | #define DPRINTF(fmt, ...) do { } while (0) |
769ce76d AG |
35 | #endif |
36 | ||
031fd1be PM |
37 | #if LIBCURL_VERSION_NUM >= 0x071000 |
38 | /* The multi interface timer callback was introduced in 7.16.0 */ | |
39 | #define NEED_CURL_TIMER_CALLBACK | |
40 | #endif | |
41 | ||
fb6d1bbd SH |
42 | #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ |
43 | CURLPROTO_FTP | CURLPROTO_FTPS | \ | |
44 | CURLPROTO_TFTP) | |
45 | ||
769ce76d AG |
46 | #define CURL_NUM_STATES 8 |
47 | #define CURL_NUM_ACB 8 | |
48 | #define SECTOR_SIZE 512 | |
49 | #define READ_AHEAD_SIZE (256 * 1024) | |
50 | ||
51 | #define FIND_RET_NONE 0 | |
52 | #define FIND_RET_OK 1 | |
53 | #define FIND_RET_WAIT 2 | |
54 | ||
55 | struct BDRVCURLState; | |
56 | ||
57 | typedef struct CURLAIOCB { | |
58 | BlockDriverAIOCB common; | |
363c3c85 | 59 | QEMUBH *bh; |
769ce76d | 60 | QEMUIOVector *qiov; |
363c3c85 NT |
61 | |
62 | int64_t sector_num; | |
63 | int nb_sectors; | |
64 | ||
769ce76d AG |
65 | size_t start; |
66 | size_t end; | |
67 | } CURLAIOCB; | |
68 | ||
69 | typedef struct CURLState | |
70 | { | |
71 | struct BDRVCURLState *s; | |
72 | CURLAIOCB *acb[CURL_NUM_ACB]; | |
73 | CURL *curl; | |
74 | char *orig_buf; | |
75 | size_t buf_start; | |
76 | size_t buf_off; | |
77 | size_t buf_len; | |
78 | char range[128]; | |
79 | char errmsg[CURL_ERROR_SIZE]; | |
80 | char in_use; | |
81 | } CURLState; | |
82 | ||
83 | typedef struct BDRVCURLState { | |
84 | CURLM *multi; | |
031fd1be | 85 | QEMUTimer timer; |
769ce76d AG |
86 | size_t len; |
87 | CURLState states[CURL_NUM_STATES]; | |
88 | char *url; | |
c76f4952 | 89 | size_t readahead_size; |
3494d650 | 90 | bool accept_range; |
769ce76d AG |
91 | } BDRVCURLState; |
92 | ||
93 | static void curl_clean_state(CURLState *s); | |
94 | static void curl_multi_do(void *arg); | |
95 | ||
031fd1be PM |
96 | #ifdef NEED_CURL_TIMER_CALLBACK |
97 | static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) | |
98 | { | |
99 | BDRVCURLState *s = opaque; | |
100 | ||
101 | DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms); | |
102 | if (timeout_ms == -1) { | |
103 | timer_del(&s->timer); | |
104 | } else { | |
105 | int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; | |
106 | timer_mod(&s->timer, | |
107 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); | |
108 | } | |
109 | return 0; | |
110 | } | |
111 | #endif | |
112 | ||
769ce76d AG |
113 | static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, |
114 | void *s, void *sp) | |
115 | { | |
d0f2c4c6 | 116 | DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd); |
769ce76d AG |
117 | switch (action) { |
118 | case CURL_POLL_IN: | |
f2e5dca4 | 119 | qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, s); |
769ce76d AG |
120 | break; |
121 | case CURL_POLL_OUT: | |
f2e5dca4 | 122 | qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, s); |
769ce76d AG |
123 | break; |
124 | case CURL_POLL_INOUT: | |
f2e5dca4 | 125 | qemu_aio_set_fd_handler(fd, curl_multi_do, curl_multi_do, s); |
769ce76d AG |
126 | break; |
127 | case CURL_POLL_REMOVE: | |
f2e5dca4 | 128 | qemu_aio_set_fd_handler(fd, NULL, NULL, NULL); |
769ce76d AG |
129 | break; |
130 | } | |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
3494d650 | 135 | static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
769ce76d | 136 | { |
3494d650 | 137 | BDRVCURLState *s = opaque; |
769ce76d | 138 | size_t realsize = size * nmemb; |
3494d650 | 139 | const char *accept_line = "Accept-Ranges: bytes"; |
769ce76d | 140 | |
3494d650 FZ |
141 | if (realsize >= strlen(accept_line) |
142 | && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) { | |
143 | s->accept_range = true; | |
0bfcd599 | 144 | } |
769ce76d AG |
145 | |
146 | return realsize; | |
147 | } | |
148 | ||
149 | static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) | |
150 | { | |
151 | CURLState *s = ((CURLState*)opaque); | |
152 | size_t realsize = size * nmemb; | |
153 | int i; | |
154 | ||
0bfcd599 | 155 | DPRINTF("CURL: Just reading %zd bytes\n", realsize); |
769ce76d AG |
156 | |
157 | if (!s || !s->orig_buf) | |
158 | goto read_end; | |
159 | ||
6d4b9e55 FZ |
160 | if (s->buf_off >= s->buf_len) { |
161 | /* buffer full, read nothing */ | |
162 | return 0; | |
163 | } | |
164 | realsize = MIN(realsize, s->buf_len - s->buf_off); | |
769ce76d AG |
165 | memcpy(s->orig_buf + s->buf_off, ptr, realsize); |
166 | s->buf_off += realsize; | |
167 | ||
168 | for(i=0; i<CURL_NUM_ACB; i++) { | |
169 | CURLAIOCB *acb = s->acb[i]; | |
170 | ||
171 | if (!acb) | |
172 | continue; | |
173 | ||
174 | if ((s->buf_off >= acb->end)) { | |
03396148 MT |
175 | qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start, |
176 | acb->end - acb->start); | |
769ce76d AG |
177 | acb->common.cb(acb->common.opaque, 0); |
178 | qemu_aio_release(acb); | |
179 | s->acb[i] = NULL; | |
180 | } | |
181 | } | |
182 | ||
183 | read_end: | |
184 | return realsize; | |
185 | } | |
186 | ||
187 | static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len, | |
188 | CURLAIOCB *acb) | |
189 | { | |
190 | int i; | |
191 | size_t end = start + len; | |
192 | ||
193 | for (i=0; i<CURL_NUM_STATES; i++) { | |
194 | CURLState *state = &s->states[i]; | |
195 | size_t buf_end = (state->buf_start + state->buf_off); | |
196 | size_t buf_fend = (state->buf_start + state->buf_len); | |
197 | ||
198 | if (!state->orig_buf) | |
199 | continue; | |
200 | if (!state->buf_off) | |
201 | continue; | |
202 | ||
203 | // Does the existing buffer cover our section? | |
204 | if ((start >= state->buf_start) && | |
205 | (start <= buf_end) && | |
206 | (end >= state->buf_start) && | |
207 | (end <= buf_end)) | |
208 | { | |
209 | char *buf = state->orig_buf + (start - state->buf_start); | |
210 | ||
03396148 | 211 | qemu_iovec_from_buf(acb->qiov, 0, buf, len); |
769ce76d AG |
212 | acb->common.cb(acb->common.opaque, 0); |
213 | ||
214 | return FIND_RET_OK; | |
215 | } | |
216 | ||
217 | // Wait for unfinished chunks | |
218 | if ((start >= state->buf_start) && | |
219 | (start <= buf_fend) && | |
220 | (end >= state->buf_start) && | |
221 | (end <= buf_fend)) | |
222 | { | |
223 | int j; | |
224 | ||
225 | acb->start = start - state->buf_start; | |
226 | acb->end = acb->start + len; | |
227 | ||
228 | for (j=0; j<CURL_NUM_ACB; j++) { | |
229 | if (!state->acb[j]) { | |
230 | state->acb[j] = acb; | |
231 | return FIND_RET_WAIT; | |
232 | } | |
233 | } | |
234 | } | |
235 | } | |
236 | ||
237 | return FIND_RET_NONE; | |
238 | } | |
239 | ||
031fd1be | 240 | static void curl_multi_read(BDRVCURLState *s) |
769ce76d | 241 | { |
769ce76d AG |
242 | int msgs_in_queue; |
243 | ||
769ce76d AG |
244 | /* Try to find done transfers, so we can free the easy |
245 | * handle again. */ | |
246 | do { | |
247 | CURLMsg *msg; | |
248 | msg = curl_multi_info_read(s->multi, &msgs_in_queue); | |
249 | ||
250 | if (!msg) | |
251 | break; | |
252 | if (msg->msg == CURLMSG_NONE) | |
253 | break; | |
254 | ||
255 | switch (msg->msg) { | |
256 | case CURLMSG_DONE: | |
257 | { | |
258 | CURLState *state = NULL; | |
f6246509 MB |
259 | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, |
260 | (char **)&state); | |
f785a5ae NT |
261 | |
262 | /* ACBs for successful messages get completed in curl_read_cb */ | |
263 | if (msg->data.result != CURLE_OK) { | |
264 | int i; | |
265 | for (i = 0; i < CURL_NUM_ACB; i++) { | |
266 | CURLAIOCB *acb = state->acb[i]; | |
267 | ||
268 | if (acb == NULL) { | |
269 | continue; | |
270 | } | |
271 | ||
272 | acb->common.cb(acb->common.opaque, -EIO); | |
273 | qemu_aio_release(acb); | |
274 | state->acb[i] = NULL; | |
275 | } | |
276 | } | |
277 | ||
769ce76d AG |
278 | curl_clean_state(state); |
279 | break; | |
280 | } | |
281 | default: | |
282 | msgs_in_queue = 0; | |
283 | break; | |
284 | } | |
285 | } while(msgs_in_queue); | |
286 | } | |
287 | ||
031fd1be PM |
288 | static void curl_multi_do(void *arg) |
289 | { | |
290 | BDRVCURLState *s = (BDRVCURLState *)arg; | |
291 | int running; | |
292 | int r; | |
293 | ||
294 | if (!s->multi) { | |
295 | return; | |
296 | } | |
297 | ||
298 | do { | |
299 | r = curl_multi_socket_all(s->multi, &running); | |
300 | } while(r == CURLM_CALL_MULTI_PERFORM); | |
301 | ||
302 | curl_multi_read(s); | |
303 | } | |
304 | ||
305 | static void curl_multi_timeout_do(void *arg) | |
306 | { | |
307 | #ifdef NEED_CURL_TIMER_CALLBACK | |
308 | BDRVCURLState *s = (BDRVCURLState *)arg; | |
309 | int running; | |
310 | ||
311 | if (!s->multi) { | |
312 | return; | |
313 | } | |
314 | ||
315 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); | |
316 | ||
317 | curl_multi_read(s); | |
318 | #else | |
319 | abort(); | |
320 | #endif | |
321 | } | |
322 | ||
769ce76d AG |
323 | static CURLState *curl_init_state(BDRVCURLState *s) |
324 | { | |
325 | CURLState *state = NULL; | |
326 | int i, j; | |
327 | ||
328 | do { | |
329 | for (i=0; i<CURL_NUM_STATES; i++) { | |
330 | for (j=0; j<CURL_NUM_ACB; j++) | |
331 | if (s->states[i].acb[j]) | |
332 | continue; | |
333 | if (s->states[i].in_use) | |
334 | continue; | |
335 | ||
336 | state = &s->states[i]; | |
337 | state->in_use = 1; | |
338 | break; | |
339 | } | |
340 | if (!state) { | |
fb7c8e8a | 341 | g_usleep(100); |
769ce76d AG |
342 | curl_multi_do(s); |
343 | } | |
344 | } while(!state); | |
345 | ||
346 | if (state->curl) | |
347 | goto has_curl; | |
348 | ||
349 | state->curl = curl_easy_init(); | |
350 | if (!state->curl) | |
351 | return NULL; | |
352 | curl_easy_setopt(state->curl, CURLOPT_URL, s->url); | |
353 | curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5); | |
df3cee1a | 354 | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb); |
769ce76d AG |
355 | curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); |
356 | curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); | |
357 | curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); | |
358 | curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); | |
359 | curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); | |
360 | curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); | |
f785a5ae NT |
361 | curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); |
362 | ||
fb6d1bbd SH |
363 | /* Restrict supported protocols to avoid security issues in the more |
364 | * obscure protocols. For example, do not allow POP3/SMTP/IMAP see | |
365 | * CVE-2013-0249. | |
8a8f5840 SH |
366 | * |
367 | * Restricting protocols is only supported from 7.19.4 upwards. | |
fb6d1bbd | 368 | */ |
8a8f5840 | 369 | #if LIBCURL_VERSION_NUM >= 0x071304 |
fb6d1bbd SH |
370 | curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); |
371 | curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); | |
8a8f5840 | 372 | #endif |
fb6d1bbd | 373 | |
769ce76d AG |
374 | #ifdef DEBUG_VERBOSE |
375 | curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); | |
376 | #endif | |
377 | ||
378 | has_curl: | |
379 | ||
380 | state->s = s; | |
381 | ||
382 | return state; | |
383 | } | |
384 | ||
385 | static void curl_clean_state(CURLState *s) | |
386 | { | |
387 | if (s->s->multi) | |
388 | curl_multi_remove_handle(s->s->multi, s->curl); | |
389 | s->in_use = 0; | |
390 | } | |
391 | ||
8e6d58cd KW |
392 | static void curl_parse_filename(const char *filename, QDict *options, |
393 | Error **errp) | |
769ce76d | 394 | { |
c76f4952 N |
395 | |
396 | #define RA_OPTSTR ":readahead=" | |
397 | char *file; | |
398 | char *ra; | |
399 | const char *ra_val; | |
400 | int parse_state = 0; | |
401 | ||
7267c094 | 402 | file = g_strdup(filename); |
c76f4952 N |
403 | |
404 | /* Parse a trailing ":readahead=#:" param, if present. */ | |
405 | ra = file + strlen(file) - 1; | |
406 | while (ra >= file) { | |
407 | if (parse_state == 0) { | |
8e6d58cd | 408 | if (*ra == ':') { |
c76f4952 | 409 | parse_state++; |
8e6d58cd | 410 | } else { |
c76f4952 | 411 | break; |
8e6d58cd | 412 | } |
c76f4952 N |
413 | } else if (parse_state == 1) { |
414 | if (*ra > '9' || *ra < '0') { | |
415 | char *opt_start = ra - strlen(RA_OPTSTR) + 1; | |
416 | if (opt_start > file && | |
417 | strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) { | |
418 | ra_val = ra + 1; | |
419 | ra -= strlen(RA_OPTSTR) - 1; | |
420 | *ra = '\0'; | |
8e6d58cd | 421 | qdict_put(options, "readahead", qstring_from_str(ra_val)); |
c76f4952 | 422 | } |
8e6d58cd | 423 | break; |
c76f4952 N |
424 | } |
425 | } | |
426 | ra--; | |
427 | } | |
428 | ||
8e6d58cd KW |
429 | qdict_put(options, "url", qstring_from_str(file)); |
430 | ||
431 | g_free(file); | |
432 | } | |
433 | ||
434 | static QemuOptsList runtime_opts = { | |
435 | .name = "curl", | |
436 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
437 | .desc = { | |
438 | { | |
439 | .name = "url", | |
440 | .type = QEMU_OPT_STRING, | |
441 | .help = "URL to open", | |
442 | }, | |
443 | { | |
444 | .name = "readahead", | |
445 | .type = QEMU_OPT_SIZE, | |
446 | .help = "Readahead size", | |
447 | }, | |
448 | { /* end of list */ } | |
449 | }, | |
450 | }; | |
451 | ||
015a1036 HR |
452 | static int curl_open(BlockDriverState *bs, QDict *options, int flags, |
453 | Error **errp) | |
8e6d58cd KW |
454 | { |
455 | BDRVCURLState *s = bs->opaque; | |
456 | CURLState *state = NULL; | |
457 | QemuOpts *opts; | |
458 | Error *local_err = NULL; | |
459 | const char *file; | |
460 | double d; | |
461 | ||
462 | static int inited = 0; | |
463 | ||
a7cea2ba | 464 | if (flags & BDRV_O_RDWR) { |
2a94fee3 | 465 | error_setg(errp, "curl block device does not support writes"); |
a7cea2ba RJ |
466 | return -EROFS; |
467 | } | |
468 | ||
87ea75d5 | 469 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
8e6d58cd | 470 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 471 | if (local_err) { |
2a94fee3 | 472 | error_propagate(errp, local_err); |
8e6d58cd KW |
473 | goto out_noclean; |
474 | } | |
475 | ||
476 | s->readahead_size = qemu_opt_get_size(opts, "readahead", READ_AHEAD_SIZE); | |
c76f4952 | 477 | if ((s->readahead_size & 0x1ff) != 0) { |
2a94fee3 PB |
478 | error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", |
479 | s->readahead_size); | |
c76f4952 N |
480 | goto out_noclean; |
481 | } | |
482 | ||
8e6d58cd KW |
483 | file = qemu_opt_get(opts, "url"); |
484 | if (file == NULL) { | |
2a94fee3 | 485 | error_setg(errp, "curl block driver requires an 'url' option"); |
8e6d58cd KW |
486 | goto out_noclean; |
487 | } | |
488 | ||
769ce76d AG |
489 | if (!inited) { |
490 | curl_global_init(CURL_GLOBAL_ALL); | |
491 | inited = 1; | |
492 | } | |
493 | ||
d0f2c4c6 | 494 | DPRINTF("CURL: Opening %s\n", file); |
8e6d58cd | 495 | s->url = g_strdup(file); |
769ce76d AG |
496 | state = curl_init_state(s); |
497 | if (!state) | |
498 | goto out_noclean; | |
499 | ||
500 | // Get file size | |
501 | ||
3494d650 | 502 | s->accept_range = false; |
769ce76d | 503 | curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); |
3494d650 FZ |
504 | curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, |
505 | curl_header_cb); | |
506 | curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); | |
769ce76d AG |
507 | if (curl_easy_perform(state->curl)) |
508 | goto out; | |
509 | curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d); | |
769ce76d AG |
510 | if (d) |
511 | s->len = (size_t)d; | |
512 | else if(!s->len) | |
513 | goto out; | |
3494d650 FZ |
514 | if ((!strncasecmp(s->url, "http://", strlen("http://")) |
515 | || !strncasecmp(s->url, "https://", strlen("https://"))) | |
516 | && !s->accept_range) { | |
517 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
518 | "Server does not support 'range' (byte ranges)."); | |
519 | goto out; | |
520 | } | |
0bfcd599 | 521 | DPRINTF("CURL: Size = %zd\n", s->len); |
769ce76d AG |
522 | |
523 | curl_clean_state(state); | |
524 | curl_easy_cleanup(state->curl); | |
525 | state->curl = NULL; | |
526 | ||
031fd1be PM |
527 | aio_timer_init(bdrv_get_aio_context(bs), &s->timer, |
528 | QEMU_CLOCK_REALTIME, SCALE_NS, | |
529 | curl_multi_timeout_do, s); | |
530 | ||
769ce76d AG |
531 | // Now we know the file exists and its size, so let's |
532 | // initialize the multi interface! | |
533 | ||
534 | s->multi = curl_multi_init(); | |
9e5e2b23 RJ |
535 | curl_multi_setopt(s->multi, CURLMOPT_SOCKETDATA, s); |
536 | curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); | |
031fd1be PM |
537 | #ifdef NEED_CURL_TIMER_CALLBACK |
538 | curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); | |
539 | curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); | |
540 | #endif | |
769ce76d AG |
541 | curl_multi_do(s); |
542 | ||
8e6d58cd | 543 | qemu_opts_del(opts); |
769ce76d AG |
544 | return 0; |
545 | ||
546 | out: | |
acd7fdc6 | 547 | error_setg(errp, "CURL: Error opening file: %s", state->errmsg); |
769ce76d AG |
548 | curl_easy_cleanup(state->curl); |
549 | state->curl = NULL; | |
550 | out_noclean: | |
8e6d58cd KW |
551 | g_free(s->url); |
552 | qemu_opts_del(opts); | |
769ce76d AG |
553 | return -EINVAL; |
554 | } | |
555 | ||
c16b5a2c CH |
556 | static void curl_aio_cancel(BlockDriverAIOCB *blockacb) |
557 | { | |
558 | // Do we have to implement canceling? Seems to work without... | |
559 | } | |
560 | ||
d7331bed | 561 | static const AIOCBInfo curl_aiocb_info = { |
c16b5a2c CH |
562 | .aiocb_size = sizeof(CURLAIOCB), |
563 | .cancel = curl_aio_cancel, | |
564 | }; | |
565 | ||
363c3c85 NT |
566 | |
567 | static void curl_readv_bh_cb(void *p) | |
769ce76d | 568 | { |
769ce76d AG |
569 | CURLState *state; |
570 | ||
363c3c85 NT |
571 | CURLAIOCB *acb = p; |
572 | BDRVCURLState *s = acb->common.bs->opaque; | |
769ce76d | 573 | |
363c3c85 NT |
574 | qemu_bh_delete(acb->bh); |
575 | acb->bh = NULL; | |
576 | ||
577 | size_t start = acb->sector_num * SECTOR_SIZE; | |
578 | size_t end; | |
769ce76d AG |
579 | |
580 | // In case we have the requested data already (e.g. read-ahead), | |
581 | // we can just call the callback and be done. | |
363c3c85 | 582 | switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) { |
769ce76d AG |
583 | case FIND_RET_OK: |
584 | qemu_aio_release(acb); | |
585 | // fall through | |
586 | case FIND_RET_WAIT: | |
363c3c85 | 587 | return; |
769ce76d AG |
588 | default: |
589 | break; | |
590 | } | |
591 | ||
592 | // No cache found, so let's start a new request | |
769ce76d | 593 | state = curl_init_state(s); |
363c3c85 NT |
594 | if (!state) { |
595 | acb->common.cb(acb->common.opaque, -EIO); | |
596 | qemu_aio_release(acb); | |
597 | return; | |
598 | } | |
769ce76d AG |
599 | |
600 | acb->start = 0; | |
363c3c85 | 601 | acb->end = (acb->nb_sectors * SECTOR_SIZE); |
769ce76d AG |
602 | |
603 | state->buf_off = 0; | |
604 | if (state->orig_buf) | |
7267c094 | 605 | g_free(state->orig_buf); |
769ce76d | 606 | state->buf_start = start; |
c76f4952 | 607 | state->buf_len = acb->end + s->readahead_size; |
769ce76d | 608 | end = MIN(start + state->buf_len, s->len) - 1; |
7267c094 | 609 | state->orig_buf = g_malloc(state->buf_len); |
769ce76d AG |
610 | state->acb[0] = acb; |
611 | ||
0bfcd599 BS |
612 | snprintf(state->range, 127, "%zd-%zd", start, end); |
613 | DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n", | |
363c3c85 | 614 | (acb->nb_sectors * SECTOR_SIZE), start, state->range); |
769ce76d AG |
615 | curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); |
616 | ||
617 | curl_multi_add_handle(s->multi, state->curl); | |
618 | curl_multi_do(s); | |
619 | ||
363c3c85 NT |
620 | } |
621 | ||
622 | static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs, | |
623 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
624 | BlockDriverCompletionFunc *cb, void *opaque) | |
625 | { | |
626 | CURLAIOCB *acb; | |
627 | ||
d7331bed | 628 | acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque); |
363c3c85 | 629 | |
363c3c85 NT |
630 | acb->qiov = qiov; |
631 | acb->sector_num = sector_num; | |
632 | acb->nb_sectors = nb_sectors; | |
633 | ||
634 | acb->bh = qemu_bh_new(curl_readv_bh_cb, acb); | |
363c3c85 | 635 | qemu_bh_schedule(acb->bh); |
769ce76d AG |
636 | return &acb->common; |
637 | } | |
638 | ||
769ce76d AG |
639 | static void curl_close(BlockDriverState *bs) |
640 | { | |
641 | BDRVCURLState *s = bs->opaque; | |
642 | int i; | |
643 | ||
d0f2c4c6 | 644 | DPRINTF("CURL: Close\n"); |
769ce76d AG |
645 | for (i=0; i<CURL_NUM_STATES; i++) { |
646 | if (s->states[i].in_use) | |
647 | curl_clean_state(&s->states[i]); | |
648 | if (s->states[i].curl) { | |
649 | curl_easy_cleanup(s->states[i].curl); | |
650 | s->states[i].curl = NULL; | |
651 | } | |
652 | if (s->states[i].orig_buf) { | |
7267c094 | 653 | g_free(s->states[i].orig_buf); |
769ce76d AG |
654 | s->states[i].orig_buf = NULL; |
655 | } | |
656 | } | |
657 | if (s->multi) | |
658 | curl_multi_cleanup(s->multi); | |
031fd1be PM |
659 | |
660 | timer_del(&s->timer); | |
661 | ||
45724d6d | 662 | g_free(s->url); |
769ce76d AG |
663 | } |
664 | ||
665 | static int64_t curl_getlength(BlockDriverState *bs) | |
666 | { | |
667 | BDRVCURLState *s = bs->opaque; | |
668 | return s->len; | |
669 | } | |
670 | ||
671 | static BlockDriver bdrv_http = { | |
8e6d58cd KW |
672 | .format_name = "http", |
673 | .protocol_name = "http", | |
769ce76d | 674 | |
8e6d58cd KW |
675 | .instance_size = sizeof(BDRVCURLState), |
676 | .bdrv_parse_filename = curl_parse_filename, | |
677 | .bdrv_file_open = curl_open, | |
678 | .bdrv_close = curl_close, | |
679 | .bdrv_getlength = curl_getlength, | |
769ce76d | 680 | |
8e6d58cd | 681 | .bdrv_aio_readv = curl_aio_readv, |
769ce76d AG |
682 | }; |
683 | ||
684 | static BlockDriver bdrv_https = { | |
8e6d58cd KW |
685 | .format_name = "https", |
686 | .protocol_name = "https", | |
769ce76d | 687 | |
8e6d58cd KW |
688 | .instance_size = sizeof(BDRVCURLState), |
689 | .bdrv_parse_filename = curl_parse_filename, | |
690 | .bdrv_file_open = curl_open, | |
691 | .bdrv_close = curl_close, | |
692 | .bdrv_getlength = curl_getlength, | |
769ce76d | 693 | |
8e6d58cd | 694 | .bdrv_aio_readv = curl_aio_readv, |
769ce76d AG |
695 | }; |
696 | ||
697 | static BlockDriver bdrv_ftp = { | |
8e6d58cd KW |
698 | .format_name = "ftp", |
699 | .protocol_name = "ftp", | |
769ce76d | 700 | |
8e6d58cd KW |
701 | .instance_size = sizeof(BDRVCURLState), |
702 | .bdrv_parse_filename = curl_parse_filename, | |
703 | .bdrv_file_open = curl_open, | |
704 | .bdrv_close = curl_close, | |
705 | .bdrv_getlength = curl_getlength, | |
769ce76d | 706 | |
8e6d58cd | 707 | .bdrv_aio_readv = curl_aio_readv, |
769ce76d AG |
708 | }; |
709 | ||
710 | static BlockDriver bdrv_ftps = { | |
8e6d58cd KW |
711 | .format_name = "ftps", |
712 | .protocol_name = "ftps", | |
769ce76d | 713 | |
8e6d58cd KW |
714 | .instance_size = sizeof(BDRVCURLState), |
715 | .bdrv_parse_filename = curl_parse_filename, | |
716 | .bdrv_file_open = curl_open, | |
717 | .bdrv_close = curl_close, | |
718 | .bdrv_getlength = curl_getlength, | |
769ce76d | 719 | |
8e6d58cd | 720 | .bdrv_aio_readv = curl_aio_readv, |
769ce76d AG |
721 | }; |
722 | ||
723 | static BlockDriver bdrv_tftp = { | |
8e6d58cd KW |
724 | .format_name = "tftp", |
725 | .protocol_name = "tftp", | |
769ce76d | 726 | |
8e6d58cd KW |
727 | .instance_size = sizeof(BDRVCURLState), |
728 | .bdrv_parse_filename = curl_parse_filename, | |
729 | .bdrv_file_open = curl_open, | |
730 | .bdrv_close = curl_close, | |
731 | .bdrv_getlength = curl_getlength, | |
769ce76d | 732 | |
8e6d58cd | 733 | .bdrv_aio_readv = curl_aio_readv, |
769ce76d AG |
734 | }; |
735 | ||
736 | static void curl_block_init(void) | |
737 | { | |
738 | bdrv_register(&bdrv_http); | |
739 | bdrv_register(&bdrv_https); | |
740 | bdrv_register(&bdrv_ftp); | |
741 | bdrv_register(&bdrv_ftps); | |
742 | bdrv_register(&bdrv_tftp); | |
743 | } | |
744 | ||
745 | block_init(curl_block_init); |