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