From patchwork Wed May 18 08:19:09 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 793082 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.3) with ESMTP id p4I8O3wn004358 for ; Wed, 18 May 2011 08:24:06 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755576Ab1ERIXV (ORCPT ); Wed, 18 May 2011 04:23:21 -0400 Received: from mail-pw0-f46.google.com ([209.85.160.46]:41627 "EHLO mail-pw0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755474Ab1ERIXU (ORCPT ); Wed, 18 May 2011 04:23:20 -0400 Received: by pwi15 with SMTP id 15so644382pwi.19 for ; Wed, 18 May 2011 01:23:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=xGAv3izIg/DsjREBQMFxSnJRbvFKZbSsB3YwwgADpm8=; b=dutDWb3PWZycOtqzJbIIAMqrVktm+g8BbpHqoAI9BBdJndtFCoudmrodATE1M2g4zL bIrAkmmV1NmAWbQElErRlrVxJw6cTw/50yuV9Yqim93fx5iNYu3LKqgBuKybMTMmAenV 0FCqFY5ljsVcjhleP6IRkwLQskLuMsOoraAYM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=PnygoQyfPsb+I8S6d60FYHfOvLJNkOamy4WbBf7Ur7Ld6o42lhMDQwbD+QI6U4uQnR TugvOtYZ7Jr05yxDG8b0oKzymZxBBB9kHNoAfU57BRiTaFhF30LcBpsIjvgfOWArO/68 kfISUunxAcm9y4EZEmftB14kS2cHx4sIdTk1M= Received: by 10.68.68.111 with SMTP id v15mr2487236pbt.310.1305706999812; Wed, 18 May 2011 01:23:19 -0700 (PDT) Received: from localhost.localdomain ([219.224.169.130]) by mx.google.com with ESMTPS id q10sm912018pbk.39.2011.05.18.01.23.13 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 18 May 2011 01:23:19 -0700 (PDT) From: Asias He To: Pekka Enberg Cc: Cyrill Gorcunov , Ingo Molnar , Sasha Levin , Prasad Joshi , kvm@vger.kernel.org, Asias He Subject: [PATCH 08/14] kvm tools: Tune up ops in 'struct disk_image_operations' Date: Wed, 18 May 2011 16:19:09 +0800 Message-Id: <1305706755-2816-8-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.5.1 In-Reply-To: <1305706755-2816-1-git-send-email-asias.hejun@gmail.com> References: <1305706755-2816-1-git-send-email-asias.hejun@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Wed, 18 May 2011 08:24:06 +0000 (UTC) Make read/write ops in 'struct disk_image_operations' always return the number of bytes read/written and close/flush ops return int. Signed-off-by: Asias He --- tools/kvm/disk/core.c | 8 +++++--- tools/kvm/disk/qcow.c | 18 ++++++++++-------- tools/kvm/disk/raw.c | 16 ++++++++++------ tools/kvm/include/kvm/disk-image.h | 22 +++++++++++++++------- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/tools/kvm/disk/core.c b/tools/kvm/disk/core.c index 2990caa..a0ccdc5 100644 --- a/tools/kvm/disk/core.c +++ b/tools/kvm/disk/core.c @@ -55,19 +55,21 @@ struct disk_image *disk_image__open(const char *filename, bool readonly) return NULL; } -void disk_image__close(struct disk_image *disk) +int disk_image__close(struct disk_image *disk) { /* If there was no disk image then there's nothing to do: */ if (!disk) - return; + return 0; if (disk->ops->close) - disk->ops->close(disk); + return disk->ops->close(disk); if (close(disk->fd) < 0) warning("close() failed"); free(disk); + + return 0; } /* Fill iov with disk data, starting from sector 'sector'. Return amount of bytes read. */ diff --git a/tools/kvm/disk/qcow.c b/tools/kvm/disk/qcow.c index b29efb4..956775e 100644 --- a/tools/kvm/disk/qcow.c +++ b/tools/kvm/disk/qcow.c @@ -103,7 +103,7 @@ out_error: goto out; } -static int qcow1_read_sector(struct disk_image *disk, u64 sector, void *dst, u32 dst_len) +static ssize_t qcow1_read_sector(struct disk_image *disk, u64 sector, void *dst, u32 dst_len) { struct qcow *q = disk->priv; struct qcow_header *header = q->header; @@ -129,7 +129,7 @@ static int qcow1_read_sector(struct disk_image *disk, u64 sector, void *dst, u32 sector += (nr >> SECTOR_SHIFT); } - return 0; + return dst_len; } static inline u64 file_size(int fd) @@ -291,7 +291,7 @@ error: return -1; } -static int qcow1_write_sector(struct disk_image *disk, u64 sector, void *src, u32 src_len) +static ssize_t qcow1_write_sector(struct disk_image *disk, u64 sector, void *src, u32 src_len) { struct qcow *q = disk->priv; struct qcow_header *header = q->header; @@ -317,33 +317,35 @@ static int qcow1_write_sector(struct disk_image *disk, u64 sector, void *src, u3 offset += nr; } - return 0; + return nr_written; } -static int qcow1_nowrite_sector(struct disk_image *disk, u64 sector, void *src, u32 src_len) +static ssize_t qcow1_nowrite_sector(struct disk_image *disk, u64 sector, void *src, u32 src_len) { /* I/O error */ return -1; } -static void qcow1_disk_close(struct disk_image *disk) +static int qcow1_disk_close(struct disk_image *disk) { struct qcow *q; if (!disk) - return; + return 0; q = disk->priv; free(q->table.l1_table); free(q->header); free(q); + + return 0; } static struct disk_image_operations qcow1_disk_readonly_ops = { .read_sector = qcow1_read_sector, .write_sector = qcow1_nowrite_sector, - .close = qcow1_disk_close + .close = qcow1_disk_close, }; static struct disk_image_operations qcow1_disk_ops = { diff --git a/tools/kvm/disk/raw.c b/tools/kvm/disk/raw.c index 8ee7e3f..b58837b 100644 --- a/tools/kvm/disk/raw.c +++ b/tools/kvm/disk/raw.c @@ -14,7 +14,7 @@ ssize_t raw_image__write_sector_iov(struct disk_image *disk, u64 sector, const s return pwritev_in_full(disk->fd, iov, iovcount, offset); } -int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len) +ssize_t raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len) { u64 offset = sector << SECTOR_SHIFT; @@ -23,10 +23,10 @@ int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *ds memcpy(dst, disk->priv + offset, dst_len); - return 0; + return dst_len; } -int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len) +ssize_t raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len) { u64 offset = sector << SECTOR_SHIFT; @@ -35,13 +35,17 @@ int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *s memcpy(disk->priv + offset, src, src_len); - return 0; + return src_len; } -void raw_image__close_ro_mmap(struct disk_image *disk) +int raw_image__close_ro_mmap(struct disk_image *disk) { + int ret = 0; + if (disk->priv != MAP_FAILED) - munmap(disk->priv, disk->size); + ret = munmap(disk->priv, disk->size); + + return ret; } static struct disk_image_operations raw_image_ops = { diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h index 463801e..6531a93 100644 --- a/tools/kvm/include/kvm/disk-image.h +++ b/tools/kvm/include/kvm/disk-image.h @@ -27,12 +27,20 @@ struct disk_image; struct disk_image_operations { - int (*read_sector)(struct disk_image *disk, u64 sector, void *dst, u32 dst_len); - int (*write_sector)(struct disk_image *disk, u64 sector, void *src, u32 src_len); + /* + * The following two are used for reading or writing with a single buffer. + * The implentation can use readv/writev or memcpy. + */ + ssize_t (*read_sector)(struct disk_image *disk, u64 sector, void *dst, u32 dst_len); + ssize_t (*write_sector)(struct disk_image *disk, u64 sector, void *src, u32 src_len); + /* + * The following two are used for reading or writing with multiple buffers. + * The implentation can use readv/writev or memcpy. + */ ssize_t (*read_sector_iov)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount); ssize_t (*write_sector_iov)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount); int (*flush)(struct disk_image *disk); - void (*close)(struct disk_image *disk); + int (*close)(struct disk_image *disk); }; struct disk_image { @@ -44,7 +52,7 @@ struct disk_image { struct disk_image *disk_image__open(const char *filename, bool readonly); struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops, int mmap); -void disk_image__close(struct disk_image *disk); +int disk_image__close(struct disk_image *disk); ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount); ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount); @@ -61,8 +69,8 @@ struct disk_image *blkdev__probe(const char *filename, struct stat *st); ssize_t raw_image__read_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount); ssize_t raw_image__write_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount); -int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len); -int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len); -void raw_image__close_ro_mmap(struct disk_image *disk); +ssize_t raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len); +ssize_t raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len); +int raw_image__close_ro_mmap(struct disk_image *disk); #endif /* KVM__DISK_IMAGE_H */