|
|
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:
25: #ifndef BLOCK_QCOW2_H
26: #define BLOCK_QCOW2_H
27:
28: #include "aes.h"
1.1.1.7 ! root 29: #include "qemu-coroutine.h"
1.1 root 30:
31: //#define DEBUG_ALLOC
32: //#define DEBUG_ALLOC2
33: //#define DEBUG_EXT
34:
35: #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
36: #define QCOW_VERSION 2
37:
38: #define QCOW_CRYPT_NONE 0
39: #define QCOW_CRYPT_AES 1
40:
41: #define QCOW_MAX_CRYPT_CLUSTERS 32
42:
43: /* indicate that the refcount of the referenced cluster is exactly one. */
44: #define QCOW_OFLAG_COPIED (1LL << 63)
45: /* indicate that the cluster is compressed (they never have the copied flag) */
46: #define QCOW_OFLAG_COMPRESSED (1LL << 62)
47:
48: #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
49:
50: #define MIN_CLUSTER_BITS 9
1.1.1.2 root 51: #define MAX_CLUSTER_BITS 21
1.1 root 52:
53: #define L2_CACHE_SIZE 16
54:
1.1.1.5 root 55: /* Must be at least 4 to cover all cases of refcount table growth */
56: #define REFCOUNT_CACHE_SIZE 4
57:
1.1.1.6 root 58: #define DEFAULT_CLUSTER_SIZE 65536
59:
1.1 root 60: typedef struct QCowHeader {
61: uint32_t magic;
62: uint32_t version;
63: uint64_t backing_file_offset;
64: uint32_t backing_file_size;
65: uint32_t cluster_bits;
66: uint64_t size; /* in bytes */
67: uint32_t crypt_method;
68: uint32_t l1_size; /* XXX: save number of clusters instead ? */
69: uint64_t l1_table_offset;
70: uint64_t refcount_table_offset;
71: uint32_t refcount_table_clusters;
72: uint32_t nb_snapshots;
73: uint64_t snapshots_offset;
74: } QCowHeader;
75:
76: typedef struct QCowSnapshot {
77: uint64_t l1_table_offset;
78: uint32_t l1_size;
79: char *id_str;
80: char *name;
81: uint32_t vm_state_size;
82: uint32_t date_sec;
83: uint32_t date_nsec;
84: uint64_t vm_clock_nsec;
85: } QCowSnapshot;
86:
1.1.1.5 root 87: struct Qcow2Cache;
88: typedef struct Qcow2Cache Qcow2Cache;
89:
1.1 root 90: typedef struct BDRVQcowState {
91: int cluster_bits;
92: int cluster_size;
93: int cluster_sectors;
94: int l2_bits;
95: int l2_size;
96: int l1_size;
97: int l1_vm_state_index;
98: int csize_shift;
99: int csize_mask;
100: uint64_t cluster_offset_mask;
101: uint64_t l1_table_offset;
102: uint64_t *l1_table;
1.1.1.5 root 103:
104: Qcow2Cache* l2_table_cache;
105: Qcow2Cache* refcount_block_cache;
106:
1.1 root 107: uint8_t *cluster_cache;
108: uint8_t *cluster_data;
109: uint64_t cluster_cache_offset;
1.1.1.2 root 110: QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
1.1 root 111:
112: uint64_t *refcount_table;
113: uint64_t refcount_table_offset;
114: uint32_t refcount_table_size;
115: int64_t free_cluster_index;
116: int64_t free_byte_offset;
117:
1.1.1.7 ! root 118: CoMutex lock;
! 119:
1.1 root 120: uint32_t crypt_method; /* current crypt method, 0 if no key yet */
121: uint32_t crypt_method_header;
122: AES_KEY aes_encrypt_key;
123: AES_KEY aes_decrypt_key;
124: uint64_t snapshots_offset;
125: int snapshots_size;
126: int nb_snapshots;
127: QCowSnapshot *snapshots;
1.1.1.7 ! root 128:
! 129: int flags;
1.1 root 130: } BDRVQcowState;
131:
132: /* XXX: use std qcow open function ? */
133: typedef struct QCowCreateState {
134: int cluster_size;
135: int cluster_bits;
136: uint16_t *refcount_block;
137: uint64_t *refcount_table;
138: int64_t l1_table_offset;
139: int64_t refcount_table_offset;
140: int64_t refcount_block_offset;
141: } QCowCreateState;
142:
143: struct QCowAIOCB;
144:
145: /* XXX This could be private for qcow2-cluster.c */
146: typedef struct QCowL2Meta
147: {
148: uint64_t offset;
1.1.1.3 root 149: uint64_t cluster_offset;
1.1 root 150: int n_start;
151: int nb_available;
152: int nb_clusters;
1.1.1.7 ! root 153: CoQueue dependent_requests;
1.1 root 154:
1.1.1.2 root 155: QLIST_ENTRY(QCowL2Meta) next_in_flight;
1.1 root 156: } QCowL2Meta;
157:
158: static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
159: {
160: return (size + (s->cluster_size - 1)) >> s->cluster_bits;
161: }
162:
1.1.1.4 root 163: static inline int size_to_l1(BDRVQcowState *s, int64_t size)
164: {
165: int shift = s->cluster_bits + s->l2_bits;
166: return (size + (1ULL << shift) - 1) >> shift;
167: }
168:
1.1 root 169: static inline int64_t align_offset(int64_t offset, int n)
170: {
171: offset = (offset + n - 1) & ~(n - 1);
172: return offset;
173: }
174:
175:
176: // FIXME Need qcow2_ prefix to global functions
177:
178: /* qcow2.c functions */
1.1.1.5 root 179: int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
180: int64_t sector_num, int nb_sectors);
1.1 root 181:
182: /* qcow2-refcount.c functions */
183: int qcow2_refcount_init(BlockDriverState *bs);
184: void qcow2_refcount_close(BlockDriverState *bs);
185:
186: int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
187: int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
188: void qcow2_free_clusters(BlockDriverState *bs,
189: int64_t offset, int64_t size);
190: void qcow2_free_any_clusters(BlockDriverState *bs,
191: uint64_t cluster_offset, int nb_clusters);
192:
193: int qcow2_update_snapshot_refcount(BlockDriverState *bs,
194: int64_t l1_table_offset, int l1_size, int addend);
195:
1.1.1.4 root 196: int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res);
1.1 root 197:
198: /* qcow2-cluster.c functions */
1.1.1.5 root 199: int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
1.1 root 200: void qcow2_l2_cache_reset(BlockDriverState *bs);
1.1.1.4 root 201: int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
1.1 root 202: void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
203: uint8_t *out_buf, const uint8_t *in_buf,
204: int nb_sectors, int enc,
205: const AES_KEY *key);
206:
1.1.1.4 root 207: int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
208: int *num, uint64_t *cluster_offset);
1.1.1.3 root 209: int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
210: int n_start, int n_end, int *num, QCowL2Meta *m);
1.1 root 211: uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
212: uint64_t offset,
213: int compressed_size);
214:
1.1.1.3 root 215: int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
1.1.1.5 root 216: int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
217: int nb_sectors);
1.1 root 218:
219: /* qcow2-snapshot.c functions */
220: int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
221: int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
222: int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
223: int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
1.1.1.5 root 224: int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
1.1 root 225:
226: void qcow2_free_snapshots(BlockDriverState *bs);
227: int qcow2_read_snapshots(BlockDriverState *bs);
228:
1.1.1.5 root 229: /* qcow2-cache.c functions */
230: Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
231: bool writethrough);
232: int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
1.1.1.6 root 233: bool qcow2_cache_set_writethrough(BlockDriverState *bs, Qcow2Cache *c,
234: bool enable);
1.1.1.5 root 235:
236: void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
237: int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
238: int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
239: Qcow2Cache *dependency);
240: void qcow2_cache_depends_on_flush(Qcow2Cache *c);
241:
242: int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
243: void **table);
244: int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
245: void **table);
246: int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
247:
1.1 root 248: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.