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