Statistics
| Branch: | Revision:

root / block / curl.c @ 18ebcc86

History | View | Annotate | Download (16.3 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 363c3c85 Nick Thomas
    QEMUBH *bh;
51 769ce76d Alexander Graf
    QEMUIOVector *qiov;
52 363c3c85 Nick Thomas
53 363c3c85 Nick Thomas
    int64_t sector_num;
54 363c3c85 Nick Thomas
    int nb_sectors;
55 363c3c85 Nick Thomas
56 769ce76d Alexander Graf
    size_t start;
57 769ce76d Alexander Graf
    size_t end;
58 769ce76d Alexander Graf
} CURLAIOCB;
59 769ce76d Alexander Graf
60 769ce76d Alexander Graf
typedef struct CURLState
61 769ce76d Alexander Graf
{
62 769ce76d Alexander Graf
    struct BDRVCURLState *s;
63 769ce76d Alexander Graf
    CURLAIOCB *acb[CURL_NUM_ACB];
64 769ce76d Alexander Graf
    CURL *curl;
65 769ce76d Alexander Graf
    char *orig_buf;
66 769ce76d Alexander Graf
    size_t buf_start;
67 769ce76d Alexander Graf
    size_t buf_off;
68 769ce76d Alexander Graf
    size_t buf_len;
69 769ce76d Alexander Graf
    char range[128];
70 769ce76d Alexander Graf
    char errmsg[CURL_ERROR_SIZE];
71 769ce76d Alexander Graf
    char in_use;
72 769ce76d Alexander Graf
} CURLState;
73 769ce76d Alexander Graf
74 769ce76d Alexander Graf
typedef struct BDRVCURLState {
75 769ce76d Alexander Graf
    CURLM *multi;
76 769ce76d Alexander Graf
    size_t len;
77 769ce76d Alexander Graf
    CURLState states[CURL_NUM_STATES];
78 769ce76d Alexander Graf
    char *url;
79 c76f4952 Nolan
    size_t readahead_size;
80 769ce76d Alexander Graf
} BDRVCURLState;
81 769ce76d Alexander Graf
82 769ce76d Alexander Graf
static void curl_clean_state(CURLState *s);
83 769ce76d Alexander Graf
static void curl_multi_do(void *arg);
84 c84dcdc1 Nick Thomas
static int curl_aio_flush(void *opaque);
85 769ce76d Alexander Graf
86 769ce76d Alexander Graf
static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
87 769ce76d Alexander Graf
                        void *s, void *sp)
88 769ce76d Alexander Graf
{
89 d0f2c4c6 malc
    DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
90 769ce76d Alexander Graf
    switch (action) {
91 769ce76d Alexander Graf
        case CURL_POLL_IN:
92 c84dcdc1 Nick Thomas
            qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, curl_aio_flush,
93 c84dcdc1 Nick Thomas
                                    NULL, s);
94 769ce76d Alexander Graf
            break;
95 769ce76d Alexander Graf
        case CURL_POLL_OUT:
96 c84dcdc1 Nick Thomas
            qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, curl_aio_flush,
97 c84dcdc1 Nick Thomas
                                    NULL, s);
98 769ce76d Alexander Graf
            break;
99 769ce76d Alexander Graf
        case CURL_POLL_INOUT:
100 c84dcdc1 Nick Thomas
            qemu_aio_set_fd_handler(fd, curl_multi_do, curl_multi_do,
101 c84dcdc1 Nick Thomas
                                    curl_aio_flush, NULL, s);
102 769ce76d Alexander Graf
            break;
103 769ce76d Alexander Graf
        case CURL_POLL_REMOVE:
104 8febfa26 Kevin Wolf
            qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL, NULL);
105 769ce76d Alexander Graf
            break;
106 769ce76d Alexander Graf
    }
107 769ce76d Alexander Graf
108 769ce76d Alexander Graf
    return 0;
