Statistics
| Branch: | Revision:

root / block / curl.c @ 57e69b7d

History | View | Annotate | Download (14.8 kB)

1 769ce76d Alexander Graf
/*
2 769ce76d Alexander Graf
 * QEMU Block driver for CURL images
3 769ce76d Alexander Graf
 *
4 769ce76d Alexander Graf
 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 769ce76d Alexander Graf
 *
6 769ce76d Alexander Graf
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 769ce76d Alexander Graf
 * of this software and associated documentation files (the "Software"), to deal
8 769ce76d Alexander Graf
 * in the Software without restriction, including without limitation the rights
9 769ce76d Alexander Graf
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 769ce76d Alexander Graf
 * copies of the Software, and to permit persons to whom the Software is
11 769ce76d Alexander Graf
 * furnished to do so, subject to the following conditions:
12 769ce76d Alexander Graf
 *
13 769ce76d Alexander Graf
 * The above copyright notice and this permission notice shall be included in
14 769ce76d Alexander Graf
 * all copies or substantial portions of the Software.
15 769ce76d Alexander Graf
 *
16 769ce76d Alexander Graf
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 769ce76d Alexander Graf
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 769ce76d Alexander Graf
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 769ce76d Alexander Graf
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 769ce76d Alexander Graf
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 769ce76d Alexander Graf
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 769ce76d Alexander Graf
 * THE SOFTWARE.
23 769ce76d Alexander Graf
 */
24 769ce76d Alexander Graf
#include "qemu-common.h"
25 769ce76d Alexander Graf
#include "block_int.h"
26 769ce76d Alexander Graf
#include <curl/curl.h>
27 769ce76d Alexander Graf
28 769ce76d Alexander Graf
// #define DEBUG
29 769ce76d Alexander Graf
// #define DEBUG_VERBOSE
30 769ce76d Alexander Graf
31 769ce76d Alexander Graf
#ifdef DEBUG_CURL
32 d0f2c4c6 malc
#define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
33 769ce76d Alexander Graf
#else
34 d0f2c4c6 malc
#define DPRINTF(fmt, ...) do { } while (0)
35 769ce76d Alexander Graf
#endif
36 769ce76d Alexander Graf
37 769ce76d Alexander Graf
#define CURL_NUM_STATES 8
38 769ce76d Alexander Graf
#define CURL_NUM_ACB    8
39 769ce76d Alexander Graf
#define SECTOR_SIZE     512
40 769ce76d Alexander Graf
#define READ_AHEAD_SIZE (256 * 1024)
41 769ce76d Alexander Graf
42 769ce76d Alexander Graf
#define FIND_RET_NONE   0
43 769ce76d Alexander Graf
#define FIND_RET_OK     1
44 769ce76d Alexander Graf
#define FIND_RET_WAIT   2
45 769ce76d Alexander Graf
46 769ce76d Alexander Graf
struct BDRVCURLState;
47 769ce76d Alexander Graf
48 769ce76d Alexander Graf
typedef struct CURLAIOCB {
49 769ce76d Alexander Graf
    BlockDriverAIOCB common;
50 769ce76d Alexander Graf
    QEMUIOVector *qiov;
51 769ce76d Alexander Graf
    size_t start;
52 769ce76d Alexander Graf
    size_t end;
53 769ce76d Alexander Graf
} CURLAIOCB;
54 769ce76d Alexander Graf
55 769ce76d Alexander Graf
typedef struct CURLState
56 769ce76d Alexander Graf
{
57 769ce76d Alexander Graf
    struct BDRVCURLState *s;
58 769ce76d Alexander Graf
    CURLAIOCB *acb[CURL_NUM_ACB];
59 769ce76d Alexander Graf
    CURL *curl;
60 769ce76d Alexander Graf
    char *orig_buf;
61 769ce76d Alexander Graf
    size_t buf_start;
62 769ce76d Alexander Graf
    size_t buf_off;
63 769ce76d Alexander Graf
    size_t buf_len;
64 769ce76d Alexander Graf
    char range[128];
65 769ce76d Alexander Graf
    char errmsg[CURL_ERROR_SIZE];
66 769ce76d Alexander Graf
    char in_use;
67 769ce76d Alexander Graf
} CURLState;
68 769ce76d Alexander Graf
69 769ce76d Alexander Graf
typedef struct BDRVCURLState {
70 769ce76d Alexander Graf
    CURLM *multi;
71 769ce76d Alexander Graf
    size_t len;
72 769ce76d Alexander Graf
    CURLState states[CURL_NUM_STATES];
73 769ce76d Alexander Graf
    char *url;
74 c76f4952 Nolan
    size_t readahead_size;
75 769ce76d Alexander Graf
} BDRVCURLState;
76 769ce76d Alexander Graf
77 769ce76d Alexander Graf
static void curl_clean_state(CURLState *s);
78 769ce76d Alexander Graf
static void curl_multi_do(void *arg);
79 769ce76d Alexander Graf
80 769ce76d Alexander Graf
static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
81 769ce76d Alexander Graf
                        void *s, void *sp)
