blob: ee390815dc5e589a920ec4cd895553a102f1274d [file] [log] [blame]
ths6ada7452007-07-31 23:28:53 +00001/*
2 * Block driver for Parallels disk image format
3 *
4 * Copyright (c) 2007 Alex Beregszaszi
Denis V. Lunevcc5690f2015-04-28 10:46:46 +03005 * Copyright (c) 2015 Denis V. Lunev <den@openvz.org>
ths6ada7452007-07-31 23:28:53 +00006 *
Denis V. Lunevcc5690f2015-04-28 10:46:46 +03007 * This code was originally based on comparing different disk images created
8 * by Parallels. Currently it is based on opened OpenVZ sources
9 * available at
10 * http://git.openvz.org/?p=ploop;a=summary
ths6ada7452007-07-31 23:28:53 +000011 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE SOFTWARE.
29 */
Peter Maydell80c71a22016-01-18 18:01:42 +000030#include "qemu/osdep.h"
pbrookfaf07962007-11-11 02:51:17 +000031#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010032#include "block/block_int.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010033#include "qemu/module.h"
Denis V. Lunev0d31c7c2015-04-28 10:46:57 +030034#include "qemu/bitmap.h"
Denis V. Lunevd6179012015-04-28 10:46:58 +030035#include "qapi/util.h"
ths6ada7452007-07-31 23:28:53 +000036
37/**************************************************************/
38
39#define HEADER_MAGIC "WithoutFreeSpace"
Denis V. Lunevd25d5982014-07-28 20:23:55 +040040#define HEADER_MAGIC2 "WithouFreSpacExt"
ths6ada7452007-07-31 23:28:53 +000041#define HEADER_VERSION 2
Denis V. Lunev6dd6b9f2015-04-28 10:46:53 +030042#define HEADER_INUSE_MAGIC (0x746F6E59)
ths6ada7452007-07-31 23:28:53 +000043
Denis V. Lunev74cf6c52015-04-28 10:46:44 +030044#define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */
45
46
ths6ada7452007-07-31 23:28:53 +000047// always little-endian
Denis V. Lunev07898902015-04-28 10:46:35 +030048typedef struct ParallelsHeader {
ths6ada7452007-07-31 23:28:53 +000049 char magic[16]; // "WithoutFreeSpace"
50 uint32_t version;
51 uint32_t heads;
52 uint32_t cylinders;
53 uint32_t tracks;
Denis V. Lunev369f7de2015-04-28 10:46:47 +030054 uint32_t bat_entries;
Denis V. Lunev8c27d542014-07-28 20:23:52 +040055 uint64_t nb_sectors;
56 uint32_t inuse;
57 uint32_t data_off;
58 char padding[12];
Denis V. Lunev07898902015-04-28 10:46:35 +030059} QEMU_PACKED ParallelsHeader;
ths6ada7452007-07-31 23:28:53 +000060
Denis V. Lunevd6179012015-04-28 10:46:58 +030061
62typedef enum ParallelsPreallocMode {
63 PRL_PREALLOC_MODE_FALLOCATE = 0,
64 PRL_PREALLOC_MODE_TRUNCATE = 1,
Eric Blake7fb1cf12015-11-18 01:52:57 -070065 PRL_PREALLOC_MODE__MAX = 2,
Denis V. Lunevd6179012015-04-28 10:46:58 +030066} ParallelsPreallocMode;
67
68static const char *prealloc_mode_lookup[] = {
69 "falloc",
70 "truncate",
71 NULL,
72};
73
74
ths6ada7452007-07-31 23:28:53 +000075typedef struct BDRVParallelsState {
Denis V. Lunev481fb9c2015-04-28 10:46:39 +030076 /** Locking is conservative, the lock protects
77 * - image file extending (truncate, fallocate)
78 * - any access to block allocation table
79 */
Paolo Bonzini848c66e2011-10-20 13:16:21 +020080 CoMutex lock;
ths6ada7452007-07-31 23:28:53 +000081
Denis V. Lunev9eae9cc2015-04-28 10:46:50 +030082 ParallelsHeader *header;
83 uint32_t header_size;
Denis V. Lunev6dd6b9f2015-04-28 10:46:53 +030084 bool header_unclean;
85
Denis V. Lunev0d31c7c2015-04-28 10:46:57 +030086 unsigned long *bat_dirty_bmap;
87 unsigned int bat_dirty_block;
88
Denis V. Lunev369f7de2015-04-28 10:46:47 +030089 uint32_t *bat_bitmap;
90 unsigned int bat_size;
ths6ada7452007-07-31 23:28:53 +000091
Denis V. Lunev19f5dc12015-04-28 10:46:59 +030092 int64_t data_end;
Denis V. Lunevd6179012015-04-28 10:46:58 +030093 uint64_t prealloc_size;
94 ParallelsPreallocMode prealloc_mode;
95
Kevin Wolf9302e862014-03-26 13:06:09 +010096 unsigned int tracks;
Denis V. Lunevd25d5982014-07-28 20:23:55 +040097
98 unsigned int off_multiplier;
ths6ada7452007-07-31 23:28:53 +000099} BDRVParallelsState;
100
Denis V. Lunev555cc9d2015-04-28 10:46:48 +0300101
Denis V. Lunevd6179012015-04-28 10:46:58 +0300102#define PARALLELS_OPT_PREALLOC_MODE "prealloc-mode"
103#define PARALLELS_OPT_PREALLOC_SIZE "prealloc-size"
104
105static QemuOptsList parallels_runtime_opts = {
106 .name = "parallels",
107 .head = QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts.head),
108 .desc = {
109 {
110 .name = PARALLELS_OPT_PREALLOC_SIZE,
111 .type = QEMU_OPT_SIZE,
112 .help = "Preallocation size on image expansion",
113 .def_value_str = "128MiB",
114 },
115 {
116 .name = PARALLELS_OPT_PREALLOC_MODE,
117 .type = QEMU_OPT_STRING,
118 .help = "Preallocation mode on image expansion "
119 "(allowed values: falloc, truncate)",
120 .def_value_str = "falloc",
121 },
122 { /* end of list */ },
123 },
124};
125
126
Denis V. Lunev555cc9d2015-04-28 10:46:48 +0300127static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx)
128{
Denis V. Lunevdd97cdc2015-04-28 10:46:49 +0300129 return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier;
Denis V. Lunev555cc9d2015-04-28 10:46:48 +0300130}
131
Denis V. Lunev2d68e222015-04-28 10:46:56 +0300132static uint32_t bat_entry_off(uint32_t idx)
133{
134 return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx;
135}
136
Roman Kagan29442562015-04-28 10:46:36 +0300137static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
ths6ada7452007-07-31 23:28:53 +0000138{
David Woodhousec34d2452009-11-07 14:10:01 +0000139 uint32_t index, offset;
ths6ada7452007-07-31 23:28:53 +0000140
141 index = sector_num / s->tracks;
142 offset = sector_num % s->tracks;
143
Christoph Hellwig9d8b88f2010-05-06 22:04:34 +0200144 /* not allocated */
Denis V. Lunev369f7de2015-04-28 10:46:47 +0300145 if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) {
Denis V. Lunevf08e2f82014-07-28 20:23:53 +0400146 return -1;
Denis V. Lunev369f7de2015-04-28 10:46:47 +0300147 }
Denis V. Lunev555cc9d2015-04-28 10:46:48 +0300148 return bat2sect(s, index) + offset;
ths6ada7452007-07-31 23:28:53 +0000149}
150
Roman Kagan9de9da12015-04-28 10:46:37 +0300151static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num,
152 int nb_sectors)
153{
154 int ret = s->tracks - sector_num % s->tracks;
155 return MIN(nb_sectors, ret);
156}
157
Denis V. Lunev6953d922015-04-28 10:46:55 +0300158static int64_t block_status(BDRVParallelsState *s, int64_t sector_num,
159 int nb_sectors, int *pnum)
160{
161 int64_t start_off = -2, prev_end_off = -2;
162
163 *pnum = 0;
164 while (nb_sectors > 0 || start_off == -2) {
165 int64_t offset = seek_to_sector(s, sector_num);
166 int to_end;
167
168 if (start_off == -2) {
169 start_off = offset;
170 prev_end_off = offset;
171 } else if (offset != prev_end_off) {
172 break;
173 }
174
175 to_end = cluster_remainder(s, sector_num, nb_sectors);
176 nb_sectors -= to_end;
177 sector_num += to_end;
178 *pnum += to_end;
179
180 if (offset > 0) {
181 prev_end_off += to_end;
182 }
183 }
184 return start_off;
185}
186
Denis V. Lunevddd2ef22015-04-28 10:47:00 +0300187static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
188 int nb_sectors, int *pnum)
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300189{
190 BDRVParallelsState *s = bs->opaque;
Denis V. Lunevddd2ef22015-04-28 10:47:00 +0300191 uint32_t idx, to_allocate, i;
192 int64_t pos, space;
193
194 pos = block_status(s, sector_num, nb_sectors, pnum);
195 if (pos > 0) {
196 return pos;
197 }
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300198
199 idx = sector_num / s->tracks;
Denis V. Lunev369f7de2015-04-28 10:46:47 +0300200 if (idx >= s->bat_size) {
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300201 return -EINVAL;
202 }
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300203
Denis V. Lunevddd2ef22015-04-28 10:47:00 +0300204 to_allocate = (sector_num + *pnum + s->tracks - 1) / s->tracks - idx;
205 space = to_allocate * s->tracks;
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200206 if (s->data_end + space > bdrv_getlength(bs->file->bs) >> BDRV_SECTOR_BITS) {
Denis V. Lunev19f5dc12015-04-28 10:46:59 +0300207 int ret;
Denis V. Lunevddd2ef22015-04-28 10:47:00 +0300208 space += s->prealloc_size;
Denis V. Lunev19f5dc12015-04-28 10:46:59 +0300209 if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200210 ret = bdrv_write_zeroes(bs->file->bs, s->data_end, space, 0);
Denis V. Lunev19f5dc12015-04-28 10:46:59 +0300211 } else {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200212 ret = bdrv_truncate(bs->file->bs,
Denis V. Lunevddd2ef22015-04-28 10:47:00 +0300213 (s->data_end + space) << BDRV_SECTOR_BITS);
Denis V. Lunev19f5dc12015-04-28 10:46:59 +0300214 }
215 if (ret < 0) {
216 return ret;
217 }
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300218 }
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300219
Denis V. Lunevddd2ef22015-04-28 10:47:00 +0300220 for (i = 0; i < to_allocate; i++) {
221 s->bat_bitmap[idx + i] = cpu_to_le32(s->data_end / s->off_multiplier);
222 s->data_end += s->tracks;
223 bitmap_set(s->bat_dirty_bmap,
Vladimir Sementsov-Ogievskiyc9f68562015-11-17 20:02:58 +0300224 bat_entry_off(idx + i) / s->bat_dirty_block, 1);
Denis V. Lunevddd2ef22015-04-28 10:47:00 +0300225 }
Denis V. Lunev0d31c7c2015-04-28 10:46:57 +0300226
Denis V. Lunevddd2ef22015-04-28 10:47:00 +0300227 return bat2sect(s, idx) + sector_num % s->tracks;
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300228}
229
Denis V. Lunev0d31c7c2015-04-28 10:46:57 +0300230
231static coroutine_fn int parallels_co_flush_to_os(BlockDriverState *bs)
232{
233 BDRVParallelsState *s = bs->opaque;
234 unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block);
235 unsigned long bit;
236
237 qemu_co_mutex_lock(&s->lock);
238
239 bit = find_first_bit(s->bat_dirty_bmap, size);
240 while (bit < size) {
241 uint32_t off = bit * s->bat_dirty_block;
242 uint32_t to_write = s->bat_dirty_block;
243 int ret;
244
245 if (off + to_write > s->header_size) {
246 to_write = s->header_size - off;
247 }
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200248 ret = bdrv_pwrite(bs->file->bs, off, (uint8_t *)s->header + off,
249 to_write);
Denis V. Lunev0d31c7c2015-04-28 10:46:57 +0300250 if (ret < 0) {
251 qemu_co_mutex_unlock(&s->lock);
252 return ret;
253 }
254 bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1);
255 }
256 bitmap_zero(s->bat_dirty_bmap, size);
257
258 qemu_co_mutex_unlock(&s->lock);
259 return 0;
260}
261
262
Roman Kagandd3bed12015-04-28 10:46:38 +0300263static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs,
264 int64_t sector_num, int nb_sectors, int *pnum)
265{
266 BDRVParallelsState *s = bs->opaque;
267 int64_t offset;
268
269 qemu_co_mutex_lock(&s->lock);
Denis V. Lunev6953d922015-04-28 10:46:55 +0300270 offset = block_status(s, sector_num, nb_sectors, pnum);
Roman Kagandd3bed12015-04-28 10:46:38 +0300271 qemu_co_mutex_unlock(&s->lock);
272
Roman Kagandd3bed12015-04-28 10:46:38 +0300273 if (offset < 0) {
274 return 0;
275 }
276
277 return (offset << BDRV_SECTOR_BITS) |
278 BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
279}
280
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300281static coroutine_fn int parallels_co_writev(BlockDriverState *bs,
282 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
283{
284 BDRVParallelsState *s = bs->opaque;
285 uint64_t bytes_done = 0;
286 QEMUIOVector hd_qiov;
287 int ret = 0;
288
289 qemu_iovec_init(&hd_qiov, qiov->niov);
290
291 while (nb_sectors > 0) {
292 int64_t position;
293 int n, nbytes;
294
295 qemu_co_mutex_lock(&s->lock);
Denis V. Lunevddd2ef22015-04-28 10:47:00 +0300296 position = allocate_clusters(bs, sector_num, nb_sectors, &n);
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300297 qemu_co_mutex_unlock(&s->lock);
298 if (position < 0) {
299 ret = (int)position;
300 break;
301 }
302
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300303 nbytes = n << BDRV_SECTOR_BITS;
304
305 qemu_iovec_reset(&hd_qiov);
306 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
307
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200308 ret = bdrv_co_writev(bs->file->bs, position, n, &hd_qiov);
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300309 if (ret < 0) {
310 break;
311 }
312
313 nb_sectors -= n;
314 sector_num += n;
315 bytes_done += nbytes;
316 }
317
318 qemu_iovec_destroy(&hd_qiov);
319 return ret;
320}
321
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300322static coroutine_fn int parallels_co_readv(BlockDriverState *bs,
323 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
ths6ada7452007-07-31 23:28:53 +0000324{
Roman Kagan29442562015-04-28 10:46:36 +0300325 BDRVParallelsState *s = bs->opaque;
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300326 uint64_t bytes_done = 0;
327 QEMUIOVector hd_qiov;
328 int ret = 0;
329
330 qemu_iovec_init(&hd_qiov, qiov->niov);
Roman Kagan29442562015-04-28 10:46:36 +0300331
ths6ada7452007-07-31 23:28:53 +0000332 while (nb_sectors > 0) {
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300333 int64_t position;
334 int n, nbytes;
335
336 qemu_co_mutex_lock(&s->lock);
Denis V. Lunev6953d922015-04-28 10:46:55 +0300337 position = block_status(s, sector_num, nb_sectors, &n);
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300338 qemu_co_mutex_unlock(&s->lock);
339
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300340 nbytes = n << BDRV_SECTOR_BITS;
341
342 if (position < 0) {
343 qemu_iovec_memset(qiov, bytes_done, 0, nbytes);
Christoph Hellwig9d8b88f2010-05-06 22:04:34 +0200344 } else {
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300345 qemu_iovec_reset(&hd_qiov);
346 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
347
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200348 ret = bdrv_co_readv(bs->file->bs, position, n, &hd_qiov);
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300349 if (ret < 0) {
350 break;
351 }
Christoph Hellwig9d8b88f2010-05-06 22:04:34 +0200352 }
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300353
Roman Kagan9de9da12015-04-28 10:46:37 +0300354 nb_sectors -= n;
355 sector_num += n;
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300356 bytes_done += nbytes;
ths6ada7452007-07-31 23:28:53 +0000357 }
ths6ada7452007-07-31 23:28:53 +0000358
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300359 qemu_iovec_destroy(&hd_qiov);
Paolo Bonzini2914caa2011-10-20 13:16:22 +0200360 return ret;
361}
362
Denis V. Lunev49ad6462015-04-28 10:46:52 +0300363
364static int parallels_check(BlockDriverState *bs, BdrvCheckResult *res,
365 BdrvCheckMode fix)
366{
367 BDRVParallelsState *s = bs->opaque;
368 int64_t size, prev_off, high_off;
369 int ret;
370 uint32_t i;
371 bool flush_bat = false;
372 int cluster_size = s->tracks << BDRV_SECTOR_BITS;
373
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200374 size = bdrv_getlength(bs->file->bs);
Denis V. Lunev49ad6462015-04-28 10:46:52 +0300375 if (size < 0) {
376 res->check_errors++;
377 return size;
378 }
379
Denis V. Lunev6dd6b9f2015-04-28 10:46:53 +0300380 if (s->header_unclean) {
381 fprintf(stderr, "%s image was not closed correctly\n",
382 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
383 res->corruptions++;
384 if (fix & BDRV_FIX_ERRORS) {
385 /* parallels_close will do the job right */
386 res->corruptions_fixed++;
387 s->header_unclean = false;
388 }
389 }
390
Denis V. Lunev49ad6462015-04-28 10:46:52 +0300391 res->bfi.total_clusters = s->bat_size;
392 res->bfi.compressed_clusters = 0; /* compression is not supported */
393
394 high_off = 0;
395 prev_off = 0;
396 for (i = 0; i < s->bat_size; i++) {
397 int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
398 if (off == 0) {
399 prev_off = 0;
400 continue;
401 }
402
403 /* cluster outside the image */
404 if (off > size) {
405 fprintf(stderr, "%s cluster %u is outside image\n",
406 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
407 res->corruptions++;
408 if (fix & BDRV_FIX_ERRORS) {
409 prev_off = 0;
410 s->bat_bitmap[i] = 0;
411 res->corruptions_fixed++;
412 flush_bat = true;
413 continue;
414 }
415 }
416
417 res->bfi.allocated_clusters++;
418 if (off > high_off) {
419 high_off = off;
420 }
421
422 if (prev_off != 0 && (prev_off + cluster_size) != off) {
423 res->bfi.fragmented_clusters++;
424 }
425 prev_off = off;
426 }
427
428 if (flush_bat) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200429 ret = bdrv_pwrite_sync(bs->file->bs, 0, s->header, s->header_size);
Denis V. Lunev49ad6462015-04-28 10:46:52 +0300430 if (ret < 0) {
431 res->check_errors++;
432 return ret;
433 }
434 }
435
436 res->image_end_offset = high_off + cluster_size;
437 if (size > res->image_end_offset) {
438 int64_t count;
439 count = DIV_ROUND_UP(size - res->image_end_offset, cluster_size);
440 fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n",
441 fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
442 size - res->image_end_offset);
443 res->leaks += count;
444 if (fix & BDRV_FIX_LEAKS) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200445 ret = bdrv_truncate(bs->file->bs, res->image_end_offset);
Denis V. Lunev49ad6462015-04-28 10:46:52 +0300446 if (ret < 0) {
447 res->check_errors++;
448 return ret;
449 }
450 res->leaks_fixed += count;
451 }
452 }
453
454 return 0;
455}
456
457
Denis V. Lunev74cf6c52015-04-28 10:46:44 +0300458static int parallels_create(const char *filename, QemuOpts *opts, Error **errp)
459{
460 int64_t total_size, cl_size;
461 uint8_t tmp[BDRV_SECTOR_SIZE];
462 Error *local_err = NULL;
463 BlockDriverState *file;
Denis V. Lunev369f7de2015-04-28 10:46:47 +0300464 uint32_t bat_entries, bat_sectors;
Denis V. Lunev74cf6c52015-04-28 10:46:44 +0300465 ParallelsHeader header;
466 int ret;
467
468 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
469 BDRV_SECTOR_SIZE);
470 cl_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
471 DEFAULT_CLUSTER_SIZE), BDRV_SECTOR_SIZE);
472
473 ret = bdrv_create_file(filename, opts, &local_err);
474 if (ret < 0) {
475 error_propagate(errp, local_err);
476 return ret;
477 }
478
479 file = NULL;
480 ret = bdrv_open(&file, filename, NULL, NULL,
Max Reitz6ebf9aa2015-08-26 19:47:49 +0200481 BDRV_O_RDWR | BDRV_O_PROTOCOL, &local_err);
Denis V. Lunev74cf6c52015-04-28 10:46:44 +0300482 if (ret < 0) {
483 error_propagate(errp, local_err);
484 return ret;
485 }
486 ret = bdrv_truncate(file, 0);
487 if (ret < 0) {
488 goto exit;
489 }
490
Denis V. Lunev369f7de2015-04-28 10:46:47 +0300491 bat_entries = DIV_ROUND_UP(total_size, cl_size);
Denis V. Lunev2d68e222015-04-28 10:46:56 +0300492 bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size);
Denis V. Lunev369f7de2015-04-28 10:46:47 +0300493 bat_sectors = (bat_sectors * cl_size) >> BDRV_SECTOR_BITS;
Denis V. Lunev74cf6c52015-04-28 10:46:44 +0300494
495 memset(&header, 0, sizeof(header));
496 memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic));
497 header.version = cpu_to_le32(HEADER_VERSION);
498 /* don't care much about geometry, it is not used on image level */
499 header.heads = cpu_to_le32(16);
500 header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE / 16 / 32);
501 header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS);
Denis V. Lunev369f7de2015-04-28 10:46:47 +0300502 header.bat_entries = cpu_to_le32(bat_entries);
Denis V. Lunev74cf6c52015-04-28 10:46:44 +0300503 header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE));
Denis V. Lunev369f7de2015-04-28 10:46:47 +0300504 header.data_off = cpu_to_le32(bat_sectors);
Denis V. Lunev74cf6c52015-04-28 10:46:44 +0300505
506 /* write all the data */
507 memset(tmp, 0, sizeof(tmp));
508 memcpy(tmp, &header, sizeof(header));
509
510 ret = bdrv_pwrite(file, 0, tmp, BDRV_SECTOR_SIZE);
511 if (ret < 0) {
512 goto exit;
513 }
Denis V. Lunev369f7de2015-04-28 10:46:47 +0300514 ret = bdrv_write_zeroes(file, 1, bat_sectors - 1, 0);
Denis V. Lunev74cf6c52015-04-28 10:46:44 +0300515 if (ret < 0) {
516 goto exit;
517 }
518 ret = 0;
519
520done:
521 bdrv_unref(file);
522 return ret;
523
524exit:
525 error_setg_errno(errp, -ret, "Failed to create Parallels image");
526 goto done;
527}
528
Denis V. Lunev23d6bd32015-04-28 10:46:51 +0300529
530static int parallels_probe(const uint8_t *buf, int buf_size,
531 const char *filename)
532{
533 const ParallelsHeader *ph = (const void *)buf;
534
535 if (buf_size < sizeof(ParallelsHeader)) {
536 return 0;
537 }
538
539 if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
540 !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
541 (le32_to_cpu(ph->version) == HEADER_VERSION)) {
542 return 100;
543 }
544
545 return 0;
546}
547
Denis V. Lunev6dd6b9f2015-04-28 10:46:53 +0300548static int parallels_update_header(BlockDriverState *bs)
549{
550 BDRVParallelsState *s = bs->opaque;
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200551 unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs),
552 sizeof(ParallelsHeader));
Denis V. Lunev6dd6b9f2015-04-28 10:46:53 +0300553
554 if (size > s->header_size) {
555 size = s->header_size;
556 }
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200557 return bdrv_pwrite_sync(bs->file->bs, 0, s->header, size);
Denis V. Lunev6dd6b9f2015-04-28 10:46:53 +0300558}
559
Denis V. Lunev23d6bd32015-04-28 10:46:51 +0300560static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
561 Error **errp)
562{
563 BDRVParallelsState *s = bs->opaque;
564 ParallelsHeader ph;
Denis V. Lunev19f5dc12015-04-28 10:46:59 +0300565 int ret, size, i;
Denis V. Lunevd6179012015-04-28 10:46:58 +0300566 QemuOpts *opts = NULL;
567 Error *local_err = NULL;
568 char *buf;
Denis V. Lunev23d6bd32015-04-28 10:46:51 +0300569
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200570 ret = bdrv_pread(bs->file->bs, 0, &ph, sizeof(ph));
Denis V. Lunev23d6bd32015-04-28 10:46:51 +0300571 if (ret < 0) {
572 goto fail;
573 }
574
575 bs->total_sectors = le64_to_cpu(ph.nb_sectors);
576
577 if (le32_to_cpu(ph.version) != HEADER_VERSION) {
578 goto fail_format;
579 }
580 if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
581 s->off_multiplier = 1;
582 bs->total_sectors = 0xffffffff & bs->total_sectors;
583 } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
584 s->off_multiplier = le32_to_cpu(ph.tracks);
585 } else {
586 goto fail_format;
587 }
588
589 s->tracks = le32_to_cpu(ph.tracks);
590 if (s->tracks == 0) {
591 error_setg(errp, "Invalid image: Zero sectors per track");
592 ret = -EINVAL;
593 goto fail;
594 }
595 if (s->tracks > INT32_MAX/513) {
596 error_setg(errp, "Invalid image: Too big cluster");
597 ret = -EFBIG;
598 goto fail;
599 }
600
601 s->bat_size = le32_to_cpu(ph.bat_entries);
602 if (s->bat_size > INT_MAX / sizeof(uint32_t)) {
603 error_setg(errp, "Catalog too large");
604 ret = -EFBIG;
605 goto fail;
606 }
607
Denis V. Lunev2d68e222015-04-28 10:46:56 +0300608 size = bat_entry_off(s->bat_size);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200609 s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file->bs));
610 s->header = qemu_try_blockalign(bs->file->bs, s->header_size);
Denis V. Lunev23d6bd32015-04-28 10:46:51 +0300611 if (s->header == NULL) {
612 ret = -ENOMEM;
613 goto fail;
614 }
Denis V. Lunev19f5dc12015-04-28 10:46:59 +0300615 s->data_end = le32_to_cpu(ph.data_off);
616 if (s->data_end == 0) {
617 s->data_end = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE);
618 }
619 if (s->data_end < s->header_size) {
Denis V. Lunev23d6bd32015-04-28 10:46:51 +0300620 /* there is not enough unused space to fit to block align between BAT
621 and actual data. We can't avoid read-modify-write... */
622 s->header_size = size;
623 }
624
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200625 ret = bdrv_pread(bs->file->bs, 0, s->header, s->header_size);
Denis V. Lunev23d6bd32015-04-28 10:46:51 +0300626 if (ret < 0) {
627 goto fail;
628 }
629 s->bat_bitmap = (uint32_t *)(s->header + 1);
630
Denis V. Lunev19f5dc12015-04-28 10:46:59 +0300631 for (i = 0; i < s->bat_size; i++) {
632 int64_t off = bat2sect(s, i);
633 if (off >= s->data_end) {
634 s->data_end = off + s->tracks;
635 }
636 }
637
Denis V. Lunev6dd6b9f2015-04-28 10:46:53 +0300638 if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
639 /* Image was not closed correctly. The check is mandatory */
640 s->header_unclean = true;
641 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
642 error_setg(errp, "parallels: Image was not closed correctly; "
643 "cannot be opened read/write");
644 ret = -EACCES;
645 goto fail;
646 }
647 }
648
Denis V. Lunevd6179012015-04-28 10:46:58 +0300649 opts = qemu_opts_create(&parallels_runtime_opts, NULL, 0, &local_err);
650 if (local_err != NULL) {
651 goto fail_options;
652 }
653
654 qemu_opts_absorb_qdict(opts, options, &local_err);
655 if (local_err != NULL) {
656 goto fail_options;
657 }
658
659 s->prealloc_size =
660 qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
661 s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS);
662 buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
663 s->prealloc_mode = qapi_enum_parse(prealloc_mode_lookup, buf,
Eric Blake7fb1cf12015-11-18 01:52:57 -0700664 PRL_PREALLOC_MODE__MAX, PRL_PREALLOC_MODE_FALLOCATE, &local_err);
Denis V. Lunevd6179012015-04-28 10:46:58 +0300665 g_free(buf);
666 if (local_err != NULL) {
667 goto fail_options;
668 }
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200669 if (!bdrv_has_zero_init(bs->file->bs) ||
670 bdrv_truncate(bs->file->bs, bdrv_getlength(bs->file->bs)) != 0) {
Denis V. Lunevd6179012015-04-28 10:46:58 +0300671 s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
672 }
673
Denis V. Lunev6dd6b9f2015-04-28 10:46:53 +0300674 if (flags & BDRV_O_RDWR) {
675 s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
676 ret = parallels_update_header(bs);
677 if (ret < 0) {
678 goto fail;
679 }
680 }
681
Denis V. Lunev0d31c7c2015-04-28 10:46:57 +0300682 s->bat_dirty_block = 4 * getpagesize();
683 s->bat_dirty_bmap =
684 bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block));
685
Denis V. Lunev23d6bd32015-04-28 10:46:51 +0300686 qemu_co_mutex_init(&s->lock);
687 return 0;
688
689fail_format:
690 error_setg(errp, "Image not in Parallels format");
691 ret = -EINVAL;
692fail:
693 qemu_vfree(s->header);
694 return ret;
Denis V. Lunevd6179012015-04-28 10:46:58 +0300695
696fail_options:
697 error_propagate(errp, local_err);
698 ret = -EINVAL;
699 goto fail;
Denis V. Lunev23d6bd32015-04-28 10:46:51 +0300700}
701
702
ths6ada7452007-07-31 23:28:53 +0000703static void parallels_close(BlockDriverState *bs)
704{
705 BDRVParallelsState *s = bs->opaque;
Denis V. Lunev6dd6b9f2015-04-28 10:46:53 +0300706
707 if (bs->open_flags & BDRV_O_RDWR) {
708 s->header->inuse = 0;
709 parallels_update_header(bs);
710 }
711
Denis V. Lunev19f5dc12015-04-28 10:46:59 +0300712 if (bs->open_flags & BDRV_O_RDWR) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200713 bdrv_truncate(bs->file->bs, s->data_end << BDRV_SECTOR_BITS);
Denis V. Lunev19f5dc12015-04-28 10:46:59 +0300714 }
715
Denis V. Lunev0d31c7c2015-04-28 10:46:57 +0300716 g_free(s->bat_dirty_bmap);
Denis V. Lunev9eae9cc2015-04-28 10:46:50 +0300717 qemu_vfree(s->header);
ths6ada7452007-07-31 23:28:53 +0000718}
719
Denis V. Lunev74cf6c52015-04-28 10:46:44 +0300720static QemuOptsList parallels_create_opts = {
721 .name = "parallels-create-opts",
722 .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head),
723 .desc = {
724 {
725 .name = BLOCK_OPT_SIZE,
726 .type = QEMU_OPT_SIZE,
727 .help = "Virtual disk size",
728 },
729 {
730 .name = BLOCK_OPT_CLUSTER_SIZE,
731 .type = QEMU_OPT_SIZE,
732 .help = "Parallels image cluster size",
733 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE),
734 },
735 { /* end of list */ }
736 }
737};
738
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500739static BlockDriver bdrv_parallels = {
aurel32e60f4692009-03-07 22:00:29 +0000740 .format_name = "parallels",
741 .instance_size = sizeof(BDRVParallelsState),
742 .bdrv_probe = parallels_probe,
Christoph Hellwig1dec5a72010-05-06 22:04:50 +0200743 .bdrv_open = parallels_open,
aurel32e60f4692009-03-07 22:00:29 +0000744 .bdrv_close = parallels_close,
Roman Kagandd3bed12015-04-28 10:46:38 +0300745 .bdrv_co_get_block_status = parallels_co_get_block_status,
Denis V. Lunevd0e61ce2015-04-28 10:46:41 +0300746 .bdrv_has_zero_init = bdrv_has_zero_init_1,
Denis V. Lunev0d31c7c2015-04-28 10:46:57 +0300747 .bdrv_co_flush_to_os = parallels_co_flush_to_os,
Denis V. Lunev481fb9c2015-04-28 10:46:39 +0300748 .bdrv_co_readv = parallels_co_readv,
Denis V. Lunev5a41e1f2015-04-28 10:46:42 +0300749 .bdrv_co_writev = parallels_co_writev,
Denis V. Lunev74cf6c52015-04-28 10:46:44 +0300750
751 .bdrv_create = parallels_create,
Denis V. Lunev49ad6462015-04-28 10:46:52 +0300752 .bdrv_check = parallels_check,
Denis V. Lunev74cf6c52015-04-28 10:46:44 +0300753 .create_opts = &parallels_create_opts,
ths6ada7452007-07-31 23:28:53 +0000754};
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500755
756static void bdrv_parallels_init(void)
757{
758 bdrv_register(&bdrv_parallels);
759}
760
761block_init(bdrv_parallels_init);