109 769ce76d Alexander Graf
}
110 769ce76d Alexander Graf
111 769ce76d Alexander Graf
static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
112 769ce76d Alexander Graf
{
113 769ce76d Alexander Graf
    CURLState *s = ((CURLState*)opaque);
114 769ce76d Alexander Graf
    size_t realsize = size * nmemb;
115 0bfcd599 Blue Swirl
    size_t fsize;
116 769ce76d Alexander Graf
117 0bfcd599 Blue Swirl
    if(sscanf(ptr, "Content-Length: %zd", &fsize) == 1) {
118 769ce76d Alexander Graf
        s->s->len = fsize;
119 0bfcd599 Blue Swirl
    }
120 769ce76d Alexander Graf
121 769ce76d Alexander Graf
    return realsize;
122 769ce76d Alexander Graf
}
123 769ce76d Alexander Graf
124 769ce76d Alexander Graf
static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
125 769ce76d Alexander Graf
{
126 769ce76d Alexander Graf
    CURLState *s = ((CURLState*)opaque);
127 769ce76d Alexander Graf
    size_t realsize = size * nmemb;
128 769ce76d Alexander Graf
    int i;
129 769ce76d Alexander Graf
130 0bfcd599 Blue Swirl
    DPRINTF("CURL: Just reading %zd bytes\n", realsize);
131 769ce76d Alexander Graf
132 769ce76d Alexander Graf
    if (!s || !s->orig_buf)
133 769ce76d Alexander Graf
        goto read_end;
134 769ce76d Alexander Graf
135 769ce76d Alexander Graf
    memcpy(s->orig_buf + s->buf_off, ptr, realsize);
136 769ce76d Alexander Graf
    s->buf_off += realsize;
137 769ce76d Alexander Graf
138 769ce76d Alexander Graf
    for(i=0; i<CURL_NUM_ACB; i++) {
139 769ce76d Alexander Graf
        CURLAIOCB *acb = s->acb[i];
140 769ce76d Alexander Graf
141 769ce76d Alexander Graf
        if (!acb)
142 769ce76d Alexander Graf
            continue;
143 769ce76d Alexander Graf
144 769ce76d Alexander Graf
        if ((s->buf_off >= acb->end)) {
145 769ce76d Alexander Graf
            qemu_iovec_from_buffer(acb->qiov, s->orig_buf + acb->start,
146 769ce76d Alexander Graf
                                   acb->end - acb->start);
147 769ce76d Alexander Graf
            acb->common.cb(acb->common.opaque, 0);
148 769ce76d Alexander Graf
            qemu_aio_release(acb);
149 769ce76d Alexander Graf
            s->acb[i] = NULL;
150 769ce76d Alexander Graf
        }
151 769ce76d Alexander Graf
    }
152 769ce76d Alexander Graf
153 769ce76d Alexander Graf
read_end:
154 769ce76d Alexander Graf
    return realsize;
155 769ce76d Alexander Graf
}
156 769ce76d Alexander Graf
157 769ce76d Alexander Graf
static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
158 769ce76d Alexander Graf
                         CURLAIOCB *acb)
