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