82 769ce76d Alexander Graf
{
83 d0f2c4c6 malc
    DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
84 769ce76d Alexander Graf
    switch (action) {
85 769ce76d Alexander Graf
        case CURL_POLL_IN:
86 8febfa26 Kevin Wolf
            qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, NULL, NULL, s);
87 769ce76d Alexander Graf
            break;
88 769ce76d Alexander Graf
        case CURL_POLL_OUT:
89 8febfa26 Kevin Wolf
            qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, NULL, NULL, s);
90 769ce76d Alexander Graf
            break;
91 769ce76d Alexander Graf
        case CURL_POLL_INOUT:
92 769ce76d Alexander Graf
            qemu_aio_set_fd_handler(fd, curl_multi_do,
93 8febfa26 Kevin Wolf
                                    curl_multi_do, NULL, NULL, s);
94 769ce76d Alexander Graf
            break;
95 769ce76d Alexander Graf
        case CURL_POLL_REMOVE:
96 8febfa26 Kevin Wolf
            qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL, NULL);
97 769ce76d Alexander Graf
            break;
98 769ce76d Alexander Graf
    }
99 769ce76d Alexander Graf
100 769ce76d Alexander Graf
    return 0;
101 769ce76d Alexander Graf
}
102 769ce76d Alexander Graf
103 769ce76d Alexander Graf
static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
104 769ce76d Alexander Graf
{
105 769ce76d Alexander Graf
    CURLState *s = ((CURLState*)opaque);
106 769ce76d Alexander Graf
    size_t realsize = size * nmemb;
107 769ce76d Alexander Graf
    long long fsize;
108 769ce76d Alexander Graf
109 769ce76d Alexander Graf
    if(sscanf(ptr, "Content-Length: %lld", &fsize) == 1)
110 769ce76d Alexander Graf
        s->s->len = fsize;
111 769ce76d Alexander Graf
112 769ce76d Alexander Graf
    return realsize;
113 769ce76d Alexander Graf
}
114 769ce76d Alexander Graf
115 769ce76d Alexander Graf
static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
116 769ce76d Alexander Graf
{
117 769ce76d Alexander Graf
    CURLState *s = ((CURLState*)opaque);
118 769ce76d Alexander Graf
    size_t realsize = size * nmemb;
119 769ce76d Alexander Graf
    int i;
120 769ce76d Alexander Graf
121 d0f2c4c6 malc
    DPRINTF("CURL: Just reading %lld bytes\n", (unsigned long long)realsize);
122 769ce76d Alexander Graf
123 769ce76d Alexander Graf
    if (!s || !s->orig_buf)
124 769ce76d Alexander Graf
        goto read_end;
125 769ce76d Alexander Graf
126 769ce76d Alexander Graf
    memcpy(s->orig_buf + s->buf_off, ptr, realsize);
127 769ce76d Alexander Graf
    s->buf_off += realsize;
128 769ce76d Alexander Graf
129 769ce76d Alexander Graf
    for(i=0; i<CURL_NUM_ACB; i++) {
130 769ce76d Alexander Graf
        CURLAIOCB *acb = s->acb[i];
131 769ce76d Alexander Graf
132 769ce76d Alexander Graf
        if (!acb)
133 769ce76d Alexander Graf
            continue;
134 769ce76d Alexander Graf
135 769ce76d Alexander Graf
        if ((s->buf_off >= acb->end)) {
136 769ce76d Alexander Graf
            qemu_iovec_from_buffer(acb->qiov, s->orig_buf + acb->start,
137 769ce76d Alexander Graf
                                   acb->end - acb->start);
138 769ce76d Alexander Graf
            acb->common.cb(acb->common.opaque, 0);
139 769ce76d Alexander Graf
            qemu_aio_release(acb);
140 769ce76d Alexander Graf
            s->acb[i] = NULL;
141 769ce76d Alexander Graf
        }
142 769ce76d Alexander Graf
    }
143 769ce76d Alexander Graf
144 769ce76d Alexander Graf
read_end:
145 769ce76d Alexander Graf
    return realsize;
146 769ce76d Alexander Graf
}
147 769ce76d Alexander Graf
148 769ce76d Alexander Graf
static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
149 769ce76d Alexander Graf
                         CURLAIOCB *acb)