159 769ce76d Alexander Graf
{
160 769ce76d Alexander Graf
    int i;
161 769ce76d Alexander Graf
    size_t end = start + len;
162 769ce76d Alexander Graf
163 769ce76d Alexander Graf
    for (i=0; i<CURL_NUM_STATES; i++) {
164 769ce76d Alexander Graf
        CURLState *state = &s->states[i];
165 769ce76d Alexander Graf
        size_t buf_end = (state->buf_start + state->buf_off);
166 769ce76d Alexander Graf
        size_t buf_fend = (state->buf_start + state->buf_len);
167 769ce76d Alexander Graf
168 769ce76d Alexander Graf
        if (!state->orig_buf)
169 769ce76d Alexander Graf
            continue;
170 769ce76d Alexander Graf
        if (!state->buf_off)
171 769ce76d Alexander Graf
            continue;
172 769ce76d Alexander Graf
173 769ce76d Alexander Graf
        // Does the existing buffer cover our section?
174 769ce76d Alexander Graf
        if ((start >= state->buf_start) &&
175 769ce76d Alexander Graf
            (start <= buf_end) &&
176 769ce76d Alexander Graf
            (end >= state->buf_start) &&
177 769ce76d Alexander Graf
            (end <= buf_end))
178 769ce76d Alexander Graf
        {
179 769ce76d Alexander Graf
            char *buf = state->orig_buf + (start - state->buf_start);
180 769ce76d Alexander Graf
181 769ce76d Alexander Graf
            qemu_iovec_from_buffer(acb->qiov, buf, len);
182 769ce76d Alexander Graf
            acb->common.cb(acb->common.opaque, 0);
183 769ce76d Alexander Graf
184 769ce76d Alexander Graf
            return FIND_RET_OK;
185 769ce76d Alexander Graf
        }
186 769ce76d Alexander Graf
187 769ce76d Alexander Graf
        // Wait for unfinished chunks
188 769ce76d Alexander Graf
        if ((start >= state->buf_start) &&
189 769ce76d Alexander Graf
            (start <= buf_fend) &&
190 769ce76d Alexander Graf
            (end >= state->buf_start) &&
191 769ce76d Alexander Graf
            (end <= buf_fend))
192 769ce76d Alexander Graf
        {
193 769ce76d Alexander Graf
            int j;
194 769ce76d Alexander Graf
195 769ce76d Alexander Graf
            acb->start = start - state->buf_start;
196 769ce76d Alexander Graf
            acb->end = acb->start + len;
197 769ce76d Alexander Graf
198 769ce76d Alexander Graf
            for (j=0; j<CURL_NUM_ACB; j++) {
199 769ce76d Alexander Graf
                if (!state->acb[j]) {
200 769ce76d Alexander Graf
                    state->acb[j] = acb;
201 769ce76d Alexander Graf
                    return FIND_RET_WAIT;
202 769ce76d Alexander Graf
                }
203 769ce76d Alexander Graf
            }
204 769ce76d Alexander Graf
        }
205 769ce76d Alexander Graf
    }
206 769ce76d Alexander Graf
207 769ce76d Alexander Graf
    return FIND_RET_NONE;
208 769ce76d Alexander Graf
}
209 769ce76d Alexander Graf
210 769ce76d Alexander Graf
static void curl_multi_do(void *arg)
211 769ce76d Alexander Graf
{
212 769ce76d Alexander Graf
    BDRVCURLState *s = (BDRVCURLState *)arg;
213 769ce76d Alexander Graf
    int running;
214 769ce76d Alexander Graf
    int r;
215 769ce76d Alexander Graf
    int msgs_in_queue;
216 769ce76d Alexander Graf
217 769ce76d Alexander Graf
    if (!s->multi)
218 769ce76d Alexander Graf
        return;
219 769ce76d Alexander Graf
220 769ce76d Alexander Graf
    do {
221 769ce76d Alexander Graf
        r = curl_multi_socket_all(s->multi, &running);
222 769ce76d Alexander Graf
    } while(r == CURLM_CALL_MULTI_PERFORM);
223 769ce76d Alexander Graf
224 769ce76d Alexander Graf
    /* Try to find done transfers, so we can free the easy
225 769ce76d Alexander Graf
     * handle again. */
226 769ce76d Alexander Graf
    do {
227 769ce76d Alexander Graf
        CURLMsg *msg;
228 769ce76d Alexander Graf
        msg = curl_multi_info_read(s->multi, &msgs_in_queue);
229 769ce76d Alexander Graf
230 769ce76d Alexander Graf
        if (!msg)
231 769ce76d Alexander Graf
            break;
232 769ce76d Alexander Graf
        if (msg->msg == CURLMSG_NONE)
233 769ce76d Alexander Graf
            break;
234 769ce76d Alexander Graf
235 769ce76d Alexander Graf
        switch (msg->msg) {
236 769ce76d Alexander Graf
            case CURLMSG_DONE:
237 769ce76d Alexander Graf
            {
238 769ce76d Alexander Graf
                CURLState *state = NULL;
239 769ce76d Alexander Graf
                curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
240 f785a5ae Nicholas Thomas
241 f785a5ae Nicholas Thomas
                /* ACBs for successful messages get completed in curl_read_cb */
242 f785a5ae Nicholas Thomas
                if (msg->data.result != CURLE_OK) {
243 f785a5ae Nicholas Thomas
                    int i;
244 f785a5ae Nicholas Thomas
                    for (i = 0; i < CURL_NUM_ACB; i++) {
245 f785a5ae Nicholas Thomas
                        CURLAIOCB *acb = state->acb[i];
246 f785a5ae Nicholas Thomas
247 f785a5ae Nicholas Thomas
                        if (acb == NULL) {
248 f785a5ae Nicholas Thomas
                            continue;
249 f785a5ae Nicholas Thomas
                        }
250 f785a5ae Nicholas Thomas
251 f785a5ae Nicholas Thomas
                        acb->common.cb(acb->common.opaque, -EIO);
252 f785a5ae Nicholas Thomas
                        qemu_aio_release(acb);
253 f785a5ae Nicholas Thomas
                        state->acb[i] = NULL;
254 f785a5ae Nicholas Thomas
                    }
255 f785a5ae Nicholas Thomas
                }
256 f785a5ae Nicholas Thomas
257 769ce76d Alexander Graf
                curl_clean_state(state);
258 769ce76d Alexander Graf
                break;
259 769ce76d Alexander Graf
            }
260 769ce76d Alexander Graf
            default:
261 769ce76d Alexander Graf
                msgs_in_queue = 0;
262 769ce76d Alexander Graf
                break;
263 769ce76d Alexander Graf
        }
264 769ce76d Alexander Graf
    } while(msgs_in_queue);
265 769ce76d Alexander Graf
}
266 769ce76d Alexander Graf
267 769ce76d Alexander Graf
static CURLState *curl_init_state(BDRVCURLState *s)
268 769ce76d Alexander Graf
{
269 769ce76d Alexander Graf
    CURLState *state = NULL;
270 769ce76d Alexander Graf
    int i, j;
271 769ce76d Alexander Graf
272 769ce76d Alexander Graf
    do {
273 769ce76d Alexander Graf
        for (i=0; i<CURL_NUM_STATES; i++) {
274 769ce76d Alexander Graf
            for (j=0; j<CURL_NUM_ACB; j++)
275 769ce76d Alexander Graf
                if (s->states[i].acb[j])
276 769ce76d Alexander Graf
                    continue;
277 769ce76d Alexander Graf
            if (s->states[i].in_use)
278 769ce76d Alexander Graf
                continue;
279 769ce76d Alexander Graf
280 769ce76d Alexander Graf
            state = &s->states[i];
281 769ce76d Alexander Graf
            state->in_use = 1;
282 769ce76d Alexander Graf
            break;
283 769ce76d Alexander Graf
        }
284 769ce76d Alexander Graf
        if (!state) {
285 769ce76d Alexander Graf
            usleep(100);
286 769ce76d Alexander Graf
            curl_multi_do(s);
287 769ce76d Alexander Graf
        }
288 769ce76d Alexander Graf
    } while(!state);
289 769ce76d Alexander Graf
290 769ce76d Alexander Graf
    if (state->curl)
291 769ce76d Alexander Graf
        goto has_curl;
292 769ce76d Alexander Graf
293 769ce76d Alexander Graf
    state->curl = curl_easy_init();
294 769ce76d Alexander Graf
    if (!state->curl)
295 769ce76d Alexander Graf
        return NULL;
296 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
297 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
298 df3cee1a Blue Swirl
    curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
299 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
300 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
301 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
302 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
303 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
304 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
305 f785a5ae Nicholas Thomas
    curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
306 f785a5ae Nicholas Thomas
307 769ce76d Alexander Graf
#ifdef DEBUG_VERBOSE
308 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
309 769ce76d Alexander Graf
#endif
310 769ce76d Alexander Graf
311 769ce76d Alexander Graf
has_curl:
312 769ce76d Alexander Graf
313 769ce76d Alexander Graf
    state->s = s;
314 769ce76d Alexander Graf
315 769ce76d Alexander Graf
    return state;
316 769ce76d Alexander Graf
}
317 769ce76d Alexander Graf
318 769ce76d Alexander Graf
static void curl_clean_state(CURLState *s)
319 769ce76d Alexander Graf
{
320 769ce76d Alexander Graf
    if (s->s->multi)
321 769ce76d Alexander Graf
        curl_multi_remove_handle(s->s->multi, s->curl);
322 769ce76d Alexander Graf
    s->in_use = 0;
323 769ce76d Alexander Graf
}
324 769ce76d Alexander Graf
325 769ce76d Alexander Graf
static int curl_open(BlockDriverState *bs, const char *filename, int flags)
326 769ce76d Alexander Graf
{
327 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
328 769ce76d Alexander Graf
    CURLState *state = NULL;
329 769ce76d Alexander Graf
    double d;
330 c76f4952 Nolan
331 c76f4952 Nolan
    #define RA_OPTSTR ":readahead="
332 c76f4952 Nolan
    char *file;
333 c76f4952 Nolan
    char *ra;
334 c76f4952 Nolan
    const char *ra_val;
335 c76f4952 Nolan
    int parse_state = 0;
336 c76f4952 Nolan
337 769ce76d Alexander Graf
    static int inited = 0;
338 769ce76d Alexander Graf
339 7267c094 Anthony Liguori
    file = g_strdup(filename);
340 c76f4952 Nolan
    s->readahead_size = READ_AHEAD_SIZE;
341 c76f4952 Nolan
342 c76f4952 Nolan
    /* Parse a trailing ":readahead=#:" param, if present. */
343 c76f4952 Nolan
    ra = file + strlen(file) - 1;
344 c76f4952 Nolan
    while (ra >= file) {
345 c76f4952 Nolan
        if (parse_state == 0) {
346 c76f4952 Nolan
            if (*ra == ':')
347 c76f4952 Nolan
                parse_state++;
348 c76f4952 Nolan
            else
349 c76f4952 Nolan
                break;
350 c76f4952 Nolan
        } else if (parse_state == 1) {
351 c76f4952 Nolan
            if (*ra > '9' || *ra < '0') {
352 c76f4952 Nolan
                char *opt_start = ra - strlen(RA_OPTSTR) + 1;
353 c76f4952 Nolan
                if (opt_start > file &&
354 c76f4952 Nolan
                    strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) {
355 c76f4952 Nolan
                    ra_val = ra + 1;
356 c76f4952 Nolan
                    ra -= strlen(RA_OPTSTR) - 1;
357 c76f4952 Nolan
                    *ra = '\0';
358 c76f4952 Nolan
                    s->readahead_size = atoi(ra_val);
359 c76f4952 Nolan
                    break;
360 c76f4952 Nolan
                } else {
361 c76f4952 Nolan
                    break;
362 c76f4952 Nolan
                }
363 c76f4952 Nolan
            }
364 c76f4952 Nolan
        }
365 c76f4952 Nolan
        ra--;
366 c76f4952 Nolan
    }
367 c76f4952 Nolan
368 c76f4952 Nolan
    if ((s->readahead_size & 0x1ff) != 0) {
369 48a402e6 malc
        fprintf(stderr, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n",
370 c76f4952 Nolan
                s->readahead_size);
371 c76f4952 Nolan
        goto out_noclean;
372 c76f4952 Nolan
    }
373 c76f4952 Nolan
374 769ce76d Alexander Graf
    if (!inited) {
375 769ce76d Alexander Graf
        curl_global_init(CURL_GLOBAL_ALL);
376 769ce76d Alexander Graf
        inited = 1;
377 769ce76d Alexander Graf
    }
378 769ce76d Alexander Graf
379 d0f2c4c6 malc
    DPRINTF("CURL: Opening %s\n", file);
380 c76f4952 Nolan
    s->url = file;
381 769ce76d Alexander Graf
    state = curl_init_state(s);
382 769ce76d Alexander Graf
    if (!state)
383 769ce76d Alexander Graf
        goto out_noclean;
384 769ce76d Alexander Graf
385 769ce76d Alexander Graf
    // Get file size
386 769ce76d Alexander Graf
387 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
388 df3cee1a Blue Swirl
    curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_size_cb);
