Skip to contentSkip to conversation

vmdk: Move descriptor buffers to heap

PATCH In review Fam Zheng 3 participants 9 messages Mar 8, 2016 – Mar 10, 2016 Block layer

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

v2 cover vmdk: Move descriptor buffers to heap

All three functions are not in hot path (all run once for the BDS lifecycle), so it's okay to convert to g_malloc0.

Fam

Fam Zheng (3):
vmdk: Switch to heap arrays for vmdk_write_cid
vmdk: Switch to heap arrays for vmdk_read_cid
vmdk: Switch to heap arrays for vmdk_parent_open

 block/vmdk.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)
Signature · 1 line
2.4.3
author
#1 lore raw

PATCH v2 1/3 vmdk: Switch to heap arrays for vmdk_write_cid

It is only called once for each opened image, so we can do it the easy way.

---
 block/vmdk.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
block/vmdk.c +14 -11
index a8db5d9..1ec2452 100644
@@ -274,36 +274,39 @@ static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
 
 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
 {
-    char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
+    char *desc, *tmp_desc;
     char *p_name, *tmp_str;
     BDRVVmdkState *s = bs->opaque;
-    int ret;
+    int ret = 0;
 
+    desc = g_malloc0(DESC_SIZE);
+    tmp_desc = g_malloc0(DESC_SIZE);
     ret = bdrv_pread(bs->file->bs, s->desc_offset, desc, DESC_SIZE);
     if (ret < 0) {
-        return ret;
+        goto out;
     }
 
     desc[DESC_SIZE - 1] = '\0';
     tmp_str = strstr(desc, "parentCID");
     if (tmp_str == NULL) {
-        return -EINVAL;
+        ret = -EINVAL;
+        goto out;
     }
 
-    pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
+    pstrcpy(tmp_desc, DESC_SIZE, tmp_str);
     p_name = strstr(desc, "CID");
     if (p_name != NULL) {
         p_name += sizeof("CID");
-        snprintf(p_name, sizeof(desc) - (p_name - desc), "%" PRIx32 "\n", cid);
-        pstrcat(desc, sizeof(desc), tmp_desc);
+        snprintf(p_name, DESC_SIZE - (p_name - desc), "%" PRIx32 "\n", cid);
+        pstrcat(desc, DESC_SIZE, tmp_desc);
     }
 
     ret = bdrv_pwrite_sync(bs->file->bs, s->desc_offset, desc, DESC_SIZE);
-    if (ret < 0) {
-        return ret;
-    }
 
-    return 0;
+out:
+    g_free(desc);
+    g_free(tmp_desc);
+    return ret;
 }
 
 static int vmdk_is_cid_valid(BlockDriverState *bs)
Signature · 1 line
2.4.3
author
#2 lore raw

PATCH v2 2/3 vmdk: Switch to heap arrays for vmdk_read_cid

In reply to Fam Zheng
---
 block/vmdk.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/vmdk.c +4 -1
index 1ec2452..c68f456 100644
@@ -242,15 +242,17 @@ static void vmdk_free_last_extent(BlockDriverState *bs)
 
 static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
 {
-    char desc[DESC_SIZE];
+    char *desc;
     uint32_t cid = 0xffffffff;
     const char *p_name, *cid_str;
     size_t cid_str_size;
     BDRVVmdkState *s = bs->opaque;
     int ret;
 
+    desc = g_malloc0(DESC_SIZE);
     ret = bdrv_pread(bs->file->bs, s->desc_offset, desc, DESC_SIZE);
     if (ret < 0) {
+        g_free(desc);
         return 0;
     }
 
@@ -269,6 +271,7 @@ static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
         sscanf(p_name, "%" SCNx32, &cid);
     }
 
+    g_free(desc);
     return cid;
 }
 
Signature · 1 line
2.4.3
author
#3 lore raw

PATCH v2 3/3 vmdk: Switch to heap arrays for vmdk_parent_open

In reply to Fam Zheng
---
 block/vmdk.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
