From patchwork Sat Apr 16 15:05:56 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 712571 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p3GF6MP2029508 for ; Sat, 16 Apr 2011 15:06:22 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755894Ab1DPPGT (ORCPT ); Sat, 16 Apr 2011 11:06:19 -0400 Received: from mail-ww0-f44.google.com ([74.125.82.44]:64526 "EHLO mail-ww0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754581Ab1DPPGQ (ORCPT ); Sat, 16 Apr 2011 11:06:16 -0400 Received: by wwa36 with SMTP id 36so4174584wwa.1 for ; Sat, 16 Apr 2011 08:06:15 -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=70mtfFLMdPtSiEI65XgN7ia9usqm8/l4rBVujGBd/DA=; b=oe2tag7W+8S3MS9KTkIMhwLe41F23OIsmR1O5ePucTMi2sbA3sXXlGCEh6HyNVl6Q/ Fd00nB1mGiLPwOb+5I4jDyMC0lMOEXnPerlLLR0eIBsXBMQw4oR82kgstP2ITglX6Se8 2xa8ZELOw1KDweu9UfvhUM+YE1488jDgkWKbU= 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=rZj8eIJz5XShULIWq+CaClc+jRnsjwuGiA4MHAy2jjW3Z8kCAwRVoS5Q43LVvZoFy+ g87wzXOrQuEfO/npze3aDueAGvYSy9FFyppECfAHDemmQi38oVhAwbBcDJ1DHNxJJBep knntF9IrxbNjLvdRBdi7GtRSCVKB7FGKb5WRc= Received: by 10.216.254.82 with SMTP id g60mr2956020wes.90.1302966374759; Sat, 16 Apr 2011 08:06:14 -0700 (PDT) Received: from localhost.localdomain (46-117-236-160.bb.netvision.net.il [46.117.236.160]) by mx.google.com with ESMTPS id r80sm1770866wei.39.2011.04.16.08.06.13 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 16 Apr 2011 08:06:14 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, kvm@vger.kernel.org, Sasha Levin Subject: [PATCH 2/2 V2] kvm tools: Add scatter-gather support for disk images Date: Sat, 16 Apr 2011 18:05:56 +0300 Message-Id: <1302966356-13145-2-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.5.rc1 In-Reply-To: <1302966356-13145-1-git-send-email-levinsasha928@gmail.com> References: <1302966356-13145-1-git-send-email-levinsasha928@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 (demeter1.kernel.org [140.211.167.41]); Sat, 16 Apr 2011 15:06:22 +0000 (UTC) Add optional support for scatter-gather to disk_image. Formats that can't take advantage of scatter-gather fallback to simple IO. Signed-off-by: Sasha Levin --- tools/kvm/disk-image.c | 16 ++++++++++++ tools/kvm/include/kvm/disk-image.h | 31 +++++++++++++++++++++++ tools/kvm/virtio-blk.c | 47 +++++++++++------------------------ 3 files changed, 62 insertions(+), 32 deletions(-) diff --git a/tools/kvm/disk-image.c b/tools/kvm/disk-image.c index 4198ebb..3a93645 100644 --- a/tools/kvm/disk-image.c +++ b/tools/kvm/disk-image.c @@ -72,6 +72,20 @@ static int raw_image__write_sector(struct disk_image *self, uint64_t sector, voi return 0; } +static ssize_t raw_image__read_sector_sg(struct disk_image *self, uint64_t sector, const struct iovec *iov, int iovcount) +{ + uint64_t offset = sector << SECTOR_SHIFT; + + return preadv_in_full(self->fd, iov, iovcount, offset); +} + +static ssize_t raw_image__write_sector_sg(struct disk_image *self, uint64_t sector, const struct iovec *iov, int iovcount) +{ + uint64_t offset = sector << SECTOR_SHIFT; + + return pwritev_in_full(self->fd, iov, iovcount, offset); +} + static int raw_image__read_sector_ro_mmap(struct disk_image *self, uint64_t sector, void *dst, uint32_t dst_len) { uint64_t offset = sector << SECTOR_SHIFT; @@ -105,6 +119,8 @@ static void raw_image__close_ro_mmap(struct disk_image *self) static struct disk_image_operations raw_image_ops = { .read_sector = raw_image__read_sector, .write_sector = raw_image__write_sector, + .read_sector_sg = raw_image__read_sector_sg, + .write_sector_sg = raw_image__write_sector_sg }; static struct disk_image_operations raw_image_ro_mmap_ops = { diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h index 33962c6..a3ba360 100644 --- a/tools/kvm/include/kvm/disk-image.h +++ b/tools/kvm/include/kvm/disk-image.h @@ -3,6 +3,7 @@ #include #include +#include #define SECTOR_SHIFT 9 #define SECTOR_SIZE (1UL << SECTOR_SHIFT) @@ -12,6 +13,8 @@ struct disk_image; struct disk_image_operations { int (*read_sector)(struct disk_image *self, uint64_t sector, void *dst, uint32_t dst_len); int (*write_sector)(struct disk_image *self, uint64_t sector, void *src, uint32_t src_len); + ssize_t (*read_sector_sg)(struct disk_image *self, uint64_t sector, const struct iovec *iov, int iovcount); + ssize_t (*write_sector_sg)(struct disk_image *self, uint64_t sector, const struct iovec *iov, int iovcount); void (*close)(struct disk_image *self); }; @@ -37,4 +40,32 @@ static inline int disk_image__write_sector(struct disk_image *self, uint64_t sec return self->ops->write_sector(self, sector, src, src_len); } +static inline ssize_t disk_image__read_sector_sg(struct disk_image *self, uint64_t sector, const struct iovec *iov, int iovcount) +{ + if (self->ops->read_sector_sg) + return self->ops->read_sector_sg(self, sector, iov, iovcount); + + while (iovcount--) { + self->ops->read_sector(self, sector, iov->iov_base, iov->iov_len); + sector += iov->iov_len >> SECTOR_SHIFT; + iov++; + } + + return sector << SECTOR_SHIFT; +} + +static inline ssize_t disk_image__write_sector_sg(struct disk_image *self, uint64_t sector, const struct iovec *iov, int iovcount) +{ + if (self->ops->write_sector_sg) + return self->ops->write_sector_sg(self, sector, iov, iovcount); + + while (iovcount--) { + self->ops->write_sector(self, sector, iov->iov_base, iov->iov_len); + sector += iov->iov_len >> SECTOR_SHIFT; + iov++; + } + + return sector << SECTOR_SHIFT; +} + #endif /* KVM__DISK_IMAGE_H */ diff --git a/tools/kvm/virtio-blk.c b/tools/kvm/virtio-blk.c index cb344d08..58c4be9 100644 --- a/tools/kvm/virtio-blk.c +++ b/tools/kvm/virtio-blk.c @@ -115,50 +115,33 @@ static bool virtio_blk_do_io_request(struct kvm *self, struct virt_queue *queue) { struct iovec iov[VIRTIO_BLK_QUEUE_SIZE]; struct virtio_blk_outhdr *req; - uint32_t block_len, block_cnt; + ssize_t block_cnt = -1; uint16_t out, in, head; uint8_t *status; - bool io_error; - void *block; - int err, i; - - io_error = false; head = virt_queue__get_iov(queue, iov, &out, &in, self); /* head */ req = iov[0].iov_base; - /* block */ - block_cnt = 0; - - for (i = 1; i < out + in - 1; i++) { - block = iov[i].iov_base; - block_len = iov[i].iov_len; - - switch (req->type) { - case VIRTIO_BLK_T_IN: - err = disk_image__read_sector(self->disk_image, req->sector, block, block_len); - if (err) - io_error = true; - break; - case VIRTIO_BLK_T_OUT: - err = disk_image__write_sector(self->disk_image, req->sector, block, block_len); - if (err) - io_error = true; - break; - default: - warning("request type %d", req->type); - io_error = true; - } - - req->sector += block_len >> SECTOR_SHIFT; - block_cnt += block_len; + switch (req->type) { + case VIRTIO_BLK_T_IN: + block_cnt = disk_image__read_sector_sg(self->disk_image, req->sector, iov + 1, in + out - 2); + + break; + case VIRTIO_BLK_T_OUT: + block_cnt = disk_image__write_sector_sg(self->disk_image, req->sector, iov + 1, in + out - 2); + + break; + + default: + warning("request type %d", req->type); + block_cnt = -1; } /* status */ status = iov[out + in - 1].iov_base; - *status = io_error ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK; + *status = (block_cnt < 0) ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK; virt_queue__set_used_elem(queue, head, block_cnt);