389 769ce76d Alexander Graf
    if (curl_easy_perform(state->curl))
390 769ce76d Alexander Graf
        goto out;
391 769ce76d Alexander Graf
    curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
392 df3cee1a Blue Swirl
    curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
393 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0);
394 769ce76d Alexander Graf
    if (d)
395 769ce76d Alexander Graf
        s->len = (size_t)d;
396 769ce76d Alexander Graf
    else if(!s->len)
397 769ce76d Alexander Graf
        goto out;
398 0bfcd599 Blue Swirl
    DPRINTF("CURL: Size = %zd\n", s->len);
399 769ce76d Alexander Graf
400 769ce76d Alexander Graf
    curl_clean_state(state);
401 769ce76d Alexander Graf
    curl_easy_cleanup(state->curl);
402 769ce76d Alexander Graf
    state->curl = NULL;
403 769ce76d Alexander Graf
404 769ce76d Alexander Graf
    // Now we know the file exists and its size, so let's
405 769ce76d Alexander Graf
    // initialize the multi interface!
406 769ce76d Alexander Graf
407 769ce76d Alexander Graf
    s->multi = curl_multi_init();
408 769ce76d Alexander Graf
    curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s); 
409 769ce76d Alexander Graf
    curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb ); 
410 769ce76d Alexander Graf
    curl_multi_do(s);
