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