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
24 #include "qemu-common.h"
25 #include "block_int.h"
26 #include <curl/curl.h>
29 // #define DEBUG_VERBOSE
32 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
34 #define DPRINTF(fmt, ...) do { } while (0)
37 #define CURL_NUM_STATES 8
38 #define CURL_NUM_ACB 8
39 #define SECTOR_SIZE 512
40 #define READ_AHEAD_SIZE (256 * 1024)
42 #define FIND_RET_NONE 0
44 #define FIND_RET_WAIT 2
48 typedef struct CURLAIOCB
{
49 BlockDriverAIOCB common
;
60 typedef struct CURLState
62 struct BDRVCURLState
*s
;
63 CURLAIOCB
*acb
[CURL_NUM_ACB
];
70 char errmsg
[CURL_ERROR_SIZE
];
74 typedef struct BDRVCURLState
{
77 CURLState states
[CURL_NUM_STATES
];
79 size_t readahead_size
;
82 static void curl_clean_state(CURLState
*s
);
83 static void curl_multi_do(void *arg
);
84 static int curl_aio_flush(void *opaque
);
86 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
89 DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action
, fd
);
92 qemu_aio_set_fd_handler(fd
, curl_multi_do
, NULL
, curl_aio_flush
, s
);
95 qemu_aio_set_fd_handler(fd
, NULL
, curl_multi_do
, curl_aio_flush
, s
);
98 qemu_aio_set_fd_handler(fd
, curl_multi_do
, curl_multi_do
,
101 case CURL_POLL_REMOVE
:
102 qemu_aio_set_fd_handler(fd
, NULL
, NULL
, NULL
, NULL
);
109 static size_t curl_size_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
111 CURLState
*s
= ((CURLState
*)opaque
);
112 size_t realsize
= size
* nmemb
;
115 if(sscanf(ptr
, "Content-Length: %zd", &fsize
) == 1) {
122 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
124 CURLState
*s
= ((CURLState
*)opaque
);
125 size_t realsize
= size
* nmemb
;
128 DPRINTF("CURL: Just reading %zd bytes\n", realsize
);
130 if (!s
|| !s
->orig_buf
)
133 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
134 s
->buf_off
+= realsize
;
136 for(i
=0; i
<CURL_NUM_ACB
; i
++) {
137 CURLAIOCB
*acb
= s
->acb
[i
];
142 if ((s
->buf_off
>= acb
->end
)) {
143 qemu_iovec_from_buf(acb
->qiov
, 0, s
->orig_buf
+ acb
->start
,
144 acb
->end
- acb
->start
);
145 acb
->common
.cb(acb
->common
.opaque
, 0);
146 qemu_aio_release(acb
);
155 static int curl_find_buf(BDRVCURLState
*s
, size_t start
, size_t len
,
159 size_t end
= start
+ len
;
161 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
162 CURLState
*state
= &s
->states
[i
];
163 size_t buf_end
= (state
->buf_start
+ state
->buf_off
);
164 size_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
166 if (!state
->orig_buf
)
171 // Does the existing buffer cover our section?
172 if ((start
>= state
->buf_start
) &&
173 (start
<= buf_end
) &&
174 (end
>= state
->buf_start
) &&
177 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
179 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, len
);
180 acb
->common
.cb(acb
->common
.opaque
, 0);
185 // Wait for unfinished chunks
186 if ((start
>= state
->buf_start
) &&
187 (start
<= buf_fend
) &&
188 (end
>= state
->buf_start
) &&
193 acb
->start
= start
- state
->buf_start
;
194 acb
->end
= acb
->start
+ len
;
196 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
197 if (!state
->acb
[j
]) {
199 return FIND_RET_WAIT
;
205 return FIND_RET_NONE
;
208 static void curl_multi_do(void *arg
)
210 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
219 r
= curl_multi_socket_all(s
->multi
, &running
);
220 } while(r
== CURLM_CALL_MULTI_PERFORM
);
222 /* Try to find done transfers, so we can free the easy
226 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
230 if (msg
->msg
== CURLMSG_NONE
)
236 CURLState
*state
= NULL
;
237 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
, (char**)&state
);
239 /* ACBs for successful messages get completed in curl_read_cb */
240 if (msg
->data
.result
!= CURLE_OK
) {
242 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
243 CURLAIOCB
*acb
= state
->acb
[i
];
249 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
250 qemu_aio_release(acb
);
251 state
->acb
[i
] = NULL
;
255 curl_clean_state(state
);
262 } while(msgs_in_queue
);
265 static CURLState
*curl_init_state(BDRVCURLState
*s
)
267 CURLState
*state
= NULL
;
271 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
272 for (j
=0; j
<CURL_NUM_ACB
; j
++)
273 if (s
->states
[i
].acb
[j
])
275 if (s
->states
[i
].in_use
)
278 state
= &s
->states
[i
];
291 state
->curl
= curl_easy_init();
294 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
295 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, 5);
296 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
, (void *)curl_read_cb
);
297 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
298 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
299 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
300 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
301 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
302 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
303 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
306 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
316 static void curl_clean_state(CURLState
*s
)
319 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
323 static int curl_open(BlockDriverState
*bs
, const char *filename
, int flags
)
325 BDRVCURLState
*s
= bs
->opaque
;
326 CURLState
*state
= NULL
;
329 #define RA_OPTSTR ":readahead="
335 static int inited
= 0;
337 file
= g_strdup(filename
);
338 s
->readahead_size
= READ_AHEAD_SIZE
;
340 /* Parse a trailing ":readahead=#:" param, if present. */
341 ra
= file
+ strlen(file
) - 1;
343 if (parse_state
== 0) {
348 } else if (parse_state
== 1) {
349 if (*ra
> '9' || *ra
< '0') {
350 char *opt_start
= ra
- strlen(RA_OPTSTR
) + 1;
351 if (opt_start
> file
&&
352 strncmp(opt_start
, RA_OPTSTR
, strlen(RA_OPTSTR
)) == 0) {
354 ra
-= strlen(RA_OPTSTR
) - 1;
356 s
->readahead_size
= atoi(ra_val
);
366 if ((s
->readahead_size
& 0x1ff) != 0) {
367 fprintf(stderr
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n",
373 curl_global_init(CURL_GLOBAL_ALL
);
377 DPRINTF("CURL: Opening %s\n", file
);
379 state
= curl_init_state(s
);
385 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
386 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
, (void *)curl_size_cb
);
387 if (curl_easy_perform(state
->curl
))
389 curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
);
390 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
, (void *)curl_read_cb
);
391 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 0);
396 DPRINTF("CURL: Size = %zd\n", s
->len
);
398 curl_clean_state(state
);
399 curl_easy_cleanup(state
->curl
);
402 // Now we know the file exists and its size, so let's
403 // initialize the multi interface!
405 s
->multi
= curl_multi_init();
406 curl_multi_setopt( s
->multi
, CURLMOPT_SOCKETDATA
, s
);
407 curl_multi_setopt( s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
413 fprintf(stderr
, "CURL: Error opening file: %s\n", state
->errmsg
);
414 curl_easy_cleanup(state
->curl
);
421 static int curl_aio_flush(void *opaque
)
423 BDRVCURLState
*s
= opaque
;
426 for (i
=0; i
< CURL_NUM_STATES
; i
++) {
427 for(j
=0; j
< CURL_NUM_ACB
; j
++) {
428 if (s
->states
[i
].acb
[j
]) {
436 static void curl_aio_cancel(BlockDriverAIOCB
*blockacb
)
438 // Do we have to implement canceling? Seems to work without...
441 static const AIOCBInfo curl_aiocb_info
= {
442 .aiocb_size
= sizeof(CURLAIOCB
),
443 .cancel
= curl_aio_cancel
,
447 static void curl_readv_bh_cb(void *p
)
452 BDRVCURLState
*s
= acb
->common
.bs
->opaque
;
454 qemu_bh_delete(acb
->bh
);
457 size_t start
= acb
->sector_num
* SECTOR_SIZE
;
460 // In case we have the requested data already (e.g. read-ahead),
461 // we can just call the callback and be done.
462 switch (curl_find_buf(s
, start
, acb
->nb_sectors
* SECTOR_SIZE
, acb
)) {
464 qemu_aio_release(acb
);
472 // No cache found, so let's start a new request
473 state
= curl_init_state(s
);
475 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
476 qemu_aio_release(acb
);
481 acb
->end
= (acb
->nb_sectors
* SECTOR_SIZE
);
485 g_free(state
->orig_buf
);
486 state
->buf_start
= start
;
487 state
->buf_len
= acb
->end
+ s
->readahead_size
;
488 end
= MIN(start
+ state
->buf_len
, s
->len
) - 1;
489 state
->orig_buf
= g_malloc(state
->buf_len
);
492 snprintf(state
->range
, 127, "%zd-%zd", start
, end
);
493 DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
494 (acb
->nb_sectors
* SECTOR_SIZE
), start
, state
->range
);
495 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
497 curl_multi_add_handle(s
->multi
, state
->curl
);
502 static BlockDriverAIOCB
*curl_aio_readv(BlockDriverState
*bs
,
503 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
504 BlockDriverCompletionFunc
*cb
, void *opaque
)
508 acb
= qemu_aio_get(&curl_aiocb_info
, bs
, cb
, opaque
);
511 acb
->sector_num
= sector_num
;
512 acb
->nb_sectors
= nb_sectors
;
514 acb
->bh
= qemu_bh_new(curl_readv_bh_cb
, acb
);
517 DPRINTF("CURL: qemu_bh_new failed\n");
521 qemu_bh_schedule(acb
->bh
);
525 static void curl_close(BlockDriverState
*bs
)
527 BDRVCURLState
*s
= bs
->opaque
;
530 DPRINTF("CURL: Close\n");
531 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
532 if (s
->states
[i
].in_use
)
533 curl_clean_state(&s
->states
[i
]);
534 if (s
->states
[i
].curl
) {
535 curl_easy_cleanup(s
->states
[i
].curl
);
536 s
->states
[i
].curl
= NULL
;
538 if (s
->states
[i
].orig_buf
) {
539 g_free(s
->states
[i
].orig_buf
);
540 s
->states
[i
].orig_buf
= NULL
;
544 curl_multi_cleanup(s
->multi
);
548 static int64_t curl_getlength(BlockDriverState
*bs
)
550 BDRVCURLState
*s
= bs
->opaque
;
554 static BlockDriver bdrv_http
= {
555 .format_name
= "http",
556 .protocol_name
= "http",
558 .instance_size
= sizeof(BDRVCURLState
),
559 .bdrv_file_open
= curl_open
,
560 .bdrv_close
= curl_close
,
561 .bdrv_getlength
= curl_getlength
,
563 .bdrv_aio_readv
= curl_aio_readv
,
566 static BlockDriver bdrv_https
= {
567 .format_name
= "https",
568 .protocol_name
= "https",
570 .instance_size
= sizeof(BDRVCURLState
),
571 .bdrv_file_open
= curl_open
,
572 .bdrv_close
= curl_close
,
573 .bdrv_getlength
= curl_getlength
,
575 .bdrv_aio_readv
= curl_aio_readv
,
578 static BlockDriver bdrv_ftp
= {
579 .format_name
= "ftp",
580 .protocol_name
= "ftp",
582 .instance_size
= sizeof(BDRVCURLState
),
583 .bdrv_file_open
= curl_open
,
584 .bdrv_close
= curl_close
,
585 .bdrv_getlength
= curl_getlength
,
587 .bdrv_aio_readv
= curl_aio_readv
,
590 static BlockDriver bdrv_ftps
= {
591 .format_name
= "ftps",
592 .protocol_name
= "ftps",
594 .instance_size
= sizeof(BDRVCURLState
),
595 .bdrv_file_open
= curl_open
,
596 .bdrv_close
= curl_close
,
597 .bdrv_getlength
= curl_getlength
,
599 .bdrv_aio_readv
= curl_aio_readv
,
602 static BlockDriver bdrv_tftp
= {
603 .format_name
= "tftp",
604 .protocol_name
= "tftp",
606 .instance_size
= sizeof(BDRVCURLState
),
607 .bdrv_file_open
= curl_open
,
608 .bdrv_close
= curl_close
,
609 .bdrv_getlength
= curl_getlength
,
611 .bdrv_aio_readv
= curl_aio_readv
,
614 static void curl_block_init(void)
616 bdrv_register(&bdrv_http
);
617 bdrv_register(&bdrv_https
);
618 bdrv_register(&bdrv_ftp
);
619 bdrv_register(&bdrv_ftps
);
620 bdrv_register(&bdrv_tftp
);
623 block_init(curl_block_init
);