Revision 45aba42f block/qcow2.c
b/block/qcow2.c | ||
---|---|---|
78 | 78 |
} QCowSnapshotHeader; |
79 | 79 |
|
80 | 80 |
|
81 |
static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset); |
|
82 |
static int qcow_read(BlockDriverState *bs, int64_t sector_num, |
|
83 |
uint8_t *buf, int nb_sectors); |
|
84 | 81 |
static int qcow_read_snapshots(BlockDriverState *bs); |
85 | 82 |
static void qcow_free_snapshots(BlockDriverState *bs); |
86 | 83 |
|
... | ... | |
334 | 331 |
return 0; |
335 | 332 |
} |
336 | 333 |
|
337 |
/* The crypt function is compatible with the linux cryptoloop |
|
338 |
algorithm for < 4 GB images. NOTE: out_buf == in_buf is |
|
339 |
supported */ |
|
340 |
static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num, |
|
341 |
uint8_t *out_buf, const uint8_t *in_buf, |
|
342 |
int nb_sectors, int enc, |
|
343 |
const AES_KEY *key) |
|
344 |
{ |
|
345 |
union { |
|
346 |
uint64_t ll[2]; |
|
347 |
uint8_t b[16]; |
|
348 |
} ivec; |
|
349 |
int i; |
|
350 |
|
|
351 |
for(i = 0; i < nb_sectors; i++) { |
|
352 |
ivec.ll[0] = cpu_to_le64(sector_num); |
|
353 |
ivec.ll[1] = 0; |
|
354 |
AES_cbc_encrypt(in_buf, out_buf, 512, key, |
|
355 |
ivec.b, enc); |
|
356 |
sector_num++; |
|
357 |
in_buf += 512; |
|
358 |
out_buf += 512; |
|
359 |
} |
|
360 |
} |
|
361 |
|
|
362 |
static int copy_sectors(BlockDriverState *bs, uint64_t start_sect, |
|
363 |
uint64_t cluster_offset, int n_start, int n_end) |
|
364 |
{ |
|
365 |
BDRVQcowState *s = bs->opaque; |
|
366 |
int n, ret; |
|
367 |
|
|
368 |
n = n_end - n_start; |
|
369 |
if (n <= 0) |
|
370 |
return 0; |
|
371 |
ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n); |
|
372 |
if (ret < 0) |
|
373 |
return ret; |
|
374 |
if (s->crypt_method) { |
|
375 |
encrypt_sectors(s, start_sect + n_start, |
|
376 |
s->cluster_data, |
|
377 |
s->cluster_data, n, 1, |
|
378 |
&s->aes_encrypt_key); |
|
379 |
} |
|
380 |
ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start, |
|
381 |
s->cluster_data, n); |
|
382 |
if (ret < 0) |
|
383 |
return ret; |
|
384 |
return 0; |
|
385 |
} |
|
386 |
|
|
387 |
void l2_cache_reset(BlockDriverState *bs) |
|
388 |
{ |
|
389 |
BDRVQcowState *s = bs->opaque; |
|
390 |
|
|
391 |
memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); |
|
392 |
memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t)); |
|
393 |
memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t)); |
|
394 |
} |
|
395 |
|
|
396 |
static inline int l2_cache_new_entry(BlockDriverState *bs) |
|
397 |
{ |
|
398 |
BDRVQcowState *s = bs->opaque; |
|
399 |
uint32_t min_count; |
|
400 |
int min_index, i; |
|
401 |
|
|
402 |
/* find a new entry in the least used one */ |
|
403 |
min_index = 0; |
|
404 |
min_count = 0xffffffff; |
|
405 |
for(i = 0; i < L2_CACHE_SIZE; i++) { |
|
406 |
if (s->l2_cache_counts[i] < min_count) { |
|
407 |
min_count = s->l2_cache_counts[i]; |
|
408 |
min_index = i; |
|
409 |
} |
|
410 |
} |
|
411 |
return min_index; |
|
412 |
} |
|
413 |
|
|
414 | 334 |
static int64_t align_offset(int64_t offset, int n) |
415 | 335 |
{ |
416 | 336 |
offset = (offset + n - 1) & ~(n - 1); |
417 | 337 |
return offset; |
418 | 338 |
} |
419 | 339 |
|
420 |
static int grow_l1_table(BlockDriverState *bs, int min_size) |
|
421 |
{ |
|
422 |
BDRVQcowState *s = bs->opaque; |
|
423 |
int new_l1_size, new_l1_size2, ret, i; |
|
424 |
uint64_t *new_l1_table; |
|
425 |
uint64_t new_l1_table_offset; |
|
426 |
uint8_t data[12]; |
|
427 |
|
|
428 |
new_l1_size = s->l1_size; |
|
429 |
if (min_size <= new_l1_size) |
|
430 |
return 0; |
|
431 |
while (min_size > new_l1_size) { |
|
432 |
new_l1_size = (new_l1_size * 3 + 1) / 2; |
|
433 |
} |
|
434 |
#ifdef DEBUG_ALLOC2 |
|
435 |
printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size); |
|
436 |
#endif |
|
437 |
|
|
438 |
new_l1_size2 = sizeof(uint64_t) * new_l1_size; |
|
439 |
new_l1_table = qemu_mallocz(new_l1_size2); |
|
440 |
memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t)); |
|
441 |
|
|
442 |
/* write new table (align to cluster) */ |
|
443 |
new_l1_table_offset = alloc_clusters(bs, new_l1_size2); |
|
444 |
|
|
445 |
for(i = 0; i < s->l1_size; i++) |
|
446 |
new_l1_table[i] = cpu_to_be64(new_l1_table[i]); |
|
447 |
ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2); |
|
448 |
if (ret != new_l1_size2) |
|
449 |
goto fail; |
|
450 |
for(i = 0; i < s->l1_size; i++) |
|
451 |
new_l1_table[i] = be64_to_cpu(new_l1_table[i]); |
|
452 |
|
|
453 |
/* set new table */ |
|
454 |
cpu_to_be32w((uint32_t*)data, new_l1_size); |
|
455 |
cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset); |
|
456 |
if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data, |
|
457 |
sizeof(data)) != sizeof(data)) |
|
458 |
goto fail; |
|
459 |
qemu_free(s->l1_table); |
|
460 |
free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t)); |
|
461 |
s->l1_table_offset = new_l1_table_offset; |
|
462 |
s->l1_table = new_l1_table; |
|
463 |
s->l1_size = new_l1_size; |
|
464 |
return 0; |
|
465 |
fail: |
|
466 |
qemu_free(s->l1_table); |
|
467 |
return -EIO; |
|
468 |
} |
|
469 |
|
|
470 |
/* |
|
471 |
* seek_l2_table |
|
472 |
* |
|
473 |
* seek l2_offset in the l2_cache table |
|
474 |
* if not found, return NULL, |
|
475 |
* if found, |
|
476 |
* increments the l2 cache hit count of the entry, |
|
477 |
* if counter overflow, divide by two all counters |
|
478 |
* return the pointer to the l2 cache entry |
|
479 |
* |
|
480 |
*/ |
|
481 |
|
|
482 |
static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset) |
|
483 |
{ |
|
484 |
int i, j; |
|
485 |
|
|
486 |
for(i = 0; i < L2_CACHE_SIZE; i++) { |
|
487 |
if (l2_offset == s->l2_cache_offsets[i]) { |
|
488 |
/* increment the hit count */ |
|
489 |
if (++s->l2_cache_counts[i] == 0xffffffff) { |
|
490 |
for(j = 0; j < L2_CACHE_SIZE; j++) { |
|
491 |
s->l2_cache_counts[j] >>= 1; |
|
492 |
} |
|
493 |
} |
|
494 |
return s->l2_cache + (i << s->l2_bits); |
|
495 |
} |
|
496 |
} |
|
497 |
return NULL; |
|
498 |
} |
|
499 |
|
|
500 |
/* |
|
501 |
* l2_load |
|
502 |
* |
|
503 |
* Loads a L2 table into memory. If the table is in the cache, the cache |
|
504 |
* is used; otherwise the L2 table is loaded from the image file. |
|
505 |
* |
|
506 |
* Returns a pointer to the L2 table on success, or NULL if the read from |
|
507 |
* the image file failed. |
|
508 |
*/ |
|
509 |
|
|
510 |
static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset) |
|
511 |
{ |
|
512 |
BDRVQcowState *s = bs->opaque; |
|
513 |
int min_index; |
|
514 |
uint64_t *l2_table; |
|
515 |
|
|
516 |
/* seek if the table for the given offset is in the cache */ |
|
517 |
|
|
518 |
l2_table = seek_l2_table(s, l2_offset); |
|
519 |
if (l2_table != NULL) |
|
520 |
return l2_table; |
|
521 |
|
|
522 |
/* not found: load a new entry in the least used one */ |
|
523 |
|
|
524 |
min_index = l2_cache_new_entry(bs); |
|
525 |
l2_table = s->l2_cache + (min_index << s->l2_bits); |
|
526 |
if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != |
|
527 |
s->l2_size * sizeof(uint64_t)) |
|
528 |
return NULL; |
|
529 |
s->l2_cache_offsets[min_index] = l2_offset; |
|
530 |
s->l2_cache_counts[min_index] = 1; |
|
531 |
|
|
532 |
return l2_table; |
|
533 |
} |
|
534 |
|
|
535 |
/* |
|
536 |
* l2_allocate |
|
537 |
* |
|
538 |
* Allocate a new l2 entry in the file. If l1_index points to an already |
|
539 |
* used entry in the L2 table (i.e. we are doing a copy on write for the L2 |
|
540 |
* table) copy the contents of the old L2 table into the newly allocated one. |
|
541 |
* Otherwise the new table is initialized with zeros. |
|
542 |
* |
|
543 |
*/ |
|
544 |
|
|
545 |
static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index) |
|
546 |
{ |
|
547 |
BDRVQcowState *s = bs->opaque; |
|
548 |
int min_index; |
|
549 |
uint64_t old_l2_offset, tmp; |
|
550 |
uint64_t *l2_table, l2_offset; |
|
551 |
|
|
552 |
old_l2_offset = s->l1_table[l1_index]; |
|
553 |
|
|
554 |
/* allocate a new l2 entry */ |
|
555 |
|
|
556 |
l2_offset = alloc_clusters(bs, s->l2_size * sizeof(uint64_t)); |
|
557 |
|
|
558 |
/* update the L1 entry */ |
|
559 |
|
|
560 |
s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED; |
|
561 |
|
|
562 |
tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED); |
|
563 |
if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp), |
|
564 |
&tmp, sizeof(tmp)) != sizeof(tmp)) |
|
565 |
return NULL; |
|
566 |
|
|
567 |
/* allocate a new entry in the l2 cache */ |
|
568 |
|
|
569 |
min_index = l2_cache_new_entry(bs); |
|
570 |
l2_table = s->l2_cache + (min_index << s->l2_bits); |
|
571 |
|
|
572 |
if (old_l2_offset == 0) { |
|
573 |
/* if there was no old l2 table, clear the new table */ |
|
574 |
memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); |
|
575 |
} else { |
|
576 |
/* if there was an old l2 table, read it from the disk */ |
|
577 |
if (bdrv_pread(s->hd, old_l2_offset, |
|
578 |
l2_table, s->l2_size * sizeof(uint64_t)) != |
|
579 |
s->l2_size * sizeof(uint64_t)) |
|
580 |
return NULL; |
|
581 |
} |
|
582 |
/* write the l2 table to the file */ |
|
583 |
if (bdrv_pwrite(s->hd, l2_offset, |
|
584 |
l2_table, s->l2_size * sizeof(uint64_t)) != |
|
585 |
s->l2_size * sizeof(uint64_t)) |
|
586 |
return NULL; |
|
587 |
|
|
588 |
/* update the l2 cache entry */ |
|
589 |
|
|
590 |
s->l2_cache_offsets[min_index] = l2_offset; |
|
591 |
s->l2_cache_counts[min_index] = 1; |
|
592 |
|
|
593 |
return l2_table; |
|
594 |
} |
|
595 |
|
|
596 |
static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size, |
|
597 |
uint64_t *l2_table, uint64_t start, uint64_t mask) |
|
598 |
{ |
|
599 |
int i; |
|
600 |
uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask; |
|
601 |
|
|
602 |
if (!offset) |
|
603 |
return 0; |
|
604 |
|
|
605 |
for (i = start; i < start + nb_clusters; i++) |
|
606 |
if (offset + i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask)) |
|
607 |
break; |
|
608 |
|
|
609 |
return (i - start); |
|
610 |
} |
|
611 |
|
|
612 |
static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table) |
|
613 |
{ |
|
614 |
int i = 0; |
|
615 |
|
|
616 |
while(nb_clusters-- && l2_table[i] == 0) |
|
617 |
i++; |
|
618 |
|
|
619 |
return i; |
|
620 |
} |
|
621 |
|
|
622 |
/* |
|
623 |
* get_cluster_offset |
|
624 |
* |
|
625 |
* For a given offset of the disk image, return cluster offset in |
|
626 |
* qcow2 file. |
|
627 |
* |
|
628 |
* on entry, *num is the number of contiguous clusters we'd like to |
|
629 |
* access following offset. |
|
630 |
* |
|
631 |
* on exit, *num is the number of contiguous clusters we can read. |
|
632 |
* |
|
633 |
* Return 1, if the offset is found |
|
634 |
* Return 0, otherwise. |
|
635 |
* |
|
636 |
*/ |
|
637 |
|
|
638 |
static uint64_t get_cluster_offset(BlockDriverState *bs, |
|
639 |
uint64_t offset, int *num) |
|
640 |
{ |
|
641 |
BDRVQcowState *s = bs->opaque; |
|
642 |
int l1_index, l2_index; |
|
643 |
uint64_t l2_offset, *l2_table, cluster_offset; |
|
644 |
int l1_bits, c; |
|
645 |
int index_in_cluster, nb_available, nb_needed, nb_clusters; |
|
646 |
|
|
647 |
index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1); |
|
648 |
nb_needed = *num + index_in_cluster; |
|
649 |
|
|
650 |
l1_bits = s->l2_bits + s->cluster_bits; |
|
651 |
|
|
652 |
/* compute how many bytes there are between the offset and |
|
653 |
* the end of the l1 entry |
|
654 |
*/ |
|
655 |
|
|
656 |
nb_available = (1 << l1_bits) - (offset & ((1 << l1_bits) - 1)); |
|
657 |
|
|
658 |
/* compute the number of available sectors */ |
|
659 |
|
|
660 |
nb_available = (nb_available >> 9) + index_in_cluster; |
|
661 |
|
|
662 |
if (nb_needed > nb_available) { |
|
663 |
nb_needed = nb_available; |
|
664 |
} |
|
665 |
|
|
666 |
cluster_offset = 0; |
|
667 |
|
|
668 |
/* seek the the l2 offset in the l1 table */ |
|
669 |
|
|
670 |
l1_index = offset >> l1_bits; |
|
671 |
if (l1_index >= s->l1_size) |
|
672 |
goto out; |
|
673 |
|
|
674 |
l2_offset = s->l1_table[l1_index]; |
|
675 |
|
|
676 |
/* seek the l2 table of the given l2 offset */ |
|
677 |
|
|
678 |
if (!l2_offset) |
|
679 |
goto out; |
|
680 |
|
|
681 |
/* load the l2 table in memory */ |
|
682 |
|
|
683 |
l2_offset &= ~QCOW_OFLAG_COPIED; |
|
684 |
l2_table = l2_load(bs, l2_offset); |
|
685 |
if (l2_table == NULL) |
|
686 |
return 0; |
|
687 |
|
|
688 |
/* find the cluster offset for the given disk offset */ |
|
689 |
|
|
690 |
l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); |
|
691 |
cluster_offset = be64_to_cpu(l2_table[l2_index]); |
|
692 |
nb_clusters = size_to_clusters(s, nb_needed << 9); |
|
693 |
|
|
694 |
if (!cluster_offset) { |
|
695 |
/* how many empty clusters ? */ |
|
696 |
c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]); |
|
697 |
} else { |
|
698 |
/* how many allocated clusters ? */ |
|
699 |
c = count_contiguous_clusters(nb_clusters, s->cluster_size, |
|
700 |
&l2_table[l2_index], 0, QCOW_OFLAG_COPIED); |
|
701 |
} |
|
702 |
|
|
703 |
nb_available = (c * s->cluster_sectors); |
|
704 |
out: |
|
705 |
if (nb_available > nb_needed) |
|
706 |
nb_available = nb_needed; |
|
707 |
|
|
708 |
*num = nb_available - index_in_cluster; |
|
709 |
|
|
710 |
return cluster_offset & ~QCOW_OFLAG_COPIED; |
|
711 |
} |
|
712 |
|
|
713 |
/* |
|
714 |
* free_any_clusters |
|
715 |
* |
|
716 |
* free clusters according to its type: compressed or not |
|
717 |
* |
|
718 |
*/ |
|
719 |
|
|
720 |
static void free_any_clusters(BlockDriverState *bs, |
|
721 |
uint64_t cluster_offset, int nb_clusters) |
|
722 |
{ |
|
723 |
BDRVQcowState *s = bs->opaque; |
|
724 |
|
|
725 |
/* free the cluster */ |
|
726 |
|
|
727 |
if (cluster_offset & QCOW_OFLAG_COMPRESSED) { |
|
728 |
int nb_csectors; |
|
729 |
nb_csectors = ((cluster_offset >> s->csize_shift) & |
|
730 |
s->csize_mask) + 1; |
|
731 |
free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511, |
|
732 |
nb_csectors * 512); |
|
733 |
return; |
|
734 |
} |
|
735 |
|
|
736 |
free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits); |
|
737 |
|
|
738 |
return; |
|
739 |
} |
|
740 |
|
|
741 |
/* |
|
742 |
* get_cluster_table |
|
743 |
* |
|
744 |
* for a given disk offset, load (and allocate if needed) |
|
745 |
* the l2 table. |
|
746 |
* |
|
747 |
* the l2 table offset in the qcow2 file and the cluster index |
|
748 |
* in the l2 table are given to the caller. |
|
749 |
* |
|
750 |
*/ |
|
751 |
|
|
752 |
static int get_cluster_table(BlockDriverState *bs, uint64_t offset, |
|
753 |
uint64_t **new_l2_table, |
|
754 |
uint64_t *new_l2_offset, |
|
755 |
int *new_l2_index) |
|
756 |
{ |
|
757 |
BDRVQcowState *s = bs->opaque; |
|
758 |
int l1_index, l2_index, ret; |
|
759 |
uint64_t l2_offset, *l2_table; |
|
760 |
|
|
761 |
/* seek the the l2 offset in the l1 table */ |
|
762 |
|
|
763 |
l1_index = offset >> (s->l2_bits + s->cluster_bits); |
|
764 |
if (l1_index >= s->l1_size) { |
|
765 |
ret = grow_l1_table(bs, l1_index + 1); |
|
766 |
if (ret < 0) |
|
767 |
return 0; |
|
768 |
} |
|
769 |
l2_offset = s->l1_table[l1_index]; |
|
770 |
|
|
771 |
/* seek the l2 table of the given l2 offset */ |
|
772 |
|
|
773 |
if (l2_offset & QCOW_OFLAG_COPIED) { |
|
774 |
/* load the l2 table in memory */ |
|
775 |
l2_offset &= ~QCOW_OFLAG_COPIED; |
|
776 |
l2_table = l2_load(bs, l2_offset); |
|
777 |
if (l2_table == NULL) |
|
778 |
return 0; |
|
779 |
} else { |
|
780 |
if (l2_offset) |
|
781 |
free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t)); |
|
782 |
l2_table = l2_allocate(bs, l1_index); |
|
783 |
if (l2_table == NULL) |
|
784 |
return 0; |
|
785 |
l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED; |
|
786 |
} |
|
787 |
|
|
788 |
/* find the cluster offset for the given disk offset */ |
|
789 |
|
|
790 |
l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); |
|
791 |
|
|
792 |
*new_l2_table = l2_table; |
|
793 |
*new_l2_offset = l2_offset; |
|
794 |
*new_l2_index = l2_index; |
|
795 |
|
|
796 |
return 1; |
|
797 |
} |
|
798 |
|
|
799 |
/* |
|
800 |
* alloc_compressed_cluster_offset |
|
801 |
* |
|
802 |
* For a given offset of the disk image, return cluster offset in |
|
803 |
* qcow2 file. |
|
804 |
* |
|
805 |
* If the offset is not found, allocate a new compressed cluster. |
|
806 |
* |
|
807 |
* Return the cluster offset if successful, |
|
808 |
* Return 0, otherwise. |
|
809 |
* |
|
810 |
*/ |
|
811 |
|
|
812 |
static uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs, |
|
813 |
uint64_t offset, |
|
814 |
int compressed_size) |
|
815 |
{ |
|
816 |
BDRVQcowState *s = bs->opaque; |
|
817 |
int l2_index, ret; |
|
818 |
uint64_t l2_offset, *l2_table, cluster_offset; |
|
819 |
int nb_csectors; |
|
820 |
|
|
821 |
ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index); |
|
822 |
if (ret == 0) |
|
823 |
return 0; |
|
824 |
|
|
825 |
cluster_offset = be64_to_cpu(l2_table[l2_index]); |
|
826 |
if (cluster_offset & QCOW_OFLAG_COPIED) |
|
827 |
return cluster_offset & ~QCOW_OFLAG_COPIED; |
|
828 |
|
|
829 |
if (cluster_offset) |
|
830 |
free_any_clusters(bs, cluster_offset, 1); |
|
831 |
|
|
832 |
cluster_offset = alloc_bytes(bs, compressed_size); |
|
833 |
nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) - |
|
834 |
(cluster_offset >> 9); |
|
835 |
|
|
836 |
cluster_offset |= QCOW_OFLAG_COMPRESSED | |
|
837 |
((uint64_t)nb_csectors << s->csize_shift); |
|
838 |
|
|
839 |
/* update L2 table */ |
|
840 |
|
|
841 |
/* compressed clusters never have the copied flag */ |
|
842 |
|
|
843 |
l2_table[l2_index] = cpu_to_be64(cluster_offset); |
|
844 |
if (bdrv_pwrite(s->hd, |
|
845 |
l2_offset + l2_index * sizeof(uint64_t), |
|
846 |
l2_table + l2_index, |
|
847 |
sizeof(uint64_t)) != sizeof(uint64_t)) |
|
848 |
return 0; |
|
849 |
|
|
850 |
return cluster_offset; |
|
851 |
} |
|
852 |
|
|
853 |
typedef struct QCowL2Meta |
|
854 |
{ |
|
855 |
uint64_t offset; |
|
856 |
int n_start; |
|
857 |
int nb_available; |
|
858 |
int nb_clusters; |
|
859 |
} QCowL2Meta; |
|
860 |
|
|
861 |
static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset, |
|
862 |
QCowL2Meta *m) |
|
863 |
{ |
|
864 |
BDRVQcowState *s = bs->opaque; |
|
865 |
int i, j = 0, l2_index, ret; |
|
866 |
uint64_t *old_cluster, start_sect, l2_offset, *l2_table; |
|
867 |
|
|
868 |
if (m->nb_clusters == 0) |
|
869 |
return 0; |
|
870 |
|
|
871 |
old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t)); |
|
872 |
|
|
873 |
/* copy content of unmodified sectors */ |
|
874 |
start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9; |
|
875 |
if (m->n_start) { |
|
876 |
ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start); |
|
877 |
if (ret < 0) |
|
878 |
goto err; |
|
879 |
} |
|
880 |
|
|
881 |
if (m->nb_available & (s->cluster_sectors - 1)) { |
|
882 |
uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1); |
|
883 |
ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9), |
|
884 |
m->nb_available - end, s->cluster_sectors); |
|
885 |
if (ret < 0) |
|
886 |
goto err; |
|
887 |
} |
|
888 |
|
|
889 |
ret = -EIO; |
|
890 |
/* update L2 table */ |
|
891 |
if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index)) |
|
892 |
goto err; |
|
893 |
|
|
894 |
for (i = 0; i < m->nb_clusters; i++) { |
|
895 |
/* if two concurrent writes happen to the same unallocated cluster |
|
896 |
* each write allocates separate cluster and writes data concurrently. |
|
897 |
* The first one to complete updates l2 table with pointer to its |
|
898 |
* cluster the second one has to do RMW (which is done above by |
|
899 |
* copy_sectors()), update l2 table with its cluster pointer and free |
|
900 |
* old cluster. This is what this loop does */ |
|
901 |
if(l2_table[l2_index + i] != 0) |
|
902 |
old_cluster[j++] = l2_table[l2_index + i]; |
|
903 |
|
|
904 |
l2_table[l2_index + i] = cpu_to_be64((cluster_offset + |
|
905 |
(i << s->cluster_bits)) | QCOW_OFLAG_COPIED); |
|
906 |
} |
|
907 |
|
|
908 |
if (bdrv_pwrite(s->hd, l2_offset + l2_index * sizeof(uint64_t), |
|
909 |
l2_table + l2_index, m->nb_clusters * sizeof(uint64_t)) != |
|
910 |
m->nb_clusters * sizeof(uint64_t)) |
|
911 |
goto err; |
|
912 |
|
|
913 |
for (i = 0; i < j; i++) |
|
914 |
free_any_clusters(bs, be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, |
|
915 |
1); |
|
916 |
|
|
917 |
ret = 0; |
|
918 |
err: |
|
919 |
qemu_free(old_cluster); |
|
920 |
return ret; |
|
921 |
} |
|
922 |
|
|
923 |
/* |
|
924 |
* alloc_cluster_offset |
|
925 |
* |
|
926 |
* For a given offset of the disk image, return cluster offset in |
|
927 |
* qcow2 file. |
|
928 |
* |
|
929 |
* If the offset is not found, allocate a new cluster. |
|
930 |
* |
|
931 |
* Return the cluster offset if successful, |
|
932 |
* Return 0, otherwise. |
|
933 |
* |
|
934 |
*/ |
|
935 |
|
|
936 |
static uint64_t alloc_cluster_offset(BlockDriverState *bs, |
|
937 |
uint64_t offset, |
|
938 |
int n_start, int n_end, |
|
939 |
int *num, QCowL2Meta *m) |
|
940 |
{ |
|
941 |
BDRVQcowState *s = bs->opaque; |
|
942 |
int l2_index, ret; |
|
943 |
uint64_t l2_offset, *l2_table, cluster_offset; |
|
944 |
int nb_clusters, i = 0; |
|
945 |
|
|
946 |
ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index); |
|
947 |
if (ret == 0) |
|
948 |
return 0; |
|
949 |
|
|
950 |
nb_clusters = size_to_clusters(s, n_end << 9); |
|
951 |
|
|
952 |
nb_clusters = MIN(nb_clusters, s->l2_size - l2_index); |
|
953 |
|
|
954 |
cluster_offset = be64_to_cpu(l2_table[l2_index]); |
|
955 |
|
|
956 |
/* We keep all QCOW_OFLAG_COPIED clusters */ |
|
957 |
|
|
958 |
if (cluster_offset & QCOW_OFLAG_COPIED) { |
|
959 |
nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size, |
|
960 |
&l2_table[l2_index], 0, 0); |
|
961 |
|
|
962 |
cluster_offset &= ~QCOW_OFLAG_COPIED; |
|
963 |
m->nb_clusters = 0; |
|
964 |
|
|
965 |
goto out; |
|
966 |
} |
|
967 |
|
|
968 |
/* for the moment, multiple compressed clusters are not managed */ |
|
969 |
|
|
970 |
if (cluster_offset & QCOW_OFLAG_COMPRESSED) |
|
971 |
nb_clusters = 1; |
|
972 |
|
|
973 |
/* how many available clusters ? */ |
|
974 |
|
|
975 |
while (i < nb_clusters) { |
|
976 |
i += count_contiguous_clusters(nb_clusters - i, s->cluster_size, |
|
977 |
&l2_table[l2_index], i, 0); |
|
978 |
|
|
979 |
if(be64_to_cpu(l2_table[l2_index + i])) |
|
980 |
break; |
|
981 |
|
|
982 |
i += count_contiguous_free_clusters(nb_clusters - i, |
|
983 |
&l2_table[l2_index + i]); |
|
984 |
|
|
985 |
cluster_offset = be64_to_cpu(l2_table[l2_index + i]); |
|
986 |
|
|
987 |
if ((cluster_offset & QCOW_OFLAG_COPIED) || |
|
988 |
(cluster_offset & QCOW_OFLAG_COMPRESSED)) |
|
989 |
break; |
|
990 |
} |
|
991 |
nb_clusters = i; |
|
992 |
|
|
993 |
/* allocate a new cluster */ |
|
994 |
|
|
995 |
cluster_offset = alloc_clusters(bs, nb_clusters * s->cluster_size); |
|
996 |
|
|
997 |
/* save info needed for meta data update */ |
|
998 |
m->offset = offset; |
|
999 |
m->n_start = n_start; |
|
1000 |
m->nb_clusters = nb_clusters; |
|
1001 |
|
|
1002 |
out: |
|
1003 |
m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end); |
|
1004 |
|
|
1005 |
*num = m->nb_available - n_start; |
|
1006 |
|
|
1007 |
return cluster_offset; |
|
1008 |
} |
|
1009 |
|
|
1010 | 340 |
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
1011 | 341 |
int nb_sectors, int *pnum) |
1012 | 342 |
{ |
... | ... | |
1018 | 348 |
return (cluster_offset != 0); |
1019 | 349 |
} |
1020 | 350 |
|
1021 |
static int decompress_buffer(uint8_t *out_buf, int out_buf_size, |
|
1022 |
const uint8_t *buf, int buf_size) |
|
1023 |
{ |
|
1024 |
z_stream strm1, *strm = &strm1; |
|
1025 |
int ret, out_len; |
|
1026 |
|
|
1027 |
memset(strm, 0, sizeof(*strm)); |
|
1028 |
|
|
1029 |
strm->next_in = (uint8_t *)buf; |
|
1030 |
strm->avail_in = buf_size; |
|
1031 |
strm->next_out = out_buf; |
|
1032 |
strm->avail_out = out_buf_size; |
|
1033 |
|
|
1034 |
ret = inflateInit2(strm, -12); |
|
1035 |
if (ret != Z_OK) |
|
1036 |
return -1; |
|
1037 |
ret = inflate(strm, Z_FINISH); |
|
1038 |
out_len = strm->next_out - out_buf; |
|
1039 |
if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || |
|
1040 |
out_len != out_buf_size) { |
|
1041 |
inflateEnd(strm); |
|
1042 |
return -1; |
|
1043 |
} |
|
1044 |
inflateEnd(strm); |
|
1045 |
return 0; |
|
1046 |
} |
|
1047 |
|
|
1048 |
static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset) |
|
1049 |
{ |
|
1050 |
int ret, csize, nb_csectors, sector_offset; |
|
1051 |
uint64_t coffset; |
|
1052 |
|
|
1053 |
coffset = cluster_offset & s->cluster_offset_mask; |
|
1054 |
if (s->cluster_cache_offset != coffset) { |
|
1055 |
nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1; |
|
1056 |
sector_offset = coffset & 511; |
|
1057 |
csize = nb_csectors * 512 - sector_offset; |
|
1058 |
ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors); |
|
1059 |
if (ret < 0) { |
|
1060 |
return -1; |
|
1061 |
} |
|
1062 |
if (decompress_buffer(s->cluster_cache, s->cluster_size, |
|
1063 |
s->cluster_data + sector_offset, csize) < 0) { |
|
1064 |
return -1; |
|
1065 |
} |
|
1066 |
s->cluster_cache_offset = coffset; |
|
1067 |
} |
|
1068 |
return 0; |
|
1069 |
} |
|
1070 |
|
|
1071 | 351 |
/* handle reading after the end of the backing file */ |
1072 |
static int backing_read1(BlockDriverState *bs,
|
|
1073 |
int64_t sector_num, uint8_t *buf, int nb_sectors)
|
|
352 |
int backing_read1(BlockDriverState *bs, |
|
353 |
int64_t sector_num, uint8_t *buf, int nb_sectors) |
|
1074 | 354 |
{ |
1075 | 355 |
int n1; |
1076 | 356 |
if ((sector_num + nb_sectors) <= bs->total_sectors) |
... | ... | |
1083 | 363 |
return n1; |
1084 | 364 |
} |
1085 | 365 |
|
1086 |
static int qcow_read(BlockDriverState *bs, int64_t sector_num, |
|
1087 |
uint8_t *buf, int nb_sectors) |
|
1088 |
{ |
|
1089 |
BDRVQcowState *s = bs->opaque; |
|
1090 |
int ret, index_in_cluster, n, n1; |
|
1091 |
uint64_t cluster_offset; |
|
1092 |
|
|
1093 |
while (nb_sectors > 0) { |
|
1094 |
n = nb_sectors; |
|
1095 |
cluster_offset = get_cluster_offset(bs, sector_num << 9, &n); |
|
1096 |
index_in_cluster = sector_num & (s->cluster_sectors - 1); |
|
1097 |
if (!cluster_offset) { |
|
1098 |
if (bs->backing_hd) { |
|
1099 |
/* read from the base image */ |
|
1100 |
n1 = backing_read1(bs->backing_hd, sector_num, buf, n); |
|
1101 |
if (n1 > 0) { |
|
1102 |
ret = bdrv_read(bs->backing_hd, sector_num, buf, n1); |
|
1103 |
if (ret < 0) |
|
1104 |
return -1; |
|
1105 |
} |
|
1106 |
} else { |
|
1107 |
memset(buf, 0, 512 * n); |
|
1108 |
} |
|
1109 |
} else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { |
|
1110 |
if (decompress_cluster(s, cluster_offset) < 0) |
|
1111 |
return -1; |
|
1112 |
memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n); |
|
1113 |
} else { |
|
1114 |
ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); |
|
1115 |
if (ret != n * 512) |
|
1116 |
return -1; |
|
1117 |
if (s->crypt_method) { |
|
1118 |
encrypt_sectors(s, sector_num, buf, buf, n, 0, |
|
1119 |
&s->aes_decrypt_key); |
|
1120 |
} |
|
1121 |
} |
|
1122 |
nb_sectors -= n; |
|
1123 |
sector_num += n; |
|
1124 |
buf += n * 512; |
|
1125 |
} |
|
1126 |
return 0; |
|
1127 |
} |
|
1128 |
|
|
1129 | 366 |
typedef struct QCowAIOCB { |
1130 | 367 |
BlockDriverAIOCB common; |
1131 | 368 |
int64_t sector_num; |
Also available in: Unified diff