Statistics
| Branch: | Revision:

root / block / curl.c @ cda9046b

History | View | Annotate | Download (13.6 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 769ce76d Alexander Graf
#define dprintf(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
33 769ce76d Alexander Graf
#else
34 769ce76d Alexander Graf
#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 769ce76d Alexander Graf
} BDRVCURLState;
75 769ce76d Alexander Graf
76 769ce76d Alexander Graf
static void curl_clean_state(CURLState *s);
77 769ce76d Alexander Graf
static void curl_multi_do(void *arg);
78 769ce76d Alexander Graf
79 769ce76d Alexander Graf
static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
80 769ce76d Alexander Graf
                        void *s, void *sp)
81 769ce76d Alexander Graf
{
82 769ce76d Alexander Graf
    dprintf("CURL (AIO): Sock action %d on fd %d\n", action, fd);
83 769ce76d Alexander Graf
    switch (action) {
84 769ce76d Alexander Graf
        case CURL_POLL_IN:
85 769ce76d Alexander Graf
            qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, NULL, s);
86 769ce76d Alexander Graf
            break;
87 769ce76d Alexander Graf
        case CURL_POLL_OUT:
88 769ce76d Alexander Graf
            qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, NULL, s);
89 769ce76d Alexander Graf
            break;
90 769ce76d Alexander Graf
        case CURL_POLL_INOUT:
91 769ce76d Alexander Graf
            qemu_aio_set_fd_handler(fd, curl_multi_do,
92 769ce76d Alexander Graf
                                    curl_multi_do, NULL, s);
93 769ce76d Alexander Graf
            break;
94 769ce76d Alexander Graf
        case CURL_POLL_REMOVE:
95 769ce76d Alexander Graf
            qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL);
96 769ce76d Alexander Graf
            break;
97 769ce76d Alexander Graf
    }
98 769ce76d Alexander Graf
99 769ce76d Alexander Graf
    return 0;
100 769ce76d Alexander Graf
}
101 769ce76d Alexander Graf
102 769ce76d Alexander Graf
static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
103 769ce76d Alexander Graf
{
104 769ce76d Alexander Graf
    CURLState *s = ((CURLState*)opaque);
105 769ce76d Alexander Graf
    size_t realsize = size * nmemb;
106 769ce76d Alexander Graf
    long long fsize;
107 769ce76d Alexander Graf
108 769ce76d Alexander Graf
    if(sscanf(ptr, "Content-Length: %lld", &fsize) == 1)
109 769ce76d Alexander Graf
        s->s->len = fsize;
110 769ce76d Alexander Graf
111 769ce76d Alexander Graf
    return realsize;
112 769ce76d Alexander Graf
}
113 769ce76d Alexander Graf
114 769ce76d Alexander Graf
static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
115 769ce76d Alexander Graf
{
116 769ce76d Alexander Graf
    CURLState *s = ((CURLState*)opaque);
117 769ce76d Alexander Graf
    size_t realsize = size * nmemb;
118 769ce76d Alexander Graf
    int i;
119 769ce76d Alexander Graf
120 769ce76d Alexander Graf
    dprintf("CURL: Just reading %lld bytes\n", (unsigned long long)realsize);
121 769ce76d Alexander Graf
122 769ce76d Alexander Graf
    if (!s || !s->orig_buf)
123 769ce76d Alexander Graf
        goto read_end;
124 769ce76d Alexander Graf
125 769ce76d Alexander Graf
    memcpy(s->orig_buf + s->buf_off, ptr, realsize);
126 769ce76d Alexander Graf
    s->buf_off += realsize;
127 769ce76d Alexander Graf
128 769ce76d Alexander Graf
    for(i=0; i<CURL_NUM_ACB; i++) {
129 769ce76d Alexander Graf
        CURLAIOCB *acb = s->acb[i];
130 769ce76d Alexander Graf
131 769ce76d Alexander Graf
        if (!acb)
132 769ce76d Alexander Graf
            continue;
133 769ce76d Alexander Graf
134 769ce76d Alexander Graf
        if ((s->buf_off >= acb->end)) {
135 769ce76d Alexander Graf
            qemu_iovec_from_buffer(acb->qiov, s->orig_buf + acb->start,
136 769ce76d Alexander Graf
                                   acb->end - acb->start);
137 769ce76d Alexander Graf
            acb->common.cb(acb->common.opaque, 0);
138 769ce76d Alexander Graf
            qemu_aio_release(acb);
139 769ce76d Alexander Graf
            s->acb[i] = NULL;
140 769ce76d Alexander Graf
        }
141 769ce76d Alexander Graf
    }
142 769ce76d Alexander Graf
143 769ce76d Alexander Graf
read_end:
144 769ce76d Alexander Graf
    return realsize;
145 769ce76d Alexander Graf
}
146 769ce76d Alexander Graf
147 769ce76d Alexander Graf
static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
148 769ce76d Alexander Graf
                         CURLAIOCB *acb)
