blob: 7fc0b12163ac1d73dc0b16e5793262e431b88c07 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
2 * Block driver for the COW format
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardea2384d2004-08-01 21:59:26 +00004 * Copyright (c) 2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * 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 */
pbrookfaf07962007-11-11 02:51:17 +000024#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010025#include "block/block_int.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/module.h"
bellardea2384d2004-08-01 21:59:26 +000027
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
35struct 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
44typedef struct BDRVCowState {
Paolo Bonzini848c66e2011-10-20 13:16:21 +020045 CoMutex lock;
bellardea2384d2004-08-01 21:59:26 +000046 int64_t cow_sectors_offset;
47} BDRVCowState;
48
49static 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
bellard712e7872005-04-28 21:09:32 +000053 if (buf_size >= sizeof(struct cow_header_v2) &&
54 be32_to_cpu(cow_header->magic) == COW_MAGIC &&
ths5fafdf22007-09-16 21:08:06 +000055 be32_to_cpu(cow_header->version) == COW_VERSION)
bellardea2384d2004-08-01 21:59:26 +000056 return 100;
57 else
58 return 0;
59}
60
Max Reitz015a1032013-09-05 14:22:29 +020061static int cow_open(BlockDriverState *bs, QDict *options, int flags,
62 Error **errp)
bellardea2384d2004-08-01 21:59:26 +000063{
64 BDRVCowState *s = bs->opaque;
bellardea2384d2004-08-01 21:59:26 +000065 struct cow_header_v2 cow_header;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +020066 int bitmap_size;
bellardea2384d2004-08-01 21:59:26 +000067 int64_t size;
Li Zhi Hui16d2fc02011-12-12 13:54:33 +080068 int ret;
bellardea2384d2004-08-01 21:59:26 +000069
bellardea2384d2004-08-01 21:59:26 +000070 /* see if it is a cow image */
Li Zhi Hui16d2fc02011-12-12 13:54:33 +080071 ret = bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header));
72 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +000073 goto fail;
74 }
75
Li Zhi Hui16d2fc02011-12-12 13:54:33 +080076 if (be32_to_cpu(cow_header.magic) != COW_MAGIC) {
Stefan Weil15bac0d2013-01-17 21:45:25 +010077 ret = -EMEDIUMTYPE;
Li Zhi Hui16d2fc02011-12-12 13:54:33 +080078 goto fail;
79 }
80
81 if (be32_to_cpu(cow_header.version) != COW_VERSION) {
82 char version[64];
83 snprintf(version, sizeof(version),
84 "COW version %d", cow_header.version);
85 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
86 bs->device_name, "cow", version);
87 ret = -ENOTSUP;
bellardea2384d2004-08-01 21:59:26 +000088 goto fail;
89 }
ths3b46e622007-09-17 08:09:54 +000090
bellardea2384d2004-08-01 21:59:26 +000091 /* cow image found */
92 size = be64_to_cpu(cow_header.size);
93 bs->total_sectors = size / 512;
94
ths5fafdf22007-09-16 21:08:06 +000095 pstrcpy(bs->backing_file, sizeof(bs->backing_file),
bellardea2384d2004-08-01 21:59:26 +000096 cow_header.backing_file);
ths3b46e622007-09-17 08:09:54 +000097
Christoph Hellwig893a9cb2010-06-07 12:06:37 +020098 bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
99 s->cow_sectors_offset = (bitmap_size + 511) & ~511;
Paolo Bonzini848c66e2011-10-20 13:16:21 +0200100 qemu_co_mutex_init(&s->lock);
bellardea2384d2004-08-01 21:59:26 +0000101 return 0;
102 fail:
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800103 return ret;
bellardea2384d2004-08-01 21:59:26 +0000104}
105
Charlie Shepherd14b98fd2013-11-15 19:47:01 +0100106static inline void cow_set_bits(uint8_t *bitmap, int start, int64_t nb_sectors)
bellardea2384d2004-08-01 21:59:26 +0000107{
Charlie Shepherd14b98fd2013-11-15 19:47:01 +0100108 int64_t bitnum = start, last = start + nb_sectors;
109 while (bitnum < last) {
110 if ((bitnum & 7) == 0 && bitnum + 8 <= last) {
111 bitmap[bitnum / 8] = 0xFF;
112 bitnum += 8;
113 continue;
Paolo Bonzini26ae9802013-09-04 19:00:19 +0200114 }
Charlie Shepherd14b98fd2013-11-15 19:47:01 +0100115 bitmap[bitnum/8] |= (1 << (bitnum % 8));
116 bitnum++;
Paolo Bonzini26ae9802013-09-04 19:00:19 +0200117 }
bellardea2384d2004-08-01 21:59:26 +0000118}
119
Paolo Bonzini276cbc72013-09-04 19:00:18 +0200120#define BITS_PER_BITMAP_SECTOR (512 * 8)
121
122/* Cannot use bitmap.c on big-endian machines. */
123static int cow_test_bit(int64_t bitnum, const uint8_t *bitmap)
bellardea2384d2004-08-01 21:59:26 +0000124{
Paolo Bonzini276cbc72013-09-04 19:00:18 +0200125 return (bitmap[bitnum / 8] & (1 << (bitnum & 7))) != 0;
126}
bellardea2384d2004-08-01 21:59:26 +0000127
Paolo Bonzini276cbc72013-09-04 19:00:18 +0200128static int cow_find_streak(const uint8_t *bitmap, int value, int start, int nb_sectors)
129{
130 int streak_value = value ? 0xFF : 0;
131 int last = MIN(start + nb_sectors, BITS_PER_BITMAP_SECTOR);
132 int bitnum = start;
133 while (bitnum < last) {
134 if ((bitnum & 7) == 0 && bitmap[bitnum / 8] == streak_value) {
135 bitnum += 8;
136 continue;
137 }
138 if (cow_test_bit(bitnum, bitmap) == value) {
139 bitnum++;
140 continue;
141 }
142 break;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200143 }
Paolo Bonzini276cbc72013-09-04 19:00:18 +0200144 return MIN(bitnum, last) - start;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200145}
bellardea2384d2004-08-01 21:59:26 +0000146
147/* Return true if first block has been changed (ie. current version is
148 * in COW file). Set the number of continuous blocks for which that
149 * is true. */
Stefan Hajnoczi81145832011-11-14 12:44:24 +0000150static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
151 int64_t sector_num, int nb_sectors, int *num_same)
bellardea2384d2004-08-01 21:59:26 +0000152{
Paolo Bonzini276cbc72013-09-04 19:00:18 +0200153 int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8;
154 uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE;
Charlie Shepherd091b1102013-11-15 19:47:02 +0100155 bool first = true;
156 int changed = 0, same = 0;
bellardea2384d2004-08-01 21:59:26 +0000157
Charlie Shepherd091b1102013-11-15 19:47:02 +0100158 do {
159 int ret;
160 uint8_t bitmap[BDRV_SECTOR_SIZE];
bellardea2384d2004-08-01 21:59:26 +0000161
Charlie Shepherd091b1102013-11-15 19:47:02 +0100162 bitnum &= BITS_PER_BITMAP_SECTOR - 1;
163 int sector_bits = MIN(nb_sectors, BITS_PER_BITMAP_SECTOR - bitnum);
164
165 ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
166 if (ret < 0) {
167 return ret;
168 }
169
170 if (first) {
171 changed = cow_test_bit(bitnum, bitmap);
172 first = false;
173 }
174
175 same += cow_find_streak(bitmap, changed, bitnum, nb_sectors);
176
177 bitnum += sector_bits;
178 nb_sectors -= sector_bits;
179 offset += BDRV_SECTOR_SIZE;
180 } while (nb_sectors);
181
182 *num_same = same;
bellardea2384d2004-08-01 21:59:26 +0000183 return changed;
184}
185
Paolo Bonzinib6b8a332013-09-04 19:00:28 +0200186static int64_t coroutine_fn cow_co_get_block_status(BlockDriverState *bs,
187 int64_t sector_num, int nb_sectors, int *num_same)
188{
Paolo Bonzini4bc74be2013-09-04 19:00:30 +0200189 BDRVCowState *s = bs->opaque;
190 int ret = cow_co_is_allocated(bs, sector_num, nb_sectors, num_same);
191 int64_t offset = s->cow_sectors_offset + (sector_num << BDRV_SECTOR_BITS);
192 if (ret < 0) {
193 return ret;
194 }
195 return (ret ? BDRV_BLOCK_DATA : 0) | offset | BDRV_BLOCK_OFFSET_VALID;
Paolo Bonzinib6b8a332013-09-04 19:00:28 +0200196}
197
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200198static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,
199 int nb_sectors)
bellardea2384d2004-08-01 21:59:26 +0000200{
Charlie Shepherd14b98fd2013-11-15 19:47:01 +0100201 int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8;
202 uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE;
Paolo Bonzini26ae9802013-09-04 19:00:19 +0200203 bool first = true;
Charlie Shepherd14b98fd2013-11-15 19:47:01 +0100204 int sector_bits;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200205
Charlie Shepherd14b98fd2013-11-15 19:47:01 +0100206 for ( ; nb_sectors;
207 bitnum += sector_bits,
208 nb_sectors -= sector_bits,
209 offset += BDRV_SECTOR_SIZE) {
210 int ret, set;
211 uint8_t bitmap[BDRV_SECTOR_SIZE];
212
213 bitnum &= BITS_PER_BITMAP_SECTOR - 1;
214 sector_bits = MIN(nb_sectors, BITS_PER_BITMAP_SECTOR - bitnum);
215
216 ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
217 if (ret < 0) {
218 return ret;
219 }
220
221 /* Skip over any already set bits */
222 set = cow_find_streak(bitmap, 1, bitnum, sector_bits);
223 bitnum += set;
224 sector_bits -= set;
225 nb_sectors -= set;
226 if (!sector_bits) {
227 continue;
228 }
229
230 if (first) {
231 ret = bdrv_flush(bs->file);
232 if (ret < 0) {
233 return ret;
234 }
235 first = false;
236 }
237
238 cow_set_bits(bitmap, bitnum, sector_bits);
239
240 ret = bdrv_pwrite(bs->file, offset, &bitmap, sizeof(bitmap));
241 if (ret < 0) {
242 return ret;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200243 }
244 }
245
Charlie Shepherd14b98fd2013-11-15 19:47:01 +0100246 return 0;
bellardea2384d2004-08-01 21:59:26 +0000247}
248
Stefan Hajnoczie94d1382011-11-23 15:00:04 +0000249static int coroutine_fn cow_read(BlockDriverState *bs, int64_t sector_num,
250 uint8_t *buf, int nb_sectors)
bellardea2384d2004-08-01 21:59:26 +0000251{
252 BDRVCowState *s = bs->opaque;
253 int ret, n;
ths3b46e622007-09-17 08:09:54 +0000254
bellardea2384d2004-08-01 21:59:26 +0000255 while (nb_sectors > 0) {
Paolo Bonzinid6636402013-09-04 19:00:25 +0200256 ret = cow_co_is_allocated(bs, sector_num, nb_sectors, &n);
257 if (ret < 0) {
258 return ret;
259 }
260 if (ret) {
Christoph Hellwig20633922010-06-07 12:06:47 +0200261 ret = bdrv_pread(bs->file,
262 s->cow_sectors_offset + sector_num * 512,
263 buf, n * 512);
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800264 if (ret < 0) {
265 return ret;
266 }
bellardea2384d2004-08-01 21:59:26 +0000267 } else {
bellard83f64092006-08-01 16:21:11 +0000268 if (bs->backing_hd) {
269 /* read from the base image */
270 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800271 if (ret < 0) {
272 return ret;
273 }
bellard83f64092006-08-01 16:21:11 +0000274 } else {
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800275 memset(buf, 0, n * 512);
276 }
bellard83f64092006-08-01 16:21:11 +0000277 }
bellardea2384d2004-08-01 21:59:26 +0000278 nb_sectors -= n;
279 sector_num += n;
280 buf += n * 512;
281 }
282 return 0;
283}
284
Paolo Bonzini2914caa2011-10-20 13:16:22 +0200285static coroutine_fn int cow_co_read(BlockDriverState *bs, int64_t sector_num,
286 uint8_t *buf, int nb_sectors)
287{
288 int ret;
289 BDRVCowState *s = bs->opaque;
290 qemu_co_mutex_lock(&s->lock);
291 ret = cow_read(bs, sector_num, buf, nb_sectors);
292 qemu_co_mutex_unlock(&s->lock);
293 return ret;
294}
295
ths5fafdf22007-09-16 21:08:06 +0000296static int cow_write(BlockDriverState *bs, int64_t sector_num,
bellardea2384d2004-08-01 21:59:26 +0000297 const uint8_t *buf, int nb_sectors)
298{
299 BDRVCowState *s = bs->opaque;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200300 int ret;
ths3b46e622007-09-17 08:09:54 +0000301
Christoph Hellwig20633922010-06-07 12:06:47 +0200302 ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512,
303 buf, nb_sectors * 512);
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800304 if (ret < 0) {
305 return ret;
306 }
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200307
308 return cow_update_bitmap(bs, sector_num, nb_sectors);
bellardea2384d2004-08-01 21:59:26 +0000309}
310
Paolo Bonzinie183ef72011-10-20 13:16:23 +0200311static coroutine_fn int cow_co_write(BlockDriverState *bs, int64_t sector_num,
312 const uint8_t *buf, int nb_sectors)
313{
314 int ret;
315 BDRVCowState *s = bs->opaque;
316 qemu_co_mutex_lock(&s->lock);
317 ret = cow_write(bs, sector_num, buf, nb_sectors);
318 qemu_co_mutex_unlock(&s->lock);
319 return ret;
320}
321
bellarde2731ad2004-09-18 19:32:11 +0000322static void cow_close(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +0000323{
bellardea2384d2004-08-01 21:59:26 +0000324}
325
Max Reitzd5124c02013-09-05 14:26:05 +0200326static int cow_create(const char *filename, QEMUOptionParameter *options,
327 Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000328{
bellardea2384d2004-08-01 21:59:26 +0000329 struct cow_header_v2 cow_header;
330 struct stat st;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200331 int64_t image_sectors = 0;
332 const char *image_filename = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200333 Error *local_err = NULL;
Kirill A. Shutemov31f38122010-01-20 00:56:11 +0100334 int ret;
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800335 BlockDriverState *cow_bs;
bellardea2384d2004-08-01 21:59:26 +0000336
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200337 /* Read out options */
338 while (options && options->name) {
339 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
340 image_sectors = options->value.n / 512;
341 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
342 image_filename = options->value.s;
343 }
344 options++;
345 }
bellardea2384d2004-08-01 21:59:26 +0000346
Max Reitzcc84d902013-09-06 17:14:26 +0200347 ret = bdrv_create_file(filename, options, &local_err);
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800348 if (ret < 0) {
Max Reitzcc84d902013-09-06 17:14:26 +0200349 qerror_report_err(local_err);
350 error_free(local_err);
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800351 return ret;
352 }
353
Max Reitz72daa722013-12-20 19:28:08 +0100354 ret = bdrv_file_open(&cow_bs, filename, NULL, NULL, BDRV_O_RDWR,
355 &local_err);
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800356 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200357 qerror_report_err(local_err);
358 error_free(local_err);
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800359 return ret;
360 }
361
bellardea2384d2004-08-01 21:59:26 +0000362 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) {
bellard83f64092006-08-01 16:21:11 +0000366 /* Note: if no file, we put a dummy mtime */
367 cow_header.mtime = cpu_to_be32(0);
368
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800369 if (stat(image_filename, &st) != 0) {
bellard83f64092006-08-01 16:21:11 +0000370 goto mtime_fail;
bellardea2384d2004-08-01 21:59:26 +0000371 }
bellardea2384d2004-08-01 21:59:26 +0000372 cow_header.mtime = cpu_to_be32(st.st_mtime);
bellard83f64092006-08-01 16:21:11 +0000373 mtime_fail:
374 pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file),
375 image_filename);
bellardea2384d2004-08-01 21:59:26 +0000376 }
377 cow_header.sectorsize = cpu_to_be32(512);
378 cow_header.size = cpu_to_be64(image_sectors * 512);
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800379 ret = bdrv_pwrite(cow_bs, 0, &cow_header, sizeof(cow_header));
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800380 if (ret < 0) {
Kirill A. Shutemov31f38122010-01-20 00:56:11 +0100381 goto exit;
382 }
383
bellardea2384d2004-08-01 21:59:26 +0000384 /* resize to include at least all the bitmap */
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800385 ret = bdrv_truncate(cow_bs,
386 sizeof(cow_header) + ((image_sectors + 7) >> 3));
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800387 if (ret < 0) {
Kirill A. Shutemov31f38122010-01-20 00:56:11 +0100388 goto exit;
389 }
390
Kirill A. Shutemov31f38122010-01-20 00:56:11 +0100391exit:
Fam Zheng4f6fd342013-08-23 09:14:47 +0800392 bdrv_unref(cow_bs);
Kirill A. Shutemov31f38122010-01-20 00:56:11 +0100393 return ret;
bellardea2384d2004-08-01 21:59:26 +0000394}
395
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200396static QEMUOptionParameter cow_create_options[] = {
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200397 {
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 },
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200407 { NULL }
408};
409
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500410static BlockDriver bdrv_cow = {
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100411 .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,
Peter Lieven3ac21622013-06-28 12:47:42 +0200418 .bdrv_has_zero_init = bdrv_has_zero_init_1,
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100419
420 .bdrv_read = cow_co_read,
421 .bdrv_write = cow_co_write,
Paolo Bonzinib6b8a332013-09-04 19:00:28 +0200422 .bdrv_co_get_block_status = cow_co_get_block_status,
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200423
424 .create_options = cow_create_options,
bellardea2384d2004-08-01 21:59:26 +0000425};
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500426
427static void bdrv_cow_init(void)
428{
429 bdrv_register(&bdrv_cow);
430}
431
432block_init(bdrv_cow_init);