Commit 5f4da8c0f3736b23744c27bf327d012480293b02
1 parent
b29a0341
New multiple snapshot support for VMDK, by Igor Lvovsky.
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@2352 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
1 changed file
with
353 additions
and
45 deletions
block-vmdk.c
| @@ -22,6 +22,7 @@ | @@ -22,6 +22,7 @@ | ||
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. | 23 | * THE SOFTWARE. |
| 24 | */ | 24 | */ |
| 25 | + | ||
| 25 | #include "vl.h" | 26 | #include "vl.h" |
| 26 | #include "block_int.h" | 27 | #include "block_int.h" |
| 27 | 28 | ||
| @@ -59,7 +60,7 @@ typedef struct { | @@ -59,7 +60,7 @@ typedef struct { | ||
| 59 | #define L2_CACHE_SIZE 16 | 60 | #define L2_CACHE_SIZE 16 |
| 60 | 61 | ||
| 61 | typedef struct BDRVVmdkState { | 62 | typedef struct BDRVVmdkState { |
| 62 | - int fd; | 63 | + BlockDriverState *hd; |
| 63 | int64_t l1_table_offset; | 64 | int64_t l1_table_offset; |
| 64 | int64_t l1_backup_table_offset; | 65 | int64_t l1_backup_table_offset; |
| 65 | uint32_t *l1_table; | 66 | uint32_t *l1_table; |
| @@ -73,6 +74,7 @@ typedef struct BDRVVmdkState { | @@ -73,6 +74,7 @@ typedef struct BDRVVmdkState { | ||
| 73 | uint32_t l2_cache_counts[L2_CACHE_SIZE]; | 74 | uint32_t l2_cache_counts[L2_CACHE_SIZE]; |
| 74 | 75 | ||
| 75 | unsigned int cluster_sectors; | 76 | unsigned int cluster_sectors; |
| 77 | + uint32_t parent_cid; | ||
| 76 | } BDRVVmdkState; | 78 | } BDRVVmdkState; |
| 77 | 79 | ||
| 78 | static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) | 80 | static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) |
| @@ -89,27 +91,278 @@ static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) | @@ -89,27 +91,278 @@ static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) | ||
| 89 | return 0; | 91 | return 0; |
| 90 | } | 92 | } |
| 91 | 93 | ||
| 92 | -static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) | 94 | +#define CHECK_CID 1 |
| 95 | + | ||
| 96 | +#define SECTOR_SIZE 512 | ||
| 97 | +#define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each | ||
| 98 | +#define HEADER_SIZE 512 // first sector of 512 bytes | ||
| 99 | + | ||
| 100 | +static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) | ||
| 93 | { | 101 | { |
| 94 | BDRVVmdkState *s = bs->opaque; | 102 | BDRVVmdkState *s = bs->opaque; |
| 95 | - int fd, i; | ||
| 96 | - uint32_t magic; | ||
| 97 | - int l1_size; | 103 | + char desc[DESC_SIZE]; |
| 104 | + uint32_t cid; | ||
| 105 | + char *p_name, *cid_str; | ||
| 106 | + size_t cid_str_size; | ||
| 107 | + | ||
| 108 | + /* the descriptor offset = 0x200 */ | ||
| 109 | + if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) | ||
| 110 | + return 0; | ||
| 111 | + | ||
| 112 | + if (parent) { | ||
| 113 | + cid_str = "parentCID"; | ||
| 114 | + cid_str_size = sizeof("parentCID"); | ||
| 115 | + } else { | ||
| 116 | + cid_str = "CID"; | ||
| 117 | + cid_str_size = sizeof("CID"); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + if ((p_name = strstr(desc,cid_str)) != 0) { | ||
| 121 | + p_name += cid_str_size; | ||
| 122 | + sscanf(p_name,"%x",&cid); | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + return cid; | ||
| 126 | +} | ||
| 127 | + | ||
| 128 | +static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid) | ||
| 129 | +{ | ||
| 130 | + BDRVVmdkState *s = bs->opaque; | ||
| 131 | + char desc[DESC_SIZE], tmp_desc[DESC_SIZE]; | ||
| 132 | + char *p_name, *tmp_str; | ||
| 133 | + | ||
| 134 | + /* the descriptor offset = 0x200 */ | ||
| 135 | + if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) | ||
| 136 | + return -1; | ||
| 137 | + | ||
| 138 | + tmp_str = strstr(desc,"parentCID"); | ||
| 139 | + strcpy(tmp_desc, tmp_str); | ||
| 140 | + if ((p_name = strstr(desc,"CID")) != 0) { | ||
| 141 | + p_name += sizeof("CID"); | ||
| 142 | + sprintf(p_name,"%x\n",cid); | ||
| 143 | + strcat(desc,tmp_desc); | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + if (bdrv_pwrite(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) | ||
| 147 | + return -1; | ||
| 148 | + return 0; | ||
| 149 | +} | ||
| 150 | + | ||
| 151 | +static int vmdk_is_cid_valid(BlockDriverState *bs) | ||
| 152 | +{ | ||
| 153 | +#ifdef CHECK_CID | ||
| 154 | + BDRVVmdkState *s = bs->opaque; | ||
| 155 | + BlockDriverState *p_bs = s->hd->backing_hd; | ||
| 156 | + uint32_t cur_pcid; | ||
| 157 | + | ||
| 158 | + if (p_bs) { | ||
| 159 | + cur_pcid = vmdk_read_cid(p_bs,0); | ||
| 160 | + if (s->parent_cid != cur_pcid) | ||
| 161 | + // CID not valid | ||
| 162 | + return 0; | ||
| 163 | + } | ||
| 164 | +#endif | ||
| 165 | + // CID valid | ||
| 166 | + return 1; | ||
| 167 | +} | ||
| 168 | + | ||
| 169 | +static int vmdk_snapshot_create(const char *filename, const char *backing_file) | ||
| 170 | +{ | ||
| 171 | + int snp_fd, p_fd; | ||
| 172 | + uint32_t p_cid; | ||
| 173 | + char *p_name, *gd_buf, *rgd_buf; | ||
| 174 | + const char *real_filename, *temp_str; | ||
| 175 | + VMDK4Header header; | ||
| 176 | + uint32_t gde_entries, gd_size; | ||
| 177 | + int64_t gd_offset, rgd_offset, capacity, gt_size; | ||
| 178 | + char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE]; | ||
| 179 | + char *desc_template = | ||
| 180 | + "# Disk DescriptorFile\n" | ||
| 181 | + "version=1\n" | ||
| 182 | + "CID=%x\n" | ||
| 183 | + "parentCID=%x\n" | ||
| 184 | + "createType=\"monolithicSparse\"\n" | ||
| 185 | + "parentFileNameHint=\"%s\"\n" | ||
| 186 | + "\n" | ||
| 187 | + "# Extent description\n" | ||
| 188 | + "RW %lu SPARSE \"%s\"\n" | ||
| 189 | + "\n" | ||
| 190 | + "# The Disk Data Base \n" | ||
| 191 | + "#DDB\n" | ||
| 192 | + "\n"; | ||
| 193 | + | ||
| 194 | + snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644); | ||
| 195 | + if (snp_fd < 0) | ||
| 196 | + return -1; | ||
| 197 | + p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE); | ||
| 198 | + if (p_fd < 0) { | ||
| 199 | + close(snp_fd); | ||
| 200 | + return -1; | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + /* read the header */ | ||
| 204 | + if (lseek(p_fd, 0x0, SEEK_SET) == -1) | ||
| 205 | + goto fail; | ||
| 206 | + if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE) | ||
| 207 | + goto fail; | ||
| 208 | + | ||
| 209 | + /* write the header */ | ||
| 210 | + if (lseek(snp_fd, 0x0, SEEK_SET) == -1) | ||
| 211 | + goto fail; | ||
| 212 | + if (write(snp_fd, hdr, HEADER_SIZE) == -1) | ||
| 213 | + goto fail; | ||
| 214 | + | ||
| 215 | + memset(&header, 0, sizeof(header)); | ||
| 216 | + memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC | ||
| 217 | + | ||
| 218 | + ftruncate(snp_fd, header.grain_offset << 9); | ||
| 219 | + /* the descriptor offset = 0x200 */ | ||
| 220 | + if (lseek(p_fd, 0x200, SEEK_SET) == -1) | ||
| 221 | + goto fail; | ||
| 222 | + if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE) | ||
| 223 | + goto fail; | ||
| 224 | + | ||
| 225 | + if ((p_name = strstr(p_desc,"CID")) != 0) { | ||
| 226 | + p_name += sizeof("CID"); | ||
| 227 | + sscanf(p_name,"%x",&p_cid); | ||
| 228 | + } | ||
| 229 | + | ||
| 230 | + real_filename = filename; | ||
| 231 | + if ((temp_str = strrchr(real_filename, '\\')) != NULL) | ||
| 232 | + real_filename = temp_str + 1; | ||
| 233 | + if ((temp_str = strrchr(real_filename, '/')) != NULL) | ||
| 234 | + real_filename = temp_str + 1; | ||
| 235 | + if ((temp_str = strrchr(real_filename, ':')) != NULL) | ||
| 236 | + real_filename = temp_str + 1; | ||
| 237 | + | ||
| 238 | + sprintf(s_desc, desc_template, p_cid, p_cid, backing_file | ||
| 239 | + , (uint32_t)header.capacity, real_filename); | ||
| 240 | + | ||
| 241 | + /* write the descriptor */ | ||
| 242 | + if (lseek(snp_fd, 0x200, SEEK_SET) == -1) | ||
| 243 | + goto fail; | ||
| 244 | + if (write(snp_fd, s_desc, strlen(s_desc)) == -1) | ||
| 245 | + goto fail; | ||
| 98 | 246 | ||
| 99 | - fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); | ||
| 100 | - if (fd < 0) { | ||
| 101 | - fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); | ||
| 102 | - if (fd < 0) | 247 | + gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table |
| 248 | + rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table | ||
| 249 | + capacity = header.capacity * SECTOR_SIZE; // Extent size | ||
| 250 | + /* | ||
| 251 | + * Each GDE span 32M disk, means: | ||
| 252 | + * 512 GTE per GT, each GTE points to grain | ||
| 253 | + */ | ||
| 254 | + gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE; | ||
| 255 | + if (!gt_size) | ||
| 256 | + goto fail; | ||
| 257 | + gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde | ||
| 258 | + gd_size = gde_entries * sizeof(uint32_t); | ||
| 259 | + | ||
| 260 | + /* write RGD */ | ||
| 261 | + rgd_buf = qemu_malloc(gd_size); | ||
| 262 | + if (!rgd_buf) | ||
| 263 | + goto fail; | ||
| 264 | + if (lseek(p_fd, rgd_offset, SEEK_SET) == -1) | ||
| 265 | + goto fail_rgd; | ||
| 266 | + if (read(p_fd, rgd_buf, gd_size) != gd_size) | ||
| 267 | + goto fail_rgd; | ||
| 268 | + if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1) | ||
| 269 | + goto fail_rgd; | ||
| 270 | + if (write(snp_fd, rgd_buf, gd_size) == -1) | ||
| 271 | + goto fail_rgd; | ||
| 272 | + qemu_free(rgd_buf); | ||
| 273 | + | ||
| 274 | + /* write GD */ | ||
| 275 | + gd_buf = qemu_malloc(gd_size); | ||
| 276 | + if (!gd_buf) | ||
| 277 | + goto fail_rgd; | ||
| 278 | + if (lseek(p_fd, gd_offset, SEEK_SET) == -1) | ||
| 279 | + goto fail_gd; | ||
| 280 | + if (read(p_fd, gd_buf, gd_size) != gd_size) | ||
| 281 | + goto fail_gd; | ||
| 282 | + if (lseek(snp_fd, gd_offset, SEEK_SET) == -1) | ||
| 283 | + goto fail_gd; | ||
| 284 | + if (write(snp_fd, gd_buf, gd_size) == -1) | ||
| 285 | + goto fail_gd; | ||
| 286 | + qemu_free(gd_buf); | ||
| 287 | + | ||
| 288 | + close(p_fd); | ||
| 289 | + close(snp_fd); | ||
| 290 | + return 0; | ||
| 291 | + | ||
| 292 | + fail_gd: | ||
| 293 | + qemu_free(gd_buf); | ||
| 294 | + fail_rgd: | ||
| 295 | + qemu_free(rgd_buf); | ||
| 296 | + fail: | ||
| 297 | + close(p_fd); | ||
| 298 | + close(snp_fd); | ||
| 299 | + return -1; | ||
| 300 | +} | ||
| 301 | + | ||
| 302 | +static void vmdk_parent_close(BlockDriverState *bs) | ||
| 303 | +{ | ||
| 304 | + if (bs->backing_hd) | ||
| 305 | + bdrv_close(bs->backing_hd); | ||
| 306 | +} | ||
| 307 | + | ||
| 308 | + | ||
| 309 | +static int vmdk_parent_open(BlockDriverState *bs, const char * filename) | ||
| 310 | +{ | ||
| 311 | + BDRVVmdkState *s = bs->opaque; | ||
| 312 | + char *p_name; | ||
| 313 | + char desc[DESC_SIZE]; | ||
| 314 | + char parent_img_name[1024]; | ||
| 315 | + | ||
| 316 | + /* the descriptor offset = 0x200 */ | ||
| 317 | + if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE) | ||
| 318 | + return -1; | ||
| 319 | + | ||
| 320 | + if ((p_name = strstr(desc,"parentFileNameHint")) != 0) { | ||
| 321 | + char *end_name; | ||
| 322 | + struct stat file_buf; | ||
| 323 | + | ||
| 324 | + p_name += sizeof("parentFileNameHint") + 1; | ||
| 325 | + if ((end_name = strchr(p_name,'\"')) == 0) | ||
| 326 | + return -1; | ||
| 327 | + | ||
| 328 | + strncpy(s->hd->backing_file, p_name, end_name - p_name); | ||
| 329 | + if (stat(s->hd->backing_file, &file_buf) != 0) { | ||
| 330 | + path_combine(parent_img_name, sizeof(parent_img_name), | ||
| 331 | + filename, s->hd->backing_file); | ||
| 332 | + } else { | ||
| 333 | + strcpy(parent_img_name, s->hd->backing_file); | ||
| 334 | + } | ||
| 335 | + | ||
| 336 | + s->hd->backing_hd = bdrv_new(""); | ||
| 337 | + if (!s->hd->backing_hd) { | ||
| 338 | + failure: | ||
| 339 | + bdrv_close(s->hd); | ||
| 103 | return -1; | 340 | return -1; |
| 104 | - bs->read_only = 1; | 341 | + } |
| 342 | + if (bdrv_open(s->hd->backing_hd, parent_img_name, 0) < 0) | ||
| 343 | + goto failure; | ||
| 105 | } | 344 | } |
| 106 | - if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) | 345 | + |
| 346 | + return 0; | ||
| 347 | +} | ||
| 348 | + | ||
| 349 | +static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) | ||
| 350 | +{ | ||
| 351 | + BDRVVmdkState *s = bs->opaque; | ||
| 352 | + uint32_t magic; | ||
| 353 | + int l1_size, i, ret; | ||
| 354 | + | ||
| 355 | + ret = bdrv_file_open(&s->hd, filename, flags); | ||
| 356 | + if (ret < 0) | ||
| 357 | + return ret; | ||
| 358 | + if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic)) | ||
| 107 | goto fail; | 359 | goto fail; |
| 360 | + | ||
| 108 | magic = be32_to_cpu(magic); | 361 | magic = be32_to_cpu(magic); |
| 109 | if (magic == VMDK3_MAGIC) { | 362 | if (magic == VMDK3_MAGIC) { |
| 110 | VMDK3Header header; | 363 | VMDK3Header header; |
| 111 | - if (read(fd, &header, sizeof(header)) != | ||
| 112 | - sizeof(header)) | 364 | + |
| 365 | + if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header)) | ||
| 113 | goto fail; | 366 | goto fail; |
| 114 | s->cluster_sectors = le32_to_cpu(header.granularity); | 367 | s->cluster_sectors = le32_to_cpu(header.granularity); |
| 115 | s->l2_size = 1 << 9; | 368 | s->l2_size = 1 << 9; |
| @@ -120,8 +373,8 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) | @@ -120,8 +373,8 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) | ||
| 120 | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; | 373 | s->l1_entry_sectors = s->l2_size * s->cluster_sectors; |
| 121 | } else if (magic == VMDK4_MAGIC) { | 374 | } else if (magic == VMDK4_MAGIC) { |
| 122 | VMDK4Header header; | 375 | VMDK4Header header; |
| 123 | - | ||
| 124 | - if (read(fd, &header, sizeof(header)) != sizeof(header)) | 376 | + |
| 377 | + if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header)) | ||
| 125 | goto fail; | 378 | goto fail; |
| 126 | bs->total_sectors = le64_to_cpu(header.capacity); | 379 | bs->total_sectors = le64_to_cpu(header.capacity); |
| 127 | s->cluster_sectors = le64_to_cpu(header.granularity); | 380 | s->cluster_sectors = le64_to_cpu(header.granularity); |
| @@ -133,17 +386,22 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) | @@ -133,17 +386,22 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) | ||
| 133 | / s->l1_entry_sectors; | 386 | / s->l1_entry_sectors; |
| 134 | s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9; | 387 | s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9; |
| 135 | s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9; | 388 | s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9; |
| 389 | + | ||
| 390 | + // try to open parent images, if exist | ||
| 391 | + if (vmdk_parent_open(bs, filename) != 0) | ||
| 392 | + goto fail; | ||
| 393 | + // write the CID once after the image creation | ||
| 394 | + s->parent_cid = vmdk_read_cid(bs,1); | ||
| 136 | } else { | 395 | } else { |
| 137 | goto fail; | 396 | goto fail; |
| 138 | } | 397 | } |
| 398 | + | ||
| 139 | /* read the L1 table */ | 399 | /* read the L1 table */ |
| 140 | l1_size = s->l1_size * sizeof(uint32_t); | 400 | l1_size = s->l1_size * sizeof(uint32_t); |
| 141 | s->l1_table = qemu_malloc(l1_size); | 401 | s->l1_table = qemu_malloc(l1_size); |
| 142 | if (!s->l1_table) | 402 | if (!s->l1_table) |
| 143 | goto fail; | 403 | goto fail; |
| 144 | - if (lseek(fd, s->l1_table_offset, SEEK_SET) == -1) | ||
| 145 | - goto fail; | ||
| 146 | - if (read(fd, s->l1_table, l1_size) != l1_size) | 404 | + if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size) |
| 147 | goto fail; | 405 | goto fail; |
| 148 | for(i = 0; i < s->l1_size; i++) { | 406 | for(i = 0; i < s->l1_size; i++) { |
| 149 | le32_to_cpus(&s->l1_table[i]); | 407 | le32_to_cpus(&s->l1_table[i]); |
| @@ -153,9 +411,7 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) | @@ -153,9 +411,7 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) | ||
| 153 | s->l1_backup_table = qemu_malloc(l1_size); | 411 | s->l1_backup_table = qemu_malloc(l1_size); |
| 154 | if (!s->l1_backup_table) | 412 | if (!s->l1_backup_table) |
| 155 | goto fail; | 413 | goto fail; |
| 156 | - if (lseek(fd, s->l1_backup_table_offset, SEEK_SET) == -1) | ||
| 157 | - goto fail; | ||
| 158 | - if (read(fd, s->l1_backup_table, l1_size) != l1_size) | 414 | + if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size) |
| 159 | goto fail; | 415 | goto fail; |
| 160 | for(i = 0; i < s->l1_size; i++) { | 416 | for(i = 0; i < s->l1_size; i++) { |
| 161 | le32_to_cpus(&s->l1_backup_table[i]); | 417 | le32_to_cpus(&s->l1_backup_table[i]); |
| @@ -165,16 +421,43 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) | @@ -165,16 +421,43 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags) | ||
| 165 | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t)); | 421 | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t)); |
| 166 | if (!s->l2_cache) | 422 | if (!s->l2_cache) |
| 167 | goto fail; | 423 | goto fail; |
| 168 | - s->fd = fd; | ||
| 169 | return 0; | 424 | return 0; |
| 170 | fail: | 425 | fail: |
| 171 | qemu_free(s->l1_backup_table); | 426 | qemu_free(s->l1_backup_table); |
| 172 | qemu_free(s->l1_table); | 427 | qemu_free(s->l1_table); |
| 173 | qemu_free(s->l2_cache); | 428 | qemu_free(s->l2_cache); |
| 174 | - close(fd); | 429 | + bdrv_delete(s->hd); |
| 175 | return -1; | 430 | return -1; |
| 176 | } | 431 | } |
| 177 | 432 | ||
| 433 | +static uint64_t get_cluster_offset(BlockDriverState *bs, uint64_t offset, int allocate); | ||
| 434 | + | ||
| 435 | +static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset, | ||
| 436 | + uint64_t offset, int allocate) | ||
| 437 | +{ | ||
| 438 | + uint64_t parent_cluster_offset; | ||
| 439 | + BDRVVmdkState *s = bs->opaque; | ||
| 440 | + uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB | ||
| 441 | + | ||
| 442 | + // we will be here if it's first write on non-exist grain(cluster). | ||
| 443 | + // try to read from parent image, if exist | ||
| 444 | + if (s->hd->backing_hd) { | ||
| 445 | + BDRVVmdkState *ps = s->hd->backing_hd->opaque; | ||
| 446 | + | ||
| 447 | + if (!vmdk_is_cid_valid(bs)) | ||
| 448 | + return -1; | ||
| 449 | + parent_cluster_offset = get_cluster_offset(s->hd->backing_hd, offset, allocate); | ||
| 450 | + if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) != | ||
| 451 | + ps->cluster_sectors*512) | ||
| 452 | + return -1; | ||
| 453 | + | ||
| 454 | + if (bdrv_pwrite(s->hd, cluster_offset << 9, whole_grain, sizeof(whole_grain)) != | ||
| 455 | + sizeof(whole_grain)) | ||
| 456 | + return -1; | ||
| 457 | + } | ||
| 458 | + return 0; | ||
| 459 | +} | ||
| 460 | + | ||
| 178 | static uint64_t get_cluster_offset(BlockDriverState *bs, | 461 | static uint64_t get_cluster_offset(BlockDriverState *bs, |
| 179 | uint64_t offset, int allocate) | 462 | uint64_t offset, int allocate) |
| 180 | { | 463 | { |
| @@ -212,34 +495,41 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, | @@ -212,34 +495,41 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, | ||
| 212 | } | 495 | } |
| 213 | } | 496 | } |
| 214 | l2_table = s->l2_cache + (min_index * s->l2_size); | 497 | l2_table = s->l2_cache + (min_index * s->l2_size); |
| 215 | - lseek(s->fd, (int64_t)l2_offset * 512, SEEK_SET); | ||
| 216 | - if (read(s->fd, l2_table, s->l2_size * sizeof(uint32_t)) != | ||
| 217 | - s->l2_size * sizeof(uint32_t)) | 498 | + if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) != |
| 499 | + s->l2_size * sizeof(uint32_t)) | ||
| 218 | return 0; | 500 | return 0; |
| 501 | + | ||
| 219 | s->l2_cache_offsets[min_index] = l2_offset; | 502 | s->l2_cache_offsets[min_index] = l2_offset; |
| 220 | s->l2_cache_counts[min_index] = 1; | 503 | s->l2_cache_counts[min_index] = 1; |
| 221 | found: | 504 | found: |
| 222 | l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size; | 505 | l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size; |
| 223 | cluster_offset = le32_to_cpu(l2_table[l2_index]); | 506 | cluster_offset = le32_to_cpu(l2_table[l2_index]); |
| 224 | if (!cluster_offset) { | 507 | if (!cluster_offset) { |
| 508 | + struct stat file_buf; | ||
| 509 | + | ||
| 225 | if (!allocate) | 510 | if (!allocate) |
| 226 | return 0; | 511 | return 0; |
| 227 | - cluster_offset = lseek(s->fd, 0, SEEK_END); | ||
| 228 | - ftruncate(s->fd, cluster_offset + (s->cluster_sectors << 9)); | 512 | + stat(s->hd->filename, &file_buf); |
| 513 | + cluster_offset = file_buf.st_size; | ||
| 514 | + bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9)); | ||
| 515 | + | ||
| 229 | cluster_offset >>= 9; | 516 | cluster_offset >>= 9; |
| 230 | /* update L2 table */ | 517 | /* update L2 table */ |
| 231 | tmp = cpu_to_le32(cluster_offset); | 518 | tmp = cpu_to_le32(cluster_offset); |
| 232 | l2_table[l2_index] = tmp; | 519 | l2_table[l2_index] = tmp; |
| 233 | - lseek(s->fd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), SEEK_SET); | ||
| 234 | - if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp)) | 520 | + if (bdrv_pwrite(s->hd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), |
| 521 | + &tmp, sizeof(tmp)) != sizeof(tmp)) | ||
| 235 | return 0; | 522 | return 0; |
| 236 | /* update backup L2 table */ | 523 | /* update backup L2 table */ |
| 237 | if (s->l1_backup_table_offset != 0) { | 524 | if (s->l1_backup_table_offset != 0) { |
| 238 | l2_offset = s->l1_backup_table[l1_index]; | 525 | l2_offset = s->l1_backup_table[l1_index]; |
| 239 | - lseek(s->fd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), SEEK_SET); | ||
| 240 | - if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp)) | 526 | + if (bdrv_pwrite(s->hd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), |
| 527 | + &tmp, sizeof(tmp)) != sizeof(tmp)) | ||
| 241 | return 0; | 528 | return 0; |
| 242 | } | 529 | } |
| 530 | + | ||
| 531 | + if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1) | ||
| 532 | + return 0; | ||
| 243 | } | 533 | } |
| 244 | cluster_offset <<= 9; | 534 | cluster_offset <<= 9; |
| 245 | return cluster_offset; | 535 | return cluster_offset; |
| @@ -265,9 +555,9 @@ static int vmdk_read(BlockDriverState *bs, int64_t sector_num, | @@ -265,9 +555,9 @@ static int vmdk_read(BlockDriverState *bs, int64_t sector_num, | ||
| 265 | uint8_t *buf, int nb_sectors) | 555 | uint8_t *buf, int nb_sectors) |
| 266 | { | 556 | { |
| 267 | BDRVVmdkState *s = bs->opaque; | 557 | BDRVVmdkState *s = bs->opaque; |
| 268 | - int ret, index_in_cluster, n; | 558 | + int index_in_cluster, n, ret; |
| 269 | uint64_t cluster_offset; | 559 | uint64_t cluster_offset; |
| 270 | - | 560 | + |
| 271 | while (nb_sectors > 0) { | 561 | while (nb_sectors > 0) { |
| 272 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0); | 562 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0); |
| 273 | index_in_cluster = sector_num % s->cluster_sectors; | 563 | index_in_cluster = sector_num % s->cluster_sectors; |
| @@ -275,11 +565,18 @@ static int vmdk_read(BlockDriverState *bs, int64_t sector_num, | @@ -275,11 +565,18 @@ static int vmdk_read(BlockDriverState *bs, int64_t sector_num, | ||
| 275 | if (n > nb_sectors) | 565 | if (n > nb_sectors) |
| 276 | n = nb_sectors; | 566 | n = nb_sectors; |
| 277 | if (!cluster_offset) { | 567 | if (!cluster_offset) { |
| 278 | - memset(buf, 0, 512 * n); | 568 | + // try to read from parent image, if exist |
| 569 | + if (s->hd->backing_hd) { | ||
| 570 | + if (!vmdk_is_cid_valid(bs)) | ||
| 571 | + return -1; | ||
| 572 | + ret = bdrv_read(s->hd->backing_hd, sector_num, buf, n); | ||
| 573 | + if (ret < 0) | ||
| 574 | + return -1; | ||
| 575 | + } else { | ||
| 576 | + memset(buf, 0, 512 * n); | ||
| 577 | + } | ||
| 279 | } else { | 578 | } else { |
| 280 | - lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET); | ||
| 281 | - ret = read(s->fd, buf, n * 512); | ||
| 282 | - if (ret != n * 512) | 579 | + if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) |
| 283 | return -1; | 580 | return -1; |
| 284 | } | 581 | } |
| 285 | nb_sectors -= n; | 582 | nb_sectors -= n; |
| @@ -293,8 +590,9 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num, | @@ -293,8 +590,9 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num, | ||
| 293 | const uint8_t *buf, int nb_sectors) | 590 | const uint8_t *buf, int nb_sectors) |
| 294 | { | 591 | { |
| 295 | BDRVVmdkState *s = bs->opaque; | 592 | BDRVVmdkState *s = bs->opaque; |
| 296 | - int ret, index_in_cluster, n; | 593 | + int index_in_cluster, n; |
| 297 | uint64_t cluster_offset; | 594 | uint64_t cluster_offset; |
| 595 | + static int cid_update = 0; | ||
| 298 | 596 | ||
| 299 | while (nb_sectors > 0) { | 597 | while (nb_sectors > 0) { |
| 300 | index_in_cluster = sector_num & (s->cluster_sectors - 1); | 598 | index_in_cluster = sector_num & (s->cluster_sectors - 1); |
| @@ -304,13 +602,17 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num, | @@ -304,13 +602,17 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num, | ||
| 304 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 1); | 602 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 1); |
| 305 | if (!cluster_offset) | 603 | if (!cluster_offset) |
| 306 | return -1; | 604 | return -1; |
| 307 | - lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET); | ||
| 308 | - ret = write(s->fd, buf, n * 512); | ||
| 309 | - if (ret != n * 512) | 605 | + if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512) |
| 310 | return -1; | 606 | return -1; |
| 311 | nb_sectors -= n; | 607 | nb_sectors -= n; |
| 312 | sector_num += n; | 608 | sector_num += n; |
| 313 | buf += n * 512; | 609 | buf += n * 512; |
| 610 | + | ||
| 611 | + // update CID on the first write every time the virtual disk is opened | ||
| 612 | + if (!cid_update) { | ||
| 613 | + vmdk_write_cid(bs, time(NULL)); | ||
| 614 | + cid_update++; | ||
| 615 | + } | ||
| 314 | } | 616 | } |
| 315 | return 0; | 617 | return 0; |
| 316 | } | 618 | } |
| @@ -334,7 +636,7 @@ static int vmdk_create(const char *filename, int64_t total_size, | @@ -334,7 +636,7 @@ static int vmdk_create(const char *filename, int64_t total_size, | ||
| 334 | "# The Disk Data Base \n" | 636 | "# The Disk Data Base \n" |
| 335 | "#DDB\n" | 637 | "#DDB\n" |
| 336 | "\n" | 638 | "\n" |
| 337 | - "ddb.virtualHWVersion = \"3\"\n" | 639 | + "ddb.virtualHWVersion = \"4\"\n" |
| 338 | "ddb.geometry.cylinders = \"%lu\"\n" | 640 | "ddb.geometry.cylinders = \"%lu\"\n" |
| 339 | "ddb.geometry.heads = \"16\"\n" | 641 | "ddb.geometry.heads = \"16\"\n" |
| 340 | "ddb.geometry.sectors = \"63\"\n" | 642 | "ddb.geometry.sectors = \"63\"\n" |
| @@ -343,6 +645,9 @@ static int vmdk_create(const char *filename, int64_t total_size, | @@ -343,6 +645,9 @@ static int vmdk_create(const char *filename, int64_t total_size, | ||
| 343 | const char *real_filename, *temp_str; | 645 | const char *real_filename, *temp_str; |
| 344 | 646 | ||
| 345 | /* XXX: add support for backing file */ | 647 | /* XXX: add support for backing file */ |
| 648 | + if (backing_file) { | ||
| 649 | + return vmdk_snapshot_create(filename, backing_file); | ||
| 650 | + } | ||
| 346 | 651 | ||
| 347 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, | 652 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
| 348 | 0644); | 653 | 0644); |
| @@ -421,15 +726,18 @@ static int vmdk_create(const char *filename, int64_t total_size, | @@ -421,15 +726,18 @@ static int vmdk_create(const char *filename, int64_t total_size, | ||
| 421 | static void vmdk_close(BlockDriverState *bs) | 726 | static void vmdk_close(BlockDriverState *bs) |
| 422 | { | 727 | { |
| 423 | BDRVVmdkState *s = bs->opaque; | 728 | BDRVVmdkState *s = bs->opaque; |
| 729 | + | ||
| 424 | qemu_free(s->l1_table); | 730 | qemu_free(s->l1_table); |
| 425 | qemu_free(s->l2_cache); | 731 | qemu_free(s->l2_cache); |
| 426 | - close(s->fd); | 732 | + bdrv_delete(s->hd); |
| 733 | + // try to close parent image, if exist | ||
| 734 | + vmdk_parent_close(s->hd); | ||
| 427 | } | 735 | } |
| 428 | 736 | ||
| 429 | static void vmdk_flush(BlockDriverState *bs) | 737 | static void vmdk_flush(BlockDriverState *bs) |
| 430 | { | 738 | { |
| 431 | BDRVVmdkState *s = bs->opaque; | 739 | BDRVVmdkState *s = bs->opaque; |
| 432 | - fsync(s->fd); | 740 | + bdrv_flush(s->hd); |
| 433 | } | 741 | } |
| 434 | 742 | ||
| 435 | BlockDriver bdrv_vmdk = { | 743 | BlockDriver bdrv_vmdk = { |