149 769ce76d Alexander Graf
{
150 769ce76d Alexander Graf
    int i;
151 769ce76d Alexander Graf
    size_t end = start + len;
152 769ce76d Alexander Graf
153 769ce76d Alexander Graf
    for (i=0; i<CURL_NUM_STATES; i++) {
154 769ce76d Alexander Graf
        CURLState *state = &s->states[i];
155 769ce76d Alexander Graf
        size_t buf_end = (state->buf_start + state->buf_off);
156 769ce76d Alexander Graf
        size_t buf_fend = (state->buf_start + state->buf_len);
157 769ce76d Alexander Graf
158 769ce76d Alexander Graf
        if (!state->orig_buf)
159 769ce76d Alexander Graf
            continue;
160 769ce76d Alexander Graf
        if (!state->buf_off)
161 769ce76d Alexander Graf
            continue;
162 769ce76d Alexander Graf
163 769ce76d Alexander Graf
        // Does the existing buffer cover our section?
164 769ce76d Alexander Graf
        if ((start >= state->buf_start) &&
165 769ce76d Alexander Graf
            (start <= buf_end) &&
166 769ce76d Alexander Graf
            (end >= state->buf_start) &&
167 769ce76d Alexander Graf
            (end <= buf_end))
168 769ce76d Alexander Graf
        {
169 769ce76d Alexander Graf
            char *buf = state->orig_buf + (start - state->buf_start);
170 769ce76d Alexander Graf
171 769ce76d Alexander Graf
            qemu_iovec_from_buffer(acb->qiov, buf, len);
172 769ce76d Alexander Graf
            acb->common.cb(acb->common.opaque, 0);
173 769ce76d Alexander Graf
174 769ce76d Alexander Graf
            return FIND_RET_OK;
175 769ce76d Alexander Graf
        }
176 769ce76d Alexander Graf
177 769ce76d Alexander Graf
        // Wait for unfinished chunks
178 769ce76d Alexander Graf
        if ((start >= state->buf_start) &&
179 769ce76d Alexander Graf
            (start <= buf_fend) &&
180 769ce76d Alexander Graf
            (end >= state->buf_start) &&
181 769ce76d Alexander Graf
            (end <= buf_fend))
182 769ce76d Alexander Graf
        {
183 769ce76d Alexander Graf
            int j;
184 769ce76d Alexander Graf
185 769ce76d Alexander Graf
            acb->start = start - state->buf_start;
186 769ce76d Alexander Graf
            acb->end = acb->start + len;
187 769ce76d Alexander Graf
188 769ce76d Alexander Graf
            for (j=0; j<CURL_NUM_ACB; j++) {
189 769ce76d Alexander Graf
                if (!state->acb[j]) {
190 769ce76d Alexander Graf
                    state->acb[j] = acb;
191 769ce76d Alexander Graf
                    return FIND_RET_WAIT;
192 769ce76d Alexander Graf
                }
193 769ce76d Alexander Graf
            }
194 769ce76d Alexander Graf
        }
195 769ce76d Alexander Graf
    }
196 769ce76d Alexander Graf
197 769ce76d Alexander Graf
    return FIND_RET_NONE;
198 769ce76d Alexander Graf
}
199 769ce76d Alexander Graf
200 769ce76d Alexander Graf
static void curl_multi_do(void *arg)
201 769ce76d Alexander Graf
{
202 769ce76d Alexander Graf
    BDRVCURLState *s = (BDRVCURLState *)arg;
203 769ce76d Alexander Graf
    int running;
204 769ce76d Alexander Graf
    int r;
205 769ce76d Alexander Graf
    int msgs_in_queue;
206 769ce76d Alexander Graf
207 769ce76d Alexander Graf
    if (!s->multi)
208 769ce76d Alexander Graf
        return;
209 769ce76d Alexander Graf
210 769ce76d Alexander Graf
    do {
211 769ce76d Alexander Graf
        r = curl_multi_socket_all(s->multi, &running);
212 769ce76d Alexander Graf
    } while(r == CURLM_CALL_MULTI_PERFORM);
213 769ce76d Alexander Graf
214 769ce76d Alexander Graf
    /* Try to find done transfers, so we can free the easy
215 769ce76d Alexander Graf
     * handle again. */
216 769ce76d Alexander Graf
    do {
217 769ce76d Alexander Graf
        CURLMsg *msg;
218 769ce76d Alexander Graf
        msg = curl_multi_info_read(s->multi, &msgs_in_queue);
219 769ce76d Alexander Graf
220 769ce76d Alexander Graf
        if (!msg)
221 769ce76d Alexander Graf
            break;
222 769ce76d Alexander Graf
        if (msg->msg == CURLMSG_NONE)
223 769ce76d Alexander Graf
            break;
224 769ce76d Alexander Graf
225 769ce76d Alexander Graf
        switch (msg->msg) {
226 769ce76d Alexander Graf
            case CURLMSG_DONE:
227 769ce76d Alexander Graf
            {
228 769ce76d Alexander Graf
                CURLState *state = NULL;
229 769ce76d Alexander Graf
                curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
230 769ce76d Alexander Graf
                curl_clean_state(state);
231 769ce76d Alexander Graf
                break;
232 769ce76d Alexander Graf
            }
233 769ce76d Alexander Graf
            default:
234 769ce76d Alexander Graf
                msgs_in_queue = 0;
235 769ce76d Alexander Graf
                break;
236 769ce76d Alexander Graf
        }
237 769ce76d Alexander Graf
    } while(msgs_in_queue);
238 769ce76d Alexander Graf
}
239 769ce76d Alexander Graf
240 769ce76d Alexander Graf
static CURLState *curl_init_state(BDRVCURLState *s)
241 769ce76d Alexander Graf
{
242 769ce76d Alexander Graf
    CURLState *state = NULL;
243 769ce76d Alexander Graf
    int i, j;
244 769ce76d Alexander Graf
245 769ce76d Alexander Graf
    do {
246 769ce76d Alexander Graf
        for (i=0; i<CURL_NUM_STATES; i++) {
247 769ce76d Alexander Graf
            for (j=0; j<CURL_NUM_ACB; j++)
248 769ce76d Alexander Graf
                if (s->states[i].acb[j])
249 769ce76d Alexander Graf
                    continue;
250 769ce76d Alexander Graf
            if (s->states[i].in_use)
251 769ce76d Alexander Graf
                continue;
252 769ce76d Alexander Graf
253 769ce76d Alexander Graf
            state = &s->states[i];
254 769ce76d Alexander Graf
            state->in_use = 1;
255 769ce76d Alexander Graf
            break;
256 769ce76d Alexander Graf
        }
257 769ce76d Alexander Graf
        if (!state) {
258 769ce76d Alexander Graf
            usleep(100);
259 769ce76d Alexander Graf
            curl_multi_do(s);
260 769ce76d Alexander Graf
        }
261 769ce76d Alexander Graf
    } while(!state);
262 769ce76d Alexander Graf
263 769ce76d Alexander Graf
    if (state->curl)
264 769ce76d Alexander Graf
        goto has_curl;
265 769ce76d Alexander Graf
266 769ce76d Alexander Graf
    state->curl = curl_easy_init();
267 769ce76d Alexander Graf
    if (!state->curl)
268 769ce76d Alexander Graf
        return NULL;
269 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
270 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
271 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, curl_read_cb);
272 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
273 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
274 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
275 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
276 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
277 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
278 769ce76d Alexander Graf
    
