<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From 17ed5e14d10cf3a6feb3d43cf6152d6db138800c Mon Sep 17 00:00:00 2001
From: Kevin Wolf &lt;kwolf@redhat.com&gt;
Date: Tue, 14 Feb 2012 11:13:33 +0100
Subject: [PATCH 08/99] bochs: use qemu block API

RH-Author: Kevin Wolf &lt;kwolf@redhat.com&gt;
Message-id: &lt;1329218101-24213-9-git-send-email-kwolf@redhat.com&gt;
Patchwork-id: 37200
O-Subject: [RHEL-6.3 qemu-kvm PATCH v2 08/96] bochs: use qemu block API
Bugzilla: 783950
RH-Acked-by: Paolo Bonzini &lt;pbonzini@redhat.com&gt;
RH-Acked-by: Marcelo Tosatti &lt;mtosatti@redhat.com&gt;
RH-Acked-by: Laszlo Ersek &lt;lersek@redhat.com&gt;

From: Christoph Hellwig &lt;hch@lst.de&gt;

Bugzilla: 783950

Use bdrv_pwrite to access the backing device instead of pread, and
convert the driver to implementing the bdrv_open method which gives
it an already opened BlockDriverState for the underlying device.

Signed-off-by: Christoph Hellwig &lt;hch@lst.de&gt;
Signed-off-by: Kevin Wolf &lt;kwolf@redhat.com&gt;
(cherry picked from commit 7a6f391376c61af1dce2d624a4fcff8eec4fa097)
---
 block/bochs.c |   30 +++++++++---------------------
 1 files changed, 9 insertions(+), 21 deletions(-)

Signed-off-by: Michal Novotny &lt;minovotn@redhat.com&gt;
---
 block/bochs.c |   30 +++++++++---------------------
 1 files changed, 9 insertions(+), 21 deletions(-)

diff --git a/block/bochs.c b/block/bochs.c
index b54f54d..5fe2fa3 100644
--- a/block/bochs.c
+++ b/block/bochs.c
@@ -80,8 +80,6 @@ struct bochs_header {
 };
 
 typedef struct BDRVBochsState {
-    int fd;
-
     uint32_t *catalog_bitmap;
     int catalog_size;
 
@@ -109,23 +107,16 @@ static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
     return 0;
 }
 
-static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
+static int bochs_open(BlockDriverState *bs, int flags)
 {
     BDRVBochsState *s = bs-&gt;opaque;
-    int fd, i;
+    int i;
     struct bochs_header bochs;
     struct bochs_header_v1 header_v1;
 
-    fd = open(filename, O_RDONLY | O_BINARY);
-    if (fd &lt; 0) {
-        return -1;
-    }
-
     bs-&gt;read_only = 1; // no write support yet
 
-    s-&gt;fd = fd;
-
-    if (pread(fd, &amp;bochs, sizeof(bochs), 0) != sizeof(bochs)) {
+    if (bdrv_pread(bs-&gt;file, 0, &amp;bochs, sizeof(bochs)) != sizeof(bochs)) {
         goto fail;
     }
 
@@ -146,8 +137,8 @@ static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
 
     s-&gt;catalog_size = le32_to_cpu(bochs.extra.redolog.catalog);
     s-&gt;catalog_bitmap = qemu_malloc(s-&gt;catalog_size * 4);
-    if (pread(s-&gt;fd, s-&gt;catalog_bitmap, s-&gt;catalog_size * 4,
-              le32_to_cpu(bochs.header)) != s-&gt;catalog_size * 4)
+    if (bdrv_pread(bs-&gt;file, le32_to_cpu(bochs.header), s-&gt;catalog_bitmap,
+                   s-&gt;catalog_size * 4) != s-&gt;catalog_size * 4)
 	goto fail;
     for (i = 0; i &lt; s-&gt;catalog_size; i++)
 	le32_to_cpus(&amp;s-&gt;catalog_bitmap[i]);
@@ -161,7 +152,6 @@ static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
 
     return 0;
  fail:
-    close(fd);
     return -1;
 }
 
@@ -184,8 +174,8 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 	(s-&gt;extent_blocks + s-&gt;bitmap_blocks));
 
     /* read in bitmap for current extent */
-    if (pread(s-&gt;fd, &amp;bitmap_entry, 1, bitmap_offset + (extent_offset / 8))
-            != 1) {
+    if (bdrv_pread(bs-&gt;file, bitmap_offset + (extent_offset / 8),
+                   &amp;bitmap_entry, 1) != 1) {
         return -1;
     }
 
@@ -199,13 +189,12 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 static int bochs_read(BlockDriverState *bs, int64_t sector_num,
                     uint8_t *buf, int nb_sectors)
 {
-    BDRVBochsState *s = bs-&gt;opaque;
     int ret;
 
     while (nb_sectors &gt; 0) {
         int64_t block_offset = seek_to_sector(bs, sector_num);
         if (block_offset &gt;= 0) {
-            ret = pread(s-&gt;fd, buf, 512, block_offset);
+            ret = bdrv_pread(bs-&gt;file, block_offset, buf, 512);
             if (ret != 512) {
                 return -1;
             }
@@ -222,14 +211,13 @@ static void bochs_close(BlockDriverState *bs)
 {
     BDRVBochsState *s = bs-&gt;opaque;
     qemu_free(s-&gt;catalog_bitmap);
-    close(s-&gt;fd);
 }
 
 static BlockDriver bdrv_bochs = {
     .format_name	= "bochs",
     .instance_size	= sizeof(BDRVBochsState),
     .bdrv_probe		= bochs_probe,
-    .bdrv_file_open	= bochs_open,
+    .bdrv_open		= bochs_open,
     .bdrv_read		= bochs_read,
     .bdrv_close		= bochs_close,
 };
-- 
1.7.7.5

</pre></body></html>