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