From patchwork Tue Apr 28 07:46:42 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Denis V. Lunev" X-Patchwork-Id: 465402 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 04A2814016A for ; Tue, 28 Apr 2015 17:52:12 +1000 (AEST) Received: from localhost ([::1]:59344 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yn0JO-0005ax-85 for incoming@patchwork.ozlabs.org; Tue, 28 Apr 2015 03:52:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44688) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yn0Ea-00052y-Hm for qemu-devel@nongnu.org; Tue, 28 Apr 2015 03:47:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yn0EZ-0004Bb-9n for qemu-devel@nongnu.org; Tue, 28 Apr 2015 03:47:12 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:29628 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yn0EY-0004BM-Tm for qemu-devel@nongnu.org; Tue, 28 Apr 2015 03:47:11 -0400 Received: from hades.sw.ru ([10.30.8.132]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id t3S7ktlg031473; Tue, 28 Apr 2015 10:47:09 +0300 (MSK) From: "Denis V. Lunev" To: Date: Tue, 28 Apr 2015 10:46:42 +0300 Message-Id: <1430207220-24458-10-git-send-email-den@openvz.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1430207220-24458-1-git-send-email-den@openvz.org> References: <1430207220-24458-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" , qemu-devel@nongnu.org, Stefan Hajnoczi , Roman Kagan Subject: [Qemu-devel] [PATCH 09/27] 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 CC: Roman Kagan CC: Kevin Wolf CC: Stefan Hajnoczi Reviewed-by: Roman Kagan Reviewed-by: Stefan Hajnoczi --- block/parallels.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/block/parallels.c b/block/parallels.c index ae64ce5..8d73803 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -60,6 +60,8 @@ typedef struct BDRVParallelsState { unsigned int tracks; unsigned int off_multiplier; + + bool has_truncate; } BDRVParallelsState; static int parallels_probe(const uint8_t *buf, int buf_size, const char *filename) @@ -85,8 +87,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; @@ -139,6 +139,9 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags, for (i = 0; i < s->catalog_size; i++) le32_to_cpus(&s->catalog_bitmap[i]); + s->has_truncate = bdrv_has_zero_init(bs->file) && + bdrv_truncate(bs->file, bdrv_getlength(bs->file)) == 0; + qemu_co_mutex_init(&s->lock); return 0; @@ -170,6 +173,46 @@ static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num, return MIN(nb_sectors, ret); } +static int64_t allocate_cluster(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; + if (s->has_truncate) { + ret = bdrv_truncate(bs->file, (pos + s->tracks) << BDRV_SECTOR_BITS); + } else { + ret = bdrv_write_zeroes(bs->file, pos, s->tracks, 0); + } + if (ret < 0) { + return ret; + } + + s->catalog_bitmap[idx] = pos / s->off_multiplier; + + tmp = cpu_to_le32(s->catalog_bitmap[idx]); + + ret = bdrv_pwrite(bs->file, + sizeof(ParallelsHeader) + idx * sizeof(tmp), &tmp, sizeof(tmp)); + if (ret < 0) { + s->catalog_bitmap[idx] = 0; + return ret; + } + return (uint64_t)s->catalog_bitmap[idx] * s->off_multiplier + offset; +} + static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum) { @@ -190,6 +233,48 @@ 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); + + while (nb_sectors > 0) { + int64_t position; + int n, nbytes; + + qemu_co_mutex_lock(&s->lock); + position = allocate_cluster(bs, sector_num); + qemu_co_mutex_unlock(&s->lock); + if (position < 0) { + ret = (int)position; + break; + } + + n = cluster_remainder(s, sector_num, nb_sectors); + nbytes = n << BDRV_SECTOR_BITS; + + qemu_iovec_reset(&hd_qiov); + qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes); + + ret = bdrv_co_writev(bs->file, position, n, &hd_qiov); + if (ret < 0) { + break; + } + + nb_sectors -= n; + sector_num += n; + bytes_done += nbytes; + } + + 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) { @@ -247,6 +332,7 @@ static BlockDriver bdrv_parallels = { .bdrv_co_get_block_status = parallels_co_get_block_status, .bdrv_has_zero_init = bdrv_has_zero_init_1, .bdrv_co_readv = parallels_co_readv, + .bdrv_co_writev = parallels_co_writev, }; static void bdrv_parallels_init(void)