150 769ce76d Alexander Graf
{
151 769ce76d Alexander Graf
    int i;
152 769ce76d Alexander Graf
    size_t end = start + len;
153 769ce76d Alexander Graf
154 769ce76d Alexander Graf
    for (i=0; i<CURL_NUM_STATES; i++) {
155 769ce76d Alexander Graf
        CURLState *state = &s->states[i];
156 769ce76d Alexander Graf
        size_t buf_end = (state->buf_start + state->buf_off);
157 769ce76d Alexander Graf
        size_t buf_fend = (state->buf_start + state->buf_len);
158 769ce76d Alexander Graf
159 769ce76d Alexander Graf
        if (!state->orig_buf)
160 769ce76d Alexander Graf
            continue;
161 769ce76d Alexander Graf
        if (!state->buf_off)
162 769ce76d Alexander Graf
            continue;
163 769ce76d Alexander Graf
164 769ce76d Alexander Graf
        // Does the existing buffer cover our section?
165 769ce76d Alexander Graf
        if ((start >= state->buf_start) &&
166 769ce76d Alexander Graf
            (start <= buf_end) &&
167 769ce76d Alexander Graf
            (end >= state->buf_start) &&
168 769ce76d Alexander Graf
            (end <= buf_end))
169 769ce76d Alexander Graf
        {
170 769ce76d Alexander Graf
            char *buf = state->orig_buf + (start - state->buf_start);
171 769ce76d Alexander Graf
172 769ce76d Alexander Graf
            qemu_iovec_from_buffer(acb->qiov, buf, len);
173 769ce76d Alexander Graf
            acb->common.cb(acb->common.opaque, 0);
174 769ce76d Alexander Graf
175 769ce76d Alexander Graf
            return FIND_RET_OK;
176 769ce76d Alexander Graf
        }
177 769ce76d Alexander Graf
178 769ce76d Alexander Graf
        // Wait for unfinished chunks
179 769ce76d Alexander Graf
        if ((start >= state->buf_start) &&
180 769ce76d Alexander Graf
            (start <= buf_fend) &&
181 769ce76d Alexander Graf
            (end >= state->buf_start) &&
182 769ce76d Alexander Graf
            (end <= buf_fend))
183 769ce76d Alexander Graf
        {
184 769ce76d Alexander Graf
            int j;
185 769ce76d Alexander Graf
186 769ce76d Alexander Graf
            acb->start = start - state->buf_start;
187 769ce76d Alexander Graf
            acb->end = acb->start + len;
188 769ce76d Alexander Graf
189 769ce76d Alexander Graf
            for (j=0; j<CURL_NUM_ACB; j++) {
190 769ce76d Alexander Graf
                if (!state->acb[j]) {
191 769ce76d Alexander Graf
                    state->acb[j] = acb;
192 769ce76d Alexander Graf
                    return FIND_RET_WAIT;
193 769ce76d Alexander Graf
                }
194 769ce76d Alexander Graf
            }
195 769ce76d Alexander Graf
        }
196 769ce76d Alexander Graf
    }
197 769ce76d Alexander Graf
198 769ce76d Alexander Graf
    return FIND_RET_NONE;
199 769ce76d Alexander Graf
}
200 769ce76d Alexander Graf
201 769ce76d Alexander Graf
static void curl_multi_do(void *arg)
202 769ce76d Alexander Graf
{
203 769ce76d Alexander Graf
    BDRVCURLState *s = (BDRVCURLState *)arg;
204 769ce76d Alexander Graf
    int running;
205 769ce76d Alexander Graf
    int r;
206 769ce76d Alexander Graf
    int msgs_in_queue;
207 769ce76d Alexander Graf
208 769ce76d Alexander Graf
    if (!s->multi)
209 769ce76d Alexander Graf
        return;
210 769ce76d Alexander Graf
211 769ce76d Alexander Graf
    do {
212 769ce76d Alexander Graf
        r = curl_multi_socket_all(s->multi, &running);
213 769ce76d Alexander Graf
    } while(r == CURLM_CALL_MULTI_PERFORM);
214 769ce76d Alexander Graf
215 769ce76d Alexander Graf
    /* Try to find done transfers, so we can free the easy
216 769ce76d Alexander Graf
     * handle again. */
217 769ce76d Alexander Graf
    do {
218 769ce76d Alexander Graf
        CURLMsg *msg;
219 769ce76d Alexander Graf
        msg = curl_multi_info_read(s->multi, &msgs_in_queue);
220 769ce76d Alexander Graf
221 769ce76d Alexander Graf
        if (!msg)
222 769ce76d Alexander Graf
            break;
223 769ce76d Alexander Graf
        if (msg->msg == CURLMSG_NONE)
224 769ce76d Alexander Graf
            break;
225 769ce76d Alexander Graf
226 769ce76d Alexander Graf
        switch (msg->msg) {
227 769ce76d Alexander Graf
            case CURLMSG_DONE:
228 769ce76d Alexander Graf
            {
229 769ce76d Alexander Graf
                CURLState *state = NULL;
230 769ce76d Alexander Graf
                curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
231 769ce76d Alexander Graf
                curl_clean_state(state);
232 769ce76d Alexander Graf
                break;
233 769ce76d Alexander Graf
            }
234 769ce76d Alexander Graf
            default:
235 769ce76d Alexander Graf
                msgs_in_queue = 0;
236 769ce76d Alexander Graf
                break;
237 769ce76d Alexander Graf
        }
238 769ce76d Alexander Graf
    } while(msgs_in_queue);
239 769ce76d Alexander Graf
}
240 769ce76d Alexander Graf
241 769ce76d Alexander Graf
static CURLState *curl_init_state(BDRVCURLState *s)
242 769ce76d Alexander Graf
{
243 769ce76d Alexander Graf
    CURLState *state = NULL;
244 769ce76d Alexander Graf
    int i, j;
245 769ce76d Alexander Graf
246 769ce76d Alexander Graf
    do {
247 769ce76d Alexander Graf
        for (i=0; i<CURL_NUM_STATES; i++) {
248 769ce76d Alexander Graf
            for (j=0; j<CURL_NUM_ACB; j++)
249 769ce76d Alexander Graf
                if (s->states[i].acb[j])
250 769ce76d Alexander Graf
                    continue;
251 769ce76d Alexander Graf
            if (s->states[i].in_use)
252 769ce76d Alexander Graf
                continue;
253 769ce76d Alexander Graf
254 769ce76d Alexander Graf
            state = &s->states[i];
255 769ce76d Alexander Graf
            state->in_use = 1;
256 769ce76d Alexander Graf
            break;
257 769ce76d Alexander Graf
        }
258 769ce76d Alexander Graf
        if (!state) {
259 769ce76d Alexander Graf
            usleep(100);
260 769ce76d Alexander Graf
            curl_multi_do(s);
261 769ce76d Alexander Graf
        }
262 769ce76d Alexander Graf
    } while(!state);
263 769ce76d Alexander Graf
264 769ce76d Alexander Graf
    if (state->curl)
265 769ce76d Alexander Graf
        goto has_curl;
266 769ce76d Alexander Graf
267 769ce76d Alexander Graf
    state->curl = curl_easy_init();
268 769ce76d Alexander Graf
    if (!state->curl)
269 769ce76d Alexander Graf
        return NULL;
270 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
271 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
272 df3cee1a Blue Swirl
    curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
273 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
274 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
275 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
276 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
277 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
278 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
279 769ce76d Alexander Graf
    
280 769ce76d Alexander Graf
#ifdef DEBUG_VERBOSE
281 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
282 769ce76d Alexander Graf
#endif
283 769ce76d Alexander Graf
284 769ce76d Alexander Graf
has_curl:
285 769ce76d Alexander Graf
286 769ce76d Alexander Graf
    state->s = s;
287 769ce76d Alexander Graf
288 769ce76d Alexander Graf
    return state;
289 769ce76d Alexander Graf
}
290 769ce76d Alexander Graf
291 769ce76d Alexander Graf
static void curl_clean_state(CURLState *s)
292 769ce76d Alexander Graf
{
293 769ce76d Alexander Graf
    if (s->s->multi)
294 769ce76d Alexander Graf
        curl_multi_remove_handle(s->s->multi, s->curl);
295 769ce76d Alexander Graf
    s->in_use = 0;
296 769ce76d Alexander Graf
}
297 769ce76d Alexander Graf
298 769ce76d Alexander Graf
static int curl_open(BlockDriverState *bs, const char *filename, int flags)
299 769ce76d Alexander Graf
{
300 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
301 769ce76d Alexander Graf
    CURLState *state = NULL;
302 769ce76d Alexander Graf
    double d;
303 c76f4952 Nolan
304 c76f4952 Nolan
    #define RA_OPTSTR ":readahead="
305 c76f4952 Nolan
    char *file;
306 c76f4952 Nolan
    char *ra;
307 c76f4952 Nolan
    const char *ra_val;
308 c76f4952 Nolan
    int parse_state = 0;
309 c76f4952 Nolan
310 769ce76d Alexander Graf
    static int inited = 0;
311 769ce76d Alexander Graf
312 6265eb26 Jim Meyering
    file = qemu_strdup(filename);
313 c76f4952 Nolan
    s->readahead_size = READ_AHEAD_SIZE;
314 c76f4952 Nolan
315 c76f4952 Nolan
    /* Parse a trailing ":readahead=#:" param, if present. */
316 c76f4952 Nolan
    ra = file + strlen(file) - 1;
317 c76f4952 Nolan
    while (ra >= file) {
318 c76f4952 Nolan
        if (parse_state == 0) {
319 c76f4952 Nolan
            if (*ra == ':')
320 c76f4952 Nolan
                parse_state++;
321 c76f4952 Nolan
            else
322 c76f4952 Nolan
                break;
323 c76f4952 Nolan
        } else if (parse_state == 1) {
324 c76f4952 Nolan
            if (*ra > '9' || *ra < '0') {
325 c76f4952 Nolan
                char *opt_start = ra - strlen(RA_OPTSTR) + 1;
326 c76f4952 Nolan
                if (opt_start > file &&
327 c76f4952 Nolan
                    strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) {
328 c76f4952 Nolan
                    ra_val = ra + 1;
329 c76f4952 Nolan
                    ra -= strlen(RA_OPTSTR) - 1;
330 c76f4952 Nolan
                    *ra = '\0';
331 c76f4952 Nolan
                    s->readahead_size = atoi(ra_val);
332 c76f4952 Nolan
                    break;
333 c76f4952 Nolan
                } else {
334 c76f4952 Nolan
                    break;
335 c76f4952 Nolan
                }
336 c76f4952 Nolan
            }
337 c76f4952 Nolan
        }
338 c76f4952 Nolan
        ra--;
339 c76f4952 Nolan
    }
340 c76f4952 Nolan
341 c76f4952 Nolan
    if ((s->readahead_size & 0x1ff) != 0) {
342 48a402e6 malc
        fprintf(stderr, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n",
343 c76f4952 Nolan
                s->readahead_size);
344 c76f4952 Nolan
        goto out_noclean;
345 c76f4952 Nolan
    }
346 c76f4952 Nolan
347 769ce76d Alexander Graf
    if (!inited) {
348 769ce76d Alexander Graf
        curl_global_init(CURL_GLOBAL_ALL);
349 769ce76d Alexander Graf
        inited = 1;
350 769ce76d Alexander Graf
    }
351 769ce76d Alexander Graf
352 d0f2c4c6 malc
    DPRINTF("CURL: Opening %s\n", file);
353 c76f4952 Nolan
    s->url = file;
354 769ce76d Alexander Graf
    state = curl_init_state(s);
355 769ce76d Alexander Graf
    if (!state)
356 769ce76d Alexander Graf
        goto out_noclean;
357 769ce76d Alexander Graf
358 769ce76d Alexander Graf
    // Get file size
359 769ce76d Alexander Graf
360 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
361 df3cee1a Blue Swirl
    curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_size_cb);
