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