qcow2: Split out snapshot functions
[armpft.git] / block / qcow2.h
blob397f21d3b39d6fd0ce452f2419a91e1c69c396b3
1 /*
2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #ifndef BLOCK_QCOW2_H
26 #define BLOCK_QCOW2_H
28 #include "aes.h"
30 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
31 #define QCOW_VERSION 2
33 #define QCOW_CRYPT_NONE 0
34 #define QCOW_CRYPT_AES 1
36 #define QCOW_MAX_CRYPT_CLUSTERS 32
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)
43 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
45 #define MIN_CLUSTER_BITS 9
46 #define MAX_CLUSTER_BITS 16
48 #define L2_CACHE_SIZE 16
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;
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;
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;
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;
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;
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;
127 /* XXX This could be private for qcow2-cluster.c */
128 typedef struct QCowL2Meta
130 uint64_t offset;
131 int n_start;
132 int nb_available;
133 int nb_clusters;
134 } QCowL2Meta;
136 static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
138 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
141 static inline int64_t align_offset(int64_t offset, int n)
143 offset = (offset + n - 1) & ~(n - 1);
144 return offset;
148 // FIXME Need qcow2_ prefix to global functions
150 /* qcow2.c functions */
151 void l2_cache_reset(BlockDriverState *bs);
152 int backing_read1(BlockDriverState *bs,
153 int64_t sector_num, uint8_t *buf, int nb_sectors);
155 /* qcow2-refcount.c functions */
156 int refcount_init(BlockDriverState *bs);
157 void refcount_close(BlockDriverState *bs);
159 int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
160 int64_t alloc_bytes(BlockDriverState *bs, int size);
161 void free_clusters(BlockDriverState *bs,
162 int64_t offset, int64_t size);
163 void free_any_clusters(BlockDriverState *bs,
164 uint64_t cluster_offset, int nb_clusters);
166 void create_refcount_update(QCowCreateState *s, int64_t offset, int64_t size);
167 int update_snapshot_refcount(BlockDriverState *bs,
168 int64_t l1_table_offset,
169 int l1_size,
170 int addend);
172 int check_refcounts(BlockDriverState *bs);
174 /* qcow2-cluster.c functions */
175 int grow_l1_table(BlockDriverState *bs, int min_size);
176 int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
177 void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
178 uint8_t *out_buf, const uint8_t *in_buf,
179 int nb_sectors, int enc,
180 const AES_KEY *key);
182 uint64_t get_cluster_offset(BlockDriverState *bs, uint64_t offset, int *num);
183 uint64_t alloc_cluster_offset(BlockDriverState *bs,
184 uint64_t offset,
185 int n_start, int n_end,
186 int *num, QCowL2Meta *m);
187 uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
188 uint64_t offset,
189 int compressed_size);
191 int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
192 QCowL2Meta *m);
194 /* qcow2-snapshot.c functions */
195 int qcow_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
196 int qcow_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
197 int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
198 int qcow_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
200 void qcow_free_snapshots(BlockDriverState *bs);
201 int qcow_read_snapshots(BlockDriverState *bs);
203 #endif