]>
Commit | Line | Data |
---|---|---|
fc01f7e7 FB |
1 | /* |
2 | * QEMU System Emulator block driver | |
5fafdf24 | 3 | * |
fc01f7e7 | 4 | * Copyright (c) 2003 Fabrice Bellard |
5fafdf24 | 5 | * |
fc01f7e7 FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
fc01f7e7 | 24 | #include "vl.h" |
ea2384d3 | 25 | #include "block_int.h" |
fc01f7e7 | 26 | |
7674e7bf FB |
27 | #ifdef _BSD |
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
30 | #include <sys/ioctl.h> | |
31 | #include <sys/queue.h> | |
32 | #include <sys/disk.h> | |
33 | #endif | |
34 | ||
83f64091 FB |
35 | #define SECTOR_BITS 9 |
36 | #define SECTOR_SIZE (1 << SECTOR_BITS) | |
37 | ||
90765429 FB |
38 | typedef struct BlockDriverAIOCBSync { |
39 | BlockDriverAIOCB common; | |
40 | QEMUBH *bh; | |
41 | int ret; | |
42 | } BlockDriverAIOCBSync; | |
43 | ||
ce1a14dc PB |
44 | static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, |
45 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
46 | BlockDriverCompletionFunc *cb, void *opaque); | |
47 | static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, | |
48 | int64_t sector_num, const uint8_t *buf, int nb_sectors, | |
49 | BlockDriverCompletionFunc *cb, void *opaque); | |
83f64091 | 50 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); |
5fafdf24 | 51 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
83f64091 FB |
52 | uint8_t *buf, int nb_sectors); |
53 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, | |
54 | const uint8_t *buf, int nb_sectors); | |
ec530c81 | 55 | |
b338082b | 56 | static BlockDriverState *bdrv_first; |
ea2384d3 FB |
57 | static BlockDriver *first_drv; |
58 | ||
83f64091 | 59 | int path_is_absolute(const char *path) |
3b0d4f61 | 60 | { |
83f64091 | 61 | const char *p; |
21664424 FB |
62 | #ifdef _WIN32 |
63 | /* specific case for names like: "\\.\d:" */ | |
64 | if (*path == '/' || *path == '\\') | |
65 | return 1; | |
66 | #endif | |
83f64091 FB |
67 | p = strchr(path, ':'); |
68 | if (p) | |
69 | p++; | |
70 | else | |
71 | p = path; | |
3b9f94e1 FB |
72 | #ifdef _WIN32 |
73 | return (*p == '/' || *p == '\\'); | |
74 | #else | |
75 | return (*p == '/'); | |
76 | #endif | |
3b0d4f61 FB |
77 | } |
78 | ||
83f64091 FB |
79 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
80 | path to it by considering it is relative to base_path. URL are | |
81 | supported. */ | |
82 | void path_combine(char *dest, int dest_size, | |
83 | const char *base_path, | |
84 | const char *filename) | |
3b0d4f61 | 85 | { |
83f64091 FB |
86 | const char *p, *p1; |
87 | int len; | |
88 | ||
89 | if (dest_size <= 0) | |
90 | return; | |
91 | if (path_is_absolute(filename)) { | |
92 | pstrcpy(dest, dest_size, filename); | |
93 | } else { | |
94 | p = strchr(base_path, ':'); | |
95 | if (p) | |
96 | p++; | |
97 | else | |
98 | p = base_path; | |
3b9f94e1 FB |
99 | p1 = strrchr(base_path, '/'); |
100 | #ifdef _WIN32 | |
101 | { | |
102 | const char *p2; | |
103 | p2 = strrchr(base_path, '\\'); | |
104 | if (!p1 || p2 > p1) | |
105 | p1 = p2; | |
106 | } | |
107 | #endif | |
83f64091 FB |
108 | if (p1) |
109 | p1++; | |
110 | else | |
111 | p1 = base_path; | |
112 | if (p1 > p) | |
113 | p = p1; | |
114 | len = p - base_path; | |
115 | if (len > dest_size - 1) | |
116 | len = dest_size - 1; | |
117 | memcpy(dest, base_path, len); | |
118 | dest[len] = '\0'; | |
119 | pstrcat(dest, dest_size, filename); | |
3b0d4f61 | 120 | } |
3b0d4f61 FB |
121 | } |
122 | ||
3b0d4f61 | 123 | |
ea2384d3 FB |
124 | void bdrv_register(BlockDriver *bdrv) |
125 | { | |
ce1a14dc | 126 | if (!bdrv->bdrv_aio_read) { |
83f64091 | 127 | /* add AIO emulation layer */ |
83f64091 FB |
128 | bdrv->bdrv_aio_read = bdrv_aio_read_em; |
129 | bdrv->bdrv_aio_write = bdrv_aio_write_em; | |
130 | bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; | |
90765429 | 131 | bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync); |
83f64091 FB |
132 | } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) { |
133 | /* add synchronous IO emulation layer */ | |
134 | bdrv->bdrv_read = bdrv_read_em; | |
135 | bdrv->bdrv_write = bdrv_write_em; | |
136 | } | |
ea2384d3 FB |
137 | bdrv->next = first_drv; |
138 | first_drv = bdrv; | |
139 | } | |
b338082b FB |
140 | |
141 | /* create a new block device (by default it is empty) */ | |
142 | BlockDriverState *bdrv_new(const char *device_name) | |
143 | { | |
144 | BlockDriverState **pbs, *bs; | |
145 | ||
146 | bs = qemu_mallocz(sizeof(BlockDriverState)); | |
147 | if(!bs) | |
148 | return NULL; | |
149 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); | |
ea2384d3 FB |
150 | if (device_name[0] != '\0') { |
151 | /* insert at the end */ | |
152 | pbs = &bdrv_first; | |
153 | while (*pbs != NULL) | |
154 | pbs = &(*pbs)->next; | |
155 | *pbs = bs; | |
156 | } | |
b338082b FB |
157 | return bs; |
158 | } | |
159 | ||
ea2384d3 FB |
160 | BlockDriver *bdrv_find_format(const char *format_name) |
161 | { | |
162 | BlockDriver *drv1; | |
163 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
164 | if (!strcmp(drv1->format_name, format_name)) | |
165 | return drv1; | |
166 | } | |
167 | return NULL; | |
168 | } | |
169 | ||
5fafdf24 | 170 | int bdrv_create(BlockDriver *drv, |
ea2384d3 FB |
171 | const char *filename, int64_t size_in_sectors, |
172 | const char *backing_file, int flags) | |
173 | { | |
174 | if (!drv->bdrv_create) | |
175 | return -ENOTSUP; | |
176 | return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); | |
177 | } | |
178 | ||
d5249393 | 179 | #ifdef _WIN32 |
95389c86 | 180 | void get_tmp_filename(char *filename, int size) |
d5249393 | 181 | { |
3b9f94e1 | 182 | char temp_dir[MAX_PATH]; |
3b46e624 | 183 | |
3b9f94e1 FB |
184 | GetTempPath(MAX_PATH, temp_dir); |
185 | GetTempFileName(temp_dir, "qem", 0, filename); | |
d5249393 FB |
186 | } |
187 | #else | |
95389c86 | 188 | void get_tmp_filename(char *filename, int size) |
fc01f7e7 | 189 | { |
67b915a5 | 190 | int fd; |
d5249393 | 191 | /* XXX: race condition possible */ |
ea2384d3 FB |
192 | pstrcpy(filename, size, "/tmp/vl.XXXXXX"); |
193 | fd = mkstemp(filename); | |
194 | close(fd); | |
195 | } | |
d5249393 | 196 | #endif |
fc01f7e7 | 197 | |
19cb3738 | 198 | #ifdef _WIN32 |
f45512fe FB |
199 | static int is_windows_drive_prefix(const char *filename) |
200 | { | |
201 | return (((filename[0] >= 'a' && filename[0] <= 'z') || | |
202 | (filename[0] >= 'A' && filename[0] <= 'Z')) && | |
203 | filename[1] == ':'); | |
204 | } | |
3b46e624 | 205 | |
19cb3738 FB |
206 | static int is_windows_drive(const char *filename) |
207 | { | |
5fafdf24 | 208 | if (is_windows_drive_prefix(filename) && |
f45512fe | 209 | filename[2] == '\0') |
19cb3738 FB |
210 | return 1; |
211 | if (strstart(filename, "\\\\.\\", NULL) || | |
212 | strstart(filename, "//./", NULL)) | |
213 | return 1; | |
214 | return 0; | |
215 | } | |
216 | #endif | |
217 | ||
83f64091 FB |
218 | static BlockDriver *find_protocol(const char *filename) |
219 | { | |
220 | BlockDriver *drv1; | |
221 | char protocol[128]; | |
222 | int len; | |
223 | const char *p; | |
19cb3738 FB |
224 | |
225 | #ifdef _WIN32 | |
f45512fe FB |
226 | if (is_windows_drive(filename) || |
227 | is_windows_drive_prefix(filename)) | |
19cb3738 FB |
228 | return &bdrv_raw; |
229 | #endif | |
83f64091 FB |
230 | p = strchr(filename, ':'); |
231 | if (!p) | |
232 | return &bdrv_raw; | |
233 | len = p - filename; | |
234 | if (len > sizeof(protocol) - 1) | |
235 | len = sizeof(protocol) - 1; | |
83f64091 FB |
236 | memcpy(protocol, filename, len); |
237 | protocol[len] = '\0'; | |
238 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
5fafdf24 | 239 | if (drv1->protocol_name && |
83f64091 FB |
240 | !strcmp(drv1->protocol_name, protocol)) |
241 | return drv1; | |
242 | } | |
243 | return NULL; | |
244 | } | |
245 | ||
7674e7bf FB |
246 | /* XXX: force raw format if block or character device ? It would |
247 | simplify the BSD case */ | |
ea2384d3 FB |
248 | static BlockDriver *find_image_format(const char *filename) |
249 | { | |
83f64091 | 250 | int ret, score, score_max; |
ea2384d3 | 251 | BlockDriver *drv1, *drv; |
83f64091 FB |
252 | uint8_t buf[2048]; |
253 | BlockDriverState *bs; | |
3b46e624 | 254 | |
19cb3738 FB |
255 | /* detect host devices. By convention, /dev/cdrom[N] is always |
256 | recognized as a host CDROM */ | |
257 | if (strstart(filename, "/dev/cdrom", NULL)) | |
258 | return &bdrv_host_device; | |
259 | #ifdef _WIN32 | |
260 | if (is_windows_drive(filename)) | |
261 | return &bdrv_host_device; | |
262 | #else | |
263 | { | |
264 | struct stat st; | |
5fafdf24 | 265 | if (stat(filename, &st) >= 0 && |
19cb3738 FB |
266 | (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { |
267 | return &bdrv_host_device; | |
268 | } | |
269 | } | |
270 | #endif | |
3b46e624 | 271 | |
83f64091 | 272 | drv = find_protocol(filename); |
19cb3738 | 273 | /* no need to test disk image formats for vvfat */ |
83f64091 FB |
274 | if (drv == &bdrv_vvfat) |
275 | return drv; | |
19cb3738 | 276 | |
83f64091 FB |
277 | ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); |
278 | if (ret < 0) | |
279 | return NULL; | |
280 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); | |
281 | bdrv_delete(bs); | |
282 | if (ret < 0) { | |
283 | return NULL; | |
284 | } | |
285 | ||
ea2384d3 FB |
286 | score_max = 0; |
287 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
83f64091 FB |
288 | if (drv1->bdrv_probe) { |
289 | score = drv1->bdrv_probe(buf, ret, filename); | |
290 | if (score > score_max) { | |
291 | score_max = score; | |
292 | drv = drv1; | |
293 | } | |
0849bf08 | 294 | } |
fc01f7e7 | 295 | } |
ea2384d3 FB |
296 | return drv; |
297 | } | |
298 | ||
83f64091 | 299 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
ea2384d3 | 300 | { |
83f64091 FB |
301 | BlockDriverState *bs; |
302 | int ret; | |
303 | ||
304 | bs = bdrv_new(""); | |
305 | if (!bs) | |
306 | return -ENOMEM; | |
307 | ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); | |
308 | if (ret < 0) { | |
309 | bdrv_delete(bs); | |
310 | return ret; | |
3b0d4f61 | 311 | } |
83f64091 FB |
312 | *pbs = bs; |
313 | return 0; | |
314 | } | |
315 | ||
316 | int bdrv_open(BlockDriverState *bs, const char *filename, int flags) | |
317 | { | |
318 | return bdrv_open2(bs, filename, flags, NULL); | |
ea2384d3 FB |
319 | } |
320 | ||
83f64091 | 321 | int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, |
ea2384d3 FB |
322 | BlockDriver *drv) |
323 | { | |
83f64091 | 324 | int ret, open_flags; |
eb5c851f TS |
325 | char tmp_filename[PATH_MAX]; |
326 | char backing_filename[PATH_MAX]; | |
3b46e624 | 327 | |
ea2384d3 FB |
328 | bs->read_only = 0; |
329 | bs->is_temporary = 0; | |
330 | bs->encrypted = 0; | |
712e7874 | 331 | |
83f64091 | 332 | if (flags & BDRV_O_SNAPSHOT) { |
ea2384d3 FB |
333 | BlockDriverState *bs1; |
334 | int64_t total_size; | |
3b46e624 | 335 | |
ea2384d3 FB |
336 | /* if snapshot, we create a temporary backing file and open it |
337 | instead of opening 'filename' directly */ | |
33e3963e | 338 | |
ea2384d3 FB |
339 | /* if there is a backing file, use it */ |
340 | bs1 = bdrv_new(""); | |
341 | if (!bs1) { | |
83f64091 | 342 | return -ENOMEM; |
ea2384d3 FB |
343 | } |
344 | if (bdrv_open(bs1, filename, 0) < 0) { | |
345 | bdrv_delete(bs1); | |
346 | return -1; | |
347 | } | |
83f64091 | 348 | total_size = bdrv_getlength(bs1) >> SECTOR_BITS; |
ea2384d3 | 349 | bdrv_delete(bs1); |
3b46e624 | 350 | |
ea2384d3 | 351 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
a817d936 | 352 | realpath(filename, backing_filename); |
5fafdf24 | 353 | if (bdrv_create(&bdrv_qcow2, tmp_filename, |
a817d936 | 354 | total_size, backing_filename, 0) < 0) { |
ea2384d3 FB |
355 | return -1; |
356 | } | |
357 | filename = tmp_filename; | |
358 | bs->is_temporary = 1; | |
359 | } | |
712e7874 | 360 | |
ea2384d3 | 361 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
83f64091 FB |
362 | if (flags & BDRV_O_FILE) { |
363 | drv = find_protocol(filename); | |
ea2384d3 | 364 | if (!drv) |
83f64091 FB |
365 | return -ENOENT; |
366 | } else { | |
367 | if (!drv) { | |
368 | drv = find_image_format(filename); | |
369 | if (!drv) | |
370 | return -1; | |
371 | } | |
ea2384d3 FB |
372 | } |
373 | bs->drv = drv; | |
374 | bs->opaque = qemu_mallocz(drv->instance_size); | |
375 | if (bs->opaque == NULL && drv->instance_size > 0) | |
376 | return -1; | |
83f64091 FB |
377 | /* Note: for compatibility, we open disk image files as RDWR, and |
378 | RDONLY as fallback */ | |
379 | if (!(flags & BDRV_O_FILE)) | |
380 | open_flags = BDRV_O_RDWR; | |
381 | else | |
382 | open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); | |
383 | ret = drv->bdrv_open(bs, filename, open_flags); | |
384 | if (ret == -EACCES && !(flags & BDRV_O_FILE)) { | |
385 | ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY); | |
386 | bs->read_only = 1; | |
387 | } | |
ea2384d3 FB |
388 | if (ret < 0) { |
389 | qemu_free(bs->opaque); | |
6b21b973 FB |
390 | bs->opaque = NULL; |
391 | bs->drv = NULL; | |
83f64091 | 392 | return ret; |
33e3963e | 393 | } |
d15a771d FB |
394 | if (drv->bdrv_getlength) { |
395 | bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; | |
396 | } | |
67b915a5 | 397 | #ifndef _WIN32 |
ea2384d3 FB |
398 | if (bs->is_temporary) { |
399 | unlink(filename); | |
400 | } | |
401 | #endif | |
83f64091 | 402 | if (bs->backing_file[0] != '\0') { |
ea2384d3 FB |
403 | /* if there is a backing file, use it */ |
404 | bs->backing_hd = bdrv_new(""); | |
405 | if (!bs->backing_hd) { | |
406 | fail: | |
407 | bdrv_close(bs); | |
6b21b973 | 408 | return -ENOMEM; |
33e3963e | 409 | } |
83f64091 FB |
410 | path_combine(backing_filename, sizeof(backing_filename), |
411 | filename, bs->backing_file); | |
412 | if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0) | |
33e3963e | 413 | goto fail; |
33e3963e FB |
414 | } |
415 | ||
b338082b | 416 | /* call the change callback */ |
19cb3738 | 417 | bs->media_changed = 1; |
b338082b FB |
418 | if (bs->change_cb) |
419 | bs->change_cb(bs->change_opaque); | |
420 | ||
421 | return 0; | |
fc01f7e7 FB |
422 | } |
423 | ||
424 | void bdrv_close(BlockDriverState *bs) | |
425 | { | |
19cb3738 | 426 | if (bs->drv) { |
ea2384d3 FB |
427 | if (bs->backing_hd) |
428 | bdrv_delete(bs->backing_hd); | |
429 | bs->drv->bdrv_close(bs); | |
430 | qemu_free(bs->opaque); | |
431 | #ifdef _WIN32 | |
432 | if (bs->is_temporary) { | |
433 | unlink(bs->filename); | |
434 | } | |
67b915a5 | 435 | #endif |
ea2384d3 FB |
436 | bs->opaque = NULL; |
437 | bs->drv = NULL; | |
b338082b FB |
438 | |
439 | /* call the change callback */ | |
19cb3738 | 440 | bs->media_changed = 1; |
b338082b FB |
441 | if (bs->change_cb) |
442 | bs->change_cb(bs->change_opaque); | |
443 | } | |
444 | } | |
445 | ||
446 | void bdrv_delete(BlockDriverState *bs) | |
447 | { | |
ea2384d3 | 448 | /* XXX: remove the driver list */ |
b338082b FB |
449 | bdrv_close(bs); |
450 | qemu_free(bs); | |
fc01f7e7 FB |
451 | } |
452 | ||
33e3963e FB |
453 | /* commit COW file into the raw image */ |
454 | int bdrv_commit(BlockDriverState *bs) | |
455 | { | |
19cb3738 | 456 | BlockDriver *drv = bs->drv; |
83f64091 | 457 | int64_t i, total_sectors; |
ea2384d3 FB |
458 | int n, j; |
459 | unsigned char sector[512]; | |
33e3963e | 460 | |
19cb3738 FB |
461 | if (!drv) |
462 | return -ENOMEDIUM; | |
33e3963e FB |
463 | |
464 | if (bs->read_only) { | |
ea2384d3 | 465 | return -EACCES; |
33e3963e FB |
466 | } |
467 | ||
ea2384d3 FB |
468 | if (!bs->backing_hd) { |
469 | return -ENOTSUP; | |
470 | } | |
33e3963e | 471 | |
83f64091 FB |
472 | total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
473 | for (i = 0; i < total_sectors;) { | |
19cb3738 | 474 | if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
ea2384d3 FB |
475 | for(j = 0; j < n; j++) { |
476 | if (bdrv_read(bs, i, sector, 1) != 0) { | |
477 | return -EIO; | |
478 | } | |
479 | ||
480 | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { | |
481 | return -EIO; | |
482 | } | |
483 | i++; | |
33e3963e | 484 | } |
ea2384d3 FB |
485 | } else { |
486 | i += n; | |
487 | } | |
33e3963e | 488 | } |
95389c86 | 489 | |
19cb3738 FB |
490 | if (drv->bdrv_make_empty) |
491 | return drv->bdrv_make_empty(bs); | |
95389c86 | 492 | |
33e3963e FB |
493 | return 0; |
494 | } | |
495 | ||
19cb3738 | 496 | /* return < 0 if error. See bdrv_write() for the return codes */ |
5fafdf24 | 497 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
fc01f7e7 FB |
498 | uint8_t *buf, int nb_sectors) |
499 | { | |
ea2384d3 FB |
500 | BlockDriver *drv = bs->drv; |
501 | ||
19cb3738 FB |
502 | if (!drv) |
503 | return -ENOMEDIUM; | |
b338082b | 504 | |
83f64091 | 505 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
cf98951b | 506 | memcpy(buf, bs->boot_sector_data, 512); |
83f64091 FB |
507 | sector_num++; |
508 | nb_sectors--; | |
509 | buf += 512; | |
510 | if (nb_sectors == 0) | |
511 | return 0; | |
512 | } | |
513 | if (drv->bdrv_pread) { | |
514 | int ret, len; | |
515 | len = nb_sectors * 512; | |
516 | ret = drv->bdrv_pread(bs, sector_num * 512, buf, len); | |
517 | if (ret < 0) | |
518 | return ret; | |
519 | else if (ret != len) | |
19cb3738 | 520 | return -EINVAL; |
83f64091 FB |
521 | else |
522 | return 0; | |
523 | } else { | |
524 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); | |
33e3963e | 525 | } |
fc01f7e7 FB |
526 | } |
527 | ||
5fafdf24 | 528 | /* Return < 0 if error. Important errors are: |
19cb3738 FB |
529 | -EIO generic I/O error (may happen for all errors) |
530 | -ENOMEDIUM No media inserted. | |
531 | -EINVAL Invalid sector number or nb_sectors | |
532 | -EACCES Trying to write a read-only device | |
533 | */ | |
5fafdf24 | 534 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
fc01f7e7 FB |
535 | const uint8_t *buf, int nb_sectors) |
536 | { | |
83f64091 | 537 | BlockDriver *drv = bs->drv; |
19cb3738 FB |
538 | if (!bs->drv) |
539 | return -ENOMEDIUM; | |
0849bf08 | 540 | if (bs->read_only) |
19cb3738 | 541 | return -EACCES; |
79639d42 | 542 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
3b46e624 | 543 | memcpy(bs->boot_sector_data, buf, 512); |
79639d42 | 544 | } |
83f64091 FB |
545 | if (drv->bdrv_pwrite) { |
546 | int ret, len; | |
547 | len = nb_sectors * 512; | |
548 | ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len); | |
549 | if (ret < 0) | |
550 | return ret; | |
551 | else if (ret != len) | |
552 | return -EIO; | |
553 | else | |
554 | return 0; | |
555 | } else { | |
556 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); | |
557 | } | |
558 | } | |
559 | ||
5fafdf24 | 560 | static int bdrv_pread_em(BlockDriverState *bs, int64_t offset, |
faea38e7 | 561 | uint8_t *buf, int count1) |
83f64091 | 562 | { |
83f64091 FB |
563 | uint8_t tmp_buf[SECTOR_SIZE]; |
564 | int len, nb_sectors, count; | |
565 | int64_t sector_num; | |
566 | ||
567 | count = count1; | |
568 | /* first read to align to sector start */ | |
569 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); | |
570 | if (len > count) | |
571 | len = count; | |
572 | sector_num = offset >> SECTOR_BITS; | |
573 | if (len > 0) { | |
574 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
575 | return -EIO; | |
576 | memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); | |
577 | count -= len; | |
578 | if (count == 0) | |
579 | return count1; | |
580 | sector_num++; | |
581 | buf += len; | |
582 | } | |
583 | ||
584 | /* read the sectors "in place" */ | |
585 | nb_sectors = count >> SECTOR_BITS; | |
586 | if (nb_sectors > 0) { | |
587 | if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) | |
588 | return -EIO; | |
589 | sector_num += nb_sectors; | |
590 | len = nb_sectors << SECTOR_BITS; | |
591 | buf += len; | |
592 | count -= len; | |
593 | } | |
594 | ||
595 | /* add data from the last sector */ | |
596 | if (count > 0) { | |
597 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
598 | return -EIO; | |
599 | memcpy(buf, tmp_buf, count); | |
600 | } | |
601 | return count1; | |
602 | } | |
603 | ||
5fafdf24 | 604 | static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset, |
faea38e7 | 605 | const uint8_t *buf, int count1) |
83f64091 | 606 | { |
83f64091 FB |
607 | uint8_t tmp_buf[SECTOR_SIZE]; |
608 | int len, nb_sectors, count; | |
609 | int64_t sector_num; | |
610 | ||
611 | count = count1; | |
612 | /* first write to align to sector start */ | |
613 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); | |
614 | if (len > count) | |
615 | len = count; | |
616 | sector_num = offset >> SECTOR_BITS; | |
617 | if (len > 0) { | |
618 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
619 | return -EIO; | |
620 | memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); | |
621 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) | |
622 | return -EIO; | |
623 | count -= len; | |
624 | if (count == 0) | |
625 | return count1; | |
626 | sector_num++; | |
627 | buf += len; | |
628 | } | |
629 | ||
630 | /* write the sectors "in place" */ | |
631 | nb_sectors = count >> SECTOR_BITS; | |
632 | if (nb_sectors > 0) { | |
633 | if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) | |
634 | return -EIO; | |
635 | sector_num += nb_sectors; | |
636 | len = nb_sectors << SECTOR_BITS; | |
637 | buf += len; | |
638 | count -= len; | |
639 | } | |
640 | ||
641 | /* add data from the last sector */ | |
642 | if (count > 0) { | |
643 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
644 | return -EIO; | |
645 | memcpy(tmp_buf, buf, count); | |
646 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) | |
647 | return -EIO; | |
648 | } | |
649 | return count1; | |
650 | } | |
83f64091 FB |
651 | |
652 | /** | |
5fafdf24 | 653 | * Read with byte offsets (needed only for file protocols) |
83f64091 | 654 | */ |
5fafdf24 | 655 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
83f64091 FB |
656 | void *buf1, int count1) |
657 | { | |
658 | BlockDriver *drv = bs->drv; | |
659 | ||
660 | if (!drv) | |
19cb3738 | 661 | return -ENOMEDIUM; |
83f64091 | 662 | if (!drv->bdrv_pread) |
faea38e7 | 663 | return bdrv_pread_em(bs, offset, buf1, count1); |
83f64091 FB |
664 | return drv->bdrv_pread(bs, offset, buf1, count1); |
665 | } | |
666 | ||
5fafdf24 TS |
667 | /** |
668 | * Write with byte offsets (needed only for file protocols) | |
83f64091 | 669 | */ |
5fafdf24 | 670 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
83f64091 FB |
671 | const void *buf1, int count1) |
672 | { | |
673 | BlockDriver *drv = bs->drv; | |
674 | ||
675 | if (!drv) | |
19cb3738 | 676 | return -ENOMEDIUM; |
83f64091 | 677 | if (!drv->bdrv_pwrite) |
faea38e7 | 678 | return bdrv_pwrite_em(bs, offset, buf1, count1); |
83f64091 FB |
679 | return drv->bdrv_pwrite(bs, offset, buf1, count1); |
680 | } | |
681 | ||
682 | /** | |
683 | * Truncate file to 'offset' bytes (needed only for file protocols) | |
684 | */ | |
685 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) | |
686 | { | |
687 | BlockDriver *drv = bs->drv; | |
688 | if (!drv) | |
19cb3738 | 689 | return -ENOMEDIUM; |
83f64091 FB |
690 | if (!drv->bdrv_truncate) |
691 | return -ENOTSUP; | |
692 | return drv->bdrv_truncate(bs, offset); | |
693 | } | |
694 | ||
695 | /** | |
696 | * Length of a file in bytes. Return < 0 if error or unknown. | |
697 | */ | |
698 | int64_t bdrv_getlength(BlockDriverState *bs) | |
699 | { | |
700 | BlockDriver *drv = bs->drv; | |
701 | if (!drv) | |
19cb3738 | 702 | return -ENOMEDIUM; |
83f64091 FB |
703 | if (!drv->bdrv_getlength) { |
704 | /* legacy mode */ | |
705 | return bs->total_sectors * SECTOR_SIZE; | |
706 | } | |
707 | return drv->bdrv_getlength(bs); | |
fc01f7e7 FB |
708 | } |
709 | ||
19cb3738 | 710 | /* return 0 as number of sectors if no device present or error */ |
fc01f7e7 FB |
711 | void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr) |
712 | { | |
19cb3738 FB |
713 | int64_t length; |
714 | length = bdrv_getlength(bs); | |
715 | if (length < 0) | |
716 | length = 0; | |
717 | else | |
718 | length = length >> SECTOR_BITS; | |
719 | *nb_sectors_ptr = length; | |
fc01f7e7 | 720 | } |
cf98951b FB |
721 | |
722 | /* force a given boot sector. */ | |
723 | void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) | |
724 | { | |
725 | bs->boot_sector_enabled = 1; | |
726 | if (size > 512) | |
727 | size = 512; | |
728 | memcpy(bs->boot_sector_data, data, size); | |
729 | memset(bs->boot_sector_data + size, 0, 512 - size); | |
730 | } | |
b338082b | 731 | |
5fafdf24 | 732 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
b338082b FB |
733 | int cyls, int heads, int secs) |
734 | { | |
735 | bs->cyls = cyls; | |
736 | bs->heads = heads; | |
737 | bs->secs = secs; | |
738 | } | |
739 | ||
740 | void bdrv_set_type_hint(BlockDriverState *bs, int type) | |
741 | { | |
742 | bs->type = type; | |
743 | bs->removable = ((type == BDRV_TYPE_CDROM || | |
744 | type == BDRV_TYPE_FLOPPY)); | |
745 | } | |
746 | ||
46d4767d FB |
747 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
748 | { | |
749 | bs->translation = translation; | |
750 | } | |
751 | ||
5fafdf24 | 752 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
b338082b FB |
753 | int *pcyls, int *pheads, int *psecs) |
754 | { | |
755 | *pcyls = bs->cyls; | |
756 | *pheads = bs->heads; | |
757 | *psecs = bs->secs; | |
758 | } | |
759 | ||
760 | int bdrv_get_type_hint(BlockDriverState *bs) | |
761 | { | |
762 | return bs->type; | |
763 | } | |
764 | ||
46d4767d FB |
765 | int bdrv_get_translation_hint(BlockDriverState *bs) |
766 | { | |
767 | return bs->translation; | |
768 | } | |
769 | ||
b338082b FB |
770 | int bdrv_is_removable(BlockDriverState *bs) |
771 | { | |
772 | return bs->removable; | |
773 | } | |
774 | ||
775 | int bdrv_is_read_only(BlockDriverState *bs) | |
776 | { | |
777 | return bs->read_only; | |
778 | } | |
779 | ||
19cb3738 | 780 | /* XXX: no longer used */ |
5fafdf24 | 781 | void bdrv_set_change_cb(BlockDriverState *bs, |
b338082b FB |
782 | void (*change_cb)(void *opaque), void *opaque) |
783 | { | |
784 | bs->change_cb = change_cb; | |
785 | bs->change_opaque = opaque; | |
786 | } | |
787 | ||
ea2384d3 FB |
788 | int bdrv_is_encrypted(BlockDriverState *bs) |
789 | { | |
790 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
791 | return 1; | |
792 | return bs->encrypted; | |
793 | } | |
794 | ||
795 | int bdrv_set_key(BlockDriverState *bs, const char *key) | |
796 | { | |
797 | int ret; | |
798 | if (bs->backing_hd && bs->backing_hd->encrypted) { | |
799 | ret = bdrv_set_key(bs->backing_hd, key); | |
800 | if (ret < 0) | |
801 | return ret; | |
802 | if (!bs->encrypted) | |
803 | return 0; | |
804 | } | |
805 | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) | |
806 | return -1; | |
807 | return bs->drv->bdrv_set_key(bs, key); | |
808 | } | |
809 | ||
810 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) | |
811 | { | |
19cb3738 | 812 | if (!bs->drv) { |
ea2384d3 FB |
813 | buf[0] = '\0'; |
814 | } else { | |
815 | pstrcpy(buf, buf_size, bs->drv->format_name); | |
816 | } | |
817 | } | |
818 | ||
5fafdf24 | 819 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
ea2384d3 FB |
820 | void *opaque) |
821 | { | |
822 | BlockDriver *drv; | |
823 | ||
824 | for (drv = first_drv; drv != NULL; drv = drv->next) { | |
825 | it(opaque, drv->format_name); | |
826 | } | |
827 | } | |
828 | ||
b338082b FB |
829 | BlockDriverState *bdrv_find(const char *name) |
830 | { | |
831 | BlockDriverState *bs; | |
832 | ||
833 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
834 | if (!strcmp(name, bs->device_name)) | |
835 | return bs; | |
836 | } | |
837 | return NULL; | |
838 | } | |
839 | ||
81d0912d FB |
840 | void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) |
841 | { | |
842 | BlockDriverState *bs; | |
843 | ||
844 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
845 | it(opaque, bs->device_name); | |
846 | } | |
847 | } | |
848 | ||
ea2384d3 FB |
849 | const char *bdrv_get_device_name(BlockDriverState *bs) |
850 | { | |
851 | return bs->device_name; | |
852 | } | |
853 | ||
7a6cba61 PB |
854 | void bdrv_flush(BlockDriverState *bs) |
855 | { | |
856 | if (bs->drv->bdrv_flush) | |
857 | bs->drv->bdrv_flush(bs); | |
858 | if (bs->backing_hd) | |
859 | bdrv_flush(bs->backing_hd); | |
860 | } | |
861 | ||
b338082b FB |
862 | void bdrv_info(void) |
863 | { | |
864 | BlockDriverState *bs; | |
865 | ||
866 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
867 | term_printf("%s:", bs->device_name); | |
868 | term_printf(" type="); | |
869 | switch(bs->type) { | |
870 | case BDRV_TYPE_HD: | |
871 | term_printf("hd"); | |
872 | break; | |
873 | case BDRV_TYPE_CDROM: | |
874 | term_printf("cdrom"); | |
875 | break; | |
876 | case BDRV_TYPE_FLOPPY: | |
877 | term_printf("floppy"); | |
878 | break; | |
879 | } | |
880 | term_printf(" removable=%d", bs->removable); | |
881 | if (bs->removable) { | |
882 | term_printf(" locked=%d", bs->locked); | |
883 | } | |
19cb3738 | 884 | if (bs->drv) { |
fef30743 TS |
885 | term_printf(" file="); |
886 | term_print_filename(bs->filename); | |
887 | if (bs->backing_file[0] != '\0') { | |
888 | term_printf(" backing_file="); | |
889 | term_print_filename(bs->backing_file); | |
890 | } | |
b338082b | 891 | term_printf(" ro=%d", bs->read_only); |
ea2384d3 FB |
892 | term_printf(" drv=%s", bs->drv->format_name); |
893 | if (bs->encrypted) | |
894 | term_printf(" encrypted"); | |
b338082b FB |
895 | } else { |
896 | term_printf(" [not inserted]"); | |
897 | } | |
898 | term_printf("\n"); | |
899 | } | |
900 | } | |
ea2384d3 | 901 | |
5fafdf24 | 902 | void bdrv_get_backing_filename(BlockDriverState *bs, |
83f64091 FB |
903 | char *filename, int filename_size) |
904 | { | |
905 | if (!bs->backing_hd) { | |
906 | pstrcpy(filename, filename_size, ""); | |
907 | } else { | |
908 | pstrcpy(filename, filename_size, bs->backing_file); | |
909 | } | |
910 | } | |
911 | ||
5fafdf24 | 912 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
faea38e7 FB |
913 | const uint8_t *buf, int nb_sectors) |
914 | { | |
915 | BlockDriver *drv = bs->drv; | |
916 | if (!drv) | |
19cb3738 | 917 | return -ENOMEDIUM; |
faea38e7 FB |
918 | if (!drv->bdrv_write_compressed) |
919 | return -ENOTSUP; | |
920 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); | |
921 | } | |
3b46e624 | 922 | |
faea38e7 FB |
923 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
924 | { | |
925 | BlockDriver *drv = bs->drv; | |
926 | if (!drv) | |
19cb3738 | 927 | return -ENOMEDIUM; |
faea38e7 FB |
928 | if (!drv->bdrv_get_info) |
929 | return -ENOTSUP; | |
930 | memset(bdi, 0, sizeof(*bdi)); | |
931 | return drv->bdrv_get_info(bs, bdi); | |
932 | } | |
933 | ||
934 | /**************************************************************/ | |
935 | /* handling of snapshots */ | |
936 | ||
5fafdf24 | 937 | int bdrv_snapshot_create(BlockDriverState *bs, |
faea38e7 FB |
938 | QEMUSnapshotInfo *sn_info) |
939 | { | |
940 | BlockDriver *drv = bs->drv; | |
941 | if (!drv) | |
19cb3738 | 942 | return -ENOMEDIUM; |
faea38e7 FB |
943 | if (!drv->bdrv_snapshot_create) |
944 | return -ENOTSUP; | |
945 | return drv->bdrv_snapshot_create(bs, sn_info); | |
946 | } | |
947 | ||
5fafdf24 | 948 | int bdrv_snapshot_goto(BlockDriverState *bs, |
faea38e7 FB |
949 | const char *snapshot_id) |
950 | { | |
951 | BlockDriver *drv = bs->drv; | |
952 | if (!drv) | |
19cb3738 | 953 | return -ENOMEDIUM; |
faea38e7 FB |
954 | if (!drv->bdrv_snapshot_goto) |
955 | return -ENOTSUP; | |
956 | return drv->bdrv_snapshot_goto(bs, snapshot_id); | |
957 | } | |
958 | ||
959 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) | |
960 | { | |
961 | BlockDriver *drv = bs->drv; | |
962 | if (!drv) | |
19cb3738 | 963 | return -ENOMEDIUM; |
faea38e7 FB |
964 | if (!drv->bdrv_snapshot_delete) |
965 | return -ENOTSUP; | |
966 | return drv->bdrv_snapshot_delete(bs, snapshot_id); | |
967 | } | |
968 | ||
5fafdf24 | 969 | int bdrv_snapshot_list(BlockDriverState *bs, |
faea38e7 FB |
970 | QEMUSnapshotInfo **psn_info) |
971 | { | |
972 | BlockDriver *drv = bs->drv; | |
973 | if (!drv) | |
19cb3738 | 974 | return -ENOMEDIUM; |
faea38e7 FB |
975 | if (!drv->bdrv_snapshot_list) |
976 | return -ENOTSUP; | |
977 | return drv->bdrv_snapshot_list(bs, psn_info); | |
978 | } | |
979 | ||
980 | #define NB_SUFFIXES 4 | |
981 | ||
982 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) | |
983 | { | |
984 | static const char suffixes[NB_SUFFIXES] = "KMGT"; | |
985 | int64_t base; | |
986 | int i; | |
987 | ||
988 | if (size <= 999) { | |
989 | snprintf(buf, buf_size, "%" PRId64, size); | |
990 | } else { | |
991 | base = 1024; | |
992 | for(i = 0; i < NB_SUFFIXES; i++) { | |
993 | if (size < (10 * base)) { | |
5fafdf24 | 994 | snprintf(buf, buf_size, "%0.1f%c", |
faea38e7 FB |
995 | (double)size / base, |
996 | suffixes[i]); | |
997 | break; | |
998 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { | |
5fafdf24 | 999 | snprintf(buf, buf_size, "%" PRId64 "%c", |
faea38e7 FB |
1000 | ((size + (base >> 1)) / base), |
1001 | suffixes[i]); | |
1002 | break; | |
1003 | } | |
1004 | base = base * 1024; | |
1005 | } | |
1006 | } | |
1007 | return buf; | |
1008 | } | |
1009 | ||
1010 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) | |
1011 | { | |
1012 | char buf1[128], date_buf[128], clock_buf[128]; | |
3b9f94e1 FB |
1013 | #ifdef _WIN32 |
1014 | struct tm *ptm; | |
1015 | #else | |
faea38e7 | 1016 | struct tm tm; |
3b9f94e1 | 1017 | #endif |
faea38e7 FB |
1018 | time_t ti; |
1019 | int64_t secs; | |
1020 | ||
1021 | if (!sn) { | |
5fafdf24 TS |
1022 | snprintf(buf, buf_size, |
1023 | "%-10s%-20s%7s%20s%15s", | |
faea38e7 FB |
1024 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
1025 | } else { | |
1026 | ti = sn->date_sec; | |
3b9f94e1 FB |
1027 | #ifdef _WIN32 |
1028 | ptm = localtime(&ti); | |
1029 | strftime(date_buf, sizeof(date_buf), | |
1030 | "%Y-%m-%d %H:%M:%S", ptm); | |
1031 | #else | |
faea38e7 FB |
1032 | localtime_r(&ti, &tm); |
1033 | strftime(date_buf, sizeof(date_buf), | |
1034 | "%Y-%m-%d %H:%M:%S", &tm); | |
3b9f94e1 | 1035 | #endif |
faea38e7 FB |
1036 | secs = sn->vm_clock_nsec / 1000000000; |
1037 | snprintf(clock_buf, sizeof(clock_buf), | |
1038 | "%02d:%02d:%02d.%03d", | |
1039 | (int)(secs / 3600), | |
1040 | (int)((secs / 60) % 60), | |
5fafdf24 | 1041 | (int)(secs % 60), |
faea38e7 FB |
1042 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
1043 | snprintf(buf, buf_size, | |
5fafdf24 | 1044 | "%-10s%-20s%7s%20s%15s", |
faea38e7 FB |
1045 | sn->id_str, sn->name, |
1046 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), | |
1047 | date_buf, | |
1048 | clock_buf); | |
1049 | } | |
1050 | return buf; | |
1051 | } | |
1052 | ||
83f64091 | 1053 | |
ea2384d3 | 1054 | /**************************************************************/ |
83f64091 | 1055 | /* async I/Os */ |
ea2384d3 | 1056 | |
ce1a14dc PB |
1057 | BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num, |
1058 | uint8_t *buf, int nb_sectors, | |
1059 | BlockDriverCompletionFunc *cb, void *opaque) | |
83f64091 FB |
1060 | { |
1061 | BlockDriver *drv = bs->drv; | |
83f64091 | 1062 | |
19cb3738 | 1063 | if (!drv) |
ce1a14dc | 1064 | return NULL; |
3b46e624 | 1065 | |
83f64091 FB |
1066 | /* XXX: we assume that nb_sectors == 0 is suppored by the async read */ |
1067 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { | |
1068 | memcpy(buf, bs->boot_sector_data, 512); | |
1069 | sector_num++; | |
1070 | nb_sectors--; | |
1071 | buf += 512; | |
1072 | } | |
1073 | ||
ce1a14dc | 1074 | return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque); |
ea2384d3 FB |
1075 | } |
1076 | ||
ce1a14dc PB |
1077 | BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, |
1078 | const uint8_t *buf, int nb_sectors, | |
1079 | BlockDriverCompletionFunc *cb, void *opaque) | |
ea2384d3 | 1080 | { |
83f64091 | 1081 | BlockDriver *drv = bs->drv; |
ea2384d3 | 1082 | |
19cb3738 | 1083 | if (!drv) |
ce1a14dc | 1084 | return NULL; |
83f64091 | 1085 | if (bs->read_only) |
ce1a14dc | 1086 | return NULL; |
83f64091 | 1087 | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
3b46e624 | 1088 | memcpy(bs->boot_sector_data, buf, 512); |
ea2384d3 | 1089 | } |
83f64091 | 1090 | |
ce1a14dc | 1091 | return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque); |
83f64091 FB |
1092 | } |
1093 | ||
1094 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) | |
83f64091 | 1095 | { |
ce1a14dc | 1096 | BlockDriver *drv = acb->bs->drv; |
83f64091 | 1097 | |
ce1a14dc | 1098 | drv->bdrv_aio_cancel(acb); |
83f64091 FB |
1099 | } |
1100 | ||
ce1a14dc | 1101 | |
83f64091 FB |
1102 | /**************************************************************/ |
1103 | /* async block device emulation */ | |
1104 | ||
1105 | #ifdef QEMU_TOOL | |
ce1a14dc PB |
1106 | static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, |
1107 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
1108 | BlockDriverCompletionFunc *cb, void *opaque) | |
ea2384d3 | 1109 | { |
ea2384d3 | 1110 | int ret; |
ce1a14dc PB |
1111 | ret = bdrv_read(bs, sector_num, buf, nb_sectors); |
1112 | cb(opaque, ret); | |
1113 | return NULL; | |
ea2384d3 FB |
1114 | } |
1115 | ||
ce1a14dc PB |
1116 | static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, |
1117 | int64_t sector_num, const uint8_t *buf, int nb_sectors, | |
1118 | BlockDriverCompletionFunc *cb, void *opaque) | |
ea2384d3 | 1119 | { |
ea2384d3 | 1120 | int ret; |
ce1a14dc PB |
1121 | ret = bdrv_write(bs, sector_num, buf, nb_sectors); |
1122 | cb(opaque, ret); | |
1123 | return NULL; | |
ea2384d3 FB |
1124 | } |
1125 | ||
83f64091 | 1126 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb) |
ea2384d3 | 1127 | { |
ea2384d3 | 1128 | } |
83f64091 | 1129 | #else |
ce1a14dc | 1130 | static void bdrv_aio_bh_cb(void *opaque) |
83f64091 | 1131 | { |
ce1a14dc PB |
1132 | BlockDriverAIOCBSync *acb = opaque; |
1133 | acb->common.cb(acb->common.opaque, acb->ret); | |
1134 | qemu_aio_release(acb); | |
83f64091 | 1135 | } |
beac80cd | 1136 | |
ce1a14dc PB |
1137 | static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, |
1138 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
1139 | BlockDriverCompletionFunc *cb, void *opaque) | |
83f64091 | 1140 | { |
ce1a14dc | 1141 | BlockDriverAIOCBSync *acb; |
83f64091 | 1142 | int ret; |
ce1a14dc PB |
1143 | |
1144 | acb = qemu_aio_get(bs, cb, opaque); | |
1145 | if (!acb->bh) | |
1146 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
1147 | ret = bdrv_read(bs, sector_num, buf, nb_sectors); | |
1148 | acb->ret = ret; | |
1149 | qemu_bh_schedule(acb->bh); | |
1150 | return &acb->common; | |
beac80cd FB |
1151 | } |
1152 | ||
ce1a14dc PB |
1153 | static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, |
1154 | int64_t sector_num, const uint8_t *buf, int nb_sectors, | |
1155 | BlockDriverCompletionFunc *cb, void *opaque) | |
beac80cd | 1156 | { |
ce1a14dc | 1157 | BlockDriverAIOCBSync *acb; |
83f64091 | 1158 | int ret; |
83f64091 | 1159 | |
ce1a14dc PB |
1160 | acb = qemu_aio_get(bs, cb, opaque); |
1161 | if (!acb->bh) | |
1162 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
1163 | ret = bdrv_write(bs, sector_num, buf, nb_sectors); | |
1164 | acb->ret = ret; | |
1165 | qemu_bh_schedule(acb->bh); | |
1166 | return &acb->common; | |
beac80cd | 1167 | } |
beac80cd | 1168 | |
ce1a14dc | 1169 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
ea2384d3 | 1170 | { |
ce1a14dc PB |
1171 | BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; |
1172 | qemu_bh_cancel(acb->bh); | |
1173 | qemu_aio_release(acb); | |
83f64091 FB |
1174 | } |
1175 | #endif /* !QEMU_TOOL */ | |
ea2384d3 | 1176 | |
83f64091 FB |
1177 | /**************************************************************/ |
1178 | /* sync block device emulation */ | |
ea2384d3 | 1179 | |
83f64091 FB |
1180 | static void bdrv_rw_em_cb(void *opaque, int ret) |
1181 | { | |
1182 | *(int *)opaque = ret; | |
ea2384d3 FB |
1183 | } |
1184 | ||
83f64091 FB |
1185 | #define NOT_DONE 0x7fffffff |
1186 | ||
5fafdf24 | 1187 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
83f64091 | 1188 | uint8_t *buf, int nb_sectors) |
7a6cba61 | 1189 | { |
ce1a14dc PB |
1190 | int async_ret; |
1191 | BlockDriverAIOCB *acb; | |
83f64091 | 1192 | |
83f64091 FB |
1193 | async_ret = NOT_DONE; |
1194 | qemu_aio_wait_start(); | |
5fafdf24 | 1195 | acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors, |
83f64091 | 1196 | bdrv_rw_em_cb, &async_ret); |
ce1a14dc | 1197 | if (acb == NULL) { |
83f64091 | 1198 | qemu_aio_wait_end(); |
ce1a14dc | 1199 | return -1; |
83f64091 FB |
1200 | } |
1201 | while (async_ret == NOT_DONE) { | |
1202 | qemu_aio_wait(); | |
1203 | } | |
1204 | qemu_aio_wait_end(); | |
1205 | return async_ret; | |
7a6cba61 PB |
1206 | } |
1207 | ||
83f64091 FB |
1208 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
1209 | const uint8_t *buf, int nb_sectors) | |
1210 | { | |
ce1a14dc PB |
1211 | int async_ret; |
1212 | BlockDriverAIOCB *acb; | |
83f64091 | 1213 | |
83f64091 FB |
1214 | async_ret = NOT_DONE; |
1215 | qemu_aio_wait_start(); | |
5fafdf24 | 1216 | acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors, |
83f64091 | 1217 | bdrv_rw_em_cb, &async_ret); |
ce1a14dc | 1218 | if (acb == NULL) { |
83f64091 | 1219 | qemu_aio_wait_end(); |
ce1a14dc | 1220 | return -1; |
83f64091 FB |
1221 | } |
1222 | while (async_ret == NOT_DONE) { | |
1223 | qemu_aio_wait(); | |
1224 | } | |
1225 | qemu_aio_wait_end(); | |
1226 | return async_ret; | |
1227 | } | |
ea2384d3 FB |
1228 | |
1229 | void bdrv_init(void) | |
1230 | { | |
1231 | bdrv_register(&bdrv_raw); | |
19cb3738 | 1232 | bdrv_register(&bdrv_host_device); |
ea2384d3 FB |
1233 | #ifndef _WIN32 |
1234 | bdrv_register(&bdrv_cow); | |
1235 | #endif | |
1236 | bdrv_register(&bdrv_qcow); | |
1237 | bdrv_register(&bdrv_vmdk); | |
3c56521b | 1238 | bdrv_register(&bdrv_cloop); |
585d0ed9 | 1239 | bdrv_register(&bdrv_dmg); |
a8753c34 | 1240 | bdrv_register(&bdrv_bochs); |
6a0f9e82 | 1241 | bdrv_register(&bdrv_vpc); |
712e7874 | 1242 | bdrv_register(&bdrv_vvfat); |
faea38e7 | 1243 | bdrv_register(&bdrv_qcow2); |
6ada7453 | 1244 | bdrv_register(&bdrv_parallels); |
ea2384d3 | 1245 | } |
ce1a14dc PB |
1246 | |
1247 | void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb, | |
1248 | void *opaque) | |
1249 | { | |
1250 | BlockDriver *drv; | |
1251 | BlockDriverAIOCB *acb; | |
1252 | ||
1253 | drv = bs->drv; | |
1254 | if (drv->free_aiocb) { | |
1255 | acb = drv->free_aiocb; | |
1256 | drv->free_aiocb = acb->next; | |
1257 | } else { | |
1258 | acb = qemu_mallocz(drv->aiocb_size); | |
1259 | if (!acb) | |
1260 | return NULL; | |
1261 | } | |
1262 | acb->bs = bs; | |
1263 | acb->cb = cb; | |
1264 | acb->opaque = opaque; | |
1265 | return acb; | |
1266 | } | |
1267 | ||
1268 | void qemu_aio_release(void *p) | |
1269 | { | |
1270 | BlockDriverAIOCB *acb = p; | |
1271 | BlockDriver *drv = acb->bs->drv; | |
1272 | acb->next = drv->free_aiocb; | |
1273 | drv->free_aiocb = acb; | |
1274 | } | |
19cb3738 FB |
1275 | |
1276 | /**************************************************************/ | |
1277 | /* removable device support */ | |
1278 | ||
1279 | /** | |
1280 | * Return TRUE if the media is present | |
1281 | */ | |
1282 | int bdrv_is_inserted(BlockDriverState *bs) | |
1283 | { | |
1284 | BlockDriver *drv = bs->drv; | |
1285 | int ret; | |
1286 | if (!drv) | |
1287 | return 0; | |
1288 | if (!drv->bdrv_is_inserted) | |
1289 | return 1; | |
1290 | ret = drv->bdrv_is_inserted(bs); | |
1291 | return ret; | |
1292 | } | |
1293 | ||
1294 | /** | |
1295 | * Return TRUE if the media changed since the last call to this | |
5fafdf24 | 1296 | * function. It is currently only used for floppy disks |
19cb3738 FB |
1297 | */ |
1298 | int bdrv_media_changed(BlockDriverState *bs) | |
1299 | { | |
1300 | BlockDriver *drv = bs->drv; | |
1301 | int ret; | |
1302 | ||
1303 | if (!drv || !drv->bdrv_media_changed) | |
1304 | ret = -ENOTSUP; | |
1305 | else | |
1306 | ret = drv->bdrv_media_changed(bs); | |
1307 | if (ret == -ENOTSUP) | |
1308 | ret = bs->media_changed; | |
1309 | bs->media_changed = 0; | |
1310 | return ret; | |
1311 | } | |
1312 | ||
1313 | /** | |
1314 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray | |
1315 | */ | |
1316 | void bdrv_eject(BlockDriverState *bs, int eject_flag) | |
1317 | { | |
1318 | BlockDriver *drv = bs->drv; | |
1319 | int ret; | |
1320 | ||
1321 | if (!drv || !drv->bdrv_eject) { | |
1322 | ret = -ENOTSUP; | |
1323 | } else { | |
1324 | ret = drv->bdrv_eject(bs, eject_flag); | |
1325 | } | |
1326 | if (ret == -ENOTSUP) { | |
1327 | if (eject_flag) | |
1328 | bdrv_close(bs); | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | int bdrv_is_locked(BlockDriverState *bs) | |
1333 | { | |
1334 | return bs->locked; | |
1335 | } | |
1336 | ||
1337 | /** | |
1338 | * Lock or unlock the media (if it is locked, the user won't be able | |
1339 | * to eject it manually). | |
1340 | */ | |
1341 | void bdrv_set_locked(BlockDriverState *bs, int locked) | |
1342 | { | |
1343 | BlockDriver *drv = bs->drv; | |
1344 | ||
1345 | bs->locked = locked; | |
1346 | if (drv && drv->bdrv_set_locked) { | |
1347 | drv->bdrv_set_locked(bs, locked); | |
1348 | } | |
1349 | } |