qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: anthony@codemonkey.ws
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 35/41] hw/block-common: Factor out fall back to legacy -drive cyls=...
Date: Tue, 17 Jul 2012 18:00:32 +0200	[thread overview]
Message-ID: <1342540838-9027-36-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1342540838-9027-1-git-send-email-kwolf@redhat.com>

From: Markus Armbruster <armbru@redhat.com>

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 hw/block-common.c |   40 ++++++++++++++++++++++++++++++++++++++++
 hw/block-common.h |    2 ++
 hw/ide/core.c     |   24 ++++++++++++------------
 hw/ide/qdev.c     |   19 ++-----------------
 hw/scsi-disk.c    |   31 +++----------------------------
 hw/virtio-blk.c   |   31 +++----------------------------
 6 files changed, 62 insertions(+), 85 deletions(-)

diff --git a/hw/block-common.c b/hw/block-common.c
index 0a0542a..f0196d7 100644
--- a/hw/block-common.c
+++ b/hw/block-common.c
@@ -9,6 +9,7 @@
 
 #include "blockdev.h"
 #include "hw/block-common.h"
+#include "qemu-error.h"
 
 void blkconf_serial(BlockConf *conf, char **serial)
 {
@@ -22,3 +23,42 @@ void blkconf_serial(BlockConf *conf, char **serial)
         }
     }
 }
+
+int blkconf_geometry(BlockConf *conf, int *ptrans,
+                     unsigned cyls_max, unsigned heads_max, unsigned secs_max)
+{
+    DriveInfo *dinfo;
+
+    if (!conf->cyls && !conf->heads && !conf->secs) {
+        /* try to fall back to value set with legacy -drive cyls=... */
+        dinfo = drive_get_by_blockdev(conf->bs);
+        conf->cyls  = dinfo->cyls;
+        conf->heads = dinfo->heads;
+        conf->secs  = dinfo->secs;
+        if (ptrans) {
+            *ptrans = dinfo->trans;
+        }
+    }
+    if (!conf->cyls && !conf->heads && !conf->secs) {
+        hd_geometry_guess(conf->bs,
+                          &conf->cyls, &conf->heads, &conf->secs,
+                          ptrans);
+    } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) {
+        *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs);
+    }
+    if (conf->cyls || conf->heads || conf->secs) {
+        if (conf->cyls < 1 || conf->cyls > cyls_max) {
+            error_report("cyls must be between 1 and %u", cyls_max);
+            return -1;
+        }
+        if (conf->heads < 1 || conf->heads > heads_max) {
+            error_report("heads must be between 1 and %u", heads_max);
+            return -1;
+        }
+        if (conf->secs < 1 || conf->secs > secs_max) {
+            error_report("secs must be between 1 and %u", secs_max);
+            return -1;
+        }
+    }
+    return 0;
+}
diff --git a/hw/block-common.h b/hw/block-common.h
index 52bddda..bb808f7 100644
--- a/hw/block-common.h
+++ b/hw/block-common.h
@@ -60,6 +60,8 @@ static inline unsigned int get_physical_block_exp(BlockConf *conf)
 /* Configuration helpers */
 
 void blkconf_serial(BlockConf *conf, char **serial);
+int blkconf_geometry(BlockConf *conf, int *trans,
+                     unsigned cyls_max, unsigned heads_max, unsigned secs_max);
 
 /* Hard disk geometry */
 
diff --git a/hw/ide/core.c b/hw/ide/core.c
index 5378fc3..d65ef3d 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -1935,18 +1935,6 @@ int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
     s->drive_kind = kind;
 
     bdrv_get_geometry(bs, &nb_sectors);
-    if (cylinders < 1 || cylinders > 65535) {
-        error_report("cyls must be between 1 and 65535");
-        return -1;
-    }
-    if (heads < 1 || heads > 16) {
-        error_report("heads must be between 1 and 16");
-        return -1;
-    }
-    if (secs < 1 || secs > 255) {
-        error_report("secs must be between 1 and 255");
-        return -1;
-    }
     s->cylinders = cylinders;
     s->heads = heads;
     s->sectors = secs;
@@ -2094,6 +2082,18 @@ void ide_init2_with_non_qdev_drives(IDEBus *bus, DriveInfo *hd0,
             } else if (trans == BIOS_ATA_TRANSLATION_AUTO) {
                 trans = hd_bios_chs_auto_trans(cyls, heads, secs);
             }
