Skip to contentSkip to conversation

xen: Clean up BlockDriverState use

PATCH In review Markus Armbruster 3 participants 12 messages Jun 5, 2012 – Jun 13, 2012

lore
Share & tools
lore
#2 lore raw

PATCH 2/2 xen: Don't peek behind the BlockDriverState abstraction

In reply to Markus Armbruster

First offender is xen_config_dev_blk()'s use of disk->bdrv->filename. Get the filename from disk->opts instead. Same result, except for
snapshots: there, we now get the filename specified by the user instead of the name of the temporary image created by bdrv_open(). Should be an improvement.

Second offender is blk_init()'s use of blkdev->bs->drv->format_name. Simply use the appropriate interface to get the format name.

---
 hw/xen_devconfig.c |    6 +++---
 hw/xen_disk.c      |    5 +++--
 2 files changed, 6 insertions(+), 5 deletions(-)
hw/xen_devconfig.c +3 -3
index 7b7b0a2..0928613 100644
@@ -1,6 +1,5 @@
 #include "xen_backend.h"
 #include "blockdev.h"
-#include "block_int.h" /* XXX */
 
 /* ------------------------------------------------------------- */
 
@@ -99,10 +98,11 @@ int xen_config_dev_blk(DriveInfo *disk)
     int cdrom = disk->media_cd;
     const char *devtype = cdrom ? "cdrom" : "disk";
     const char *mode    = cdrom ? "r"     : "w";
+    const char *filename = qemu_opt_get(disk->opts, "file");
 
     snprintf(device_name, sizeof(device_name), "xvd%c", 'a' + disk->unit);
     xen_be_printf(NULL, 1, "config disk %d [%s]: %s\n",
-                  disk->unit, device_name, disk->bdrv->filename);
+                  disk->unit, device_name, filename);
     xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe));
 
     /* frontend */
@@ -112,7 +112,7 @@ int xen_config_dev_blk(DriveInfo *disk)
     /* backend */
     xenstore_write_str(be, "dev",             device_name);
     xenstore_write_str(be, "type",            "file");
-    xenstore_write_str(be, "params",          disk->bdrv->filename);
+    xenstore_write_str(be, "params",          filename);
     xenstore_write_str(be, "mode",            mode);
 
     /* common stuff */
hw/xen_disk.c +3 -2
index 07594bc..c73b71e 100644
@@ -40,7 +40,6 @@
 #include <xen/io/xenbus.h>
 
 #include "hw.h"
-#include "block_int.h"
 #include "qemu-char.h"
 #include "xen_blkif.h"
 #include "xen_backend.h"
@@ -554,6 +553,7 @@ static int blk_init(struct XenDevice *xendev)
 {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, info = 0;
+    char fmt_name[128];
 
     /* read xenstore entries */
     if (blkdev->params == NULL) {
@@ -634,9 +634,10 @@ static int blk_init(struct XenDevice *xendev)
     blkdev->file_blk  = BLOCK_SIZE;
     blkdev->file_size = bdrv_getlength(blkdev->bs);
     if (blkdev->file_size < 0) {
+        bdrv_get_format(blkdev->bs, fmt_name, sizeof(fmt_name));
         xen_be_printf(&blkdev->xendev, 1, "bdrv_getlength: %d (%s) | drv %s\n",
                       (int)blkdev->file_size, strerror(-blkdev->file_size),
-                      blkdev->bs->drv ? blkdev->bs->drv->format_name : "-");
+                      fmt_name[0] ? fmt_name : "-");
         blkdev->file_size = 0;
     }
 
Signature · 1 line
1.7.6.5
#1 lore raw

PATCH 1/2 xen: Don't change -drive if=xen device name during machine init

In reply to Markus Armbruster

A "top" BlockDriverState has a non-empty device_name. If the user doesn't specify one with -drive parameter id, the system supplies a default name.

xen_config_dev_blk() changes this name, during machine initialization. Naughty. Don't do that.

---
 hw/xen_devconfig.c |    9 ++++-----
 1 files changed, 4 insertions(+), 5 deletions(-)
hw/xen_devconfig.c +4 -5
index 41accbb..7b7b0a2 100644
@@ -94,16 +94,15 @@ static int xen_config_dev_all(char *fe, char *be)
 
 int xen_config_dev_blk(DriveInfo *disk)
 {
-    char fe[256], be[256];
+    char fe[256], be[256], device_name[32];
     int vdev = 202 * 256 + 16 * disk->unit;
     int cdrom = disk->media_cd;
     const char *devtype = cdrom ? "cdrom" : "disk";
     const char *mode    = cdrom ? "r"     : "w";
 
-    snprintf(disk->bdrv->device_name, sizeof(disk->bdrv->device_name),
-	     "xvd%c", 'a' + disk->unit);
+    snprintf(device_name, sizeof(device_name), "xvd%c", 'a' + disk->unit);
     xen_be_printf(NULL, 1, "config disk %d [%s]: %s\n",
-                  disk->unit, disk->bdrv->device_name, disk->bdrv->filename);
+                  disk->unit, device_name, disk->bdrv->filename);
     xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe));
 
     /* frontend */
