From: Christoph Hellwig <hch@lst.de>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 3/6] cloop: use pread
Date: Tue, 4 May 2010 12:44:38 +0200 [thread overview]
Message-ID: <20100504104438.GC6388@lst.de> (raw)
In-Reply-To: <20100504104351.GA6342@lst.de>
Use pread instead of lseek + read in preparation of using the qemu
block API.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Index: qemu-kevin/block/cloop.c
===================================================================
--- qemu-kevin.orig/block/cloop.c 2010-05-03 13:01:09.035025542 +0200
+++ qemu-kevin/block/cloop.c 2010-05-03 13:01:57.918284307 +0200
@@ -62,23 +62,22 @@ static int cloop_open(BlockDriverState *
bs->read_only = 1;
/* read header */
- if(lseek(s->fd,128,SEEK_SET)<0) {
-cloop_close:
- close(s->fd);
- return -1;
+ if (pread(s->fd, &s->block_size, 4, 128) < 4) {
+ goto cloop_close;
}
- if(read(s->fd,&s->block_size,4)<4)
- goto cloop_close;
- s->block_size=be32_to_cpu(s->block_size);
- if(read(s->fd,&s->n_blocks,4)<4)
- goto cloop_close;
- s->n_blocks=be32_to_cpu(s->n_blocks);
+ s->block_size = be32_to_cpu(s->block_size);
+
+ if (pread(s->fd, &s->n_blocks, 4, 128 + 4) < 4) {
+ goto cloop_close;
+ }
+ s->n_blocks = be32_to_cpu(s->n_blocks);
/* read offsets */
- offsets_size=s->n_blocks*sizeof(uint64_t);
- s->offsets=(uint64_t*)qemu_malloc(offsets_size);
- if(read(s->fd,s->offsets,offsets_size)<offsets_size)
+ offsets_size = s->n_blocks * sizeof(uint64_t);
+ s->offsets = qemu_malloc(offsets_size);
+ if (pread(s->fd, s->offsets, offsets_size, 128 + 4 + 4) < offsets_size) {
goto cloop_close;
+ }
for(i=0;i<s->n_blocks;i++) {
s->offsets[i]=be64_to_cpu(s->offsets[i]);
if(i>0) {
@@ -98,6 +97,10 @@ cloop_close:
s->sectors_per_block = s->block_size/512;
bs->total_sectors = s->n_blocks*s->sectors_per_block;
return 0;
+
+cloop_close:
+ close(s->fd);
+ return -1;
}
static inline int cloop_read_block(BDRVCloopState *s,int block_num)
@@ -106,8 +109,7 @@ static inline int cloop_read_block(BDRVC
int ret;
uint32_t bytes = s->offsets[block_num+1]-s->offsets[block_num];
- lseek(s->fd, s->offsets[block_num], SEEK_SET);
- ret = read(s->fd, s->compressed_block, bytes);
+ ret = pread(s->fd, s->compressed_block, bytes, s->offsets[block_num]);
if (ret != bytes)
return -1;
next prev parent reply other threads:[~2010-05-04 10:44 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-05-04 10:43 [Qemu-devel] simple block driver cleanups Christoph Hellwig
2010-05-04 10:44 ` [Qemu-devel] [PATCH 2/6] bochs: use qemu block API Christoph Hellwig
2010-05-04 10:44 ` Christoph Hellwig [this message]
2010-05-04 10:44 ` [Qemu-devel] [PATCH 4/6] cloop: " Christoph Hellwig
2010-05-04 10:45 ` [Qemu-devel] [PATCH 5/6] parallels: use pread Christoph Hellwig
2010-05-05 14:19 ` Kevin Wolf
2010-05-05 18:11 ` Christoph Hellwig
2010-05-04 10:45 ` [Qemu-devel] [PATCH 6/6] parallels: use qemu block API Christoph Hellwig
2010-05-04 12:38 ` [Qemu-devel] simple block driver cleanups Kevin Wolf
2010-05-04 13:22 ` Christoph Hellwig
2010-05-05 14:28 ` Kevin Wolf
2010-05-05 18:10 ` Christoph Hellwig
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=20100504104438.GC6388@lst.de \
--to=hch@lst.de \
--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).