279 769ce76d Alexander Graf
#ifdef DEBUG_VERBOSE
280 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
281 769ce76d Alexander Graf
#endif
282 769ce76d Alexander Graf
283 769ce76d Alexander Graf
has_curl:
284 769ce76d Alexander Graf
285 769ce76d Alexander Graf
    state->s = s;
286 769ce76d Alexander Graf
287 769ce76d Alexander Graf
    return state;
288 769ce76d Alexander Graf
}
289 769ce76d Alexander Graf
290 769ce76d Alexander Graf
static void curl_clean_state(CURLState *s)
291 769ce76d Alexander Graf
{
292 769ce76d Alexander Graf
    if (s->s->multi)
293 769ce76d Alexander Graf
        curl_multi_remove_handle(s->s->multi, s->curl);
294 769ce76d Alexander Graf
    s->in_use = 0;
295 769ce76d Alexander Graf
}
296 769ce76d Alexander Graf
297 769ce76d Alexander Graf
static int curl_open(BlockDriverState *bs, const char *filename, int flags)
298 769ce76d Alexander Graf
{
299 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
300 769ce76d Alexander Graf
    CURLState *state = NULL;
301 769ce76d Alexander Graf
    double d;
302 769ce76d Alexander Graf
    static int inited = 0;
303 769ce76d Alexander Graf
304 769ce76d Alexander Graf
    if (!inited) {
305 769ce76d Alexander Graf
        curl_global_init(CURL_GLOBAL_ALL);
306 769ce76d Alexander Graf
        inited = 1;
307 769ce76d Alexander Graf
    }
308 769ce76d Alexander Graf
309 769ce76d Alexander Graf
    dprintf("CURL: Opening %s\n", filename);
310 769ce76d Alexander Graf
    s->url = strdup(filename);
311 769ce76d Alexander Graf
    state = curl_init_state(s);
312 769ce76d Alexander Graf
    if (!state)
313 769ce76d Alexander Graf
        goto out_noclean;
314 769ce76d Alexander Graf
315 769ce76d Alexander Graf
    // Get file size
316 769ce76d Alexander Graf
317 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
318 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, curl_size_cb);
319 769ce76d Alexander Graf
    if (curl_easy_perform(state->curl))
