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
25 #include "qemu-common.h"
26 #include "block_int.h"
30 /**************************************************************/
31 /* COW block driver using file system holes */
33 /* user mode linux compatible COW file */
34 #define COW_MAGIC 0x4f4f4f4d /* MOOO */
37 struct cow_header_v2
{
40 char backing_file
[1024];
46 typedef struct BDRVCowState
{
48 uint8_t *cow_bitmap
; /* if non NULL, COW mappings are used first */
49 uint8_t *cow_bitmap_addr
; /* mmap address of cow_bitmap */
51 int64_t cow_sectors_offset
;
54 static int cow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
56 const struct cow_header_v2
*cow_header
= (const void *)buf
;
58 if (buf_size
>= sizeof(struct cow_header_v2
) &&
59 be32_to_cpu(cow_header
->magic
) == COW_MAGIC
&&
60 be32_to_cpu(cow_header
->version
) == COW_VERSION
)
66 static int cow_open(BlockDriverState
*bs
, const char *filename
, int flags
)
68 BDRVCowState
*s
= bs
->opaque
;
70 struct cow_header_v2 cow_header
;
73 fd
= open(filename
, O_RDWR
| O_BINARY
| O_LARGEFILE
);
75 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
80 /* see if it is a cow image */
81 if (read(fd
, &cow_header
, sizeof(cow_header
)) != sizeof(cow_header
)) {
85 if (be32_to_cpu(cow_header
.magic
) != COW_MAGIC
||
86 be32_to_cpu(cow_header
.version
) != 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
);
98 s
->cow_bitmap_size
= ((bs
->total_sectors
+ 7) >> 3) + sizeof(cow_header
);
99 s
->cow_bitmap_addr
= (void *)mmap(get_mmap_addr(s
->cow_bitmap_size
),
101 PROT_READ
| PROT_WRITE
,
102 MAP_SHARED
, s
->fd
, 0);
103 if (s
->cow_bitmap_addr
== MAP_FAILED
)
105 s
->cow_bitmap
= s
->cow_bitmap_addr
+ sizeof(cow_header
);
106 s
->cow_sectors_offset
= (s
->cow_bitmap_size
+ 511) & ~511;
113 static inline void cow_set_bit(uint8_t *bitmap
, int64_t bitnum
)
115 bitmap
[bitnum
/ 8] |= (1 << (bitnum
%8));
118 static inline int is_bit_set(const uint8_t *bitmap
, int64_t bitnum
)
120 return !!(bitmap
[bitnum
/ 8] & (1 << (bitnum
%8)));
124 /* Return true if first block has been changed (ie. current version is
125 * in COW file). Set the number of continuous blocks for which that
127 static inline int is_changed(uint8_t *bitmap
,
128 int64_t sector_num
, int nb_sectors
,
133 if (!bitmap
|| nb_sectors
== 0) {
134 *num_same
= nb_sectors
;
138 changed
= is_bit_set(bitmap
, sector_num
);
139 for (*num_same
= 1; *num_same
< nb_sectors
; (*num_same
)++) {
140 if (is_bit_set(bitmap
, sector_num
+ *num_same
) != changed
)
147 static int cow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
148 int nb_sectors
, int *pnum
)
150 BDRVCowState
*s
= bs
->opaque
;
151 return is_changed(s
->cow_bitmap
, sector_num
, nb_sectors
, pnum
);
154 static int cow_read(BlockDriverState
*bs
, int64_t sector_num
,
155 uint8_t *buf
, int nb_sectors
)
157 BDRVCowState
*s
= bs
->opaque
;
160 while (nb_sectors
> 0) {
161 if (is_changed(s
->cow_bitmap
, sector_num
, nb_sectors
, &n
)) {
162 lseek(s
->fd
, s
->cow_sectors_offset
+ sector_num
* 512, SEEK_SET
);
163 ret
= read(s
->fd
, buf
, n
* 512);
167 if (bs
->backing_hd
) {
168 /* read from the base image */
169 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
173 memset(buf
, 0, n
* 512);
183 static int cow_write(BlockDriverState
*bs
, int64_t sector_num
,
184 const uint8_t *buf
, int nb_sectors
)
186 BDRVCowState
*s
= bs
->opaque
;
189 lseek(s
->fd
, s
->cow_sectors_offset
+ sector_num
* 512, SEEK_SET
);
190 ret
= write(s
->fd
, buf
, nb_sectors
* 512);
191 if (ret
!= nb_sectors
* 512)
193 for (i
= 0; i
< nb_sectors
; i
++)
194 cow_set_bit(s
->cow_bitmap
, sector_num
+ i
);
198 static void cow_close(BlockDriverState
*bs
)
200 BDRVCowState
*s
= bs
->opaque
;
201 munmap((void *)s
->cow_bitmap_addr
, s
->cow_bitmap_size
);
205 static int cow_create(const char *filename
, QEMUOptionParameter
*options
)
208 struct cow_header_v2 cow_header
;
210 int64_t image_sectors
= 0;
211 const char *image_filename
= NULL
;
214 /* Read out options */
215 while (options
&& options
->name
) {
216 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
217 image_sectors
= options
->value
.n
/ 512;
218 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
219 image_filename
= options
->value
.s
;
224 cow_fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
228 memset(&cow_header
, 0, sizeof(cow_header
));
229 cow_header
.magic
= cpu_to_be32(COW_MAGIC
);
230 cow_header
.version
= cpu_to_be32(COW_VERSION
);
231 if (image_filename
) {
232 /* Note: if no file, we put a dummy mtime */
233 cow_header
.mtime
= cpu_to_be32(0);
235 fd
= open(image_filename
, O_RDONLY
| O_BINARY
);
240 if (fstat(fd
, &st
) != 0) {
245 cow_header
.mtime
= cpu_to_be32(st
.st_mtime
);
247 pstrcpy(cow_header
.backing_file
, sizeof(cow_header
.backing_file
),
250 cow_header
.sectorsize
= cpu_to_be32(512);
251 cow_header
.size
= cpu_to_be64(image_sectors
* 512);
252 ret
= qemu_write_full(cow_fd
, &cow_header
, sizeof(cow_header
));
253 if (ret
!= sizeof(cow_header
)) {
258 /* resize to include at least all the bitmap */
259 ret
= ftruncate(cow_fd
, sizeof(cow_header
) + ((image_sectors
+ 7) >> 3));
270 static void cow_flush(BlockDriverState
*bs
)
272 BDRVCowState
*s
= bs
->opaque
;
273 qemu_fdatasync(s
->fd
);
276 static QEMUOptionParameter cow_create_options
[] = {
278 .name
= BLOCK_OPT_SIZE
,
280 .help
= "Virtual disk size"
283 .name
= BLOCK_OPT_BACKING_FILE
,
285 .help
= "File name of a base image"
290 static BlockDriver bdrv_cow
= {
291 .format_name
= "cow",
292 .instance_size
= sizeof(BDRVCowState
),
293 .bdrv_probe
= cow_probe
,
294 .bdrv_file_open
= cow_open
,
295 .bdrv_read
= cow_read
,
296 .bdrv_write
= cow_write
,
297 .bdrv_close
= cow_close
,
298 .bdrv_create
= cow_create
,
299 .bdrv_flush
= cow_flush
,
300 .bdrv_is_allocated
= cow_is_allocated
,
302 .create_options
= cow_create_options
,
305 static void bdrv_cow_init(void)
307 bdrv_register(&bdrv_cow
);
310 block_init(bdrv_cow_init
);