tests: Remove redundant assignment
[qemu/ar7.git] / block / curl.c
blob2708d57c2f908c38fb8fc0facd1a5aa7b06d3f5d
1 /*
2 * QEMU Block driver for CURL images
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu-common.h"
27 #include "qemu/error-report.h"
28 #include "block/block_int.h"
29 #include "qapi/qmp/qbool.h"
30 #include "qapi/qmp/qstring.h"
31 #include "crypto/secret.h"
32 #include <curl/curl.h>
33 #include "qemu/cutils.h"
35 // #define DEBUG_CURL
36 // #define DEBUG_VERBOSE
38 #ifdef DEBUG_CURL
39 #define DEBUG_CURL_PRINT 1
40 #else
41 #define DEBUG_CURL_PRINT 0
42 #endif
43 #define DPRINTF(fmt, ...) \
44 do { \
45 if (DEBUG_CURL_PRINT) { \
46 fprintf(stderr, fmt, ## __VA_ARGS__); \
47 } \
48 } while (0)
50 #if LIBCURL_VERSION_NUM >= 0x071000
51 /* The multi interface timer callback was introduced in 7.16.0 */
52 #define NEED_CURL_TIMER_CALLBACK
53 #define HAVE_SOCKET_ACTION
54 #endif
56 #ifndef HAVE_SOCKET_ACTION
57 /* If curl_multi_socket_action isn't available, define it statically here in
58 * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
59 * less efficient but still safe. */
60 static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
61 curl_socket_t sockfd,
62 int ev_bitmask,
63 int *running_handles)
65 return curl_multi_socket(multi_handle, sockfd, running_handles);
67 #define curl_multi_socket_action __curl_multi_socket_action
68 #endif
70 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
71 CURLPROTO_FTP | CURLPROTO_FTPS)
73 #define CURL_NUM_STATES 8
74 #define CURL_NUM_ACB 8
75 #define READ_AHEAD_DEFAULT (256 * 1024)
76 #define CURL_TIMEOUT_DEFAULT 5
77 #define CURL_TIMEOUT_MAX 10000
79 #define FIND_RET_NONE 0
80 #define FIND_RET_OK 1
81 #define FIND_RET_WAIT 2
83 #define CURL_BLOCK_OPT_URL "url"
84 #define CURL_BLOCK_OPT_READAHEAD "readahead"
85 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
86 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
87 #define CURL_BLOCK_OPT_COOKIE "cookie"
88 #define CURL_BLOCK_OPT_USERNAME "username"
89 #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
90 #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
91 #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
93 struct BDRVCURLState;
95 typedef struct CURLAIOCB {
96 BlockAIOCB common;
97 QEMUIOVector *qiov;
99 int64_t sector_num;
100 int nb_sectors;
102 size_t start;
103 size_t end;
104 } CURLAIOCB;
106 typedef struct CURLSocket {
107 int fd;
108 QLIST_ENTRY(CURLSocket) next;
109 } CURLSocket;
111 typedef struct CURLState
113 struct BDRVCURLState *s;
114 CURLAIOCB *acb[CURL_NUM_ACB];
115 CURL *curl;
116 QLIST_HEAD(, CURLSocket) sockets;
117 char *orig_buf;
118 size_t buf_start;
119 size_t buf_off;
120 size_t buf_len;
121 char range[128];
122 char errmsg[CURL_ERROR_SIZE];
123 char in_use;
124 } CURLState;
126 typedef struct BDRVCURLState {
127 CURLM *multi;
128 QEMUTimer timer;
129 size_t len;
130 CURLState states[CURL_NUM_STATES];
131 char *url;
132 size_t readahead_size;
133 bool sslverify;
134 uint64_t timeout;
135 char *cookie;
136 bool accept_range;
137 AioContext *aio_context;
138 QemuMutex mutex;
139 char *username;
140 char *password;
141 char *proxyusername;
142 char *proxypassword;
143 } BDRVCURLState;
145 static void curl_clean_state(CURLState *s);
146 static void curl_multi_do(void *arg);
147 static void curl_multi_read(void *arg);
149 #ifdef NEED_CURL_TIMER_CALLBACK
150 static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
152 BDRVCURLState *s = opaque;
154 DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
155 if (timeout_ms == -1) {
156 timer_del(&s->timer);
157 } else {
158 int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
159 timer_mod(&s->timer,
160 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
162 return 0;
164 #endif
166 static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
167 void *userp, void *sp)
169 BDRVCURLState *s;
170 CURLState *state = NULL;
171 CURLSocket *socket;
173 curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
174 s = state->s;
176 QLIST_FOREACH(socket, &state->sockets, next) {
177 if (socket->fd == fd) {
178 if (action == CURL_POLL_REMOVE) {
179 QLIST_REMOVE(socket, next);
180 g_free(socket);
182 break;
185 if (!socket) {
186 socket = g_new0(CURLSocket, 1);
187 socket->fd = fd;
188 QLIST_INSERT_HEAD(&state->sockets, socket, next);
190 socket = NULL;
192 DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, (int)fd);
193 switch (action) {
194 case CURL_POLL_IN:
195 aio_set_fd_handler(s->aio_context, fd, false,
196 curl_multi_read, NULL, NULL, state);
197 break;
198 case CURL_POLL_OUT:
199 aio_set_fd_handler(s->aio_context, fd, false,
200 NULL, curl_multi_do, NULL, state);
201 break;
202 case CURL_POLL_INOUT:
203 aio_set_fd_handler(s->aio_context, fd, false,
204 curl_multi_read, curl_multi_do, NULL, state);
205 break;
206 case CURL_POLL_REMOVE:
207 aio_set_fd_handler(s->aio_context, fd, false,
208 NULL, NULL, NULL, NULL);
209 break;
212 return 0;
215 static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
217 BDRVCURLState *s = opaque;
218 size_t realsize = size * nmemb;
219 const char *accept_line = "Accept-Ranges: bytes";
221 if (realsize >= strlen(accept_line)
222 && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
223 s->accept_range = true;
226 return realsize;
229 static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
231 CURLState *s = ((CURLState*)opaque);
232 size_t realsize = size * nmemb;
233 int i;
235 DPRINTF("CURL: Just reading %zd bytes\n", realsize);
237 if (!s || !s->orig_buf) {
238 goto read_end;
241 if (s->buf_off >= s->buf_len) {
242 /* buffer full, read nothing */
243 goto read_end;
245 realsize = MIN(realsize, s->buf_len - s->buf_off);
246 memcpy(s->orig_buf + s->buf_off, ptr, realsize);
247 s->buf_off += realsize;
249 for(i=0; i<CURL_NUM_ACB; i++) {
250 CURLAIOCB *acb = s->acb[i];
252 if (!acb)
253 continue;
255 if ((s->buf_off >= acb->end)) {
256 size_t request_length = acb->nb_sectors * BDRV_SECTOR_SIZE;
258 qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
259 acb->end - acb->start);
261 if (acb->end - acb->start < request_length) {
262 size_t offset = acb->end - acb->start;
263 qemu_iovec_memset(acb->qiov, offset, 0,
264 request_length - offset);
267 acb->common.cb(acb->common.opaque, 0);
268 qemu_aio_unref(acb);
269 s->acb[i] = NULL;
273 read_end:
274 /* curl will error out if we do not return this value */
275 return size * nmemb;
278 static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
279 CURLAIOCB *acb)
281 int i;
282 size_t end = start + len;
283 size_t clamped_end = MIN(end, s->len);
284 size_t clamped_len = clamped_end - start;
286 for (i=0; i<CURL_NUM_STATES; i++) {
287 CURLState *state = &s->states[i];
288 size_t buf_end = (state->buf_start + state->buf_off);
289 size_t buf_fend = (state->buf_start + state->buf_len);
291 if (!state->orig_buf)
292 continue;
293 if (!state->buf_off)
294 continue;
296 // Does the existing buffer cover our section?
297 if ((start >= state->buf_start) &&
298 (start <= buf_end) &&
299 (clamped_end >= state->buf_start) &&
300 (clamped_end <= buf_end))
302 char *buf = state->orig_buf + (start - state->buf_start);
304 qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len);
305 if (clamped_len < len) {
306 qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len);
308 acb->common.cb(acb->common.opaque, 0);
310 return FIND_RET_OK;
313 // Wait for unfinished chunks
314 if (state->in_use &&
315 (start >= state->buf_start) &&
316 (start <= buf_fend) &&
317 (clamped_end >= state->buf_start) &&
318 (clamped_end <= buf_fend))
320 int j;
322 acb->start = start - state->buf_start;
323 acb->end = acb->start + clamped_len;
325 for (j=0; j<CURL_NUM_ACB; j++) {
326 if (!state->acb[j]) {
327 state->acb[j] = acb;
328 return FIND_RET_WAIT;
334 return FIND_RET_NONE;
337 /* Called with s->mutex held. */
338 static void curl_multi_check_completion(BDRVCURLState *s)
340 int msgs_in_queue;
342 /* Try to find done transfers, so we can free the easy
343 * handle again. */
344 for (;;) {
345 CURLMsg *msg;
346 msg = curl_multi_info_read(s->multi, &msgs_in_queue);
348 /* Quit when there are no more completions */
349 if (!msg)
350 break;
352 if (msg->msg == CURLMSG_DONE) {
353 CURLState *state = NULL;
354 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
355 (char **)&state);
357 /* ACBs for successful messages get completed in curl_read_cb */
358 if (msg->data.result != CURLE_OK) {
359 int i;
360 static int errcount = 100;
362 /* Don't lose the original error message from curl, since
363 * it contains extra data.
365 if (errcount > 0) {
366 error_report("curl: %s", state->errmsg);
367 if (--errcount == 0) {
368 error_report("curl: further errors suppressed");
372 for (i = 0; i < CURL_NUM_ACB; i++) {
373 CURLAIOCB *acb = state->acb[i];
375 if (acb == NULL) {
376 continue;
379 qemu_mutex_unlock(&s->mutex);
380 acb->common.cb(acb->common.opaque, -EIO);
381 qemu_mutex_lock(&s->mutex);
382 qemu_aio_unref(acb);
383 state->acb[i] = NULL;
387 curl_clean_state(state);
388 break;
393 /* Called with s->mutex held. */
394 static void curl_multi_do_locked(CURLState *s)
396 CURLSocket *socket, *next_socket;
397 int running;
398 int r;
400 if (!s->s->multi) {
401 return;
404 /* Need to use _SAFE because curl_multi_socket_action() may trigger
405 * curl_sock_cb() which might modify this list */
406 QLIST_FOREACH_SAFE(socket, &s->sockets, next, next_socket) {
407 do {
408 r = curl_multi_socket_action(s->s->multi, socket->fd, 0, &running);
409 } while (r == CURLM_CALL_MULTI_PERFORM);
413 static void curl_multi_do(void *arg)
415 CURLState *s = (CURLState *)arg;
417 qemu_mutex_lock(&s->s->mutex);
418 curl_multi_do_locked(s);
419 qemu_mutex_unlock(&s->s->mutex);
422 static void curl_multi_read(void *arg)
424 CURLState *s = (CURLState *)arg;
426 qemu_mutex_lock(&s->s->mutex);
427 curl_multi_do_locked(s);
428 curl_multi_check_completion(s->s);
429 qemu_mutex_unlock(&s->s->mutex);
432 static void curl_multi_timeout_do(void *arg)
434 #ifdef NEED_CURL_TIMER_CALLBACK
435 BDRVCURLState *s = (BDRVCURLState *)arg;
436 int running;
438 if (!s->multi) {
439 return;
442 qemu_mutex_lock(&s->mutex);
443 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
445 curl_multi_check_completion(s);
446 qemu_mutex_unlock(&s->mutex);
447 #else
448 abort();
449 #endif
452 static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)
454 CURLState *state = NULL;
455 int i, j;
457 do {
458 for (i=0; i<CURL_NUM_STATES; i++) {
459 for (j=0; j<CURL_NUM_ACB; j++)
460 if (s->states[i].acb[j])
461 continue;
462 if (s->states[i].in_use)
463 continue;
465 state = &s->states[i];
466 state->in_use = 1;
467 break;
469 if (!state) {
470 aio_poll(bdrv_get_aio_context(bs), true);
472 } while(!state);
474 if (!state->curl) {
475 state->curl = curl_easy_init();
476 if (!state->curl) {
477 return NULL;
479 curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
480 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
481 (long) s->sslverify);
482 if (s->cookie) {
483 curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
485 curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
486 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
487 (void *)curl_read_cb);
488 curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
489 curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
490 curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
491 curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
492 curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
493 curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
494 curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
496 if (s->username) {
497 curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username);
499 if (s->password) {
500 curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password);
502 if (s->proxyusername) {
503 curl_easy_setopt(state->curl,
504 CURLOPT_PROXYUSERNAME, s->proxyusername);
506 if (s->proxypassword) {
507 curl_easy_setopt(state->curl,
508 CURLOPT_PROXYPASSWORD, s->proxypassword);
511 /* Restrict supported protocols to avoid security issues in the more
512 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
513 * CVE-2013-0249.
515 * Restricting protocols is only supported from 7.19.4 upwards.
517 #if LIBCURL_VERSION_NUM >= 0x071304
518 curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
519 curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
520 #endif
522 #ifdef DEBUG_VERBOSE
523 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
524 #endif
527 QLIST_INIT(&state->sockets);
528 state->s = s;
530 return state;
533 static void curl_clean_state(CURLState *s)
535 if (s->s->multi)
536 curl_multi_remove_handle(s->s->multi, s->curl);
538 while (!QLIST_EMPTY(&s->sockets)) {
539 CURLSocket *socket = QLIST_FIRST(&s->sockets);
541 QLIST_REMOVE(socket, next);
542 g_free(socket);
545 s->in_use = 0;
548 static void curl_parse_filename(const char *filename, QDict *options,
549 Error **errp)
551 qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename));
554 static void curl_detach_aio_context(BlockDriverState *bs)
556 BDRVCURLState *s = bs->opaque;
557 int i;
559 for (i = 0; i < CURL_NUM_STATES; i++) {
560 if (s->states[i].in_use) {
561 curl_clean_state(&s->states[i]);
563 if (s->states[i].curl) {
564 curl_easy_cleanup(s->states[i].curl);
565 s->states[i].curl = NULL;
567 g_free(s->states[i].orig_buf);
568 s->states[i].orig_buf = NULL;
570 if (s->multi) {
571 curl_multi_cleanup(s->multi);
572 s->multi = NULL;
575 timer_del(&s->timer);
578 static void curl_attach_aio_context(BlockDriverState *bs,
579 AioContext *new_context)
581 BDRVCURLState *s = bs->opaque;
583 aio_timer_init(new_context, &s->timer,
584 QEMU_CLOCK_REALTIME, SCALE_NS,
585 curl_multi_timeout_do, s);
587 assert(!s->multi);
588 s->multi = curl_multi_init();
589 s->aio_context = new_context;
590 curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
591 #ifdef NEED_CURL_TIMER_CALLBACK
592 curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
593 curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
594 #endif
597 static QemuOptsList runtime_opts = {
598 .name = "curl",
599 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
600 .desc = {
602 .name = CURL_BLOCK_OPT_URL,
603 .type = QEMU_OPT_STRING,
604 .help = "URL to open",
607 .name = CURL_BLOCK_OPT_READAHEAD,
608 .type = QEMU_OPT_SIZE,
609 .help = "Readahead size",
612 .name = CURL_BLOCK_OPT_SSLVERIFY,
613 .type = QEMU_OPT_BOOL,
614 .help = "Verify SSL certificate"
617 .name = CURL_BLOCK_OPT_TIMEOUT,
618 .type = QEMU_OPT_NUMBER,
619 .help = "Curl timeout"
622 .name = CURL_BLOCK_OPT_COOKIE,
623 .type = QEMU_OPT_STRING,
624 .help = "Pass the cookie or list of cookies with each request"
627 .name = CURL_BLOCK_OPT_USERNAME,
628 .type = QEMU_OPT_STRING,
629 .help = "Username for HTTP auth"
632 .name = CURL_BLOCK_OPT_PASSWORD_SECRET,
633 .type = QEMU_OPT_STRING,
634 .help = "ID of secret used as password for HTTP auth",
637 .name = CURL_BLOCK_OPT_PROXY_USERNAME,
638 .type = QEMU_OPT_STRING,
639 .help = "Username for HTTP proxy auth"
642 .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
643 .type = QEMU_OPT_STRING,
644 .help = "ID of secret used as password for HTTP proxy auth",
646 { /* end of list */ }
651 static int curl_open(BlockDriverState *bs, QDict *options, int flags,
652 Error **errp)
654 BDRVCURLState *s = bs->opaque;
655 CURLState *state = NULL;
656 QemuOpts *opts;
657 Error *local_err = NULL;
658 const char *file;
659 const char *cookie;
660 double d;
661 const char *secretid;
662 const char *protocol_delimiter;
664 static int inited = 0;
666 if (flags & BDRV_O_RDWR) {
667 error_setg(errp, "curl block device does not support writes");
668 return -EROFS;
671 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
672 qemu_opts_absorb_qdict(opts, options, &local_err);
673 if (local_err) {
674 error_propagate(errp, local_err);
675 goto out_noclean;
678 s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
679 READ_AHEAD_DEFAULT);
680 if ((s->readahead_size & 0x1ff) != 0) {
681 error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
682 s->readahead_size);
683 goto out_noclean;
686 s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
687 CURL_TIMEOUT_DEFAULT);
688 if (s->timeout > CURL_TIMEOUT_MAX) {
689 error_setg(errp, "timeout parameter is too large or negative");
690 goto out_noclean;
693 s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
695 cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
696 s->cookie = g_strdup(cookie);
698 file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
699 if (file == NULL) {
700 error_setg(errp, "curl block driver requires an 'url' option");
701 goto out_noclean;
704 if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) ||
705 !strstart(protocol_delimiter, "://", NULL))
707 error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not "
708 "start with '%s://')", bs->drv->protocol_name, file,
709 bs->drv->protocol_name);
710 goto out_noclean;
713 s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME));
714 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET);
716 if (secretid) {
717 s->password = qcrypto_secret_lookup_as_utf8(secretid, errp);
718 if (!s->password) {
719 goto out_noclean;
723 s->proxyusername = g_strdup(
724 qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME));
725 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET);
726 if (secretid) {
727 s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp);
728 if (!s->proxypassword) {
729 goto out_noclean;
733 if (!inited) {
734 curl_global_init(CURL_GLOBAL_ALL);
735 inited = 1;
738 DPRINTF("CURL: Opening %s\n", file);
739 s->aio_context = bdrv_get_aio_context(bs);
740 s->url = g_strdup(file);
741 state = curl_init_state(bs, s);
742 if (!state)
743 goto out_noclean;
745 // Get file size
747 s->accept_range = false;
748 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
749 curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
750 curl_header_cb);
751 curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
752 if (curl_easy_perform(state->curl))
753 goto out;
754 if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
755 goto out;
757 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
758 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
759 * known and zero if it is realy zero-length file. */
760 #if LIBCURL_VERSION_NUM >= 0x071304
761 if (d < 0) {
762 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
763 "Server didn't report file size.");
764 goto out;
766 #else
767 if (d <= 0) {
768 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
769 "Unknown file size or zero-length file.");
770 goto out;
772 #endif
774 s->len = (size_t)d;
776 if ((!strncasecmp(s->url, "http://", strlen("http://"))
777 || !strncasecmp(s->url, "https://", strlen("https://")))
778 && !s->accept_range) {
779 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
780 "Server does not support 'range' (byte ranges).");
781 goto out;
783 DPRINTF("CURL: Size = %zd\n", s->len);
785 curl_clean_state(state);
786 curl_easy_cleanup(state->curl);
787 state->curl = NULL;
789 qemu_mutex_init(&s->mutex);
790 curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
792 qemu_opts_del(opts);
793 return 0;
795 out:
796 error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
797 curl_easy_cleanup(state->curl);
798 state->curl = NULL;
799 out_noclean:
800 g_free(s->cookie);
801 g_free(s->url);
802 qemu_opts_del(opts);
803 return -EINVAL;
806 static const AIOCBInfo curl_aiocb_info = {
807 .aiocb_size = sizeof(CURLAIOCB),
811 static void curl_readv_bh_cb(void *p)
813 CURLState *state;
814 int running;
815 int ret = -EINPROGRESS;
817 CURLAIOCB *acb = p;
818 BlockDriverState *bs = acb->common.bs;
819 BDRVCURLState *s = bs->opaque;
821 size_t start = acb->sector_num * BDRV_SECTOR_SIZE;
822 size_t end;
824 qemu_mutex_lock(&s->mutex);
826 // In case we have the requested data already (e.g. read-ahead),
827 // we can just call the callback and be done.
828 switch (curl_find_buf(s, start, acb->nb_sectors * BDRV_SECTOR_SIZE, acb)) {
829 case FIND_RET_OK:
830 qemu_aio_unref(acb);
831 // fall through
832 case FIND_RET_WAIT:
833 goto out;
834 default:
835 break;
838 // No cache found, so let's start a new request
839 state = curl_init_state(acb->common.bs, s);
840 if (!state) {
841 ret = -EIO;
842 goto out;
845 acb->start = 0;
846 acb->end = MIN(acb->nb_sectors * BDRV_SECTOR_SIZE, s->len - start);
848 state->buf_off = 0;
849 g_free(state->orig_buf);
850 state->buf_start = start;
851 state->buf_len = MIN(acb->end + s->readahead_size, s->len - start);
852 end = start + state->buf_len - 1;
853 state->orig_buf = g_try_malloc(state->buf_len);
854 if (state->buf_len && state->orig_buf == NULL) {
855 curl_clean_state(state);
856 ret = -ENOMEM;
857 goto out;
859 state->acb[0] = acb;
861 snprintf(state->range, 127, "%zd-%zd", start, end);
862 DPRINTF("CURL (AIO): Reading %llu at %zd (%s)\n",
863 (acb->nb_sectors * BDRV_SECTOR_SIZE), start, state->range);
864 curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
866 curl_multi_add_handle(s->multi, state->curl);
868 /* Tell curl it needs to kick things off */
869 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
871 out:
872 qemu_mutex_unlock(&s->mutex);
873 if (ret != -EINPROGRESS) {
874 acb->common.cb(acb->common.opaque, ret);
875 qemu_aio_unref(acb);
879 static BlockAIOCB *curl_aio_readv(BlockDriverState *bs,
880 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
881 BlockCompletionFunc *cb, void *opaque)
883 CURLAIOCB *acb;
885 acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
887 acb->qiov = qiov;
888 acb->sector_num = sector_num;
889 acb->nb_sectors = nb_sectors;
891 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
892 return &acb->common;
895 static void curl_close(BlockDriverState *bs)
897 BDRVCURLState *s = bs->opaque;
899 DPRINTF("CURL: Close\n");
900 curl_detach_aio_context(bs);
901 qemu_mutex_destroy(&s->mutex);
903 g_free(s->cookie);
904 g_free(s->url);
907 static int64_t curl_getlength(BlockDriverState *bs)
909 BDRVCURLState *s = bs->opaque;
910 return s->len;
913 static BlockDriver bdrv_http = {
914 .format_name = "http",
915 .protocol_name = "http",
917 .instance_size = sizeof(BDRVCURLState),
918 .bdrv_parse_filename = curl_parse_filename,
919 .bdrv_file_open = curl_open,
920 .bdrv_close = curl_close,
921 .bdrv_getlength = curl_getlength,
923 .bdrv_aio_readv = curl_aio_readv,
925 .bdrv_detach_aio_context = curl_detach_aio_context,
926 .bdrv_attach_aio_context = curl_attach_aio_context,
929 static BlockDriver bdrv_https = {
930 .format_name = "https",
931 .protocol_name = "https",
933 .instance_size = sizeof(BDRVCURLState),
934 .bdrv_parse_filename = curl_parse_filename,
935 .bdrv_file_open = curl_open,
936 .bdrv_close = curl_close,
937 .bdrv_getlength = curl_getlength,
939 .bdrv_aio_readv = curl_aio_readv,
941 .bdrv_detach_aio_context = curl_detach_aio_context,
942 .bdrv_attach_aio_context = curl_attach_aio_context,
945 static BlockDriver bdrv_ftp = {
946 .format_name = "ftp",
947 .protocol_name = "ftp",
949 .instance_size = sizeof(BDRVCURLState),
950 .bdrv_parse_filename = curl_parse_filename,
951 .bdrv_file_open = curl_open,
952 .bdrv_close = curl_close,
953 .bdrv_getlength = curl_getlength,
955 .bdrv_aio_readv = curl_aio_readv,
957 .bdrv_detach_aio_context = curl_detach_aio_context,
958 .bdrv_attach_aio_context = curl_attach_aio_context,
961 static BlockDriver bdrv_ftps = {
962 .format_name = "ftps",
963 .protocol_name = "ftps",
965 .instance_size = sizeof(BDRVCURLState),
966 .bdrv_parse_filename = curl_parse_filename,
967 .bdrv_file_open = curl_open,
968 .bdrv_close = curl_close,
969 .bdrv_getlength = curl_getlength,
971 .bdrv_aio_readv = curl_aio_readv,
973 .bdrv_detach_aio_context = curl_detach_aio_context,
974 .bdrv_attach_aio_context = curl_attach_aio_context,
977 static void curl_block_init(void)
979 bdrv_register(&bdrv_http);
980 bdrv_register(&bdrv_https);
981 bdrv_register(&bdrv_ftp);
982 bdrv_register(&bdrv_ftps);
985 block_init(curl_block_init);