]> git.proxmox.com Git - qemu.git/blob - block/cow.c
block: introduce bdrv_get_block_status API
[qemu.git] / block / cow.c
1 /*
2 * Block driver for the COW format
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 *
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 */
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/module.h"
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 {
45 CoMutex lock;
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
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;
59 }
60
61 static int cow_open(BlockDriverState *bs, QDict *options, int flags)
62 {
63 BDRVCowState *s = bs->opaque;
64 struct cow_header_v2 cow_header;
65 int bitmap_size;
66 int64_t size;
67 int ret;
68
69 /* see if it is a cow image */
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) {
76 ret = -EMEDIUMTYPE;
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;
87 goto fail;
88 }
89
90 /* cow image found */
91 size = be64_to_cpu(cow_header.size);
92 bs->total_sectors = size / 512;
93
94 pstrcpy(bs->backing_file, sizeof(bs->backing_file),
95 cow_header.backing_file);
96
97 bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
98 s->cow_sectors_offset = (bitmap_size + 511) & ~511;
99 qemu_co_mutex_init(&s->lock);
100 return 0;
101 fail:
102 return ret;
103 }
104
105 /*
106 * XXX(hch): right now these functions are extremely inefficient.
107 * We should just read the whole bitmap we'll need in one go instead.
108 */
109 static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum, bool *first)
110 {
111 uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
112 uint8_t bitmap;
113 int ret;
114
115 ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
116 if (ret < 0) {
117 return ret;
118 }
119
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
132 bitmap |= (1 << (bitnum % 8));
133
134 ret = bdrv_pwrite(bs->file, offset, &bitmap, sizeof(bitmap));
135 if (ret < 0) {
136 return ret;
137 }
138 return 0;
139 }
140
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)
145 {
146 return (bitmap[bitnum / 8] & (1 << (bitnum & 7))) != 0;
147 }
148
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;
164 }
165 return MIN(bitnum, last) - start;
166 }
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. */
171 static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
172 int64_t sector_num, int nb_sectors, int *num_same)
173 {
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;
178 int changed;
179
180 ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
181 if (ret < 0) {
182 return ret;
183 }
184
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);
188 return changed;
189 }
190
191 static int64_t coroutine_fn cow_co_get_block_status(BlockDriverState *bs,
192 int64_t sector_num, int nb_sectors, int *num_same)
193 {
194 return cow_co_is_allocated(bs, sector_num, nb_sectors, num_same);
195 }
196
197 static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,
198 int nb_sectors)
199 {
200 int error = 0;
201 int i;
202 bool first = true;
203
204 for (i = 0; i < nb_sectors; i++) {
205 error = cow_set_bit(bs, sector_num + i, &first);
206 if (error) {
207 break;
208 }
209 }
210
211 return error;
212 }
213
214 static int coroutine_fn cow_read(BlockDriverState *bs, int64_t sector_num,
215 uint8_t *buf, int nb_sectors)
216 {
217 BDRVCowState *s = bs->opaque;
218 int ret, n;
219
220 while (nb_sectors > 0) {
221 ret = cow_co_is_allocated(bs, sector_num, nb_sectors, &n);
222 if (ret < 0) {
223 return ret;
224 }
225 if (ret) {
226 ret = bdrv_pread(bs->file,
227 s->cow_sectors_offset + sector_num * 512,
228 buf, n * 512);
229 if (ret < 0) {
230 return ret;
231 }
232 } else {
233 if (bs->backing_hd) {
234 /* read from the base image */
235 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
236 if (ret < 0) {
237 return ret;
238 }
239 } else {
240 memset(buf, 0, n * 512);
241 }
242 }
243 nb_sectors -= n;
244 sector_num += n;
245 buf += n * 512;
246 }
247 return 0;
248 }
249
250 static coroutine_fn int cow_co_read(BlockDriverState *bs, int64_t sector_num,
251 uint8_t *buf, int nb_sectors)
252 {
253 int ret;
254 BDRVCowState *s = bs->opaque;
255 qemu_co_mutex_lock(&s->lock);
256 ret = cow_read(bs, sector_num, buf, nb_sectors);
257 qemu_co_mutex_unlock(&s->lock);
258 return ret;
259 }
260
261 static int cow_write(BlockDriverState *bs, int64_t sector_num,
262 const uint8_t *buf, int nb_sectors)
263 {
264 BDRVCowState *s = bs->opaque;
265 int ret;
266
267 ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512,
268 buf, nb_sectors * 512);
269 if (ret < 0) {
270 return ret;
271 }
272
273 return cow_update_bitmap(bs, sector_num, nb_sectors);
274 }
275
276 static coroutine_fn int cow_co_write(BlockDriverState *bs, int64_t sector_num,
277 const uint8_t *buf, int nb_sectors)
278 {
279 int ret;
280 BDRVCowState *s = bs->opaque;
281 qemu_co_mutex_lock(&s->lock);
282 ret = cow_write(bs, sector_num, buf, nb_sectors);
283 qemu_co_mutex_unlock(&s->lock);
284 return ret;
285 }
286
287 static void cow_close(BlockDriverState *bs)
288 {
289 }
290
291 static int cow_create(const char *filename, QEMUOptionParameter *options)
292 {
293 struct cow_header_v2 cow_header;
294 struct stat st;
295 int64_t image_sectors = 0;
296 const char *image_filename = NULL;
297 int ret;
298 BlockDriverState *cow_bs;
299
300 /* Read out options */
301 while (options && options->name) {
302 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
303 image_sectors = options->value.n / 512;
304 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
305 image_filename = options->value.s;
306 }
307 options++;
308 }
309
310 ret = bdrv_create_file(filename, options);
311 if (ret < 0) {
312 return ret;
313 }
314
315 ret = bdrv_file_open(&cow_bs, filename, NULL, BDRV_O_RDWR);
316 if (ret < 0) {
317 return ret;
318 }
319
320 memset(&cow_header, 0, sizeof(cow_header));
321 cow_header.magic = cpu_to_be32(COW_MAGIC);
322 cow_header.version = cpu_to_be32(COW_VERSION);
323 if (image_filename) {
324 /* Note: if no file, we put a dummy mtime */
325 cow_header.mtime = cpu_to_be32(0);
326
327 if (stat(image_filename, &st) != 0) {
328 goto mtime_fail;
329 }
330 cow_header.mtime = cpu_to_be32(st.st_mtime);
331 mtime_fail:
332 pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file),
333 image_filename);
334 }
335 cow_header.sectorsize = cpu_to_be32(512);
336 cow_header.size = cpu_to_be64(image_sectors * 512);
337 ret = bdrv_pwrite(cow_bs, 0, &cow_header, sizeof(cow_header));
338 if (ret < 0) {
339 goto exit;
340 }
341
342 /* resize to include at least all the bitmap */
343 ret = bdrv_truncate(cow_bs,
344 sizeof(cow_header) + ((image_sectors + 7) >> 3));
345 if (ret < 0) {
346 goto exit;
347 }
348
349 exit:
350 bdrv_unref(cow_bs);
351 return ret;
352 }
353
354 static QEMUOptionParameter cow_create_options[] = {
355 {
356 .name = BLOCK_OPT_SIZE,
357 .type = OPT_SIZE,
358 .help = "Virtual disk size"
359 },
360 {
361 .name = BLOCK_OPT_BACKING_FILE,
362 .type = OPT_STRING,
363 .help = "File name of a base image"
364 },
365 { NULL }
366 };
367
368 static BlockDriver bdrv_cow = {
369 .format_name = "cow",
370 .instance_size = sizeof(BDRVCowState),
371
372 .bdrv_probe = cow_probe,
373 .bdrv_open = cow_open,
374 .bdrv_close = cow_close,
375 .bdrv_create = cow_create,
376 .bdrv_has_zero_init = bdrv_has_zero_init_1,
377
378 .bdrv_read = cow_co_read,
379 .bdrv_write = cow_co_write,
380 .bdrv_co_get_block_status = cow_co_get_block_status,
381
382 .create_options = cow_create_options,
383 };
384
385 static void bdrv_cow_init(void)
386 {
387 bdrv_register(&bdrv_cow);
388 }
389
390 block_init(bdrv_cow_init);