320 769ce76d Alexander Graf
        goto out;
321 769ce76d Alexander Graf
    curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
322 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, curl_read_cb);
323 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0);
324 769ce76d Alexander Graf
    if (d)
325 769ce76d Alexander Graf
        s->len = (size_t)d;
326 769ce76d Alexander Graf
    else if(!s->len)
327 769ce76d Alexander Graf
        goto out;
328 769ce76d Alexander Graf
    dprintf("CURL: Size = %lld\n", (long long)s->len);
329 769ce76d Alexander Graf
330 769ce76d Alexander Graf
    curl_clean_state(state);
331 769ce76d Alexander Graf
    curl_easy_cleanup(state->curl);
332 769ce76d Alexander Graf
    state->curl = NULL;
333 769ce76d Alexander Graf
334 769ce76d Alexander Graf
    // Now we know the file exists and its size, so let's
335 769ce76d Alexander Graf
    // initialize the multi interface!
336 769ce76d Alexander Graf
337 769ce76d Alexander Graf
    s->multi = curl_multi_init();
338 769ce76d Alexander Graf
    curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s); 
339 769ce76d Alexander Graf
    curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb ); 
340 769ce76d Alexander Graf
    curl_multi_do(s);
341 769ce76d Alexander Graf
342 769ce76d Alexander Graf
    return 0;
343 769ce76d Alexander Graf
344 769ce76d Alexander Graf
out:
345 769ce76d Alexander Graf
    fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
346 769ce76d Alexander Graf
    curl_easy_cleanup(state->curl);
347 769ce76d Alexander Graf
    state->curl = NULL;
348 769ce76d Alexander Graf
out_noclean:
349 769ce76d Alexander Graf
    return -EINVAL;
350 769ce76d Alexander Graf
}
351 769ce76d Alexander Graf
352 c16b5a2c Christoph Hellwig
static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
353 c16b5a2c Christoph Hellwig
{
354 c16b5a2c Christoph Hellwig
    // Do we have to implement canceling? Seems to work without...
355 c16b5a2c Christoph Hellwig
}
356 c16b5a2c Christoph Hellwig
357 c16b5a2c Christoph Hellwig
static AIOPool curl_aio_pool = {
358 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(CURLAIOCB),
359 c16b5a2c Christoph Hellwig
    .cancel             = curl_aio_cancel,
360 c16b5a2c Christoph Hellwig
};
361 c16b5a2c Christoph Hellwig
362 769ce76d Alexander Graf
static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
363 769ce76d Alexander Graf
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
364 769ce76d Alexander Graf
        BlockDriverCompletionFunc *cb, void *opaque)
