Commit 4c1612d95412bcc487e0c734ad5b738105b67b19
Committed by
Anthony Liguori
1 parent
6583e3c7
alloc_cluster_link_l2: Write complete sectors
When updating the L2 tables in alloc_cluster_link_l2(), write complete sectors instead of updating single entries. Signed-off-by: Kevin Wolf <kwolf@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing
1 changed file
with
25 additions
and
3 deletions
block/qcow2-cluster.c
... | ... | @@ -578,6 +578,28 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
578 | 578 | return cluster_offset; |
579 | 579 | } |
580 | 580 | |
581 | +/* | |
582 | + * Write L2 table updates to disk, writing whole sectors to avoid a | |
583 | + * read-modify-write in bdrv_pwrite | |
584 | + */ | |
585 | +#define L2_ENTRIES_PER_SECTOR (512 / 8) | |
586 | +static int write_l2_entries(BDRVQcowState *s, uint64_t *l2_table, | |
587 | + uint64_t l2_offset, int l2_index, int num) | |
588 | +{ | |
589 | + int l2_start_index = l2_index & ~(L1_ENTRIES_PER_SECTOR - 1); | |
590 | + int start_offset = (8 * l2_index) & ~511; | |
591 | + int end_offset = (8 * (l2_index + num) + 511) & ~511; | |
592 | + size_t len = end_offset - start_offset; | |
593 | + | |
594 | + if (bdrv_pwrite(s->hd, l2_offset + start_offset, &l2_table[l2_start_index], | |
595 | + len) != len) | |
596 | + { | |
597 | + return -1; | |
598 | + } | |
599 | + | |
600 | + return 0; | |
601 | +} | |
602 | + | |
581 | 603 | int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset, |
582 | 604 | QCowL2Meta *m) |
583 | 605 | { |
... | ... | @@ -625,10 +647,10 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset, |
625 | 647 | (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); |
626 | 648 | } |
627 | 649 | |
628 | - if (bdrv_pwrite(s->hd, l2_offset + l2_index * sizeof(uint64_t), | |
629 | - l2_table + l2_index, m->nb_clusters * sizeof(uint64_t)) != | |
630 | - m->nb_clusters * sizeof(uint64_t)) | |
650 | + if (write_l2_entries(s, l2_table, l2_offset, l2_index, m->nb_clusters) < 0) { | |
651 | + ret = -1; | |
631 | 652 | goto err; |
653 | + } | |
632 | 654 | |
633 | 655 | for (i = 0; i < j; i++) |
634 | 656 | qcow2_free_any_clusters(bs, | ... | ... |