411 769ce76d Alexander Graf
412 769ce76d Alexander Graf
    return 0;
413 769ce76d Alexander Graf
414 769ce76d Alexander Graf
out:
415 769ce76d Alexander Graf
    fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
416 769ce76d Alexander Graf
    curl_easy_cleanup(state->curl);
417 769ce76d Alexander Graf
    state->curl = NULL;
418 769ce76d Alexander Graf
out_noclean:
419 7267c094 Anthony Liguori
    g_free(file);
420 769ce76d Alexander Graf
    return -EINVAL;
421 769ce76d Alexander Graf
}
422 769ce76d Alexander Graf
423 c84dcdc1 Nick Thomas
static int curl_aio_flush(void *opaque)
424 c84dcdc1 Nick Thomas
{
425 c84dcdc1 Nick Thomas
    BDRVCURLState *s = opaque;
426 c84dcdc1 Nick Thomas
    int i, j;
427 c84dcdc1 Nick Thomas
428 c84dcdc1 Nick Thomas
    for (i=0; i < CURL_NUM_STATES; i++) {
429 c84dcdc1 Nick Thomas
        for(j=0; j < CURL_NUM_ACB; j++) {
430 c84dcdc1 Nick Thomas
            if (s->states[i].acb[j]) {
431 c84dcdc1 Nick Thomas
                return 1;
432 c84dcdc1 Nick Thomas
            }
433 c84dcdc1 Nick Thomas
        }
434 c84dcdc1 Nick Thomas
    }
435 c84dcdc1 Nick Thomas
    return 0;
436 c84dcdc1 Nick Thomas
}
437 c84dcdc1 Nick Thomas
438 c16b5a2c Christoph Hellwig
static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
439 c16b5a2c Christoph Hellwig
{
440 c16b5a2c Christoph Hellwig
    // Do we have to implement canceling? Seems to work without...
441 c16b5a2c Christoph Hellwig
}
442 c16b5a2c Christoph Hellwig
443 c16b5a2c Christoph Hellwig
static AIOPool curl_aio_pool = {
444 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(CURLAIOCB),
445 c16b5a2c Christoph Hellwig
    .cancel             = curl_aio_cancel,
446 c16b5a2c Christoph Hellwig
};
447 c16b5a2c Christoph Hellwig
448 363c3c85 Nick Thomas
449 363c3c85 Nick Thomas
static void curl_readv_bh_cb(void *p)
450 769ce76d Alexander Graf
{
451 769ce76d Alexander Graf
    CURLState *state;
452 769ce76d Alexander Graf
453 363c3c85 Nick Thomas
    CURLAIOCB *acb = p;
454 363c3c85 Nick Thomas
    BDRVCURLState *s = acb->common.bs->opaque;
455 769ce76d Alexander Graf
456 363c3c85 Nick Thomas
    qemu_bh_delete(acb->bh);
457 363c3c85 Nick Thomas
    acb->bh = NULL;
458 363c3c85 Nick Thomas
459 363c3c85 Nick Thomas
    size_t start = acb->sector_num * SECTOR_SIZE;
460 363c3c85 Nick Thomas
    size_t end;
461 769ce76d Alexander Graf
462 769ce76d Alexander Graf
    // In case we have the requested data already (e.g. read-ahead),
463 769ce76d Alexander Graf
    // we can just call the callback and be done.
464 363c3c85 Nick Thomas
    switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
465 769ce76d Alexander Graf
        case FIND_RET_OK:
466 769ce76d Alexander Graf
            qemu_aio_release(acb);
467 769ce76d Alexander Graf
            // fall through
468 769ce76d Alexander Graf
        case FIND_RET_WAIT:
469 363c3c85 Nick Thomas
            return;
470 769ce76d Alexander Graf
        default:
471 769ce76d Alexander Graf
            break;
472 769ce76d Alexander Graf
    }
