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