362 769ce76d Alexander Graf
    if (curl_easy_perform(state->curl))
363 769ce76d Alexander Graf
        goto out;
364 769ce76d Alexander Graf
    curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
365 df3cee1a Blue Swirl
    curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
366 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0);
367 769ce76d Alexander Graf
    if (d)
368 769ce76d Alexander Graf
        s->len = (size_t)d;
369 769ce76d Alexander Graf
    else if(!s->len)
370 769ce76d Alexander Graf
        goto out;
371 d0f2c4c6 malc
    DPRINTF("CURL: Size = %lld\n", (long long)s->len);
372 769ce76d Alexander Graf
373 769ce76d Alexander Graf
    curl_clean_state(state);
374 769ce76d Alexander Graf
    curl_easy_cleanup(state->curl);
375 769ce76d Alexander Graf
    state->curl = NULL;
376 769ce76d Alexander Graf
377 769ce76d Alexander Graf
    // Now we know the file exists and its size, so let's
378 769ce76d Alexander Graf
    // initialize the multi interface!
379 769ce76d Alexander Graf
380 769ce76d Alexander Graf
    s->multi = curl_multi_init();
381 769ce76d Alexander Graf
    curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s); 
382 769ce76d Alexander Graf
    curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb ); 
383 769ce76d Alexander Graf
    curl_multi_do(s);
