From d0988af1c414d35d231a60daebe5ee706daab86b Mon Sep 17 00:00:00 2001 From: Kevin Wolf <kwolf@redhat.com> Date: Fri, 25 Jun 2010 08:06:15 -0300 Subject: [PATCH 18/18] qcow2: Use bdrv_(p)write_sync for metadata writes RH-Author: Kevin Wolf <kwolf@redhat.com> Message-id: <1277453175-7489-3-git-send-email-kwolf@redhat.com> Patchwork-id: 10220 O-Subject: [RHEL-5.6/5.5.z KVM PATCH v2 2/2] qcow2: Use bdrv_(p)write_sync for metadata writes Bugzilla: 572825 RH-Acked-by: Jes Sorensen <Jes.Sorensen@redhat.com> RH-Acked-by: Juan Quintela <quintela@redhat.com> RH-Acked-by: Christoph Hellwig <chellwig@redhat.com> Bugzilla: 572825 Upstream status: 8b3b7206 is the upstream equivalent Use bdrv_(p)write_sync to ensure metadata integrity in case of a crash. Signed-off-by: Kevin Wolf <kwolf@redhat.com> --- qemu/block-qcow2.c | 70 ++++++++++++++++++++++++++-------------------------- 1 files changed, 35 insertions(+), 35 deletions(-) Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> --- qemu/block-qcow2.c | 70 ++++++++++++++++++++++++++-------------------------- 1 files changed, 35 insertions(+), 35 deletions(-) diff --git a/qemu/block-qcow2.c b/qemu/block-qcow2.c index 5306a50..5647350 100644 --- a/qemu/block-qcow2.c +++ b/qemu/block-qcow2.c @@ -500,7 +500,7 @@ static int copy_sectors(BlockDriverState *bs, uint64_t start_sect, s->cluster_data, n, 1, &s->aes_encrypt_cipher); } - ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start, + ret = bdrv_write_sync(s->hd, (cluster_offset >> 9) + n_start, s->cluster_data, n); if (ret < 0) return ret; @@ -567,8 +567,9 @@ static int grow_l1_table(BlockDriverState *bs, int min_size) for(i = 0; i < s->l1_size; i++) new_l1_table[i] = cpu_to_be64(new_l1_table[i]); - ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2); - if (ret != new_l1_size2) + ret = bdrv_pwrite_sync(s->hd, new_l1_table_offset, new_l1_table, + new_l1_size2); + if (ret < 0) goto fail; for(i = 0; i < s->l1_size; i++) new_l1_table[i] = be64_to_cpu(new_l1_table[i]); @@ -576,8 +577,9 @@ static int grow_l1_table(BlockDriverState *bs, int min_size) /* set new table */ cpu_to_be32w((uint32_t*)data, new_l1_size); cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset); - ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data,sizeof(data)); - if (ret != sizeof(data)) { + ret = bdrv_pwrite_sync(s->hd, offsetof(QCowHeader, l1_size), data, + sizeof(data)); + if (ret < 0) { goto fail; } qemu_free(s->l1_table); @@ -678,7 +680,7 @@ static int write_l1_entry(BDRVQcowState *s, int l1_index) buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]); } - ret = bdrv_pwrite(s->hd, s->l1_table_offset + 8 * l1_start_index, + ret = bdrv_pwrite_sync(s->hd, s->l1_table_offset + 8 * l1_start_index, buf, sizeof(buf)); if (ret < 0) { return ret; @@ -732,7 +734,7 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table) } } /* write the l2 table to the file */ - ret = bdrv_pwrite(s->hd, l2_offset, l2_table, + ret = bdrv_pwrite_sync(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)); if (ret < 0) { goto fail; @@ -1025,10 +1027,10 @@ static uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs, /* compressed clusters never have the copied flag */ l2_table[l2_index] = cpu_to_be64(cluster_offset); - if (bdrv_pwrite(s->hd, + if (bdrv_pwrite_sync(s->hd, l2_offset + l2_index * sizeof(uint64_t), l2_table + l2_index, - sizeof(uint64_t)) != sizeof(uint64_t)) + sizeof(uint64_t)) < 0) return 0; return cluster_offset; @@ -1057,7 +1059,7 @@ static int write_l2_entries(BDRVQcowState *s, uint64_t *l2_table, size_t len = end_offset - start_offset; int ret; - ret = bdrv_pwrite(s->hd, l2_offset + start_offset, + ret = bdrv_pwrite_sync(s->hd, l2_offset + start_offset, &l2_table[l2_start_index], len); if (ret < 0) { return ret; @@ -1748,7 +1750,7 @@ static int qcow2_update_ext_header(BlockDriverState *bs, backing_file_offset = sizeof(QCowHeader) + offset; } - ret = bdrv_pwrite(s->hd, sizeof(QCowHeader), buf, ext_size); + ret = bdrv_pwrite_sync(s->hd, sizeof(QCowHeader), buf, ext_size); if (ret < 0) { goto fail; } @@ -1757,13 +1759,13 @@ static int qcow2_update_ext_header(BlockDriverState *bs, uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset); uint32_t be_backing_file_size = cpu_to_be32(backing_file_len); - ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, backing_file_offset), + ret = bdrv_pwrite_sync(s->hd, offsetof(QCowHeader, backing_file_offset), &be_backing_file_offset, sizeof(uint64_t)); if (ret < 0) { goto fail; } - ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, backing_file_size), + ret = bdrv_pwrite_sync(s->hd, offsetof(QCowHeader, backing_file_size), &be_backing_file_size, sizeof(uint32_t)); if (ret < 0) { goto fail; @@ -2134,8 +2136,7 @@ static int update_snapshot_refcount(BlockDriverState *bs, } } if (l2_modified) { - if (bdrv_pwrite(s->hd, - l2_offset, l2_table, l2_size) != l2_size) + if (bdrv_pwrite_sync(s->hd, l2_offset, l2_table, l2_size) < 0) goto fail; } @@ -2158,8 +2159,8 @@ static int update_snapshot_refcount(BlockDriverState *bs, if (l1_modified) { for(i = 0; i < l1_size; i++) cpu_to_be64s(&l1_table[i]); - if (bdrv_pwrite(s->hd, l1_table_offset, l1_table, - l1_size2) != l1_size2) + if (bdrv_pwrite_sync(s->hd, l1_table_offset, l1_table, + l1_size2) < 0) goto fail; for(i = 0; i < l1_size; i++) be64_to_cpus(&l1_table[i]); @@ -2287,25 +2288,25 @@ static int qcow_write_snapshots(BlockDriverState *bs) h.id_str_size = cpu_to_be16(id_str_size); h.name_size = cpu_to_be16(name_size); offset = align_offset(offset, 8); - if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h)) + if (bdrv_pwrite_sync(s->hd, offset, &h, sizeof(h)) < 0) goto fail; offset += sizeof(h); - if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size) + if (bdrv_pwrite_sync(s->hd, offset, sn->id_str, id_str_size) < 0) goto fail; offset += id_str_size; - if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size) + if (bdrv_pwrite_sync(s->hd, offset, sn->name, name_size) < 0) goto fail; offset += name_size; } /* update the various header fields */ data64 = cpu_to_be64(snapshots_offset); - if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset), - &data64, sizeof(data64)) != sizeof(data64)) + if (bdrv_pwrite_sync(s->hd, offsetof(QCowHeader, snapshots_offset), + &data64, sizeof(data64)) < 0) goto fail; data32 = cpu_to_be32(s->nb_snapshots); - if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots), - &data32, sizeof(data32)) != sizeof(data32)) + if (bdrv_pwrite_sync(s->hd, offsetof(QCowHeader, nb_snapshots), + &data32, sizeof(data32)) < 0) goto fail; /* free the old snapshot table */ @@ -2411,9 +2412,8 @@ static int qcow_snapshot_create(BlockDriverState *bs, for(i = 0; i < s->l1_size; i++) { l1_table[i] = cpu_to_be64(s->l1_table[i]); } - if (bdrv_pwrite(s->hd, sn->l1_table_offset, - l1_table, s->l1_size * sizeof(uint64_t)) != - (s->l1_size * sizeof(uint64_t))) + if (bdrv_pwrite_sync(s->hd, sn->l1_table_offset, + l1_table, s->l1_size * sizeof(uint64_t)) < 0) goto fail; qemu_free(l1_table); l1_table = NULL; @@ -2463,8 +2463,8 @@ static int qcow_snapshot_goto(BlockDriverState *bs, if (bdrv_pread(s->hd, sn->l1_table_offset, s->l1_table, l1_size2) != l1_size2) goto fail; - if (bdrv_pwrite(s->hd, s->l1_table_offset, - s->l1_table, l1_size2) != l1_size2) + if (bdrv_pwrite_sync(s->hd, s->l1_table_offset, + s->l1_table, l1_size2) < 0) goto fail; for(i = 0;i < s->l1_size; i++) { be64_to_cpus(&s->l1_table[i]); @@ -2863,7 +2863,7 @@ static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index) } /* Now the new refcount block needs to be written to disk */ - ret = bdrv_pwrite(s->hd, new_block, s->refcount_block_cache, + ret = bdrv_pwrite_sync(s->hd, new_block, s->refcount_block_cache, s->cluster_size); if (ret < 0) { goto fail_block; @@ -2872,7 +2872,7 @@ static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index) /* If the refcount table is big enough, just hook the block up there */ if (refcount_table_index < s->refcount_table_size) { uint64_t data64 = cpu_to_be64(new_block); - ret = bdrv_pwrite(s->hd, + ret = bdrv_pwrite_sync(s->hd, s->refcount_table_offset + refcount_table_index * sizeof(uint64_t), &data64, sizeof(data64)); if (ret < 0) { @@ -2949,7 +2949,7 @@ static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index) } /* Write refcount blocks to disk */ - ret = bdrv_pwrite(s->hd, meta_offset, new_blocks, + ret = bdrv_pwrite_sync(s->hd, meta_offset, new_blocks, blocks_clusters * s->cluster_size); qemu_free(new_blocks); if (ret < 0) { @@ -2961,7 +2961,7 @@ static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index) cpu_to_be64s(&new_table[i]); } - ret = bdrv_pwrite(s->hd, table_offset, new_table, + ret = bdrv_pwrite_sync(s->hd, table_offset, new_table, table_size * sizeof(uint64_t)); if (ret < 0) { goto fail_table; @@ -2975,7 +2975,7 @@ static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index) uint8_t data[12]; cpu_to_be64w((uint64_t*)data, table_offset); cpu_to_be32w((uint32_t*)(data + 8), table_clusters); - ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset), + ret = bdrv_pwrite_sync(s->hd, offsetof(QCowHeader, refcount_table_offset), data, sizeof(data)); if (ret < 0) { goto fail_table; @@ -3048,7 +3048,7 @@ static int write_refcount_block_entries(BDRVQcowState *s, size = (last_index - first_index) << REFCOUNT_SHIFT; - ret = bdrv_pwrite(s->hd, + ret = bdrv_pwrite_sync(s->hd, refcount_block_offset + (first_index << REFCOUNT_SHIFT), &s->refcount_block_cache[first_index], size); if (ret < 0) { -- 1.7.0.3