root / block-cow.c @ c171148c
History | View | Annotate | Download (7.7 kB)
1 | ea2384d3 | bellard | /*
|
---|---|---|---|
2 | ea2384d3 | bellard | * Block driver for the COW format
|
3 | 5fafdf24 | ths | *
|
4 | ea2384d3 | bellard | * Copyright (c) 2004 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
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 | faf07963 | pbrook | #include "qemu-common.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 | 5fafdf24 | ths | 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 | 83f64091 | bellard | static int cow_open(BlockDriverState *bs, const char *filename, int flags) |
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 | 3b46e624 | ths | |
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 | 5fafdf24 | ths | pstrcpy(bs->backing_file, sizeof(bs->backing_file),
|
94 | ea2384d3 | bellard | cow_header.backing_file); |
95 | 3b46e624 | ths | |
96 | ea2384d3 | bellard | /* mmap the bitmap */
|
97 | ea2384d3 | bellard | s->cow_bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header); |
98 | 5fafdf24 | ths | s->cow_bitmap_addr = mmap(get_mmap_addr(s->cow_bitmap_size), |
99 | 5fafdf24 | ths | s->cow_bitmap_size, |
100 | ea2384d3 | bellard | PROT_READ | PROT_WRITE, |
101 | ea2384d3 | bellard | MAP_SHARED, s->fd, 0);
|
102 | ea2384d3 | bellard | if (s->cow_bitmap_addr == MAP_FAILED)
|
103 | ea2384d3 | bellard | goto fail;
|
104 | ea2384d3 | bellard | s->cow_bitmap = s->cow_bitmap_addr + sizeof(cow_header);
|
105 | ea2384d3 | bellard | s->cow_sectors_offset = (s->cow_bitmap_size + 511) & ~511; |
106 | ea2384d3 | bellard | return 0; |
107 | ea2384d3 | bellard | fail:
|
108 | ea2384d3 | bellard | close(fd); |
109 | ea2384d3 | bellard | return -1; |
110 | ea2384d3 | bellard | } |
111 | ea2384d3 | bellard | |
112 | 2b03a7a5 | bellard | static inline void cow_set_bit(uint8_t *bitmap, int64_t bitnum) |
113 | ea2384d3 | bellard | { |
114 | ea2384d3 | bellard | bitmap[bitnum / 8] |= (1 << (bitnum%8)); |
115 | ea2384d3 | bellard | } |
116 | ea2384d3 | bellard | |
117 | ea2384d3 | bellard | static inline int is_bit_set(const uint8_t *bitmap, int64_t bitnum) |
118 | ea2384d3 | bellard | { |
119 | ea2384d3 | bellard | return !!(bitmap[bitnum / 8] & (1 << (bitnum%8))); |
120 | ea2384d3 | bellard | } |
121 | ea2384d3 | bellard | |
122 | ea2384d3 | bellard | |
123 | ea2384d3 | bellard | /* Return true if first block has been changed (ie. current version is
|
124 | ea2384d3 | bellard | * in COW file). Set the number of continuous blocks for which that
|
125 | ea2384d3 | bellard | * is true. */
|
126 | ea2384d3 | bellard | static inline int is_changed(uint8_t *bitmap, |
127 | ea2384d3 | bellard | int64_t sector_num, int nb_sectors,
|
128 | ea2384d3 | bellard | int *num_same)
|
129 | ea2384d3 | bellard | { |
130 | ea2384d3 | bellard | int changed;
|
131 | ea2384d3 | bellard | |
132 | ea2384d3 | bellard | if (!bitmap || nb_sectors == 0) { |
133 | ea2384d3 | bellard | *num_same = nb_sectors; |
134 | ea2384d3 | bellard | return 0; |
135 | ea2384d3 | bellard | } |
136 | ea2384d3 | bellard | |
137 | ea2384d3 | bellard | changed = is_bit_set(bitmap, sector_num); |
138 | ea2384d3 | bellard | for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) { |
139 | ea2384d3 | bellard | if (is_bit_set(bitmap, sector_num + *num_same) != changed)
|
140 | ea2384d3 | bellard | break;
|
141 | ea2384d3 | bellard | } |
142 | ea2384d3 | bellard | |
143 | ea2384d3 | bellard | return changed;
|
144 | ea2384d3 | bellard | } |
145 | ea2384d3 | bellard | |
146 | 5fafdf24 | ths | static int cow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
147 | ea2384d3 | bellard | int nb_sectors, int *pnum) |
148 | ea2384d3 | bellard | { |
149 | ea2384d3 | bellard | BDRVCowState *s = bs->opaque; |
150 | ea2384d3 | bellard | return is_changed(s->cow_bitmap, sector_num, nb_sectors, pnum);
|
151 | ea2384d3 | bellard | } |
152 | ea2384d3 | bellard | |
153 | 5fafdf24 | ths | static int cow_read(BlockDriverState *bs, int64_t sector_num, |
154 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
155 | ea2384d3 | bellard | { |
156 | ea2384d3 | bellard | BDRVCowState *s = bs->opaque; |
157 | ea2384d3 | bellard | int ret, n;
|
158 | 3b46e624 | ths | |
159 | ea2384d3 | bellard | while (nb_sectors > 0) { |
160 | ea2384d3 | bellard | if (is_changed(s->cow_bitmap, sector_num, nb_sectors, &n)) {
|
161 | d5249393 | bellard | lseek(s->fd, s->cow_sectors_offset + sector_num * 512, SEEK_SET);
|
162 | ea2384d3 | bellard | ret = read(s->fd, buf, n * 512);
|
163 | 5fafdf24 | ths | if (ret != n * 512) |
164 | ea2384d3 | bellard | return -1; |
165 | ea2384d3 | bellard | } else {
|
166 | 83f64091 | bellard | if (bs->backing_hd) {
|
167 | 83f64091 | bellard | /* read from the base image */
|
168 | 83f64091 | bellard | ret = bdrv_read(bs->backing_hd, sector_num, buf, n); |
169 | 83f64091 | bellard | if (ret < 0) |
170 | 83f64091 | bellard | return -1; |
171 | 83f64091 | bellard | } else {
|
172 | ea2384d3 | bellard | memset(buf, 0, n * 512); |
173 | ea2384d3 | bellard | } |
174 | 83f64091 | bellard | } |
175 | ea2384d3 | bellard | nb_sectors -= n; |
176 | ea2384d3 | bellard | sector_num += n; |
177 | ea2384d3 | bellard | buf += n * 512;
|
178 | ea2384d3 | bellard | } |
179 | ea2384d3 | bellard | return 0; |
180 | ea2384d3 | bellard | } |
181 | ea2384d3 | bellard | |
182 | 5fafdf24 | ths | static int cow_write(BlockDriverState *bs, int64_t sector_num, |
183 | ea2384d3 | bellard | const uint8_t *buf, int nb_sectors) |
184 | ea2384d3 | bellard | { |
185 | ea2384d3 | bellard | BDRVCowState *s = bs->opaque; |
186 | ea2384d3 | bellard | int ret, i;
|
187 | 3b46e624 | ths | |
188 | d5249393 | bellard | lseek(s->fd, s->cow_sectors_offset + sector_num * 512, SEEK_SET);
|
189 | ea2384d3 | bellard | ret = write(s->fd, buf, nb_sectors * 512);
|
190 | 5fafdf24 | ths | if (ret != nb_sectors * 512) |
191 | ea2384d3 | bellard | return -1; |
192 | ea2384d3 | bellard | for (i = 0; i < nb_sectors; i++) |
193 | 2b03a7a5 | bellard | cow_set_bit(s->cow_bitmap, sector_num + i); |
194 | ea2384d3 | bellard | return 0; |
195 | ea2384d3 | bellard | } |
196 | ea2384d3 | bellard | |
197 | e2731add | bellard | static void cow_close(BlockDriverState *bs) |
198 | ea2384d3 | bellard | { |
199 | ea2384d3 | bellard | BDRVCowState *s = bs->opaque; |
200 | ea2384d3 | bellard | munmap(s->cow_bitmap_addr, s->cow_bitmap_size); |
201 | ea2384d3 | bellard | close(s->fd); |
202 | ea2384d3 | bellard | } |
203 | ea2384d3 | bellard | |
204 | ea2384d3 | bellard | static int cow_create(const char *filename, int64_t image_sectors, |
205 | ea2384d3 | bellard | const char *image_filename, int flags) |
206 | ea2384d3 | bellard | { |
207 | ea2384d3 | bellard | int fd, cow_fd;
|
208 | ea2384d3 | bellard | struct cow_header_v2 cow_header;
|
209 | ea2384d3 | bellard | struct stat st;
|
210 | ea2384d3 | bellard | |
211 | ea2384d3 | bellard | if (flags)
|
212 | ea2384d3 | bellard | return -ENOTSUP;
|
213 | ea2384d3 | bellard | |
214 | 5fafdf24 | ths | cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
215 | ea2384d3 | bellard | 0644);
|
216 | ea2384d3 | bellard | if (cow_fd < 0) |
217 | ea2384d3 | bellard | return -1; |
218 | ea2384d3 | bellard | memset(&cow_header, 0, sizeof(cow_header)); |
219 | ea2384d3 | bellard | cow_header.magic = cpu_to_be32(COW_MAGIC); |
220 | ea2384d3 | bellard | cow_header.version = cpu_to_be32(COW_VERSION); |
221 | ea2384d3 | bellard | if (image_filename) {
|
222 | 83f64091 | bellard | /* Note: if no file, we put a dummy mtime */
|
223 | 83f64091 | bellard | cow_header.mtime = cpu_to_be32(0);
|
224 | 83f64091 | bellard | |
225 | ea2384d3 | bellard | fd = open(image_filename, O_RDONLY | O_BINARY); |
226 | ea2384d3 | bellard | if (fd < 0) { |
227 | ea2384d3 | bellard | close(cow_fd); |
228 | 83f64091 | bellard | goto mtime_fail;
|
229 | ea2384d3 | bellard | } |
230 | ea2384d3 | bellard | if (fstat(fd, &st) != 0) { |
231 | ea2384d3 | bellard | close(fd); |
232 | 83f64091 | bellard | goto mtime_fail;
|
233 | ea2384d3 | bellard | } |
234 | ea2384d3 | bellard | close(fd); |
235 | ea2384d3 | bellard | cow_header.mtime = cpu_to_be32(st.st_mtime); |
236 | 83f64091 | bellard | mtime_fail:
|
237 | 83f64091 | bellard | pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file),
|
238 | 83f64091 | bellard | image_filename); |
239 | ea2384d3 | bellard | } |
240 | ea2384d3 | bellard | cow_header.sectorsize = cpu_to_be32(512);
|
241 | ea2384d3 | bellard | cow_header.size = cpu_to_be64(image_sectors * 512);
|
242 | ea2384d3 | bellard | write(cow_fd, &cow_header, sizeof(cow_header));
|
243 | ea2384d3 | bellard | /* resize to include at least all the bitmap */
|
244 | ea2384d3 | bellard | ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3)); |
245 | ea2384d3 | bellard | close(cow_fd); |
246 | ea2384d3 | bellard | return 0; |
247 | ea2384d3 | bellard | } |
248 | ea2384d3 | bellard | |
249 | 7a6cba61 | pbrook | static void cow_flush(BlockDriverState *bs) |
250 | 7a6cba61 | pbrook | { |
251 | 7a6cba61 | pbrook | BDRVCowState *s = bs->opaque; |
252 | 7a6cba61 | pbrook | fsync(s->fd); |
253 | 7a6cba61 | pbrook | } |
254 | 7a6cba61 | pbrook | |
255 | ea2384d3 | bellard | BlockDriver bdrv_cow = { |
256 | ea2384d3 | bellard | "cow",
|
257 | ea2384d3 | bellard | sizeof(BDRVCowState),
|
258 | ea2384d3 | bellard | cow_probe, |
259 | ea2384d3 | bellard | cow_open, |
260 | ea2384d3 | bellard | cow_read, |
261 | ea2384d3 | bellard | cow_write, |
262 | ea2384d3 | bellard | cow_close, |
263 | ea2384d3 | bellard | cow_create, |
264 | 7a6cba61 | pbrook | cow_flush, |
265 | ea2384d3 | bellard | cow_is_allocated, |
266 | ea2384d3 | bellard | }; |
267 | ea2384d3 | bellard | #endif |