blob: bb5927c6aa123469a5842cde3cdbdbc2aed7e8d9 [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"
bellardea2384d2004-08-01 21:59:26 +000025#include "block_int.h"
Anthony Liguori5efa9d52009-05-09 17:03:42 -050026#include "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
Christoph Hellwig20633922010-06-07 12:06:47 +020061static int cow_open(BlockDriverState *bs, int flags)
bellardea2384d2004-08-01 21:59:26 +000062{
63 BDRVCowState *s = bs->opaque;
bellardea2384d2004-08-01 21:59:26 +000064 struct cow_header_v2 cow_header;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +020065 int bitmap_size;
bellardea2384d2004-08-01 21:59:26 +000066 int64_t size;
Li Zhi Hui16d2fc02011-12-12 13:54:33 +080067 int ret;
bellardea2384d2004-08-01 21:59:26 +000068
bellardea2384d2004-08-01 21:59:26 +000069 /* see if it is a cow image */
Li Zhi Hui16d2fc02011-12-12 13:54:33 +080070 ret = bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header));
71 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +000072 goto fail;
73 }
74
Li Zhi Hui16d2fc02011-12-12 13:54:33 +080075 if (be32_to_cpu(cow_header.magic) != COW_MAGIC) {
76 ret = -EINVAL;
77 goto fail;
78 }
79
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;
bellardea2384d2004-08-01 21:59:26 +000087 goto fail;
88 }
ths3b46e622007-09-17 08:09:54 +000089
bellardea2384d2004-08-01 21:59:26 +000090 /* cow image found */
91 size = be64_to_cpu(cow_header.size);
92 bs->total_sectors = size / 512;
93
ths5fafdf22007-09-16 21:08:06 +000094 pstrcpy(bs->backing_file, sizeof(bs->backing_file),
bellardea2384d2004-08-01 21:59:26 +000095 cow_header.backing_file);
ths3b46e622007-09-17 08:09:54 +000096
Christoph Hellwig893a9cb2010-06-07 12:06:37 +020097 bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
98 s->cow_sectors_offset = (bitmap_size + 511) & ~511;
Paolo Bonzini848c66e2011-10-20 13:16:21 +020099 qemu_co_mutex_init(&s->lock);
bellardea2384d2004-08-01 21:59:26 +0000100 return 0;
101 fail:
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800102 return ret;
bellardea2384d2004-08-01 21:59:26 +0000103}
104
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200105/*
Dong Xu Wang9b2260c2011-11-22 18:06:25 +0800106 * XXX(hch): right now these functions are extremely ineffcient.
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200107 * We should just read the whole bitmap we'll need in one go instead.
108 */
109static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum)
bellardea2384d2004-08-01 21:59:26 +0000110{
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200111 uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
112 uint8_t bitmap;
Kevin Wolfb0ad5a42010-06-18 16:31:14 +0200113 int ret;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200114
Kevin Wolfb0ad5a42010-06-18 16:31:14 +0200115 ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
116 if (ret < 0) {
117 return ret;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200118 }
119
120 bitmap |= (1 << (bitnum % 8));
121
Kevin Wolfb0ad5a42010-06-18 16:31:14 +0200122 ret = bdrv_pwrite_sync(bs->file, offset, &bitmap, sizeof(bitmap));
123 if (ret < 0) {
124 return ret;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200125 }
126 return 0;
bellardea2384d2004-08-01 21:59:26 +0000127}
128
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200129static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum)
bellardea2384d2004-08-01 21:59:26 +0000130{
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200131 uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
132 uint8_t bitmap;
Kevin Wolfb0ad5a42010-06-18 16:31:14 +0200133 int ret;
bellardea2384d2004-08-01 21:59:26 +0000134
Kevin Wolfb0ad5a42010-06-18 16:31:14 +0200135 ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
136 if (ret < 0) {
137 return ret;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200138 }
139
140 return !!(bitmap & (1 << (bitnum % 8)));
141}
bellardea2384d2004-08-01 21:59:26 +0000142
143/* Return true if first block has been changed (ie. current version is
144 * in COW file). Set the number of continuous blocks for which that
145 * is true. */
Stefan Hajnoczi81145832011-11-14 12:44:24 +0000146static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
147 int64_t sector_num, int nb_sectors, int *num_same)
bellardea2384d2004-08-01 21:59:26 +0000148{
149 int changed;
150
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200151 if (nb_sectors == 0) {
bellardea2384d2004-08-01 21:59:26 +0000152 *num_same = nb_sectors;
153 return 0;
154 }
155
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200156 changed = is_bit_set(bs, sector_num);
157 if (changed < 0) {
158 return 0; /* XXX: how to return I/O errors? */
159 }
160
bellardea2384d2004-08-01 21:59:26 +0000161 for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) {
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200162 if (is_bit_set(bs, sector_num + *num_same) != changed)
bellardea2384d2004-08-01 21:59:26 +0000163 break;
164 }
165
166 return changed;
167}
168
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200169static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,
170 int nb_sectors)
bellardea2384d2004-08-01 21:59:26 +0000171{
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200172 int error = 0;
173 int i;
174
175 for (i = 0; i < nb_sectors; i++) {
176 error = cow_set_bit(bs, sector_num + i);
177 if (error) {
178 break;
179 }
180 }
181
182 return error;
bellardea2384d2004-08-01 21:59:26 +0000183}
184
Stefan Hajnoczie94d1382011-11-23 15:00:04 +0000185static int coroutine_fn cow_read(BlockDriverState *bs, int64_t sector_num,
186 uint8_t *buf, int nb_sectors)
bellardea2384d2004-08-01 21:59:26 +0000187{
188 BDRVCowState *s = bs->opaque;
189 int ret, n;
ths3b46e622007-09-17 08:09:54 +0000190
bellardea2384d2004-08-01 21:59:26 +0000191 while (nb_sectors > 0) {
Stefan Hajnoczie94d1382011-11-23 15:00:04 +0000192 if (bdrv_co_is_allocated(bs, sector_num, nb_sectors, &n)) {
Christoph Hellwig20633922010-06-07 12:06:47 +0200193 ret = bdrv_pread(bs->file,
194 s->cow_sectors_offset + sector_num * 512,
195 buf, n * 512);
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800196 if (ret < 0) {
197 return ret;
198 }
bellardea2384d2004-08-01 21:59:26 +0000199 } else {
bellard83f64092006-08-01 16:21:11 +0000200 if (bs->backing_hd) {
201 /* read from the base image */
202 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800203 if (ret < 0) {
204 return ret;
205 }
bellard83f64092006-08-01 16:21:11 +0000206 } else {
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800207 memset(buf, 0, n * 512);
208 }
bellard83f64092006-08-01 16:21:11 +0000209 }
bellardea2384d2004-08-01 21:59:26 +0000210 nb_sectors -= n;
211 sector_num += n;
212 buf += n * 512;
213 }
214 return 0;
215}
216
Paolo Bonzini2914caa2011-10-20 13:16:22 +0200217static coroutine_fn int cow_co_read(BlockDriverState *bs, int64_t sector_num,
218 uint8_t *buf, int nb_sectors)
219{
220 int ret;
221 BDRVCowState *s = bs->opaque;
222 qemu_co_mutex_lock(&s->lock);
223 ret = cow_read(bs, sector_num, buf, nb_sectors);
224 qemu_co_mutex_unlock(&s->lock);
225 return ret;
226}
227
ths5fafdf22007-09-16 21:08:06 +0000228static int cow_write(BlockDriverState *bs, int64_t sector_num,
bellardea2384d2004-08-01 21:59:26 +0000229 const uint8_t *buf, int nb_sectors)
230{
231 BDRVCowState *s = bs->opaque;
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200232 int ret;
ths3b46e622007-09-17 08:09:54 +0000233
Christoph Hellwig20633922010-06-07 12:06:47 +0200234 ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512,
235 buf, nb_sectors * 512);
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800236 if (ret < 0) {
237 return ret;
238 }
Christoph Hellwig893a9cb2010-06-07 12:06:37 +0200239
240 return cow_update_bitmap(bs, sector_num, nb_sectors);
bellardea2384d2004-08-01 21:59:26 +0000241}
242
Paolo Bonzinie183ef72011-10-20 13:16:23 +0200243static coroutine_fn int cow_co_write(BlockDriverState *bs, int64_t sector_num,
244 const uint8_t *buf, int nb_sectors)
245{
246 int ret;
247 BDRVCowState *s = bs->opaque;
248 qemu_co_mutex_lock(&s->lock);
249 ret = cow_write(bs, sector_num, buf, nb_sectors);
250 qemu_co_mutex_unlock(&s->lock);
251 return ret;
252}
253
bellarde2731ad2004-09-18 19:32:11 +0000254static void cow_close(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +0000255{
bellardea2384d2004-08-01 21:59:26 +0000256}
257
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200258static int cow_create(const char *filename, QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000259{
bellardea2384d2004-08-01 21:59:26 +0000260 struct cow_header_v2 cow_header;
261 struct stat st;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200262 int64_t image_sectors = 0;
263 const char *image_filename = NULL;
Kirill A. Shutemov31f38122010-01-20 00:56:11 +0100264 int ret;
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800265 BlockDriverState *cow_bs;
bellardea2384d2004-08-01 21:59:26 +0000266
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200267 /* Read out options */
268 while (options && options->name) {
269 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
270 image_sectors = options->value.n / 512;
271 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
272 image_filename = options->value.s;
273 }
274 options++;
275 }
bellardea2384d2004-08-01 21:59:26 +0000276
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800277 ret = bdrv_create_file(filename, options);
278 if (ret < 0) {
279 return ret;
280 }
281
282 ret = bdrv_file_open(&cow_bs, filename, BDRV_O_RDWR);
283 if (ret < 0) {
284 return ret;
285 }
286
bellardea2384d2004-08-01 21:59:26 +0000287 memset(&cow_header, 0, sizeof(cow_header));
288 cow_header.magic = cpu_to_be32(COW_MAGIC);
289 cow_header.version = cpu_to_be32(COW_VERSION);
290 if (image_filename) {
bellard83f64092006-08-01 16:21:11 +0000291 /* Note: if no file, we put a dummy mtime */
292 cow_header.mtime = cpu_to_be32(0);
293
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800294 if (stat(image_filename, &st) != 0) {
bellard83f64092006-08-01 16:21:11 +0000295 goto mtime_fail;
bellardea2384d2004-08-01 21:59:26 +0000296 }
bellardea2384d2004-08-01 21:59:26 +0000297 cow_header.mtime = cpu_to_be32(st.st_mtime);
bellard83f64092006-08-01 16:21:11 +0000298 mtime_fail:
299 pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file),
300 image_filename);
bellardea2384d2004-08-01 21:59:26 +0000301 }
302 cow_header.sectorsize = cpu_to_be32(512);
303 cow_header.size = cpu_to_be64(image_sectors * 512);
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800304 ret = bdrv_pwrite(cow_bs, 0, &cow_header, sizeof(cow_header));
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800305 if (ret < 0) {
Kirill A. Shutemov31f38122010-01-20 00:56:11 +0100306 goto exit;
307 }
308
bellardea2384d2004-08-01 21:59:26 +0000309 /* resize to include at least all the bitmap */
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800310 ret = bdrv_truncate(cow_bs,
311 sizeof(cow_header) + ((image_sectors + 7) >> 3));
Li Zhi Hui16d2fc02011-12-12 13:54:33 +0800312 if (ret < 0) {
Kirill A. Shutemov31f38122010-01-20 00:56:11 +0100313 goto exit;
314 }
315
Kirill A. Shutemov31f38122010-01-20 00:56:11 +0100316exit:
Li Zhi Hui3535a9c2011-11-08 14:21:13 +0800317 bdrv_delete(cow_bs);
Kirill A. Shutemov31f38122010-01-20 00:56:11 +0100318 return ret;
bellardea2384d2004-08-01 21:59:26 +0000319}
320
Paolo Bonzini8b94ff82011-10-20 13:16:24 +0200321static coroutine_fn int cow_co_flush(BlockDriverState *bs)
pbrook7a6cba62006-06-04 11:39:07 +0000322{
Paolo Bonzini8b94ff82011-10-20 13:16:24 +0200323 return bdrv_co_flush(bs->file);
pbrook7a6cba62006-06-04 11:39:07 +0000324}
325
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200326static QEMUOptionParameter cow_create_options[] = {
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200327 {
328 .name = BLOCK_OPT_SIZE,
329 .type = OPT_SIZE,
330 .help = "Virtual disk size"
331 },
332 {
333 .name = BLOCK_OPT_BACKING_FILE,
334 .type = OPT_STRING,
335 .help = "File name of a base image"
336 },
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200337 { NULL }
338};
339
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500340static BlockDriver bdrv_cow = {
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100341 .format_name = "cow",
342 .instance_size = sizeof(BDRVCowState),
343
344 .bdrv_probe = cow_probe,
345 .bdrv_open = cow_open,
346 .bdrv_close = cow_close,
347 .bdrv_create = cow_create,
348
349 .bdrv_read = cow_co_read,
350 .bdrv_write = cow_co_write,
351 .bdrv_co_flush_to_disk = cow_co_flush,
Stefan Hajnoczi81145832011-11-14 12:44:24 +0000352 .bdrv_co_is_allocated = cow_co_is_allocated,
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200353
354 .create_options = cow_create_options,
bellardea2384d2004-08-01 21:59:26 +0000355};
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500356
357static void bdrv_cow_init(void)
358{
359 bdrv_register(&bdrv_cow);
360}
361
362block_init(bdrv_cow_init);