Revision 83f64091 block-qcow.c
b/block-qcow.c | ||
---|---|---|
1 | 1 |
/* |
2 | 2 |
* Block driver for the QCOW format |
3 | 3 |
* |
4 |
* Copyright (c) 2004 Fabrice Bellard |
|
4 |
* Copyright (c) 2004-2006 Fabrice Bellard
|
|
5 | 5 |
* |
6 | 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | 7 |
* of this software and associated documentation files (the "Software"), to deal |
... | ... | |
53 | 53 |
#define L2_CACHE_SIZE 16 |
54 | 54 |
|
55 | 55 |
typedef struct BDRVQcowState { |
56 |
int fd;
|
|
56 |
BlockDriverState *hd;
|
|
57 | 57 |
int cluster_bits; |
58 | 58 |
int cluster_size; |
59 | 59 |
int cluster_sectors; |
... | ... | |
89 | 89 |
return 0; |
90 | 90 |
} |
91 | 91 |
|
92 |
static int qcow_open(BlockDriverState *bs, const char *filename) |
|
92 |
static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
|
|
93 | 93 |
{ |
94 | 94 |
BDRVQcowState *s = bs->opaque; |
95 |
int fd, len, i, shift;
|
|
95 |
int len, i, shift, ret;
|
|
96 | 96 |
QCowHeader header; |
97 |
|
|
98 |
fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
|
99 |
if (fd < 0) { |
|
100 |
fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
|
101 |
if (fd < 0) |
|
102 |
return -1; |
|
103 |
} |
|
104 |
s->fd = fd; |
|
105 |
if (read(fd, &header, sizeof(header)) != sizeof(header)) |
|
97 |
|
|
98 |
ret = bdrv_file_open(&s->hd, filename, flags); |
|
99 |
if (ret < 0) |
|
100 |
return ret; |
|
101 |
if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header)) |
|
106 | 102 |
goto fail; |
107 | 103 |
be32_to_cpus(&header.magic); |
108 | 104 |
be32_to_cpus(&header.version); |
... | ... | |
138 | 134 |
s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t)); |
139 | 135 |
if (!s->l1_table) |
140 | 136 |
goto fail; |
141 |
lseek(fd, s->l1_table_offset, SEEK_SET); |
|
142 |
if (read(fd, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
|
137 |
if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
|
143 | 138 |
s->l1_size * sizeof(uint64_t)) |
144 | 139 |
goto fail; |
145 | 140 |
for(i = 0;i < s->l1_size; i++) { |
... | ... | |
162 | 157 |
len = header.backing_file_size; |
163 | 158 |
if (len > 1023) |
164 | 159 |
len = 1023; |
165 |
lseek(fd, header.backing_file_offset, SEEK_SET); |
|
166 |
if (read(fd, bs->backing_file, len) != len) |
|
160 |
if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len) |
|
167 | 161 |
goto fail; |
168 | 162 |
bs->backing_file[len] = '\0'; |
169 | 163 |
} |
... | ... | |
174 | 168 |
qemu_free(s->l2_cache); |
175 | 169 |
qemu_free(s->cluster_cache); |
176 | 170 |
qemu_free(s->cluster_data); |
177 |
close(fd);
|
|
171 |
bdrv_delete(s->hd);
|
|
178 | 172 |
return -1; |
179 | 173 |
} |
180 | 174 |
|
... | ... | |
276 | 270 |
if (!allocate) |
277 | 271 |
return 0; |
278 | 272 |
/* allocate a new l2 entry */ |
279 |
l2_offset = lseek(s->fd, 0, SEEK_END);
|
|
273 |
l2_offset = bdrv_getlength(s->hd);
|
|
280 | 274 |
/* round to cluster size */ |
281 | 275 |
l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1); |
282 | 276 |
/* update the L1 entry */ |
283 | 277 |
s->l1_table[l1_index] = l2_offset; |
284 | 278 |
tmp = cpu_to_be64(l2_offset); |
285 |
lseek(s->fd, s->l1_table_offset + l1_index * sizeof(tmp), SEEK_SET);
|
|
286 |
if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp))
|
|
279 |
if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp),
|
|
280 |
&tmp, sizeof(tmp)) != sizeof(tmp))
|
|
287 | 281 |
return 0; |
288 | 282 |
new_l2_table = 1; |
289 | 283 |
} |
... | ... | |
309 | 303 |
} |
310 | 304 |
} |
311 | 305 |
l2_table = s->l2_cache + (min_index << s->l2_bits); |
312 |
lseek(s->fd, l2_offset, SEEK_SET); |
|
313 | 306 |
if (new_l2_table) { |
314 | 307 |
memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); |
315 |
if (write(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) !=
|
|
308 |
if (bdrv_pwrite(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
|
|
316 | 309 |
s->l2_size * sizeof(uint64_t)) |
317 | 310 |
return 0; |
318 | 311 |
} else { |
319 |
if (read(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) !=
|
|
312 |
if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
|
|
320 | 313 |
s->l2_size * sizeof(uint64_t)) |
321 | 314 |
return 0; |
322 | 315 |
} |
... | ... | |
337 | 330 |
overwritten */ |
338 | 331 |
if (decompress_cluster(s, cluster_offset) < 0) |
339 | 332 |
return 0; |
340 |
cluster_offset = lseek(s->fd, 0, SEEK_END);
|
|
333 |
cluster_offset = bdrv_getlength(s->hd);
|
|
341 | 334 |
cluster_offset = (cluster_offset + s->cluster_size - 1) & |
342 | 335 |
~(s->cluster_size - 1); |
343 | 336 |
/* write the cluster content */ |
344 |
lseek(s->fd, cluster_offset, SEEK_SET); |
|
345 |
if (write(s->fd, s->cluster_cache, s->cluster_size) != |
|
337 |
if (bdrv_pwrite(s->hd, cluster_offset, s->cluster_cache, s->cluster_size) != |
|
346 | 338 |
s->cluster_size) |
347 | 339 |
return -1; |
348 | 340 |
} else { |
349 |
cluster_offset = lseek(s->fd, 0, SEEK_END);
|
|
341 |
cluster_offset = bdrv_getlength(s->hd);
|
|
350 | 342 |
if (allocate == 1) { |
351 | 343 |
/* round to cluster size */ |
352 | 344 |
cluster_offset = (cluster_offset + s->cluster_size - 1) & |
353 | 345 |
~(s->cluster_size - 1); |
354 |
ftruncate(s->fd, cluster_offset + s->cluster_size);
|
|
346 |
bdrv_truncate(s->hd, cluster_offset + s->cluster_size);
|
|
355 | 347 |
/* if encrypted, we must initialize the cluster |
356 | 348 |
content which won't be written */ |
357 | 349 |
if (s->crypt_method && |
... | ... | |
365 | 357 |
s->cluster_data, |
366 | 358 |
s->cluster_data + 512, 1, 1, |
367 | 359 |
&s->aes_encrypt_key); |
368 |
lseek(s->fd, cluster_offset + i * 512, SEEK_SET);
|
|
369 |
if (write(s->fd, s->cluster_data, 512) != 512)
|
|
360 |
if (bdrv_pwrite(s->hd, cluster_offset + i * 512,
|
|
361 |
s->cluster_data, 512) != 512)
|
|
370 | 362 |
return -1; |
371 | 363 |
} |
372 | 364 |
} |
... | ... | |
379 | 371 |
/* update L2 table */ |
380 | 372 |
tmp = cpu_to_be64(cluster_offset); |
381 | 373 |
l2_table[l2_index] = tmp; |
382 |
lseek(s->fd, l2_offset + l2_index * sizeof(tmp), SEEK_SET);
|
|
383 |
if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp))
|
|
374 |
if (bdrv_pwrite(s->hd,
|
|
375 |
l2_offset + l2_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp))
|
|
384 | 376 |
return 0; |
385 | 377 |
} |
386 | 378 |
return cluster_offset; |
... | ... | |
438 | 430 |
if (s->cluster_cache_offset != coffset) { |
439 | 431 |
csize = cluster_offset >> (63 - s->cluster_bits); |
440 | 432 |
csize &= (s->cluster_size - 1); |
441 |
lseek(s->fd, coffset, SEEK_SET); |
|
442 |
ret = read(s->fd, s->cluster_data, csize); |
|
433 |
ret = bdrv_pread(s->hd, coffset, s->cluster_data, csize); |
|
443 | 434 |
if (ret != csize) |
444 | 435 |
return -1; |
445 | 436 |
if (decompress_buffer(s->cluster_cache, s->cluster_size, |
... | ... | |
451 | 442 |
return 0; |
452 | 443 |
} |
453 | 444 |
|
445 |
#if 0 |
|
446 |
|
|
454 | 447 |
static int qcow_read(BlockDriverState *bs, int64_t sector_num, |
455 | 448 |
uint8_t *buf, int nb_sectors) |
456 | 449 |
{ |
... | ... | |
465 | 458 |
if (n > nb_sectors) |
466 | 459 |
n = nb_sectors; |
467 | 460 |
if (!cluster_offset) { |
468 |
memset(buf, 0, 512 * n); |
|
461 |
if (bs->backing_hd) { |
|
462 |
/* read from the base image */ |
|
463 |
ret = bdrv_read(bs->backing_hd, sector_num, buf, n); |
|
464 |
if (ret < 0) |
|
465 |
return -1; |
|
466 |
} else { |
|
467 |
memset(buf, 0, 512 * n); |
|
468 |
} |
|
469 | 469 |
} else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { |
470 | 470 |
if (decompress_cluster(s, cluster_offset) < 0) |
471 | 471 |
return -1; |
472 | 472 |
memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n); |
473 | 473 |
} else { |
474 |
lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET); |
|
475 |
ret = read(s->fd, buf, n * 512); |
|
474 |
ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); |
|
476 | 475 |
if (ret != n * 512) |
477 | 476 |
return -1; |
478 | 477 |
if (s->crypt_method) { |
... | ... | |
486 | 485 |
} |
487 | 486 |
return 0; |
488 | 487 |
} |
488 |
#endif |
|
489 | 489 |
|
490 | 490 |
static int qcow_write(BlockDriverState *bs, int64_t sector_num, |
491 | 491 |
const uint8_t *buf, int nb_sectors) |
... | ... | |
504 | 504 |
index_in_cluster + n); |
505 | 505 |
if (!cluster_offset) |
506 | 506 |
return -1; |
507 |
lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET); |
|
508 | 507 |
if (s->crypt_method) { |
509 | 508 |
encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1, |
510 | 509 |
&s->aes_encrypt_key); |
511 |
ret = write(s->fd, s->cluster_data, n * 512); |
|
510 |
ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, |
|
511 |
s->cluster_data, n * 512); |
|
512 | 512 |
} else { |
513 |
ret = write(s->fd, buf, n * 512);
|
|
513 |
ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
|
|
514 | 514 |
} |
515 | 515 |
if (ret != n * 512) |
516 | 516 |
return -1; |
... | ... | |
522 | 522 |
return 0; |
523 | 523 |
} |
524 | 524 |
|
525 |
typedef struct { |
|
526 |
int64_t sector_num; |
|
527 |
uint8_t *buf; |
|
528 |
int nb_sectors; |
|
529 |
int n; |
|
530 |
uint64_t cluster_offset; |
|
531 |
uint8_t *cluster_data; |
|
532 |
BlockDriverAIOCB *hd_aiocb; |
|
533 |
BlockDriverAIOCB *backing_hd_aiocb; |
|
534 |
} QCowAIOCB; |
|
535 |
|
|
536 |
static void qcow_aio_delete(BlockDriverAIOCB *acb); |
|
537 |
|
|
538 |
static int qcow_aio_new(BlockDriverAIOCB *acb) |
|
539 |
{ |
|
540 |
BlockDriverState *bs = acb->bs; |
|
541 |
BDRVQcowState *s = bs->opaque; |
|
542 |
QCowAIOCB *acb1; |
|
543 |
acb1 = qemu_mallocz(sizeof(QCowAIOCB)); |
|
544 |
if (!acb1) |
|
545 |
return -1; |
|
546 |
acb->opaque = acb1; |
|
547 |
acb1->hd_aiocb = bdrv_aio_new(s->hd); |
|
548 |
if (!acb1->hd_aiocb) |
|
549 |
goto fail; |
|
550 |
if (bs->backing_hd) { |
|
551 |
acb1->backing_hd_aiocb = bdrv_aio_new(bs->backing_hd); |
|
552 |
if (!acb1->backing_hd_aiocb) |
|
553 |
goto fail; |
|
554 |
} |
|
555 |
return 0; |
|
556 |
fail: |
|
557 |
qcow_aio_delete(acb); |
|
558 |
return -1; |
|
559 |
} |
|
560 |
|
|
561 |
static void qcow_aio_read_cb(void *opaque, int ret) |
|
562 |
{ |
|
563 |
BlockDriverAIOCB *acb = opaque; |
|
564 |
BlockDriverState *bs = acb->bs; |
|
565 |
BDRVQcowState *s = bs->opaque; |
|
566 |
QCowAIOCB *acb1 = acb->opaque; |
|
567 |
int index_in_cluster; |
|
568 |
|
|
569 |
if (ret < 0) { |
|
570 |
fail: |
|
571 |
acb->cb(acb->cb_opaque, ret); |
|
572 |
return; |
|
573 |
} |
|
574 |
|
|
575 |
redo: |
|
576 |
/* post process the read buffer */ |
|
577 |
if (!acb1->cluster_offset) { |
|
578 |
/* nothing to do */ |
|
579 |
} else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
|
580 |
/* nothing to do */ |
|
581 |
} else { |
|
582 |
if (s->crypt_method) { |
|
583 |
encrypt_sectors(s, acb1->sector_num, acb1->buf, acb1->buf, |
|
584 |
acb1->n, 0, |
|
585 |
&s->aes_decrypt_key); |
|
586 |
} |
|
587 |
} |
|
588 |
|
|
589 |
acb1->nb_sectors -= acb1->n; |
|
590 |
acb1->sector_num += acb1->n; |
|
591 |
acb1->buf += acb1->n * 512; |
|
592 |
|
|
593 |
if (acb1->nb_sectors == 0) { |
|
594 |
/* request completed */ |
|
595 |
acb->cb(acb->cb_opaque, 0); |
|
596 |
return; |
|
597 |
} |
|
598 |
|
|
599 |
/* prepare next AIO request */ |
|
600 |
acb1->cluster_offset = get_cluster_offset(bs, |
|
601 |
acb1->sector_num << 9, |
|
602 |
0, 0, 0, 0); |
|
603 |
index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1); |
|
604 |
acb1->n = s->cluster_sectors - index_in_cluster; |
|
605 |
if (acb1->n > acb1->nb_sectors) |
|
606 |
acb1->n = acb1->nb_sectors; |
|
607 |
|
|
608 |
if (!acb1->cluster_offset) { |
|
609 |
if (bs->backing_hd) { |
|
610 |
/* read from the base image */ |
|
611 |
ret = bdrv_aio_read(acb1->backing_hd_aiocb, acb1->sector_num, |
|
612 |
acb1->buf, acb1->n, qcow_aio_read_cb, acb); |
|
613 |
if (ret < 0) |
|
614 |
goto fail; |
|
615 |
} else { |
|
616 |
/* Note: in this case, no need to wait */ |
|
617 |
memset(acb1->buf, 0, 512 * acb1->n); |
|
618 |
goto redo; |
|
619 |
} |
|
620 |
} else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
|
621 |
/* add AIO support for compressed blocks ? */ |
|
622 |
if (decompress_cluster(s, acb1->cluster_offset) < 0) |
|
623 |
goto fail; |
|
624 |
memcpy(acb1->buf, |
|
625 |
s->cluster_cache + index_in_cluster * 512, 512 * acb1->n); |
|
626 |
goto redo; |
|
627 |
} else { |
|
628 |
if ((acb1->cluster_offset & 511) != 0) { |
|
629 |
ret = -EIO; |
|
630 |
goto fail; |
|
631 |
} |
|
632 |
ret = bdrv_aio_read(acb1->hd_aiocb, |
|
633 |
(acb1->cluster_offset >> 9) + index_in_cluster, |
|
634 |
acb1->buf, acb1->n, qcow_aio_read_cb, acb); |
|
635 |
if (ret < 0) |
|
636 |
goto fail; |
|
637 |
} |
|
638 |
} |
|
639 |
|
|
640 |
static int qcow_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, |
|
641 |
uint8_t *buf, int nb_sectors) |
|
642 |
{ |
|
643 |
QCowAIOCB *acb1 = acb->opaque; |
|
644 |
|
|
645 |
acb1->sector_num = sector_num; |
|
646 |
acb1->buf = buf; |
|
647 |
acb1->nb_sectors = nb_sectors; |
|
648 |
acb1->n = 0; |
|
649 |
acb1->cluster_offset = 0; |
|
650 |
|
|
651 |
qcow_aio_read_cb(acb, 0); |
|
652 |
} |
|
653 |
|
|
654 |
static void qcow_aio_write_cb(void *opaque, int ret) |
|
655 |
{ |
|
656 |
BlockDriverAIOCB *acb = opaque; |
|
657 |
BlockDriverState *bs = acb->bs; |
|
658 |
BDRVQcowState *s = bs->opaque; |
|
659 |
QCowAIOCB *acb1 = acb->opaque; |
|
660 |
int index_in_cluster; |
|
661 |
uint64_t cluster_offset; |
|
662 |
const uint8_t *src_buf; |
|
663 |
|
|
664 |
if (ret < 0) { |
|
665 |
fail: |
|
666 |
acb->cb(acb->cb_opaque, ret); |
|
667 |
return; |
|
668 |
} |
|
669 |
|
|
670 |
acb1->nb_sectors -= acb1->n; |
|
671 |
acb1->sector_num += acb1->n; |
|
672 |
acb1->buf += acb1->n * 512; |
|
673 |
|
|
674 |
if (acb1->nb_sectors == 0) { |
|
675 |
/* request completed */ |
|
676 |
acb->cb(acb->cb_opaque, 0); |
|
677 |
return; |
|
678 |
} |
|
679 |
|
|
680 |
index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1); |
|
681 |
acb1->n = s->cluster_sectors - index_in_cluster; |
|
682 |
if (acb1->n > acb1->nb_sectors) |
|
683 |
acb1->n = acb1->nb_sectors; |
|
684 |
cluster_offset = get_cluster_offset(bs, acb1->sector_num << 9, 1, 0, |
|
685 |
index_in_cluster, |
|
686 |
index_in_cluster + acb1->n); |
|
687 |
if (!cluster_offset || (cluster_offset & 511) != 0) { |
|
688 |
ret = -EIO; |
|
689 |
goto fail; |
|
690 |
} |
|
691 |
if (s->crypt_method) { |
|
692 |
if (!acb1->cluster_data) { |
|
693 |
acb1->cluster_data = qemu_mallocz(s->cluster_size); |
|
694 |
if (!acb1->cluster_data) { |
|
695 |
ret = -ENOMEM; |
|
696 |
goto fail; |
|
697 |
} |
|
698 |
} |
|
699 |
encrypt_sectors(s, acb1->sector_num, acb1->cluster_data, acb1->buf, |
|
700 |
acb1->n, 1, &s->aes_encrypt_key); |
|
701 |
src_buf = acb1->cluster_data; |
|
702 |
} else { |
|
703 |
src_buf = acb1->buf; |
|
704 |
} |
|
705 |
ret = bdrv_aio_write(acb1->hd_aiocb, |
|
706 |
(cluster_offset >> 9) + index_in_cluster, |
|
707 |
src_buf, acb1->n, |
|
708 |
qcow_aio_write_cb, acb); |
|
709 |
if (ret < 0) |
|
710 |
goto fail; |
|
711 |
} |
|
712 |
|
|
713 |
static int qcow_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, |
|
714 |
const uint8_t *buf, int nb_sectors) |
|
715 |
{ |
|
716 |
QCowAIOCB *acb1 = acb->opaque; |
|
717 |
BlockDriverState *bs = acb->bs; |
|
718 |
BDRVQcowState *s = bs->opaque; |
|
719 |
|
|
720 |
s->cluster_cache_offset = -1; /* disable compressed cache */ |
|
721 |
|
|
722 |
acb1->sector_num = sector_num; |
|
723 |
acb1->buf = (uint8_t *)buf; |
|
724 |
acb1->nb_sectors = nb_sectors; |
|
725 |
acb1->n = 0; |
|
726 |
|
|
727 |
qcow_aio_write_cb(acb, 0); |
|
728 |
} |
|
729 |
|
|
730 |
static void qcow_aio_cancel(BlockDriverAIOCB *acb) |
|
731 |
{ |
|
732 |
QCowAIOCB *acb1 = acb->opaque; |
|
733 |
if (acb1->hd_aiocb) |
|
734 |
bdrv_aio_cancel(acb1->hd_aiocb); |
|
735 |
if (acb1->backing_hd_aiocb) |
|
736 |
bdrv_aio_cancel(acb1->backing_hd_aiocb); |
|
737 |
} |
|
738 |
|
|
739 |
static void qcow_aio_delete(BlockDriverAIOCB *acb) |
|
740 |
{ |
|
741 |
QCowAIOCB *acb1 = acb->opaque; |
|
742 |
if (acb1->hd_aiocb) |
|
743 |
bdrv_aio_delete(acb1->hd_aiocb); |
|
744 |
if (acb1->backing_hd_aiocb) |
|
745 |
bdrv_aio_delete(acb1->backing_hd_aiocb); |
|
746 |
qemu_free(acb1->cluster_data); |
|
747 |
qemu_free(acb1); |
|
748 |
} |
|
749 |
|
|
525 | 750 |
static void qcow_close(BlockDriverState *bs) |
526 | 751 |
{ |
527 | 752 |
BDRVQcowState *s = bs->opaque; |
... | ... | |
529 | 754 |
qemu_free(s->l2_cache); |
530 | 755 |
qemu_free(s->cluster_cache); |
531 | 756 |
qemu_free(s->cluster_data); |
532 |
close(s->fd);
|
|
757 |
bdrv_delete(s->hd);
|
|
533 | 758 |
} |
534 | 759 |
|
535 | 760 |
static int qcow_create(const char *filename, int64_t total_size, |
... | ... | |
537 | 762 |
{ |
538 | 763 |
int fd, header_size, backing_filename_len, l1_size, i, shift; |
539 | 764 |
QCowHeader header; |
540 |
char backing_filename[1024]; |
|
541 | 765 |
uint64_t tmp; |
542 |
struct stat st; |
|
543 | 766 |
|
544 |
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
|
545 |
0644); |
|
767 |
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); |
|
546 | 768 |
if (fd < 0) |
547 | 769 |
return -1; |
548 | 770 |
memset(&header, 0, sizeof(header)); |
... | ... | |
552 | 774 |
header_size = sizeof(header); |
553 | 775 |
backing_filename_len = 0; |
554 | 776 |
if (backing_file) { |
555 |
if (strcmp(backing_file, "fat:")) { |
|
556 |
const char *p; |
|
557 |
/* XXX: this is a hack: we do not attempt to check for URL |
|
558 |
like syntax */ |
|
559 |
p = strchr(backing_file, ':'); |
|
560 |
if (p && (p - backing_file) >= 2) { |
|
561 |
/* URL like but exclude "c:" like filenames */ |
|
562 |
pstrcpy(backing_filename, sizeof(backing_filename), |
|
563 |
backing_file); |
|
564 |
} else { |
|
565 |
realpath(backing_file, backing_filename); |
|
566 |
if (stat(backing_filename, &st) != 0) { |
|
567 |
return -1; |
|
568 |
} |
|
569 |
} |
|
570 |
header.backing_file_offset = cpu_to_be64(header_size); |
|
571 |
backing_filename_len = strlen(backing_filename); |
|
572 |
header.backing_file_size = cpu_to_be32(backing_filename_len); |
|
573 |
header_size += backing_filename_len; |
|
574 |
} else |
|
575 |
backing_file = NULL; |
|
576 |
header.mtime = cpu_to_be32(st.st_mtime); |
|
777 |
header.backing_file_offset = cpu_to_be64(header_size); |
|
778 |
backing_filename_len = strlen(backing_file); |
|
779 |
header.backing_file_size = cpu_to_be32(backing_filename_len); |
|
780 |
header_size += backing_filename_len; |
|
781 |
header.mtime = cpu_to_be32(0); |
|
577 | 782 |
header.cluster_bits = 9; /* 512 byte cluster to avoid copying |
578 | 783 |
unmodifyed sectors */ |
579 | 784 |
header.l2_bits = 12; /* 32 KB L2 tables */ |
... | ... | |
595 | 800 |
/* write all the data */ |
596 | 801 |
write(fd, &header, sizeof(header)); |
597 | 802 |
if (backing_file) { |
598 |
write(fd, backing_filename, backing_filename_len);
|
|
803 |
write(fd, backing_file, backing_filename_len); |
|
599 | 804 |
} |
600 | 805 |
lseek(fd, header_size, SEEK_SET); |
601 | 806 |
tmp = 0; |
... | ... | |
610 | 815 |
{ |
611 | 816 |
BDRVQcowState *s = bs->opaque; |
612 | 817 |
uint32_t l1_length = s->l1_size * sizeof(uint64_t); |
818 |
int ret; |
|
613 | 819 |
|
614 | 820 |
memset(s->l1_table, 0, l1_length); |
615 |
lseek(s->fd, s->l1_table_offset, SEEK_SET); |
|
616 |
if (write(s->fd, s->l1_table, l1_length) < 0) |
|
821 |
if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0) |
|
617 | 822 |
return -1; |
618 |
ftruncate(s->fd, s->l1_table_offset + l1_length); |
|
823 |
ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length); |
|
824 |
if (ret < 0) |
|
825 |
return ret; |
|
619 | 826 |
|
620 | 827 |
memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); |
621 | 828 |
memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t)); |
... | ... | |
682 | 889 |
cluster_offset = get_cluster_offset(bs, sector_num << 9, 2, |
683 | 890 |
out_len, 0, 0); |
684 | 891 |
cluster_offset &= s->cluster_offset_mask; |
685 |
lseek(s->fd, cluster_offset, SEEK_SET); |
|
686 |
if (write(s->fd, out_buf, out_len) != out_len) { |
|
892 |
if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) { |
|
687 | 893 |
qemu_free(out_buf); |
688 | 894 |
return -1; |
689 | 895 |
} |
... | ... | |
696 | 902 |
static void qcow_flush(BlockDriverState *bs) |
697 | 903 |
{ |
698 | 904 |
BDRVQcowState *s = bs->opaque; |
699 |
fsync(s->fd);
|
|
905 |
bdrv_flush(s->hd);
|
|
700 | 906 |
} |
701 | 907 |
|
702 | 908 |
BlockDriver bdrv_qcow = { |
... | ... | |
704 | 910 |
sizeof(BDRVQcowState), |
705 | 911 |
qcow_probe, |
706 | 912 |
qcow_open, |
707 |
qcow_read,
|
|
708 |
qcow_write,
|
|
913 |
NULL,
|
|
914 |
NULL,
|
|
709 | 915 |
qcow_close, |
710 | 916 |
qcow_create, |
711 | 917 |
qcow_flush, |
712 | 918 |
qcow_is_allocated, |
713 | 919 |
qcow_set_key, |
714 |
qcow_make_empty |
|
920 |
qcow_make_empty, |
|
921 |
|
|
922 |
.bdrv_aio_new = qcow_aio_new, |
|
923 |
.bdrv_aio_read = qcow_aio_read, |
|
924 |
.bdrv_aio_write = qcow_aio_write, |
|
925 |
.bdrv_aio_cancel = qcow_aio_cancel, |
|
926 |
.bdrv_aio_delete = qcow_aio_delete, |
|
715 | 927 |
}; |
716 | 928 |
|
717 | 929 |
|
Also available in: Unified diff