gluster: Convert aio routines into coroutines
[qemu/rayw.git] / block / cow.c
blobdc15e46b6c2bf42a3b6f9f3ec10c9b8a5c7374f4
1 /*
2 * Block driver for the COW format
4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/module.h"
28 /**************************************************************/
29 /* COW block driver using file system holes */
31 /* user mode linux compatible COW file */
32 #define COW_MAGIC 0x4f4f4f4d /* MOOO */
33 #define COW_VERSION 2
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;
44 typedef struct BDRVCowState {
45 CoMutex lock;
46 int64_t cow_sectors_offset;
47 } BDRVCowState;
49 static int cow_probe(const uint8_t *buf, int buf_size, const char *filename)
51 const struct cow_header_v2 *cow_header = (const void *)buf;
53 if (buf_size >= sizeof(struct cow_header_v2) &&
54 be32_to_cpu(cow_header->magic) == COW_MAGIC &&
55 be32_to_cpu(cow_header->version) == COW_VERSION)
56 return 100;
57 else
58 return 0;
61 static int cow_open(BlockDriverState *bs, QDict *options, int flags,
62 Error **errp)
64 BDRVCowState *s = bs->opaque;
65 struct cow_header_v2 cow_header;
66 int bitmap_size;
67 int64_t size;
68 int ret;
70 /* see if it is a cow image */
71 ret = bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header));
72 if (ret < 0) {
73 goto fail;
76 if (be32_to_cpu(cow_header.magic) != COW_MAGIC) {
77 ret = -EMEDIUMTYPE;
78 goto fail;
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;
88 goto fail;
91 /* cow image found */
92 size = be64_to_cpu(cow_header.size);
93 bs->total_sectors = size / 512;
95 pstrcpy(bs->backing_file, sizeof(bs->backing_file),
96 cow_header.backing_file);
98 bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
99 s->cow_sectors_offset = (bitmap_size + 511) & ~511;
100 qemu_co_mutex_init(&s->lock);
101 return 0;
102 fail:
103 return ret;
106 static inline void cow_set_bits(uint8_t *bitmap, int start, int64_t nb_sectors)
108 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;
115 bitmap[bitnum/8] |= (1 << (bitnum % 8));
116 bitnum++;
120 #define BITS_PER_BITMAP_SECTOR (512 * 8)
122 /* Cannot use bitmap.c on big-endian machines. */
123 static int cow_test_bit(int64_t bitnum, const uint8_t *bitmap)
125 return (bitmap[bitnum / 8] & (1 << (bitnum & 7))) != 0;
128 static int cow_find_streak(const uint8_t *bitmap, int value, int start, int nb_sectors)
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;
138 if (cow_test_bit(bitnum, bitmap) == value) {
139 bitnum++;
140 continue;
142 break;
144 return MIN(bitnum, last) - start;
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. */
150 static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
151 int64_t sector_num, int nb_sectors, int *num_same)
153 int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8;
154 uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE;
155 bool first = true;
156 int changed = 0, same = 0;
158 do {
159 int ret;
160 uint8_t bitmap[BDRV_SECTOR_SIZE];
162 bitnum &= BITS_PER_BITMAP_SECTOR - 1;
163 int sector_bits = MIN(nb_sectors, BITS_PER_BITMAP_SECTOR - bitnum);
165 ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
166 if (ret < 0) {
167 return ret;
170 if (first) {
171 changed = cow_test_bit(bitnum, bitmap);
172 first = false;
175 same += cow_find_streak(bitmap, changed, bitnum, nb_sectors);
177 bitnum += sector_bits;
178 nb_sectors -= sector_bits;
179 offset += BDRV_SECTOR_SIZE;
180 } while (nb_sectors);
182 *num_same = same;
183 return changed;
186 static int64_t coroutine_fn cow_co_get_block_status(BlockDriverState *bs,
187 int64_t sector_num, int nb_sectors, int *num_same)
189 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;
195 return (ret ? BDRV_BLOCK_DATA : 0) | offset | BDRV_BLOCK_OFFSET_VALID;
198 static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,
199 int nb_sectors)
201 int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8;
202 uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE;
203 bool first = true;
204 int sector_bits;
206 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];
213 bitnum &= BITS_PER_BITMAP_SECTOR - 1;
214 sector_bits = MIN(nb_sectors, BITS_PER_BITMAP_SECTOR - bitnum);
216 ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
217 if (ret < 0) {
218 return ret;
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;
230 if (first) {
231 ret = bdrv_flush(bs->file);
232 if (ret < 0) {
233 return ret;
235 first = false;
238 cow_set_bits(bitmap, bitnum, sector_bits);
240 ret = bdrv_pwrite(bs->file, offset, &bitmap, sizeof(bitmap));
241 if (ret < 0) {
242 return ret;
246 return 0;
249 static int coroutine_fn cow_read(BlockDriverState *bs, int64_t sector_num,
250 uint8_t *buf, int nb_sectors)
252 BDRVCowState *s = bs->opaque;
253 int ret, n;
255 while (nb_sectors > 0) {
256 ret = cow_co_is_allocated(bs, sector_num, nb_sectors, &n);
257 if (ret < 0) {
258 return ret;
260 if (ret) {
261 ret = bdrv_pread(bs->file,
262 s->cow_sectors_offset + sector_num * 512,
263 buf, n * 512);
264 if (ret < 0) {
265 return ret;
267 } else {
268 if (bs->backing_hd) {
269 /* read from the base image */
270 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
271 if (ret < 0) {
272 return ret;
274 } else {
275 memset(buf, 0, n * 512);
278 nb_sectors -= n;
279 sector_num += n;
280 buf += n * 512;
282 return 0;
285 static coroutine_fn int cow_co_read(BlockDriverState *bs, int64_t sector_num,
286 uint8_t *buf, int nb_sectors)
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;
296 static int cow_write(BlockDriverState *bs, int64_t sector_num,
297 const uint8_t *buf, int nb_sectors)
299 BDRVCowState *s = bs->opaque;
300 int ret;
302 ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512,
303 buf, nb_sectors * 512);
304 if (ret < 0) {
305 return ret;
308 return cow_update_bitmap(bs, sector_num, nb_sectors);
311 static coroutine_fn int cow_co_write(BlockDriverState *bs, int64_t sector_num,
312 const uint8_t *buf, int nb_sectors)
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;
322 static void cow_close(BlockDriverState *bs)
326 static int cow_create(const char *filename, QEMUOptionParameter *options,
327 Error **errp)
329 struct cow_header_v2 cow_header;
330 struct stat st;
331 int64_t image_sectors = 0;
332 const char *image_filename = NULL;
333 Error *local_err = NULL;
334 int ret;
335 BlockDriverState *cow_bs;
337 /* 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;
344 options++;
347 ret = bdrv_create_file(filename, options, &local_err);
348 if (ret < 0) {
349 qerror_report_err(local_err);
350 error_free(local_err);
351 return ret;
354 ret = bdrv_file_open(&cow_bs, filename, NULL, BDRV_O_RDWR, &local_err);
355 if (ret < 0) {
356 qerror_report_err(local_err);
357 error_free(local_err);
358 return ret;
361 memset(&cow_header, 0, sizeof(cow_header));
362 cow_header.magic = cpu_to_be32(COW_MAGIC);
363 cow_header.version = cpu_to_be32(COW_VERSION);
364 if (image_filename) {
365 /* Note: if no file, we put a dummy mtime */
366 cow_header.mtime = cpu_to_be32(0);
368 if (stat(image_filename, &st) != 0) {
369 goto mtime_fail;
371 cow_header.mtime = cpu_to_be32(st.st_mtime);
372 mtime_fail:
373 pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file),
374 image_filename);
376 cow_header.sectorsize = cpu_to_be32(512);
377 cow_header.size = cpu_to_be64(image_sectors * 512);
378 ret = bdrv_pwrite(cow_bs, 0, &cow_header, sizeof(cow_header));
379 if (ret < 0) {
380 goto exit;
383 /* resize to include at least all the bitmap */
384 ret = bdrv_truncate(cow_bs,
385 sizeof(cow_header) + ((image_sectors + 7) >> 3));
386 if (ret < 0) {
387 goto exit;
390 exit:
391 bdrv_unref(cow_bs);
392 return ret;
395 static QEMUOptionParameter cow_create_options[] = {
397 .name = BLOCK_OPT_SIZE,
398 .type = OPT_SIZE,
399 .help = "Virtual disk size"
402 .name = BLOCK_OPT_BACKING_FILE,
403 .type = OPT_STRING,
404 .help = "File name of a base image"
406 { NULL }
409 static BlockDriver bdrv_cow = {
410 .format_name = "cow",
411 .instance_size = sizeof(BDRVCowState),
413 .bdrv_probe = cow_probe,
414 .bdrv_open = cow_open,
415 .bdrv_close = cow_close,
416 .bdrv_create = cow_create,
417 .bdrv_has_zero_init = bdrv_has_zero_init_1,
419 .bdrv_read = cow_co_read,
420 .bdrv_write = cow_co_write,
421 .bdrv_co_get_block_status = cow_co_get_block_status,
423 .create_options = cow_create_options,
426 static void bdrv_cow_init(void)
428 bdrv_register(&bdrv_cow);
431 block_init(bdrv_cow_init);