2 * Block driver for the COW format
4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/module.h"
28 /**************************************************************/
29 /* COW block driver using file system holes */
31 /* user mode linux compatible COW file */
32 #define COW_MAGIC 0x4f4f4f4d /* MOOO */
35 struct cow_header_v2
{
38 char backing_file
[1024];
44 typedef struct BDRVCowState
{
46 int64_t cow_sectors_offset
;
49 static int cow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
51 const struct cow_header_v2
*cow_header
= (const void *)buf
;
53 if (buf_size
>= sizeof(struct cow_header_v2
) &&
54 be32_to_cpu(cow_header
->magic
) == COW_MAGIC
&&
55 be32_to_cpu(cow_header
->version
) == COW_VERSION
)
61 static int cow_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
63 BDRVCowState
*s
= bs
->opaque
;
64 struct cow_header_v2 cow_header
;
69 /* see if it is a cow image */
70 ret
= bdrv_pread(bs
->file
, 0, &cow_header
, sizeof(cow_header
));
75 if (be32_to_cpu(cow_header
.magic
) != COW_MAGIC
) {
80 if (be32_to_cpu(cow_header
.version
) != COW_VERSION
) {
82 snprintf(version
, sizeof(version
),
83 "COW version %d", cow_header
.version
);
84 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
85 bs
->device_name
, "cow", version
);
91 size
= be64_to_cpu(cow_header
.size
);
92 bs
->total_sectors
= size
/ 512;
94 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
95 cow_header
.backing_file
);
97 bitmap_size
= ((bs
->total_sectors
+ 7) >> 3) + sizeof(cow_header
);
98 s
->cow_sectors_offset
= (bitmap_size
+ 511) & ~511;
99 qemu_co_mutex_init(&s
->lock
);
106 * XXX(hch): right now these functions are extremely inefficient.
107 * We should just read the whole bitmap we'll need in one go instead.
109 static inline int cow_set_bit(BlockDriverState
*bs
, int64_t bitnum
, bool *first
)
111 uint64_t offset
= sizeof(struct cow_header_v2
) + bitnum
/ 8;
115 ret
= bdrv_pread(bs
->file
, offset
, &bitmap
, sizeof(bitmap
));
120 if (bitmap
& (1 << (bitnum
% 8))) {
125 ret
= bdrv_flush(bs
->file
);
132 bitmap
|= (1 << (bitnum
% 8));
134 ret
= bdrv_pwrite(bs
->file
, offset
, &bitmap
, sizeof(bitmap
));
141 #define BITS_PER_BITMAP_SECTOR (512 * 8)
143 /* Cannot use bitmap.c on big-endian machines. */
144 static int cow_test_bit(int64_t bitnum
, const uint8_t *bitmap
)
146 return (bitmap
[bitnum
/ 8] & (1 << (bitnum
& 7))) != 0;
149 static int cow_find_streak(const uint8_t *bitmap
, int value
, int start
, int nb_sectors
)
151 int streak_value
= value
? 0xFF : 0;
152 int last
= MIN(start
+ nb_sectors
, BITS_PER_BITMAP_SECTOR
);
154 while (bitnum
< last
) {
155 if ((bitnum
& 7) == 0 && bitmap
[bitnum
/ 8] == streak_value
) {
159 if (cow_test_bit(bitnum
, bitmap
) == value
) {
165 return MIN(bitnum
, last
) - start
;
168 /* Return true if first block has been changed (ie. current version is
169 * in COW file). Set the number of continuous blocks for which that
171 static int coroutine_fn
cow_co_is_allocated(BlockDriverState
*bs
,
172 int64_t sector_num
, int nb_sectors
, int *num_same
)
174 int64_t bitnum
= sector_num
+ sizeof(struct cow_header_v2
) * 8;
175 uint64_t offset
= (bitnum
/ 8) & -BDRV_SECTOR_SIZE
;
176 uint8_t bitmap
[BDRV_SECTOR_SIZE
];
180 ret
= bdrv_pread(bs
->file
, offset
, &bitmap
, sizeof(bitmap
));
185 bitnum
&= BITS_PER_BITMAP_SECTOR
- 1;
186 changed
= cow_test_bit(bitnum
, bitmap
);
187 *num_same
= cow_find_streak(bitmap
, changed
, bitnum
, nb_sectors
);
191 static int64_t coroutine_fn
cow_co_get_block_status(BlockDriverState
*bs
,
192 int64_t sector_num
, int nb_sectors
, int *num_same
)
194 BDRVCowState
*s
= bs
->opaque
;
195 int ret
= cow_co_is_allocated(bs
, sector_num
, nb_sectors
, num_same
);
196 int64_t offset
= s
->cow_sectors_offset
+ (sector_num
<< BDRV_SECTOR_BITS
);
200 return (ret
? BDRV_BLOCK_DATA
: 0) | offset
| BDRV_BLOCK_OFFSET_VALID
;
203 static int cow_update_bitmap(BlockDriverState
*bs
, int64_t sector_num
,
210 for (i
= 0; i
< nb_sectors
; i
++) {
211 error
= cow_set_bit(bs
, sector_num
+ i
, &first
);
220 static int coroutine_fn
cow_read(BlockDriverState
*bs
, int64_t sector_num
,
221 uint8_t *buf
, int nb_sectors
)
223 BDRVCowState
*s
= bs
->opaque
;
226 while (nb_sectors
> 0) {
227 ret
= cow_co_is_allocated(bs
, sector_num
, nb_sectors
, &n
);
232 ret
= bdrv_pread(bs
->file
,
233 s
->cow_sectors_offset
+ sector_num
* 512,
239 if (bs
->backing_hd
) {
240 /* read from the base image */
241 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
246 memset(buf
, 0, n
* 512);
256 static coroutine_fn
int cow_co_read(BlockDriverState
*bs
, int64_t sector_num
,
257 uint8_t *buf
, int nb_sectors
)
260 BDRVCowState
*s
= bs
->opaque
;
261 qemu_co_mutex_lock(&s
->lock
);
262 ret
= cow_read(bs
, sector_num
, buf
, nb_sectors
);
263 qemu_co_mutex_unlock(&s
->lock
);
267 static int cow_write(BlockDriverState
*bs
, int64_t sector_num
,
268 const uint8_t *buf
, int nb_sectors
)
270 BDRVCowState
*s
= bs
->opaque
;
273 ret
= bdrv_pwrite(bs
->file
, s
->cow_sectors_offset
+ sector_num
* 512,
274 buf
, nb_sectors
* 512);
279 return cow_update_bitmap(bs
, sector_num
, nb_sectors
);
282 static coroutine_fn
int cow_co_write(BlockDriverState
*bs
, int64_t sector_num
,
283 const uint8_t *buf
, int nb_sectors
)
286 BDRVCowState
*s
= bs
->opaque
;
287 qemu_co_mutex_lock(&s
->lock
);
288 ret
= cow_write(bs
, sector_num
, buf
, nb_sectors
);
289 qemu_co_mutex_unlock(&s
->lock
);
293 static void cow_close(BlockDriverState
*bs
)
297 static int cow_create(const char *filename
, QEMUOptionParameter
*options
)
299 struct cow_header_v2 cow_header
;
301 int64_t image_sectors
= 0;
302 const char *image_filename
= NULL
;
304 BlockDriverState
*cow_bs
;
306 /* Read out options */
307 while (options
&& options
->name
) {
308 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
309 image_sectors
= options
->value
.n
/ 512;
310 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
311 image_filename
= options
->value
.s
;
316 ret
= bdrv_create_file(filename
, options
);
321 ret
= bdrv_file_open(&cow_bs
, filename
, NULL
, BDRV_O_RDWR
);
326 memset(&cow_header
, 0, sizeof(cow_header
));
327 cow_header
.magic
= cpu_to_be32(COW_MAGIC
);
328 cow_header
.version
= cpu_to_be32(COW_VERSION
);
329 if (image_filename
) {
330 /* Note: if no file, we put a dummy mtime */
331 cow_header
.mtime
= cpu_to_be32(0);
333 if (stat(image_filename
, &st
) != 0) {
336 cow_header
.mtime
= cpu_to_be32(st
.st_mtime
);
338 pstrcpy(cow_header
.backing_file
, sizeof(cow_header
.backing_file
),
341 cow_header
.sectorsize
= cpu_to_be32(512);
342 cow_header
.size
= cpu_to_be64(image_sectors
* 512);
343 ret
= bdrv_pwrite(cow_bs
, 0, &cow_header
, sizeof(cow_header
));
348 /* resize to include at least all the bitmap */
349 ret
= bdrv_truncate(cow_bs
,
350 sizeof(cow_header
) + ((image_sectors
+ 7) >> 3));
360 static QEMUOptionParameter cow_create_options
[] = {
362 .name
= BLOCK_OPT_SIZE
,
364 .help
= "Virtual disk size"
367 .name
= BLOCK_OPT_BACKING_FILE
,
369 .help
= "File name of a base image"
374 static BlockDriver bdrv_cow
= {
375 .format_name
= "cow",
376 .instance_size
= sizeof(BDRVCowState
),
378 .bdrv_probe
= cow_probe
,
379 .bdrv_open
= cow_open
,
380 .bdrv_close
= cow_close
,
381 .bdrv_create
= cow_create
,
382 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
384 .bdrv_read
= cow_co_read
,
385 .bdrv_write
= cow_co_write
,
386 .bdrv_co_get_block_status
= cow_co_get_block_status
,
388 .create_options
= cow_create_options
,
391 static void bdrv_cow_init(void)
393 bdrv_register(&bdrv_cow
);
396 block_init(bdrv_cow_init
);