1769ce76dSAlexander Graf /* 2769ce76dSAlexander Graf * QEMU Block driver for CURL images 3769ce76dSAlexander Graf * 4769ce76dSAlexander Graf * Copyright (c) 2009 Alexander Graf <agraf@suse.de> 5769ce76dSAlexander Graf * 6769ce76dSAlexander Graf * Permission is hereby granted, free of charge, to any person obtaining a copy 7769ce76dSAlexander Graf * of this software and associated documentation files (the "Software"), to deal 8769ce76dSAlexander Graf * in the Software without restriction, including without limitation the rights 9769ce76dSAlexander Graf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10769ce76dSAlexander Graf * copies of the Software, and to permit persons to whom the Software is 11769ce76dSAlexander Graf * furnished to do so, subject to the following conditions: 12769ce76dSAlexander Graf * 13769ce76dSAlexander Graf * The above copyright notice and this permission notice shall be included in 14769ce76dSAlexander Graf * all copies or substantial portions of the Software. 15769ce76dSAlexander Graf * 16769ce76dSAlexander Graf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17769ce76dSAlexander Graf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18769ce76dSAlexander Graf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19769ce76dSAlexander Graf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20769ce76dSAlexander Graf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21769ce76dSAlexander Graf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22769ce76dSAlexander Graf * THE SOFTWARE. 23769ce76dSAlexander Graf */ 24769ce76dSAlexander Graf #include "qemu-common.h" 25769ce76dSAlexander Graf #include "block_int.h" 26769ce76dSAlexander Graf #include <curl/curl.h> 27769ce76dSAlexander Graf 28769ce76dSAlexander Graf // #define DEBUG 29769ce76dSAlexander Graf // #define DEBUG_VERBOSE 30769ce76dSAlexander Graf 31769ce76dSAlexander Graf #ifdef DEBUG_CURL 32*d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0) 33769ce76dSAlexander Graf #else 34*d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { } while (0) 35769ce76dSAlexander Graf #endif 36769ce76dSAlexander Graf 37769ce76dSAlexander Graf #define CURL_NUM_STATES 8 38769ce76dSAlexander Graf #define CURL_NUM_ACB 8 39769ce76dSAlexander Graf #define SECTOR_SIZE 512 40769ce76dSAlexander Graf #define READ_AHEAD_SIZE (256 * 1024) 41769ce76dSAlexander Graf 42769ce76dSAlexander Graf #define FIND_RET_NONE 0 43769ce76dSAlexander Graf #define FIND_RET_OK 1 44769ce76dSAlexander Graf #define FIND_RET_WAIT 2 45769ce76dSAlexander Graf 46769ce76dSAlexander Graf struct BDRVCURLState; 47769ce76dSAlexander Graf 48769ce76dSAlexander Graf typedef struct CURLAIOCB { 49769ce76dSAlexander Graf BlockDriverAIOCB common; 50769ce76dSAlexander Graf QEMUIOVector *qiov; 51769ce76dSAlexander Graf size_t start; 52769ce76dSAlexander Graf size_t end; 53769ce76dSAlexander Graf } CURLAIOCB; 54769ce76dSAlexander Graf 55769ce76dSAlexander Graf typedef struct CURLState 56769ce76dSAlexander Graf { 57769ce76dSAlexander Graf struct BDRVCURLState *s; 58769ce76dSAlexander Graf CURLAIOCB *acb[CURL_NUM_ACB]; 59769ce76dSAlexander Graf CURL *curl; 60769ce76dSAlexander Graf char *orig_buf; 61769ce76dSAlexander Graf size_t buf_start; 62769ce76dSAlexander Graf size_t buf_off; 63769ce76dSAlexander Graf size_t buf_len; 64769ce76dSAlexander Graf char range[128]; 65769ce76dSAlexander Graf char errmsg[CURL_ERROR_SIZE]; 66769ce76dSAlexander Graf char in_use; 67769ce76dSAlexander Graf } CURLState; 68769ce76dSAlexander Graf 69769ce76dSAlexander Graf typedef struct BDRVCURLState { 70769ce76dSAlexander Graf CURLM *multi; 71769ce76dSAlexander Graf size_t len; 72769ce76dSAlexander Graf CURLState states[CURL_NUM_STATES]; 73769ce76dSAlexander Graf char *url; 74c76f4952SNolan size_t readahead_size; 75769ce76dSAlexander Graf } BDRVCURLState; 76769ce76dSAlexander Graf 77769ce76dSAlexander Graf static void curl_clean_state(CURLState *s); 78769ce76dSAlexander Graf static void curl_multi_do(void *arg); 79769ce76dSAlexander Graf 80769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, 81769ce76dSAlexander Graf void *s, void *sp) 82769ce76dSAlexander Graf { 83*d0f2c4c6Smalc DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd); 84769ce76dSAlexander Graf switch (action) { 85769ce76dSAlexander Graf case CURL_POLL_IN: 868febfa26SKevin Wolf qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, NULL, NULL, s); 87769ce76dSAlexander Graf break; 88769ce76dSAlexander Graf case CURL_POLL_OUT: 898febfa26SKevin Wolf qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, NULL, NULL, s); 90769ce76dSAlexander Graf break; 91769ce76dSAlexander Graf case CURL_POLL_INOUT: 92769ce76dSAlexander Graf qemu_aio_set_fd_handler(fd, curl_multi_do, 938febfa26SKevin Wolf curl_multi_do, NULL, NULL, s); 94769ce76dSAlexander Graf break; 95769ce76dSAlexander Graf case CURL_POLL_REMOVE: 968febfa26SKevin Wolf qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL, NULL); 97769ce76dSAlexander Graf break; 98769ce76dSAlexander Graf } 99769ce76dSAlexander Graf 100769ce76dSAlexander Graf return 0; 101769ce76dSAlexander Graf } 102769ce76dSAlexander Graf 103769ce76dSAlexander Graf static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 104769ce76dSAlexander Graf { 105769ce76dSAlexander Graf CURLState *s = ((CURLState*)opaque); 106769ce76dSAlexander Graf size_t realsize = size * nmemb; 107769ce76dSAlexander Graf long long fsize; 108769ce76dSAlexander Graf 109769ce76dSAlexander Graf if(sscanf(ptr, "Content-Length: %lld", &fsize) == 1) 110769ce76dSAlexander Graf s->s->len = fsize; 111769ce76dSAlexander Graf 112769ce76dSAlexander Graf return realsize; 113769ce76dSAlexander Graf } 114769ce76dSAlexander Graf 115769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 116769ce76dSAlexander Graf { 117769ce76dSAlexander Graf CURLState *s = ((CURLState*)opaque); 118769ce76dSAlexander Graf size_t realsize = size * nmemb; 119769ce76dSAlexander Graf int i; 120769ce76dSAlexander Graf 121*d0f2c4c6Smalc DPRINTF("CURL: Just reading %lld bytes\n", (unsigned long long)realsize); 122769ce76dSAlexander Graf 123769ce76dSAlexander Graf if (!s || !s->orig_buf) 124769ce76dSAlexander Graf goto read_end; 125769ce76dSAlexander Graf 126769ce76dSAlexander Graf memcpy(s->orig_buf + s->buf_off, ptr, realsize); 127769ce76dSAlexander Graf s->buf_off += realsize; 128769ce76dSAlexander Graf 129769ce76dSAlexander Graf for(i=0; i<CURL_NUM_ACB; i++) { 130769ce76dSAlexander Graf CURLAIOCB *acb = s->acb[i]; 131769ce76dSAlexander Graf 132769ce76dSAlexander Graf if (!acb) 133769ce76dSAlexander Graf continue; 134769ce76dSAlexander Graf 135769ce76dSAlexander Graf if ((s->buf_off >= acb->end)) { 136769ce76dSAlexander Graf qemu_iovec_from_buffer(acb->qiov, s->orig_buf + acb->start, 137769ce76dSAlexander Graf acb->end - acb->start); 138769ce76dSAlexander Graf acb->common.cb(acb->common.opaque, 0); 139769ce76dSAlexander Graf qemu_aio_release(acb); 140769ce76dSAlexander Graf s->acb[i] = NULL; 141769ce76dSAlexander Graf } 142769ce76dSAlexander Graf } 143769ce76dSAlexander Graf 144769ce76dSAlexander Graf read_end: 145769ce76dSAlexander Graf return realsize; 146769ce76dSAlexander Graf } 147769ce76dSAlexander Graf 148769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len, 149769ce76dSAlexander Graf CURLAIOCB *acb) 150769ce76dSAlexander Graf { 151769ce76dSAlexander Graf int i; 152769ce76dSAlexander Graf size_t end = start + len; 153769ce76dSAlexander Graf 154769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 155769ce76dSAlexander Graf CURLState *state = &s->states[i]; 156769ce76dSAlexander Graf size_t buf_end = (state->buf_start + state->buf_off); 157769ce76dSAlexander Graf size_t buf_fend = (state->buf_start + state->buf_len); 158769ce76dSAlexander Graf 159769ce76dSAlexander Graf if (!state->orig_buf) 160769ce76dSAlexander Graf continue; 161769ce76dSAlexander Graf if (!state->buf_off) 162769ce76dSAlexander Graf continue; 163769ce76dSAlexander Graf 164769ce76dSAlexander Graf // Does the existing buffer cover our section? 165769ce76dSAlexander Graf if ((start >= state->buf_start) && 166769ce76dSAlexander Graf (start <= buf_end) && 167769ce76dSAlexander Graf (end >= state->buf_start) && 168769ce76dSAlexander Graf (end <= buf_end)) 169769ce76dSAlexander Graf { 170769ce76dSAlexander Graf char *buf = state->orig_buf + (start - state->buf_start); 171769ce76dSAlexander Graf 172769ce76dSAlexander Graf qemu_iovec_from_buffer(acb->qiov, buf, len); 173769ce76dSAlexander Graf acb->common.cb(acb->common.opaque, 0); 174769ce76dSAlexander Graf 175769ce76dSAlexander Graf return FIND_RET_OK; 176769ce76dSAlexander Graf } 177769ce76dSAlexander Graf 178769ce76dSAlexander Graf // Wait for unfinished chunks 179769ce76dSAlexander Graf if ((start >= state->buf_start) && 180769ce76dSAlexander Graf (start <= buf_fend) && 181769ce76dSAlexander Graf (end >= state->buf_start) && 182769ce76dSAlexander Graf (end <= buf_fend)) 183769ce76dSAlexander Graf { 184769ce76dSAlexander Graf int j; 185769ce76dSAlexander Graf 186769ce76dSAlexander Graf acb->start = start - state->buf_start; 187769ce76dSAlexander Graf acb->end = acb->start + len; 188769ce76dSAlexander Graf 189769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) { 190769ce76dSAlexander Graf if (!state->acb[j]) { 191769ce76dSAlexander Graf state->acb[j] = acb; 192769ce76dSAlexander Graf return FIND_RET_WAIT; 193769ce76dSAlexander Graf } 194769ce76dSAlexander Graf } 195769ce76dSAlexander Graf } 196769ce76dSAlexander Graf } 197769ce76dSAlexander Graf 198769ce76dSAlexander Graf return FIND_RET_NONE; 199769ce76dSAlexander Graf } 200769ce76dSAlexander Graf 201769ce76dSAlexander Graf static void curl_multi_do(void *arg) 202769ce76dSAlexander Graf { 203769ce76dSAlexander Graf BDRVCURLState *s = (BDRVCURLState *)arg; 204769ce76dSAlexander Graf int running; 205769ce76dSAlexander Graf int r; 206769ce76dSAlexander Graf int msgs_in_queue; 207769ce76dSAlexander Graf 208769ce76dSAlexander Graf if (!s->multi) 209769ce76dSAlexander Graf return; 210769ce76dSAlexander Graf 211769ce76dSAlexander Graf do { 212769ce76dSAlexander Graf r = curl_multi_socket_all(s->multi, &running); 213769ce76dSAlexander Graf } while(r == CURLM_CALL_MULTI_PERFORM); 214769ce76dSAlexander Graf 215769ce76dSAlexander Graf /* Try to find done transfers, so we can free the easy 216769ce76dSAlexander Graf * handle again. */ 217769ce76dSAlexander Graf do { 218769ce76dSAlexander Graf CURLMsg *msg; 219769ce76dSAlexander Graf msg = curl_multi_info_read(s->multi, &msgs_in_queue); 220769ce76dSAlexander Graf 221769ce76dSAlexander Graf if (!msg) 222769ce76dSAlexander Graf break; 223769ce76dSAlexander Graf if (msg->msg == CURLMSG_NONE) 224769ce76dSAlexander Graf break; 225769ce76dSAlexander Graf 226769ce76dSAlexander Graf switch (msg->msg) { 227769ce76dSAlexander Graf case CURLMSG_DONE: 228769ce76dSAlexander Graf { 229769ce76dSAlexander Graf CURLState *state = NULL; 230769ce76dSAlexander Graf curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state); 231769ce76dSAlexander Graf curl_clean_state(state); 232769ce76dSAlexander Graf break; 233769ce76dSAlexander Graf } 234769ce76dSAlexander Graf default: 235769ce76dSAlexander Graf msgs_in_queue = 0; 236769ce76dSAlexander Graf break; 237769ce76dSAlexander Graf } 238769ce76dSAlexander Graf } while(msgs_in_queue); 239769ce76dSAlexander Graf } 240769ce76dSAlexander Graf 241769ce76dSAlexander Graf static CURLState *curl_init_state(BDRVCURLState *s) 242769ce76dSAlexander Graf { 243769ce76dSAlexander Graf CURLState *state = NULL; 244769ce76dSAlexander Graf int i, j; 245769ce76dSAlexander Graf 246769ce76dSAlexander Graf do { 247769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 248769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) 249769ce76dSAlexander Graf if (s->states[i].acb[j]) 250769ce76dSAlexander Graf continue; 251769ce76dSAlexander Graf if (s->states[i].in_use) 252769ce76dSAlexander Graf continue; 253769ce76dSAlexander Graf 254769ce76dSAlexander Graf state = &s->states[i]; 255769ce76dSAlexander Graf state->in_use = 1; 256769ce76dSAlexander Graf break; 257769ce76dSAlexander Graf } 258769ce76dSAlexander Graf if (!state) { 259769ce76dSAlexander Graf usleep(100); 260769ce76dSAlexander Graf curl_multi_do(s); 261769ce76dSAlexander Graf } 262769ce76dSAlexander Graf } while(!state); 263769ce76dSAlexander Graf 264769ce76dSAlexander Graf if (state->curl) 265769ce76dSAlexander Graf goto has_curl; 266769ce76dSAlexander Graf 267769ce76dSAlexander Graf state->curl = curl_easy_init(); 268769ce76dSAlexander Graf if (!state->curl) 269769ce76dSAlexander Graf return NULL; 270769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_URL, s->url); 271769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5); 272df3cee1aSBlue Swirl curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb); 273769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); 274769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); 275769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); 276769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); 277769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); 278769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); 279769ce76dSAlexander Graf 280769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE 281769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); 282769ce76dSAlexander Graf #endif 283769ce76dSAlexander Graf 284769ce76dSAlexander Graf has_curl: 285769ce76dSAlexander Graf 286769ce76dSAlexander Graf state->s = s; 287769ce76dSAlexander Graf 288769ce76dSAlexander Graf return state; 289769ce76dSAlexander Graf } 290769ce76dSAlexander Graf 291769ce76dSAlexander Graf static void curl_clean_state(CURLState *s) 292769ce76dSAlexander Graf { 293769ce76dSAlexander Graf if (s->s->multi) 294769ce76dSAlexander Graf curl_multi_remove_handle(s->s->multi, s->curl); 295769ce76dSAlexander Graf s->in_use = 0; 296769ce76dSAlexander Graf } 297769ce76dSAlexander Graf 298769ce76dSAlexander Graf static int curl_open(BlockDriverState *bs, const char *filename, int flags) 299769ce76dSAlexander Graf { 300769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 301769ce76dSAlexander Graf CURLState *state = NULL; 302769ce76dSAlexander Graf double d; 303c76f4952SNolan 304c76f4952SNolan #define RA_OPTSTR ":readahead=" 305c76f4952SNolan char *file; 306c76f4952SNolan char *ra; 307c76f4952SNolan const char *ra_val; 308c76f4952SNolan int parse_state = 0; 309c76f4952SNolan 310769ce76dSAlexander Graf static int inited = 0; 311769ce76dSAlexander Graf 312c76f4952SNolan file = strdup(filename); 313c76f4952SNolan s->readahead_size = READ_AHEAD_SIZE; 314c76f4952SNolan 315c76f4952SNolan /* Parse a trailing ":readahead=#:" param, if present. */ 316c76f4952SNolan ra = file + strlen(file) - 1; 317c76f4952SNolan while (ra >= file) { 318c76f4952SNolan if (parse_state == 0) { 319c76f4952SNolan if (*ra == ':') 320c76f4952SNolan parse_state++; 321c76f4952SNolan else 322c76f4952SNolan break; 323c76f4952SNolan } else if (parse_state == 1) { 324c76f4952SNolan if (*ra > '9' || *ra < '0') { 325c76f4952SNolan char *opt_start = ra - strlen(RA_OPTSTR) + 1; 326c76f4952SNolan if (opt_start > file && 327c76f4952SNolan strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) { 328c76f4952SNolan ra_val = ra + 1; 329c76f4952SNolan ra -= strlen(RA_OPTSTR) - 1; 330c76f4952SNolan *ra = '\0'; 331c76f4952SNolan s->readahead_size = atoi(ra_val); 332c76f4952SNolan break; 333c76f4952SNolan } else { 334c76f4952SNolan break; 335c76f4952SNolan } 336c76f4952SNolan } 337c76f4952SNolan } 338c76f4952SNolan ra--; 339c76f4952SNolan } 340c76f4952SNolan 341c76f4952SNolan if ((s->readahead_size & 0x1ff) != 0) { 342c76f4952SNolan fprintf(stderr, "HTTP_READAHEAD_SIZE %Zd is not a multiple of 512\n", 343c76f4952SNolan s->readahead_size); 344c76f4952SNolan goto out_noclean; 345c76f4952SNolan } 346c76f4952SNolan 347769ce76dSAlexander Graf if (!inited) { 348769ce76dSAlexander Graf curl_global_init(CURL_GLOBAL_ALL); 349769ce76dSAlexander Graf inited = 1; 350769ce76dSAlexander Graf } 351769ce76dSAlexander Graf 352*d0f2c4c6Smalc DPRINTF("CURL: Opening %s\n", file); 353c76f4952SNolan s->url = file; 354769ce76dSAlexander Graf state = curl_init_state(s); 355769ce76dSAlexander Graf if (!state) 356769ce76dSAlexander Graf goto out_noclean; 357769ce76dSAlexander Graf 358769ce76dSAlexander Graf // Get file size 359769ce76dSAlexander Graf 360769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); 361df3cee1aSBlue Swirl curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_size_cb); 362769ce76dSAlexander Graf if (curl_easy_perform(state->curl)) 363769ce76dSAlexander Graf goto out; 364769ce76dSAlexander Graf curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d); 365df3cee1aSBlue Swirl curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb); 366769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0); 367769ce76dSAlexander Graf if (d) 368769ce76dSAlexander Graf s->len = (size_t)d; 369769ce76dSAlexander Graf else if(!s->len) 370769ce76dSAlexander Graf goto out; 371*d0f2c4c6Smalc DPRINTF("CURL: Size = %lld\n", (long long)s->len); 372769ce76dSAlexander Graf 373769ce76dSAlexander Graf curl_clean_state(state); 374769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 375769ce76dSAlexander Graf state->curl = NULL; 376769ce76dSAlexander Graf 377769ce76dSAlexander Graf // Now we know the file exists and its size, so let's 378769ce76dSAlexander Graf // initialize the multi interface! 379769ce76dSAlexander Graf 380769ce76dSAlexander Graf s->multi = curl_multi_init(); 381769ce76dSAlexander Graf curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s); 382769ce76dSAlexander Graf curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb ); 383769ce76dSAlexander Graf curl_multi_do(s); 384769ce76dSAlexander Graf 385769ce76dSAlexander Graf return 0; 386769ce76dSAlexander Graf 387769ce76dSAlexander Graf out: 388769ce76dSAlexander Graf fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg); 389769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 390769ce76dSAlexander Graf state->curl = NULL; 391769ce76dSAlexander Graf out_noclean: 392c76f4952SNolan qemu_free(file); 393769ce76dSAlexander Graf return -EINVAL; 394769ce76dSAlexander Graf } 395769ce76dSAlexander Graf 396c16b5a2cSChristoph Hellwig static void curl_aio_cancel(BlockDriverAIOCB *blockacb) 397c16b5a2cSChristoph Hellwig { 398c16b5a2cSChristoph Hellwig // Do we have to implement canceling? Seems to work without... 399c16b5a2cSChristoph Hellwig } 400c16b5a2cSChristoph Hellwig 401c16b5a2cSChristoph Hellwig static AIOPool curl_aio_pool = { 402c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(CURLAIOCB), 403c16b5a2cSChristoph Hellwig .cancel = curl_aio_cancel, 404c16b5a2cSChristoph Hellwig }; 405c16b5a2cSChristoph Hellwig 406769ce76dSAlexander Graf static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs, 407769ce76dSAlexander Graf int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 408769ce76dSAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 409769ce76dSAlexander Graf { 410769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 411769ce76dSAlexander Graf CURLAIOCB *acb; 412769ce76dSAlexander Graf size_t start = sector_num * SECTOR_SIZE; 413769ce76dSAlexander Graf size_t end; 414769ce76dSAlexander Graf CURLState *state; 415769ce76dSAlexander Graf 416c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&curl_aio_pool, bs, cb, opaque); 417769ce76dSAlexander Graf if (!acb) 418769ce76dSAlexander Graf return NULL; 419769ce76dSAlexander Graf 420769ce76dSAlexander Graf acb->qiov = qiov; 421769ce76dSAlexander Graf 422769ce76dSAlexander Graf // In case we have the requested data already (e.g. read-ahead), 423769ce76dSAlexander Graf // we can just call the callback and be done. 424769ce76dSAlexander Graf 425769ce76dSAlexander Graf switch (curl_find_buf(s, start, nb_sectors * SECTOR_SIZE, acb)) { 426769ce76dSAlexander Graf case FIND_RET_OK: 427769ce76dSAlexander Graf qemu_aio_release(acb); 428769ce76dSAlexander Graf // fall through 429769ce76dSAlexander Graf case FIND_RET_WAIT: 430769ce76dSAlexander Graf return &acb->common; 431769ce76dSAlexander Graf default: 432769ce76dSAlexander Graf break; 433769ce76dSAlexander Graf } 434769ce76dSAlexander Graf 435769ce76dSAlexander Graf // No cache found, so let's start a new request 436769ce76dSAlexander Graf 437769ce76dSAlexander Graf state = curl_init_state(s); 438769ce76dSAlexander Graf if (!state) 439769ce76dSAlexander Graf return NULL; 440769ce76dSAlexander Graf 441769ce76dSAlexander Graf acb->start = 0; 442769ce76dSAlexander Graf acb->end = (nb_sectors * SECTOR_SIZE); 443769ce76dSAlexander Graf 444769ce76dSAlexander Graf state->buf_off = 0; 445769ce76dSAlexander Graf if (state->orig_buf) 446769ce76dSAlexander Graf qemu_free(state->orig_buf); 447769ce76dSAlexander Graf state->buf_start = start; 448c76f4952SNolan state->buf_len = acb->end + s->readahead_size; 449769ce76dSAlexander Graf end = MIN(start + state->buf_len, s->len) - 1; 450769ce76dSAlexander Graf state->orig_buf = qemu_malloc(state->buf_len); 451769ce76dSAlexander Graf state->acb[0] = acb; 452769ce76dSAlexander Graf 453769ce76dSAlexander Graf snprintf(state->range, 127, "%lld-%lld", (long long)start, (long long)end); 454*d0f2c4c6Smalc DPRINTF("CURL (AIO): Reading %d at %lld (%s)\n", (nb_sectors * SECTOR_SIZE), start, state->range); 455769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); 456769ce76dSAlexander Graf 457769ce76dSAlexander Graf curl_multi_add_handle(s->multi, state->curl); 458769ce76dSAlexander Graf curl_multi_do(s); 459769ce76dSAlexander Graf 460769ce76dSAlexander Graf return &acb->common; 461769ce76dSAlexander Graf } 462769ce76dSAlexander Graf 463769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs) 464769ce76dSAlexander Graf { 465769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 466769ce76dSAlexander Graf int i; 467769ce76dSAlexander Graf 468*d0f2c4c6Smalc DPRINTF("CURL: Close\n"); 469769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 470769ce76dSAlexander Graf if (s->states[i].in_use) 471769ce76dSAlexander Graf curl_clean_state(&s->states[i]); 472769ce76dSAlexander Graf if (s->states[i].curl) { 473769ce76dSAlexander Graf curl_easy_cleanup(s->states[i].curl); 474769ce76dSAlexander Graf s->states[i].curl = NULL; 475769ce76dSAlexander Graf } 476769ce76dSAlexander Graf if (s->states[i].orig_buf) { 477769ce76dSAlexander Graf qemu_free(s->states[i].orig_buf); 478769ce76dSAlexander Graf s->states[i].orig_buf = NULL; 479769ce76dSAlexander Graf } 480769ce76dSAlexander Graf } 481769ce76dSAlexander Graf if (s->multi) 482769ce76dSAlexander Graf curl_multi_cleanup(s->multi); 483769ce76dSAlexander Graf if (s->url) 484769ce76dSAlexander Graf free(s->url); 485769ce76dSAlexander Graf } 486769ce76dSAlexander Graf 487769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs) 488769ce76dSAlexander Graf { 489769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 490769ce76dSAlexander Graf return s->len; 491769ce76dSAlexander Graf } 492769ce76dSAlexander Graf 493769ce76dSAlexander Graf static BlockDriver bdrv_http = { 494769ce76dSAlexander Graf .format_name = "http", 495769ce76dSAlexander Graf .protocol_name = "http", 496769ce76dSAlexander Graf 497769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 498769ce76dSAlexander Graf .bdrv_open = curl_open, 499769ce76dSAlexander Graf .bdrv_close = curl_close, 500769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 501769ce76dSAlexander Graf 502769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 503769ce76dSAlexander Graf }; 504769ce76dSAlexander Graf 505769ce76dSAlexander Graf static BlockDriver bdrv_https = { 506769ce76dSAlexander Graf .format_name = "https", 507769ce76dSAlexander Graf .protocol_name = "https", 508769ce76dSAlexander Graf 509769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 510769ce76dSAlexander Graf .bdrv_open = curl_open, 511769ce76dSAlexander Graf .bdrv_close = curl_close, 512769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 513769ce76dSAlexander Graf 514769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 515769ce76dSAlexander Graf }; 516769ce76dSAlexander Graf 517769ce76dSAlexander Graf static BlockDriver bdrv_ftp = { 518769ce76dSAlexander Graf .format_name = "ftp", 519769ce76dSAlexander Graf .protocol_name = "ftp", 520769ce76dSAlexander Graf 521769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 522769ce76dSAlexander Graf .bdrv_open = curl_open, 523769ce76dSAlexander Graf .bdrv_close = curl_close, 524769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 525769ce76dSAlexander Graf 526769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 527769ce76dSAlexander Graf }; 528769ce76dSAlexander Graf 529769ce76dSAlexander Graf static BlockDriver bdrv_ftps = { 530769ce76dSAlexander Graf .format_name = "ftps", 531769ce76dSAlexander Graf .protocol_name = "ftps", 532769ce76dSAlexander Graf 533769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 534769ce76dSAlexander Graf .bdrv_open = curl_open, 535769ce76dSAlexander Graf .bdrv_close = curl_close, 536769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 537769ce76dSAlexander Graf 538769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 539769ce76dSAlexander Graf }; 540769ce76dSAlexander Graf 541769ce76dSAlexander Graf static BlockDriver bdrv_tftp = { 542769ce76dSAlexander Graf .format_name = "tftp", 543769ce76dSAlexander Graf .protocol_name = "tftp", 544769ce76dSAlexander Graf 545769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 546769ce76dSAlexander Graf .bdrv_open = curl_open, 547769ce76dSAlexander Graf .bdrv_close = curl_close, 548769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 549769ce76dSAlexander Graf 550769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 551769ce76dSAlexander Graf }; 552769ce76dSAlexander Graf 553769ce76dSAlexander Graf static void curl_block_init(void) 554769ce76dSAlexander Graf { 555769ce76dSAlexander Graf bdrv_register(&bdrv_http); 556769ce76dSAlexander Graf bdrv_register(&bdrv_https); 557769ce76dSAlexander Graf bdrv_register(&bdrv_ftp); 558769ce76dSAlexander Graf bdrv_register(&bdrv_ftps); 559769ce76dSAlexander Graf bdrv_register(&bdrv_tftp); 560769ce76dSAlexander Graf } 561769ce76dSAlexander Graf 562769ce76dSAlexander Graf block_init(curl_block_init); 563