Revision ea2384d3
b/block-cow.c | ||
---|---|---|
1 |
/* |
|
2 |
* Block driver for the COW format |
|
3 |
* |
|
4 |
* Copyright (c) 2004 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 |
#ifndef _WIN32 |
|
25 |
#include "vl.h" |
|
26 |
#include "block_int.h" |
|
27 |
#include <sys/mman.h> |
|
28 |
|
|
29 |
/**************************************************************/ |
|
30 |
/* COW block driver using file system holes */ |
|
31 |
|
|
32 |
/* user mode linux compatible COW file */ |
|
33 |
#define COW_MAGIC 0x4f4f4f4d /* MOOO */ |
|
34 |
#define COW_VERSION 2 |
|
35 |
|
|
36 |
struct cow_header_v2 { |
|
37 |
uint32_t magic; |
|
38 |
uint32_t version; |
|
39 |
char backing_file[1024]; |
|
40 |
int32_t mtime; |
|
41 |
uint64_t size; |
|
42 |
uint32_t sectorsize; |
|
43 |
}; |
|
44 |
|
|
45 |
typedef struct BDRVCowState { |
|
46 |
int fd; |
|
47 |
uint8_t *cow_bitmap; /* if non NULL, COW mappings are used first */ |
|
48 |
uint8_t *cow_bitmap_addr; /* mmap address of cow_bitmap */ |
|
49 |
int cow_bitmap_size; |
|
50 |
int64_t cow_sectors_offset; |
|
51 |
} BDRVCowState; |
|
52 |
|
|
53 |
static int cow_probe(const uint8_t *buf, int buf_size, const char *filename) |
|
54 |
{ |
|
55 |
const struct cow_header_v2 *cow_header = (const void *)buf; |
|
56 |
|
|
57 |
if (be32_to_cpu(cow_header->magic) == COW_MAGIC && |
|
58 |
be32_to_cpu(cow_header->version) == COW_VERSION) |
|
59 |
return 100; |
|
60 |
else |
|
61 |
return 0; |
|
62 |
} |
|
63 |
|
|
64 |
static int cow_open(BlockDriverState *bs, const char *filename) |
|
65 |
{ |
|
66 |
BDRVCowState *s = bs->opaque; |
|
67 |
int fd; |
|
68 |
struct cow_header_v2 cow_header; |
|
69 |
int64_t size; |
|
70 |
|
|
71 |
fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
|
72 |
if (fd < 0) { |
|
73 |
fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
|
74 |
if (fd < 0) |
|
75 |
return -1; |
|
76 |
} |
|
77 |
s->fd = fd; |
|
78 |
/* see if it is a cow image */ |
|
79 |
if (read(fd, &cow_header, sizeof(cow_header)) != sizeof(cow_header)) { |
|
80 |
goto fail; |
|
81 |
} |
|
82 |
|
|
83 |
if (be32_to_cpu(cow_header.magic) != COW_MAGIC || |
|
84 |
be32_to_cpu(cow_header.version) != COW_VERSION) { |
|
85 |
goto fail; |
|
86 |
} |
|
87 |
|
|
88 |
/* cow image found */ |
|
89 |
size = be64_to_cpu(cow_header.size); |
|
90 |
bs->total_sectors = size / 512; |
|
91 |
|
|
92 |
pstrcpy(bs->backing_file, sizeof(bs->backing_file), |
|
93 |
cow_header.backing_file); |
|
94 |
|
|
95 |
#if 0 |
|
96 |
if (cow_header.backing_file[0] != '\0') { |
|
97 |
if (stat(cow_header.backing_file, &st) != 0) { |
|
98 |
fprintf(stderr, "%s: could not find original disk image '%s'\n", filename, cow_header.backing_file); |
|
99 |
goto fail; |
|
100 |
} |
|
101 |
if (st.st_mtime != be32_to_cpu(cow_header.mtime)) { |
|
102 |
fprintf(stderr, "%s: original raw disk image '%s' does not match saved timestamp\n", filename, cow_header.backing_file); |
|
103 |
goto fail; |
|
104 |
} |
|
105 |
fd = open(cow_header.backing_file, O_RDONLY | O_LARGEFILE); |
|
106 |
if (fd < 0) |
|
107 |
goto fail; |
|
108 |
bs->fd = fd; |
|
109 |
} |
|
110 |
#endif |
|
111 |
/* mmap the bitmap */ |
|
112 |
s->cow_bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header); |
|
113 |
s->cow_bitmap_addr = mmap(get_mmap_addr(s->cow_bitmap_size), |
|
114 |
s->cow_bitmap_size, |
|
115 |
PROT_READ | PROT_WRITE, |
|
116 |
MAP_SHARED, s->fd, 0); |
|
117 |
if (s->cow_bitmap_addr == MAP_FAILED) |
|
118 |
goto fail; |
|
119 |
s->cow_bitmap = s->cow_bitmap_addr + sizeof(cow_header); |
|
120 |
s->cow_sectors_offset = (s->cow_bitmap_size + 511) & ~511; |
|
121 |
return 0; |
|
122 |
fail: |
|
123 |
close(fd); |
|
124 |
return -1; |
|
125 |
} |
|
126 |
|
|
127 |
static inline void set_bit(uint8_t *bitmap, int64_t bitnum) |
|
128 |
{ |
|
129 |
bitmap[bitnum / 8] |= (1 << (bitnum%8)); |
|
130 |
} |
|
131 |
|
|
132 |
static inline int is_bit_set(const uint8_t *bitmap, int64_t bitnum) |
|
133 |
{ |
|
134 |
return !!(bitmap[bitnum / 8] & (1 << (bitnum%8))); |
|
135 |
} |
|
136 |
|
|
137 |
|
|
138 |
/* Return true if first block has been changed (ie. current version is |
|
139 |
* in COW file). Set the number of continuous blocks for which that |
|
140 |
* is true. */ |
|
141 |
static inline int is_changed(uint8_t *bitmap, |
|
142 |
int64_t sector_num, int nb_sectors, |
|
143 |
int *num_same) |
|
144 |
{ |
|
145 |
int changed; |
|
146 |
|
|
147 |
if (!bitmap || nb_sectors == 0) { |
|
148 |
*num_same = nb_sectors; |
|
149 |
return 0; |
|
150 |
} |
|
151 |
|
|
152 |
changed = is_bit_set(bitmap, sector_num); |
|
153 |
for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) { |
|
154 |
if (is_bit_set(bitmap, sector_num + *num_same) != changed) |
|
155 |
break; |
|
156 |
} |
|
157 |
|
|
158 |
return changed; |
|
159 |
} |
|
160 |
|
|
161 |
static int cow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
|
162 |
int nb_sectors, int *pnum) |
|
163 |
{ |
|
164 |
BDRVCowState *s = bs->opaque; |
|
165 |
return is_changed(s->cow_bitmap, sector_num, nb_sectors, pnum); |
|
166 |
} |
|
167 |
|
|
168 |
static int cow_read(BlockDriverState *bs, int64_t sector_num, |
|
169 |
uint8_t *buf, int nb_sectors) |
|
170 |
{ |
|
171 |
BDRVCowState *s = bs->opaque; |
|
172 |
int ret, n; |
|
173 |
|
|
174 |
while (nb_sectors > 0) { |
|
175 |
if (is_changed(s->cow_bitmap, sector_num, nb_sectors, &n)) { |
|
176 |
lseek64(s->fd, s->cow_sectors_offset + sector_num * 512, SEEK_SET); |
|
177 |
ret = read(s->fd, buf, n * 512); |
|
178 |
if (ret != n * 512) |
|
179 |
return -1; |
|
180 |
} else { |
|
181 |
memset(buf, 0, n * 512); |
|
182 |
} |
|
183 |
nb_sectors -= n; |
|
184 |
sector_num += n; |
|
185 |
buf += n * 512; |
|
186 |
} |
|
187 |
return 0; |
|
188 |
} |
|
189 |
|
|
190 |
static int cow_write(BlockDriverState *bs, int64_t sector_num, |
|
191 |
const uint8_t *buf, int nb_sectors) |
|
192 |
{ |
|
193 |
BDRVCowState *s = bs->opaque; |
|
194 |
int ret, i; |
|
195 |
|
|
196 |
lseek64(s->fd, s->cow_sectors_offset + sector_num * 512, SEEK_SET); |
|
197 |
ret = write(s->fd, buf, nb_sectors * 512); |
|
198 |
if (ret != nb_sectors * 512) |
|
199 |
return -1; |
|
200 |
for (i = 0; i < nb_sectors; i++) |
|
201 |
set_bit(s->cow_bitmap, sector_num + i); |
|
202 |
return 0; |
|
203 |
} |
|
204 |
|
|
205 |
static int cow_close(BlockDriverState *bs) |
|
206 |
{ |
|
207 |
BDRVCowState *s = bs->opaque; |
|
208 |
munmap(s->cow_bitmap_addr, s->cow_bitmap_size); |
|
209 |
close(s->fd); |
|
210 |
} |
|
211 |
|
|
212 |
static int cow_create(const char *filename, int64_t image_sectors, |
|
213 |
const char *image_filename, int flags) |
|
214 |
{ |
|
215 |
int fd, cow_fd; |
|
216 |
struct cow_header_v2 cow_header; |
|
217 |
struct stat st; |
|
218 |
|
|
219 |
if (flags) |
|
220 |
return -ENOTSUP; |
|
221 |
|
|
222 |
cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
|
223 |
0644); |
|
224 |
if (cow_fd < 0) |
|
225 |
return -1; |
|
226 |
memset(&cow_header, 0, sizeof(cow_header)); |
|
227 |
cow_header.magic = cpu_to_be32(COW_MAGIC); |
|
228 |
cow_header.version = cpu_to_be32(COW_VERSION); |
|
229 |
if (image_filename) { |
|
230 |
fd = open(image_filename, O_RDONLY | O_BINARY); |
|
231 |
if (fd < 0) { |
|
232 |
close(cow_fd); |
|
233 |
return -1; |
|
234 |
} |
|
235 |
if (fstat(fd, &st) != 0) { |
|
236 |
close(fd); |
|
237 |
return -1; |
|
238 |
} |
|
239 |
close(fd); |
|
240 |
cow_header.mtime = cpu_to_be32(st.st_mtime); |
|
241 |
realpath(image_filename, cow_header.backing_file); |
|
242 |
} |
|
243 |
cow_header.sectorsize = cpu_to_be32(512); |
|
244 |
cow_header.size = cpu_to_be64(image_sectors * 512); |
|
245 |
write(cow_fd, &cow_header, sizeof(cow_header)); |
|
246 |
/* resize to include at least all the bitmap */ |
|
247 |
ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3)); |
|
248 |
close(cow_fd); |
|
249 |
return 0; |
|
250 |
} |
|
251 |
|
|
252 |
BlockDriver bdrv_cow = { |
|
253 |
"cow", |
|
254 |
sizeof(BDRVCowState), |
|
255 |
cow_probe, |
|
256 |
cow_open, |
|
257 |
cow_read, |
|
258 |
cow_write, |
|
259 |
cow_close, |
|
260 |
cow_create, |
|
261 |
cow_is_allocated, |
|
262 |
}; |
|
263 |
#endif |
b/block-qcow.c | ||
---|---|---|
1 |
/* |
|
2 |
* Block driver for the QCOW format |
|
3 |
* |
|
4 |
* Copyright (c) 2004 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 "vl.h" |
|
25 |
#include "block_int.h" |
|
26 |
#include "zlib.h" |
|
27 |
#include "aes.h" |
|
28 |
|
|
29 |
/**************************************************************/ |
|
30 |
/* QEMU COW block driver with compression and encryption support */ |
|
31 |
|
|
32 |
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) |
|
33 |
#define QCOW_VERSION 1 |
|
34 |
|
|
35 |
#define QCOW_CRYPT_NONE 0 |
|
36 |
#define QCOW_CRYPT_AES 1 |
|
37 |
|
|
38 |
#define QCOW_OFLAG_COMPRESSED (1LL << 63) |
|
39 |
|
|
40 |
typedef struct QCowHeader { |
|
41 |
uint32_t magic; |
|
42 |
uint32_t version; |
|
43 |
uint64_t backing_file_offset; |
|
44 |
uint32_t backing_file_size; |
|
45 |
uint32_t mtime; |
|
46 |
uint64_t size; /* in bytes */ |
|
47 |
uint8_t cluster_bits; |
|
48 |
uint8_t l2_bits; |
|
49 |
uint32_t crypt_method; |
|
50 |
uint64_t l1_table_offset; |
|
51 |
} QCowHeader; |
|
52 |
|
|
53 |
#define L2_CACHE_SIZE 16 |
|
54 |
|
|
55 |
typedef struct BDRVQcowState { |
|
56 |
int fd; |
|
57 |
int cluster_bits; |
|
58 |
int cluster_size; |
|
59 |
int cluster_sectors; |
|
60 |
int l2_bits; |
|
61 |
int l2_size; |
|
62 |
int l1_size; |
|
63 |
uint64_t cluster_offset_mask; |
|
64 |
uint64_t l1_table_offset; |
|
65 |
uint64_t *l1_table; |
|
66 |
uint64_t *l2_cache; |
|
67 |
uint64_t l2_cache_offsets[L2_CACHE_SIZE]; |
|
68 |
uint32_t l2_cache_counts[L2_CACHE_SIZE]; |
|
69 |
uint8_t *cluster_cache; |
|
70 |
uint8_t *cluster_data; |
|
71 |
uint64_t cluster_cache_offset; |
|
72 |
uint32_t crypt_method; /* current crypt method, 0 if no key yet */ |
|
73 |
uint32_t crypt_method_header; |
|
74 |
AES_KEY aes_encrypt_key; |
|
75 |
AES_KEY aes_decrypt_key; |
|
76 |
} BDRVQcowState; |
|
77 |
|
|
78 |
static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset); |
|
79 |
|
|
80 |
static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) |
|
81 |
{ |
|
82 |
const QCowHeader *cow_header = (const void *)buf; |
|
83 |
|
|
84 |
if (be32_to_cpu(cow_header->magic) == QCOW_MAGIC && |
|
85 |
be32_to_cpu(cow_header->version) == QCOW_VERSION) |
|
86 |
return 100; |
|
87 |
else |
|
88 |
return 0; |
|
89 |
} |
|
90 |
|
|
91 |
static int qcow_open(BlockDriverState *bs, const char *filename) |
|
92 |
{ |
|
93 |
BDRVQcowState *s = bs->opaque; |
|
94 |
int fd, len, i, shift; |
|
95 |
QCowHeader header; |
|
96 |
|
|
97 |
fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
|
98 |
if (fd < 0) { |
|
99 |
fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
|
100 |
if (fd < 0) |
|
101 |
return -1; |
|
102 |
} |
|
103 |
s->fd = fd; |
|
104 |
if (read(fd, &header, sizeof(header)) != sizeof(header)) |
|
105 |
goto fail; |
|
106 |
be32_to_cpus(&header.magic); |
|
107 |
be32_to_cpus(&header.version); |
|
108 |
be64_to_cpus(&header.backing_file_offset); |
|
109 |
be32_to_cpus(&header.backing_file_size); |
|
110 |
be32_to_cpus(&header.mtime); |
|
111 |
be64_to_cpus(&header.size); |
|
112 |
be32_to_cpus(&header.crypt_method); |
|
113 |
be64_to_cpus(&header.l1_table_offset); |
|
114 |
|
|
115 |
if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION) |
|
116 |
goto fail; |
|
117 |
if (header.size <= 1 || header.cluster_bits < 9) |
|
118 |
goto fail; |
|
119 |
if (header.crypt_method > QCOW_CRYPT_AES) |
|
120 |
goto fail; |
|
121 |
s->crypt_method_header = header.crypt_method; |
|
122 |
if (s->crypt_method_header) |
|
123 |
bs->encrypted = 1; |
|
124 |
s->cluster_bits = header.cluster_bits; |
|
125 |
s->cluster_size = 1 << s->cluster_bits; |
|
126 |
s->cluster_sectors = 1 << (s->cluster_bits - 9); |
|
127 |
s->l2_bits = header.l2_bits; |
|
128 |
s->l2_size = 1 << s->l2_bits; |
|
129 |
bs->total_sectors = header.size / 512; |
|
130 |
s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1; |
|
131 |
|
|
132 |
/* read the level 1 table */ |
|
133 |
shift = s->cluster_bits + s->l2_bits; |
|
134 |
s->l1_size = (header.size + (1LL << shift) - 1) >> shift; |
|
135 |
|
|
136 |
s->l1_table_offset = header.l1_table_offset; |
|
137 |
s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t)); |
|
138 |
if (!s->l1_table) |
|
139 |
goto fail; |
|
140 |
lseek64(fd, s->l1_table_offset, SEEK_SET); |
|
141 |
if (read(fd, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
|
142 |
s->l1_size * sizeof(uint64_t)) |
|
143 |
goto fail; |
|
144 |
for(i = 0;i < s->l1_size; i++) { |
|
145 |
be64_to_cpus(&s->l1_table[i]); |
|
146 |
} |
|
147 |
/* alloc L2 cache */ |
|
148 |
s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); |
|
149 |
if (!s->l2_cache) |
|
150 |
goto fail; |
|
151 |
s->cluster_cache = qemu_malloc(s->cluster_size); |
|
152 |
if (!s->cluster_cache) |
|
153 |
goto fail; |
|
154 |
s->cluster_data = qemu_malloc(s->cluster_size); |
|
155 |
if (!s->cluster_data) |
|
156 |
goto fail; |
|
157 |
s->cluster_cache_offset = -1; |
|
158 |
|
|
159 |
/* read the backing file name */ |
|
160 |
if (header.backing_file_offset != 0) { |
|
161 |
len = header.backing_file_size; |
|
162 |
if (len > 1023) |
|
163 |
len = 1023; |
|
164 |
lseek64(fd, header.backing_file_offset, SEEK_SET); |
|
165 |
if (read(fd, bs->backing_file, len) != len) |
|
166 |
goto fail; |
|
167 |
bs->backing_file[len] = '\0'; |
|
168 |
} |
|
169 |
return 0; |
|
170 |
|
|
171 |
fail: |
|
172 |
qemu_free(s->l1_table); |
|
173 |
qemu_free(s->l2_cache); |
|
174 |
qemu_free(s->cluster_cache); |
|
175 |
qemu_free(s->cluster_data); |
|
176 |
close(fd); |
|
177 |
return -1; |
|
178 |
} |
|
179 |
|
|
180 |
static int qcow_set_key(BlockDriverState *bs, const char *key) |
|
181 |
{ |
|
182 |
BDRVQcowState *s = bs->opaque; |
|
183 |
uint8_t keybuf[16]; |
|
184 |
int len, i; |
|
185 |
|
|
186 |
memset(keybuf, 0, 16); |
|
187 |
len = strlen(key); |
|
188 |
if (len > 16) |
|
189 |
len = 16; |
|
190 |
/* XXX: we could compress the chars to 7 bits to increase |
|
191 |
entropy */ |
|
192 |
for(i = 0;i < len;i++) { |
|
193 |
keybuf[i] = key[i]; |
|
194 |
} |
|
195 |
s->crypt_method = s->crypt_method_header; |
|
196 |
|
|
197 |
if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) |
|
198 |
return -1; |
|
199 |
if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) |
|
200 |
return -1; |
|
201 |
#if 0 |
|
202 |
/* test */ |
|
203 |
{ |
|
204 |
uint8_t in[16]; |
|
205 |
uint8_t out[16]; |
|
206 |
uint8_t tmp[16]; |
|
207 |
for(i=0;i<16;i++) |
|
208 |
in[i] = i; |
|
209 |
AES_encrypt(in, tmp, &s->aes_encrypt_key); |
|
210 |
AES_decrypt(tmp, out, &s->aes_decrypt_key); |
|
211 |
for(i = 0; i < 16; i++) |
|
212 |
printf(" %02x", tmp[i]); |
|
213 |
printf("\n"); |
|
214 |
for(i = 0; i < 16; i++) |
|
215 |
printf(" %02x", out[i]); |
|
216 |
printf("\n"); |
|
217 |
} |
|
218 |
#endif |
|
219 |
return 0; |
|
220 |
} |
|
221 |
|
|
222 |
/* The crypt function is compatible with the linux cryptoloop |
|
223 |
algorithm for < 4 GB images. NOTE: out_buf == in_buf is |
|
224 |
supported */ |
|
225 |
static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num, |
|
226 |
uint8_t *out_buf, const uint8_t *in_buf, |
|
227 |
int nb_sectors, int enc, |
|
228 |
const AES_KEY *key) |
|
229 |
{ |
|
230 |
union { |
|
231 |
uint64_t ll[2]; |
|
232 |
uint8_t b[16]; |
|
233 |
} ivec; |
|
234 |
int i; |
|
235 |
|
|
236 |
for(i = 0; i < nb_sectors; i++) { |
|
237 |
ivec.ll[0] = cpu_to_le64(sector_num); |
|
238 |
ivec.ll[1] = 0; |
|
239 |
AES_cbc_encrypt(in_buf, out_buf, 512, key, |
|
240 |
ivec.b, enc); |
|
241 |
sector_num++; |
|
242 |
in_buf += 512; |
|
243 |
out_buf += 512; |
|
244 |
} |
|
245 |
} |
|
246 |
|
|
247 |
/* 'allocate' is: |
|
248 |
* |
|
249 |
* 0 to not allocate. |
|
250 |
* |
|
251 |
* 1 to allocate a normal cluster (for sector indexes 'n_start' to |
|
252 |
* 'n_end') |
|
253 |
* |
|
254 |
* 2 to allocate a compressed cluster of size |
|
255 |
* 'compressed_size'. 'compressed_size' must be > 0 and < |
|
256 |
* cluster_size |
|
257 |
* |
|
258 |
* return 0 if not allocated. |
|
259 |
*/ |
|
260 |
static uint64_t get_cluster_offset(BlockDriverState *bs, |
|
261 |
uint64_t offset, int allocate, |
|
262 |
int compressed_size, |
|
263 |
int n_start, int n_end) |
|
264 |
{ |
|
265 |
BDRVQcowState *s = bs->opaque; |
|
266 |
int min_index, i, j, l1_index, l2_index; |
|
267 |
uint64_t l2_offset, *l2_table, cluster_offset, tmp; |
|
268 |
uint32_t min_count; |
|
269 |
int new_l2_table; |
|
270 |
|
|
271 |
l1_index = offset >> (s->l2_bits + s->cluster_bits); |
|
272 |
l2_offset = s->l1_table[l1_index]; |
|
273 |
new_l2_table = 0; |
|
274 |
if (!l2_offset) { |
|
275 |
if (!allocate) |
|
276 |
return 0; |
|
277 |
/* allocate a new l2 entry */ |
|
278 |
l2_offset = lseek64(s->fd, 0, SEEK_END); |
|
279 |
/* round to cluster size */ |
|
280 |
l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1); |
|
281 |
/* update the L1 entry */ |
|
282 |
s->l1_table[l1_index] = l2_offset; |
|
283 |
tmp = cpu_to_be64(l2_offset); |
|
284 |
lseek64(s->fd, s->l1_table_offset + l1_index * sizeof(tmp), SEEK_SET); |
|
285 |
if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp)) |
|
286 |
return 0; |
|
287 |
new_l2_table = 1; |
|
288 |
} |
|
289 |
for(i = 0; i < L2_CACHE_SIZE; i++) { |
|
290 |
if (l2_offset == s->l2_cache_offsets[i]) { |
|
291 |
/* increment the hit count */ |
|
292 |
if (++s->l2_cache_counts[i] == 0xffffffff) { |
|
293 |
for(j = 0; j < L2_CACHE_SIZE; j++) { |
|
294 |
s->l2_cache_counts[j] >>= 1; |
|
295 |
} |
|
296 |
} |
|
297 |
l2_table = s->l2_cache + (i << s->l2_bits); |
|
298 |
goto found; |
|
299 |
} |
|
300 |
} |
|
301 |
/* not found: load a new entry in the least used one */ |
|
302 |
min_index = 0; |
|
303 |
min_count = 0xffffffff; |
|
304 |
for(i = 0; i < L2_CACHE_SIZE; i++) { |
|
305 |
if (s->l2_cache_counts[i] < min_count) { |
|
306 |
min_count = s->l2_cache_counts[i]; |
|
307 |
min_index = i; |
|
308 |
} |
|
309 |
} |
|
310 |
l2_table = s->l2_cache + (min_index << s->l2_bits); |
|
311 |
lseek(s->fd, l2_offset, SEEK_SET); |
|
312 |
if (new_l2_table) { |
|
313 |
memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); |
|
314 |
if (write(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) != |
|
315 |
s->l2_size * sizeof(uint64_t)) |
|
316 |
return 0; |
|
317 |
} else { |
|
318 |
if (read(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) != |
|
319 |
s->l2_size * sizeof(uint64_t)) |
|
320 |
return 0; |
|
321 |
} |
|
322 |
s->l2_cache_offsets[min_index] = l2_offset; |
|
323 |
s->l2_cache_counts[min_index] = 1; |
|
324 |
found: |
|
325 |
l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); |
|
326 |
cluster_offset = be64_to_cpu(l2_table[l2_index]); |
|
327 |
if (!cluster_offset || |
|
328 |
((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) { |
|
329 |
if (!allocate) |
|
330 |
return 0; |
|
331 |
/* allocate a new cluster */ |
|
332 |
if ((cluster_offset & QCOW_OFLAG_COMPRESSED) && |
|
333 |
(n_end - n_start) < s->cluster_sectors) { |
|
334 |
/* if the cluster is already compressed, we must |
|
335 |
decompress it in the case it is not completely |
|
336 |
overwritten */ |
|
337 |
if (decompress_cluster(s, cluster_offset) < 0) |
|
338 |
return 0; |
|
339 |
cluster_offset = lseek64(s->fd, 0, SEEK_END); |
|
340 |
cluster_offset = (cluster_offset + s->cluster_size - 1) & |
|
341 |
~(s->cluster_size - 1); |
|
342 |
/* write the cluster content */ |
|
343 |
lseek64(s->fd, cluster_offset, SEEK_SET); |
|
344 |
if (write(s->fd, s->cluster_cache, s->cluster_size) != |
|
345 |
s->cluster_size) |
|
346 |
return -1; |
|
347 |
} else { |
|
348 |
cluster_offset = lseek64(s->fd, 0, SEEK_END); |
|
349 |
if (allocate == 1) { |
|
350 |
/* round to cluster size */ |
|
351 |
cluster_offset = (cluster_offset + s->cluster_size - 1) & |
|
352 |
~(s->cluster_size - 1); |
|
353 |
ftruncate(s->fd, cluster_offset + s->cluster_size); |
|
354 |
/* if encrypted, we must initialize the cluster |
|
355 |
content which won't be written */ |
|
356 |
if (s->crypt_method && |
|
357 |
(n_end - n_start) < s->cluster_sectors) { |
|
358 |
uint64_t start_sect; |
|
359 |
start_sect = (offset & ~(s->cluster_size - 1)) >> 9; |
|
360 |
memset(s->cluster_data + 512, 0xaa, 512); |
|
361 |
for(i = 0; i < s->cluster_sectors; i++) { |
|
362 |
if (i < n_start || i >= n_end) { |
|
363 |
encrypt_sectors(s, start_sect + i, |
|
364 |
s->cluster_data, |
|
365 |
s->cluster_data + 512, 1, 1, |
|
366 |
&s->aes_encrypt_key); |
|
367 |
lseek64(s->fd, cluster_offset + i * 512, SEEK_SET); |
|
368 |
if (write(s->fd, s->cluster_data, 512) != 512) |
|
369 |
return -1; |
|
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
} else { |
|
374 |
cluster_offset |= QCOW_OFLAG_COMPRESSED | |
|
375 |
(uint64_t)compressed_size << (63 - s->cluster_bits); |
|
376 |
} |
|
377 |
} |
|
378 |
/* update L2 table */ |
|
379 |
tmp = cpu_to_be64(cluster_offset); |
|
380 |
l2_table[l2_index] = tmp; |
|
381 |
lseek64(s->fd, l2_offset + l2_index * sizeof(tmp), SEEK_SET); |
|
382 |
if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp)) |
|
383 |
return 0; |
|
384 |
} |
|
385 |
return cluster_offset; |
|
386 |
} |
|
387 |
|
|
388 |
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
|
389 |
int nb_sectors, int *pnum) |
|
390 |
{ |
|
391 |
BDRVQcowState *s = bs->opaque; |
|
392 |
int index_in_cluster, n; |
|
393 |
uint64_t cluster_offset; |
|
394 |
|
|
395 |
cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); |
|
396 |
index_in_cluster = sector_num & (s->cluster_sectors - 1); |
|
397 |
n = s->cluster_sectors - index_in_cluster; |
|
398 |
if (n > nb_sectors) |
|
399 |
n = nb_sectors; |
|
400 |
*pnum = n; |
|
401 |
return (cluster_offset != 0); |
|
402 |
} |
|
403 |
|
|
404 |
static int decompress_buffer(uint8_t *out_buf, int out_buf_size, |
|
405 |
const uint8_t *buf, int buf_size) |
|
406 |
{ |
|
407 |
z_stream strm1, *strm = &strm1; |
|
408 |
int ret, out_len; |
|
409 |
|
|
410 |
memset(strm, 0, sizeof(*strm)); |
|
411 |
|
|
412 |
strm->next_in = (uint8_t *)buf; |
|
413 |
strm->avail_in = buf_size; |
|
414 |
strm->next_out = out_buf; |
|
415 |
strm->avail_out = out_buf_size; |
|
416 |
|
|
417 |
ret = inflateInit2(strm, -12); |
|
418 |
if (ret != Z_OK) |
|
419 |
return -1; |
|
420 |
ret = inflate(strm, Z_FINISH); |
|
421 |
out_len = strm->next_out - out_buf; |
|
422 |
if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || |
|
423 |
out_len != out_buf_size) { |
|
424 |
inflateEnd(strm); |
|
425 |
return -1; |
|
426 |
} |
|
427 |
inflateEnd(strm); |
|
428 |
return 0; |
|
429 |
} |
|
430 |
|
|
431 |
static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset) |
|
432 |
{ |
|
433 |
int ret, csize; |
|
434 |
uint64_t coffset; |
|
435 |
|
|
436 |
coffset = cluster_offset & s->cluster_offset_mask; |
|
437 |
if (s->cluster_cache_offset != coffset) { |
|
438 |
csize = cluster_offset >> (63 - s->cluster_bits); |
|
439 |
csize &= (s->cluster_size - 1); |
|
440 |
lseek64(s->fd, coffset, SEEK_SET); |
|
441 |
ret = read(s->fd, s->cluster_data, csize); |
|
442 |
if (ret != csize) |
|
443 |
return -1; |
|
444 |
if (decompress_buffer(s->cluster_cache, s->cluster_size, |
|
445 |
s->cluster_data, csize) < 0) { |
|
446 |
return -1; |
|
447 |
} |
|
448 |
s->cluster_cache_offset = coffset; |
|
449 |
} |
|
450 |
return 0; |
|
451 |
} |
|
452 |
|
|
453 |
static int qcow_read(BlockDriverState *bs, int64_t sector_num, |
|
454 |
uint8_t *buf, int nb_sectors) |
|
455 |
{ |
|
456 |
BDRVQcowState *s = bs->opaque; |
|
457 |
int ret, index_in_cluster, n; |
|
458 |
uint64_t cluster_offset; |
|
459 |
|
|
460 |
while (nb_sectors > 0) { |
|
461 |
cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); |
|
462 |
index_in_cluster = sector_num & (s->cluster_sectors - 1); |
|
463 |
n = s->cluster_sectors - index_in_cluster; |
|
464 |
if (n > nb_sectors) |
|
465 |
n = nb_sectors; |
|
466 |
if (!cluster_offset) { |
|
467 |
memset(buf, 0, 512 * n); |
|
468 |
} else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { |
|
469 |
if (decompress_cluster(s, cluster_offset) < 0) |
|
470 |
return -1; |
|
471 |
memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n); |
|
472 |
} else { |
|
473 |
lseek64(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET); |
|
474 |
ret = read(s->fd, buf, n * 512); |
|
475 |
if (ret != n * 512) |
|
476 |
return -1; |
|
477 |
if (s->crypt_method) { |
|
478 |
encrypt_sectors(s, sector_num, buf, buf, n, 0, |
|
479 |
&s->aes_decrypt_key); |
|
480 |
} |
|
481 |
} |
|
482 |
nb_sectors -= n; |
|
483 |
sector_num += n; |
|
484 |
buf += n * 512; |
|
485 |
} |
|
486 |
return 0; |
|
487 |
} |
|
488 |
|
|
489 |
static int qcow_write(BlockDriverState *bs, int64_t sector_num, |
|
490 |
const uint8_t *buf, int nb_sectors) |
|
491 |
{ |
|
492 |
BDRVQcowState *s = bs->opaque; |
|
493 |
int ret, index_in_cluster, n; |
|
494 |
uint64_t cluster_offset; |
|
495 |
|
|
496 |
while (nb_sectors > 0) { |
|
497 |
index_in_cluster = sector_num & (s->cluster_sectors - 1); |
|
498 |
n = s->cluster_sectors - index_in_cluster; |
|
499 |
if (n > nb_sectors) |
|
500 |
n = nb_sectors; |
|
501 |
cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0, |
|
502 |
index_in_cluster, |
|
503 |
index_in_cluster + n); |
|
504 |
if (!cluster_offset) |
|
505 |
return -1; |
|
506 |
lseek64(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET); |
|
507 |
if (s->crypt_method) { |
|
508 |
encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1, |
|
509 |
&s->aes_encrypt_key); |
|
510 |
ret = write(s->fd, s->cluster_data, n * 512); |
|
511 |
} else { |
|
512 |
ret = write(s->fd, buf, n * 512); |
|
513 |
} |
|
514 |
if (ret != n * 512) |
|
515 |
return -1; |
|
516 |
nb_sectors -= n; |
|
517 |
sector_num += n; |
|
518 |
buf += n * 512; |
|
519 |
} |
|
520 |
s->cluster_cache_offset = -1; /* disable compressed cache */ |
|
521 |
return 0; |
|
522 |
} |
|
523 |
|
|
524 |
static int qcow_close(BlockDriverState *bs) |
|
525 |
{ |
|
526 |
BDRVQcowState *s = bs->opaque; |
|
527 |
qemu_free(s->l1_table); |
|
528 |
qemu_free(s->l2_cache); |
|
529 |
qemu_free(s->cluster_cache); |
|
530 |
qemu_free(s->cluster_data); |
|
531 |
close(s->fd); |
|
532 |
} |
|
533 |
|
|
534 |
static int qcow_create(const char *filename, int64_t total_size, |
|
535 |
const char *backing_file, int flags) |
|
536 |
{ |
|
537 |
int fd, header_size, backing_filename_len, l1_size, i, shift; |
|
538 |
QCowHeader header; |
|
539 |
char backing_filename[1024]; |
|
540 |
uint64_t tmp; |
|
541 |
struct stat st; |
|
542 |
|
|
543 |
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
|
544 |
0644); |
|
545 |
if (fd < 0) |
|
546 |
return -1; |
|
547 |
memset(&header, 0, sizeof(header)); |
|
548 |
header.magic = cpu_to_be32(QCOW_MAGIC); |
|
549 |
header.version = cpu_to_be32(QCOW_VERSION); |
|
550 |
header.size = cpu_to_be64(total_size * 512); |
|
551 |
header_size = sizeof(header); |
|
552 |
backing_filename_len = 0; |
|
553 |
if (backing_file) { |
|
554 |
realpath(backing_file, backing_filename); |
|
555 |
if (stat(backing_filename, &st) != 0) { |
|
556 |
return -1; |
|
557 |
} |
|
558 |
header.mtime = cpu_to_be32(st.st_mtime); |
|
559 |
header.backing_file_offset = cpu_to_be64(header_size); |
|
560 |
backing_filename_len = strlen(backing_filename); |
|
561 |
header.backing_file_size = cpu_to_be32(backing_filename_len); |
|
562 |
header_size += backing_filename_len; |
|
563 |
header.cluster_bits = 9; /* 512 byte cluster to avoid copying |
|
564 |
unmodifyed sectors */ |
|
565 |
header.l2_bits = 12; /* 32 KB L2 tables */ |
|
566 |
} else { |
|
567 |
header.cluster_bits = 12; /* 4 KB clusters */ |
|
568 |
header.l2_bits = 9; /* 4 KB L2 tables */ |
|
569 |
} |
|
570 |
header_size = (header_size + 7) & ~7; |
|
571 |
shift = header.cluster_bits + header.l2_bits; |
|
572 |
l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift; |
|
573 |
|
|
574 |
header.l1_table_offset = cpu_to_be64(header_size); |
|
575 |
if (flags) { |
|
576 |
header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
|
577 |
} else { |
|
578 |
header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); |
|
579 |
} |
|
580 |
|
|
581 |
/* write all the data */ |
|
582 |
write(fd, &header, sizeof(header)); |
|
583 |
if (backing_file) { |
|
584 |
write(fd, backing_filename, backing_filename_len); |
|
585 |
} |
|
586 |
lseek(fd, header_size, SEEK_SET); |
|
587 |
tmp = 0; |
|
588 |
for(i = 0;i < l1_size; i++) { |
|
589 |
write(fd, &tmp, sizeof(tmp)); |
|
590 |
} |
|
591 |
close(fd); |
|
592 |
return 0; |
|
593 |
} |
|
594 |
|
|
595 |
int qcow_get_cluster_size(BlockDriverState *bs) |
|
596 |
{ |
|
597 |
BDRVQcowState *s = bs->opaque; |
|
598 |
if (bs->drv != &bdrv_qcow) |
|
599 |
return -1; |
|
600 |
return s->cluster_size; |
|
601 |
} |
|
602 |
|
|
603 |
/* XXX: put compressed sectors first, then all the cluster aligned |
|
604 |
tables to avoid losing bytes in alignment */ |
|
605 |
int qcow_compress_cluster(BlockDriverState *bs, int64_t sector_num, |
|
606 |
const uint8_t *buf) |
|
607 |
{ |
|
608 |
BDRVQcowState *s = bs->opaque; |
|
609 |
z_stream strm; |
|
610 |
int ret, out_len; |
|
611 |
uint8_t *out_buf; |
|
612 |
uint64_t cluster_offset; |
|
613 |
|
|
614 |
if (bs->drv != &bdrv_qcow) |
|
615 |
return -1; |
|
616 |
|
|
617 |
out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); |
|
618 |
if (!out_buf) |
|
619 |
return -1; |
|
620 |
|
|
621 |
/* best compression, small window, no zlib header */ |
|
622 |
memset(&strm, 0, sizeof(strm)); |
|
623 |
ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, |
|
624 |
Z_DEFLATED, -12, |
|
625 |
9, Z_DEFAULT_STRATEGY); |
|
626 |
if (ret != 0) { |
|
627 |
qemu_free(out_buf); |
|
628 |
return -1; |
|
629 |
} |
|
630 |
|
|
631 |
strm.avail_in = s->cluster_size; |
|
632 |
strm.next_in = (uint8_t *)buf; |
|
633 |
strm.avail_out = s->cluster_size; |
|
634 |
strm.next_out = out_buf; |
|
635 |
|
|
636 |
ret = deflate(&strm, Z_FINISH); |
|
637 |
if (ret != Z_STREAM_END && ret != Z_OK) { |
|
638 |
qemu_free(out_buf); |
|
639 |
deflateEnd(&strm); |
|
640 |
return -1; |
|
641 |
} |
|
642 |
out_len = strm.next_out - out_buf; |
|
643 |
|
|
644 |
deflateEnd(&strm); |
|
645 |
|
|
646 |
if (ret != Z_STREAM_END || out_len >= s->cluster_size) { |
|
647 |
/* could not compress: write normal cluster */ |
|
648 |
qcow_write(bs, sector_num, buf, s->cluster_sectors); |
|
649 |
} else { |
|
650 |
cluster_offset = get_cluster_offset(bs, sector_num << 9, 2, |
|
651 |
out_len, 0, 0); |
|
652 |
cluster_offset &= s->cluster_offset_mask; |
|
653 |
lseek64(s->fd, cluster_offset, SEEK_SET); |
|
654 |
if (write(s->fd, out_buf, out_len) != out_len) { |
|
655 |
qemu_free(out_buf); |
|
656 |
return -1; |
|
657 |
} |
|
658 |
} |
|
659 |
|
|
660 |
qemu_free(out_buf); |
|
661 |
return 0; |
|
662 |
} |
|
663 |
|
|
664 |
BlockDriver bdrv_qcow = { |
|
665 |
"qcow", |
|
666 |
sizeof(BDRVQcowState), |
|
667 |
qcow_probe, |
|
668 |
qcow_open, |
|
669 |
qcow_read, |
|
670 |
qcow_write, |
|
671 |
qcow_close, |
|
672 |
qcow_create, |
|
673 |
qcow_is_allocated, |
|
674 |
qcow_set_key, |
|
675 |
}; |
|
676 |
|
|
677 |
|
b/block-vmdk.c | ||
---|---|---|
1 |
/* |
|
2 |
* Block driver for the VMDK format |
|
3 |
* |
|
4 |
* Copyright (c) 2004 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 "vl.h" |
|
25 |
#include "block_int.h" |
|
26 |
|
|
27 |
/* XXX: this code is untested */ |
|
28 |
/* XXX: add write support */ |
|
29 |
|
|
30 |
#define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D') |
|
31 |
#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V') |
|
32 |
|
|
33 |
typedef struct { |
|
34 |
uint32_t version; |
|
35 |
uint32_t flags; |
|
36 |
uint32_t disk_sectors; |
|
37 |
uint32_t granularity; |
|
38 |
uint32_t l1dir_offset; |
|
39 |
uint32_t l1dir_size; |
|
40 |
uint32_t file_sectors; |
|
41 |
uint32_t cylinders; |
|
42 |
uint32_t heads; |
|
43 |
uint32_t sectors_per_track; |
|
44 |
} VMDK3Header; |
|
45 |
|
|
46 |
typedef struct { |
|
47 |
uint32_t version; |
|
48 |
uint32_t flags; |
|
49 |
int64_t capacity; |
|
50 |
int64_t granularity; |
|
51 |
int64_t desc_offset; |
|
52 |
int64_t desc_size; |
|
53 |
int32_t num_gtes_per_gte; |
|
54 |
int64_t rgd_offset; |
|
55 |
int64_t gd_offset; |
|
56 |
int64_t grain_offset; |
|
57 |
char filler[1]; |
|
58 |
char check_bytes[4]; |
|
59 |
} VMDK4Header; |
|
60 |
|
|
61 |
#define L2_CACHE_SIZE 16 |
|
62 |
|
|
63 |
typedef struct BDRVVmdkState { |
|
64 |
int fd; |
|
65 |
int64_t l1_table_offset; |
|
66 |
uint32_t *l1_table; |
|
67 |
unsigned int l1_size; |
|
68 |
uint32_t l1_entry_sectors; |
|
69 |
|
|
70 |
unsigned int l2_size; |
|
71 |
uint32_t *l2_cache; |
|
72 |
uint32_t l2_cache_offsets[L2_CACHE_SIZE]; |
|
73 |
uint32_t l2_cache_counts[L2_CACHE_SIZE]; |
|
74 |
|
|
75 |
unsigned int cluster_sectors; |
|
76 |
} BDRVVmdkState; |
|
77 |
|
|
78 |
static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) |
|
79 |
{ |
|
80 |
uint32_t magic; |
|
81 |
|
|
82 |
if (buf_size < 4) |
|
83 |
return 0; |
|
84 |
magic = be32_to_cpu(*(uint32_t *)buf); |
|
85 |
if (magic == VMDK3_MAGIC || |
|
86 |
magic == VMDK4_MAGIC) |
|
87 |
return 100; |
|
88 |
else |
|
89 |
return 0; |
|
90 |
} |
|
91 |
|
|
92 |
static int vmdk_open(BlockDriverState *bs, const char *filename) |
|
93 |
{ |
|
94 |
BDRVVmdkState *s = bs->opaque; |
|
95 |
int fd, i; |
|
96 |
uint32_t magic; |
|
97 |
int l1_size; |
|
98 |
|
|
99 |
fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
|
100 |
if (fd < 0) |
|
101 |
return -1; |
|
102 |
if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) |
|
103 |
goto fail; |
|
104 |
magic = le32_to_cpu(magic); |
|
105 |
|
|
106 |
if (magic == VMDK3_MAGIC) { |
|
107 |
VMDK3Header header; |
|
108 |
if (read(fd, &header, sizeof(header)) != |
|
109 |
sizeof(header)) |
|
110 |
goto fail; |
|
111 |
s->cluster_sectors = le32_to_cpu(header.granularity); |
|
112 |
s->l2_size = 1 << 9; |
|
113 |
s->l1_size = 1 << 6; |
|
114 |
bs->total_sectors = le32_to_cpu(header.disk_sectors); |
|
115 |
s->l1_table_offset = le32_to_cpu(header.l1dir_offset) * 512; |
|
116 |
s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
|
117 |
} else if (magic == VMDK4_MAGIC) { |
|
118 |
VMDK4Header header; |
|
119 |
|
|
120 |
if (read(fd, &header, sizeof(header)) != sizeof(header)) |
|
121 |
goto fail; |
|
122 |
bs->total_sectors = le32_to_cpu(header.capacity); |
|
123 |
s->cluster_sectors = le32_to_cpu(header.granularity); |
|
124 |
s->l2_size = le32_to_cpu(header.num_gtes_per_gte); |
|
125 |
s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
|
126 |
if (s->l1_entry_sectors <= 0) |
|
127 |
goto fail; |
|
128 |
s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1) |
|
129 |
/ s->l1_entry_sectors; |
|
130 |
s->l1_table_offset = le64_to_cpu(header.rgd_offset) * 512; |
|
131 |
} else { |
|
132 |
goto fail; |
|
133 |
} |
|
134 |
/* read the L1 table */ |
|
135 |
l1_size = s->l1_size * sizeof(uint32_t); |
|
136 |
s->l1_table = qemu_malloc(l1_size); |
|
137 |
if (!s->l1_table) |
|
138 |
goto fail; |
|
139 |
if (read(s->fd, s->l1_table, l1_size) != l1_size) |
|
140 |
goto fail; |
|
141 |
for(i = 0; i < s->l1_size; i++) { |
|
142 |
le32_to_cpus(&s->l1_table[i]); |
|
143 |
} |
|
144 |
|
|
145 |
s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t)); |
|
146 |
if (!s->l2_cache) |
|
147 |
goto fail; |
|
148 |
s->fd = fd; |
|
149 |
/* XXX: currently only read only */ |
|
150 |
bs->read_only = 1; |
|
151 |
return 0; |
|
152 |
fail: |
|
153 |
qemu_free(s->l1_table); |
|
154 |
qemu_free(s->l2_cache); |
|
155 |
close(fd); |
|
156 |
return -1; |
|
157 |
} |
|
158 |
|
|
159 |
static uint64_t get_cluster_offset(BlockDriverState *bs, |
|
160 |
uint64_t offset) |
|
161 |
{ |
|
162 |
BDRVVmdkState *s = bs->opaque; |
|
163 |
unsigned int l1_index, l2_offset, l2_index; |
|
164 |
int min_index, i, j; |
|
165 |
uint32_t min_count, *l2_table; |
|
166 |
uint64_t cluster_offset; |
|
167 |
|
|
168 |
l1_index = (offset >> 9) / s->l1_entry_sectors; |
|
169 |
if (l1_index >= s->l1_size) |
|
170 |
return 0; |
|
171 |
l2_offset = s->l1_table[l1_index]; |
|
172 |
if (!l2_offset) |
|
173 |
return 0; |
|
174 |
|
|
175 |
for(i = 0; i < L2_CACHE_SIZE; i++) { |
|
176 |
if (l2_offset == s->l2_cache_offsets[i]) { |
|
177 |
/* increment the hit count */ |
|
178 |
if (++s->l2_cache_counts[i] == 0xffffffff) { |
|
179 |
for(j = 0; j < L2_CACHE_SIZE; j++) { |
|
180 |
s->l2_cache_counts[j] >>= 1; |
|
181 |
} |
|
182 |
} |
|
183 |
l2_table = s->l2_cache + (i * s->l2_size); |
|
184 |
goto found; |
|
185 |
} |
|
186 |
} |
|
187 |
/* not found: load a new entry in the least used one */ |
|
188 |
min_index = 0; |
|
189 |
min_count = 0xffffffff; |
|
190 |
for(i = 0; i < L2_CACHE_SIZE; i++) { |
|
191 |
if (s->l2_cache_counts[i] < min_count) { |
|
192 |
min_count = s->l2_cache_counts[i]; |
|
193 |
min_index = i; |
|
194 |
} |
|
195 |
} |
|
196 |
l2_table = s->l2_cache + (min_index * s->l2_size); |
|
197 |
lseek(s->fd, (int64_t)l2_offset * 512, SEEK_SET); |
|
198 |
if (read(s->fd, l2_table, s->l2_size * sizeof(uint32_t)) != |
|
199 |
s->l2_size * sizeof(uint32_t)) |
|
200 |
return 0; |
|
201 |
s->l2_cache_offsets[min_index] = l2_offset; |
|
202 |
s->l2_cache_counts[min_index] = 1; |
|
203 |
found: |
|
204 |
l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size; |
|
205 |
cluster_offset = le32_to_cpu(l2_table[l2_index]); |
|
206 |
cluster_offset <<= 9; |
|
207 |
return cluster_offset; |
|
208 |
} |
|
209 |
|
|
210 |
static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num, |
|
211 |
int nb_sectors, int *pnum) |
|
212 |
{ |
|
213 |
BDRVVmdkState *s = bs->opaque; |
|
214 |
int index_in_cluster, n; |
|
215 |
uint64_t cluster_offset; |
|
216 |
|
|
217 |
cluster_offset = get_cluster_offset(bs, sector_num << 9); |
|
218 |
index_in_cluster = sector_num % s->cluster_sectors; |
|
219 |
n = s->cluster_sectors - index_in_cluster; |
|
220 |
if (n > nb_sectors) |
|
221 |
n = nb_sectors; |
|
222 |
*pnum = n; |
|
223 |
return (cluster_offset != 0); |
|
224 |
} |
|
225 |
|
|
226 |
static int vmdk_read(BlockDriverState *bs, int64_t sector_num, |
|
227 |
uint8_t *buf, int nb_sectors) |
|
228 |
{ |
|
229 |
BDRVVmdkState *s = bs->opaque; |
|
230 |
int ret, index_in_cluster, n; |
|
231 |
uint64_t cluster_offset; |
|
232 |
|
|
233 |
while (nb_sectors > 0) { |
|
234 |
cluster_offset = get_cluster_offset(bs, sector_num << 9); |
|
235 |
index_in_cluster = sector_num % s->cluster_sectors; |
|
236 |
n = s->cluster_sectors - index_in_cluster; |
|
237 |
if (n > nb_sectors) |
|
238 |
n = nb_sectors; |
|
239 |
if (!cluster_offset) { |
|
240 |
memset(buf, 0, 512 * n); |
|
241 |
} else { |
|
242 |
lseek64(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET); |
|
243 |
ret = read(s->fd, buf, n * 512); |
|
244 |
if (ret != n * 512) |
|
245 |
return -1; |
|
246 |
} |
|
247 |
nb_sectors -= n; |
|
248 |
sector_num += n; |
|
249 |
buf += n * 512; |
|
250 |
} |
|
251 |
return 0; |
|
252 |
} |
|
253 |
|
|
254 |
static int vmdk_write(BlockDriverState *bs, int64_t sector_num, |
|
255 |
const uint8_t *buf, int nb_sectors) |
|
256 |
{ |
|
257 |
return -1; |
|
258 |
} |
|
259 |
|
|
260 |
static int vmdk_close(BlockDriverState *bs) |
|
261 |
{ |
|
262 |
BDRVVmdkState *s = bs->opaque; |
|
263 |
qemu_free(s->l1_table); |
|
264 |
qemu_free(s->l2_cache); |
|
265 |
close(s->fd); |
|
266 |
} |
|
267 |
|
|
268 |
BlockDriver bdrv_vmdk = { |
|
269 |
"vmdk", |
|
270 |
sizeof(BDRVVmdkState), |
|
271 |
vmdk_probe, |
|
272 |
vmdk_open, |
|
273 |
vmdk_read, |
|
274 |
vmdk_write, |
|
275 |
vmdk_close, |
|
276 |
NULL, /* no create yet */ |
|
277 |
vmdk_is_allocated, |
|
278 |
}; |
b/block.c | ||
---|---|---|
22 | 22 |
* THE SOFTWARE. |
23 | 23 |
*/ |
24 | 24 |
#include "vl.h" |
25 |
|
|
26 |
#ifndef _WIN32 |
|
27 |
#include <sys/mman.h> |
|
28 |
#endif |
|
29 |
|
|
30 |
#include "cow.h" |
|
31 |
|
|
32 |
struct BlockDriverState { |
|
33 |
int fd; /* if -1, only COW mappings */ |
|
34 |
int64_t total_sectors; |
|
35 |
int read_only; /* if true, the media is read only */ |
|
36 |
int inserted; /* if true, the media is present */ |
|
37 |
int removable; /* if true, the media can be removed */ |
|
38 |
int locked; /* if true, the media cannot temporarily be ejected */ |
|
39 |
/* event callback when inserting/removing */ |
|
40 |
void (*change_cb)(void *opaque); |
|
41 |
void *change_opaque; |
|
42 |
|
|
43 |
uint8_t *cow_bitmap; /* if non NULL, COW mappings are used first */ |
|
44 |
uint8_t *cow_bitmap_addr; /* mmap address of cow_bitmap */ |
|
45 |
int cow_bitmap_size; |
|
46 |
int cow_fd; |
|
47 |
int64_t cow_sectors_offset; |
|
48 |
int boot_sector_enabled; |
|
49 |
uint8_t boot_sector_data[512]; |
|
50 |
|
|
51 |
char filename[1024]; |
|
52 |
|
|
53 |
/* NOTE: the following infos are only hints for real hardware |
|
54 |
drivers. They are not used by the block driver */ |
|
55 |
int cyls, heads, secs; |
|
56 |
int type; |
|
57 |
char device_name[32]; |
|
58 |
BlockDriverState *next; |
|
59 |
}; |
|
25 |
#include "block_int.h" |
|
60 | 26 |
|
61 | 27 |
static BlockDriverState *bdrv_first; |
28 |
static BlockDriver *first_drv; |
|
29 |
|
|
30 |
void bdrv_register(BlockDriver *bdrv) |
|
31 |
{ |
|
32 |
bdrv->next = first_drv; |
|
33 |
first_drv = bdrv; |
|
34 |
} |
|
62 | 35 |
|
63 | 36 |
/* create a new block device (by default it is empty) */ |
64 | 37 |
BlockDriverState *bdrv_new(const char *device_name) |
... | ... | |
69 | 42 |
if(!bs) |
70 | 43 |
return NULL; |
71 | 44 |
pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
72 |
/* insert at the end */ |
|
73 |
pbs = &bdrv_first; |
|
74 |
while (*pbs != NULL) |
|
75 |
pbs = &(*pbs)->next; |
|
76 |
*pbs = bs; |
|
45 |
if (device_name[0] != '\0') { |
|
46 |
/* insert at the end */ |
|
47 |
pbs = &bdrv_first; |
|
48 |
while (*pbs != NULL) |
|
49 |
pbs = &(*pbs)->next; |
|
50 |
*pbs = bs; |
|
51 |
} |
|
77 | 52 |
return bs; |
78 | 53 |
} |
79 | 54 |
|
80 |
int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot) |
|
55 |
BlockDriver *bdrv_find_format(const char *format_name) |
|
56 |
{ |
|
57 |
BlockDriver *drv1; |
|
58 |
for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
|
59 |
if (!strcmp(drv1->format_name, format_name)) |
|
60 |
return drv1; |
|
61 |
} |
|
62 |
return NULL; |
|
63 |
} |
|
64 |
|
|
65 |
int bdrv_create(BlockDriver *drv, |
|
66 |
const char *filename, int64_t size_in_sectors, |
|
67 |
const char *backing_file, int flags) |
|
68 |
{ |
|
69 |
if (!drv->bdrv_create) |
|
70 |
return -ENOTSUP; |
|
71 |
return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); |
|
72 |
} |
|
73 |
|
|
74 |
/* XXX: race condition possible */ |
|
75 |
static void get_tmp_filename(char *filename, int size) |
|
81 | 76 |
{ |
82 | 77 |
int fd; |
83 |
int64_t size; |
|
84 |
struct cow_header_v2 cow_header; |
|
85 |
#ifndef _WIN32 |
|
86 |
char template[] = "/tmp/vl.XXXXXX"; |
|
87 |
int cow_fd; |
|
88 |
struct stat st; |
|
89 |
#endif |
|
78 |
pstrcpy(filename, size, "/tmp/vl.XXXXXX"); |
|
79 |
fd = mkstemp(filename); |
|
80 |
close(fd); |
|
81 |
} |
|
90 | 82 |
|
91 |
bs->read_only = 0;
|
|
92 |
bs->fd = -1;
|
|
93 |
bs->cow_fd = -1;
|
|
94 |
bs->cow_bitmap = NULL;
|
|
95 |
pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
|
83 |
static BlockDriver *find_image_format(const char *filename)
|
|
84 |
{
|
|
85 |
int fd, ret, score, score_max;
|
|
86 |
BlockDriver *drv1, *drv;
|
|
87 |
uint8_t buf[1024];
|
|
96 | 88 |
|
97 |
/* open standard HD image */ |
|
98 |
#ifdef _WIN32 |
|
99 |
fd = open(filename, O_RDWR | O_BINARY); |
|
100 |
#else |
|
101 |
fd = open(filename, O_RDWR | O_LARGEFILE); |
|
102 |
#endif |
|
103 |
if (fd < 0) { |
|
104 |
/* read only image on disk */ |
|
105 |
#ifdef _WIN32 |
|
106 |
fd = open(filename, O_RDONLY | O_BINARY); |
|
107 |
#else |
|
108 |
fd = open(filename, O_RDONLY | O_LARGEFILE); |
|
109 |
#endif |
|
110 |
if (fd < 0) { |
|
111 |
perror(filename); |
|
112 |
goto fail; |
|
89 |
fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
|
90 |
if (fd < 0) |
|
91 |
return NULL; |
|
92 |
ret = read(fd, buf, sizeof(buf)); |
|
93 |
if (ret < 0) { |
|
94 |
close(fd); |
|
95 |
return NULL; |
|
96 |
} |
|
97 |
close(fd); |
|
98 |
|
|
99 |
drv = NULL; |
|
100 |
score_max = 0; |
|
101 |
for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
|
102 |
score = drv1->bdrv_probe(buf, ret, filename); |
|
103 |
if (score > score_max) { |
|
104 |
score_max = score; |
|
105 |
drv = drv1; |
|
113 | 106 |
} |
114 |
if (!snapshot) |
|
115 |
bs->read_only = 1; |
|
116 | 107 |
} |
117 |
bs->fd = fd; |
|
108 |
return drv; |
|
109 |
} |
|
110 |
|
|
111 |
int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot) |
|
112 |
{ |
|
113 |
return bdrv_open2(bs, filename, snapshot, NULL); |
|
114 |
} |
|
115 |
|
|
116 |
int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot, |
|
117 |
BlockDriver *drv) |
|
118 |
{ |
|
119 |
int ret; |
|
120 |
char tmp_filename[1024]; |
|
121 |
|
|
122 |
bs->read_only = 0; |
|
123 |
bs->is_temporary = 0; |
|
124 |
bs->encrypted = 0; |
|
125 |
|
|
126 |
if (snapshot) { |
|
127 |
BlockDriverState *bs1; |
|
128 |
int64_t total_size; |
|
129 |
|
|
130 |
/* if snapshot, we create a temporary backing file and open it |
|
131 |
instead of opening 'filename' directly */ |
|
118 | 132 |
|
119 |
/* see if it is a cow image */ |
|
120 |
if (read(fd, &cow_header, sizeof(cow_header)) != sizeof(cow_header)) { |
|
121 |
fprintf(stderr, "%s: could not read header\n", filename); |
|
122 |
goto fail; |
|
133 |
/* if there is a backing file, use it */ |
|
134 |
bs1 = bdrv_new(""); |
|
135 |
if (!bs1) { |
|
136 |
return -1; |
|
137 |
} |
|
138 |
if (bdrv_open(bs1, filename, 0) < 0) { |
|
139 |
bdrv_delete(bs1); |
|
140 |
return -1; |
|
141 |
} |
|
142 |
total_size = bs1->total_sectors; |
|
143 |
bdrv_delete(bs1); |
|
144 |
|
|
145 |
get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
|
146 |
/* XXX: use cow for linux as it is more efficient ? */ |
|
147 |
if (bdrv_create(&bdrv_qcow, tmp_filename, |
|
148 |
total_size, filename, 0) < 0) { |
|
149 |
return -1; |
|
150 |
} |
|
151 |
filename = tmp_filename; |
|
152 |
bs->is_temporary = 1; |
|
153 |
} |
|
154 |
|
|
155 |
pstrcpy(bs->filename, sizeof(bs->filename), filename); |
|
156 |
if (!drv) { |
|
157 |
drv = find_image_format(filename); |
|
158 |
if (!drv) |
|
159 |
return -1; |
|
160 |
} |
|
161 |
bs->drv = drv; |
|
162 |
bs->opaque = qemu_mallocz(drv->instance_size); |
|
163 |
if (bs->opaque == NULL && drv->instance_size > 0) |
|
164 |
return -1; |
|
165 |
|
|
166 |
ret = drv->bdrv_open(bs, filename); |
|
167 |
if (ret < 0) { |
|
168 |
qemu_free(bs->opaque); |
|
169 |
return -1; |
|
123 | 170 |
} |
124 | 171 |
#ifndef _WIN32 |
125 |
if (be32_to_cpu(cow_header.magic) == COW_MAGIC && |
|
126 |
be32_to_cpu(cow_header.version) == COW_VERSION) { |
|
127 |
/* cow image found */ |
|
128 |
size = cow_header.size; |
|
129 |
#ifndef WORDS_BIGENDIAN |
|
130 |
size = bswap64(size); |
|
131 |
#endif |
|
132 |
bs->total_sectors = size / 512; |
|
133 |
|
|
134 |
bs->cow_fd = fd; |
|
135 |
bs->fd = -1; |
|
136 |
if (cow_header.backing_file[0] != '\0') { |
|
137 |
if (stat(cow_header.backing_file, &st) != 0) { |
|
138 |
fprintf(stderr, "%s: could not find original disk image '%s'\n", filename, cow_header.backing_file); |
|
139 |
goto fail; |
|
140 |
} |
|
141 |
if (st.st_mtime != be32_to_cpu(cow_header.mtime)) { |
|
142 |
fprintf(stderr, "%s: original raw disk image '%s' does not match saved timestamp\n", filename, cow_header.backing_file); |
|
143 |
goto fail; |
|
144 |
} |
|
145 |
fd = open(cow_header.backing_file, O_RDONLY | O_LARGEFILE); |
|
146 |
if (fd < 0) |
|
147 |
goto fail; |
|
148 |
bs->fd = fd; |
|
172 |
if (bs->is_temporary) { |
|
173 |
unlink(filename); |
|
174 |
} |
|
175 |
#endif |
Also available in: Unified diff