blob: c99b374bd03c44829ecdf694439c4a0d52f5dc53 [file] [log] [blame]
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001/*
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
25#ifndef BLOCK_QCOW2_H
26#define BLOCK_QCOW2_H
27
28#include "aes.h"
29
Filip Navara14899cd2009-06-23 19:17:30 -050030//#define DEBUG_ALLOC
31//#define DEBUG_ALLOC2
32//#define DEBUG_EXT
33
Kevin Wolff7d0fe02009-05-28 16:07:04 +020034#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
35#define QCOW_VERSION 2
36
37#define QCOW_CRYPT_NONE 0
38#define QCOW_CRYPT_AES 1
39
40#define QCOW_MAX_CRYPT_CLUSTERS 32
41
42/* indicate that the refcount of the referenced cluster is exactly one. */
43#define QCOW_OFLAG_COPIED (1LL << 63)
44/* indicate that the cluster is compressed (they never have the copied flag) */
45#define QCOW_OFLAG_COMPRESSED (1LL << 62)
46
47#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
48
49#define MIN_CLUSTER_BITS 9
50#define MAX_CLUSTER_BITS 16
51
52#define L2_CACHE_SIZE 16
53
54typedef struct QCowHeader {
55 uint32_t magic;
56 uint32_t version;
57 uint64_t backing_file_offset;
58 uint32_t backing_file_size;
59 uint32_t cluster_bits;
60 uint64_t size; /* in bytes */
61 uint32_t crypt_method;
62 uint32_t l1_size; /* XXX: save number of clusters instead ? */
63 uint64_t l1_table_offset;
64 uint64_t refcount_table_offset;
65 uint32_t refcount_table_clusters;
66 uint32_t nb_snapshots;
67 uint64_t snapshots_offset;
68} QCowHeader;
69
70typedef struct QCowSnapshot {
71 uint64_t l1_table_offset;
72 uint32_t l1_size;
73 char *id_str;
74 char *name;
75 uint32_t vm_state_size;
76 uint32_t date_sec;
77 uint32_t date_nsec;
78 uint64_t vm_clock_nsec;
79} QCowSnapshot;
80
81typedef struct BDRVQcowState {
82 BlockDriverState *hd;
83 int cluster_bits;
84 int cluster_size;
85 int cluster_sectors;
86 int l2_bits;
87 int l2_size;
88 int l1_size;
89 int l1_vm_state_index;
90 int csize_shift;
91 int csize_mask;
92 uint64_t cluster_offset_mask;
93 uint64_t l1_table_offset;
94 uint64_t *l1_table;
95 uint64_t *l2_cache;
96 uint64_t l2_cache_offsets[L2_CACHE_SIZE];
97 uint32_t l2_cache_counts[L2_CACHE_SIZE];
98 uint8_t *cluster_cache;
99 uint8_t *cluster_data;
100 uint64_t cluster_cache_offset;
101
102 uint64_t *refcount_table;
103 uint64_t refcount_table_offset;
104 uint32_t refcount_table_size;
105 uint64_t refcount_block_cache_offset;
106 uint16_t *refcount_block_cache;
107 int64_t free_cluster_index;
108 int64_t free_byte_offset;
109
110 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
111 uint32_t crypt_method_header;
112 AES_KEY aes_encrypt_key;
113 AES_KEY aes_decrypt_key;
114 uint64_t snapshots_offset;
115 int snapshots_size;
116 int nb_snapshots;
117 QCowSnapshot *snapshots;
118} BDRVQcowState;
119
120/* XXX: use std qcow open function ? */
121typedef struct QCowCreateState {
122 int cluster_size;
123 int cluster_bits;
124 uint16_t *refcount_block;
125 uint64_t *refcount_table;
126 int64_t l1_table_offset;
127 int64_t refcount_table_offset;
128 int64_t refcount_block_offset;
129} QCowCreateState;
130
Kevin Wolf45aba422009-05-28 16:07:05 +0200131/* XXX This could be private for qcow2-cluster.c */
132typedef struct QCowL2Meta
133{
134 uint64_t offset;
135 int n_start;
136 int nb_available;
137 int nb_clusters;
138} QCowL2Meta;
139
140static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200141{
142 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
143}
144
Kevin Wolfc1424422009-05-28 16:07:06 +0200145static inline int64_t align_offset(int64_t offset, int n)
146{
147 offset = (offset + n - 1) & ~(n - 1);
148 return offset;
149}
150
151
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200152// FIXME Need qcow2_ prefix to global functions
153
154/* qcow2.c functions */
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200155int qcow2_backing_read1(BlockDriverState *bs,
Kevin Wolf45aba422009-05-28 16:07:05 +0200156 int64_t sector_num, uint8_t *buf, int nb_sectors);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200157
158/* qcow2-refcount.c functions */
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200159int qcow2_refcount_init(BlockDriverState *bs);
160void qcow2_refcount_close(BlockDriverState *bs);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200161
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200162int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
163int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
164void qcow2_free_clusters(BlockDriverState *bs,
Kevin Wolf45aba422009-05-28 16:07:05 +0200165 int64_t offset, int64_t size);
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200166void qcow2_free_any_clusters(BlockDriverState *bs,
Kevin Wolf45aba422009-05-28 16:07:05 +0200167 uint64_t cluster_offset, int nb_clusters);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200168
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200169void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
170 int64_t size);
171int qcow2_update_snapshot_refcount(BlockDriverState *bs,
172 int64_t l1_table_offset, int l1_size, int addend);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200173
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200174int qcow2_check_refcounts(BlockDriverState *bs);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200175
Kevin Wolf45aba422009-05-28 16:07:05 +0200176/* qcow2-cluster.c functions */
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200177int qcow2_grow_l1_table(BlockDriverState *bs, int min_size);
178void qcow2_l2_cache_reset(BlockDriverState *bs);
179int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
180void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
Kevin Wolf45aba422009-05-28 16:07:05 +0200181 uint8_t *out_buf, const uint8_t *in_buf,
182 int nb_sectors, int enc,
183 const AES_KEY *key);
184
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200185uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
186 int *num);
187uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs,
Kevin Wolf45aba422009-05-28 16:07:05 +0200188 uint64_t offset,
189 int n_start, int n_end,
190 int *num, QCowL2Meta *m);
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200191uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
Kevin Wolf45aba422009-05-28 16:07:05 +0200192 uint64_t offset,
193 int compressed_size);
194
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200195int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
Kevin Wolf45aba422009-05-28 16:07:05 +0200196 QCowL2Meta *m);
197
Kevin Wolfc1424422009-05-28 16:07:06 +0200198/* qcow2-snapshot.c functions */
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200199int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
200int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
201int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
202int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
Kevin Wolfc1424422009-05-28 16:07:06 +0200203
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200204void qcow2_free_snapshots(BlockDriverState *bs);
205int qcow2_read_snapshots(BlockDriverState *bs);
Kevin Wolfc1424422009-05-28 16:07:06 +0200206
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200207#endif