+            if (cyls < 1 || cyls > 65535) {
+                error_report("cyls must be between 1 and 65535");
+                exit(1);
+            }
+            if (heads < 1 || heads > 16) {
+                error_report("heads must be between 1 and 16");
+                exit(1);
+            }
+            if (secs < 1 || secs > 255) {
+                error_report("secs must be between 1 and 255");
+                exit(1);
+            }
             if (ide_init_drive(&bus->ifs[i], dinfo->bdrv,
                                dinfo->media_cd ? IDE_CD : IDE_HD,
                                NULL, dinfo->serial, NULL, 0,
diff --git a/hw/ide/qdev.c b/hw/ide/qdev.c
index 7fe803c..22e58df 100644
--- a/hw/ide/qdev.c
+++ b/hw/ide/qdev.c
@@ -142,7 +142,6 @@ static int ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind)
 {
     IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
     IDEState *s = bus->ifs + dev->unit;
-    DriveInfo *dinfo;
 
     if (dev->conf.discard_granularity && dev->conf.discard_granularity != 512) {
         error_report("discard_granularity must be 512 for ide");
@@ -150,22 +149,8 @@ static int ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind)
     }
 
     blkconf_serial(&dev->conf, &dev->serial);
-
-    if (!dev->conf.cyls && !dev->conf.heads && !dev->conf.secs) {
-        /* try to fall back to value set with legacy -drive cyls=... */
-        dinfo = drive_get_by_blockdev(dev->conf.bs);
-        dev->conf.cyls  = dinfo->cyls;
-        dev->conf.heads = dinfo->heads;
-        dev->conf.secs  = dinfo->secs;
-        dev->chs_trans  = dinfo->trans;
-    }
-    if (!dev->conf.cyls && !dev->conf.heads && !dev->conf.secs) {
-        hd_geometry_guess(dev->conf.bs,
-                          &dev->conf.cyls, &dev->conf.heads, &dev->conf.secs,
-                          &dev->chs_trans);
-    } else if (dev->chs_trans == BIOS_ATA_TRANSLATION_AUTO) {
-        dev->chs_trans = hd_bios_chs_auto_trans(dev->conf.cyls,
-                                        dev->conf.heads, dev->conf.secs);
+    if (blkconf_geometry(&dev->conf, &dev->chs_trans, 65536, 16, 255) < 0) {
+        return -1;
     }
 
     if (ide_init_drive(s, dev->conf.bs, kind,
diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
index 39a07d7..525816c 100644
--- a/hw/scsi-disk.c
+++ b/hw/scsi-disk.c
@@ -1737,7 +1737,6 @@ static void scsi_disk_unit_attention_reported(SCSIDevice *dev)
 static int scsi_initfn(SCSIDevice *dev)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
-    DriveInfo *dinfo;
 
     if (!s->qdev.conf.bs) {
         error_report("drive property not set");
@@ -1750,34 +1749,10 @@ static int scsi_initfn(SCSIDevice *dev)
         return -1;
     }
 
-    if (!dev->conf.cyls && !dev->conf.heads && !dev->conf.secs) {
-        /* try to fall back to value set with legacy -drive cyls=... */
-        dinfo = drive_get_by_blockdev(s->qdev.conf.bs);
-        dev->conf.cyls = dinfo->cyls;
-        dev->conf.heads = dinfo->heads;
-        dev->conf.secs = dinfo->secs;
-    }
-    if (!dev->conf.cyls && !dev->conf.heads && !dev->conf.secs) {
-        hd_geometry_guess(s->qdev.conf.bs,
-                          &dev->conf.cyls, &dev->conf.heads, &dev->conf.secs,
-                          NULL);
-    }
-    if (dev->conf.cyls || dev->conf.heads || dev->conf.secs) {
-        if (dev->conf.cyls < 1 || dev->conf.cyls > 65535) {
-            error_report("cyls must be between 1 and 65535");
-            return -1;
-        }
-        if (dev->conf.heads < 1 || dev->conf.heads > 255) {
-            error_report("heads must be between 1 and 255");
-            return -1;
-        }
-        if (dev->conf.secs < 1 || dev->conf.secs > 255) {
-            error_report("secs must be between 1 and 255");
-            return -1;
-        }
-    }
-
     blkconf_serial(&s->qdev.conf, &s->serial);
+    if (blkconf_geometry(&dev->conf, NULL, 65535, 255, 255) < 0) {
+        return -1;
+    }
 
     if (!s->version) {
         s->version = g_strdup(qemu_get_version());
diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c
index ba087bc..f21757e 100644
--- a/hw/virtio-blk.c
+++ b/hw/virtio-blk.c
@@ -589,7 +589,6 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
 {
     VirtIOBlock *s;
     static int virtio_blk_id;
-    DriveInfo *dinfo;
 
     if (!blk->conf.bs) {
         error_report("drive property not set");
@@ -601,6 +600,9 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
     }
 
     blkconf_serial(&blk->conf, &blk->serial);
+    if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
+        return NULL;
+    }
 
     s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
                                           sizeof(struct virtio_blk_config),
@@ -615,33 +617,6 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
     s->rq = NULL;
     s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
 
-    if (!blk->conf.cyls && !blk->conf.heads && !blk->conf.secs) {
-        /* try to fall back to value set with legacy -drive cyls=... */
-        dinfo = drive_get_by_blockdev(blk->conf.bs);
-        blk->conf.cyls = dinfo->cyls;
-        blk->conf.heads = dinfo->heads;
-        blk->conf.secs = dinfo->secs;
-    }
-    if (!blk->conf.cyls && !blk->conf.heads && !blk->conf.secs) {
-        hd_geometry_guess(s->bs,
-                          &blk->conf.cyls, &blk->conf.heads, &blk->conf.secs,
-                          NULL);
-    }
-    if (blk->conf.cyls || blk->conf.heads || blk->conf.secs) {
-        if (blk->conf.cyls < 1 || blk->conf.cyls > 65535) {
-            error_report("cyls must be between 1 and 65535");
-            return NULL;
-        }
-        if (blk->conf.heads < 1 || blk->conf.heads > 255) {
-            error_report("heads must be between 1 and 255");
-            return NULL;
-        }
-        if (blk->conf.secs < 1 || blk->conf.secs > 255) {
-            error_report("secs must be between 1 and 255");
-            return NULL;
-        }
-    }
-
     s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
 
     qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
-- 
1.7.6.5

  parent reply	other threads:[~2012-07-17 16:01 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-17 15:59 [Qemu-devel] [PULL 00/41] Block patches Kevin Wolf
2012-07-17 15:59 ` [Qemu-devel] [PATCH 01/41] sheepdog: always use coroutine-based network functions Kevin Wolf
2012-07-17 15:59 ` [Qemu-devel] [PATCH 02/41] sheepdog: do not blindly memset all read buffers Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 03/41] fdc: Move floppy geometry guessing back from block.c Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 04/41] vvfat: Fix partition table Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 05/41] vvfat: Do not clobber the user's geometry Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 06/41] qtest: Add hard disk geometry test Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 07/41] hd-geometry: Move disk geometry guessing back from block.c Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 08/41] hd-geometry: Add tracepoints Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 09/41] hd-geometry: Unnest conditional in hd_geometry_guess() Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 10/41] hd-geometry: Factor out guess_chs_for_size() Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 11/41] hd-geometry: Clean up gratuitous goto in hd_geometry_guess() Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 12/41] hd-geometry: Clean up confusing use of prior translation hint Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 13/41] hd-geometry: Cut out block layer translation middleman Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 14/41] ide pc: Cut out the block layer geometry middleman Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 15/41] blockdev: Save geometry in DriveInfo Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 16/41] qdev: Introduce block geometry properties Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 17/41] hd-geometry: Switch to uint32_t to match BlockConf Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 18/41] scsi-hd: qdev properties for disk geometry Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 19/41] virtio-blk: " Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 20/41] ide: " Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 21/41] qtest: Cover " Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 22/41] qdev: Collect private helpers in one place Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 23/41] qdev: New property type chs-translation Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 24/41] ide: qdev property for BIOS CHS translation Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 25/41] qtest: Cover " Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 26/41] block: Geometry and translation hints are now useless, purge them Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 27/41] ide pc: Put hard disk info into CMOS only for hard disks Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 28/41] qtest: Test we don't put hard disk info into CMOS for a CD-ROM Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 29/41] hd-geometry: Compute BIOS CHS translation in one place Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 30/41] blockdev: Drop redundant CHS validation for if=ide Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 31/41] Relax IDE CHS limits from 16383, 16, 63 to 65535, 16, 255 Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 32/41] hw/block-common: Move BlockConf & friends from block.h Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 33/41] hw/block-common: Factor out fall back to legacy -drive serial= Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 34/41] blockdev: Don't limit DriveInfo serial to 20 characters Kevin Wolf
2012-07-17 16:00 ` Kevin Wolf [this message]
2012-07-17 16:00 ` [Qemu-devel] [PATCH 36/41] qemu-io: Fix memory leaks Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 37/41] coroutine-ucontext: Help valgrind understand coroutines Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 38/41] qemu-iotests: Valgrind support Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 39/41] fdc: fix relative seek Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 40/41] fdc-test: introduce test_relative_seek Kevin Wolf
2012-07-17 16:00 ` [Qemu-devel] [PATCH 41/41] fdc-test: Clean up a bit Kevin Wolf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1342540838-9027-36-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=anthony@codemonkey.ws \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).