|
|
1.1 root 1: /*
2: * Block driver for the QCOW version 2 format
3: *
4: * Copyright (c) 2004-2006 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_int.h"
26: #include "module.h"
27: #include <zlib.h>
28: #include "aes.h"
29: #include "block/qcow2.h"
30:
31: /*
32: Differences with QCOW:
33:
34: - Support for multiple incremental snapshots.
35: - Memory management by reference counts.
36: - Clusters which have a reference count of one have the bit
37: QCOW_OFLAG_COPIED to optimize write performance.
38: - Size of compressed clusters is stored in sectors to reduce bit usage
39: in the cluster offsets.
40: - Support for storing additional data (such as the VM state) in the
41: snapshots.
42: - If a backing store is used, the cluster size is not constrained
43: (could be backported to QCOW).
44: - L2 tables have always a size of one cluster.
45: */
46:
47:
48: typedef struct {
49: uint32_t magic;
50: uint32_t len;
51: } QCowExtension;
52: #define QCOW_EXT_MAGIC_END 0
53: #define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
54:
55:
56:
57: static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
58: {
59: const QCowHeader *cow_header = (const void *)buf;
60:
61: if (buf_size >= sizeof(QCowHeader) &&
62: be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
63: be32_to_cpu(cow_header->version) == QCOW_VERSION)
64: return 100;
65: else
66: return 0;
67: }
68:
69:
70: /*
71: * read qcow2 extension and fill bs
72: * start reading from start_offset
73: * finish reading upon magic of value 0 or when end_offset reached
74: * unknown magic is skipped (future extension this version knows nothing about)
75: * return 0 upon success, non-0 otherwise
76: */
77: static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset,
78: uint64_t end_offset)
79: {
80: BDRVQcowState *s = bs->opaque;
81: QCowExtension ext;
82: uint64_t offset;
83:
84: #ifdef DEBUG_EXT
85: printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
86: #endif
87: offset = start_offset;
88: while (offset < end_offset) {
89:
90: #ifdef DEBUG_EXT
91: /* Sanity check */
92: if (offset > s->cluster_size)
93: printf("qcow_handle_extension: suspicious offset %lu\n", offset);
94:
95: printf("attemting to read extended header in offset %lu\n", offset);
96: #endif
97:
98: if (bdrv_pread(s->hd, offset, &ext, sizeof(ext)) != sizeof(ext)) {
99: fprintf(stderr, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
100: (unsigned long long)offset);
101: return 1;
102: }
103: be32_to_cpus(&ext.magic);
104: be32_to_cpus(&ext.len);
105: offset += sizeof(ext);
106: #ifdef DEBUG_EXT
107: printf("ext.magic = 0x%x\n", ext.magic);
108: #endif
109: switch (ext.magic) {
110: case QCOW_EXT_MAGIC_END:
111: return 0;
112:
113: case QCOW_EXT_MAGIC_BACKING_FORMAT:
114: if (ext.len >= sizeof(bs->backing_format)) {
115: fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
116: " (>=%zu)\n",
117: ext.len, sizeof(bs->backing_format));
118: return 2;
119: }
120: if (bdrv_pread(s->hd, offset , bs->backing_format,
121: ext.len) != ext.len)
122: return 3;
123: bs->backing_format[ext.len] = '\0';
124: #ifdef DEBUG_EXT
125: printf("Qcow2: Got format extension %s\n", bs->backing_format);
126: #endif
1.1.1.2 root 127: offset = ((offset + ext.len + 7) & ~7);
1.1 root 128: break;
129:
130: default:
131: /* unknown magic -- just skip it */
1.1.1.2 root 132: offset = ((offset + ext.len + 7) & ~7);
1.1 root 133: break;
134: }
135: }
136:
137: return 0;
138: }
139:
140:
141: static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
142: {
143: BDRVQcowState *s = bs->opaque;
144: int len, i, shift, ret;
145: QCowHeader header;
146: uint64_t ext_end;
147:
148: ret = bdrv_file_open(&s->hd, filename, flags);
149: if (ret < 0)
150: return ret;
151: if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
152: goto fail;
153: be32_to_cpus(&header.magic);
154: be32_to_cpus(&header.version);
155: be64_to_cpus(&header.backing_file_offset);
156: be32_to_cpus(&header.backing_file_size);
157: be64_to_cpus(&header.size);
158: be32_to_cpus(&header.cluster_bits);
159: be32_to_cpus(&header.crypt_method);
160: be64_to_cpus(&header.l1_table_offset);
161: be32_to_cpus(&header.l1_size);
162: be64_to_cpus(&header.refcount_table_offset);
163: be32_to_cpus(&header.refcount_table_clusters);
164: be64_to_cpus(&header.snapshots_offset);
165: be32_to_cpus(&header.nb_snapshots);
166:
167: if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
168: goto fail;
1.1.1.2 root 169: if (header.cluster_bits < MIN_CLUSTER_BITS ||
1.1 root 170: header.cluster_bits > MAX_CLUSTER_BITS)
171: goto fail;
172: if (header.crypt_method > QCOW_CRYPT_AES)
173: goto fail;
174: s->crypt_method_header = header.crypt_method;
175: if (s->crypt_method_header)
176: bs->encrypted = 1;
177: s->cluster_bits = header.cluster_bits;
178: s->cluster_size = 1 << s->cluster_bits;
179: s->cluster_sectors = 1 << (s->cluster_bits - 9);
180: s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
181: s->l2_size = 1 << s->l2_bits;
182: bs->total_sectors = header.size / 512;
183: s->csize_shift = (62 - (s->cluster_bits - 8));
184: s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
185: s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
186: s->refcount_table_offset = header.refcount_table_offset;
187: s->refcount_table_size =
188: header.refcount_table_clusters << (s->cluster_bits - 3);
189:
190: s->snapshots_offset = header.snapshots_offset;
191: s->nb_snapshots = header.nb_snapshots;
192:
193: /* read the level 1 table */
194: s->l1_size = header.l1_size;
195: shift = s->cluster_bits + s->l2_bits;
196: s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
197: /* the L1 table must contain at least enough entries to put
198: header.size bytes */
199: if (s->l1_size < s->l1_vm_state_index)
200: goto fail;
201: s->l1_table_offset = header.l1_table_offset;
1.1.1.2 root 202: if (s->l1_size > 0) {
203: s->l1_table = qemu_mallocz(
204: align_offset(s->l1_size * sizeof(uint64_t), 512));
205: if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
206: s->l1_size * sizeof(uint64_t))
207: goto fail;
208: for(i = 0;i < s->l1_size; i++) {
209: be64_to_cpus(&s->l1_table[i]);
210: }
1.1 root 211: }
212: /* alloc L2 cache */
213: s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
214: s->cluster_cache = qemu_malloc(s->cluster_size);
215: /* one more sector for decompressed data alignment */
216: s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
217: + 512);
218: s->cluster_cache_offset = -1;
219:
220: if (qcow2_refcount_init(bs) < 0)
221: goto fail;
222:
1.1.1.2 root 223: QLIST_INIT(&s->cluster_allocs);
1.1 root 224:
225: /* read qcow2 extensions */
226: if (header.backing_file_offset)
227: ext_end = header.backing_file_offset;
228: else
229: ext_end = s->cluster_size;
230: if (qcow_read_extensions(bs, sizeof(header), ext_end))
231: goto fail;
232:
233: /* read the backing file name */
234: if (header.backing_file_offset != 0) {
235: len = header.backing_file_size;
236: if (len > 1023)
237: len = 1023;
238: if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
239: goto fail;
240: bs->backing_file[len] = '\0';
241: }
242: if (qcow2_read_snapshots(bs) < 0)
243: goto fail;
244:
245: #ifdef DEBUG_ALLOC
246: qcow2_check_refcounts(bs);
247: #endif
248: return 0;
249:
250: fail:
251: qcow2_free_snapshots(bs);
252: qcow2_refcount_close(bs);
253: qemu_free(s->l1_table);
254: qemu_free(s->l2_cache);
255: qemu_free(s->cluster_cache);
256: qemu_free(s->cluster_data);
257: bdrv_delete(s->hd);
258: return -1;
259: }
260:
261: static int qcow_set_key(BlockDriverState *bs, const char *key)
262: {
263: BDRVQcowState *s = bs->opaque;
264: uint8_t keybuf[16];
265: int len, i;
266:
267: memset(keybuf, 0, 16);
268: len = strlen(key);
269: if (len > 16)
270: len = 16;
271: /* XXX: we could compress the chars to 7 bits to increase
272: entropy */
273: for(i = 0;i < len;i++) {
274: keybuf[i] = key[i];
275: }
276: s->crypt_method = s->crypt_method_header;
277:
278: if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
279: return -1;
280: if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
281: return -1;
282: #if 0
283: /* test */
284: {
285: uint8_t in[16];
286: uint8_t out[16];
287: uint8_t tmp[16];
288: for(i=0;i<16;i++)
289: in[i] = i;
290: AES_encrypt(in, tmp, &s->aes_encrypt_key);
291: AES_decrypt(tmp, out, &s->aes_decrypt_key);
292: for(i = 0; i < 16; i++)
293: printf(" %02x", tmp[i]);
294: printf("\n");
295: for(i = 0; i < 16; i++)
296: printf(" %02x", out[i]);
297: printf("\n");
298: }
299: #endif
300: return 0;
301: }
302:
303: static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
304: int nb_sectors, int *pnum)
305: {
306: uint64_t cluster_offset;
307:
308: *pnum = nb_sectors;
309: cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, pnum);
310:
311: return (cluster_offset != 0);
312: }
313:
314: /* handle reading after the end of the backing file */
315: int qcow2_backing_read1(BlockDriverState *bs,
316: int64_t sector_num, uint8_t *buf, int nb_sectors)
317: {
318: int n1;
319: if ((sector_num + nb_sectors) <= bs->total_sectors)
320: return nb_sectors;
321: if (sector_num >= bs->total_sectors)
322: n1 = 0;
323: else
324: n1 = bs->total_sectors - sector_num;
325: memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
326: return n1;
327: }
328:
329: typedef struct QCowAIOCB {
330: BlockDriverAIOCB common;
331: int64_t sector_num;
332: QEMUIOVector *qiov;
333: uint8_t *buf;
334: void *orig_buf;
335: int nb_sectors;
336: int n;
337: uint64_t cluster_offset;
338: uint8_t *cluster_data;
339: BlockDriverAIOCB *hd_aiocb;
340: struct iovec hd_iov;
341: QEMUIOVector hd_qiov;
342: QEMUBH *bh;
343: QCowL2Meta l2meta;
1.1.1.2 root 344: QLIST_ENTRY(QCowAIOCB) next_depend;
1.1 root 345: } QCowAIOCB;
346:
347: static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
348: {
349: QCowAIOCB *acb = (QCowAIOCB *)blockacb;
350: if (acb->hd_aiocb)
351: bdrv_aio_cancel(acb->hd_aiocb);
352: qemu_aio_release(acb);
353: }
354:
355: static AIOPool qcow_aio_pool = {
356: .aiocb_size = sizeof(QCowAIOCB),
357: .cancel = qcow_aio_cancel,
358: };
359:
360: static void qcow_aio_read_cb(void *opaque, int ret);
361: static void qcow_aio_read_bh(void *opaque)
362: {
363: QCowAIOCB *acb = opaque;
364: qemu_bh_delete(acb->bh);
365: acb->bh = NULL;
366: qcow_aio_read_cb(opaque, 0);
367: }
368:
369: static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb)
370: {
371: if (acb->bh)
372: return -EIO;
373:
374: acb->bh = qemu_bh_new(cb, acb);
375: if (!acb->bh)
376: return -EIO;
377:
378: qemu_bh_schedule(acb->bh);
379:
380: return 0;
381: }
382:
383: static void qcow_aio_read_cb(void *opaque, int ret)
384: {
385: QCowAIOCB *acb = opaque;
386: BlockDriverState *bs = acb->common.bs;
387: BDRVQcowState *s = bs->opaque;
388: int index_in_cluster, n1;
389:
390: acb->hd_aiocb = NULL;
391: if (ret < 0)
392: goto done;
393:
394: /* post process the read buffer */
395: if (!acb->cluster_offset) {
396: /* nothing to do */
397: } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
398: /* nothing to do */
399: } else {
400: if (s->crypt_method) {
401: qcow2_encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
402: acb->n, 0,
403: &s->aes_decrypt_key);
404: }
405: }
406:
407: acb->nb_sectors -= acb->n;
408: acb->sector_num += acb->n;
409: acb->buf += acb->n * 512;
410:
411: if (acb->nb_sectors == 0) {
412: /* request completed */
413: ret = 0;
414: goto done;
415: }
416:
417: /* prepare next AIO request */
418: acb->n = acb->nb_sectors;
419: acb->cluster_offset =
420: qcow2_get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
421: index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
422:
423: if (!acb->cluster_offset) {
424: if (bs->backing_hd) {
425: /* read from the base image */
426: n1 = qcow2_backing_read1(bs->backing_hd, acb->sector_num,
427: acb->buf, acb->n);
428: if (n1 > 0) {
429: acb->hd_iov.iov_base = (void *)acb->buf;
430: acb->hd_iov.iov_len = acb->n * 512;
431: qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
432: acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
433: &acb->hd_qiov, acb->n,
434: qcow_aio_read_cb, acb);
435: if (acb->hd_aiocb == NULL)
436: goto done;
437: } else {
438: ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
439: if (ret < 0)
440: goto done;
441: }
442: } else {
443: /* Note: in this case, no need to wait */
444: memset(acb->buf, 0, 512 * acb->n);
445: ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
446: if (ret < 0)
447: goto done;
448: }
449: } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
450: /* add AIO support for compressed blocks ? */
451: if (qcow2_decompress_cluster(s, acb->cluster_offset) < 0)
452: goto done;
453: memcpy(acb->buf,
454: s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
455: ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
456: if (ret < 0)
457: goto done;
458: } else {
459: if ((acb->cluster_offset & 511) != 0) {
460: ret = -EIO;
461: goto done;
462: }
463:
464: acb->hd_iov.iov_base = (void *)acb->buf;
465: acb->hd_iov.iov_len = acb->n * 512;
466: qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
467: acb->hd_aiocb = bdrv_aio_readv(s->hd,
468: (acb->cluster_offset >> 9) + index_in_cluster,
469: &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb);
1.1.1.4 root 470: if (acb->hd_aiocb == NULL) {
471: ret = -EIO;
1.1 root 472: goto done;
1.1.1.4 root 473: }
1.1 root 474: }
475:
476: return;
477: done:
478: if (acb->qiov->niov > 1) {
479: qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
480: qemu_vfree(acb->orig_buf);
481: }
482: acb->common.cb(acb->common.opaque, ret);
483: qemu_aio_release(acb);
484: }
485:
486: static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
487: int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
488: BlockDriverCompletionFunc *cb, void *opaque, int is_write)
489: {
490: QCowAIOCB *acb;
491:
492: acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque);
493: if (!acb)
494: return NULL;
495: acb->hd_aiocb = NULL;
496: acb->sector_num = sector_num;
497: acb->qiov = qiov;
498: if (qiov->niov > 1) {
499: acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size);
500: if (is_write)
501: qemu_iovec_to_buffer(qiov, acb->buf);
502: } else {
503: acb->buf = (uint8_t *)qiov->iov->iov_base;
504: }
505: acb->nb_sectors = nb_sectors;
506: acb->n = 0;
507: acb->cluster_offset = 0;
508: acb->l2meta.nb_clusters = 0;
1.1.1.2 root 509: QLIST_INIT(&acb->l2meta.dependent_requests);
1.1 root 510: return acb;
511: }
512:
513: static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs,
514: int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
515: BlockDriverCompletionFunc *cb, void *opaque)
516: {
517: QCowAIOCB *acb;
518:
519: acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
520: if (!acb)
521: return NULL;
522:
523: qcow_aio_read_cb(acb, 0);
524: return &acb->common;
525: }
526:
527: static void qcow_aio_write_cb(void *opaque, int ret);
528:
529: static void run_dependent_requests(QCowL2Meta *m)
530: {
531: QCowAIOCB *req;
532: QCowAIOCB *next;
533:
534: /* Take the request off the list of running requests */
535: if (m->nb_clusters != 0) {
1.1.1.2 root 536: QLIST_REMOVE(m, next_in_flight);
1.1 root 537: }
538:
539: /*
540: * Restart all dependent requests.
1.1.1.2 root 541: * Can't use QLIST_FOREACH here - the next link might not be the same
1.1 root 542: * any more after the callback (request could depend on a different
543: * request now)
544: */
545: for (req = m->dependent_requests.lh_first; req != NULL; req = next) {
546: next = req->next_depend.le_next;
547: qcow_aio_write_cb(req, 0);
548: }
549:
550: /* Empty the list for the next part of the request */
1.1.1.2 root 551: QLIST_INIT(&m->dependent_requests);
1.1 root 552: }
553:
554: static void qcow_aio_write_cb(void *opaque, int ret)
555: {
556: QCowAIOCB *acb = opaque;
557: BlockDriverState *bs = acb->common.bs;
558: BDRVQcowState *s = bs->opaque;
559: int index_in_cluster;
560: const uint8_t *src_buf;
561: int n_end;
562:
563: acb->hd_aiocb = NULL;
564:
565: if (ret >= 0) {
1.1.1.3 root 566: ret = qcow2_alloc_cluster_link_l2(bs, &acb->l2meta);
1.1 root 567: }
568:
569: run_dependent_requests(&acb->l2meta);
570:
571: if (ret < 0)
572: goto done;
573:
574: acb->nb_sectors -= acb->n;
575: acb->sector_num += acb->n;
576: acb->buf += acb->n * 512;
577:
578: if (acb->nb_sectors == 0) {
579: /* request completed */
580: ret = 0;
581: goto done;
582: }
583:
584: index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
585: n_end = index_in_cluster + acb->nb_sectors;
586: if (s->crypt_method &&
587: n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
588: n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
589:
1.1.1.3 root 590: ret = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9,
591: index_in_cluster, n_end, &acb->n, &acb->l2meta);
592: if (ret < 0) {
593: goto done;
594: }
595:
596: acb->cluster_offset = acb->l2meta.cluster_offset;
1.1 root 597:
598: /* Need to wait for another request? If so, we are done for now. */
1.1.1.3 root 599: if (acb->l2meta.nb_clusters == 0 && acb->l2meta.depends_on != NULL) {
1.1.1.2 root 600: QLIST_INSERT_HEAD(&acb->l2meta.depends_on->dependent_requests,
1.1 root 601: acb, next_depend);
602: return;
603: }
604:
1.1.1.3 root 605: assert((acb->cluster_offset & 511) == 0);
606:
1.1 root 607: if (s->crypt_method) {
608: if (!acb->cluster_data) {
609: acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
610: s->cluster_size);
611: }
612: qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
613: acb->n, 1, &s->aes_encrypt_key);
614: src_buf = acb->cluster_data;
615: } else {
616: src_buf = acb->buf;
617: }
618: acb->hd_iov.iov_base = (void *)src_buf;
619: acb->hd_iov.iov_len = acb->n * 512;
620: qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
621: acb->hd_aiocb = bdrv_aio_writev(s->hd,
622: (acb->cluster_offset >> 9) + index_in_cluster,
623: &acb->hd_qiov, acb->n,
624: qcow_aio_write_cb, acb);
1.1.1.4 root 625: if (acb->hd_aiocb == NULL) {
626: ret = -EIO;
627: goto fail;
628: }
1.1 root 629:
630: return;
631:
1.1.1.4 root 632: fail:
633: if (acb->l2meta.nb_clusters != 0) {
634: QLIST_REMOVE(&acb->l2meta, next_in_flight);
635: }
1.1 root 636: done:
637: if (acb->qiov->niov > 1)
638: qemu_vfree(acb->orig_buf);
639: acb->common.cb(acb->common.opaque, ret);
640: qemu_aio_release(acb);
641: }
642:
643: static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs,
644: int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
645: BlockDriverCompletionFunc *cb, void *opaque)
646: {
647: BDRVQcowState *s = bs->opaque;
648: QCowAIOCB *acb;
649:
650: s->cluster_cache_offset = -1; /* disable compressed cache */
651:
652: acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
653: if (!acb)
654: return NULL;
655:
656: qcow_aio_write_cb(acb, 0);
657: return &acb->common;
658: }
659:
660: static void qcow_close(BlockDriverState *bs)
661: {
662: BDRVQcowState *s = bs->opaque;
663: qemu_free(s->l1_table);
664: qemu_free(s->l2_cache);
665: qemu_free(s->cluster_cache);
666: qemu_free(s->cluster_data);
667: qcow2_refcount_close(bs);
668: bdrv_delete(s->hd);
669: }
670:
671: static int get_bits_from_size(size_t size)
672: {
673: int res = 0;
674:
675: if (size == 0) {
676: return -1;
677: }
678:
679: while (size != 1) {
680: /* Not a power of two */
681: if (size & 1) {
682: return -1;
683: }
684:
685: size >>= 1;
686: res++;
687: }
688:
689: return res;
690: }
691:
1.1.1.2 root 692:
693: static int preallocate(BlockDriverState *bs)
694: {
695: BDRVQcowState *s = bs->opaque;
696: uint64_t nb_sectors;
697: uint64_t offset;
698: int num;
1.1.1.3 root 699: int ret;
1.1.1.2 root 700: QCowL2Meta meta;
701:
702: nb_sectors = bdrv_getlength(bs) >> 9;
703: offset = 0;
704: QLIST_INIT(&meta.dependent_requests);
1.1.1.3 root 705: meta.cluster_offset = 0;
1.1.1.2 root 706:
707: while (nb_sectors) {
708: num = MIN(nb_sectors, INT_MAX >> 9);
1.1.1.3 root 709: ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, &meta);
1.1.1.2 root 710:
1.1.1.3 root 711: if (ret < 0) {
1.1.1.2 root 712: return -1;
713: }
714:
1.1.1.3 root 715: if (qcow2_alloc_cluster_link_l2(bs, &meta) < 0) {
716: qcow2_free_any_clusters(bs, meta.cluster_offset, meta.nb_clusters);
1.1.1.2 root 717: return -1;
718: }
719:
720: /* There are no dependent requests, but we need to remove our request
721: * from the list of in-flight requests */
722: run_dependent_requests(&meta);
723:
724: /* TODO Preallocate data if requested */
725:
726: nb_sectors -= num;
727: offset += num << 9;
728: }
729:
730: /*
731: * It is expected that the image file is large enough to actually contain
732: * all of the allocated clusters (otherwise we get failing reads after
733: * EOF). Extend the image to the last allocated sector.
734: */
1.1.1.3 root 735: if (meta.cluster_offset != 0) {
1.1.1.2 root 736: uint8_t buf[512];
737: memset(buf, 0, 512);
1.1.1.3 root 738: bdrv_write(s->hd, (meta.cluster_offset >> 9) + num - 1, buf, 1);
1.1.1.2 root 739: }
740:
741: return 0;
742: }
743:
1.1 root 744: static int qcow_create2(const char *filename, int64_t total_size,
745: const char *backing_file, const char *backing_format,
1.1.1.2 root 746: int flags, size_t cluster_size, int prealloc)
1.1 root 747: {
748:
749: int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
1.1.1.5 ! root 750: int ref_clusters, reftable_clusters, backing_format_len = 0;
1.1.1.2 root 751: int rounded_ext_bf_len = 0;
1.1 root 752: QCowHeader header;
753: uint64_t tmp, offset;
1.1.1.5 ! root 754: uint64_t old_ref_clusters;
1.1 root 755: QCowCreateState s1, *s = &s1;
756: QCowExtension ext_bf = {0, 0};
757:
758:
759: memset(s, 0, sizeof(*s));
760:
761: fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
762: if (fd < 0)
763: return -1;
764: memset(&header, 0, sizeof(header));
765: header.magic = cpu_to_be32(QCOW_MAGIC);
766: header.version = cpu_to_be32(QCOW_VERSION);
767: header.size = cpu_to_be64(total_size * 512);
768: header_size = sizeof(header);
769: backing_filename_len = 0;
770: if (backing_file) {
771: if (backing_format) {
772: ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT;
773: backing_format_len = strlen(backing_format);
1.1.1.2 root 774: ext_bf.len = backing_format_len;
775: rounded_ext_bf_len = (sizeof(ext_bf) + ext_bf.len + 7) & ~7;
776: header_size += rounded_ext_bf_len;
1.1 root 777: }
778: header.backing_file_offset = cpu_to_be64(header_size);
779: backing_filename_len = strlen(backing_file);
780: header.backing_file_size = cpu_to_be32(backing_filename_len);
781: header_size += backing_filename_len;
782: }
783:
784: /* Cluster size */
785: s->cluster_bits = get_bits_from_size(cluster_size);
786: if (s->cluster_bits < MIN_CLUSTER_BITS ||
787: s->cluster_bits > MAX_CLUSTER_BITS)
788: {
789: fprintf(stderr, "Cluster size must be a power of two between "
790: "%d and %dk\n",
791: 1 << MIN_CLUSTER_BITS,
792: 1 << (MAX_CLUSTER_BITS - 10));
793: return -EINVAL;
794: }
795: s->cluster_size = 1 << s->cluster_bits;
796:
797: header.cluster_bits = cpu_to_be32(s->cluster_bits);
798: header_size = (header_size + 7) & ~7;
799: if (flags & BLOCK_FLAG_ENCRYPT) {
800: header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
801: } else {
802: header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
803: }
804: l2_bits = s->cluster_bits - 3;
805: shift = s->cluster_bits + l2_bits;
806: l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
807: offset = align_offset(header_size, s->cluster_size);
808: s->l1_table_offset = offset;
809: header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
810: header.l1_size = cpu_to_be32(l1_size);
811: offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
812:
1.1.1.5 ! root 813: /* count how many refcount blocks needed */
! 814:
! 815: #define NUM_CLUSTERS(bytes) \
! 816: (((bytes) + (s->cluster_size) - 1) / (s->cluster_size))
! 817:
! 818: ref_clusters = NUM_CLUSTERS(NUM_CLUSTERS(offset) * sizeof(uint16_t));
! 819:
! 820: do {
! 821: uint64_t image_clusters;
! 822: old_ref_clusters = ref_clusters;
! 823:
! 824: /* Number of clusters used for the refcount table */
! 825: reftable_clusters = NUM_CLUSTERS(ref_clusters * sizeof(uint64_t));
! 826:
! 827: /* Number of clusters that the whole image will have */
! 828: image_clusters = NUM_CLUSTERS(offset) + ref_clusters
! 829: + reftable_clusters;
! 830:
! 831: /* Number of refcount blocks needed for the image */
! 832: ref_clusters = NUM_CLUSTERS(image_clusters * sizeof(uint16_t));
! 833:
! 834: } while (ref_clusters != old_ref_clusters);
! 835:
! 836: s->refcount_table = qemu_mallocz(reftable_clusters * s->cluster_size);
1.1 root 837:
838: s->refcount_table_offset = offset;
839: header.refcount_table_offset = cpu_to_be64(offset);
1.1.1.5 ! root 840: header.refcount_table_clusters = cpu_to_be32(reftable_clusters);
! 841: offset += (reftable_clusters * s->cluster_size);
1.1 root 842: s->refcount_block_offset = offset;
843:
844: for (i=0; i < ref_clusters; i++) {
845: s->refcount_table[i] = cpu_to_be64(offset);
846: offset += s->cluster_size;
847: }
848:
849: s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size);
850:
851: /* update refcounts */
852: qcow2_create_refcount_update(s, 0, header_size);
853: qcow2_create_refcount_update(s, s->l1_table_offset,
854: l1_size * sizeof(uint64_t));
1.1.1.5 ! root 855: qcow2_create_refcount_update(s, s->refcount_table_offset,
! 856: reftable_clusters * s->cluster_size);
1.1 root 857: qcow2_create_refcount_update(s, s->refcount_block_offset,
858: ref_clusters * s->cluster_size);
859:
860: /* write all the data */
861: write(fd, &header, sizeof(header));
862: if (backing_file) {
863: if (backing_format_len) {
864: char zero[16];
1.1.1.2 root 865: int padding = rounded_ext_bf_len - (ext_bf.len + sizeof(ext_bf));
1.1 root 866:
867: memset(zero, 0, sizeof(zero));
868: cpu_to_be32s(&ext_bf.magic);
869: cpu_to_be32s(&ext_bf.len);
870: write(fd, &ext_bf, sizeof(ext_bf));
871: write(fd, backing_format, backing_format_len);
1.1.1.2 root 872: if (padding > 0) {
873: write(fd, zero, padding);
1.1 root 874: }
875: }
876: write(fd, backing_file, backing_filename_len);
877: }
878: lseek(fd, s->l1_table_offset, SEEK_SET);
879: tmp = 0;
880: for(i = 0;i < l1_size; i++) {
881: write(fd, &tmp, sizeof(tmp));
882: }
883: lseek(fd, s->refcount_table_offset, SEEK_SET);
1.1.1.5 ! root 884: write(fd, s->refcount_table,
! 885: reftable_clusters * s->cluster_size);
1.1 root 886:
887: lseek(fd, s->refcount_block_offset, SEEK_SET);
888: write(fd, s->refcount_block, ref_clusters * s->cluster_size);
889:
890: qemu_free(s->refcount_table);
891: qemu_free(s->refcount_block);
892: close(fd);
1.1.1.2 root 893:
894: /* Preallocate metadata */
895: if (prealloc) {
896: BlockDriverState *bs;
897: bs = bdrv_new("");
898: bdrv_open(bs, filename, BDRV_O_CACHE_WB);
899: preallocate(bs);
900: bdrv_close(bs);
901: }
902:
1.1 root 903: return 0;
904: }
905:
906: static int qcow_create(const char *filename, QEMUOptionParameter *options)
907: {
908: const char *backing_file = NULL;
909: const char *backing_fmt = NULL;
910: uint64_t sectors = 0;
911: int flags = 0;
912: size_t cluster_size = 65536;
1.1.1.2 root 913: int prealloc = 0;
1.1 root 914:
915: /* Read out options */
916: while (options && options->name) {
917: if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
918: sectors = options->value.n / 512;
919: } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
920: backing_file = options->value.s;
921: } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
922: backing_fmt = options->value.s;
923: } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
924: flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
925: } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
926: if (options->value.n) {
927: cluster_size = options->value.n;
928: }
1.1.1.2 root 929: } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
930: if (!options->value.s || !strcmp(options->value.s, "off")) {
931: prealloc = 0;
932: } else if (!strcmp(options->value.s, "metadata")) {
933: prealloc = 1;
934: } else {
935: fprintf(stderr, "Invalid preallocation mode: '%s'\n",
936: options->value.s);
937: return -EINVAL;
938: }
1.1 root 939: }
940: options++;
941: }
942:
1.1.1.2 root 943: if (backing_file && prealloc) {
944: fprintf(stderr, "Backing file and preallocation cannot be used at "
945: "the same time\n");
946: return -EINVAL;
947: }
948:
1.1 root 949: return qcow_create2(filename, sectors, backing_file, backing_fmt, flags,
1.1.1.2 root 950: cluster_size, prealloc);
1.1 root 951: }
952:
953: static int qcow_make_empty(BlockDriverState *bs)
954: {
955: #if 0
956: /* XXX: not correct */
957: BDRVQcowState *s = bs->opaque;
958: uint32_t l1_length = s->l1_size * sizeof(uint64_t);
959: int ret;
960:
961: memset(s->l1_table, 0, l1_length);
962: if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
963: return -1;
964: ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
965: if (ret < 0)
966: return ret;
967:
968: l2_cache_reset(bs);
969: #endif
970: return 0;
971: }
972:
973: /* XXX: put compressed sectors first, then all the cluster aligned
974: tables to avoid losing bytes in alignment */
975: static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
976: const uint8_t *buf, int nb_sectors)
977: {
978: BDRVQcowState *s = bs->opaque;
979: z_stream strm;
980: int ret, out_len;
981: uint8_t *out_buf;
982: uint64_t cluster_offset;
983:
984: if (nb_sectors == 0) {
985: /* align end of file to a sector boundary to ease reading with
986: sector based I/Os */
987: cluster_offset = bdrv_getlength(s->hd);
988: cluster_offset = (cluster_offset + 511) & ~511;
989: bdrv_truncate(s->hd, cluster_offset);
990: return 0;
991: }
992:
993: if (nb_sectors != s->cluster_sectors)
994: return -EINVAL;
995:
996: out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
997:
998: /* best compression, small window, no zlib header */
999: memset(&strm, 0, sizeof(strm));
1000: ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1001: Z_DEFLATED, -12,
1002: 9, Z_DEFAULT_STRATEGY);
1003: if (ret != 0) {
1004: qemu_free(out_buf);
1005: return -1;
1006: }
1007:
1008: strm.avail_in = s->cluster_size;
1009: strm.next_in = (uint8_t *)buf;
1010: strm.avail_out = s->cluster_size;
1011: strm.next_out = out_buf;
1012:
1013: ret = deflate(&strm, Z_FINISH);
1014: if (ret != Z_STREAM_END && ret != Z_OK) {
1015: qemu_free(out_buf);
1016: deflateEnd(&strm);
1017: return -1;
1018: }
1019: out_len = strm.next_out - out_buf;
1020:
1021: deflateEnd(&strm);
1022:
1023: if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1024: /* could not compress: write normal cluster */
1025: bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1026: } else {
1027: cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1028: sector_num << 9, out_len);
1029: if (!cluster_offset)
1030: return -1;
1031: cluster_offset &= s->cluster_offset_mask;
1032: if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
1033: qemu_free(out_buf);
1034: return -1;
1035: }
1036: }
1037:
1038: qemu_free(out_buf);
1039: return 0;
1040: }
1041:
1042: static void qcow_flush(BlockDriverState *bs)
1043: {
1044: BDRVQcowState *s = bs->opaque;
1045: bdrv_flush(s->hd);
1046: }
1047:
1048: static int64_t qcow_vm_state_offset(BDRVQcowState *s)
1049: {
1050: return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
1051: }
1052:
1053: static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1054: {
1055: BDRVQcowState *s = bs->opaque;
1056: bdi->cluster_size = s->cluster_size;
1057: bdi->vm_state_offset = qcow_vm_state_offset(s);
1058: return 0;
1059: }
1060:
1061:
1062: static int qcow_check(BlockDriverState *bs)
1063: {
1064: return qcow2_check_refcounts(bs);
1065: }
1066:
1067: #if 0
1068: static void dump_refcounts(BlockDriverState *bs)
1069: {
1070: BDRVQcowState *s = bs->opaque;
1071: int64_t nb_clusters, k, k1, size;
1072: int refcount;
1073:
1074: size = bdrv_getlength(s->hd);
1075: nb_clusters = size_to_clusters(s, size);
1076: for(k = 0; k < nb_clusters;) {
1077: k1 = k;
1078: refcount = get_refcount(bs, k);
1079: k++;
1080: while (k < nb_clusters && get_refcount(bs, k) == refcount)
1081: k++;
1082: printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
1083: }
1084: }
1085: #endif
1086:
1087: static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1088: int64_t pos, int size)
1089: {
1090: BDRVQcowState *s = bs->opaque;
1091: int growable = bs->growable;
1.1.1.3 root 1092: int ret;
1.1 root 1093:
1094: bs->growable = 1;
1.1.1.3 root 1095: ret = bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size);
1.1 root 1096: bs->growable = growable;
1097:
1.1.1.3 root 1098: return ret;
1.1 root 1099: }
1100:
1101: static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1102: int64_t pos, int size)
1103: {
1104: BDRVQcowState *s = bs->opaque;
1105: int growable = bs->growable;
1106: int ret;
1107:
1108: bs->growable = 1;
1109: ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size);
1110: bs->growable = growable;
1111:
1112: return ret;
1113: }
1114:
1115: static QEMUOptionParameter qcow_create_options[] = {
1116: {
1117: .name = BLOCK_OPT_SIZE,
1118: .type = OPT_SIZE,
1119: .help = "Virtual disk size"
1120: },
1121: {
1122: .name = BLOCK_OPT_BACKING_FILE,
1123: .type = OPT_STRING,
1124: .help = "File name of a base image"
1125: },
1126: {
1127: .name = BLOCK_OPT_BACKING_FMT,
1128: .type = OPT_STRING,
1129: .help = "Image format of the base image"
1130: },
1131: {
1132: .name = BLOCK_OPT_ENCRYPT,
1133: .type = OPT_FLAG,
1134: .help = "Encrypt the image"
1135: },
1136: {
1137: .name = BLOCK_OPT_CLUSTER_SIZE,
1138: .type = OPT_SIZE,
1139: .help = "qcow2 cluster size"
1140: },
1.1.1.2 root 1141: {
1142: .name = BLOCK_OPT_PREALLOC,
1143: .type = OPT_STRING,
1144: .help = "Preallocation mode (allowed values: off, metadata)"
1145: },
1.1 root 1146: { NULL }
1147: };
1148:
1149: static BlockDriver bdrv_qcow2 = {
1150: .format_name = "qcow2",
1151: .instance_size = sizeof(BDRVQcowState),
1152: .bdrv_probe = qcow_probe,
1153: .bdrv_open = qcow_open,
1154: .bdrv_close = qcow_close,
1155: .bdrv_create = qcow_create,
1156: .bdrv_flush = qcow_flush,
1157: .bdrv_is_allocated = qcow_is_allocated,
1158: .bdrv_set_key = qcow_set_key,
1159: .bdrv_make_empty = qcow_make_empty,
1160:
1161: .bdrv_aio_readv = qcow_aio_readv,
1162: .bdrv_aio_writev = qcow_aio_writev,
1163: .bdrv_write_compressed = qcow_write_compressed,
1164:
1165: .bdrv_snapshot_create = qcow2_snapshot_create,
1166: .bdrv_snapshot_goto = qcow2_snapshot_goto,
1167: .bdrv_snapshot_delete = qcow2_snapshot_delete,
1168: .bdrv_snapshot_list = qcow2_snapshot_list,
1169: .bdrv_get_info = qcow_get_info,
1170:
1171: .bdrv_save_vmstate = qcow_save_vmstate,
1172: .bdrv_load_vmstate = qcow_load_vmstate,
1173:
1174: .create_options = qcow_create_options,
1175: .bdrv_check = qcow_check,
1176: };
1177:
1178: static void bdrv_qcow2_init(void)
1179: {
1180: bdrv_register(&bdrv_qcow2);
1181: }
1182:
1183: block_init(bdrv_qcow2_init);
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.