]> git.proxmox.com Git - mirror_qemu.git/blob - block/parallels.c
block/parallels: mark parallels format driver as zero inited
[mirror_qemu.git] / block / parallels.c
1 /*
2 * Block driver for Parallels disk image format
3 *
4 * Copyright (c) 2007 Alex Beregszaszi
5 *
6 * This code is based on comparing different disk images created by Parallels.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26 #include "qemu-common.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29
30 /**************************************************************/
31
32 #define HEADER_MAGIC "WithoutFreeSpace"
33 #define HEADER_MAGIC2 "WithouFreSpacExt"
34 #define HEADER_VERSION 2
35
36 // always little-endian
37 typedef struct ParallelsHeader {
38 char magic[16]; // "WithoutFreeSpace"
39 uint32_t version;
40 uint32_t heads;
41 uint32_t cylinders;
42 uint32_t tracks;
43 uint32_t catalog_entries;
44 uint64_t nb_sectors;
45 uint32_t inuse;
46 uint32_t data_off;
47 char padding[12];
48 } QEMU_PACKED ParallelsHeader;
49
50 typedef struct BDRVParallelsState {
51 /** Locking is conservative, the lock protects
52 * - image file extending (truncate, fallocate)
53 * - any access to block allocation table
54 */
55 CoMutex lock;
56
57 uint32_t *catalog_bitmap;
58 unsigned int catalog_size;
59
60 unsigned int tracks;
61
62 unsigned int off_multiplier;
63 } BDRVParallelsState;
64
65 static int parallels_probe(const uint8_t *buf, int buf_size, const char *filename)
66 {
67 const ParallelsHeader *ph = (const void *)buf;
68
69 if (buf_size < sizeof(ParallelsHeader))
70 return 0;
71
72 if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
73 !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
74 (le32_to_cpu(ph->version) == HEADER_VERSION))
75 return 100;
76
77 return 0;
78 }
79
80 static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
81 Error **errp)
82 {
83 BDRVParallelsState *s = bs->opaque;
84 int i;
85 ParallelsHeader ph;
86 int ret;
87
88 bs->read_only = 1; // no write support yet
89
90 ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
91 if (ret < 0) {
92 goto fail;
93 }
94
95 bs->total_sectors = le64_to_cpu(ph.nb_sectors);
96
97 if (le32_to_cpu(ph.version) != HEADER_VERSION) {
98 goto fail_format;
99 }
100 if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
101 s->off_multiplier = 1;
102 bs->total_sectors = 0xffffffff & bs->total_sectors;
103 } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
104 s->off_multiplier = le32_to_cpu(ph.tracks);
105 } else {
106 goto fail_format;
107 }
108
109 s->tracks = le32_to_cpu(ph.tracks);
110 if (s->tracks == 0) {
111 error_setg(errp, "Invalid image: Zero sectors per track");
112 ret = -EINVAL;
113 goto fail;
114 }
115 if (s->tracks > INT32_MAX/513) {
116 error_setg(errp, "Invalid image: Too big cluster");
117 ret = -EFBIG;
118 goto fail;
119 }
120
121 s->catalog_size = le32_to_cpu(ph.catalog_entries);
122 if (s->catalog_size > INT_MAX / sizeof(uint32_t)) {
123 error_setg(errp, "Catalog too large");
124 ret = -EFBIG;
125 goto fail;
126 }
127 s->catalog_bitmap = g_try_new(uint32_t, s->catalog_size);
128 if (s->catalog_size && s->catalog_bitmap == NULL) {
129 ret = -ENOMEM;
130 goto fail;
131 }
132
133 ret = bdrv_pread(bs->file, sizeof(ParallelsHeader),
134 s->catalog_bitmap, s->catalog_size * sizeof(uint32_t));
135 if (ret < 0) {
136 goto fail;
137 }
138
139 for (i = 0; i < s->catalog_size; i++)
140 le32_to_cpus(&s->catalog_bitmap[i]);
141
142 qemu_co_mutex_init(&s->lock);
143 return 0;
144
145 fail_format:
146 error_setg(errp, "Image not in Parallels format");
147 ret = -EINVAL;
148 fail:
149 g_free(s->catalog_bitmap);
150 return ret;
151 }
152
153 static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
154 {
155 uint32_t index, offset;
156
157 index = sector_num / s->tracks;
158 offset = sector_num % s->tracks;
159
160 /* not allocated */
161 if ((index >= s->catalog_size) || (s->catalog_bitmap[index] == 0))
162 return -1;
163 return (uint64_t)s->catalog_bitmap[index] * s->off_multiplier + offset;
164 }
165
166 static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num,
167 int nb_sectors)
168 {
169 int ret = s->tracks - sector_num % s->tracks;
170 return MIN(nb_sectors, ret);
171 }
172
173 static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs,
174 int64_t sector_num, int nb_sectors, int *pnum)
175 {
176 BDRVParallelsState *s = bs->opaque;
177 int64_t offset;
178
179 qemu_co_mutex_lock(&s->lock);
180 offset = seek_to_sector(s, sector_num);
181 qemu_co_mutex_unlock(&s->lock);
182
183 *pnum = cluster_remainder(s, sector_num, nb_sectors);
184
185 if (offset < 0) {
186 return 0;
187 }
188
189 return (offset << BDRV_SECTOR_BITS) |
190 BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
191 }
192
193 static coroutine_fn int parallels_co_readv(BlockDriverState *bs,
194 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
195 {
196 BDRVParallelsState *s = bs->opaque;
197 uint64_t bytes_done = 0;
198 QEMUIOVector hd_qiov;
199 int ret = 0;
200
201 qemu_iovec_init(&hd_qiov, qiov->niov);
202
203 while (nb_sectors > 0) {
204 int64_t position;
205 int n, nbytes;
206
207 qemu_co_mutex_lock(&s->lock);
208 position = seek_to_sector(s, sector_num);
209 qemu_co_mutex_unlock(&s->lock);
210
211 n = cluster_remainder(s, sector_num, nb_sectors);
212 nbytes = n << BDRV_SECTOR_BITS;
213
214 if (position < 0) {
215 qemu_iovec_memset(qiov, bytes_done, 0, nbytes);
216 } else {
217 qemu_iovec_reset(&hd_qiov);
218 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
219
220 ret = bdrv_co_readv(bs->file, position, n, &hd_qiov);
221 if (ret < 0) {
222 break;
223 }
224 }
225
226 nb_sectors -= n;
227 sector_num += n;
228 bytes_done += nbytes;
229 }
230
231 qemu_iovec_destroy(&hd_qiov);
232 return ret;
233 }
234
235 static void parallels_close(BlockDriverState *bs)
236 {
237 BDRVParallelsState *s = bs->opaque;
238 g_free(s->catalog_bitmap);
239 }
240
241 static BlockDriver bdrv_parallels = {
242 .format_name = "parallels",
243 .instance_size = sizeof(BDRVParallelsState),
244 .bdrv_probe = parallels_probe,
245 .bdrv_open = parallels_open,
246 .bdrv_close = parallels_close,
247 .bdrv_co_get_block_status = parallels_co_get_block_status,
248 .bdrv_has_zero_init = bdrv_has_zero_init_1,
249 .bdrv_co_readv = parallels_co_readv,
250 };
251
252 static void bdrv_parallels_init(void)
253 {
254 bdrv_register(&bdrv_parallels);
255 }
256
257 block_init(bdrv_parallels_init);