]>
Commit | Line | Data |
---|---|---|
ea2384d3 FB |
1 | /* |
2 | * Block driver for the COW format | |
5fafdf24 | 3 | * |
ea2384d3 | 4 | * Copyright (c) 2004 Fabrice Bellard |
5fafdf24 | 5 | * |
ea2384d3 FB |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * THE SOFTWARE. | |
23 | */ | |
faf07963 | 24 | #include "qemu-common.h" |
ea2384d3 | 25 | #include "block_int.h" |
5efa9d5a | 26 | #include "module.h" |
ea2384d3 FB |
27 | |
28 | /**************************************************************/ | |
29 | /* COW block driver using file system holes */ | |
30 | ||
31 | /* user mode linux compatible COW file */ | |
32 | #define COW_MAGIC 0x4f4f4f4d /* MOOO */ | |
33 | #define COW_VERSION 2 | |
34 | ||
35 | struct cow_header_v2 { | |
36 | uint32_t magic; | |
37 | uint32_t version; | |
38 | char backing_file[1024]; | |
39 | int32_t mtime; | |
40 | uint64_t size; | |
41 | uint32_t sectorsize; | |
42 | }; | |
43 | ||
44 | typedef struct BDRVCowState { | |
848c66e8 | 45 | CoMutex lock; |
ea2384d3 FB |
46 | int64_t cow_sectors_offset; |
47 | } BDRVCowState; | |
48 | ||
49 | static int cow_probe(const uint8_t *buf, int buf_size, const char *filename) | |
50 | { | |
51 | const struct cow_header_v2 *cow_header = (const void *)buf; | |
52 | ||
712e7874 FB |
53 | if (buf_size >= sizeof(struct cow_header_v2) && |
54 | be32_to_cpu(cow_header->magic) == COW_MAGIC && | |
5fafdf24 | 55 | be32_to_cpu(cow_header->version) == COW_VERSION) |
ea2384d3 FB |
56 | return 100; |
57 | else | |
58 | return 0; | |
59 | } | |
60 | ||
2063392a | 61 | static int cow_open(BlockDriverState *bs, int flags) |
ea2384d3 FB |
62 | { |
63 | BDRVCowState *s = bs->opaque; | |
ea2384d3 | 64 | struct cow_header_v2 cow_header; |
893a9cb4 | 65 | int bitmap_size; |
ea2384d3 FB |
66 | int64_t size; |
67 | ||
ea2384d3 | 68 | /* see if it is a cow image */ |
2063392a CH |
69 | if (bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header)) != |
70 | sizeof(cow_header)) { | |
ea2384d3 FB |
71 | goto fail; |
72 | } | |
73 | ||
74 | if (be32_to_cpu(cow_header.magic) != COW_MAGIC || | |
75 | be32_to_cpu(cow_header.version) != COW_VERSION) { | |
76 | goto fail; | |
77 | } | |
3b46e624 | 78 | |
ea2384d3 FB |
79 | /* cow image found */ |
80 | size = be64_to_cpu(cow_header.size); | |
81 | bs->total_sectors = size / 512; | |
82 | ||
5fafdf24 | 83 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), |
ea2384d3 | 84 | cow_header.backing_file); |
3b46e624 | 85 | |
893a9cb4 CH |
86 | bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header); |
87 | s->cow_sectors_offset = (bitmap_size + 511) & ~511; | |
848c66e8 | 88 | qemu_co_mutex_init(&s->lock); |
ea2384d3 FB |
89 | return 0; |
90 | fail: | |
ea2384d3 FB |
91 | return -1; |
92 | } | |
93 | ||
893a9cb4 CH |
94 | /* |
95 | * XXX(hch): right now these functions are extremly ineffcient. | |
96 | * We should just read the whole bitmap we'll need in one go instead. | |
97 | */ | |
98 | static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum) | |
ea2384d3 | 99 | { |
893a9cb4 CH |
100 | uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8; |
101 | uint8_t bitmap; | |
b0ad5a45 | 102 | int ret; |
893a9cb4 | 103 | |
b0ad5a45 KW |
104 | ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)); |
105 | if (ret < 0) { | |
106 | return ret; | |
893a9cb4 CH |
107 | } |
108 | ||
109 | bitmap |= (1 << (bitnum % 8)); | |
110 | ||
b0ad5a45 KW |
111 | ret = bdrv_pwrite_sync(bs->file, offset, &bitmap, sizeof(bitmap)); |
112 | if (ret < 0) { | |
113 | return ret; | |
893a9cb4 CH |
114 | } |
115 | return 0; | |
ea2384d3 FB |
116 | } |
117 | ||
893a9cb4 | 118 | static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum) |
ea2384d3 | 119 | { |
893a9cb4 CH |
120 | uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8; |
121 | uint8_t bitmap; | |
b0ad5a45 | 122 | int ret; |
ea2384d3 | 123 | |
b0ad5a45 KW |
124 | ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)); |
125 | if (ret < 0) { | |
126 | return ret; | |
893a9cb4 CH |
127 | } |
128 | ||
129 | return !!(bitmap & (1 << (bitnum % 8))); | |
130 | } | |
ea2384d3 FB |
131 | |
132 | /* Return true if first block has been changed (ie. current version is | |
133 | * in COW file). Set the number of continuous blocks for which that | |
134 | * is true. */ | |
893a9cb4 CH |
135 | static int cow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
136 | int nb_sectors, int *num_same) | |
ea2384d3 FB |
137 | { |
138 | int changed; | |
139 | ||
893a9cb4 | 140 | if (nb_sectors == 0) { |
ea2384d3 FB |
141 | *num_same = nb_sectors; |
142 | return 0; | |
143 | } | |
144 | ||
893a9cb4 CH |
145 | changed = is_bit_set(bs, sector_num); |
146 | if (changed < 0) { | |
147 | return 0; /* XXX: how to return I/O errors? */ | |
148 | } | |
149 | ||
ea2384d3 | 150 | for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) { |
893a9cb4 | 151 | if (is_bit_set(bs, sector_num + *num_same) != changed) |
ea2384d3 FB |
152 | break; |
153 | } | |
154 | ||
155 | return changed; | |
156 | } | |
157 | ||
893a9cb4 CH |
158 | static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num, |
159 | int nb_sectors) | |
ea2384d3 | 160 | { |
893a9cb4 CH |
161 | int error = 0; |
162 | int i; | |
163 | ||
164 | for (i = 0; i < nb_sectors; i++) { | |
165 | error = cow_set_bit(bs, sector_num + i); | |
166 | if (error) { | |
167 | break; | |
168 | } | |
169 | } | |
170 | ||
171 | return error; | |
ea2384d3 FB |
172 | } |
173 | ||
5fafdf24 | 174 | static int cow_read(BlockDriverState *bs, int64_t sector_num, |
ea2384d3 FB |
175 | uint8_t *buf, int nb_sectors) |
176 | { | |
177 | BDRVCowState *s = bs->opaque; | |
178 | int ret, n; | |
3b46e624 | 179 | |
ea2384d3 | 180 | while (nb_sectors > 0) { |
893a9cb4 | 181 | if (cow_is_allocated(bs, sector_num, nb_sectors, &n)) { |
2063392a CH |
182 | ret = bdrv_pread(bs->file, |
183 | s->cow_sectors_offset + sector_num * 512, | |
184 | buf, n * 512); | |
5fafdf24 | 185 | if (ret != n * 512) |
ea2384d3 FB |
186 | return -1; |
187 | } else { | |
83f64091 FB |
188 | if (bs->backing_hd) { |
189 | /* read from the base image */ | |
190 | ret = bdrv_read(bs->backing_hd, sector_num, buf, n); | |
191 | if (ret < 0) | |
192 | return -1; | |
193 | } else { | |
ea2384d3 FB |
194 | memset(buf, 0, n * 512); |
195 | } | |
83f64091 | 196 | } |
ea2384d3 FB |
197 | nb_sectors -= n; |
198 | sector_num += n; | |
199 | buf += n * 512; | |
200 | } | |
201 | return 0; | |
202 | } | |
203 | ||
2914caa0 PB |
204 | static coroutine_fn int cow_co_read(BlockDriverState *bs, int64_t sector_num, |
205 | uint8_t *buf, int nb_sectors) | |
206 | { | |
207 | int ret; | |
208 | BDRVCowState *s = bs->opaque; | |
209 | qemu_co_mutex_lock(&s->lock); | |
210 | ret = cow_read(bs, sector_num, buf, nb_sectors); | |
211 | qemu_co_mutex_unlock(&s->lock); | |
212 | return ret; | |
213 | } | |
214 | ||
5fafdf24 | 215 | static int cow_write(BlockDriverState *bs, int64_t sector_num, |
ea2384d3 FB |
216 | const uint8_t *buf, int nb_sectors) |
217 | { | |
218 | BDRVCowState *s = bs->opaque; | |
893a9cb4 | 219 | int ret; |
3b46e624 | 220 | |
2063392a CH |
221 | ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512, |
222 | buf, nb_sectors * 512); | |
5fafdf24 | 223 | if (ret != nb_sectors * 512) |
ea2384d3 | 224 | return -1; |
893a9cb4 CH |
225 | |
226 | return cow_update_bitmap(bs, sector_num, nb_sectors); | |
ea2384d3 FB |
227 | } |
228 | ||
e183ef75 PB |
229 | static coroutine_fn int cow_co_write(BlockDriverState *bs, int64_t sector_num, |
230 | const uint8_t *buf, int nb_sectors) | |
231 | { | |
232 | int ret; | |
233 | BDRVCowState *s = bs->opaque; | |
234 | qemu_co_mutex_lock(&s->lock); | |
235 | ret = cow_write(bs, sector_num, buf, nb_sectors); | |
236 | qemu_co_mutex_unlock(&s->lock); | |
237 | return ret; | |
238 | } | |
239 | ||
e2731add | 240 | static void cow_close(BlockDriverState *bs) |
ea2384d3 | 241 | { |
ea2384d3 FB |
242 | } |
243 | ||
0e7e1989 | 244 | static int cow_create(const char *filename, QEMUOptionParameter *options) |
ea2384d3 | 245 | { |
ea2384d3 FB |
246 | struct cow_header_v2 cow_header; |
247 | struct stat st; | |
0e7e1989 KW |
248 | int64_t image_sectors = 0; |
249 | const char *image_filename = NULL; | |
31f38120 | 250 | int ret; |
3535a9c6 | 251 | BlockDriverState *cow_bs; |
0e7e1989 KW |
252 | |
253 | /* Read out options */ | |
254 | while (options && options->name) { | |
255 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
256 | image_sectors = options->value.n / 512; | |
257 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { | |
258 | image_filename = options->value.s; | |
259 | } | |
260 | options++; | |
261 | } | |
ea2384d3 | 262 | |
3535a9c6 LZH |
263 | ret = bdrv_create_file(filename, options); |
264 | if (ret < 0) { | |
265 | return ret; | |
266 | } | |
267 | ||
268 | ret = bdrv_file_open(&cow_bs, filename, BDRV_O_RDWR); | |
269 | if (ret < 0) { | |
270 | return ret; | |
271 | } | |
272 | ||
ea2384d3 FB |
273 | memset(&cow_header, 0, sizeof(cow_header)); |
274 | cow_header.magic = cpu_to_be32(COW_MAGIC); | |
275 | cow_header.version = cpu_to_be32(COW_VERSION); | |
276 | if (image_filename) { | |
83f64091 FB |
277 | /* Note: if no file, we put a dummy mtime */ |
278 | cow_header.mtime = cpu_to_be32(0); | |
279 | ||
3535a9c6 | 280 | if (stat(image_filename, &st) != 0) { |
83f64091 | 281 | goto mtime_fail; |
ea2384d3 | 282 | } |
ea2384d3 | 283 | cow_header.mtime = cpu_to_be32(st.st_mtime); |
83f64091 FB |
284 | mtime_fail: |
285 | pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file), | |
286 | image_filename); | |
ea2384d3 FB |
287 | } |
288 | cow_header.sectorsize = cpu_to_be32(512); | |
289 | cow_header.size = cpu_to_be64(image_sectors * 512); | |
3535a9c6 | 290 | ret = bdrv_pwrite(cow_bs, 0, &cow_header, sizeof(cow_header)); |
31f38120 | 291 | if (ret != sizeof(cow_header)) { |
31f38120 KS |
292 | goto exit; |
293 | } | |
294 | ||
ea2384d3 | 295 | /* resize to include at least all the bitmap */ |
3535a9c6 LZH |
296 | ret = bdrv_truncate(cow_bs, |
297 | sizeof(cow_header) + ((image_sectors + 7) >> 3)); | |
31f38120 | 298 | if (ret) { |
31f38120 KS |
299 | goto exit; |
300 | } | |
301 | ||
31f38120 | 302 | exit: |
3535a9c6 | 303 | bdrv_delete(cow_bs); |
31f38120 | 304 | return ret; |
ea2384d3 FB |
305 | } |
306 | ||
8b94ff85 | 307 | static coroutine_fn int cow_co_flush(BlockDriverState *bs) |
7a6cba61 | 308 | { |
8b94ff85 | 309 | return bdrv_co_flush(bs->file); |
7a6cba61 PB |
310 | } |
311 | ||
0e7e1989 | 312 | static QEMUOptionParameter cow_create_options[] = { |
db08adf5 KW |
313 | { |
314 | .name = BLOCK_OPT_SIZE, | |
315 | .type = OPT_SIZE, | |
316 | .help = "Virtual disk size" | |
317 | }, | |
318 | { | |
319 | .name = BLOCK_OPT_BACKING_FILE, | |
320 | .type = OPT_STRING, | |
321 | .help = "File name of a base image" | |
322 | }, | |
0e7e1989 KW |
323 | { NULL } |
324 | }; | |
325 | ||
5efa9d5a | 326 | static BlockDriver bdrv_cow = { |
c68b89ac KW |
327 | .format_name = "cow", |
328 | .instance_size = sizeof(BDRVCowState), | |
329 | ||
330 | .bdrv_probe = cow_probe, | |
331 | .bdrv_open = cow_open, | |
332 | .bdrv_close = cow_close, | |
333 | .bdrv_create = cow_create, | |
334 | ||
335 | .bdrv_read = cow_co_read, | |
336 | .bdrv_write = cow_co_write, | |
337 | .bdrv_co_flush_to_disk = cow_co_flush, | |
338 | .bdrv_is_allocated = cow_is_allocated, | |
0e7e1989 KW |
339 | |
340 | .create_options = cow_create_options, | |
ea2384d3 | 341 | }; |
5efa9d5a AL |
342 | |
343 | static void bdrv_cow_init(void) | |
344 | { | |
345 | bdrv_register(&bdrv_cow); | |
346 | } | |
347 | ||
348 | block_init(bdrv_cow_init); |