@@ -111,7 +110,7 @@ int xen_config_dev_blk(DriveInfo *disk)
     xenstore_write_str(fe, "device-type",     devtype);
 
     /* backend */
-    xenstore_write_str(be, "dev",             disk->bdrv->device_name);
+    xenstore_write_str(be, "dev",             device_name);
     xenstore_write_str(be, "type",            "file");
     xenstore_write_str(be, "params",          disk->bdrv->filename);
     xenstore_write_str(be, "mode",            mode);
Signature · 1 line
1.7.6.5
#0 lore raw

cover xen: Clean up BlockDriverState use

Compile tested only. Stefano, please give it a whirl.

Markus Armbruster (2):
xen: Don't change -drive if=xen device name during machine init
xen: Don't peek behind the BlockDriverState abstraction

 hw/xen_devconfig.c |   13 ++++++-------
 hw/xen_disk.c      |    5 +++--
 2 files changed, 9 insertions(+), 9 deletions(-)
Signature · 1 line
1.7.6.5
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] xen: Don't peek behind the BlockDriverState abstraction

In reply to Markus Armbruster
Quoted 69 lines · Markus Armbruster

First offender is xen_config_dev_blk()'s use of disk->bdrv->filename. Get the filename from disk->opts instead. Same result, except for
snapshots: there, we now get the filename specified by the user instead of the name of the temporary image created by bdrv_open(). Should be an improvement.

Second offender is blk_init()'s use of blkdev->bs->drv->format_name. Simply use the appropriate interface to get the format name.

---
 hw/xen_devconfig.c |    6 +++---
 hw/xen_disk.c      |    5 +++--
 2 files changed, 6 insertions(+), 5 deletions(-)
hw/xen_devconfig.c +3 -3
index 7b7b0a2..0928613 100644
@@ -1,6 +1,5 @@
 #include "xen_backend.h"
 #include "blockdev.h"
-#include "block_int.h" /* XXX */
 
 /* ------------------------------------------------------------- */
 
@@ -99,10 +98,11 @@ int xen_config_dev_blk(DriveInfo *disk)
     int cdrom = disk->media_cd;
     const char *devtype = cdrom ? "cdrom" : "disk";
     const char *mode    = cdrom ? "r"     : "w";
+    const char *filename = qemu_opt_get(disk->opts, "file");
 
     snprintf(device_name, sizeof(device_name), "xvd%c", 'a' + disk->unit);
     xen_be_printf(NULL, 1, "config disk %d [%s]: %s\n",
-                  disk->unit, device_name, disk->bdrv->filename);
+                  disk->unit, device_name, filename);
     xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe));
 
     /* frontend */
@@ -112,7 +112,7 @@ int xen_config_dev_blk(DriveInfo *disk)
     /* backend */
     xenstore_write_str(be, "dev",             device_name);
     xenstore_write_str(be, "type",            "file");
-    xenstore_write_str(be, "params",          disk->bdrv->filename);
+    xenstore_write_str(be, "params",          filename);
     xenstore_write_str(be, "mode",            mode);
 
     /* common stuff */
hw/xen_disk.c +1 -1
index 07594bc..c73b71e 100644
@@ -40,7 +40,6 @@
 #include <xen/io/xenbus.h>
 
 #include "hw.h"
-#include "block_int.h"
 #include "qemu-char.h"
 #include "xen_blkif.h"
 #include "xen_backend.h"