473 769ce76d Alexander Graf
474 769ce76d Alexander Graf
    // No cache found, so let's start a new request
475 769ce76d Alexander Graf
    state = curl_init_state(s);
476 363c3c85 Nick Thomas
    if (!state) {
477 363c3c85 Nick Thomas
        acb->common.cb(acb->common.opaque, -EIO);
478 363c3c85 Nick Thomas
        qemu_aio_release(acb);
479 363c3c85 Nick Thomas
        return;
480 363c3c85 Nick Thomas
    }
481 769ce76d Alexander Graf
482 769ce76d Alexander Graf
    acb->start = 0;
483 363c3c85 Nick Thomas
    acb->end = (acb->nb_sectors * SECTOR_SIZE);
484 769ce76d Alexander Graf
485 769ce76d Alexander Graf
    state->buf_off = 0;
486 769ce76d Alexander Graf
    if (state->orig_buf)
487 7267c094 Anthony Liguori
        g_free(state->orig_buf);
488 769ce76d Alexander Graf
    state->buf_start = start;
489 c76f4952 Nolan
    state->buf_len = acb->end + s->readahead_size;
490 769ce76d Alexander Graf
    end = MIN(start + state->buf_len, s->len) - 1;
491 7267c094 Anthony Liguori
    state->orig_buf = g_malloc(state->buf_len);
