xref: /openbmc/qemu/block/curl.c (revision 461a2753)
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_release(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_release(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 void curl_aio_cancel(BlockDriverAIOCB *blockacb)
617 {
618     // Do we have to implement canceling? Seems to work without...
619 }
620 
621 static const AIOCBInfo curl_aiocb_info = {
622     .aiocb_size         = sizeof(CURLAIOCB),
623     .cancel             = curl_aio_cancel,
624 };
625 
626 
627 static void curl_readv_bh_cb(void *p)
628 {
629     CURLState *state;
630     int running;
631 
632     CURLAIOCB *acb = p;
633     BDRVCURLState *s = acb->common.bs->opaque;
634 
635     qemu_bh_delete(acb->bh);
636     acb->bh = NULL;
637 
638     size_t start = acb->sector_num * SECTOR_SIZE;
639     size_t end;
640 
641     // In case we have the requested data already (e.g. read-ahead),
642     // we can just call the callback and be done.
643     switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
644         case FIND_RET_OK:
645             qemu_aio_release(acb);
646             // fall through
647         case FIND_RET_WAIT:
648             return;
649         default:
650             break;
651     }
652 
653     // No cache found, so let's start a new request
654     state = curl_init_state(acb->common.bs, s);
655     if (!state) {
656         acb->common.cb(acb->common.opaque, -EIO);
657         qemu_aio_release(acb);
658         return;
659     }
660 
661     acb->start = 0;
662     acb->end = (acb->nb_sectors * SECTOR_SIZE);
663 
664     state->buf_off = 0;
665     g_free(state->orig_buf);
666     state->buf_start = start;
667     state->buf_len = acb->end + s->readahead_size;
668     end = MIN(start + state->buf_len, s->len) - 1;
669     state->orig_buf = g_try_malloc(state->buf_len);
670     if (state->buf_len && state->orig_buf == NULL) {
671         curl_clean_state(state);
672         acb->common.cb(acb->common.opaque, -ENOMEM);
673         qemu_aio_release(acb);
674         return;
675     }
676     state->acb[0] = acb;
677 
678     snprintf(state->range, 127, "%zd-%zd", start, end);
679     DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
680             (acb->nb_sectors * SECTOR_SIZE), start, state->range);
681     curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
682 
683     curl_multi_add_handle(s->multi, state->curl);
684 
685     /* Tell curl it needs to kick things off */
686     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
687 }
688 
689 static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
690         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
691         BlockDriverCompletionFunc *cb, void *opaque)
692 {
693     CURLAIOCB *acb;
694 
695     acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
696 
697     acb->qiov = qiov;
698     acb->sector_num = sector_num;
699     acb->nb_sectors = nb_sectors;
700 
701     acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
702     qemu_bh_schedule(acb->bh);
703     return &acb->common;
704 }
705 
706 static void curl_close(BlockDriverState *bs)
707 {
708     BDRVCURLState *s = bs->opaque;
709 
710     DPRINTF("CURL: Close\n");
711     curl_detach_aio_context(bs);
712 
713     g_free(s->cookie);
714     g_free(s->url);
715 }
716 
717 static int64_t curl_getlength(BlockDriverState *bs)
718 {
719     BDRVCURLState *s = bs->opaque;
720     return s->len;
721 }
722 
723 static BlockDriver bdrv_http = {
724     .format_name                = "http",
725     .protocol_name              = "http",
726 
727     .instance_size              = sizeof(BDRVCURLState),
728     .bdrv_parse_filename        = curl_parse_filename,
729     .bdrv_file_open             = curl_open,
730     .bdrv_close                 = curl_close,
731     .bdrv_getlength             = curl_getlength,
732 
733     .bdrv_aio_readv             = curl_aio_readv,
734 
735     .bdrv_detach_aio_context    = curl_detach_aio_context,
736     .bdrv_attach_aio_context    = curl_attach_aio_context,
737 };
738 
739 static BlockDriver bdrv_https = {
740     .format_name                = "https",
741     .protocol_name              = "https",
742 
743     .instance_size              = sizeof(BDRVCURLState),
744     .bdrv_parse_filename        = curl_parse_filename,
745     .bdrv_file_open             = curl_open,
746     .bdrv_close                 = curl_close,
747     .bdrv_getlength             = curl_getlength,
748 
749     .bdrv_aio_readv             = curl_aio_readv,
750 
751     .bdrv_detach_aio_context    = curl_detach_aio_context,
752     .bdrv_attach_aio_context    = curl_attach_aio_context,
753 };
754 
755 static BlockDriver bdrv_ftp = {
756     .format_name                = "ftp",
757     .protocol_name              = "ftp",
758 
759     .instance_size              = sizeof(BDRVCURLState),
760     .bdrv_parse_filename        = curl_parse_filename,
761     .bdrv_file_open             = curl_open,
762     .bdrv_close                 = curl_close,
763     .bdrv_getlength             = curl_getlength,
764 
765     .bdrv_aio_readv             = curl_aio_readv,
766 
767     .bdrv_detach_aio_context    = curl_detach_aio_context,
768     .bdrv_attach_aio_context    = curl_attach_aio_context,
769 };
770 
771 static BlockDriver bdrv_ftps = {
772     .format_name                = "ftps",
773     .protocol_name              = "ftps",
774 
775     .instance_size              = sizeof(BDRVCURLState),
776     .bdrv_parse_filename        = curl_parse_filename,
777     .bdrv_file_open             = curl_open,
778     .bdrv_close                 = curl_close,
779     .bdrv_getlength             = curl_getlength,
780 
781     .bdrv_aio_readv             = curl_aio_readv,
782 
783     .bdrv_detach_aio_context    = curl_detach_aio_context,
784     .bdrv_attach_aio_context    = curl_attach_aio_context,
785 };
786 
787 static BlockDriver bdrv_tftp = {
788     .format_name                = "tftp",
789     .protocol_name              = "tftp",
790 
791     .instance_size              = sizeof(BDRVCURLState),
792     .bdrv_parse_filename        = curl_parse_filename,
793     .bdrv_file_open             = curl_open,
794     .bdrv_close                 = curl_close,
795     .bdrv_getlength             = curl_getlength,
796 
797     .bdrv_aio_readv             = curl_aio_readv,
798 
799     .bdrv_detach_aio_context    = curl_detach_aio_context,
800     .bdrv_attach_aio_context    = curl_attach_aio_context,
801 };
802 
803 static void curl_block_init(void)
804 {
805     bdrv_register(&bdrv_http);
806     bdrv_register(&bdrv_https);
807     bdrv_register(&bdrv_ftp);
808     bdrv_register(&bdrv_ftps);
809     bdrv_register(&bdrv_tftp);
810 }
811 
812 block_init(curl_block_init);
813