3a34212181335c7d30bb5e660f67857f43fbed70
[qemu] / block / qcow2.h
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
25 #ifndef BLOCK_QCOW2_H
26 #define BLOCK_QCOW2_H
27
28 #include "aes.h"
29
30 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
31 #define QCOW_VERSION 2
32
33 #define QCOW_CRYPT_NONE 0
34 #define QCOW_CRYPT_AES  1
35
36 #define QCOW_MAX_CRYPT_CLUSTERS 32
37
38 /* indicate that the refcount of the referenced cluster is exactly one. */
39 #define QCOW_OFLAG_COPIED     (1LL << 63)
40 /* indicate that the cluster is compressed (they never have the copied flag) */
41 #define QCOW_OFLAG_COMPRESSED (1LL << 62)
42
43 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
44
45 #define MIN_CLUSTER_BITS 9
46 #define MAX_CLUSTER_BITS 16
47
48 #define L2_CACHE_SIZE 16
49
50 typedef struct QCowHeader {
51     uint32_t magic;
52     uint32_t version;
53     uint64_t backing_file_offset;
54     uint32_t backing_file_size;
55     uint32_t cluster_bits;
56     uint64_t size; /* in bytes */
57     uint32_t crypt_method;
58     uint32_t l1_size; /* XXX: save number of clusters instead ? */
59     uint64_t l1_table_offset;
60     uint64_t refcount_table_offset;
61     uint32_t refcount_table_clusters;
62     uint32_t nb_snapshots;
63     uint64_t snapshots_offset;
64 } QCowHeader;
65
66 typedef struct QCowSnapshot {
67     uint64_t l1_table_offset;
68     uint32_t l1_size;
69     char *id_str;
70     char *name;
71     uint32_t vm_state_size;
72     uint32_t date_sec;
73     uint32_t date_nsec;
74     uint64_t vm_clock_nsec;
75 } QCowSnapshot;
76
77 typedef struct BDRVQcowState {
78     BlockDriverState *hd;
79     int cluster_bits;
80     int cluster_size;
81     int cluster_sectors;
82     int l2_bits;
83     int l2_size;
84     int l1_size;
85     int l1_vm_state_index;
86     int csize_shift;
87     int csize_mask;
88     uint64_t cluster_offset_mask;
89     uint64_t l1_table_offset;
90     uint64_t *l1_table;
91     uint64_t *l2_cache;
92     uint64_t l2_cache_offsets[L2_CACHE_SIZE];
93     uint32_t l2_cache_counts[L2_CACHE_SIZE];
94     uint8_t *cluster_cache;
95     uint8_t *cluster_data;
96     uint64_t cluster_cache_offset;
97
98     uint64_t *refcount_table;
99     uint64_t refcount_table_offset;
100     uint32_t refcount_table_size;
101     uint64_t refcount_block_cache_offset;
102     uint16_t *refcount_block_cache;
103     int64_t free_cluster_index;
104     int64_t free_byte_offset;
105
106     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
107     uint32_t crypt_method_header;
108     AES_KEY aes_encrypt_key;
109     AES_KEY aes_decrypt_key;
110     uint64_t snapshots_offset;
111     int snapshots_size;
112     int nb_snapshots;
113     QCowSnapshot *snapshots;
114 } BDRVQcowState;
115
116 /* XXX: use std qcow open function ? */
117 typedef struct QCowCreateState {
118     int cluster_size;
119     int cluster_bits;
120     uint16_t *refcount_block;
121     uint64_t *refcount_table;
122     int64_t l1_table_offset;
123     int64_t refcount_table_offset;
124     int64_t refcount_block_offset;
125 } QCowCreateState;
126
127 static int size_to_clusters(BDRVQcowState *s, int64_t size)
128 {
129     return (size + (s->cluster_size - 1)) >> s->cluster_bits;
130 }
131
132 // FIXME Need qcow2_ prefix to global functions
133
134 /* qcow2.c functions */
135 void l2_cache_reset(BlockDriverState *bs);
136
137 /* qcow2-refcount.c functions */
138 int refcount_init(BlockDriverState *bs);
139 void refcount_close(BlockDriverState *bs);
140
141 int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
142 int64_t alloc_bytes(BlockDriverState *bs, int size);
143 void free_clusters(BlockDriverState *bs,
144                           int64_t offset, int64_t size);
145
146 void create_refcount_update(QCowCreateState *s, int64_t offset, int64_t size);
147 int update_snapshot_refcount(BlockDriverState *bs,
148                              int64_t l1_table_offset,
149                              int l1_size,
150                              int addend);
151
152 int check_refcounts(BlockDriverState *bs);
153
154 #endif