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
26 #include "block_int.h"
29 /**************************************************************/
30 /* COW block driver using file system holes */
32 /* user mode linux compatible COW file */
33 #define COW_MAGIC 0x4f4f4f4d /* MOOO */
36 struct cow_header_v2
{
39 char backing_file
[1024];
45 typedef struct BDRVCowState
{
47 uint8_t *cow_bitmap
; /* if non NULL, COW mappings are used first */
48 uint8_t *cow_bitmap_addr
; /* mmap address of cow_bitmap */
50 int64_t cow_sectors_offset
;
53 static int cow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
55 const struct cow_header_v2
*cow_header
= (const void *)buf
;
57 if (buf_size
>= sizeof(struct cow_header_v2
) &&
58 be32_to_cpu(cow_header
->magic
) == COW_MAGIC
&&
59 be32_to_cpu(cow_header
->version
) == COW_VERSION
)
65 static int cow_open(BlockDriverState
*bs
, const char *filename
)
67 BDRVCowState
*s
= bs
->opaque
;
69 struct cow_header_v2 cow_header
;
72 fd
= open(filename
, O_RDWR
| O_BINARY
| O_LARGEFILE
);
74 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
79 /* see if it is a cow image */
80 if (read(fd
, &cow_header
, sizeof(cow_header
)) != sizeof(cow_header
)) {
84 if (be32_to_cpu(cow_header
.magic
) != COW_MAGIC
||
85 be32_to_cpu(cow_header
.version
) != COW_VERSION
) {
90 size
= be64_to_cpu(cow_header
.size
);
91 bs
->total_sectors
= size
/ 512;
93 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
94 cow_header
.backing_file
);
97 if (cow_header
.backing_file
[0] != '\0') {
98 if (stat(cow_header
.backing_file
, &st
) != 0) {
99 fprintf(stderr
, "%s: could not find original disk image '%s'\n", filename
, cow_header
.backing_file
);
102 if (st
.st_mtime
!= be32_to_cpu(cow_header
.mtime
)) {
103 fprintf(stderr
, "%s: original raw disk image '%s' does not match saved timestamp\n", filename
, cow_header
.backing_file
);
106 fd
= open(cow_header
.backing_file
, O_RDONLY
| O_LARGEFILE
);
112 /* mmap the bitmap */
113 s
->cow_bitmap_size
= ((bs
->total_sectors
+ 7) >> 3) + sizeof(cow_header
);
114 s
->cow_bitmap_addr
= mmap(get_mmap_addr(s
->cow_bitmap_size
),
116 PROT_READ
| PROT_WRITE
,
117 MAP_SHARED
, s
->fd
, 0);
118 if (s
->cow_bitmap_addr
== MAP_FAILED
)
120 s
->cow_bitmap
= s
->cow_bitmap_addr
+ sizeof(cow_header
);
121 s
->cow_sectors_offset
= (s
->cow_bitmap_size
+ 511) & ~511;
128 static inline void cow_set_bit(uint8_t *bitmap
, int64_t bitnum
)
130 bitmap
[bitnum
/ 8] |= (1 << (bitnum
%8));
133 static inline int is_bit_set(const uint8_t *bitmap
, int64_t bitnum
)
135 return !!(bitmap
[bitnum
/ 8] & (1 << (bitnum
%8)));
139 /* Return true if first block has been changed (ie. current version is
140 * in COW file). Set the number of continuous blocks for which that
142 static inline int is_changed(uint8_t *bitmap
,
143 int64_t sector_num
, int nb_sectors
,
148 if (!bitmap
|| nb_sectors
== 0) {
149 *num_same
= nb_sectors
;
153 changed
= is_bit_set(bitmap
, sector_num
);
154 for (*num_same
= 1; *num_same
< nb_sectors
; (*num_same
)++) {
155 if (is_bit_set(bitmap
, sector_num
+ *num_same
) != changed
)
162 static int cow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
163 int nb_sectors
, int *pnum
)
165 BDRVCowState
*s
= bs
->opaque
;
166 return is_changed(s
->cow_bitmap
, sector_num
, nb_sectors
, pnum
);
169 static int cow_read(BlockDriverState
*bs
, int64_t sector_num
,
170 uint8_t *buf
, int nb_sectors
)
172 BDRVCowState
*s
= bs
->opaque
;
175 while (nb_sectors
> 0) {
176 if (is_changed(s
->cow_bitmap
, sector_num
, nb_sectors
, &n
)) {
177 lseek(s
->fd
, s
->cow_sectors_offset
+ sector_num
* 512, SEEK_SET
);
178 ret
= read(s
->fd
, buf
, n
* 512);
182 memset(buf
, 0, n
* 512);
191 static int cow_write(BlockDriverState
*bs
, int64_t sector_num
,
192 const uint8_t *buf
, int nb_sectors
)
194 BDRVCowState
*s
= bs
->opaque
;
197 lseek(s
->fd
, s
->cow_sectors_offset
+ sector_num
* 512, SEEK_SET
);
198 ret
= write(s
->fd
, buf
, nb_sectors
* 512);
199 if (ret
!= nb_sectors
* 512)
201 for (i
= 0; i
< nb_sectors
; i
++)
202 cow_set_bit(s
->cow_bitmap
, sector_num
+ i
);
206 static void cow_close(BlockDriverState
*bs
)
208 BDRVCowState
*s
= bs
->opaque
;
209 munmap(s
->cow_bitmap_addr
, s
->cow_bitmap_size
);
213 static int cow_create(const char *filename
, int64_t image_sectors
,
214 const char *image_filename
, int flags
)
217 struct cow_header_v2 cow_header
;
223 cow_fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
227 memset(&cow_header
, 0, sizeof(cow_header
));
228 cow_header
.magic
= cpu_to_be32(COW_MAGIC
);
229 cow_header
.version
= cpu_to_be32(COW_VERSION
);
230 if (image_filename
) {
231 fd
= open(image_filename
, O_RDONLY
| O_BINARY
);
236 if (fstat(fd
, &st
) != 0) {
241 cow_header
.mtime
= cpu_to_be32(st
.st_mtime
);
242 realpath(image_filename
, cow_header
.backing_file
);
244 cow_header
.sectorsize
= cpu_to_be32(512);
245 cow_header
.size
= cpu_to_be64(image_sectors
* 512);
246 write(cow_fd
, &cow_header
, sizeof(cow_header
));
247 /* resize to include at least all the bitmap */
248 ftruncate(cow_fd
, sizeof(cow_header
) + ((image_sectors
+ 7) >> 3));
253 BlockDriver bdrv_cow
= {
255 sizeof(BDRVCowState
),