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