]> git.proxmox.com Git - qemu.git/blob - block/cow.c
block: expect errors from bdrv_co_is_allocated
[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)
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 bitmap |= (1 << (bitnum % 8));
121
122 ret = bdrv_pwrite_sync(bs->file, offset, &bitmap, sizeof(bitmap));
123 if (ret < 0) {
124 return ret;
125 }
126 return 0;
127 }
128
129 static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum)
130 {
131 uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
132 uint8_t bitmap;
133 int ret;
134
135 ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
136 if (ret < 0) {
137 return ret;
138 }
139
140 return !!(bitmap & (1 << (bitnum % 8)));
141 }
142
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. */
146 static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
147 int64_t sector_num, int nb_sectors, int *num_same)
148 {
149 int changed;
150
151 if (nb_sectors == 0) {
152 *num_same = nb_sectors;
153 return 0;
154 }
155
156 changed = is_bit_set(bs, sector_num);
157 if (changed < 0) {
158 return 0; /* XXX: how to return I/O errors? */
159 }
160
161 for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) {
162 if (is_bit_set(bs, sector_num + *num_same) != changed)
163 break;
164 }
165
166 return changed;
167 }
168
169 static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,
170 int nb_sectors)
171 {
172 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;
183 }
184
185 static int coroutine_fn cow_read(BlockDriverState *bs, int64_t sector_num,
186 uint8_t *buf, int nb_sectors)
187 {
188 BDRVCowState *s = bs->opaque;
189 int ret, n;
190
191 while (nb_sectors > 0) {
192 ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &n);
193 if (ret < 0) {
194 return ret;
195 }
196 if (ret) {
197 ret = bdrv_pread(bs->file,
198 s->cow_sectors_offset + sector_num * 512,
199 buf, n * 512);
200 if (ret < 0) {
201 return ret;
202 }
203 } else {
204 if (bs->backing_hd) {
205 /* read from the base image */
206 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
207 if (ret < 0) {
208 return ret;
209 }
210 } else {
211 memset(buf, 0, n * 512);
212 }
213 }
214 nb_sectors -= n;
215 sector_num += n;
216 buf += n * 512;
217 }
218 return 0;
219 }
220
221 static coroutine_fn int cow_co_read(BlockDriverState *bs, int64_t sector_num,
222 uint8_t *buf, int nb_sectors)
223 {
224 int ret;
225 BDRVCowState *s = bs->opaque;
226 qemu_co_mutex_lock(&s->lock);
227 ret = cow_read(bs, sector_num, buf, nb_sectors);
228 qemu_co_mutex_unlock(&s->lock);
229 return ret;
230 }
231
232 static int cow_write(BlockDriverState *bs, int64_t sector_num,
233 const uint8_t *buf, int nb_sectors)
234 {
235 BDRVCowState *s = bs->opaque;
236 int ret;
237
238 ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512,
239 buf, nb_sectors * 512);
240 if (ret < 0) {
241 return ret;
242 }
243
244 return cow_update_bitmap(bs, sector_num, nb_sectors);
245 }
246
247 static coroutine_fn int cow_co_write(BlockDriverState *bs, int64_t sector_num,
248 const uint8_t *buf, int nb_sectors)
249 {
250 int ret;
251 BDRVCowState *s = bs->opaque;
252 qemu_co_mutex_lock(&s->lock);
253 ret = cow_write(bs, sector_num, buf, nb_sectors);
254 qemu_co_mutex_unlock(&s->lock);
255 return ret;
256 }
257
258 static void cow_close(BlockDriverState *bs)
259 {
260 }
261
262 static int cow_create(const char *filename, QEMUOptionParameter *options)
263 {
264 struct cow_header_v2 cow_header;
265 struct stat st;
266 int64_t image_sectors = 0;
267 const char *image_filename = NULL;
268 int ret;
269 BlockDriverState *cow_bs;
270
271 /* Read out options */
272 while (options && options->name) {
273 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
274 image_sectors = options->value.n / 512;
275 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
276 image_filename = options->value.s;
277 }
278 options++;
279 }
280
281 ret = bdrv_create_file(filename, options);
282 if (ret < 0) {
283 return ret;
284 }
285
286 ret = bdrv_file_open(&cow_bs, filename, NULL, BDRV_O_RDWR);
287 if (ret < 0) {
288 return ret;
289 }
290
291 memset(&cow_header, 0, sizeof(cow_header));
292 cow_header.magic = cpu_to_be32(COW_MAGIC);
293 cow_header.version = cpu_to_be32(COW_VERSION);
294 if (image_filename) {
295 /* Note: if no file, we put a dummy mtime */
296 cow_header.mtime = cpu_to_be32(0);
297
298 if (stat(image_filename, &st) != 0) {
299 goto mtime_fail;
300 }
301 cow_header.mtime = cpu_to_be32(st.st_mtime);
302 mtime_fail:
303 pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file),
304 image_filename);
305 }
306 cow_header.sectorsize = cpu_to_be32(512);
307 cow_header.size = cpu_to_be64(image_sectors * 512);
308 ret = bdrv_pwrite(cow_bs, 0, &cow_header, sizeof(cow_header));
309 if (ret < 0) {
310 goto exit;
311 }
312
313 /* resize to include at least all the bitmap */
314 ret = bdrv_truncate(cow_bs,
315 sizeof(cow_header) + ((image_sectors + 7) >> 3));
316 if (ret < 0) {
317 goto exit;
318 }
319
320 exit:
321 bdrv_delete(cow_bs);
322 return ret;
323 }
324
325 static QEMUOptionParameter cow_create_options[] = {
326 {
327 .name = BLOCK_OPT_SIZE,
328 .type = OPT_SIZE,
329 .help = "Virtual disk size"
330 },
331 {
332 .name = BLOCK_OPT_BACKING_FILE,
333 .type = OPT_STRING,
334 .help = "File name of a base image"
335 },
336 { NULL }
337 };
338
339 static BlockDriver bdrv_cow = {
340 .format_name = "cow",
341 .instance_size = sizeof(BDRVCowState),
342
343 .bdrv_probe = cow_probe,
344 .bdrv_open = cow_open,
345 .bdrv_close = cow_close,
346 .bdrv_create = cow_create,
347 .bdrv_has_zero_init = bdrv_has_zero_init_1,
348
349 .bdrv_read = cow_co_read,
350 .bdrv_write = cow_co_write,
351 .bdrv_co_is_allocated = cow_co_is_allocated,
352
353 .create_options = cow_create_options,
354 };
355
356 static void bdrv_cow_init(void)
357 {
358 bdrv_register(&bdrv_cow);
359 }
360
361 block_init(bdrv_cow_init);