From patchwork Tue May 4 10:44:08 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 51579 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2FB81B7D11 for ; Tue, 4 May 2010 20:55:21 +1000 (EST) Received: from localhost ([127.0.0.1]:58100 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O9Fg2-0001VS-5z for incoming@patchwork.ozlabs.org; Tue, 04 May 2010 06:48:34 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O9Fbw-0000v1-8g for qemu-devel@nongnu.org; Tue, 04 May 2010 06:44:20 -0400 Received: from [140.186.70.92] (port=41239 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O9Fbu-0000ud-NI for qemu-devel@nongnu.org; Tue, 04 May 2010 06:44:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O9Fbl-0002ev-QL for qemu-devel@nongnu.org; Tue, 04 May 2010 06:44:18 -0400 Received: from verein.lst.de ([213.95.11.210]:37863) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O9Fbl-0002eZ-8D for qemu-devel@nongnu.org; Tue, 04 May 2010 06:44:09 -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 o44Ai8WY006408 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Tue, 4 May 2010 12:44:08 +0200 Received: (from hch@localhost) by verein.lst.de (8.12.3/8.12.3/Debian-7.2) id o44Ai8gI006407 for qemu-devel@nongnu.org; Tue, 4 May 2010 12:44:08 +0200 Date: Tue, 4 May 2010 12:44:08 +0200 From: Christoph Hellwig To: qemu-devel@nongnu.org Message-ID: <20100504104408.GA6388@lst.de> Mime-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.3.28i X-Scanned-By: MIMEDefang 2.39 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Subject: [Qemu-devel] [PATCH 1/6] bochs: use pread X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Use pread instead of lseek + read in preparation of using the qemu block API. Signed-off-by: Christoph Hellwig Reviewed-by: Anthony Liguori Index: qemu-kevin/block/bochs.c =================================================================== --- qemu-kevin.orig/block/bochs.c 2010-05-03 12:58:53.419012621 +0200 +++ qemu-kevin/block/bochs.c 2010-05-03 12:59:13.873005360 +0200 @@ -125,7 +125,7 @@ static int bochs_open(BlockDriverState * 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->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 * 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) { + /* read in bitmap for current extent */ + if (pread(s->fd, &bitmap_entry, 1, bitmap_offset + (extent_offset / 8)) + != 1) { return -1; } - if (read(s->fd, &bitmap_entry, 1) != 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 (!((bitmap_entry >> (extent_offset % 8)) & 1)) { + return -1; /* not allocated */ } - if (lseek(s->fd, block_offset, SEEK_SET) == (off_t)-1) { - return -1; - } - - 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 * 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++;