365 769ce76d Alexander Graf
{
366 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
367 769ce76d Alexander Graf
    CURLAIOCB *acb;
368 769ce76d Alexander Graf
    size_t start = sector_num * SECTOR_SIZE;
369 769ce76d Alexander Graf
    size_t end;
370 769ce76d Alexander Graf
    CURLState *state;
371 769ce76d Alexander Graf
372 c16b5a2c Christoph Hellwig
    acb = qemu_aio_get(&curl_aio_pool, bs, cb, opaque);
373 769ce76d Alexander Graf
    if (!acb)
374 769ce76d Alexander Graf
        return NULL;
375 769ce76d Alexander Graf
376 769ce76d Alexander Graf
    acb->qiov = qiov;
377 769ce76d Alexander Graf
378 769ce76d Alexander Graf
    // In case we have the requested data already (e.g. read-ahead),
379 769ce76d Alexander Graf
    // we can just call the callback and be done.
380 769ce76d Alexander Graf
381 769ce76d Alexander Graf
    switch (curl_find_buf(s, start, nb_sectors * SECTOR_SIZE, acb)) {
382 769ce76d Alexander Graf
        case FIND_RET_OK:
383 769ce76d Alexander Graf
            qemu_aio_release(acb);
384 769ce76d Alexander Graf
            // fall through
385 769ce76d Alexander Graf
        case FIND_RET_WAIT:
386 769ce76d Alexander Graf
            return &acb->common;
387 769ce76d Alexander Graf
        default:
388 769ce76d Alexander Graf
            break;
389 769ce76d Alexander Graf
    }
390 769ce76d Alexander Graf
391 769ce76d Alexander Graf
    // No cache found, so let's start a new request
392 769ce76d Alexander Graf
393 769ce76d Alexander Graf
    state = curl_init_state(s);
394 769ce76d Alexander Graf
    if (!state)
395 769ce76d Alexander Graf
        return NULL;
396 769ce76d Alexander Graf
397 769ce76d Alexander Graf
    acb->start = 0;
398 769ce76d Alexander Graf
    acb->end = (nb_sectors * SECTOR_SIZE);
399 769ce76d Alexander Graf
400 769ce76d Alexander Graf
    state->buf_off = 0;
401 769ce76d Alexander Graf
    if (state->orig_buf)
402 769ce76d Alexander Graf
        qemu_free(state->orig_buf);
403 769ce76d Alexander Graf
    state->buf_start = start;
404 769ce76d Alexander Graf
    state->buf_len = acb->end + READ_AHEAD_SIZE;
405 769ce76d Alexander Graf
    end = MIN(start + state->buf_len, s->len) - 1;
406 769ce76d Alexander Graf
    state->orig_buf = qemu_malloc(state->buf_len);
407 769ce76d Alexander Graf
    state->acb[0] = acb;
408 769ce76d Alexander Graf
409 769ce76d Alexander Graf
    snprintf(state->range, 127, "%lld-%lld", (long long)start, (long long)end);
410 769ce76d Alexander Graf
    dprintf("CURL (AIO): Reading %d at %lld (%s)\n", (nb_sectors * SECTOR_SIZE), start, state->range);
411 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
412 769ce76d Alexander Graf
413 769ce76d Alexander Graf
    curl_multi_add_handle(s->multi, state->curl);
414 769ce76d Alexander Graf
    curl_multi_do(s);
415 769ce76d Alexander Graf
416 769ce76d Alexander Graf
    return &acb->common;
417 769ce76d Alexander Graf
}
418 769ce76d Alexander Graf
419 769ce76d Alexander Graf
static void curl_close(BlockDriverState *bs)
420 769ce76d Alexander Graf
{
421 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
422 769ce76d Alexander Graf
    int i;
423 769ce76d Alexander Graf
424 769ce76d Alexander Graf
    dprintf("CURL: Close\n");
425 769ce76d Alexander Graf
    for (i=0; i<CURL_NUM_STATES; i++) {
426 769ce76d Alexander Graf
        if (s->states[i].in_use)
427 769ce76d Alexander Graf
            curl_clean_state(&s->states[i]);
428 769ce76d Alexander Graf
        if (s->states[i].curl) {
429 769ce76d Alexander Graf
            curl_easy_cleanup(s->states[i].curl);
430 769ce76d Alexander Graf
            s->states[i].curl = NULL;
431 769ce76d Alexander Graf
        }
432 769ce76d Alexander Graf
        if (s->states[i].orig_buf) {
433 769ce76d Alexander Graf
            qemu_free(s->states[i].orig_buf);
434 769ce76d Alexander Graf
            s->states[i].orig_buf = NULL;
435 769ce76d Alexander Graf
        }
436 769ce76d Alexander Graf
    }
437 769ce76d Alexander Graf
    if (s->multi)
438 769ce76d Alexander Graf
        curl_multi_cleanup(s->multi);
439 769ce76d Alexander Graf
    if (s->url)
440 769ce76d Alexander Graf
        free(s->url);
441 769ce76d Alexander Graf
}
442 769ce76d Alexander Graf
443 769ce76d Alexander Graf
static int64_t curl_getlength(BlockDriverState *bs)
444 769ce76d Alexander Graf
{
445 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
446 769ce76d Alexander Graf
    return s->len;
447 769ce76d Alexander Graf
}
448 769ce76d Alexander Graf
449 769ce76d Alexander Graf
static BlockDriver bdrv_http = {
450 769ce76d Alexander Graf
    .format_name     = "http",
451 769ce76d Alexander Graf
    .protocol_name   = "http",
452 769ce76d Alexander Graf
453 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
454 769ce76d Alexander Graf
    .bdrv_open       = curl_open,
455 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
456 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
457 769ce76d Alexander Graf
458 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
459 769ce76d Alexander Graf
};
460 769ce76d Alexander Graf
461 769ce76d Alexander Graf
static BlockDriver bdrv_https = {
462 769ce76d Alexander Graf
    .format_name     = "https",
463 769ce76d Alexander Graf
    .protocol_name   = "https",
464 769ce76d Alexander Graf
465 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
466 769ce76d Alexander Graf
    .bdrv_open       = curl_open,
467 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
468 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
469 769ce76d Alexander Graf
470 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
471 769ce76d Alexander Graf
};
472 769ce76d Alexander Graf
473 769ce76d Alexander Graf
static BlockDriver bdrv_ftp = {
474 769ce76d Alexander Graf
    .format_name     = "ftp",
475 769ce76d Alexander Graf
    .protocol_name   = "ftp",
476 769ce76d Alexander Graf
477 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
478 769ce76d Alexander Graf
    .bdrv_open       = curl_open,
479 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
480 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
481 769ce76d Alexander Graf
482 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
483 769ce76d Alexander Graf
};
484 769ce76d Alexander Graf
485 769ce76d Alexander Graf
static BlockDriver bdrv_ftps = {
486 769ce76d Alexander Graf
    .format_name     = "ftps",
487 769ce76d Alexander Graf
    .protocol_name   = "ftps",
488 769ce76d Alexander Graf
489 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
490 769ce76d Alexander Graf
    .bdrv_open       = curl_open,
491 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
492 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
493 769ce76d Alexander Graf
494 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
495 769ce76d Alexander Graf
};
496 769ce76d Alexander Graf
497 769ce76d Alexander Graf
static BlockDriver bdrv_tftp = {
498 769ce76d Alexander Graf
    .format_name     = "tftp",
499 769ce76d Alexander Graf
    .protocol_name   = "tftp",
500 769ce76d Alexander Graf
501 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
502 769ce76d Alexander Graf
    .bdrv_open       = curl_open,
503 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
504 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
505 769ce76d Alexander Graf
506 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
507 769ce76d Alexander Graf
};
508 769ce76d Alexander Graf
509 769ce76d Alexander Graf
static void curl_block_init(void)
510 769ce76d Alexander Graf
{
511 769ce76d Alexander Graf
    bdrv_register(&bdrv_http);
512 769ce76d Alexander Graf
    bdrv_register(&bdrv_https);
513 769ce76d Alexander Graf
    bdrv_register(&bdrv_ftp);
514 769ce76d Alexander Graf
    bdrv_register(&bdrv_ftps);
515 769ce76d Alexander Graf
    bdrv_register(&bdrv_tftp);
516 769ce76d Alexander Graf
}
517 769ce76d Alexander Graf
518 769ce76d Alexander Graf
block_init(curl_block_init);