]> git.proxmox.com Git - qemu.git/blame - block.c
raw dmg support
[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
b338082b 35static BlockDriverState *bdrv_first;
ea2384d3
FB
36static BlockDriver *first_drv;
37
38void bdrv_register(BlockDriver *bdrv)
39{
40 bdrv->next = first_drv;
41 first_drv = bdrv;
42}
b338082b
FB
43
44/* create a new block device (by default it is empty) */
45BlockDriverState *bdrv_new(const char *device_name)
46{
47 BlockDriverState **pbs, *bs;
48
49 bs = qemu_mallocz(sizeof(BlockDriverState));
50 if(!bs)
51 return NULL;
52 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
ea2384d3
FB
53 if (device_name[0] != '\0') {
54 /* insert at the end */
55 pbs = &bdrv_first;
56 while (*pbs != NULL)
57 pbs = &(*pbs)->next;
58 *pbs = bs;
59 }
b338082b
FB
60 return bs;
61}
62
ea2384d3
FB
63BlockDriver *bdrv_find_format(const char *format_name)
64{
65 BlockDriver *drv1;
66 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
67 if (!strcmp(drv1->format_name, format_name))
68 return drv1;
69 }
70 return NULL;
71}
72
73int bdrv_create(BlockDriver *drv,
74 const char *filename, int64_t size_in_sectors,
75 const char *backing_file, int flags)
76{
77 if (!drv->bdrv_create)
78 return -ENOTSUP;
79 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
80}
81
d5249393
FB
82#ifdef _WIN32
83static void get_tmp_filename(char *filename, int size)
84{
85 /* XXX: find a better function */
86 tmpnam(filename);
87}
88#else
ea2384d3 89static void get_tmp_filename(char *filename, int size)
fc01f7e7 90{
67b915a5 91 int fd;
d5249393 92 /* XXX: race condition possible */
ea2384d3
FB
93 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
94 fd = mkstemp(filename);
95 close(fd);
96}
d5249393 97#endif
fc01f7e7 98
7674e7bf
FB
99/* XXX: force raw format if block or character device ? It would
100 simplify the BSD case */
ea2384d3
FB
101static BlockDriver *find_image_format(const char *filename)
102{
103 int fd, ret, score, score_max;
104 BlockDriver *drv1, *drv;
7674e7bf
FB
105 uint8_t *buf;
106 size_t bufsize = 1024;
33e3963e 107
ea2384d3
FB
108 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
109 if (fd < 0)
110 return NULL;
7674e7bf
FB
111#ifdef DIOCGSECTORSIZE
112 {
113 unsigned int sectorsize = 512;
114 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
115 sectorsize > bufsize)
116 bufsize = sectorsize;
117 }
118#endif
119 buf = malloc(bufsize);
120 if (!buf)
121 return NULL;
122 ret = read(fd, buf, bufsize);
ea2384d3
FB
123 if (ret < 0) {
124 close(fd);
7674e7bf 125 free(buf);
ea2384d3
FB
126 return NULL;
127 }
128 close(fd);
129
130 drv = NULL;
131 score_max = 0;
132 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
133 score = drv1->bdrv_probe(buf, ret, filename);
134 if (score > score_max) {
135 score_max = score;
136 drv = drv1;
0849bf08 137 }
fc01f7e7 138 }
7674e7bf 139 free(buf);
ea2384d3
FB
140 return drv;
141}
142
143int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
144{
145 return bdrv_open2(bs, filename, snapshot, NULL);
146}
147
148int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot,
149 BlockDriver *drv)
150{
151 int ret;
152 char tmp_filename[1024];
153
154 bs->read_only = 0;
155 bs->is_temporary = 0;
156 bs->encrypted = 0;
157
158 if (snapshot) {
159 BlockDriverState *bs1;
160 int64_t total_size;
161
162 /* if snapshot, we create a temporary backing file and open it
163 instead of opening 'filename' directly */
33e3963e 164
ea2384d3
FB
165 /* if there is a backing file, use it */
166 bs1 = bdrv_new("");
167 if (!bs1) {
168 return -1;
169 }
170 if (bdrv_open(bs1, filename, 0) < 0) {
171 bdrv_delete(bs1);
172 return -1;
173 }
174 total_size = bs1->total_sectors;
175 bdrv_delete(bs1);
176
177 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
178 /* XXX: use cow for linux as it is more efficient ? */
179 if (bdrv_create(&bdrv_qcow, tmp_filename,
180 total_size, filename, 0) < 0) {
181 return -1;
182 }
183 filename = tmp_filename;
184 bs->is_temporary = 1;
185 }
186
187 pstrcpy(bs->filename, sizeof(bs->filename), filename);
188 if (!drv) {
189 drv = find_image_format(filename);
190 if (!drv)
191 return -1;
192 }
193 bs->drv = drv;
194 bs->opaque = qemu_mallocz(drv->instance_size);
195 if (bs->opaque == NULL && drv->instance_size > 0)
196 return -1;
197
198 ret = drv->bdrv_open(bs, filename);
199 if (ret < 0) {
200 qemu_free(bs->opaque);
201 return -1;
33e3963e 202 }
67b915a5 203#ifndef _WIN32
ea2384d3
FB
204 if (bs->is_temporary) {
205 unlink(filename);
206 }
207#endif
208 if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) {
209 /* if there is a backing file, use it */
210 bs->backing_hd = bdrv_new("");
211 if (!bs->backing_hd) {
212 fail:
213 bdrv_close(bs);
214 return -1;
33e3963e 215 }
ea2384d3 216 if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0)
33e3963e 217 goto fail;
33e3963e
FB
218 }
219
b338082b
FB
220 bs->inserted = 1;
221
222 /* call the change callback */
223 if (bs->change_cb)
224 bs->change_cb(bs->change_opaque);
225
226 return 0;
fc01f7e7
FB
227}
228
229void bdrv_close(BlockDriverState *bs)
230{
b338082b 231 if (bs->inserted) {
ea2384d3
FB
232 if (bs->backing_hd)
233 bdrv_delete(bs->backing_hd);
234 bs->drv->bdrv_close(bs);
235 qemu_free(bs->opaque);
236#ifdef _WIN32
237 if (bs->is_temporary) {
238 unlink(bs->filename);
239 }
67b915a5 240#endif
ea2384d3
FB
241 bs->opaque = NULL;
242 bs->drv = NULL;
b338082b
FB
243 bs->inserted = 0;
244
245 /* call the change callback */
246 if (bs->change_cb)
247 bs->change_cb(bs->change_opaque);
248 }
249}
250
251void bdrv_delete(BlockDriverState *bs)
252{
ea2384d3 253 /* XXX: remove the driver list */
b338082b
FB
254 bdrv_close(bs);
255 qemu_free(bs);
fc01f7e7
FB
256}
257
33e3963e
FB
258/* commit COW file into the raw image */
259int bdrv_commit(BlockDriverState *bs)
260{
261 int64_t i;
ea2384d3
FB
262 int n, j;
263 unsigned char sector[512];
33e3963e 264
b338082b 265 if (!bs->inserted)
ea2384d3 266 return -ENOENT;
33e3963e
FB
267
268 if (bs->read_only) {
ea2384d3 269 return -EACCES;
33e3963e
FB
270 }
271
ea2384d3
FB
272 if (!bs->backing_hd) {
273 return -ENOTSUP;
274 }
33e3963e 275
ea2384d3
FB
276 for (i = 0; i < bs->total_sectors;) {
277 if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
278 for(j = 0; j < n; j++) {
279 if (bdrv_read(bs, i, sector, 1) != 0) {
280 return -EIO;
281 }
282
283 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
284 return -EIO;
285 }
286 i++;
33e3963e 287 }
ea2384d3
FB
288 } else {
289 i += n;
290 }
33e3963e 291 }
33e3963e
FB
292 return 0;
293}
294
fc01f7e7
FB
295/* return -1 if error */
296int bdrv_read(BlockDriverState *bs, int64_t sector_num,
297 uint8_t *buf, int nb_sectors)
298{
ea2384d3
FB
299 int ret, n;
300 BlockDriver *drv = bs->drv;
301
b338082b
FB
302 if (!bs->inserted)
303 return -1;
304
33e3963e 305 while (nb_sectors > 0) {
ea2384d3 306 if (sector_num == 0 && bs->boot_sector_enabled) {
cf98951b
FB
307 memcpy(buf, bs->boot_sector_data, 512);
308 n = 1;
ea2384d3
FB
309 } else if (bs->backing_hd) {
310 if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) {
311 ret = drv->bdrv_read(bs, sector_num, buf, n);
312 if (ret < 0)
313 return -1;
314 } else {
315 /* read from the base image */
316 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
317 if (ret < 0)
318 return -1;
319 }
33e3963e 320 } else {
ea2384d3
FB
321 ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors);
322 if (ret < 0)
33e3963e 323 return -1;
ea2384d3
FB
324 /* no need to loop */
325 break;
33e3963e
FB
326 }
327 nb_sectors -= n;
328 sector_num += n;
329 buf += n * 512;
330 }
331 return 0;
fc01f7e7
FB
332}
333
334/* return -1 if error */
335int bdrv_write(BlockDriverState *bs, int64_t sector_num,
336 const uint8_t *buf, int nb_sectors)
337{
b338082b
FB
338 if (!bs->inserted)
339 return -1;
0849bf08
FB
340 if (bs->read_only)
341 return -1;
ea2384d3 342 return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
fc01f7e7
FB
343}
344
345void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
346{
347 *nb_sectors_ptr = bs->total_sectors;
348}
cf98951b
FB
349
350/* force a given boot sector. */
351void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
352{
353 bs->boot_sector_enabled = 1;
354 if (size > 512)
355 size = 512;
356 memcpy(bs->boot_sector_data, data, size);
357 memset(bs->boot_sector_data + size, 0, 512 - size);
358}
b338082b
FB
359
360void bdrv_set_geometry_hint(BlockDriverState *bs,
361 int cyls, int heads, int secs)
362{
363 bs->cyls = cyls;
364 bs->heads = heads;
365 bs->secs = secs;
366}
367
368void bdrv_set_type_hint(BlockDriverState *bs, int type)
369{
370 bs->type = type;
371 bs->removable = ((type == BDRV_TYPE_CDROM ||
372 type == BDRV_TYPE_FLOPPY));
373}
374
46d4767d
FB
375void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
376{
377 bs->translation = translation;
378}
379
b338082b
FB
380void bdrv_get_geometry_hint(BlockDriverState *bs,
381 int *pcyls, int *pheads, int *psecs)
382{
383 *pcyls = bs->cyls;
384 *pheads = bs->heads;
385 *psecs = bs->secs;
386}
387
388int bdrv_get_type_hint(BlockDriverState *bs)
389{
390 return bs->type;
391}
392
46d4767d
FB
393int bdrv_get_translation_hint(BlockDriverState *bs)
394{
395 return bs->translation;
396}
397
b338082b
FB
398int bdrv_is_removable(BlockDriverState *bs)
399{
400 return bs->removable;
401}
402
403int bdrv_is_read_only(BlockDriverState *bs)
404{
405 return bs->read_only;
406}
407
408int bdrv_is_inserted(BlockDriverState *bs)
409{
410 return bs->inserted;
411}
412
413int bdrv_is_locked(BlockDriverState *bs)
414{
415 return bs->locked;
416}
417
418void bdrv_set_locked(BlockDriverState *bs, int locked)
419{
420 bs->locked = locked;
421}
422
423void bdrv_set_change_cb(BlockDriverState *bs,
424 void (*change_cb)(void *opaque), void *opaque)
425{
426 bs->change_cb = change_cb;
427 bs->change_opaque = opaque;
428}
429
ea2384d3
FB
430int bdrv_is_encrypted(BlockDriverState *bs)
431{
432 if (bs->backing_hd && bs->backing_hd->encrypted)
433 return 1;
434 return bs->encrypted;
435}
436
437int bdrv_set_key(BlockDriverState *bs, const char *key)
438{
439 int ret;
440 if (bs->backing_hd && bs->backing_hd->encrypted) {
441 ret = bdrv_set_key(bs->backing_hd, key);
442 if (ret < 0)
443 return ret;
444 if (!bs->encrypted)
445 return 0;
446 }
447 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
448 return -1;
449 return bs->drv->bdrv_set_key(bs, key);
450}
451
452void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
453{
454 if (!bs->inserted || !bs->drv) {
455 buf[0] = '\0';
456 } else {
457 pstrcpy(buf, buf_size, bs->drv->format_name);
458 }
459}
460
461void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
462 void *opaque)
463{
464 BlockDriver *drv;
465
466 for (drv = first_drv; drv != NULL; drv = drv->next) {
467 it(opaque, drv->format_name);
468 }
469}
470
b338082b
FB
471BlockDriverState *bdrv_find(const char *name)
472{
473 BlockDriverState *bs;
474
475 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
476 if (!strcmp(name, bs->device_name))
477 return bs;
478 }
479 return NULL;
480}
481
81d0912d
FB
482void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
483{
484 BlockDriverState *bs;
485
486 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
487 it(opaque, bs->device_name);
488 }
489}
490
ea2384d3
FB
491const char *bdrv_get_device_name(BlockDriverState *bs)
492{
493 return bs->device_name;
494}
495
b338082b
FB
496void bdrv_info(void)
497{
498 BlockDriverState *bs;
499
500 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
501 term_printf("%s:", bs->device_name);
502 term_printf(" type=");
503 switch(bs->type) {
504 case BDRV_TYPE_HD:
505 term_printf("hd");
506 break;
507 case BDRV_TYPE_CDROM:
508 term_printf("cdrom");
509 break;
510 case BDRV_TYPE_FLOPPY:
511 term_printf("floppy");
512 break;
513 }
514 term_printf(" removable=%d", bs->removable);
515 if (bs->removable) {
516 term_printf(" locked=%d", bs->locked);
517 }
518 if (bs->inserted) {
519 term_printf(" file=%s", bs->filename);
ea2384d3
FB
520 if (bs->backing_file[0] != '\0')
521 term_printf(" backing_file=%s", bs->backing_file);
b338082b 522 term_printf(" ro=%d", bs->read_only);
ea2384d3
FB
523 term_printf(" drv=%s", bs->drv->format_name);
524 if (bs->encrypted)
525 term_printf(" encrypted");
b338082b
FB
526 } else {
527 term_printf(" [not inserted]");
528 }
529 term_printf("\n");
530 }
531}
ea2384d3
FB
532
533
534/**************************************************************/
535/* RAW block driver */
536
537typedef struct BDRVRawState {
538 int fd;
539} BDRVRawState;
540
541static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
542{
543 return 1; /* maybe */
544}
545
546static int raw_open(BlockDriverState *bs, const char *filename)
547{
548 BDRVRawState *s = bs->opaque;
549 int fd;
550 int64_t size;
e5484d33
FB
551#ifdef _BSD
552 struct stat sb;
553#endif
ea2384d3
FB
554
555 fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
556 if (fd < 0) {
557 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
558 if (fd < 0)
559 return -1;
560 bs->read_only = 1;
561 }
7674e7bf 562#ifdef _BSD
e5484d33 563 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
7674e7bf 564#ifdef DIOCGMEDIASIZE
e5484d33 565 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
7674e7bf 566#endif
e5484d33 567 size = lseek(fd, 0LL, SEEK_END);
7674e7bf
FB
568 } else
569#endif
570 {
571 size = lseek(fd, 0, SEEK_END);
572 }
c747cd1f
FB
573#ifdef _WIN32
574 /* On Windows hosts it can happen that we're unable to get file size
575 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
576 if (size == -1)
577 size = LONG_LONG_MAX;
578#endif
ea2384d3
FB
579 bs->total_sectors = size / 512;
580 s->fd = fd;
581 return 0;
582}
583
584static int raw_read(BlockDriverState *bs, int64_t sector_num,
585 uint8_t *buf, int nb_sectors)
586{
587 BDRVRawState *s = bs->opaque;
588 int ret;
589
d5249393 590 lseek(s->fd, sector_num * 512, SEEK_SET);
ea2384d3
FB
591 ret = read(s->fd, buf, nb_sectors * 512);
592 if (ret != nb_sectors * 512)
593 return -1;
594 return 0;
595}
596
597static int raw_write(BlockDriverState *bs, int64_t sector_num,
598 const uint8_t *buf, int nb_sectors)
599{
600 BDRVRawState *s = bs->opaque;
601 int ret;
602
d5249393 603 lseek(s->fd, sector_num * 512, SEEK_SET);
ea2384d3
FB
604 ret = write(s->fd, buf, nb_sectors * 512);
605 if (ret != nb_sectors * 512)
606 return -1;
607 return 0;
608}
609
e2731add 610static void raw_close(BlockDriverState *bs)
ea2384d3
FB
611{
612 BDRVRawState *s = bs->opaque;
613 close(s->fd);
614}
615
616static int raw_create(const char *filename, int64_t total_size,
617 const char *backing_file, int flags)
618{
619 int fd;
620
621 if (flags || backing_file)
622 return -ENOTSUP;
623
624 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
625 0644);
626 if (fd < 0)
627 return -EIO;
d5249393 628 ftruncate(fd, total_size * 512);
ea2384d3
FB
629 close(fd);
630 return 0;
631}
632
633BlockDriver bdrv_raw = {
634 "raw",
635 sizeof(BDRVRawState),
636 raw_probe,
637 raw_open,
638 raw_read,
639 raw_write,
640 raw_close,
641 raw_create,
642};
643
644void bdrv_init(void)
645{
646 bdrv_register(&bdrv_raw);
647#ifndef _WIN32
648 bdrv_register(&bdrv_cow);
649#endif
650 bdrv_register(&bdrv_qcow);
651 bdrv_register(&bdrv_vmdk);
3c56521b 652 bdrv_register(&bdrv_cloop);
585d0ed9 653 bdrv_register(&bdrv_dmg);
a8753c34 654 bdrv_register(&bdrv_bochs);
6a0f9e82 655 bdrv_register(&bdrv_vpc);
ea2384d3 656}