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