Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 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" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 25 | #include "block/block_int.h" |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 26 | #include <curl/curl.h> |
| 27 | |
| 28 | // #define DEBUG |
| 29 | // #define DEBUG_VERBOSE |
| 30 | |
| 31 | #ifdef DEBUG_CURL |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 32 | #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 33 | #else |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 34 | #define DPRINTF(fmt, ...) do { } while (0) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 35 | #endif |
| 36 | |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 37 | #if LIBCURL_VERSION_NUM >= 0x071000 |
| 38 | /* The multi interface timer callback was introduced in 7.16.0 */ |
| 39 | #define NEED_CURL_TIMER_CALLBACK |
| 40 | #endif |
| 41 | |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 42 | #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ |
| 43 | CURLPROTO_FTP | CURLPROTO_FTPS | \ |
| 44 | CURLPROTO_TFTP) |
| 45 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 46 | #define CURL_NUM_STATES 8 |
| 47 | #define CURL_NUM_ACB 8 |
| 48 | #define SECTOR_SIZE 512 |
| 49 | #define READ_AHEAD_SIZE (256 * 1024) |
| 50 | |
| 51 | #define FIND_RET_NONE 0 |
| 52 | #define FIND_RET_OK 1 |
| 53 | #define FIND_RET_WAIT 2 |
| 54 | |
| 55 | struct BDRVCURLState; |
| 56 | |
| 57 | typedef struct CURLAIOCB { |
| 58 | BlockDriverAIOCB common; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 59 | QEMUBH *bh; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 60 | QEMUIOVector *qiov; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 61 | |
| 62 | int64_t sector_num; |
| 63 | int nb_sectors; |
| 64 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 65 | size_t start; |
| 66 | size_t end; |
| 67 | } CURLAIOCB; |
| 68 | |
| 69 | typedef struct CURLState |
| 70 | { |
| 71 | struct BDRVCURLState *s; |
| 72 | CURLAIOCB *acb[CURL_NUM_ACB]; |
| 73 | CURL *curl; |
| 74 | char *orig_buf; |
| 75 | size_t buf_start; |
| 76 | size_t buf_off; |
| 77 | size_t buf_len; |
| 78 | char range[128]; |
| 79 | char errmsg[CURL_ERROR_SIZE]; |
| 80 | char in_use; |
| 81 | } CURLState; |
| 82 | |
| 83 | typedef struct BDRVCURLState { |
| 84 | CURLM *multi; |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 85 | QEMUTimer timer; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 86 | size_t len; |
| 87 | CURLState states[CURL_NUM_STATES]; |
| 88 | char *url; |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 89 | size_t readahead_size; |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 90 | bool accept_range; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 91 | } BDRVCURLState; |
| 92 | |
| 93 | static void curl_clean_state(CURLState *s); |
| 94 | static void curl_multi_do(void *arg); |
| 95 | |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 96 | #ifdef NEED_CURL_TIMER_CALLBACK |
| 97 | static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) |
| 98 | { |
| 99 | BDRVCURLState *s = opaque; |
| 100 | |
| 101 | DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms); |
| 102 | if (timeout_ms == -1) { |
| 103 | timer_del(&s->timer); |
| 104 | } else { |
| 105 | int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; |
| 106 | timer_mod(&s->timer, |
| 107 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | #endif |
| 112 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 113 | static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, |
| 114 | void *s, void *sp) |
| 115 | { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 116 | DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 117 | switch (action) { |
| 118 | case CURL_POLL_IN: |
Stefan Hajnoczi | f2e5dca | 2013-04-11 17:26:25 +0200 | [diff] [blame] | 119 | qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, s); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 120 | break; |
| 121 | case CURL_POLL_OUT: |
Stefan Hajnoczi | f2e5dca | 2013-04-11 17:26:25 +0200 | [diff] [blame] | 122 | qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, s); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 123 | break; |
| 124 | case CURL_POLL_INOUT: |
Stefan Hajnoczi | f2e5dca | 2013-04-11 17:26:25 +0200 | [diff] [blame] | 125 | qemu_aio_set_fd_handler(fd, curl_multi_do, curl_multi_do, s); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 126 | break; |
| 127 | case CURL_POLL_REMOVE: |
Stefan Hajnoczi | f2e5dca | 2013-04-11 17:26:25 +0200 | [diff] [blame] | 128 | qemu_aio_set_fd_handler(fd, NULL, NULL, NULL); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 129 | break; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 135 | static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 136 | { |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 137 | BDRVCURLState *s = opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 138 | size_t realsize = size * nmemb; |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 139 | const char *accept_line = "Accept-Ranges: bytes"; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 140 | |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 141 | if (realsize >= strlen(accept_line) |
| 142 | && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) { |
| 143 | s->accept_range = true; |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 144 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 145 | |
| 146 | return realsize; |
| 147 | } |
| 148 | |
| 149 | static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
| 150 | { |
| 151 | CURLState *s = ((CURLState*)opaque); |
| 152 | size_t realsize = size * nmemb; |
| 153 | int i; |
| 154 | |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 155 | DPRINTF("CURL: Just reading %zd bytes\n", realsize); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 156 | |
| 157 | if (!s || !s->orig_buf) |
| 158 | goto read_end; |
| 159 | |
Fam Zheng | 6d4b9e5 | 2014-03-26 13:05:40 +0100 | [diff] [blame] | 160 | if (s->buf_off >= s->buf_len) { |
| 161 | /* buffer full, read nothing */ |
| 162 | return 0; |
| 163 | } |
| 164 | realsize = MIN(realsize, s->buf_len - s->buf_off); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 165 | memcpy(s->orig_buf + s->buf_off, ptr, realsize); |
| 166 | s->buf_off += realsize; |
| 167 | |
| 168 | for(i=0; i<CURL_NUM_ACB; i++) { |
| 169 | CURLAIOCB *acb = s->acb[i]; |
| 170 | |
| 171 | if (!acb) |
| 172 | continue; |
| 173 | |
| 174 | if ((s->buf_off >= acb->end)) { |
Michael Tokarev | 0339614 | 2012-06-07 20:17:55 +0400 | [diff] [blame] | 175 | qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start, |
| 176 | acb->end - acb->start); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 177 | acb->common.cb(acb->common.opaque, 0); |
| 178 | qemu_aio_release(acb); |
| 179 | s->acb[i] = NULL; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | read_end: |
| 184 | return realsize; |
| 185 | } |
| 186 | |
| 187 | static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len, |
| 188 | CURLAIOCB *acb) |
| 189 | { |
| 190 | int i; |
| 191 | size_t end = start + len; |
| 192 | |
| 193 | for (i=0; i<CURL_NUM_STATES; i++) { |
| 194 | CURLState *state = &s->states[i]; |
| 195 | size_t buf_end = (state->buf_start + state->buf_off); |
| 196 | size_t buf_fend = (state->buf_start + state->buf_len); |
| 197 | |
| 198 | if (!state->orig_buf) |
| 199 | continue; |
| 200 | if (!state->buf_off) |
| 201 | continue; |
| 202 | |
| 203 | // Does the existing buffer cover our section? |
| 204 | if ((start >= state->buf_start) && |
| 205 | (start <= buf_end) && |
| 206 | (end >= state->buf_start) && |
| 207 | (end <= buf_end)) |
| 208 | { |
| 209 | char *buf = state->orig_buf + (start - state->buf_start); |
| 210 | |
Michael Tokarev | 0339614 | 2012-06-07 20:17:55 +0400 | [diff] [blame] | 211 | qemu_iovec_from_buf(acb->qiov, 0, buf, len); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 212 | acb->common.cb(acb->common.opaque, 0); |
| 213 | |
| 214 | return FIND_RET_OK; |
| 215 | } |
| 216 | |
| 217 | // Wait for unfinished chunks |
| 218 | if ((start >= state->buf_start) && |
| 219 | (start <= buf_fend) && |
| 220 | (end >= state->buf_start) && |
| 221 | (end <= buf_fend)) |
| 222 | { |
| 223 | int j; |
| 224 | |
| 225 | acb->start = start - state->buf_start; |
| 226 | acb->end = acb->start + len; |
| 227 | |
| 228 | for (j=0; j<CURL_NUM_ACB; j++) { |
| 229 | if (!state->acb[j]) { |
| 230 | state->acb[j] = acb; |
| 231 | return FIND_RET_WAIT; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return FIND_RET_NONE; |
| 238 | } |
| 239 | |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 240 | static void curl_multi_read(BDRVCURLState *s) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 241 | { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 242 | int msgs_in_queue; |
| 243 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 244 | /* Try to find done transfers, so we can free the easy |
| 245 | * handle again. */ |
| 246 | do { |
| 247 | CURLMsg *msg; |
| 248 | msg = curl_multi_info_read(s->multi, &msgs_in_queue); |
| 249 | |
| 250 | if (!msg) |
| 251 | break; |
| 252 | if (msg->msg == CURLMSG_NONE) |
| 253 | break; |
| 254 | |
| 255 | switch (msg->msg) { |
| 256 | case CURLMSG_DONE: |
| 257 | { |
| 258 | CURLState *state = NULL; |
| 259 | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state); |
Nicholas Thomas | f785a5a | 2011-08-15 10:00:34 +0100 | [diff] [blame] | 260 | |
| 261 | /* ACBs for successful messages get completed in curl_read_cb */ |
| 262 | if (msg->data.result != CURLE_OK) { |
| 263 | int i; |
| 264 | for (i = 0; i < CURL_NUM_ACB; i++) { |
| 265 | CURLAIOCB *acb = state->acb[i]; |
| 266 | |
| 267 | if (acb == NULL) { |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | acb->common.cb(acb->common.opaque, -EIO); |
| 272 | qemu_aio_release(acb); |
| 273 | state->acb[i] = NULL; |
| 274 | } |
| 275 | } |
| 276 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 277 | curl_clean_state(state); |
| 278 | break; |
| 279 | } |
| 280 | default: |
| 281 | msgs_in_queue = 0; |
| 282 | break; |
| 283 | } |
| 284 | } while(msgs_in_queue); |
| 285 | } |
| 286 | |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 287 | static void curl_multi_do(void *arg) |
| 288 | { |
| 289 | BDRVCURLState *s = (BDRVCURLState *)arg; |
| 290 | int running; |
| 291 | int r; |
| 292 | |
| 293 | if (!s->multi) { |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | do { |
| 298 | r = curl_multi_socket_all(s->multi, &running); |
| 299 | } while(r == CURLM_CALL_MULTI_PERFORM); |
| 300 | |
| 301 | curl_multi_read(s); |
| 302 | } |
| 303 | |
| 304 | static void curl_multi_timeout_do(void *arg) |
| 305 | { |
| 306 | #ifdef NEED_CURL_TIMER_CALLBACK |
| 307 | BDRVCURLState *s = (BDRVCURLState *)arg; |
| 308 | int running; |
| 309 | |
| 310 | if (!s->multi) { |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); |
| 315 | |
| 316 | curl_multi_read(s); |
| 317 | #else |
| 318 | abort(); |
| 319 | #endif |
| 320 | } |
| 321 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 322 | static CURLState *curl_init_state(BDRVCURLState *s) |
| 323 | { |
| 324 | CURLState *state = NULL; |
| 325 | int i, j; |
| 326 | |
| 327 | do { |
| 328 | for (i=0; i<CURL_NUM_STATES; i++) { |
| 329 | for (j=0; j<CURL_NUM_ACB; j++) |
| 330 | if (s->states[i].acb[j]) |
| 331 | continue; |
| 332 | if (s->states[i].in_use) |
| 333 | continue; |
| 334 | |
| 335 | state = &s->states[i]; |
| 336 | state->in_use = 1; |
| 337 | break; |
| 338 | } |
| 339 | if (!state) { |
Stefan Weil | fb7c8e8 | 2012-03-31 17:34:41 +0200 | [diff] [blame] | 340 | g_usleep(100); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 341 | curl_multi_do(s); |
| 342 | } |
| 343 | } while(!state); |
| 344 | |
| 345 | if (state->curl) |
| 346 | goto has_curl; |
| 347 | |
| 348 | state->curl = curl_easy_init(); |
| 349 | if (!state->curl) |
| 350 | return NULL; |
| 351 | curl_easy_setopt(state->curl, CURLOPT_URL, s->url); |
| 352 | curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5); |
Blue Swirl | df3cee1 | 2009-08-01 10:13:44 +0000 | [diff] [blame] | 353 | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 354 | curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); |
| 355 | curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); |
| 356 | curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); |
| 357 | curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); |
| 358 | curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); |
| 359 | curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); |
Nicholas Thomas | f785a5a | 2011-08-15 10:00:34 +0100 | [diff] [blame] | 360 | curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); |
| 361 | |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 362 | /* Restrict supported protocols to avoid security issues in the more |
| 363 | * obscure protocols. For example, do not allow POP3/SMTP/IMAP see |
| 364 | * CVE-2013-0249. |
Stefan Hajnoczi | 8a8f584 | 2013-02-13 09:25:34 +0100 | [diff] [blame] | 365 | * |
| 366 | * Restricting protocols is only supported from 7.19.4 upwards. |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 367 | */ |
Stefan Hajnoczi | 8a8f584 | 2013-02-13 09:25:34 +0100 | [diff] [blame] | 368 | #if LIBCURL_VERSION_NUM >= 0x071304 |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 369 | curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); |
| 370 | curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); |
Stefan Hajnoczi | 8a8f584 | 2013-02-13 09:25:34 +0100 | [diff] [blame] | 371 | #endif |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 372 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 373 | #ifdef DEBUG_VERBOSE |
| 374 | curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); |
| 375 | #endif |
| 376 | |
| 377 | has_curl: |
| 378 | |
| 379 | state->s = s; |
| 380 | |
| 381 | return state; |
| 382 | } |
| 383 | |
| 384 | static void curl_clean_state(CURLState *s) |
| 385 | { |
| 386 | if (s->s->multi) |
| 387 | curl_multi_remove_handle(s->s->multi, s->curl); |
| 388 | s->in_use = 0; |
| 389 | } |
| 390 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 391 | static void curl_parse_filename(const char *filename, QDict *options, |
| 392 | Error **errp) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 393 | { |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 394 | |
| 395 | #define RA_OPTSTR ":readahead=" |
| 396 | char *file; |
| 397 | char *ra; |
| 398 | const char *ra_val; |
| 399 | int parse_state = 0; |
| 400 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 401 | file = g_strdup(filename); |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 402 | |
| 403 | /* Parse a trailing ":readahead=#:" param, if present. */ |
| 404 | ra = file + strlen(file) - 1; |
| 405 | while (ra >= file) { |
| 406 | if (parse_state == 0) { |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 407 | if (*ra == ':') { |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 408 | parse_state++; |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 409 | } else { |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 410 | break; |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 411 | } |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 412 | } else if (parse_state == 1) { |
| 413 | if (*ra > '9' || *ra < '0') { |
| 414 | char *opt_start = ra - strlen(RA_OPTSTR) + 1; |
| 415 | if (opt_start > file && |
| 416 | strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) { |
| 417 | ra_val = ra + 1; |
| 418 | ra -= strlen(RA_OPTSTR) - 1; |
| 419 | *ra = '\0'; |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 420 | qdict_put(options, "readahead", qstring_from_str(ra_val)); |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 421 | } |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 422 | break; |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | ra--; |
| 426 | } |
| 427 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 428 | qdict_put(options, "url", qstring_from_str(file)); |
| 429 | |
| 430 | g_free(file); |
| 431 | } |
| 432 | |
| 433 | static QemuOptsList runtime_opts = { |
| 434 | .name = "curl", |
| 435 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
| 436 | .desc = { |
| 437 | { |
| 438 | .name = "url", |
| 439 | .type = QEMU_OPT_STRING, |
| 440 | .help = "URL to open", |
| 441 | }, |
| 442 | { |
| 443 | .name = "readahead", |
| 444 | .type = QEMU_OPT_SIZE, |
| 445 | .help = "Readahead size", |
| 446 | }, |
| 447 | { /* end of list */ } |
| 448 | }, |
| 449 | }; |
| 450 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 451 | static int curl_open(BlockDriverState *bs, QDict *options, int flags, |
| 452 | Error **errp) |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 453 | { |
| 454 | BDRVCURLState *s = bs->opaque; |
| 455 | CURLState *state = NULL; |
| 456 | QemuOpts *opts; |
| 457 | Error *local_err = NULL; |
| 458 | const char *file; |
| 459 | double d; |
| 460 | |
| 461 | static int inited = 0; |
| 462 | |
Richard W.M. Jones | a7cea2b | 2013-06-10 12:38:43 +0100 | [diff] [blame] | 463 | if (flags & BDRV_O_RDWR) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 464 | error_setg(errp, "curl block device does not support writes"); |
Richard W.M. Jones | a7cea2b | 2013-06-10 12:38:43 +0100 | [diff] [blame] | 465 | return -EROFS; |
| 466 | } |
| 467 | |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 468 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 469 | qemu_opts_absorb_qdict(opts, options, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 470 | if (local_err) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 471 | error_propagate(errp, local_err); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 472 | goto out_noclean; |
| 473 | } |
| 474 | |
| 475 | s->readahead_size = qemu_opt_get_size(opts, "readahead", READ_AHEAD_SIZE); |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 476 | if ((s->readahead_size & 0x1ff) != 0) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 477 | error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", |
| 478 | s->readahead_size); |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 479 | goto out_noclean; |
| 480 | } |
| 481 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 482 | file = qemu_opt_get(opts, "url"); |
| 483 | if (file == NULL) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 484 | error_setg(errp, "curl block driver requires an 'url' option"); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 485 | goto out_noclean; |
| 486 | } |
| 487 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 488 | if (!inited) { |
| 489 | curl_global_init(CURL_GLOBAL_ALL); |
| 490 | inited = 1; |
| 491 | } |
| 492 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 493 | DPRINTF("CURL: Opening %s\n", file); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 494 | s->url = g_strdup(file); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 495 | state = curl_init_state(s); |
| 496 | if (!state) |
| 497 | goto out_noclean; |
| 498 | |
| 499 | // Get file size |
| 500 | |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 501 | s->accept_range = false; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 502 | curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 503 | curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, |
| 504 | curl_header_cb); |
| 505 | curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 506 | if (curl_easy_perform(state->curl)) |
| 507 | goto out; |
| 508 | curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 509 | if (d) |
| 510 | s->len = (size_t)d; |
| 511 | else if(!s->len) |
| 512 | goto out; |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 513 | if ((!strncasecmp(s->url, "http://", strlen("http://")) |
| 514 | || !strncasecmp(s->url, "https://", strlen("https://"))) |
| 515 | && !s->accept_range) { |
| 516 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
| 517 | "Server does not support 'range' (byte ranges)."); |
| 518 | goto out; |
| 519 | } |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 520 | DPRINTF("CURL: Size = %zd\n", s->len); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 521 | |
| 522 | curl_clean_state(state); |
| 523 | curl_easy_cleanup(state->curl); |
| 524 | state->curl = NULL; |
| 525 | |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 526 | aio_timer_init(bdrv_get_aio_context(bs), &s->timer, |
| 527 | QEMU_CLOCK_REALTIME, SCALE_NS, |
| 528 | curl_multi_timeout_do, s); |
| 529 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 530 | // Now we know the file exists and its size, so let's |
| 531 | // initialize the multi interface! |
| 532 | |
| 533 | s->multi = curl_multi_init(); |
Richard W.M. Jones | 9e5e2b2 | 2013-06-10 14:14:35 +0100 | [diff] [blame] | 534 | curl_multi_setopt(s->multi, CURLMOPT_SOCKETDATA, s); |
| 535 | curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 536 | #ifdef NEED_CURL_TIMER_CALLBACK |
| 537 | curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); |
| 538 | curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); |
| 539 | #endif |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 540 | curl_multi_do(s); |
| 541 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 542 | qemu_opts_del(opts); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 543 | return 0; |
| 544 | |
| 545 | out: |
Maria Kustova | acd7fdc | 2014-03-18 09:59:18 +0400 | [diff] [blame] | 546 | error_setg(errp, "CURL: Error opening file: %s", state->errmsg); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 547 | curl_easy_cleanup(state->curl); |
| 548 | state->curl = NULL; |
| 549 | out_noclean: |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 550 | g_free(s->url); |
| 551 | qemu_opts_del(opts); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 552 | return -EINVAL; |
| 553 | } |
| 554 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 555 | static void curl_aio_cancel(BlockDriverAIOCB *blockacb) |
| 556 | { |
| 557 | // Do we have to implement canceling? Seems to work without... |
| 558 | } |
| 559 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 560 | static const AIOCBInfo curl_aiocb_info = { |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 561 | .aiocb_size = sizeof(CURLAIOCB), |
| 562 | .cancel = curl_aio_cancel, |
| 563 | }; |
| 564 | |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 565 | |
| 566 | static void curl_readv_bh_cb(void *p) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 567 | { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 568 | CURLState *state; |
| 569 | |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 570 | CURLAIOCB *acb = p; |
| 571 | BDRVCURLState *s = acb->common.bs->opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 572 | |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 573 | qemu_bh_delete(acb->bh); |
| 574 | acb->bh = NULL; |
| 575 | |
| 576 | size_t start = acb->sector_num * SECTOR_SIZE; |
| 577 | size_t end; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 578 | |
| 579 | // In case we have the requested data already (e.g. read-ahead), |
| 580 | // we can just call the callback and be done. |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 581 | switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 582 | case FIND_RET_OK: |
| 583 | qemu_aio_release(acb); |
| 584 | // fall through |
| 585 | case FIND_RET_WAIT: |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 586 | return; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 587 | default: |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | // No cache found, so let's start a new request |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 592 | state = curl_init_state(s); |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 593 | if (!state) { |
| 594 | acb->common.cb(acb->common.opaque, -EIO); |
| 595 | qemu_aio_release(acb); |
| 596 | return; |
| 597 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 598 | |
| 599 | acb->start = 0; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 600 | acb->end = (acb->nb_sectors * SECTOR_SIZE); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 601 | |
| 602 | state->buf_off = 0; |
| 603 | if (state->orig_buf) |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 604 | g_free(state->orig_buf); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 605 | state->buf_start = start; |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 606 | state->buf_len = acb->end + s->readahead_size; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 607 | end = MIN(start + state->buf_len, s->len) - 1; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 608 | state->orig_buf = g_malloc(state->buf_len); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 609 | state->acb[0] = acb; |
| 610 | |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 611 | snprintf(state->range, 127, "%zd-%zd", start, end); |
| 612 | DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n", |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 613 | (acb->nb_sectors * SECTOR_SIZE), start, state->range); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 614 | curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); |
| 615 | |
| 616 | curl_multi_add_handle(s->multi, state->curl); |
| 617 | curl_multi_do(s); |
| 618 | |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs, |
| 622 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 623 | BlockDriverCompletionFunc *cb, void *opaque) |
| 624 | { |
| 625 | CURLAIOCB *acb; |
| 626 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 627 | acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque); |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 628 | |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 629 | acb->qiov = qiov; |
| 630 | acb->sector_num = sector_num; |
| 631 | acb->nb_sectors = nb_sectors; |
| 632 | |
| 633 | acb->bh = qemu_bh_new(curl_readv_bh_cb, acb); |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 634 | qemu_bh_schedule(acb->bh); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 635 | return &acb->common; |
| 636 | } |
| 637 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 638 | static void curl_close(BlockDriverState *bs) |
| 639 | { |
| 640 | BDRVCURLState *s = bs->opaque; |
| 641 | int i; |
| 642 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 643 | DPRINTF("CURL: Close\n"); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 644 | for (i=0; i<CURL_NUM_STATES; i++) { |
| 645 | if (s->states[i].in_use) |
| 646 | curl_clean_state(&s->states[i]); |
| 647 | if (s->states[i].curl) { |
| 648 | curl_easy_cleanup(s->states[i].curl); |
| 649 | s->states[i].curl = NULL; |
| 650 | } |
| 651 | if (s->states[i].orig_buf) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 652 | g_free(s->states[i].orig_buf); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 653 | s->states[i].orig_buf = NULL; |
| 654 | } |
| 655 | } |
| 656 | if (s->multi) |
| 657 | curl_multi_cleanup(s->multi); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 658 | |
| 659 | timer_del(&s->timer); |
| 660 | |
Stefan Weil | 45724d6 | 2012-09-01 11:06:45 +0200 | [diff] [blame] | 661 | g_free(s->url); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static int64_t curl_getlength(BlockDriverState *bs) |
| 665 | { |
| 666 | BDRVCURLState *s = bs->opaque; |
| 667 | return s->len; |
| 668 | } |
| 669 | |
| 670 | static BlockDriver bdrv_http = { |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 671 | .format_name = "http", |
| 672 | .protocol_name = "http", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 673 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 674 | .instance_size = sizeof(BDRVCURLState), |
| 675 | .bdrv_parse_filename = curl_parse_filename, |
| 676 | .bdrv_file_open = curl_open, |
| 677 | .bdrv_close = curl_close, |
| 678 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 679 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 680 | .bdrv_aio_readv = curl_aio_readv, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 681 | }; |
| 682 | |
| 683 | static BlockDriver bdrv_https = { |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 684 | .format_name = "https", |
| 685 | .protocol_name = "https", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 686 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 687 | .instance_size = sizeof(BDRVCURLState), |
| 688 | .bdrv_parse_filename = curl_parse_filename, |
| 689 | .bdrv_file_open = curl_open, |
| 690 | .bdrv_close = curl_close, |
| 691 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 692 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 693 | .bdrv_aio_readv = curl_aio_readv, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 694 | }; |
| 695 | |
| 696 | static BlockDriver bdrv_ftp = { |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 697 | .format_name = "ftp", |
| 698 | .protocol_name = "ftp", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 699 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 700 | .instance_size = sizeof(BDRVCURLState), |
| 701 | .bdrv_parse_filename = curl_parse_filename, |
| 702 | .bdrv_file_open = curl_open, |
| 703 | .bdrv_close = curl_close, |
| 704 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 705 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 706 | .bdrv_aio_readv = curl_aio_readv, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 707 | }; |
| 708 | |
| 709 | static BlockDriver bdrv_ftps = { |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 710 | .format_name = "ftps", |
| 711 | .protocol_name = "ftps", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 712 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 713 | .instance_size = sizeof(BDRVCURLState), |
| 714 | .bdrv_parse_filename = curl_parse_filename, |
| 715 | .bdrv_file_open = curl_open, |
| 716 | .bdrv_close = curl_close, |
| 717 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 718 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 719 | .bdrv_aio_readv = curl_aio_readv, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 720 | }; |
| 721 | |
| 722 | static BlockDriver bdrv_tftp = { |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 723 | .format_name = "tftp", |
| 724 | .protocol_name = "tftp", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 725 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 726 | .instance_size = sizeof(BDRVCURLState), |
| 727 | .bdrv_parse_filename = curl_parse_filename, |
| 728 | .bdrv_file_open = curl_open, |
| 729 | .bdrv_close = curl_close, |
| 730 | .bdrv_getlength = curl_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 731 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 732 | .bdrv_aio_readv = curl_aio_readv, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 733 | }; |
| 734 | |
| 735 | static void curl_block_init(void) |
| 736 | { |
| 737 | bdrv_register(&bdrv_http); |
| 738 | bdrv_register(&bdrv_https); |
| 739 | bdrv_register(&bdrv_ftp); |
| 740 | bdrv_register(&bdrv_ftps); |
| 741 | bdrv_register(&bdrv_tftp); |
| 742 | } |
| 743 | |
| 744 | block_init(curl_block_init); |