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