block/vmdk.c +11 -6
index c68f456..03be7f0 100644
@@ -343,15 +343,16 @@ static int vmdk_reopen_prepare(BDRVReopenState *state,
 static int vmdk_parent_open(BlockDriverState *bs)
 {
     char *p_name;
-    char desc[DESC_SIZE + 1];
+    char *desc;
     BDRVVmdkState *s = bs->opaque;
     int ret;
 
-    desc[DESC_SIZE] = '\0';
+    desc = g_malloc0(DESC_SIZE + 1);
     ret = bdrv_pread(bs->file->bs, s->desc_offset, desc, DESC_SIZE);
     if (ret < 0) {
-        return ret;
+        goto out;
     }
+    ret = 0;
 
     p_name = strstr(desc, "parentFileNameHint");
     if (p_name != NULL) {
@@ -360,16 +361,20 @@ static int vmdk_parent_open(BlockDriverState *bs)
         p_name += sizeof("parentFileNameHint") + 1;
         end_name = strchr(p_name, '\"');
         if (end_name == NULL) {
-            return -EINVAL;
+            ret = -EINVAL;
+            goto out;
         }
         if ((end_name - p_name) > sizeof(bs->backing_file) - 1) {
-            return -EINVAL;
+            ret = -EINVAL;
+            goto out;
         }
 
         pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
     }
 
-    return 0;
+out:
+    g_free(desc);
+    return ret;
 }
 
 /* Create and append extent to the extent array. Return the added VmdkExtent
Signature · 1 line
2.4.3
author
#4 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] vmdk: Switch to heap arrays for vmdk_parent_open

Quoted 16 lines · Fam Zheng
---
 block/vmdk.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
block/vmdk.c +1 -1
index c68f456..03be7f0 100644
@@ -343,15 +343,16 @@ static int vmdk_reopen_prepare(BDRVReopenState *state,
 static int vmdk_parent_open(BlockDriverState *bs)
 {
     char *p_name;
-    char desc[DESC_SIZE + 1];
+    char *desc;
     BDRVVmdkState *s = bs->opaque;

block/vmdk.c static int vmdk_reopen_prepare(BDRVReopenState *state, · line 355

Quoted 10 lines
block/vmdk.c +3 -2
     int ret;
 
-    desc[DESC_SIZE] = '\0';
+    desc = g_malloc0(DESC_SIZE + 1);
     ret = bdrv_pread(bs->file->bs, s->desc_offset, desc, DESC_SIZE);
     if (ret < 0) {
-        return ret;
+        goto out;
     }
+    ret = 0;

Kevin, were you referring to this "ret = 0" in the cover letter? If so, it is not useless, because ret was set to DESC_SIZE by bdrv_pread. :)

Fam

#5 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] vmdk: Switch to heap arrays for vmdk_parent_open

Am 09.03.2016 um 01:43 hat Fam Zheng geschrieben:

Quoted 19 lines · Fam Zheng
Quoted 18 lines
---
 block/vmdk.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
block/vmdk.c +1 -1
index c68f456..03be7f0 100644
@@ -343,15 +343,16 @@ static int vmdk_reopen_prepare(BDRVReopenState *state,
 static int vmdk_parent_open(BlockDriverState *bs)
 {
     char *p_name;
-    char desc[DESC_SIZE + 1];
+    char *desc;
     BDRVVmdkState *s = bs->opaque;
     int ret;
 
Quoted 11 lines

block/vmdk.c static int vmdk_reopen_prepare(BDRVReopenState *state, · line 355

Quoted 8 lines
block/vmdk.c +3 -2
-    desc[DESC_SIZE] = '\0';
+    desc = g_malloc0(DESC_SIZE + 1);
     ret = bdrv_pread(bs->file->bs, s->desc_offset, desc, DESC_SIZE);
     if (ret < 0) {
-        return ret;
+        goto out;
     }
+    ret = 0;

Kevin, were you referring to this "ret = 0" in the cover letter? If so, it is not useless, because ret was set to DESC_SIZE by bdrv_pread. :)

Nope, I meant the initialisation in patch 1.

Kevin

author
#6 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] vmdk: Switch to heap arrays for vmdk_parent_open

Quoted 32 lines · Kevin Wolf

Am 09.03.2016 um 01:43 hat Fam Zheng geschrieben:

Quoted 19 lines · Fam Zheng
Quoted 18 lines
---
 block/vmdk.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
block/vmdk.c +1 -1
index c68f456..03be7f0 100644
@@ -343,15 +343,16 @@ static int vmdk_reopen_prepare(BDRVReopenState *state,
 static int vmdk_parent_open(BlockDriverState *bs)
 {
     char *p_name;
-    char desc[DESC_SIZE + 1];
+    char *desc;
     BDRVVmdkState *s = bs->opaque;
     int ret;
 
Quoted 11 lines

block/vmdk.c static int vmdk_reopen_prepare(BDRVReopenState *state, · line 355

Quoted 8 lines
block/vmdk.c +3 -2
-    desc[DESC_SIZE] = '\0';
+    desc = g_malloc0(DESC_SIZE + 1);
     ret = bdrv_pread(bs->file->bs, s->desc_offset, desc, DESC_SIZE);
     if (ret < 0) {
-        return ret;
+        goto out;
     }
+    ret = 0;

Kevin, were you referring to this "ret = 0" in the cover letter? If so, it is not useless, because ret was set to DESC_SIZE by bdrv_pread. :)

Quoted 1 line

Nope, I meant the initialisation in patch 1.

You're right.

Fam

Reviewed-by on the v2 series
#7 lore raw
In reply to Fam Zheng
Quoted 6 lines · Fam Zheng

All three functions are not in hot path (all run once for the BDS lifecycle), so it's okay to convert to g_malloc0.

Fam

Quoted 8 lines

Fam Zheng (3):
vmdk: Switch to heap arrays for vmdk_write_cid
vmdk: Switch to heap arrays for vmdk_read_cid
vmdk: Switch to heap arrays for vmdk_parent_open

 block/vmdk.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)
#8 lore raw
In reply to Fam Zheng

Am 08.03.2016 um 09:24 hat Fam Zheng geschrieben:

Quoted 2 lines

All three functions are not in hot path (all run once for the BDS lifecycle), so it's okay to convert to g_malloc0.

Yeah, it's okay, but still kind of useless to memset the whole buffer before you overwrite it anyway. There's also a useless ret = 0 somewhere. I'd prefer not to do that in the next series.

Anyway, thanks, applied to the block branch.

Kevin