From patchwork Mon Dec 22 13:06:06 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Denis V. Lunev" X-Patchwork-Id: 423397 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D44D214007D for ; Tue, 23 Dec 2014 00:08:37 +1100 (AEDT) Received: from localhost ([::1]:40538 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y32ix-0004VP-TY for incoming@patchwork.ozlabs.org; Mon, 22 Dec 2014 08:08:35 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35240) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y32ff-0007O4-AI for qemu-devel@nongnu.org; Mon, 22 Dec 2014 08:05:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y32fW-0001ko-FS for qemu-devel@nongnu.org; Mon, 22 Dec 2014 08:05:11 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:33241 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y32fW-0001jS-2u for qemu-devel@nongnu.org; Mon, 22 Dec 2014 08:05:02 -0500 Received: from hades.sw.ru ([10.30.8.132]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id sBMD4kKG013499; Mon, 22 Dec 2014 17:05:00 +0400 (MSK) From: "Denis V. Lunev" To: Date: Mon, 22 Dec 2014 16:06:06 +0300 Message-Id: <1419253570-16914-9-git-send-email-den@openvz.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1419253570-16914-1-git-send-email-den@openvz.org> References: <1419253570-16914-1-git-send-email-den@openvz.org> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x X-Received-From: 195.214.232.25 Cc: Kevin Wolf , "Denis V. Lunev" , Jeff Cody , qemu-devel@nongnu.org, Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 08/12] block/parallels: _co_writev callback for Parallels format X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Support write on Parallels images. The code is almost the same as one in the previous patch implemented scatter-gather IO for read. Signed-off-by: Denis V. Lunev Acked-by: Roman Kagan CC: Jeff Cody CC: Kevin Wolf CC: Stefan Hajnoczi --- block/parallels.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/block/parallels.c b/block/parallels.c index 306f2e3..dca3d0b 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -81,8 +81,6 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags, ParallelsHeader ph; int ret; - bs->read_only = 1; // no write support yet - ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph)); if (ret < 0) { goto fail; @@ -159,6 +157,37 @@ static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num) return (uint64_t)s->catalog_bitmap[index] * s->off_multiplier + offset; } +static int64_t allocate_sector(BlockDriverState *bs, int64_t sector_num) +{ + BDRVParallelsState *s = bs->opaque; + uint32_t idx, offset, tmp; + int64_t pos; + int ret; + + idx = sector_num / s->tracks; + offset = sector_num % s->tracks; + + if (idx >= s->catalog_size) { + return -EINVAL; + } + if (s->catalog_bitmap[idx] != 0) { + return (uint64_t)s->catalog_bitmap[idx] * s->off_multiplier + offset; + } + + pos = bdrv_getlength(bs->file) >> BDRV_SECTOR_BITS; + bdrv_truncate(bs->file, (pos + s->tracks) << BDRV_SECTOR_BITS); + s->catalog_bitmap[idx] = pos / s->off_multiplier; + + tmp = cpu_to_le32(s->catalog_bitmap[idx]); + + ret = bdrv_pwrite_sync(bs->file, + sizeof(ParallelsHeader) + idx * sizeof(tmp), &tmp, sizeof(tmp)); + if (ret < 0) { + return ret; + } + return (uint64_t)s->catalog_bitmap[idx] * s->off_multiplier + offset; +} + static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num, int nb_sectors) { @@ -186,6 +215,49 @@ static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs, BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; } +static coroutine_fn int parallels_co_writev(BlockDriverState *bs, + int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) +{ + BDRVParallelsState *s = bs->opaque; + uint64_t bytes_done = 0; + QEMUIOVector hd_qiov; + int ret = 0; + + qemu_iovec_init(&hd_qiov, qiov->niov); + + qemu_co_mutex_lock(&s->lock); + while (nb_sectors > 0) { + int64_t position = allocate_sector(bs, sector_num); + int n = cluster_remainder(s, sector_num, nb_sectors); + int nbytes = n << BDRV_SECTOR_BITS; + + if (position < 0) { + ret = (int)position; + break; + } + + qemu_iovec_reset(&hd_qiov); + qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes); + + qemu_co_mutex_unlock(&s->lock); + ret = bdrv_co_writev(bs->file, position, n, &hd_qiov); + qemu_co_mutex_lock(&s->lock); + + if (ret < 0) { + goto fail; + } + + nb_sectors -= n; + sector_num += n; + bytes_done += nbytes; + } + qemu_co_mutex_unlock(&s->lock); + +fail: + qemu_iovec_destroy(&hd_qiov); + return ret; +} + static coroutine_fn int parallels_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) { @@ -242,6 +314,7 @@ static BlockDriver bdrv_parallels = { .bdrv_close = parallels_close, .bdrv_co_get_block_status = parallels_co_get_block_status, .bdrv_co_readv = parallels_co_readv, + .bdrv_co_writev = parallels_co_writev, }; static void bdrv_parallels_init(void)