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 32d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0) 33769ce76dSAlexander Graf #else 34d0f2c4c6Smalc #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; 50363c3c85SNick Thomas QEMUBH *bh; 51769ce76dSAlexander Graf QEMUIOVector *qiov; 52363c3c85SNick Thomas 53363c3c85SNick Thomas int64_t sector_num; 54363c3c85SNick Thomas int nb_sectors; 55363c3c85SNick Thomas 56769ce76dSAlexander Graf size_t start; 57769ce76dSAlexander Graf size_t end; 58769ce76dSAlexander Graf } CURLAIOCB; 59769ce76dSAlexander Graf 60769ce76dSAlexander Graf typedef struct CURLState 61769ce76dSAlexander Graf { 62769ce76dSAlexander Graf struct BDRVCURLState *s; 63769ce76dSAlexander Graf CURLAIOCB *acb[CURL_NUM_ACB]; 64769ce76dSAlexander Graf CURL *curl; 65769ce76dSAlexander Graf char *orig_buf; 66769ce76dSAlexander Graf size_t buf_start; 67769ce76dSAlexander Graf size_t buf_off; 68769ce76dSAlexander Graf size_t buf_len; 69769ce76dSAlexander Graf char range[128]; 70769ce76dSAlexander Graf char errmsg[CURL_ERROR_SIZE]; 71769ce76dSAlexander Graf char in_use; 72769ce76dSAlexander Graf } CURLState; 73769ce76dSAlexander Graf 74769ce76dSAlexander Graf typedef struct BDRVCURLState { 75769ce76dSAlexander Graf CURLM *multi; 76769ce76dSAlexander Graf size_t len; 77769ce76dSAlexander Graf CURLState states[CURL_NUM_STATES]; 78769ce76dSAlexander Graf char *url; 79c76f4952SNolan size_t readahead_size; 80769ce76dSAlexander Graf } BDRVCURLState; 81769ce76dSAlexander Graf 82769ce76dSAlexander Graf static void curl_clean_state(CURLState *s); 83769ce76dSAlexander Graf static void curl_multi_do(void *arg); 84c84dcdc1SNick Thomas static int curl_aio_flush(void *opaque); 85769ce76dSAlexander Graf 86769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, 87769ce76dSAlexander Graf void *s, void *sp) 88769ce76dSAlexander Graf { 89d0f2c4c6Smalc DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd); 90769ce76dSAlexander Graf switch (action) { 91769ce76dSAlexander Graf case CURL_POLL_IN: 92bafbd6a1SPaolo Bonzini qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, curl_aio_flush, s); 93769ce76dSAlexander Graf break; 94769ce76dSAlexander Graf case CURL_POLL_OUT: 95bafbd6a1SPaolo Bonzini qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, curl_aio_flush, s); 96769ce76dSAlexander Graf break; 97769ce76dSAlexander Graf case CURL_POLL_INOUT: 98c84dcdc1SNick Thomas qemu_aio_set_fd_handler(fd, curl_multi_do, curl_multi_do, 99bafbd6a1SPaolo Bonzini curl_aio_flush, s); 100769ce76dSAlexander Graf break; 101769ce76dSAlexander Graf case CURL_POLL_REMOVE: 102bafbd6a1SPaolo Bonzini qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL); 103769ce76dSAlexander Graf break; 104769ce76dSAlexander Graf } 105769ce76dSAlexander Graf 106769ce76dSAlexander Graf return 0; 107769ce76dSAlexander Graf } 108769ce76dSAlexander Graf 109769ce76dSAlexander Graf static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 110769ce76dSAlexander Graf { 111769ce76dSAlexander Graf CURLState *s = ((CURLState*)opaque); 112769ce76dSAlexander Graf size_t realsize = size * nmemb; 1130bfcd599SBlue Swirl size_t fsize; 114769ce76dSAlexander Graf 1150bfcd599SBlue Swirl if(sscanf(ptr, "Content-Length: %zd", &fsize) == 1) { 116769ce76dSAlexander Graf s->s->len = fsize; 1170bfcd599SBlue Swirl } 118769ce76dSAlexander Graf 119769ce76dSAlexander Graf return realsize; 120769ce76dSAlexander Graf } 121769ce76dSAlexander Graf 122769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 123769ce76dSAlexander Graf { 124769ce76dSAlexander Graf CURLState *s = ((CURLState*)opaque); 125769ce76dSAlexander Graf size_t realsize = size * nmemb; 126769ce76dSAlexander Graf int i; 127769ce76dSAlexander Graf 1280bfcd599SBlue Swirl DPRINTF("CURL: Just reading %zd bytes\n", realsize); 129769ce76dSAlexander Graf 130769ce76dSAlexander Graf if (!s || !s->orig_buf) 131769ce76dSAlexander Graf goto read_end; 132769ce76dSAlexander Graf 133769ce76dSAlexander Graf memcpy(s->orig_buf + s->buf_off, ptr, realsize); 134769ce76dSAlexander Graf s->buf_off += realsize; 135769ce76dSAlexander Graf 136769ce76dSAlexander Graf for(i=0; i<CURL_NUM_ACB; i++) { 137769ce76dSAlexander Graf CURLAIOCB *acb = s->acb[i]; 138769ce76dSAlexander Graf 139769ce76dSAlexander Graf if (!acb) 140769ce76dSAlexander Graf continue; 141769ce76dSAlexander Graf 142769ce76dSAlexander Graf if ((s->buf_off >= acb->end)) { 143*03396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start, 144769ce76dSAlexander Graf acb->end - acb->start); 145769ce76dSAlexander Graf acb->common.cb(acb->common.opaque, 0); 146769ce76dSAlexander Graf qemu_aio_release(acb); 147769ce76dSAlexander Graf s->acb[i] = NULL; 148769ce76dSAlexander Graf } 149769ce76dSAlexander Graf } 150769ce76dSAlexander Graf 151769ce76dSAlexander Graf read_end: 152769ce76dSAlexander Graf return realsize; 153769ce76dSAlexander Graf } 154769ce76dSAlexander Graf 155769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len, 156769ce76dSAlexander Graf CURLAIOCB *acb) 157769ce76dSAlexander Graf { 158769ce76dSAlexander Graf int i; 159769ce76dSAlexander Graf size_t end = start + len; 160769ce76dSAlexander Graf 161769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 162769ce76dSAlexander Graf CURLState *state = &s->states[i]; 163769ce76dSAlexander Graf size_t buf_end = (state->buf_start + state->buf_off); 164769ce76dSAlexander Graf size_t buf_fend = (state->buf_start + state->buf_len); 165769ce76dSAlexander Graf 166769ce76dSAlexander Graf if (!state->orig_buf) 167769ce76dSAlexander Graf continue; 168769ce76dSAlexander Graf if (!state->buf_off) 169769ce76dSAlexander Graf continue; 170769ce76dSAlexander Graf 171769ce76dSAlexander Graf // Does the existing buffer cover our section? 172769ce76dSAlexander Graf if ((start >= state->buf_start) && 173769ce76dSAlexander Graf (start <= buf_end) && 174769ce76dSAlexander Graf (end >= state->buf_start) && 175769ce76dSAlexander Graf (end <= buf_end)) 176769ce76dSAlexander Graf { 177769ce76dSAlexander Graf char *buf = state->orig_buf + (start - state->buf_start); 178769ce76dSAlexander Graf 179*03396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, buf, len); 180769ce76dSAlexander Graf acb->common.cb(acb->common.opaque, 0); 181769ce76dSAlexander Graf 182769ce76dSAlexander Graf return FIND_RET_OK; 183769ce76dSAlexander Graf } 184769ce76dSAlexander Graf 185769ce76dSAlexander Graf // Wait for unfinished chunks 186769ce76dSAlexander Graf if ((start >= state->buf_start) && 187769ce76dSAlexander Graf (start <= buf_fend) && 188769ce76dSAlexander Graf (end >= state->buf_start) && 189769ce76dSAlexander Graf (end <= buf_fend)) 190769ce76dSAlexander Graf { 191769ce76dSAlexander Graf int j; 192769ce76dSAlexander Graf 193769ce76dSAlexander Graf acb->start = start - state->buf_start; 194769ce76dSAlexander Graf acb->end = acb->start + len; 195769ce76dSAlexander Graf 196769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) { 197769ce76dSAlexander Graf if (!state->acb[j]) { 198769ce76dSAlexander Graf state->acb[j] = acb; 199769ce76dSAlexander Graf return FIND_RET_WAIT; 200769ce76dSAlexander Graf } 201769ce76dSAlexander Graf } 202769ce76dSAlexander Graf } 203769ce76dSAlexander Graf } 204769ce76dSAlexander Graf 205769ce76dSAlexander Graf return FIND_RET_NONE; 206769ce76dSAlexander Graf } 207769ce76dSAlexander Graf 208769ce76dSAlexander Graf static void curl_multi_do(void *arg) 209769ce76dSAlexander Graf { 210769ce76dSAlexander Graf BDRVCURLState *s = (BDRVCURLState *)arg; 211769ce76dSAlexander Graf int running; 212769ce76dSAlexander Graf int r; 213769ce76dSAlexander Graf int msgs_in_queue; 214769ce76dSAlexander Graf 215769ce76dSAlexander Graf if (!s->multi) 216769ce76dSAlexander Graf return; 217769ce76dSAlexander Graf 218769ce76dSAlexander Graf do { 219769ce76dSAlexander Graf r = curl_multi_socket_all(s->multi, &running); 220769ce76dSAlexander Graf } while(r == CURLM_CALL_MULTI_PERFORM); 221769ce76dSAlexander Graf 222769ce76dSAlexander Graf /* Try to find done transfers, so we can free the easy 223769ce76dSAlexander Graf * handle again. */ 224769ce76dSAlexander Graf do { 225769ce76dSAlexander Graf CURLMsg *msg; 226769ce76dSAlexander Graf msg = curl_multi_info_read(s->multi, &msgs_in_queue); 227769ce76dSAlexander Graf 228769ce76dSAlexander Graf if (!msg) 229769ce76dSAlexander Graf break; 230769ce76dSAlexander Graf if (msg->msg == CURLMSG_NONE) 231769ce76dSAlexander Graf break; 232769ce76dSAlexander Graf 233769ce76dSAlexander Graf switch (msg->msg) { 234769ce76dSAlexander Graf case CURLMSG_DONE: 235769ce76dSAlexander Graf { 236769ce76dSAlexander Graf CURLState *state = NULL; 237769ce76dSAlexander Graf curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state); 238f785a5aeSNicholas Thomas 239f785a5aeSNicholas Thomas /* ACBs for successful messages get completed in curl_read_cb */ 240f785a5aeSNicholas Thomas if (msg->data.result != CURLE_OK) { 241f785a5aeSNicholas Thomas int i; 242f785a5aeSNicholas Thomas for (i = 0; i < CURL_NUM_ACB; i++) { 243f785a5aeSNicholas Thomas CURLAIOCB *acb = state->acb[i]; 244f785a5aeSNicholas Thomas 245f785a5aeSNicholas Thomas if (acb == NULL) { 246f785a5aeSNicholas Thomas continue; 247f785a5aeSNicholas Thomas } 248f785a5aeSNicholas Thomas 249f785a5aeSNicholas Thomas acb->common.cb(acb->common.opaque, -EIO); 250f785a5aeSNicholas Thomas qemu_aio_release(acb); 251f785a5aeSNicholas Thomas state->acb[i] = NULL; 252f785a5aeSNicholas Thomas } 253f785a5aeSNicholas Thomas } 254f785a5aeSNicholas Thomas 255769ce76dSAlexander Graf curl_clean_state(state); 256769ce76dSAlexander Graf break; 257769ce76dSAlexander Graf } 258769ce76dSAlexander Graf default: 259769ce76dSAlexander Graf msgs_in_queue = 0; 260769ce76dSAlexander Graf break; 261769ce76dSAlexander Graf } 262769ce76dSAlexander Graf } while(msgs_in_queue); 263769ce76dSAlexander Graf } 264769ce76dSAlexander Graf 265769ce76dSAlexander Graf static CURLState *curl_init_state(BDRVCURLState *s) 266769ce76dSAlexander Graf { 267769ce76dSAlexander Graf CURLState *state = NULL; 268769ce76dSAlexander Graf int i, j; 269769ce76dSAlexander Graf 270769ce76dSAlexander Graf do { 271769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 272769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) 273769ce76dSAlexander Graf if (s->states[i].acb[j]) 274769ce76dSAlexander Graf continue; 275769ce76dSAlexander Graf if (s->states[i].in_use) 276769ce76dSAlexander Graf continue; 277769ce76dSAlexander Graf 278769ce76dSAlexander Graf state = &s->states[i]; 279769ce76dSAlexander Graf state->in_use = 1; 280769ce76dSAlexander Graf break; 281769ce76dSAlexander Graf } 282769ce76dSAlexander Graf if (!state) { 283fb7c8e8aSStefan Weil g_usleep(100); 284769ce76dSAlexander Graf curl_multi_do(s); 285769ce76dSAlexander Graf } 286769ce76dSAlexander Graf } while(!state); 287769ce76dSAlexander Graf 288769ce76dSAlexander Graf if (state->curl) 289769ce76dSAlexander Graf goto has_curl; 290769ce76dSAlexander Graf 291769ce76dSAlexander Graf state->curl = curl_easy_init(); 292769ce76dSAlexander Graf if (!state->curl) 293769ce76dSAlexander Graf return NULL; 294769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_URL, s->url); 295769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5); 296df3cee1aSBlue Swirl curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb); 297769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); 298769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); 299769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); 300769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); 301769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); 302769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); 303f785a5aeSNicholas Thomas curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); 304769ce76dSAlexander Graf 305769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE 306769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); 307769ce76dSAlexander Graf #endif 308769ce76dSAlexander Graf 309769ce76dSAlexander Graf has_curl: 310769ce76dSAlexander Graf 311769ce76dSAlexander Graf state->s = s; 312769ce76dSAlexander Graf 313769ce76dSAlexander Graf return state; 314769ce76dSAlexander Graf } 315769ce76dSAlexander Graf 316769ce76dSAlexander Graf static void curl_clean_state(CURLState *s) 317769ce76dSAlexander Graf { 318769ce76dSAlexander Graf if (s->s->multi) 319769ce76dSAlexander Graf curl_multi_remove_handle(s->s->multi, s->curl); 320769ce76dSAlexander Graf s->in_use = 0; 321769ce76dSAlexander Graf } 322769ce76dSAlexander Graf 323769ce76dSAlexander Graf static int curl_open(BlockDriverState *bs, const char *filename, int flags) 324769ce76dSAlexander Graf { 325769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 326769ce76dSAlexander Graf CURLState *state = NULL; 327769ce76dSAlexander Graf double d; 328c76f4952SNolan 329c76f4952SNolan #define RA_OPTSTR ":readahead=" 330c76f4952SNolan char *file; 331c76f4952SNolan char *ra; 332c76f4952SNolan const char *ra_val; 333c76f4952SNolan int parse_state = 0; 334c76f4952SNolan 335769ce76dSAlexander Graf static int inited = 0; 336769ce76dSAlexander Graf 3377267c094SAnthony Liguori file = g_strdup(filename); 338c76f4952SNolan s->readahead_size = READ_AHEAD_SIZE; 339c76f4952SNolan 340c76f4952SNolan /* Parse a trailing ":readahead=#:" param, if present. */ 341c76f4952SNolan ra = file + strlen(file) - 1; 342c76f4952SNolan while (ra >= file) { 343c76f4952SNolan if (parse_state == 0) { 344c76f4952SNolan if (*ra == ':') 345c76f4952SNolan parse_state++; 346c76f4952SNolan else 347c76f4952SNolan break; 348c76f4952SNolan } else if (parse_state == 1) { 349c76f4952SNolan if (*ra > '9' || *ra < '0') { 350c76f4952SNolan char *opt_start = ra - strlen(RA_OPTSTR) + 1; 351c76f4952SNolan if (opt_start > file && 352c76f4952SNolan strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) { 353c76f4952SNolan ra_val = ra + 1; 354c76f4952SNolan ra -= strlen(RA_OPTSTR) - 1; 355c76f4952SNolan *ra = '\0'; 356c76f4952SNolan s->readahead_size = atoi(ra_val); 357c76f4952SNolan break; 358c76f4952SNolan } else { 359c76f4952SNolan break; 360c76f4952SNolan } 361c76f4952SNolan } 362c76f4952SNolan } 363c76f4952SNolan ra--; 364c76f4952SNolan } 365c76f4952SNolan 366c76f4952SNolan if ((s->readahead_size & 0x1ff) != 0) { 36748a402e6Smalc fprintf(stderr, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n", 368c76f4952SNolan s->readahead_size); 369c76f4952SNolan goto out_noclean; 370c76f4952SNolan } 371c76f4952SNolan 372769ce76dSAlexander Graf if (!inited) { 373769ce76dSAlexander Graf curl_global_init(CURL_GLOBAL_ALL); 374769ce76dSAlexander Graf inited = 1; 375769ce76dSAlexander Graf } 376769ce76dSAlexander Graf 377d0f2c4c6Smalc DPRINTF("CURL: Opening %s\n", file); 378c76f4952SNolan s->url = file; 379769ce76dSAlexander Graf state = curl_init_state(s); 380769ce76dSAlexander Graf if (!state) 381769ce76dSAlexander Graf goto out_noclean; 382769ce76dSAlexander Graf 383769ce76dSAlexander Graf // Get file size 384769ce76dSAlexander Graf 385769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); 386df3cee1aSBlue Swirl curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_size_cb); 387769ce76dSAlexander Graf if (curl_easy_perform(state->curl)) 388769ce76dSAlexander Graf goto out; 389769ce76dSAlexander Graf curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d); 390df3cee1aSBlue Swirl curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb); 391769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0); 392769ce76dSAlexander Graf if (d) 393769ce76dSAlexander Graf s->len = (size_t)d; 394769ce76dSAlexander Graf else if(!s->len) 395769ce76dSAlexander Graf goto out; 3960bfcd599SBlue Swirl DPRINTF("CURL: Size = %zd\n", s->len); 397769ce76dSAlexander Graf 398769ce76dSAlexander Graf curl_clean_state(state); 399769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 400769ce76dSAlexander Graf state->curl = NULL; 401769ce76dSAlexander Graf 402769ce76dSAlexander Graf // Now we know the file exists and its size, so let's 403769ce76dSAlexander Graf // initialize the multi interface! 404769ce76dSAlexander Graf 405769ce76dSAlexander Graf s->multi = curl_multi_init(); 406769ce76dSAlexander Graf curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s); 407769ce76dSAlexander Graf curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb ); 408769ce76dSAlexander Graf curl_multi_do(s); 409769ce76dSAlexander Graf 410769ce76dSAlexander Graf return 0; 411769ce76dSAlexander Graf 412769ce76dSAlexander Graf out: 413769ce76dSAlexander Graf fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg); 414769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 415769ce76dSAlexander Graf state->curl = NULL; 416769ce76dSAlexander Graf out_noclean: 4177267c094SAnthony Liguori g_free(file); 418769ce76dSAlexander Graf return -EINVAL; 419769ce76dSAlexander Graf } 420769ce76dSAlexander Graf 421c84dcdc1SNick Thomas static int curl_aio_flush(void *opaque) 422c84dcdc1SNick Thomas { 423c84dcdc1SNick Thomas BDRVCURLState *s = opaque; 424c84dcdc1SNick Thomas int i, j; 425c84dcdc1SNick Thomas 426c84dcdc1SNick Thomas for (i=0; i < CURL_NUM_STATES; i++) { 427c84dcdc1SNick Thomas for(j=0; j < CURL_NUM_ACB; j++) { 428c84dcdc1SNick Thomas if (s->states[i].acb[j]) { 429c84dcdc1SNick Thomas return 1; 430c84dcdc1SNick Thomas } 431c84dcdc1SNick Thomas } 432c84dcdc1SNick Thomas } 433c84dcdc1SNick Thomas return 0; 434c84dcdc1SNick Thomas } 435c84dcdc1SNick Thomas 436c16b5a2cSChristoph Hellwig static void curl_aio_cancel(BlockDriverAIOCB *blockacb) 437c16b5a2cSChristoph Hellwig { 438c16b5a2cSChristoph Hellwig // Do we have to implement canceling? Seems to work without... 439c16b5a2cSChristoph Hellwig } 440c16b5a2cSChristoph Hellwig 441c16b5a2cSChristoph Hellwig static AIOPool curl_aio_pool = { 442c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(CURLAIOCB), 443c16b5a2cSChristoph Hellwig .cancel = curl_aio_cancel, 444c16b5a2cSChristoph Hellwig }; 445c16b5a2cSChristoph Hellwig 446363c3c85SNick Thomas 447363c3c85SNick Thomas static void curl_readv_bh_cb(void *p) 448769ce76dSAlexander Graf { 449769ce76dSAlexander Graf CURLState *state; 450769ce76dSAlexander Graf 451363c3c85SNick Thomas CURLAIOCB *acb = p; 452363c3c85SNick Thomas BDRVCURLState *s = acb->common.bs->opaque; 453769ce76dSAlexander Graf 454363c3c85SNick Thomas qemu_bh_delete(acb->bh); 455363c3c85SNick Thomas acb->bh = NULL; 456363c3c85SNick Thomas 457363c3c85SNick Thomas size_t start = acb->sector_num * SECTOR_SIZE; 458363c3c85SNick Thomas size_t end; 459769ce76dSAlexander Graf 460769ce76dSAlexander Graf // In case we have the requested data already (e.g. read-ahead), 461769ce76dSAlexander Graf // we can just call the callback and be done. 462363c3c85SNick Thomas switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) { 463769ce76dSAlexander Graf case FIND_RET_OK: 464769ce76dSAlexander Graf qemu_aio_release(acb); 465769ce76dSAlexander Graf // fall through 466769ce76dSAlexander Graf case FIND_RET_WAIT: 467363c3c85SNick Thomas return; 468769ce76dSAlexander Graf default: 469769ce76dSAlexander Graf break; 470769ce76dSAlexander Graf } 471769ce76dSAlexander Graf 472769ce76dSAlexander Graf // No cache found, so let's start a new request 473769ce76dSAlexander Graf state = curl_init_state(s); 474363c3c85SNick Thomas if (!state) { 475363c3c85SNick Thomas acb->common.cb(acb->common.opaque, -EIO); 476363c3c85SNick Thomas qemu_aio_release(acb); 477363c3c85SNick Thomas return; 478363c3c85SNick Thomas } 479769ce76dSAlexander Graf 480769ce76dSAlexander Graf acb->start = 0; 481363c3c85SNick Thomas acb->end = (acb->nb_sectors * SECTOR_SIZE); 482769ce76dSAlexander Graf 483769ce76dSAlexander Graf state->buf_off = 0; 484769ce76dSAlexander Graf if (state->orig_buf) 4857267c094SAnthony Liguori g_free(state->orig_buf); 486769ce76dSAlexander Graf state->buf_start = start; 487c76f4952SNolan state->buf_len = acb->end + s->readahead_size; 488769ce76dSAlexander Graf end = MIN(start + state->buf_len, s->len) - 1; 4897267c094SAnthony Liguori state->orig_buf = g_malloc(state->buf_len); 490769ce76dSAlexander Graf state->acb[0] = acb; 491769ce76dSAlexander Graf 4920bfcd599SBlue Swirl snprintf(state->range, 127, "%zd-%zd", start, end); 4930bfcd599SBlue Swirl DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n", 494363c3c85SNick Thomas (acb->nb_sectors * SECTOR_SIZE), start, state->range); 495769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); 496769ce76dSAlexander Graf 497769ce76dSAlexander Graf curl_multi_add_handle(s->multi, state->curl); 498769ce76dSAlexander Graf curl_multi_do(s); 499769ce76dSAlexander Graf 500363c3c85SNick Thomas } 501363c3c85SNick Thomas 502363c3c85SNick Thomas static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs, 503363c3c85SNick Thomas int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 504363c3c85SNick Thomas BlockDriverCompletionFunc *cb, void *opaque) 505363c3c85SNick Thomas { 506363c3c85SNick Thomas CURLAIOCB *acb; 507363c3c85SNick Thomas 508363c3c85SNick Thomas acb = qemu_aio_get(&curl_aio_pool, bs, cb, opaque); 509363c3c85SNick Thomas 510363c3c85SNick Thomas acb->qiov = qiov; 511363c3c85SNick Thomas acb->sector_num = sector_num; 512363c3c85SNick Thomas acb->nb_sectors = nb_sectors; 513363c3c85SNick Thomas 514363c3c85SNick Thomas acb->bh = qemu_bh_new(curl_readv_bh_cb, acb); 515363c3c85SNick Thomas 516363c3c85SNick Thomas if (!acb->bh) { 517363c3c85SNick Thomas DPRINTF("CURL: qemu_bh_new failed\n"); 518363c3c85SNick Thomas return NULL; 519363c3c85SNick Thomas } 520363c3c85SNick Thomas 521363c3c85SNick Thomas qemu_bh_schedule(acb->bh); 522769ce76dSAlexander Graf return &acb->common; 523769ce76dSAlexander Graf } 524769ce76dSAlexander Graf 525769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs) 526769ce76dSAlexander Graf { 527769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 528769ce76dSAlexander Graf int i; 529769ce76dSAlexander Graf 530d0f2c4c6Smalc DPRINTF("CURL: Close\n"); 531769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 532769ce76dSAlexander Graf if (s->states[i].in_use) 533769ce76dSAlexander Graf curl_clean_state(&s->states[i]); 534769ce76dSAlexander Graf if (s->states[i].curl) { 535769ce76dSAlexander Graf curl_easy_cleanup(s->states[i].curl); 536769ce76dSAlexander Graf s->states[i].curl = NULL; 537769ce76dSAlexander Graf } 538769ce76dSAlexander Graf if (s->states[i].orig_buf) { 5397267c094SAnthony Liguori g_free(s->states[i].orig_buf); 540769ce76dSAlexander Graf s->states[i].orig_buf = NULL; 541769ce76dSAlexander Graf } 542769ce76dSAlexander Graf } 543769ce76dSAlexander Graf if (s->multi) 544769ce76dSAlexander Graf curl_multi_cleanup(s->multi); 545769ce76dSAlexander Graf if (s->url) 546769ce76dSAlexander Graf free(s->url); 547769ce76dSAlexander Graf } 548769ce76dSAlexander Graf 549769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs) 550769ce76dSAlexander Graf { 551769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 552769ce76dSAlexander Graf return s->len; 553769ce76dSAlexander Graf } 554769ce76dSAlexander Graf 555769ce76dSAlexander Graf static BlockDriver bdrv_http = { 556769ce76dSAlexander Graf .format_name = "http", 557769ce76dSAlexander Graf .protocol_name = "http", 558769ce76dSAlexander Graf 559769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 56066f82ceeSKevin Wolf .bdrv_file_open = curl_open, 561769ce76dSAlexander Graf .bdrv_close = curl_close, 562769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 563769ce76dSAlexander Graf 564769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 565769ce76dSAlexander Graf }; 566769ce76dSAlexander Graf 567769ce76dSAlexander Graf static BlockDriver bdrv_https = { 568769ce76dSAlexander Graf .format_name = "https", 569769ce76dSAlexander Graf .protocol_name = "https", 570769ce76dSAlexander Graf 571769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 57266f82ceeSKevin Wolf .bdrv_file_open = curl_open, 573769ce76dSAlexander Graf .bdrv_close = curl_close, 574769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 575769ce76dSAlexander Graf 576769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 577769ce76dSAlexander Graf }; 578769ce76dSAlexander Graf 579769ce76dSAlexander Graf static BlockDriver bdrv_ftp = { 580769ce76dSAlexander Graf .format_name = "ftp", 581769ce76dSAlexander Graf .protocol_name = "ftp", 582769ce76dSAlexander Graf 583769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 58466f82ceeSKevin Wolf .bdrv_file_open = curl_open, 585769ce76dSAlexander Graf .bdrv_close = curl_close, 586769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 587769ce76dSAlexander Graf 588769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 589769ce76dSAlexander Graf }; 590769ce76dSAlexander Graf 591769ce76dSAlexander Graf static BlockDriver bdrv_ftps = { 592769ce76dSAlexander Graf .format_name = "ftps", 593769ce76dSAlexander Graf .protocol_name = "ftps", 594769ce76dSAlexander Graf 595769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 59666f82ceeSKevin Wolf .bdrv_file_open = curl_open, 597769ce76dSAlexander Graf .bdrv_close = curl_close, 598769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 599769ce76dSAlexander Graf 600769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 601769ce76dSAlexander Graf }; 602769ce76dSAlexander Graf 603769ce76dSAlexander Graf static BlockDriver bdrv_tftp = { 604769ce76dSAlexander Graf .format_name = "tftp", 605769ce76dSAlexander Graf .protocol_name = "tftp", 606769ce76dSAlexander Graf 607769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 60866f82ceeSKevin Wolf .bdrv_file_open = curl_open, 609769ce76dSAlexander Graf .bdrv_close = curl_close, 610769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 611769ce76dSAlexander Graf 612769ce76dSAlexander Graf .bdrv_aio_readv = curl_aio_readv, 613769ce76dSAlexander Graf }; 614769ce76dSAlexander Graf 615769ce76dSAlexander Graf static void curl_block_init(void) 616769ce76dSAlexander Graf { 617769ce76dSAlexander Graf bdrv_register(&bdrv_http); 618769ce76dSAlexander Graf bdrv_register(&bdrv_https); 619769ce76dSAlexander Graf bdrv_register(&bdrv_ftp); 620769ce76dSAlexander Graf bdrv_register(&bdrv_ftps); 621769ce76dSAlexander Graf bdrv_register(&bdrv_tftp); 622769ce76dSAlexander Graf } 623769ce76dSAlexander Graf 624769ce76dSAlexander Graf block_init(curl_block_init); 625