@@ -554,6 +553,7 @@ static int blk_init(struct XenDevice *xendev)
 {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, info = 0;
+    char fmt_name[128];
 
     /* read xenstore entries */
     if (blkdev->params == NULL) {

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 642

Quoted 10 lines
hw/xen_disk.c +2 -1
@@ -634,9 +634,10 @@ static int blk_init(struct XenDevice *xendev)
     blkdev->file_blk  = BLOCK_SIZE;
     blkdev->file_size = bdrv_getlength(blkdev->bs);
     if (blkdev->file_size < 0) {
+        bdrv_get_format(blkdev->bs, fmt_name, sizeof(fmt_name));
         xen_be_printf(&blkdev->xendev, 1, "bdrv_getlength: %d (%s) | drv %s\n",
                       (int)blkdev->file_size, strerror(-blkdev->file_size),
-                      blkdev->bs->drv ? blkdev->bs->drv->format_name : "-");
+                      fmt_name[0] ? fmt_name : "-");
         blkdev->file_size = 0;
     }

You might as well move fmt_name here because it is only used if blkdev->file_size < 0.

Apart from this minor nitpick, both patches are OK.

#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] xen: Don't peek behind the BlockDriverState abstraction

Quoted 67 lines · Stefano Stabellini
Quoted 66 lines · Markus Armbruster

First offender is xen_config_dev_blk()'s use of disk->bdrv->filename. Get the filename from disk->opts instead. Same result, except for
snapshots: there, we now get the filename specified by the user instead of the name of the temporary image created by bdrv_open(). Should be an improvement.

Second offender is blk_init()'s use of blkdev->bs->drv->format_name. Simply use the appropriate interface to get the format name.

---
 hw/xen_devconfig.c |    6 +++---
 hw/xen_disk.c      |    5 +++--
 2 files changed, 6 insertions(+), 5 deletions(-)
hw/xen_devconfig.c +3 -3
index 7b7b0a2..0928613 100644
@@ -1,6 +1,5 @@
 #include "xen_backend.h"
 #include "blockdev.h"
-#include "block_int.h" /* XXX */
 
 /* ------------------------------------------------------------- */
 
@@ -99,10 +98,11 @@ int xen_config_dev_blk(DriveInfo *disk)
     int cdrom = disk->media_cd;
     const char *devtype = cdrom ? "cdrom" : "disk";
     const char *mode    = cdrom ? "r"     : "w";
+    const char *filename = qemu_opt_get(disk->opts, "file");
 
     snprintf(device_name, sizeof(device_name), "xvd%c", 'a' + disk->unit);
     xen_be_printf(NULL, 1, "config disk %d [%s]: %s\n",
-                  disk->unit, device_name, disk->bdrv->filename);
+                  disk->unit, device_name, filename);
     xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe));
 
     /* frontend */
@@ -112,7 +112,7 @@ int xen_config_dev_blk(DriveInfo *disk)
     /* backend */
     xenstore_write_str(be, "dev",             device_name);
     xenstore_write_str(be, "type",            "file");
-    xenstore_write_str(be, "params",          disk->bdrv->filename);
+    xenstore_write_str(be, "params",          filename);
     xenstore_write_str(be, "mode",            mode);
 
     /* common stuff */
hw/xen_disk.c +1 -1
index 07594bc..c73b71e 100644
@@ -40,7 +40,6 @@
 #include <xen/io/xenbus.h>
 
 #include "hw.h"
-#include "block_int.h"
 #include "qemu-char.h"
 #include "xen_blkif.h"
 #include "xen_backend.h"
