From 3cd3cff6738ade76defa241810975fbc10d576b2 Mon Sep 17 00:00:00 2001 Message-Id: <3cd3cff6738ade76defa241810975fbc10d576b2.1369658547.git.minovotn@redhat.com> In-Reply-To: <07146f8b79923c529fd93fa528e6fcbd6f571a02.1369658547.git.minovotn@redhat.com> References: <07146f8b79923c529fd93fa528e6fcbd6f571a02.1369658547.git.minovotn@redhat.com> From: Fam Zheng Date: Mon, 20 May 2013 03:36:57 +0200 Subject: [PATCH 42/47] vmdk: named return code. RH-Author: Fam Zheng Message-id: <1369021022-22728-43-git-send-email-famz@redhat.com> Patchwork-id: 51478 O-Subject: [PATCH RHEL-6.5 qemu-kvm v3 42/47] vmdk: named return code. Bugzilla: 960685 RH-Acked-by: Stefan Hajnoczi RH-Acked-by: Jeffrey Cody RH-Acked-by: Kevin Wolf Internal routines in vmdk.c previously return -1 on error and 0 on success. More return values are useful for future changes such as zeroed-grain GTE. Change all the magic `return 0` and `return -1` to macro names: * VMDK_OK 0 * VMDK_ERROR (-1) * VMDK_UNALLOC (-2) * VMDK_ZEROED (-3) Signed-off-by: Fam Zheng Signed-off-by: Stefan Hajnoczi (cherry picked from commit 65f7472577f9409d285b0216fae76c7c3f03fc41) Signed-off-by: Fam Zheng --- block/vmdk.c | 60 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 26 deletions(-) Signed-off-by: Michal Novotny --- block/vmdk.c | 60 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 55bc44b..c0d4c9f 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -37,6 +37,14 @@ #define VMDK4_FLAG_MARKER (1 << 17) #define VMDK4_GD_AT_END 0xffffffffffffffffULL + +/* VMDK internal error codes */ +#define VMDK_OK 0 +#define VMDK_ERROR (-1) +/* Cluster not allocated */ +#define VMDK_UNALLOC (-2) +#define VMDK_ZEROED (-3) + typedef struct { uint32_t version; uint32_t flags; @@ -576,22 +584,22 @@ static int vmdk_parse_description(const char *desc, const char *opt_name, opt_pos = strstr(desc, opt_name); if (!opt_pos) { - return -1; + return VMDK_ERROR; } /* Skip "=\"" following opt_name */ opt_pos += strlen(opt_name) + 2; if (opt_pos >= end) { - return -1; + return VMDK_ERROR; } opt_end = opt_pos; while (opt_end < end && *opt_end != '"') { opt_end++; } if (opt_end == end || buf_size < opt_end - opt_pos + 1) { - return -1; + return VMDK_ERROR; } pstrcpy(buf, opt_end - opt_pos + 1, opt_pos); - return 0; + return VMDK_OK; } /* Open an extent file and append to bs array */ @@ -763,7 +771,7 @@ static int get_whole_cluster(BlockDriverState *bs, int ret; if (!vmdk_is_cid_valid(bs)) { - return -1; + return VMDK_ERROR; } /* floor offset to cluster */ @@ -771,17 +779,17 @@ static int get_whole_cluster(BlockDriverState *bs, ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain, extent->cluster_sectors); if (ret < 0) { - return -1; + return VMDK_ERROR; } /* Write grain only into the active image */ ret = bdrv_write(extent->file, cluster_offset, whole_grain, extent->cluster_sectors); if (ret < 0) { - return -1; + return VMDK_ERROR; } } - return 0; + return VMDK_OK; } static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) @@ -794,7 +802,7 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) &(m_data->offset), sizeof(m_data->offset) ) < 0) { - return -1; + return VMDK_ERROR; } /* update backup L2 table */ if (extent->l1_backup_table_offset != 0) { @@ -805,11 +813,11 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) + (m_data->l2_index * sizeof(m_data->offset)), &(m_data->offset), sizeof(m_data->offset) ) < 0) { - return -1; + return VMDK_ERROR; } } - return 0; + return VMDK_OK; } static int get_cluster_offset(BlockDriverState *bs, @@ -828,17 +836,17 @@ static int get_cluster_offset(BlockDriverState *bs, } if (extent->flat) { *cluster_offset = extent->flat_start_offset; - return 0; + return VMDK_OK; } offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE; l1_index = (offset >> 9) / extent->l1_entry_sectors; if (l1_index >= extent->l1_size) { - return -1; + return VMDK_ERROR; } l2_offset = extent->l1_table[l1_index]; if (!l2_offset) { - return -1; + return VMDK_UNALLOC; } for (i = 0; i < L2_CACHE_SIZE; i++) { if (l2_offset == extent->l2_cache_offsets[i]) { @@ -868,7 +876,7 @@ static int get_cluster_offset(BlockDriverState *bs, l2_table, extent->l2_size * sizeof(uint32_t) ) != extent->l2_size * sizeof(uint32_t)) { - return -1; + return VMDK_ERROR; } extent->l2_cache_offsets[min_index] = l2_offset; @@ -879,7 +887,7 @@ static int get_cluster_offset(BlockDriverState *bs, if (!*cluster_offset) { if (!allocate) { - return -1; + return VMDK_UNALLOC; } /* Avoid the L2 tables update for the images that have snapshots. */ @@ -902,7 +910,7 @@ static int get_cluster_offset(BlockDriverState *bs, */ if (get_whole_cluster( bs, extent, *cluster_offset, offset, allocate) == -1) { - return -1; + return VMDK_ERROR; } if (m_data) { @@ -914,7 +922,7 @@ static int get_cluster_offset(BlockDriverState *bs, } } *cluster_offset <<= 9; - return 0; + return VMDK_OK; } static VmdkExtent *find_extent(BDRVVmdkState *s, @@ -1164,7 +1172,7 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num, sector_num << 9, !extent->compressed, &cluster_offset); if (extent->compressed) { - if (ret == 0) { + if (ret == VMDK_OK) { /* Refuse write to allocated cluster for streamOptimized */ fprintf(stderr, "VMDK: can't write to allocated cluster" @@ -1349,7 +1357,7 @@ static int filename_decompose(const char *filename, char *path, char *prefix, if (filename == NULL || !strlen(filename)) { fprintf(stderr, "Vmdk: no filename provided.\n"); - return -1; + return VMDK_ERROR; } p = strrchr(filename, '/'); if (p == NULL) { @@ -1361,7 +1369,7 @@ static int filename_decompose(const char *filename, char *path, char *prefix, if (p != NULL) { p++; if (p - filename >= buf_len) { - return -1; + return VMDK_ERROR; } pstrcpy(path, p - filename + 1, filename); } else { @@ -1374,12 +1382,12 @@ static int filename_decompose(const char *filename, char *path, char *prefix, postfix[0] = '\0'; } else { if (q - p >= buf_len) { - return -1; + return VMDK_ERROR; } pstrcpy(prefix, q - p + 1, p); pstrcpy(postfix, buf_len, q); } - return 0; + return VMDK_OK; } static int relative_path(char *dest, int dest_size, @@ -1395,11 +1403,11 @@ static int relative_path(char *dest, int dest_size, #endif if (!(dest && base && target)) { - return -1; + return VMDK_ERROR; } if (path_is_absolute(target)) { pstrcpy(dest, dest_size, target); - return 0; + return VMDK_OK; } while (base[i] == target[i]) { i++; @@ -1418,7 +1426,7 @@ static int relative_path(char *dest, int dest_size, pstrcat(dest, dest_size, sep); } pstrcat(dest, dest_size, q); - return 0; + return VMDK_OK; } static int vmdk_create(const char *filename, QEMUOptionParameter *options) -- 1.7.11.7