492 769ce76d Alexander Graf
    state->acb[0] = acb;
493 769ce76d Alexander Graf
494 0bfcd599 Blue Swirl
    snprintf(state->range, 127, "%zd-%zd", start, end);
495 0bfcd599 Blue Swirl
    DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
496 363c3c85 Nick Thomas
            (acb->nb_sectors * SECTOR_SIZE), start, state->range);
497 769ce76d Alexander Graf
    curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
498 769ce76d Alexander Graf
499 769ce76d Alexander Graf
    curl_multi_add_handle(s->multi, state->curl);
500 769ce76d Alexander Graf
    curl_multi_do(s);
501 769ce76d Alexander Graf
502 363c3c85 Nick Thomas
}
503 363c3c85 Nick Thomas
504 363c3c85 Nick Thomas
static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
505 363c3c85 Nick Thomas
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
506 363c3c85 Nick Thomas
        BlockDriverCompletionFunc *cb, void *opaque)
507 363c3c85 Nick Thomas
{
508 363c3c85 Nick Thomas
    CURLAIOCB *acb;
509 363c3c85 Nick Thomas
510 363c3c85 Nick Thomas
    acb = qemu_aio_get(&curl_aio_pool, bs, cb, opaque);
511 363c3c85 Nick Thomas
512 363c3c85 Nick Thomas
    if (!acb) {
513 363c3c85 Nick Thomas
        return NULL;
514 363c3c85 Nick Thomas
    }
515 363c3c85 Nick Thomas
516 363c3c85 Nick Thomas
    acb->qiov = qiov;
517 363c3c85 Nick Thomas
    acb->sector_num = sector_num;
518 363c3c85 Nick Thomas
    acb->nb_sectors = nb_sectors;
519 363c3c85 Nick Thomas
520 363c3c85 Nick Thomas
    acb->bh = qemu_bh_new(curl_readv_bh_cb, acb);
521 363c3c85 Nick Thomas
522 363c3c85 Nick Thomas
    if (!acb->bh) {
523 363c3c85 Nick Thomas
        DPRINTF("CURL: qemu_bh_new failed\n");
524 363c3c85 Nick Thomas
        return NULL;
525 363c3c85 Nick Thomas
    }
526 363c3c85 Nick Thomas
527 363c3c85 Nick Thomas
    qemu_bh_schedule(acb->bh);
528 769ce76d Alexander Graf
    return &acb->common;
529 769ce76d Alexander Graf
}
530 769ce76d Alexander Graf
531 769ce76d Alexander Graf
static void curl_close(BlockDriverState *bs)
532 769ce76d Alexander Graf
{
533 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
534 769ce76d Alexander Graf
    int i;
535 769ce76d Alexander Graf
536 d0f2c4c6 malc
    DPRINTF("CURL: Close\n");
537 769ce76d Alexander Graf
    for (i=0; i<CURL_NUM_STATES; i++) {
538 769ce76d Alexander Graf
        if (s->states[i].in_use)
539 769ce76d Alexander Graf
            curl_clean_state(&s->states[i]);
540 769ce76d Alexander Graf
        if (s->states[i].curl) {
541 769ce76d Alexander Graf
            curl_easy_cleanup(s->states[i].curl);
542 769ce76d Alexander Graf
            s->states[i].curl = NULL;
543 769ce76d Alexander Graf
        }
544 769ce76d Alexander Graf
        if (s->states[i].orig_buf) {
545 7267c094 Anthony Liguori
            g_free(s->states[i].orig_buf);
546 769ce76d Alexander Graf
            s->states[i].orig_buf = NULL;
547 769ce76d Alexander Graf
        }
548 769ce76d Alexander Graf
    }
549 769ce76d Alexander Graf
    if (s->multi)
550 769ce76d Alexander Graf
        curl_multi_cleanup(s->multi);
551 769ce76d Alexander Graf
    if (s->url)
552 769ce76d Alexander Graf
        free(s->url);
553 769ce76d Alexander Graf
}
554 769ce76d Alexander Graf
555 769ce76d Alexander Graf
static int64_t curl_getlength(BlockDriverState *bs)
556 769ce76d Alexander Graf
{
557 769ce76d Alexander Graf
    BDRVCURLState *s = bs->opaque;
558 769ce76d Alexander Graf
    return s->len;
559 769ce76d Alexander Graf
}
560 769ce76d Alexander Graf
561 769ce76d Alexander Graf
static BlockDriver bdrv_http = {
562 769ce76d Alexander Graf
    .format_name     = "http",
563 769ce76d Alexander Graf
    .protocol_name   = "http",
564 769ce76d Alexander Graf
565 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
566 66f82cee Kevin Wolf
    .bdrv_file_open  = curl_open,
567 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
568 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
569 769ce76d Alexander Graf
570 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
571 769ce76d Alexander Graf
};
572 769ce76d Alexander Graf
573 769ce76d Alexander Graf
static BlockDriver bdrv_https = {
574 769ce76d Alexander Graf
    .format_name     = "https",
575 769ce76d Alexander Graf
    .protocol_name   = "https",
576 769ce76d Alexander Graf
577 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
578 66f82cee Kevin Wolf
    .bdrv_file_open  = curl_open,
579 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
580 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
581 769ce76d Alexander Graf
582 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
583 769ce76d Alexander Graf
};
584 769ce76d Alexander Graf
585 769ce76d Alexander Graf
static BlockDriver bdrv_ftp = {
586 769ce76d Alexander Graf
    .format_name     = "ftp",
587 769ce76d Alexander Graf
    .protocol_name   = "ftp",
588 769ce76d Alexander Graf
589 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
590 66f82cee Kevin Wolf
    .bdrv_file_open  = curl_open,
591 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
592 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
593 769ce76d Alexander Graf
594 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
595 769ce76d Alexander Graf
};
596 769ce76d Alexander Graf
597 769ce76d Alexander Graf
static BlockDriver bdrv_ftps = {
598 769ce76d Alexander Graf
    .format_name     = "ftps",
599 769ce76d Alexander Graf
    .protocol_name   = "ftps",
600 769ce76d Alexander Graf
601 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
602 66f82cee Kevin Wolf
    .bdrv_file_open  = curl_open,
603 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
604 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
605 769ce76d Alexander Graf
606 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
607 769ce76d Alexander Graf
};
608 769ce76d Alexander Graf
609 769ce76d Alexander Graf
static BlockDriver bdrv_tftp = {
610 769ce76d Alexander Graf
    .format_name     = "tftp",
611 769ce76d Alexander Graf
    .protocol_name   = "tftp",
612 769ce76d Alexander Graf
613 769ce76d Alexander Graf
    .instance_size   = sizeof(BDRVCURLState),
614 66f82cee Kevin Wolf
    .bdrv_file_open  = curl_open,
615 769ce76d Alexander Graf
    .bdrv_close      = curl_close,
616 769ce76d Alexander Graf
    .bdrv_getlength  = curl_getlength,
617 769ce76d Alexander Graf
618 769ce76d Alexander Graf
    .bdrv_aio_readv  = curl_aio_readv,
619 769ce76d Alexander Graf
};
620 769ce76d Alexander Graf
621 769ce76d Alexander Graf
static void curl_block_init(void)
622 769ce76d Alexander Graf
{
623 769ce76d Alexander Graf
    bdrv_register(&bdrv_http);
624 769ce76d Alexander Graf
    bdrv_register(&bdrv_https);
625 769ce76d Alexander Graf
    bdrv_register(&bdrv_ftp);
626 769ce76d Alexander Graf
    bdrv_register(&bdrv_ftps);
627 769ce76d Alexander Graf
    bdrv_register(&bdrv_tftp);
628 769ce76d Alexander Graf
}
629 769ce76d Alexander Graf
630 769ce76d Alexander Graf
block_init(curl_block_init);