@@ -554,6 +553,7 @@ static int blk_init(struct XenDevice *xendev)
 {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, info = 0;
+    char fmt_name[128];
 
Quoted 16 lines

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 642

Quoted 13 lines
hw/xen_disk.c +2 -1
     /* read xenstore entries */
     if (blkdev->params == NULL) {
@@ -634,9 +634,10 @@ static int blk_init(struct XenDevice *xendev)
     blkdev->file_blk  = BLOCK_SIZE;
     blkdev->file_size = bdrv_getlength(blkdev->bs);
     if (blkdev->file_size < 0) {
+        bdrv_get_format(blkdev->bs, fmt_name, sizeof(fmt_name));
         xen_be_printf(&blkdev->xendev, 1, "bdrv_getlength: %d (%s) | drv %s\n",
                       (int)blkdev->file_size, strerror(-blkdev->file_size),
-                      blkdev->bs->drv ? blkdev->bs->drv->format_name : "-");
+                      fmt_name[0] ? fmt_name : "-");
         blkdev->file_size = 0;
     }

You might as well move fmt_name here because it is only used if blkdev->file_size < 0.

Matter of taste, and you're the maintainer. Want me to respin?

Quoted 1 line

Apart from this minor nitpick, both patches are OK.

Thanks!

#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] xen: Don't peek behind the BlockDriverState abstraction

In reply to Markus Armbruster

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 556

Quoted 5 lines · Markus Armbruster
hw/xen_disk.c +1 -0
@@ -554,6 +553,7 @@ static int blk_init(struct XenDevice *xendev)
  {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, info = 0;
+    char fmt_name[128];

Fixed length array with a hardcoded magic number size ? If the block layer guarantees that format names are going to be less than 128 bytes it ought to provide a suitable #define for people to set array sizes to...

-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] xen: Don't peek behind the BlockDriverState abstraction

In reply to Markus Armbruster
Quoted 85 lines · Markus Armbruster
Quoted 67 lines · Stefano Stabellini
Quoted 66 lines · Markus Armbruster

First offender is xen_config_dev_blk()'s use of disk->bdrv->filename. Get the filename from disk->opts instead. Same result, except for
snapshots: there, we now get the filename specified by the user instead of the name of the temporary image created by bdrv_open(). Should be an improvement.

Second offender is blk_init()'s use of blkdev->bs->drv->format_name. Simply use the appropriate interface to get the format name.

---
 hw/xen_devconfig.c |    6 +++---
 hw/xen_disk.c      |    5 +++--
 2 files changed, 6 insertions(+), 5 deletions(-)
hw/xen_devconfig.c +3 -3
index 7b7b0a2..0928613 100644
@@ -1,6 +1,5 @@
 #include "xen_backend.h"
 #include "blockdev.h"
-#include "block_int.h" /* XXX */
 
 /* ------------------------------------------------------------- */
 
@@ -99,10 +98,11 @@ int xen_config_dev_blk(DriveInfo *disk)
     int cdrom = disk->media_cd;
     const char *devtype = cdrom ? "cdrom" : "disk";
     const char *mode    = cdrom ? "r"     : "w";
+    const char *filename = qemu_opt_get(disk->opts, "file");
 
     snprintf(device_name, sizeof(device_name), "xvd%c", 'a' + disk->unit);
     xen_be_printf(NULL, 1, "config disk %d [%s]: %s\n",
-                  disk->unit, device_name, disk->bdrv->filename);
+                  disk->unit, device_name, filename);
     xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe));
 
     /* frontend */
@@ -112,7 +112,7 @@ int xen_config_dev_blk(DriveInfo *disk)
     /* backend */
     xenstore_write_str(be, "dev",             device_name);
     xenstore_write_str(be, "type",            "file");
-    xenstore_write_str(be, "params",          disk->bdrv->filename);
+    xenstore_write_str(be, "params",          filename);
     xenstore_write_str(be, "mode",            mode);
 
     /* common stuff */
hw/xen_disk.c +1 -1
index 07594bc..c73b71e 100644
@@ -40,7 +40,6 @@
 #include <xen/io/xenbus.h>
 
 #include "hw.h"
-#include "block_int.h"
 #include "qemu-char.h"
 #include "xen_blkif.h"
 #include "xen_backend.h"
@@ -554,6 +553,7 @@ static int blk_init(struct XenDevice *xendev)
 {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, info = 0;
+    char fmt_name[128];
 
Quoted 16 lines

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 642

Quoted 13 lines
hw/xen_disk.c +2 -1
     /* read xenstore entries */
     if (blkdev->params == NULL) {
@@ -634,9 +634,10 @@ static int blk_init(struct XenDevice *xendev)
     blkdev->file_blk  = BLOCK_SIZE;
     blkdev->file_size = bdrv_getlength(blkdev->bs);
     if (blkdev->file_size < 0) {
+        bdrv_get_format(blkdev->bs, fmt_name, sizeof(fmt_name));
         xen_be_printf(&blkdev->xendev, 1, "bdrv_getlength: %d (%s) | drv %s\n",
                       (int)blkdev->file_size, strerror(-blkdev->file_size),
-                      blkdev->bs->drv ? blkdev->bs->drv->format_name : "-");
+                      fmt_name[0] ? fmt_name : "-");
         blkdev->file_size = 0;
     }

You might as well move fmt_name here because it is only used if blkdev->file_size < 0.

Quoted 1 line

Matter of taste, and you're the maintainer. Want me to respin?

Yes, please :-)

#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] xen: Don't peek behind the BlockDriverState abstraction

In reply to Peter Maydell
Quoted 11 lines · Peter Maydell

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 556

