loader: fix potential memory leak
[qemu/kevin.git] / block / bochs.c
blobf0e18c0b84ddfbdbfcc35591e5ca1b9815f39db1
1 /*
2 * Block driver for the various disk image formats used by Bochs
3 * Currently only for "growing" type in read-only mode
5 * Copyright (c) 2005 Alex Beregszaszi
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu-common.h"
28 #include "block/block_int.h"
29 #include "qemu/module.h"
31 /**************************************************************/
33 #define HEADER_MAGIC "Bochs Virtual HD Image"
34 #define HEADER_VERSION 0x00020000
35 #define HEADER_V1 0x00010000
36 #define HEADER_SIZE 512
38 #define REDOLOG_TYPE "Redolog"
39 #define GROWING_TYPE "Growing"
41 // not allocated: 0xffffffff
43 // always little-endian
44 struct bochs_header {
45 char magic[32]; /* "Bochs Virtual HD Image" */
46 char type[16]; /* "Redolog" */
47 char subtype[16]; /* "Undoable" / "Volatile" / "Growing" */
48 uint32_t version;
49 uint32_t header; /* size of header */
51 uint32_t catalog; /* num of entries */
52 uint32_t bitmap; /* bitmap size */
53 uint32_t extent; /* extent size */
55 union {
56 struct {
57 uint32_t reserved; /* for ??? */
58 uint64_t disk; /* disk size */
59 char padding[HEADER_SIZE - 64 - 20 - 12];
60 } QEMU_PACKED redolog;
61 struct {
62 uint64_t disk; /* disk size */
63 char padding[HEADER_SIZE - 64 - 20 - 8];
64 } QEMU_PACKED redolog_v1;
65 char padding[HEADER_SIZE - 64 - 20];
66 } extra;
67 } QEMU_PACKED;
69 typedef struct BDRVBochsState {
70 CoMutex lock;
71 uint32_t *catalog_bitmap;
72 uint32_t catalog_size;
74 uint32_t data_offset;
76 uint32_t bitmap_blocks;
77 uint32_t extent_blocks;
78 uint32_t extent_size;
79 } BDRVBochsState;
81 static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
83 const struct bochs_header *bochs = (const void *)buf;
85 if (buf_size < HEADER_SIZE)
86 return 0;
88 if (!strcmp(bochs->magic, HEADER_MAGIC) &&
89 !strcmp(bochs->type, REDOLOG_TYPE) &&
90 !strcmp(bochs->subtype, GROWING_TYPE) &&
91 ((le32_to_cpu(bochs->version) == HEADER_VERSION) ||
92 (le32_to_cpu(bochs->version) == HEADER_V1)))
93 return 100;
95 return 0;
98 static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
99 Error **errp)
101 BDRVBochsState *s = bs->opaque;
102 uint32_t i;
103 struct bochs_header bochs;
104 int ret;
106 bs->read_only = 1; // no write support yet
107 bs->request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O supported */
109 ret = bdrv_pread(bs->file->bs, 0, &bochs, sizeof(bochs));
110 if (ret < 0) {
111 return ret;
114 if (strcmp(bochs.magic, HEADER_MAGIC) ||
115 strcmp(bochs.type, REDOLOG_TYPE) ||
116 strcmp(bochs.subtype, GROWING_TYPE) ||
117 ((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
118 (le32_to_cpu(bochs.version) != HEADER_V1))) {
119 error_setg(errp, "Image not in Bochs format");
120 return -EINVAL;
123 if (le32_to_cpu(bochs.version) == HEADER_V1) {
124 bs->total_sectors = le64_to_cpu(bochs.extra.redolog_v1.disk) / 512;
125 } else {
126 bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
129 /* Limit to 1M entries to avoid unbounded allocation. This is what is
130 * needed for the largest image that bximage can create (~8 TB). */
131 s->catalog_size = le32_to_cpu(bochs.catalog);
132 if (s->catalog_size > 0x100000) {
133 error_setg(errp, "Catalog size is too large");
134 return -EFBIG;
137 s->catalog_bitmap = g_try_new(uint32_t, s->catalog_size);
138 if (s->catalog_size && s->catalog_bitmap == NULL) {
139 error_setg(errp, "Could not allocate memory for catalog");
140 return -ENOMEM;
143 ret = bdrv_pread(bs->file->bs, le32_to_cpu(bochs.header), s->catalog_bitmap,
144 s->catalog_size * 4);
145 if (ret < 0) {
146 goto fail;
149 for (i = 0; i < s->catalog_size; i++)
150 le32_to_cpus(&s->catalog_bitmap[i]);
152 s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
154 s->bitmap_blocks = 1 + (le32_to_cpu(bochs.bitmap) - 1) / 512;
155 s->extent_blocks = 1 + (le32_to_cpu(bochs.extent) - 1) / 512;
157 s->extent_size = le32_to_cpu(bochs.extent);
158 if (s->extent_size < BDRV_SECTOR_SIZE) {
159 /* bximage actually never creates extents smaller than 4k */
160 error_setg(errp, "Extent size must be at least 512");
161 ret = -EINVAL;
162 goto fail;
163 } else if (!is_power_of_2(s->extent_size)) {
164 error_setg(errp, "Extent size %" PRIu32 " is not a power of two",
165 s->extent_size);
166 ret = -EINVAL;
167 goto fail;
168 } else if (s->extent_size > 0x800000) {
169 error_setg(errp, "Extent size %" PRIu32 " is too large",
170 s->extent_size);
171 ret = -EINVAL;
172 goto fail;
175 if (s->catalog_size < DIV_ROUND_UP(bs->total_sectors,
176 s->extent_size / BDRV_SECTOR_SIZE))
178 error_setg(errp, "Catalog size is too small for this disk size");
179 ret = -EINVAL;
180 goto fail;
183 qemu_co_mutex_init(&s->lock);
184 return 0;
186 fail:
187 g_free(s->catalog_bitmap);
188 return ret;
191 static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
193 BDRVBochsState *s = bs->opaque;
194 uint64_t offset = sector_num * 512;
195 uint64_t extent_index, extent_offset, bitmap_offset;
196 char bitmap_entry;
197 int ret;
199 // seek to sector
200 extent_index = offset / s->extent_size;
201 extent_offset = (offset % s->extent_size) / 512;
203 if (s->catalog_bitmap[extent_index] == 0xffffffff) {
204 return 0; /* not allocated */
207 bitmap_offset = s->data_offset +
208 (512 * (uint64_t) s->catalog_bitmap[extent_index] *
209 (s->extent_blocks + s->bitmap_blocks));
211 /* read in bitmap for current extent */
212 ret = bdrv_pread(bs->file->bs, bitmap_offset + (extent_offset / 8),
213 &bitmap_entry, 1);
214 if (ret < 0) {
215 return ret;
218 if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
219 return 0; /* not allocated */
222 return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
225 static int coroutine_fn
226 bochs_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
227 QEMUIOVector *qiov, int flags)
229 BDRVBochsState *s = bs->opaque;
230 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
231 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
232 uint64_t bytes_done = 0;
233 QEMUIOVector local_qiov;
234 int ret;
236 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
237 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
239 qemu_iovec_init(&local_qiov, qiov->niov);
240 qemu_co_mutex_lock(&s->lock);
242 while (nb_sectors > 0) {
243 int64_t block_offset = seek_to_sector(bs, sector_num);
244 if (block_offset < 0) {
245 ret = block_offset;
246 goto fail;
249 qemu_iovec_reset(&local_qiov);
250 qemu_iovec_concat(&local_qiov, qiov, bytes_done, 512);
252 if (block_offset > 0) {
253 ret = bdrv_co_preadv(bs->file->bs, block_offset, 512,
254 &local_qiov, 0);
255 if (ret < 0) {
256 goto fail;
258 } else {
259 qemu_iovec_memset(&local_qiov, 0, 0, 512);
261 nb_sectors--;
262 sector_num++;
263 bytes_done += 512;
266 ret = 0;
267 fail:
268 qemu_co_mutex_unlock(&s->lock);
269 qemu_iovec_destroy(&local_qiov);
271 return ret;
274 static void bochs_close(BlockDriverState *bs)
276 BDRVBochsState *s = bs->opaque;
277 g_free(s->catalog_bitmap);
280 static BlockDriver bdrv_bochs = {
281 .format_name = "bochs",
282 .instance_size = sizeof(BDRVBochsState),
283 .bdrv_probe = bochs_probe,
284 .bdrv_open = bochs_open,
285 .bdrv_co_preadv = bochs_co_preadv,
286 .bdrv_close = bochs_close,
289 static void bdrv_bochs_init(void)
291 bdrv_register(&bdrv_bochs);
294 block_init(bdrv_bochs_init);