384 769ce76d Alexander Graf
385 769ce76d Alexander Graf
    return 0;
386 769ce76d Alexander Graf
387 769ce76d Alexander Graf
out:
388 769ce76d Alexander Graf
    fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
389 769ce76d Alexander Graf
    curl_easy_cleanup(state->curl);
390 769ce76d Alexander Graf
    state->curl = NULL;
391 769ce76d Alexander Graf
out_noclean:
392 c76f4952 Nolan
    qemu_free(file);
393 769ce76d Alexander Graf
    return -EINVAL;
394 769ce76d Alexander Graf
}
395 769ce76d Alexander Graf
396 c16b5a2c Christoph Hellwig
static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
397 c16b5a2c Christoph Hellwig
{
398 c16b5a2c Christoph Hellwig
    // Do we have to implement canceling? Seems to work without...
399 c16b5a2c Christoph Hellwig
}
400 c16b5a2c Christoph Hellwig
401 c16b5a2c Christoph Hellwig
static AIOPool curl_aio_pool = {
402 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(CURLAIOCB),
403 c16b5a2c Christoph Hellwig
    .cancel             = curl_aio_cancel,
404 c16b5a2c Christoph Hellwig
};
405 c16b5a2c Christoph Hellwig
406 769ce76d Alexander Graf
static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
407 769ce76d Alexander Graf
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
408 769ce76d Alexander Graf
        BlockDriverCompletionFunc *cb, void *opaque)
