xref: /openbmc/qemu/block/parallels.c (revision 788b305c91398f18e5952667b929d7f45e2c211c)
1 /*
2  * Block driver for Parallels disk image format
3  *
4  * Copyright (c) 2007 Alex Beregszaszi
5  * Copyright (c) 2015 Denis V. Lunev <den@openvz.org>
6  *
7  * 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
11  *
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  */
30 #include "qemu/osdep.h"
31 #include "qapi/error.h"
32 #include "qemu-common.h"
33 #include "block/block_int.h"
34 #include "sysemu/block-backend.h"
35 #include "qemu/module.h"
36 #include "qemu/bswap.h"
37 #include "qemu/bitmap.h"
38 
39 /**************************************************************/
40 
41 #define HEADER_MAGIC "WithoutFreeSpace"
42 #define HEADER_MAGIC2 "WithouFreSpacExt"
43 #define HEADER_VERSION 2
44 #define HEADER_INUSE_MAGIC  (0x746F6E59)
45 #define MAX_PARALLELS_IMAGE_FACTOR (1ull << 32)
46 
47 #define DEFAULT_CLUSTER_SIZE 1048576        /* 1 MiB */
48 
49 
50 // always little-endian
51 typedef struct ParallelsHeader {
52     char magic[16]; // "WithoutFreeSpace"
53     uint32_t version;
54     uint32_t heads;
55     uint32_t cylinders;
56     uint32_t tracks;
57     uint32_t bat_entries;
58     uint64_t nb_sectors;
59     uint32_t inuse;
60     uint32_t data_off;
61     char padding[12];
62 } QEMU_PACKED ParallelsHeader;
63 
64 
65 typedef enum ParallelsPreallocMode {
66     PRL_PREALLOC_MODE_FALLOCATE = 0,
67     PRL_PREALLOC_MODE_TRUNCATE = 1,
68     PRL_PREALLOC_MODE__MAX = 2,
69 } ParallelsPreallocMode;
70 
71 static const char *prealloc_mode_lookup[] = {
72     "falloc",
73     "truncate",
74     NULL,
75 };
76 
77 
78 typedef struct BDRVParallelsState {
79     /** Locking is conservative, the lock protects
80      *   - image file extending (truncate, fallocate)
81      *   - any access to block allocation table
82      */
83     CoMutex lock;
84 
85     ParallelsHeader *header;
86     uint32_t header_size;
87     bool header_unclean;
88 
89     unsigned long *bat_dirty_bmap;
90     unsigned int  bat_dirty_block;
91 
92     uint32_t *bat_bitmap;
93     unsigned int bat_size;
94 
95     int64_t  data_end;
96     uint64_t prealloc_size;
97     ParallelsPreallocMode prealloc_mode;
98 
99     unsigned int tracks;
100 
101     unsigned int off_multiplier;
102 } BDRVParallelsState;
103 
104 
105 #define PARALLELS_OPT_PREALLOC_MODE     "prealloc-mode"
106 #define PARALLELS_OPT_PREALLOC_SIZE     "prealloc-size"
107 
108 static QemuOptsList parallels_runtime_opts = {
109     .name = "parallels",
110     .head = QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts.head),
111     .desc = {
112         {
113             .name = PARALLELS_OPT_PREALLOC_SIZE,
114             .type = QEMU_OPT_SIZE,
115             .help = "Preallocation size on image expansion",
116             .def_value_str = "128M",
117         },
118         {
119             .name = PARALLELS_OPT_PREALLOC_MODE,
120             .type = QEMU_OPT_STRING,
121             .help = "Preallocation mode on image expansion "
122                     "(allowed values: falloc, truncate)",
123             .def_value_str = "falloc",
124         },
125         { /* end of list */ },
126     },
127 };
128 
129 
130 static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx)
131 {
132     return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier;
133 }
134 
135 static uint32_t bat_entry_off(uint32_t idx)
136 {
137     return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx;
138 }
139 
140 static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
141 {
142     uint32_t index, offset;
143 
144     index = sector_num / s->tracks;
145     offset = sector_num % s->tracks;
146 
147     /* not allocated */
148     if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) {
149         return -1;
150     }
151     return bat2sect(s, index) + offset;
152 }
153 
154 static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num,
155         int nb_sectors)
156 {
157     int ret = s->tracks - sector_num % s->tracks;
158     return MIN(nb_sectors, ret);
159 }
160 
161 static int64_t block_status(BDRVParallelsState *s, int64_t sector_num,
162                             int nb_sectors, int *pnum)
163 {
164     int64_t start_off = -2, prev_end_off = -2;
165 
166     *pnum = 0;
167     while (nb_sectors > 0 || start_off == -2) {
168         int64_t offset = seek_to_sector(s, sector_num);
169         int to_end;
170 
171         if (start_off == -2) {
172             start_off = offset;
173             prev_end_off = offset;
174         } else if (offset != prev_end_off) {
175             break;
176         }
177 
178         to_end = cluster_remainder(s, sector_num, nb_sectors);
179         nb_sectors -= to_end;
180         sector_num += to_end;
181         *pnum += to_end;
182 
183         if (offset > 0) {
184             prev_end_off += to_end;
185         }
186     }
187     return start_off;
188 }
189 
190 static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
191                                  int nb_sectors, int *pnum)
192 {
193     BDRVParallelsState *s = bs->opaque;
194     int64_t pos, space, idx, to_allocate, i, len;
195 
196     pos = block_status(s, sector_num, nb_sectors, pnum);
197     if (pos > 0) {
198         return pos;
199     }
200 
201     idx = sector_num / s->tracks;
202     to_allocate = DIV_ROUND_UP(sector_num + *pnum, s->tracks) - idx;
203 
204     /* This function is called only by parallels_co_writev(), which will never
205      * pass a sector_num at or beyond the end of the image (because the block
206      * layer never passes such a sector_num to that function). Therefore, idx
207      * is always below s->bat_size.
208      * block_status() will limit *pnum so that sector_num + *pnum will not
209      * exceed the image end. Therefore, idx + to_allocate cannot exceed
210      * s->bat_size.
211      * Note that s->bat_size is an unsigned int, therefore idx + to_allocate
212      * will always fit into a uint32_t. */
213     assert(idx < s->bat_size && idx + to_allocate <= s->bat_size);
214 
215     space = to_allocate * s->tracks;
216     len = bdrv_getlength(bs->file->bs);
217     if (len < 0) {
218         return len;
219     }
220     if (s->data_end + space > (len >> BDRV_SECTOR_BITS)) {
221         int ret;
222         space += s->prealloc_size;
223         if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
224             ret = bdrv_pwrite_zeroes(bs->file,
225                                      s->data_end << BDRV_SECTOR_BITS,
226                                      space << BDRV_SECTOR_BITS, 0);
227         } else {
228             ret = bdrv_truncate(bs->file,
229                                 (s->data_end + space) << BDRV_SECTOR_BITS,
230                                 PREALLOC_MODE_OFF, NULL);
231         }
232         if (ret < 0) {
233             return ret;
234         }
235     }
236 
237     for (i = 0; i < to_allocate; i++) {
238         s->bat_bitmap[idx + i] = cpu_to_le32(s->data_end / s->off_multiplier);
239         s->data_end += s->tracks;
240         bitmap_set(s->bat_dirty_bmap,
241                    bat_entry_off(idx + i) / s->bat_dirty_block, 1);
242     }
243 
244     return bat2sect(s, idx) + sector_num % s->tracks;
245 }
246 
247 
248 static coroutine_fn int parallels_co_flush_to_os(BlockDriverState *bs)
249 {
250     BDRVParallelsState *s = bs->opaque;
251     unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block);
252     unsigned long bit;
253 
254     qemu_co_mutex_lock(&s->lock);
255 
256     bit = find_first_bit(s->bat_dirty_bmap, size);
257     while (bit < size) {
258         uint32_t off = bit * s->bat_dirty_block;
259         uint32_t to_write = s->bat_dirty_block;
260         int ret;
261 
262         if (off + to_write > s->header_size) {
263             to_write = s->header_size - off;
264         }
265         ret = bdrv_pwrite(bs->file, off, (uint8_t *)s->header + off,
266                           to_write);
267         if (ret < 0) {
268             qemu_co_mutex_unlock(&s->lock);
269             return ret;
270         }
271         bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1);
272     }
273     bitmap_zero(s->bat_dirty_bmap, size);
274 
275     qemu_co_mutex_unlock(&s->lock);
276     return 0;
277 }
278 
279 
280 static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs,
281         int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file)
282 {
283     BDRVParallelsState *s = bs->opaque;
284     int64_t offset;
285 
286     qemu_co_mutex_lock(&s->lock);
287     offset = block_status(s, sector_num, nb_sectors, pnum);
288     qemu_co_mutex_unlock(&s->lock);
289 
290     if (offset < 0) {
291         return 0;
292     }
293 
294     *file = bs->file->bs;
295     return (offset << BDRV_SECTOR_BITS) |
296         BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
297 }
298 
299 static coroutine_fn int parallels_co_writev(BlockDriverState *bs,
300         int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
301 {
302     BDRVParallelsState *s = bs->opaque;
303     uint64_t bytes_done = 0;
304     QEMUIOVector hd_qiov;
305     int ret = 0;
306 
307     qemu_iovec_init(&hd_qiov, qiov->niov);
308 
309     while (nb_sectors > 0) {
310         int64_t position;
311         int n, nbytes;
312 
313         qemu_co_mutex_lock(&s->lock);
314         position = allocate_clusters(bs, sector_num, nb_sectors, &n);
315         qemu_co_mutex_unlock(&s->lock);
316         if (position < 0) {
317             ret = (int)position;
318             break;
319         }
320 
321         nbytes = n << BDRV_SECTOR_BITS;
322 
323         qemu_iovec_reset(&hd_qiov);
324         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
325 
326         ret = bdrv_co_writev(bs->file, position, n, &hd_qiov);
327         if (ret < 0) {
328             break;
329         }
330 
331         nb_sectors -= n;
332         sector_num += n;
333         bytes_done += nbytes;
334     }
335 
336     qemu_iovec_destroy(&hd_qiov);
337     return ret;
338 }
339 
340 static coroutine_fn int parallels_co_readv(BlockDriverState *bs,
341         int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
342 {
343     BDRVParallelsState *s = bs->opaque;
344     uint64_t bytes_done = 0;
345     QEMUIOVector hd_qiov;
346     int ret = 0;
347 
348     qemu_iovec_init(&hd_qiov, qiov->niov);
349 
350     while (nb_sectors > 0) {
351         int64_t position;
352         int n, nbytes;
353 
354         qemu_co_mutex_lock(&s->lock);
355         position = block_status(s, sector_num, nb_sectors, &n);
356         qemu_co_mutex_unlock(&s->lock);
357 
358         nbytes = n << BDRV_SECTOR_BITS;
359 
360         if (position < 0) {
361             qemu_iovec_memset(qiov, bytes_done, 0, nbytes);
362         } else {
363             qemu_iovec_reset(&hd_qiov);
364             qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
365 
366             ret = bdrv_co_readv(bs->file, position, n, &hd_qiov);
367             if (ret < 0) {
368                 break;
369             }
370         }
371 
372         nb_sectors -= n;
373         sector_num += n;
374         bytes_done += nbytes;
375     }
376 
377     qemu_iovec_destroy(&hd_qiov);
378     return ret;
379 }
380 
381 
382 static int parallels_check(BlockDriverState *bs, BdrvCheckResult *res,
383                            BdrvCheckMode fix)
384 {
385     BDRVParallelsState *s = bs->opaque;
386     int64_t size, prev_off, high_off;
387     int ret;
388     uint32_t i;
389     bool flush_bat = false;
390     int cluster_size = s->tracks << BDRV_SECTOR_BITS;
391 
392     size = bdrv_getlength(bs->file->bs);
393     if (size < 0) {
394         res->check_errors++;
395         return size;
396     }
397 
398     if (s->header_unclean) {
399         fprintf(stderr, "%s image was not closed correctly\n",
400                 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
401         res->corruptions++;
402         if (fix & BDRV_FIX_ERRORS) {
403             /* parallels_close will do the job right */
404             res->corruptions_fixed++;
405             s->header_unclean = false;
406         }
407     }
408 
409     res->bfi.total_clusters = s->bat_size;
410     res->bfi.compressed_clusters = 0; /* compression is not supported */
411 
412     high_off = 0;
413     prev_off = 0;
414     for (i = 0; i < s->bat_size; i++) {
415         int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
416         if (off == 0) {
417             prev_off = 0;
418             continue;
419         }
420 
421         /* cluster outside the image */
422         if (off > size) {
423             fprintf(stderr, "%s cluster %u is outside image\n",
424                     fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
425             res->corruptions++;
426             if (fix & BDRV_FIX_ERRORS) {
427                 prev_off = 0;
428                 s->bat_bitmap[i] = 0;
429                 res->corruptions_fixed++;
430                 flush_bat = true;
431                 continue;
432             }
433         }
434 
435         res->bfi.allocated_clusters++;
436         if (off > high_off) {
437             high_off = off;
438         }
439 
440         if (prev_off != 0 && (prev_off + cluster_size) != off) {
441             res->bfi.fragmented_clusters++;
442         }
443         prev_off = off;
444     }
445 
446     if (flush_bat) {
447         ret = bdrv_pwrite_sync(bs->file, 0, s->header, s->header_size);
448         if (ret < 0) {
449             res->check_errors++;
450             return ret;
451         }
452     }
453 
454     res->image_end_offset = high_off + cluster_size;
455     if (size > res->image_end_offset) {
456         int64_t count;
457         count = DIV_ROUND_UP(size - res->image_end_offset, cluster_size);
458         fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n",
459                 fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
460                 size - res->image_end_offset);
461         res->leaks += count;
462         if (fix & BDRV_FIX_LEAKS) {
463             Error *local_err = NULL;
464             ret = bdrv_truncate(bs->file, res->image_end_offset,
465                                 PREALLOC_MODE_OFF, &local_err);
466             if (ret < 0) {
467                 error_report_err(local_err);
468                 res->check_errors++;
469                 return ret;
470             }
471             res->leaks_fixed += count;
472         }
473     }
474 
475     return 0;
476 }
477 
478 
479 static int parallels_create(const char *filename, QemuOpts *opts, Error **errp)
480 {
481     int64_t total_size, cl_size;
482     uint8_t tmp[BDRV_SECTOR_SIZE];
483     Error *local_err = NULL;
484     BlockBackend *file;
485     uint32_t bat_entries, bat_sectors;
486     ParallelsHeader header;
487     int ret;
488 
489     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
490                           BDRV_SECTOR_SIZE);
491     cl_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
492                           DEFAULT_CLUSTER_SIZE), BDRV_SECTOR_SIZE);
493     if (total_size >= MAX_PARALLELS_IMAGE_FACTOR * cl_size) {
494         error_propagate(errp, local_err);
495         return -E2BIG;
496     }
497 
498     ret = bdrv_create_file(filename, opts, &local_err);
499     if (ret < 0) {
500         error_propagate(errp, local_err);
501         return ret;
502     }
503 
504     file = blk_new_open(filename, NULL, NULL,
505                         BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
506                         &local_err);
507     if (file == NULL) {
508         error_propagate(errp, local_err);
509         return -EIO;
510     }
511 
512     blk_set_allow_write_beyond_eof(file, true);
513 
514     ret = blk_truncate(file, 0, PREALLOC_MODE_OFF, errp);
515     if (ret < 0) {
516         goto exit;
517     }
518 
519     bat_entries = DIV_ROUND_UP(total_size, cl_size);
520     bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size);
521     bat_sectors = (bat_sectors *  cl_size) >> BDRV_SECTOR_BITS;
522 
523     memset(&header, 0, sizeof(header));
524     memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic));
525     header.version = cpu_to_le32(HEADER_VERSION);
526     /* don't care much about geometry, it is not used on image level */
527     header.heads = cpu_to_le32(16);
528     header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE / 16 / 32);
529     header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS);
530     header.bat_entries = cpu_to_le32(bat_entries);
531     header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE));
532     header.data_off = cpu_to_le32(bat_sectors);
533 
534     /* write all the data */
535     memset(tmp, 0, sizeof(tmp));
536     memcpy(tmp, &header, sizeof(header));
537 
538     ret = blk_pwrite(file, 0, tmp, BDRV_SECTOR_SIZE, 0);
539     if (ret < 0) {
540         goto exit;
541     }
542     ret = blk_pwrite_zeroes(file, BDRV_SECTOR_SIZE,
543                             (bat_sectors - 1) << BDRV_SECTOR_BITS, 0);
544     if (ret < 0) {
545         goto exit;
546     }
547     ret = 0;
548 
549 done:
550     blk_unref(file);
551     return ret;
552 
553 exit:
554     error_setg_errno(errp, -ret, "Failed to create Parallels image");
555     goto done;
556 }
557 
558 
559 static int parallels_probe(const uint8_t *buf, int buf_size,
560                            const char *filename)
561 {
562     const ParallelsHeader *ph = (const void *)buf;
563 
564     if (buf_size < sizeof(ParallelsHeader)) {
565         return 0;
566     }
567 
568     if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
569            !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
570            (le32_to_cpu(ph->version) == HEADER_VERSION)) {
571         return 100;
572     }
573 
574     return 0;
575 }
576 
577 static int parallels_update_header(BlockDriverState *bs)
578 {
579     BDRVParallelsState *s = bs->opaque;
580     unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs),
581                         sizeof(ParallelsHeader));
582 
583     if (size > s->header_size) {
584         size = s->header_size;
585     }
586     return bdrv_pwrite_sync(bs->file, 0, s->header, size);
587 }
588 
589 static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
590                           Error **errp)
591 {
592     BDRVParallelsState *s = bs->opaque;
593     ParallelsHeader ph;
594     int ret, size, i;
595     QemuOpts *opts = NULL;
596     Error *local_err = NULL;
597     char *buf;
598 
599     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
600                                false, errp);
601     if (!bs->file) {
602         return -EINVAL;
603     }
604 
605     ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
606     if (ret < 0) {
607         goto fail;
608     }
609 
610     bs->total_sectors = le64_to_cpu(ph.nb_sectors);
611 
612     if (le32_to_cpu(ph.version) != HEADER_VERSION) {
613         goto fail_format;
614     }
615     if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
616         s->off_multiplier = 1;
617         bs->total_sectors = 0xffffffff & bs->total_sectors;
618     } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
619         s->off_multiplier = le32_to_cpu(ph.tracks);
620     } else {
621         goto fail_format;
622     }
623 
624     s->tracks = le32_to_cpu(ph.tracks);
625     if (s->tracks == 0) {
626         error_setg(errp, "Invalid image: Zero sectors per track");
627         ret = -EINVAL;
628         goto fail;
629     }
630     if (s->tracks > INT32_MAX/513) {
631         error_setg(errp, "Invalid image: Too big cluster");
632         ret = -EFBIG;
633         goto fail;
634     }
635 
636     s->bat_size = le32_to_cpu(ph.bat_entries);
637     if (s->bat_size > INT_MAX / sizeof(uint32_t)) {
638         error_setg(errp, "Catalog too large");
639         ret = -EFBIG;
640         goto fail;
641     }
642 
643     size = bat_entry_off(s->bat_size);
644     s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file->bs));
645     s->header = qemu_try_blockalign(bs->file->bs, s->header_size);
646     if (s->header == NULL) {
647         ret = -ENOMEM;
648         goto fail;
649     }
650     s->data_end = le32_to_cpu(ph.data_off);
651     if (s->data_end == 0) {
652         s->data_end = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE);
653     }
654     if (s->data_end < s->header_size) {
655         /* there is not enough unused space to fit to block align between BAT
656            and actual data. We can't avoid read-modify-write... */
657         s->header_size = size;
658     }
659 
660     ret = bdrv_pread(bs->file, 0, s->header, s->header_size);
661     if (ret < 0) {
662         goto fail;
663     }
664     s->bat_bitmap = (uint32_t *)(s->header + 1);
665 
666     for (i = 0; i < s->bat_size; i++) {
667         int64_t off = bat2sect(s, i);
668         if (off >= s->data_end) {
669             s->data_end = off + s->tracks;
670         }
671     }
672 
673     if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
674         /* Image was not closed correctly. The check is mandatory */
675         s->header_unclean = true;
676         if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
677             error_setg(errp, "parallels: Image was not closed correctly; "
678                        "cannot be opened read/write");
679             ret = -EACCES;
680             goto fail;
681         }
682     }
683 
684     opts = qemu_opts_create(&parallels_runtime_opts, NULL, 0, &local_err);
685     if (local_err != NULL) {
686         goto fail_options;
687     }
688 
689     qemu_opts_absorb_qdict(opts, options, &local_err);
690     if (local_err != NULL) {
691         goto fail_options;
692     }
693 
694     s->prealloc_size =
695         qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
696     s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS);
697     buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
698     s->prealloc_mode = qapi_enum_parse(prealloc_mode_lookup, buf,
699                                        PRL_PREALLOC_MODE_FALLOCATE,
700                                        &local_err);
701     g_free(buf);
702     if (local_err != NULL) {
703         goto fail_options;
704     }
705 
706     if (!bdrv_has_zero_init(bs->file->bs)) {
707         s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
708     }
709 
710     if (flags & BDRV_O_RDWR) {
711         s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
712         ret = parallels_update_header(bs);
713         if (ret < 0) {
714             goto fail;
715         }
716     }
717 
718     s->bat_dirty_block = 4 * getpagesize();
719     s->bat_dirty_bmap =
720         bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block));
721 
722     qemu_co_mutex_init(&s->lock);
723     return 0;
724 
725 fail_format:
726     error_setg(errp, "Image not in Parallels format");
727     ret = -EINVAL;
728 fail:
729     qemu_vfree(s->header);
730     return ret;
731 
732 fail_options:
733     error_propagate(errp, local_err);
734     ret = -EINVAL;
735     goto fail;
736 }
737 
738 
739 static void parallels_close(BlockDriverState *bs)
740 {
741     BDRVParallelsState *s = bs->opaque;
742 
743     if (bs->open_flags & BDRV_O_RDWR) {
744         s->header->inuse = 0;
745         parallels_update_header(bs);
746     }
747 
748     if (bs->open_flags & BDRV_O_RDWR) {
749         bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS,
750                       PREALLOC_MODE_OFF, NULL);
751     }
752 
753     g_free(s->bat_dirty_bmap);
754     qemu_vfree(s->header);
755 }
756 
757 static QemuOptsList parallels_create_opts = {
758     .name = "parallels-create-opts",
759     .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head),
760     .desc = {
761         {
762             .name = BLOCK_OPT_SIZE,
763             .type = QEMU_OPT_SIZE,
764             .help = "Virtual disk size",
765         },
766         {
767             .name = BLOCK_OPT_CLUSTER_SIZE,
768             .type = QEMU_OPT_SIZE,
769             .help = "Parallels image cluster size",
770             .def_value_str = stringify(DEFAULT_CLUSTER_SIZE),
771         },
772         { /* end of list */ }
773     }
774 };
775 
776 static BlockDriver bdrv_parallels = {
777     .format_name	= "parallels",
778     .instance_size	= sizeof(BDRVParallelsState),
779     .bdrv_probe		= parallels_probe,
780     .bdrv_open		= parallels_open,
781     .bdrv_close		= parallels_close,
782     .bdrv_child_perm          = bdrv_format_default_perms,
783     .bdrv_co_get_block_status = parallels_co_get_block_status,
784     .bdrv_has_zero_init       = bdrv_has_zero_init_1,
785     .bdrv_co_flush_to_os      = parallels_co_flush_to_os,
786     .bdrv_co_readv  = parallels_co_readv,
787     .bdrv_co_writev = parallels_co_writev,
788 
789     .bdrv_create    = parallels_create,
790     .bdrv_check     = parallels_check,
791     .create_opts    = &parallels_create_opts,
792 };
793 
794 static void bdrv_parallels_init(void)
795 {
796     bdrv_register(&bdrv_parallels);
797 }
798 
799 block_init(bdrv_parallels_init);
800