From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1OA7JK-0003AN-Qk for qemu-devel@nongnu.org; Thu, 06 May 2010 16:04:42 -0400 Received: from [140.186.70.92] (port=40778 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OA7JJ-000378-7v for qemu-devel@nongnu.org; Thu, 06 May 2010 16:04:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OA7JH-0008KO-Eq for qemu-devel@nongnu.org; Thu, 06 May 2010 16:04:41 -0400 Received: from verein.lst.de ([213.95.11.210]:47630) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OA7JG-0008Jb-U1 for qemu-devel@nongnu.org; Thu, 06 May 2010 16:04:39 -0400 Received: from verein.lst.de (localhost [127.0.0.1]) by verein.lst.de (8.12.3/8.12.3/Debian-7.1) with ESMTP id o46K4YWY006888 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Thu, 6 May 2010 22:04:34 +0200 Received: (from hch@localhost) by verein.lst.de (8.12.3/8.12.3/Debian-7.2) id o46K4YWR006887 for qemu-devel@nongnu.org; Thu, 6 May 2010 22:04:34 +0200 Date: Thu, 6 May 2010 22:04:34 +0200 From: Christoph Hellwig Message-ID: <20100506200434.GA6865@lst.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Subject: [Qemu-devel] [PATCH 1/2] parallels: use pread List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Use pread instead of lseek + read in preparation of using the qemu block API. Signed-off-by: Christoph Hellwig Index: qemu-kevin/block/parallels.c =================================================================== --- qemu-kevin.orig/block/parallels.c 2010-05-06 21:59:25.809255629 +0200 +++ qemu-kevin/block/parallels.c 2010-05-06 22:01:11.343284544 +0200 @@ -83,7 +83,7 @@ static int parallels_open(BlockDriverSta s->fd = fd; - if (read(fd, &ph, sizeof(ph)) != sizeof(ph)) + if (pread(fd, &ph, sizeof(ph), 0) != sizeof(ph)) goto fail; if (memcmp(ph.magic, HEADER_MAGIC, 16) || @@ -93,14 +93,11 @@ static int parallels_open(BlockDriverSta bs->total_sectors = le32_to_cpu(ph.nb_sectors); - if (lseek(s->fd, 64, SEEK_SET) != 64) - goto fail; - s->tracks = le32_to_cpu(ph.tracks); s->catalog_size = le32_to_cpu(ph.catalog_entries); s->catalog_bitmap = qemu_malloc(s->catalog_size * 4); - if (read(s->fd, s->catalog_bitmap, s->catalog_size * 4) != + if (pread(s->fd, s->catalog_bitmap, s->catalog_size * 4, 64) != s->catalog_size * 4) goto fail; for (i = 0; i < s->catalog_size; i++) @@ -114,28 +111,18 @@ fail: 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) { BDRVParallelsState *s = bs->opaque; uint32_t index, offset; - uint64_t position; index = sector_num / s->tracks; offset = sector_num % s->tracks; - // not allocated + /* not allocated */ if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0)) return -1; - - position = (uint64_t)(s->catalog_bitmap[index] + offset) * 512; - -// fprintf(stderr, "sector: %llx index=%x offset=%x pointer=%x position=%x\n", -// sector_num, index, offset, s->catalog_bitmap[index], position); - - if (lseek(s->fd, position, SEEK_SET) != position) - return -1; - - return 0; + return (uint64_t)(s->catalog_bitmap[index] + offset) * 512; } static int parallels_read(BlockDriverState *bs, int64_t sector_num, @@ -144,11 +131,13 @@ static int parallels_read(BlockDriverSta BDRVParallelsState *s = bs->opaque; while (nb_sectors > 0) { - if (!seek_to_sector(bs, sector_num)) { - if (read(s->fd, buf, 512) != 512) - return -1; - } else + int64_t position = seek_to_sector(bs, sector_num); + if (position >= 0) { + if (pread(s->fd, buf, 512, position) != 512) + return -1; + } else { memset(buf, 0, 512); + } nb_sectors--; sector_num++; buf += 512;