Quoted 5 lines · Markus Armbruster
hw/xen_disk.c +1 -0
@@ -554,6 +553,7 @@ static int blk_init(struct XenDevice *xendev)
  {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, info = 0;
+    char fmt_name[128];

Fixed length array with a hardcoded magic number size ? If the block layer guarantees that format names are going to be less than 128 bytes it ought to provide a suitable #define for people to set array sizes to...

Maybe it should, but it doesn't. Does it really matter in this particular case? If somebody insists on giving his driver a name longer than 127 characters, we'll silently log it truncated, that's all.

#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] xen: Don't peek behind the BlockDriverState abstraction

Quoted 14 lines · Markus Armbruster
Quoted 11 lines · Peter Maydell

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 556

Quoted 5 lines · Markus Armbruster
hw/xen_disk.c +1 -0
@@ -554,6 +553,7 @@ static int blk_init(struct XenDevice *xendev)
  {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, info = 0;
+    char fmt_name[128];

Fixed length array with a hardcoded magic number size ? If the block layer guarantees that format names are going to be less than 128 bytes it ought to provide a suitable #define for people to set array sizes to...

Quoted 3 lines

Maybe it should, but it doesn't.  Does it really matter in this particular case?  If somebody insists on giving his driver a name longer than 127 characters, we'll silently log it truncated, that's all.

I think it matters in the general case, yours is just the first usage of this API which has caught my attention. We should fix the API before adding more uses of it (at the moment it seems to be only used in two places).

Alternatively, we could have the function return a const char* rather than taking a buffer to be filled in.

-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] xen: Don't peek behind the BlockDriverState abstraction

Quoted 15 lines · Peter Maydell
Quoted 14 lines · Markus Armbruster
Quoted 11 lines · Peter Maydell

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 556

Quoted 5 lines · Markus Armbruster
hw/xen_disk.c +1 -0
@@ -554,6 +553,7 @@ static int blk_init(struct XenDevice *xendev)
  {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, info = 0;
+    char fmt_name[128];

Fixed length array with a hardcoded magic number size ? If the block layer guarantees that format names are going to be less than 128 bytes it ought to provide a suitable #define for people to set array sizes to...

Quoted 8 lines
Quoted 3 lines

Maybe it should, but it doesn't.  Does it really matter in this particular case?  If somebody insists on giving his driver a name longer than 127 characters, we'll silently log it truncated, that's all.

I think it matters in the general case, yours is just the first usage of this API which has caught my attention. We should fix the API before adding more uses of it (at the moment it seems to be only used in two places).

What kind of fix do you have in mind?

Quoted 2 lines

Alternatively, we could have the function return a const char* rather than taking a buffer to be filled in.

Trades the theoretical string truncation problem for a theoretical dangling pointer problem.

#10 lore raw

Re: [Qemu-devel] [PATCH 2/2] xen: Don't peek behind the BlockDriverState abstraction

Quoted 7 lines · Markus Armbruster
Quoted 4 lines · Peter Maydell

I think it matters in the general case, yours is just the first usage of this API which has caught my attention. We should fix the API before adding more uses of it (at the moment it seems to be only used in two places).

What kind of fix do you have in mind?

Option 1: the function should guarantee that it won't ever use more than X bytes of buffer, and provide a #define that corresponds to that maximum length.

Option 2: this: vv

Quoted 5 lines
Quoted 2 lines

Alternatively, we could have the function return a const char* rather than taking a buffer to be filled in.

Trades the theoretical string truncation problem for a theoretical dangling pointer problem.

Yes, you'd need to come up with some reasonable lifecycle management if you took this option.

-- PMM

#11 lore raw

Re: [Qemu-devel] [PATCH 2/2] xen: Don't peek behind the BlockDriverState abstraction

Quoted 15 lines · Peter Maydell
Quoted 7 lines · Markus Armbruster
Quoted 4 lines · Peter Maydell

I think it matters in the general case, yours is just the first usage of this API which has caught my attention. We should fix the API before adding more uses of it (at the moment it seems to be only used in two places).

What kind of fix do you have in mind?

Option 1: the function should guarantee that it won't ever use more than X bytes of buffer, and provide a #define that corresponds to that maximum length.

Option 2: this: vv

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Alternatively, we could have the function return a const char* rather than taking a buffer to be filled in.

Trades the theoretical string truncation problem for a theoretical dangling pointer problem.

Yes, you'd need to come up with some reasonable lifecycle management if you took this option.

Actually, the lifecycle is trivial, because it's a *driver* name, and drivers never go away. Taking option 2.