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