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