409 769ce76d Alexander Graf
{
410 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
411 769ce76d Alexander Graf
    CURLAIOCB *acb;
412 769ce76d Alexander Graf
    size_t start = sector_num * SECTOR_SIZE;
413 769ce76d Alexander Graf
    size_t end;
414 769ce76d Alexander Graf
    CURLState *state;
415 769ce76d Alexander Graf
416 c16b5a2c Christoph Hellwig
    acb = qemu_aio_get(&curl_aio_pool, bs, cb, opaque);
417 769ce76d Alexander Graf
    if (!acb)
418 769ce76d Alexander Graf
        return NULL;
419 769ce76d Alexander Graf
420 769ce76d Alexander Graf
    acb->qiov = qiov;
421 769ce76d Alexander Graf
422 769ce76d Alexander Graf
    // In case we have the requested data already (e.g. read-ahead),
423 769ce76d Alexander Graf
    // we can just call the callback and be done.
424 769ce76d Alexander Graf
425 769ce76d Alexander Graf
    switch (curl_find_buf(s, start, nb_sectors * SECTOR_SIZE, acb)) {
426 769ce76d Alexander Graf
        case FIND_RET_OK:
427 769ce76d Alexander Graf
            qemu_aio_release(acb);
428 769ce76d Alexander Graf
            // fall through
429 769ce76d Alexander Graf
        case FIND_RET_WAIT:
430 769ce76d Alexander Graf
            return &acb->common;
431 769ce76d Alexander Graf
        default:
432 769ce76d Alexander Graf
            break;
433 769ce76d Alexander Graf
    }
434 769ce76d Alexander Graf
435 769ce76d Alexander Graf
    // No cache found, so let's start a new request
436 769ce76d Alexander Graf
437 769ce76d Alexander Graf
    state = curl_init_state(s);
438 769ce76d Alexander Graf
    if (!state)
439 769ce76d Alexander Graf
        return NULL;
440 769ce76d Alexander Graf
441 769ce76d Alexander Graf
    acb->start = 0;
442 769ce76d Alexander Graf
    acb->end = (nb_sectors * SECTOR_SIZE);
443 769ce76d Alexander Graf
444 769ce76d Alexander Graf
    state->buf_off = 0;
445 769ce76d Alexander Graf
    if (state->orig_buf)
446 769ce76d Alexander Graf
        qemu_free(state->orig_buf);
447 769ce76d Alexander Graf
    state->buf_start = start;
448 c76f4952 Nolan
    state->buf_len = acb->end + s->readahead_size;
449 769ce76d Alexander Graf
    end = MIN(start + state->buf_len, s->len) - 1;
450 769ce76d Alexander Graf
    state->orig_buf = qemu_malloc(state->buf_len);
451 769ce76d Alexander Graf
    state->acb[0] = acb;
452 769ce76d Alexander Graf
453 769ce76d Alexander Graf
    snprintf(state->range, 127, "%lld-%lld", (long long)start, (long long)end);
454 d0f2c4c6 malc
    DPRINTF("CURL (AIO): Reading %d at %lld (%s)\n", (nb_sectors * SECTOR_SIZE), start, state->range);
455 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
456 769ce76d Alexander Graf
457 769ce76d Alexander Graf
    curl_multi_add_handle(s->multi, state->curl);
458 769ce76d Alexander Graf
    curl_multi_do(s);
459 769ce76d Alexander Graf
460 769ce76d Alexander Graf
    return &acb->common;
461 769ce76d Alexander Graf
}
462 769ce76d Alexander Graf
463 769ce76d Alexander Graf
static void curl_close(BlockDriverState *bs)
464 769ce76d Alexander Graf
{
465 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
466 769ce76d Alexander Graf
    int i;
467 769ce76d Alexander Graf
468 d0f2c4c6 malc
    DPRINTF("CURL: Close\n");
469 769ce76d Alexander Graf
    for (i=0; i<CURL_NUM_STATES; i++) {
470 769ce76d Alexander Graf
        if (s->states[i].in_use)
471 769ce76d Alexander Graf
            curl_clean_state(&s->states[i]);
472 769ce76d Alexander Graf
        if (s->states[i].curl) {
473 769ce76d Alexander Graf
            curl_easy_cleanup(s->states[i].curl);
474 769ce76d Alexander Graf
            s->states[i].curl = NULL;
475 769ce76d Alexander Graf
        }
476 769ce76d Alexander Graf
        if (s->states[i].orig_buf) {
477 769ce76d Alexander Graf
            qemu_free(s->states[i].orig_buf);
478 769ce76d Alexander Graf
            s->states[i].orig_buf = NULL;
479 769ce76d Alexander Graf
        }
480 769ce76d Alexander Graf
    }
481 769ce76d Alexander Graf
    if (s->multi)
482 769ce76d Alexander Graf
        curl_multi_cleanup(s->multi);
483 769ce76d Alexander Graf
    if (s->url)
484 769ce76d Alexander Graf
        free(s->url);
485 769ce76d Alexander Graf
}
486 769ce76d Alexander Graf
487 769ce76d Alexander Graf
static int64_t curl_getlength(BlockDriverState *bs)
488 769ce76d Alexander Graf
{
489 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
490 769ce76d Alexander Graf
    return s->len;
491 769ce76d Alexander Graf
}
492 769ce76d Alexander Graf
493 769ce76d Alexander Graf
static BlockDriver bdrv_http = {
494 769ce76d Alexander Graf
    .format_name     = "http",
495 769ce76d Alexander Graf
    .protocol_name   = "http",
496 769ce76d Alexander Graf
497 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
498 769ce76d Alexander Graf
    .bdrv_open       = curl_open,
499 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
500 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
501 769ce76d Alexander Graf
502 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
503 769ce76d Alexander Graf
};
504 769ce76d Alexander Graf
505 769ce76d Alexander Graf
static BlockDriver bdrv_https = {
506 769ce76d Alexander Graf
    .format_name     = "https",
507 769ce76d Alexander Graf
    .protocol_name   = "https",
508 769ce76d Alexander Graf
509 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
510 769ce76d Alexander Graf
    .bdrv_open       = curl_open,
511 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
512 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
513 769ce76d Alexander Graf
514 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
515 769ce76d Alexander Graf
};
516 769ce76d Alexander Graf
517 769ce76d Alexander Graf
static BlockDriver bdrv_ftp = {
518 769ce76d Alexander Graf
    .format_name     = "ftp",
519 769ce76d Alexander Graf
    .protocol_name   = "ftp",
520 769ce76d Alexander Graf
521 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
522 769ce76d Alexander Graf
    .bdrv_open       = curl_open,
523 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
524 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
525 769ce76d Alexander Graf
526 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
527 769ce76d Alexander Graf
};
528 769ce76d Alexander Graf
529 769ce76d Alexander Graf
static BlockDriver bdrv_ftps = {
530 769ce76d Alexander Graf
    .format_name     = "ftps",
531 769ce76d Alexander Graf
    .protocol_name   = "ftps",
532 769ce76d Alexander Graf
533 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
534 769ce76d Alexander Graf
    .bdrv_open       = curl_open,
535 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
536 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
537 769ce76d Alexander Graf
538 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
539 769ce76d Alexander Graf
};
540 769ce76d Alexander Graf
541 769ce76d Alexander Graf
static BlockDriver bdrv_tftp = {
542 769ce76d Alexander Graf
    .format_name     = "tftp",
543 769ce76d Alexander Graf
    .protocol_name   = "tftp",
544 769ce76d Alexander Graf
545 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
546 769ce76d Alexander Graf
    .bdrv_open       = curl_open,
547 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
548 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
549 769ce76d Alexander Graf
550 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
551 769ce76d Alexander Graf
};
552 769ce76d Alexander Graf
553 769ce76d Alexander Graf
static void curl_block_init(void)
554 769ce76d Alexander Graf
{
555 769ce76d Alexander Graf
    bdrv_register(&bdrv_http);
556 769ce76d Alexander Graf
    bdrv_register(&bdrv_https);
557 769ce76d Alexander Graf
    bdrv_register(&bdrv_ftp);
558 769ce76d Alexander Graf
    bdrv_register(&bdrv_ftps);
559 769ce76d Alexander Graf
    bdrv_register(&bdrv_tftp);
560 769ce76d Alexander Graf
}
561 769ce76d Alexander Graf
562 769ce76d Alexander Graf
block_init(curl_block_init);