]>
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" |
737e150e | 25 | #include "block/block_int.h" |
1de7afc9 | 26 | #include "qemu/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 | ||
015a1036 HR |
61 | static int cow_open(BlockDriverState *bs, QDict *options, int flags, |
62 | Error **errp) | |
ea2384d3 FB |
63 | { |
64 | BDRVCowState *s = bs->opaque; | |
ea2384d3 | 65 | struct cow_header_v2 cow_header; |
893a9cb4 | 66 | int bitmap_size; |
ea2384d3 | 67 | int64_t size; |
16d2fc00 | 68 | int ret; |
ea2384d3 | 69 | |
ea2384d3 | 70 | /* see if it is a cow image */ |
16d2fc00 LZH |
71 | ret = bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header)); |
72 | if (ret < 0) { | |
73 | goto fail; | |
74 | } | |
75 | ||
76 | if (be32_to_cpu(cow_header.magic) != COW_MAGIC) { | |
76abe407 PB |
77 | error_setg(errp, "Image not in COW format"); |
78 | ret = -EINVAL; | |
ea2384d3 FB |
79 | goto fail; |
80 | } | |
81 | ||
16d2fc00 LZH |
82 | if (be32_to_cpu(cow_header.version) != COW_VERSION) { |
83 | char version[64]; | |
84 | snprintf(version, sizeof(version), | |
521b2b5d | 85 | "COW version %" PRIu32, cow_header.version); |
f8d924e4 | 86 | error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, |
16d2fc00 LZH |
87 | bs->device_name, "cow", version); |
88 | ret = -ENOTSUP; | |
ea2384d3 FB |
89 | goto fail; |
90 | } | |
3b46e624 | 91 | |
ea2384d3 FB |
92 | /* cow image found */ |
93 | size = be64_to_cpu(cow_header.size); | |
94 | bs->total_sectors = size / 512; | |
95 | ||
5fafdf24 | 96 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), |
ea2384d3 | 97 | cow_header.backing_file); |
3b46e624 | 98 | |
893a9cb4 CH |
99 | bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header); |
100 | s->cow_sectors_offset = (bitmap_size + 511) & ~511; | |
848c66e8 | 101 | qemu_co_mutex_init(&s->lock); |
ea2384d3 FB |
102 | return 0; |
103 | fail: | |
16d2fc00 | 104 | return ret; |
ea2384d3 FB |
105 | } |
106 | ||
14b98fda | 107 | static inline void cow_set_bits(uint8_t *bitmap, int start, int64_t nb_sectors) |
ea2384d3 | 108 | { |
14b98fda CS |
109 | int64_t bitnum = start, last = start + nb_sectors; |
110 | while (bitnum < last) { | |
111 | if ((bitnum & 7) == 0 && bitnum + 8 <= last) { | |
112 | bitmap[bitnum / 8] = 0xFF; | |
113 | bitnum += 8; | |
114 | continue; | |
26ae9804 | 115 | } |
14b98fda CS |
116 | bitmap[bitnum/8] |= (1 << (bitnum % 8)); |
117 | bitnum++; | |
26ae9804 | 118 | } |
ea2384d3 FB |
119 | } |
120 | ||
276cbc7f PB |
121 | #define BITS_PER_BITMAP_SECTOR (512 * 8) |
122 | ||
123 | /* Cannot use bitmap.c on big-endian machines. */ | |
124 | static int cow_test_bit(int64_t bitnum, const uint8_t *bitmap) | |
ea2384d3 | 125 | { |
276cbc7f PB |
126 | return (bitmap[bitnum / 8] & (1 << (bitnum & 7))) != 0; |
127 | } | |
ea2384d3 | 128 | |
276cbc7f PB |
129 | static int cow_find_streak(const uint8_t *bitmap, int value, int start, int nb_sectors) |
130 | { | |
131 | int streak_value = value ? 0xFF : 0; | |
132 | int last = MIN(start + nb_sectors, BITS_PER_BITMAP_SECTOR); | |
133 | int bitnum = start; | |
134 | while (bitnum < last) { | |
135 | if ((bitnum & 7) == 0 && bitmap[bitnum / 8] == streak_value) { | |
136 | bitnum += 8; | |
137 | continue; | |
138 | } | |
139 | if (cow_test_bit(bitnum, bitmap) == value) { | |
140 | bitnum++; | |
141 | continue; | |
142 | } | |
143 | break; | |
893a9cb4 | 144 | } |
276cbc7f | 145 | return MIN(bitnum, last) - start; |
893a9cb4 | 146 | } |
ea2384d3 FB |
147 | |
148 | /* Return true if first block has been changed (ie. current version is | |
149 | * in COW file). Set the number of continuous blocks for which that | |
150 | * is true. */ | |
81145834 SH |
151 | static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs, |
152 | int64_t sector_num, int nb_sectors, int *num_same) | |
ea2384d3 | 153 | { |
276cbc7f PB |
154 | int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8; |
155 | uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE; | |
091b1108 CS |
156 | bool first = true; |
157 | int changed = 0, same = 0; | |
ea2384d3 | 158 | |
091b1108 CS |
159 | do { |
160 | int ret; | |
161 | uint8_t bitmap[BDRV_SECTOR_SIZE]; | |
162 | ||
163 | bitnum &= BITS_PER_BITMAP_SECTOR - 1; | |
164 | int sector_bits = MIN(nb_sectors, BITS_PER_BITMAP_SECTOR - bitnum); | |
165 | ||
166 | ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)); | |
167 | if (ret < 0) { | |
168 | return ret; | |
169 | } | |
170 | ||
171 | if (first) { | |
172 | changed = cow_test_bit(bitnum, bitmap); | |
173 | first = false; | |
174 | } | |
175 | ||
176 | same += cow_find_streak(bitmap, changed, bitnum, nb_sectors); | |
177 | ||
178 | bitnum += sector_bits; | |
179 | nb_sectors -= sector_bits; | |
180 | offset += BDRV_SECTOR_SIZE; | |
181 | } while (nb_sectors); | |
ea2384d3 | 182 | |
091b1108 | 183 | *num_same = same; |
ea2384d3 FB |
184 | return changed; |
185 | } | |
186 | ||
b6b8a333 PB |
187 | static int64_t coroutine_fn cow_co_get_block_status(BlockDriverState *bs, |
188 | int64_t sector_num, int nb_sectors, int *num_same) | |
189 | { | |
4bc74be9 PB |
190 | BDRVCowState *s = bs->opaque; |
191 | int ret = cow_co_is_allocated(bs, sector_num, nb_sectors, num_same); | |
192 | int64_t offset = s->cow_sectors_offset + (sector_num << BDRV_SECTOR_BITS); | |
193 | if (ret < 0) { | |
194 | return ret; | |
195 | } | |
196 | return (ret ? BDRV_BLOCK_DATA : 0) | offset | BDRV_BLOCK_OFFSET_VALID; | |
b6b8a333 PB |
197 | } |
198 | ||
893a9cb4 CH |
199 | static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num, |
200 | int nb_sectors) | |
ea2384d3 | 201 | { |
14b98fda CS |
202 | int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8; |
203 | uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE; | |
26ae9804 | 204 | bool first = true; |
14b98fda CS |
205 | int sector_bits; |
206 | ||
207 | for ( ; nb_sectors; | |
208 | bitnum += sector_bits, | |
209 | nb_sectors -= sector_bits, | |
210 | offset += BDRV_SECTOR_SIZE) { | |
211 | int ret, set; | |
212 | uint8_t bitmap[BDRV_SECTOR_SIZE]; | |
213 | ||
214 | bitnum &= BITS_PER_BITMAP_SECTOR - 1; | |
215 | sector_bits = MIN(nb_sectors, BITS_PER_BITMAP_SECTOR - bitnum); | |
216 | ||
217 | ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)); | |
218 | if (ret < 0) { | |
219 | return ret; | |
220 | } | |
221 | ||
222 | /* Skip over any already set bits */ | |
223 | set = cow_find_streak(bitmap, 1, bitnum, sector_bits); | |
224 | bitnum += set; | |
225 | sector_bits -= set; | |
226 | nb_sectors -= set; | |
227 | if (!sector_bits) { | |
228 | continue; | |
229 | } | |
230 | ||
231 | if (first) { | |
232 | ret = bdrv_flush(bs->file); | |
233 | if (ret < 0) { | |
234 | return ret; | |
235 | } | |
236 | first = false; | |
237 | } | |
238 | ||
239 | cow_set_bits(bitmap, bitnum, sector_bits); | |
893a9cb4 | 240 | |
14b98fda CS |
241 | ret = bdrv_pwrite(bs->file, offset, &bitmap, sizeof(bitmap)); |
242 | if (ret < 0) { | |
243 | return ret; | |
893a9cb4 CH |
244 | } |
245 | } | |
246 | ||
14b98fda | 247 | return 0; |
ea2384d3 FB |
248 | } |
249 | ||
e94d1387 SH |
250 | static int coroutine_fn cow_read(BlockDriverState *bs, int64_t sector_num, |
251 | uint8_t *buf, int nb_sectors) | |
ea2384d3 FB |
252 | { |
253 | BDRVCowState *s = bs->opaque; | |
254 | int ret, n; | |
3b46e624 | 255 | |
ea2384d3 | 256 | while (nb_sectors > 0) { |
d663640c PB |
257 | ret = cow_co_is_allocated(bs, sector_num, nb_sectors, &n); |
258 | if (ret < 0) { | |
259 | return ret; | |
260 | } | |
261 | if (ret) { | |
2063392a CH |
262 | ret = bdrv_pread(bs->file, |
263 | s->cow_sectors_offset + sector_num * 512, | |
264 | buf, n * 512); | |
16d2fc00 LZH |
265 | if (ret < 0) { |
266 | return ret; | |
267 | } | |
ea2384d3 | 268 | } else { |
83f64091 FB |
269 | if (bs->backing_hd) { |
270 | /* read from the base image */ | |
271 | ret = bdrv_read(bs->backing_hd, sector_num, buf, n); | |
16d2fc00 LZH |
272 | if (ret < 0) { |
273 | return ret; | |
274 | } | |
83f64091 | 275 | } else { |
16d2fc00 LZH |
276 | memset(buf, 0, n * 512); |
277 | } | |
83f64091 | 278 | } |
ea2384d3 FB |
279 | nb_sectors -= n; |
280 | sector_num += n; | |
281 | buf += n * 512; | |
282 | } | |
283 | return 0; | |
284 | } | |
285 | ||
2914caa0 PB |
286 | static coroutine_fn int cow_co_read(BlockDriverState *bs, int64_t sector_num, |
287 | uint8_t *buf, int nb_sectors) | |
288 | { | |
289 | int ret; | |
290 | BDRVCowState *s = bs->opaque; | |
291 | qemu_co_mutex_lock(&s->lock); | |
292 | ret = cow_read(bs, sector_num, buf, nb_sectors); | |
293 | qemu_co_mutex_unlock(&s->lock); | |
294 | return ret; | |
295 | } | |
296 | ||
5fafdf24 | 297 | static int cow_write(BlockDriverState *bs, int64_t sector_num, |
ea2384d3 FB |
298 | const uint8_t *buf, int nb_sectors) |
299 | { | |
300 | BDRVCowState *s = bs->opaque; | |
893a9cb4 | 301 | int ret; |
3b46e624 | 302 | |
2063392a CH |
303 | ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512, |
304 | buf, nb_sectors * 512); | |
16d2fc00 LZH |
305 | if (ret < 0) { |
306 | return ret; | |
307 | } | |
893a9cb4 CH |
308 | |
309 | return cow_update_bitmap(bs, sector_num, nb_sectors); | |
ea2384d3 FB |
310 | } |
311 | ||
e183ef75 PB |
312 | static coroutine_fn int cow_co_write(BlockDriverState *bs, int64_t sector_num, |
313 | const uint8_t *buf, int nb_sectors) | |
314 | { | |
315 | int ret; | |
316 | BDRVCowState *s = bs->opaque; | |
317 | qemu_co_mutex_lock(&s->lock); | |
318 | ret = cow_write(bs, sector_num, buf, nb_sectors); | |
319 | qemu_co_mutex_unlock(&s->lock); | |
320 | return ret; | |
321 | } | |
322 | ||
e2731add | 323 | static void cow_close(BlockDriverState *bs) |
ea2384d3 | 324 | { |
ea2384d3 FB |
325 | } |
326 | ||
d5124c00 HR |
327 | static int cow_create(const char *filename, QEMUOptionParameter *options, |
328 | Error **errp) | |
ea2384d3 | 329 | { |
ea2384d3 FB |
330 | struct cow_header_v2 cow_header; |
331 | struct stat st; | |
0e7e1989 KW |
332 | int64_t image_sectors = 0; |
333 | const char *image_filename = NULL; | |
34b5d2c6 | 334 | Error *local_err = NULL; |
31f38120 | 335 | int ret; |
3535a9c6 | 336 | BlockDriverState *cow_bs; |
0e7e1989 KW |
337 | |
338 | /* Read out options */ | |
339 | while (options && options->name) { | |
340 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
341 | image_sectors = options->value.n / 512; | |
342 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { | |
343 | image_filename = options->value.s; | |
344 | } | |
345 | options++; | |
346 | } | |
ea2384d3 | 347 | |
cc84d90f | 348 | ret = bdrv_create_file(filename, options, &local_err); |
3535a9c6 | 349 | if (ret < 0) { |
f8d924e4 | 350 | error_propagate(errp, local_err); |
3535a9c6 LZH |
351 | return ret; |
352 | } | |
353 | ||
2e40134b HR |
354 | cow_bs = NULL; |
355 | ret = bdrv_open(&cow_bs, filename, NULL, NULL, | |
356 | BDRV_O_RDWR | BDRV_O_PROTOCOL, NULL, &local_err); | |
3535a9c6 | 357 | if (ret < 0) { |
f8d924e4 | 358 | error_propagate(errp, local_err); |
3535a9c6 LZH |
359 | return ret; |
360 | } | |
361 | ||
ea2384d3 FB |
362 | memset(&cow_header, 0, sizeof(cow_header)); |
363 | cow_header.magic = cpu_to_be32(COW_MAGIC); | |
364 | cow_header.version = cpu_to_be32(COW_VERSION); | |
365 | if (image_filename) { | |
83f64091 FB |
366 | /* Note: if no file, we put a dummy mtime */ |
367 | cow_header.mtime = cpu_to_be32(0); | |
368 | ||
3535a9c6 | 369 | if (stat(image_filename, &st) != 0) { |
83f64091 | 370 | goto mtime_fail; |
ea2384d3 | 371 | } |
ea2384d3 | 372 | cow_header.mtime = cpu_to_be32(st.st_mtime); |
83f64091 FB |
373 | mtime_fail: |
374 | pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file), | |
375 | image_filename); | |
ea2384d3 FB |
376 | } |
377 | cow_header.sectorsize = cpu_to_be32(512); | |
378 | cow_header.size = cpu_to_be64(image_sectors * 512); | |
3535a9c6 | 379 | ret = bdrv_pwrite(cow_bs, 0, &cow_header, sizeof(cow_header)); |
16d2fc00 | 380 | if (ret < 0) { |
31f38120 KS |
381 | goto exit; |
382 | } | |
383 | ||
ea2384d3 | 384 | /* resize to include at least all the bitmap */ |
3535a9c6 LZH |
385 | ret = bdrv_truncate(cow_bs, |
386 | sizeof(cow_header) + ((image_sectors + 7) >> 3)); | |
16d2fc00 | 387 | if (ret < 0) { |
31f38120 KS |
388 | goto exit; |
389 | } | |
390 | ||
31f38120 | 391 | exit: |
4f6fd349 | 392 | bdrv_unref(cow_bs); |
31f38120 | 393 | return ret; |
ea2384d3 FB |
394 | } |
395 | ||
0e7e1989 | 396 | static QEMUOptionParameter cow_create_options[] = { |
db08adf5 KW |
397 | { |
398 | .name = BLOCK_OPT_SIZE, | |
399 | .type = OPT_SIZE, | |
400 | .help = "Virtual disk size" | |
401 | }, | |
402 | { | |
403 | .name = BLOCK_OPT_BACKING_FILE, | |
404 | .type = OPT_STRING, | |
405 | .help = "File name of a base image" | |
406 | }, | |
0e7e1989 KW |
407 | { NULL } |
408 | }; | |
409 | ||
5efa9d5a | 410 | static BlockDriver bdrv_cow = { |
c68b89ac KW |
411 | .format_name = "cow", |
412 | .instance_size = sizeof(BDRVCowState), | |
413 | ||
414 | .bdrv_probe = cow_probe, | |
415 | .bdrv_open = cow_open, | |
416 | .bdrv_close = cow_close, | |
417 | .bdrv_create = cow_create, | |
3ac21627 | 418 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
c68b89ac KW |
419 | |
420 | .bdrv_read = cow_co_read, | |
421 | .bdrv_write = cow_co_write, | |
b6b8a333 | 422 | .bdrv_co_get_block_status = cow_co_get_block_status, |
0e7e1989 KW |
423 | |
424 | .create_options = cow_create_options, | |
ea2384d3 | 425 | }; |
5efa9d5a AL |
426 | |
427 | static void bdrv_cow_init(void) | |
428 | { | |
429 | bdrv_register(&bdrv_cow); | |
430 | } | |
431 | ||
432 | block_init(bdrv_cow_init); |