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