qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: aliguori@linux.vnet.ibm.com
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 06/11] bochs: use pread
Date: Fri,  7 May 2010 17:14:02 +0200	[thread overview]
Message-ID: <1273245247-24827-7-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1273245247-24827-1-git-send-email-kwolf@redhat.com>

From: Christoph Hellwig <hch@lst.de>

Use pread instead of lseek + read in preparation of using the qemu
block API.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/bochs.c |   63 ++++++++++++++++++--------------------------------------
 1 files changed, 20 insertions(+), 43 deletions(-)

diff --git a/block/bochs.c b/block/bochs.c
index e952670..b54f54d 100644
--- a/block/bochs.c
+++ b/block/bochs.c
@@ -125,7 +125,7 @@ static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
 
     s->fd = fd;
 
-    if (read(fd, &bochs, sizeof(bochs)) != sizeof(bochs)) {
+    if (pread(fd, &bochs, sizeof(bochs), 0) != sizeof(bochs)) {
         goto fail;
     }
 
@@ -144,14 +144,10 @@ static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
       bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
     }
 
-    if (lseek(s->fd, le32_to_cpu(bochs.header), SEEK_SET) == (off_t)-1) {
-        goto fail;
-    }
-
     s->catalog_size = le32_to_cpu(bochs.extra.redolog.catalog);
     s->catalog_bitmap = qemu_malloc(s->catalog_size * 4);
-    if (read(s->fd, s->catalog_bitmap, s->catalog_size * 4) !=
-	s->catalog_size * 4)
+    if (pread(s->fd, s->catalog_bitmap, s->catalog_size * 4,
+              le32_to_cpu(bochs.header)) != s->catalog_size * 4)
 	goto fail;
     for (i = 0; i < s->catalog_size; i++)
 	le32_to_cpus(&s->catalog_bitmap[i]);
@@ -169,54 +165,35 @@ static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
     return -1;
 }
 
-static inline int seek_to_sector(BlockDriverState *bs, int64_t sector_num)
+static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 {
     BDRVBochsState *s = bs->opaque;
     int64_t offset = sector_num * 512;
-    int64_t extent_index, extent_offset, bitmap_offset, block_offset;
+    int64_t extent_index, extent_offset, bitmap_offset;
     char bitmap_entry;
 
     // seek to sector
     extent_index = offset / s->extent_size;
     extent_offset = (offset % s->extent_size) / 512;
 
-    if (s->catalog_bitmap[extent_index] == 0xffffffff)
-    {
-//	fprintf(stderr, "page not allocated [%x - %x:%x]\n",
-//	    sector_num, extent_index, extent_offset);
-	return -1; // not allocated
+    if (s->catalog_bitmap[extent_index] == 0xffffffff) {
+	return -1; /* not allocated */
     }
 
     bitmap_offset = s->data_offset + (512 * s->catalog_bitmap[extent_index] *
 	(s->extent_blocks + s->bitmap_blocks));
-    block_offset = bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
-
-//    fprintf(stderr, "sect: %x [ext i: %x o: %x] -> %x bitmap: %x block: %x\n",
-//	sector_num, extent_index, extent_offset,
-//	le32_to_cpu(s->catalog_bitmap[extent_index]),
-//	bitmap_offset, block_offset);
-
-    // read in bitmap for current extent
-    if (lseek(s->fd, bitmap_offset + (extent_offset / 8), SEEK_SET) ==
-        (off_t)-1) {
-        return -1;
-    }
 
-    if (read(s->fd, &bitmap_entry, 1) != 1)
+    /* read in bitmap for current extent */
+    if (pread(s->fd, &bitmap_entry, 1, bitmap_offset + (extent_offset / 8))
+            != 1) {
         return -1;
-
-    if (!((bitmap_entry >> (extent_offset % 8)) & 1))
-    {
-//	fprintf(stderr, "sector (%x) in bitmap not allocated\n",
-//	    sector_num);
-	return -1; // not allocated
     }
 
-    if (lseek(s->fd, block_offset, SEEK_SET) == (off_t)-1) {
-        return -1;
+    if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
+	return -1; /* not allocated */
     }
 
-    return 0;
+    return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
 }
 
 static int bochs_read(BlockDriverState *bs, int64_t sector_num,
@@ -226,13 +203,13 @@ static int bochs_read(BlockDriverState *bs, int64_t sector_num,
     int ret;
 
     while (nb_sectors > 0) {
-	if (!seek_to_sector(bs, sector_num))
-	{
-	    ret = read(s->fd, buf, 512);
-	    if (ret != 512)
-		return -1;
-	}
-	else
+        int64_t block_offset = seek_to_sector(bs, sector_num);
+        if (block_offset >= 0) {
+            ret = pread(s->fd, buf, 512, block_offset);
+            if (ret != 512) {
+                return -1;
+            }
+        } else
             memset(buf, 0, 512);
         nb_sectors--;
         sector_num++;
-- 
1.6.6.1

  parent reply	other threads:[~2010-05-07 15:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-07 15:13 [Qemu-devel] [PULL 00/11] Block patches Kevin Wolf
2010-05-07 15:13 ` [Qemu-devel] [PATCH 01/11] block: Remove semicolon in BDRV_SECTOR_MASK macro Kevin Wolf
2010-05-07 15:13 ` [Qemu-devel] [PATCH 02/11] qemu-nbd: Improve error reporting Kevin Wolf
2010-05-07 15:13 ` [Qemu-devel] [PATCH 03/11] cloop: use pread Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 04/11] cloop: use qemu block API Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 05/11] ide: Fix ide_dma_cancel Kevin Wolf
2010-05-07 15:14 ` Kevin Wolf [this message]
2010-05-07 15:14 ` [Qemu-devel] [PATCH 07/11] bochs: use qemu block API Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 08/11] block: Avoid unchecked casts for AIOCBs Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 09/11] block: Fix protocol detection for Windows devices Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 10/11] block: Fix bdrv_commit Kevin Wolf
2010-05-07 15:14 ` [Qemu-devel] [PATCH 11/11] block/vdi: Allow disk images of size 0 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=1273245247-24827-7-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=aliguori@linux.vnet.ibm.com \
    --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).