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