root / block-cow.c @ 94ac5158
History | View | Annotate | Download (7.8 kB)
1 | ea2384d3 | bellard | /*
|
---|---|---|---|
2 | ea2384d3 | bellard | * Block driver for the COW format
|
3 | ea2384d3 | bellard | *
|
4 | ea2384d3 | bellard | * Copyright (c) 2004 Fabrice Bellard
|
5 | ea2384d3 | bellard | *
|
6 | ea2384d3 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | ea2384d3 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | ea2384d3 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | ea2384d3 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | ea2384d3 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | ea2384d3 | bellard | * furnished to do so, subject to the following conditions:
|
12 | ea2384d3 | bellard | *
|
13 | ea2384d3 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | ea2384d3 | bellard | * all copies or substantial portions of the Software.
|
15 | ea2384d3 | bellard | *
|
16 | ea2384d3 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | ea2384d3 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | ea2384d3 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | ea2384d3 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | ea2384d3 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | ea2384d3 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | ea2384d3 | bellard | * THE SOFTWARE.
|
23 | ea2384d3 | bellard | */
|
24 | ea2384d3 | bellard | #ifndef _WIN32
|
25 | ea2384d3 | bellard | #include "vl.h" |
26 | ea2384d3 | bellard | #include "block_int.h" |
27 | ea2384d3 | bellard | #include <sys/mman.h> |
28 | ea2384d3 | bellard | |
29 | ea2384d3 | bellard | /**************************************************************/
|
30 | ea2384d3 | bellard | /* COW block driver using file system holes */
|
31 | ea2384d3 | bellard | |
32 | ea2384d3 | bellard | /* user mode linux compatible COW file */
|
33 | ea2384d3 | bellard | #define COW_MAGIC 0x4f4f4f4d /* MOOO */ |
34 | ea2384d3 | bellard | #define COW_VERSION 2 |
35 | ea2384d3 | bellard | |
36 | ea2384d3 | bellard | struct cow_header_v2 {
|
37 | ea2384d3 | bellard | uint32_t magic; |
38 | ea2384d3 | bellard | uint32_t version; |
39 | ea2384d3 | bellard | char backing_file[1024]; |
40 | ea2384d3 | bellard | int32_t mtime; |
41 | ea2384d3 | bellard | uint64_t size; |
42 | ea2384d3 | bellard | uint32_t sectorsize; |
43 | ea2384d3 | bellard | }; |
44 | ea2384d3 | bellard | |
45 | ea2384d3 | bellard | typedef struct BDRVCowState { |
46 | ea2384d3 | bellard | int fd;
|
47 | ea2384d3 | bellard | uint8_t *cow_bitmap; /* if non NULL, COW mappings are used first */
|
48 | ea2384d3 | bellard | uint8_t *cow_bitmap_addr; /* mmap address of cow_bitmap */
|
49 | ea2384d3 | bellard | int cow_bitmap_size;
|
50 | ea2384d3 | bellard | int64_t cow_sectors_offset; |
51 | ea2384d3 | bellard | } BDRVCowState; |
52 | ea2384d3 | bellard | |
53 | ea2384d3 | bellard | static int cow_probe(const uint8_t *buf, int buf_size, const char *filename) |
54 | ea2384d3 | bellard | { |
55 | ea2384d3 | bellard | const struct cow_header_v2 *cow_header = (const void *)buf; |
56 | ea2384d3 | bellard | |
57 | 712e7874 | bellard | if (buf_size >= sizeof(struct cow_header_v2) && |
58 | 712e7874 | bellard | be32_to_cpu(cow_header->magic) == COW_MAGIC && |
59 | ea2384d3 | bellard | be32_to_cpu(cow_header->version) == COW_VERSION) |
60 | ea2384d3 | bellard | return 100; |
61 | ea2384d3 | bellard | else
|
62 | ea2384d3 | bellard | return 0; |
63 | ea2384d3 | bellard | } |
64 | ea2384d3 | bellard | |
65 | ea2384d3 | bellard | static int cow_open(BlockDriverState *bs, const char *filename) |
66 | ea2384d3 | bellard | { |
67 | ea2384d3 | bellard | BDRVCowState *s = bs->opaque; |
68 | ea2384d3 | bellard | int fd;
|
69 | ea2384d3 | bellard | struct cow_header_v2 cow_header;
|
70 | ea2384d3 | bellard | int64_t size; |
71 | ea2384d3 | bellard | |
72 | ea2384d3 | bellard | fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
73 | ea2384d3 | bellard | if (fd < 0) { |
74 | ea2384d3 | bellard | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
75 | ea2384d3 | bellard | if (fd < 0) |
76 | ea2384d3 | bellard | return -1; |
77 | ea2384d3 | bellard | } |
78 | ea2384d3 | bellard | s->fd = fd; |
79 | ea2384d3 | bellard | /* see if it is a cow image */
|
80 | ea2384d3 | bellard | if (read(fd, &cow_header, sizeof(cow_header)) != sizeof(cow_header)) { |
81 | ea2384d3 | bellard | goto fail;
|
82 | ea2384d3 | bellard | } |
83 | ea2384d3 | bellard | |
84 | ea2384d3 | bellard | if (be32_to_cpu(cow_header.magic) != COW_MAGIC ||
|
85 | ea2384d3 | bellard | be32_to_cpu(cow_header.version) != COW_VERSION) { |
86 | ea2384d3 | bellard | goto fail;
|
87 | ea2384d3 | bellard | } |
88 | ea2384d3 | bellard | |
89 | ea2384d3 | bellard | /* cow image found */
|
90 | ea2384d3 | bellard | size = be64_to_cpu(cow_header.size); |
91 | ea2384d3 | bellard | bs->total_sectors = size / 512;
|
92 | ea2384d3 | bellard | |
93 | ea2384d3 | bellard | pstrcpy(bs->backing_file, sizeof(bs->backing_file),
|
94 | ea2384d3 | bellard | cow_header.backing_file); |
95 | ea2384d3 | bellard | |
96 | ea2384d3 | bellard | #if 0
|
97 | ea2384d3 | bellard | if (cow_header.backing_file[0] != '\0') {
|
98 | ea2384d3 | bellard | if (stat(cow_header.backing_file, &st) != 0) {
|
99 | ea2384d3 | bellard | fprintf(stderr, "%s: could not find original disk image '%s'\n", filename, cow_header.backing_file);
|
100 | ea2384d3 | bellard | goto fail;
|
101 | ea2384d3 | bellard | }
|
102 | ea2384d3 | bellard | if (st.st_mtime != be32_to_cpu(cow_header.mtime)) {
|
103 | ea2384d3 | bellard | fprintf(stderr, "%s: original raw disk image '%s' does not match saved timestamp\n", filename, cow_header.backing_file);
|
104 | ea2384d3 | bellard | goto fail;
|
105 | ea2384d3 | bellard | }
|
106 | ea2384d3 | bellard | fd = open(cow_header.backing_file, O_RDONLY | O_LARGEFILE);
|
107 | ea2384d3 | bellard | if (fd < 0)
|
108 | ea2384d3 | bellard | goto fail;
|
109 | ea2384d3 | bellard | bs->fd = fd;
|
110 | ea2384d3 | bellard | }
|
111 | ea2384d3 | bellard | #endif
|
112 | ea2384d3 | bellard | /* mmap the bitmap */
|
113 | ea2384d3 | bellard | s->cow_bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header); |
114 | ea2384d3 | bellard | s->cow_bitmap_addr = mmap(get_mmap_addr(s->cow_bitmap_size), |
115 | ea2384d3 | bellard | s->cow_bitmap_size, |
116 | ea2384d3 | bellard | PROT_READ | PROT_WRITE, |
117 | ea2384d3 | bellard | MAP_SHARED, s->fd, 0);
|
118 | ea2384d3 | bellard | if (s->cow_bitmap_addr == MAP_FAILED)
|
119 | ea2384d3 | bellard | goto fail;
|
120 | ea2384d3 | bellard | s->cow_bitmap = s->cow_bitmap_addr + sizeof(cow_header);
|
121 | ea2384d3 | bellard | s->cow_sectors_offset = (s->cow_bitmap_size + 511) & ~511; |
122 | ea2384d3 | bellard | return 0; |
123 | ea2384d3 | bellard | fail:
|
124 | ea2384d3 | bellard | close(fd); |
125 | ea2384d3 | bellard | return -1; |
126 | ea2384d3 | bellard | } |
127 | ea2384d3 | bellard | |
128 | 2b03a7a5 | bellard | static inline void cow_set_bit(uint8_t *bitmap, int64_t bitnum) |
129 | ea2384d3 | bellard | { |
130 | ea2384d3 | bellard | bitmap[bitnum / 8] |= (1 << (bitnum%8)); |
131 | ea2384d3 | bellard | } |
132 | ea2384d3 | bellard | |
133 | ea2384d3 | bellard | static inline int is_bit_set(const uint8_t *bitmap, int64_t bitnum) |
134 | ea2384d3 | bellard | { |
135 | ea2384d3 | bellard | return !!(bitmap[bitnum / 8] & (1 << (bitnum%8))); |
136 | ea2384d3 | bellard | } |
137 | ea2384d3 | bellard | |
138 | ea2384d3 | bellard | |
139 | ea2384d3 | bellard | /* Return true if first block has been changed (ie. current version is
|
140 | ea2384d3 | bellard | * in COW file). Set the number of continuous blocks for which that
|
141 | ea2384d3 | bellard | * is true. */
|
142 | ea2384d3 | bellard | static inline int is_changed(uint8_t *bitmap, |
143 | ea2384d3 | bellard | int64_t sector_num, int nb_sectors,
|
144 | ea2384d3 | bellard | int *num_same)
|
145 | ea2384d3 | bellard | { |
146 | ea2384d3 | bellard | int changed;
|
147 | ea2384d3 | bellard | |
148 | ea2384d3 | bellard | if (!bitmap || nb_sectors == 0) { |
149 | ea2384d3 | bellard | *num_same = nb_sectors; |
150 | ea2384d3 | bellard | return 0; |
151 | ea2384d3 | bellard | } |
152 | ea2384d3 | bellard | |
153 | ea2384d3 | bellard | changed = is_bit_set(bitmap, sector_num); |
154 | ea2384d3 | bellard | for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) { |
155 | ea2384d3 | bellard | if (is_bit_set(bitmap, sector_num + *num_same) != changed)
|
156 | ea2384d3 | bellard | break;
|
157 | ea2384d3 | bellard | } |
158 | ea2384d3 | bellard | |
159 | ea2384d3 | bellard | return changed;
|
160 | ea2384d3 | bellard | } |
161 | ea2384d3 | bellard | |
162 | ea2384d3 | bellard | static int cow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
163 | ea2384d3 | bellard | int nb_sectors, int *pnum) |
164 | ea2384d3 | bellard | { |
165 | ea2384d3 | bellard | BDRVCowState *s = bs->opaque; |
166 | ea2384d3 | bellard | return is_changed(s->cow_bitmap, sector_num, nb_sectors, pnum);
|
167 | ea2384d3 | bellard | } |
168 | ea2384d3 | bellard | |
169 | ea2384d3 | bellard | static int cow_read(BlockDriverState *bs, int64_t sector_num, |
170 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
171 | ea2384d3 | bellard | { |
172 | ea2384d3 | bellard | BDRVCowState *s = bs->opaque; |
173 | ea2384d3 | bellard | int ret, n;
|
174 | ea2384d3 | bellard | |
175 | ea2384d3 | bellard | while (nb_sectors > 0) { |
176 | ea2384d3 | bellard | if (is_changed(s->cow_bitmap, sector_num, nb_sectors, &n)) {
|
177 | d5249393 | bellard | lseek(s->fd, s->cow_sectors_offset + sector_num * 512, SEEK_SET);
|
178 | ea2384d3 | bellard | ret = read(s->fd, buf, n * 512);
|
179 | ea2384d3 | bellard | if (ret != n * 512) |
180 | ea2384d3 | bellard | return -1; |
181 | ea2384d3 | bellard | } else {
|
182 | ea2384d3 | bellard | memset(buf, 0, n * 512); |
183 | ea2384d3 | bellard | } |
184 | ea2384d3 | bellard | nb_sectors -= n; |
185 | ea2384d3 | bellard | sector_num += n; |
186 | ea2384d3 | bellard | buf += n * 512;
|
187 | ea2384d3 | bellard | } |
188 | ea2384d3 | bellard | return 0; |
189 | ea2384d3 | bellard | } |
190 | ea2384d3 | bellard | |
191 | ea2384d3 | bellard | static int cow_write(BlockDriverState *bs, int64_t sector_num, |
192 | ea2384d3 | bellard | const uint8_t *buf, int nb_sectors) |
193 | ea2384d3 | bellard | { |
194 | ea2384d3 | bellard | BDRVCowState *s = bs->opaque; |
195 | ea2384d3 | bellard | int ret, i;
|
196 | ea2384d3 | bellard | |
197 | d5249393 | bellard | lseek(s->fd, s->cow_sectors_offset + sector_num * 512, SEEK_SET);
|
198 | ea2384d3 | bellard | ret = write(s->fd, buf, nb_sectors * 512);
|
199 | ea2384d3 | bellard | if (ret != nb_sectors * 512) |
200 | ea2384d3 | bellard | return -1; |
201 | ea2384d3 | bellard | for (i = 0; i < nb_sectors; i++) |
202 | 2b03a7a5 | bellard | cow_set_bit(s->cow_bitmap, sector_num + i); |
203 | ea2384d3 | bellard | return 0; |
204 | ea2384d3 | bellard | } |
205 | ea2384d3 | bellard | |
206 | e2731add | bellard | static void cow_close(BlockDriverState *bs) |
207 | ea2384d3 | bellard | { |
208 | ea2384d3 | bellard | BDRVCowState *s = bs->opaque; |
209 | ea2384d3 | bellard | munmap(s->cow_bitmap_addr, s->cow_bitmap_size); |
210 | ea2384d3 | bellard | close(s->fd); |
211 | ea2384d3 | bellard | } |
212 | ea2384d3 | bellard | |
213 | ea2384d3 | bellard | static int cow_create(const char *filename, int64_t image_sectors, |
214 | ea2384d3 | bellard | const char *image_filename, int flags) |
215 | ea2384d3 | bellard | { |
216 | ea2384d3 | bellard | int fd, cow_fd;
|
217 | ea2384d3 | bellard | struct cow_header_v2 cow_header;
|
218 | ea2384d3 | bellard | struct stat st;
|
219 | ea2384d3 | bellard | |
220 | ea2384d3 | bellard | if (flags)
|
221 | ea2384d3 | bellard | return -ENOTSUP;
|
222 | ea2384d3 | bellard | |
223 | ea2384d3 | bellard | cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
224 | ea2384d3 | bellard | 0644);
|
225 | ea2384d3 | bellard | if (cow_fd < 0) |
226 | ea2384d3 | bellard | return -1; |
227 | ea2384d3 | bellard | memset(&cow_header, 0, sizeof(cow_header)); |
228 | ea2384d3 | bellard | cow_header.magic = cpu_to_be32(COW_MAGIC); |
229 | ea2384d3 | bellard | cow_header.version = cpu_to_be32(COW_VERSION); |
230 | ea2384d3 | bellard | if (image_filename) {
|
231 | ea2384d3 | bellard | fd = open(image_filename, O_RDONLY | O_BINARY); |
232 | ea2384d3 | bellard | if (fd < 0) { |
233 | ea2384d3 | bellard | close(cow_fd); |
234 | ea2384d3 | bellard | return -1; |
235 | ea2384d3 | bellard | } |
236 | ea2384d3 | bellard | if (fstat(fd, &st) != 0) { |
237 | ea2384d3 | bellard | close(fd); |
238 | ea2384d3 | bellard | return -1; |
239 | ea2384d3 | bellard | } |
240 | ea2384d3 | bellard | close(fd); |
241 | ea2384d3 | bellard | cow_header.mtime = cpu_to_be32(st.st_mtime); |
242 | ea2384d3 | bellard | realpath(image_filename, cow_header.backing_file); |
243 | ea2384d3 | bellard | } |
244 | ea2384d3 | bellard | cow_header.sectorsize = cpu_to_be32(512);
|
245 | ea2384d3 | bellard | cow_header.size = cpu_to_be64(image_sectors * 512);
|
246 | ea2384d3 | bellard | write(cow_fd, &cow_header, sizeof(cow_header));
|
247 | ea2384d3 | bellard | /* resize to include at least all the bitmap */
|
248 | ea2384d3 | bellard | ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3)); |
249 | ea2384d3 | bellard | close(cow_fd); |
250 | ea2384d3 | bellard | return 0; |
251 | ea2384d3 | bellard | } |
252 | ea2384d3 | bellard | |
253 | ea2384d3 | bellard | BlockDriver bdrv_cow = { |
254 | ea2384d3 | bellard | "cow",
|
255 | ea2384d3 | bellard | sizeof(BDRVCowState),
|
256 | ea2384d3 | bellard | cow_probe, |
257 | ea2384d3 | bellard | cow_open, |
258 | ea2384d3 | bellard | cow_read, |
259 | ea2384d3 | bellard | cow_write, |
260 | ea2384d3 | bellard | cow_close, |
261 | ea2384d3 | bellard | cow_create, |
262 | ea2384d3 | bellard | cow_is_allocated, |
263 | ea2384d3 | bellard | }; |
264 | ea2384d3 | bellard | #endif |