From nobody Fri Mar 29 10:32:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562875121; cv=none; d=zoho.com; s=zohoarc; b=K2QLjS2EhJKroD+bPz/+q2VSojE3TFMEyXBlE/IcobsvTmoM86CtM/b1LxZ2rFu270lxn6D/NsC7zzVvuTOy++ZRQlMGrhOtnF/suWaqOK+bvs9+QWcF7VQ/BiaUW33z+4I4DPUNykU4Nm95rtdZhb8K9DynvChzbgwQI/g69xQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562875121; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=14x9hUMzF4higLG/0XWwXhEISdOpvKBq90xS/hLzics=; b=jB2DeKcYe+BRjmzEbOz4un58uC/vaKdptCmSAY50L95ZE3DxXlGBGib7x8CDFgmCvbdanXWPDg8F542jbcHUe5rcNXlIq5EJ5/P6J09tNUfPfvN8lan8eogdcE2KrIwKJd4X3ORn8AVKo41Et8msYUa2BAYvIEr3oIEOh0SBTgM= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15628751210971016.3257589120386; Thu, 11 Jul 2019 12:58:41 -0700 (PDT) Received: from localhost ([::1]:44958 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlfDA-0004W9-VS for importer@patchew.org; Thu, 11 Jul 2019 15:58:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51307) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlfCt-0003kK-G7 for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hlfCr-0000lm-4U for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:23784) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hlfCp-0000jn-Nn for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:16 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AF3358762B; Thu, 11 Jul 2019 19:58:13 +0000 (UTC) Received: from localhost (unknown [10.40.205.198]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EF2405DA65; Thu, 11 Jul 2019 19:58:10 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Thu, 11 Jul 2019 21:58:00 +0200 Message-Id: <20190711195804.30703-2-mreitz@redhat.com> In-Reply-To: <20190711195804.30703-1-mreitz@redhat.com> References: <20190711195804.30703-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 11 Jul 2019 19:58:13 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC 1/5] block/nbd: Fix hang in .bdrv_close() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" When nbd_close() is called from a coroutine, the connection_co never gets to run, and thus nbd_teardown_connection() hangs. This is because aio_co_enter() only puts the connection_co into the main coroutine's wake-up queue, so this main coroutine needs to yield and reschedule itself to let the connection_co run. Signed-off-by: Max Reitz --- block/nbd.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/block/nbd.c b/block/nbd.c index 81edabbf35..b83b6cd43e 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -135,7 +135,17 @@ static void nbd_teardown_connection(BlockDriverState *= bs) qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); - BDRV_POLL_WHILE(bs, s->connection_co); + + if (qemu_in_coroutine()) { + /* Let our caller poll and just yield until connection_co is done = */ + while (s->connection_co) { + aio_co_schedule(qemu_get_current_aio_context(), + qemu_coroutine_self()); + qemu_coroutine_yield(); + } + } else { + BDRV_POLL_WHILE(bs, s->connection_co); + } =20 nbd_client_detach_aio_context(bs); object_unref(OBJECT(s->sioc)); --=20 2.21.0 From nobody Fri Mar 29 10:32:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562875162; cv=none; d=zoho.com; s=zohoarc; b=Tomtl4iULdFbybLGfpQZFWfXlbXAYKUpxCgC+UX+kzUXvQvyH23doQnEQtaxWdFWpcQIps8LIKKUjrtluesvCKqVc4dxf0s1yCukdTdE0H+vE8XyBD4MYmRzYSMnm8/SNji3HEo09hGTnrpGsf7kEhz+WttRs75+y67phYP1uXE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562875162; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=8cjlN36+NOpJqWdP1ZcVAy4EIG3HGdhLEsWEpQfoBU0=; b=gAnLBHA7/oK6goNHtsQkTYX9OYjbxl0ikIew/WrlTxnS+q7fG0g5RCC5Nl5h2b7iUetn9p7BJ/yQtA68NXiajq6dnFDVthoxmwhDF17J8QVedO9gMJV3mCzvSJmDpX2nkuoQ6ShwJ/grxNrkXExOGK1yWz9wSnOLwaHsJTJqDEE= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562875162773575.1962628114537; Thu, 11 Jul 2019 12:59:22 -0700 (PDT) Received: from localhost ([::1]:44982 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlfDt-0006iy-8D for importer@patchew.org; Thu, 11 Jul 2019 15:59:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51449) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlfDP-0005Zp-CB for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hlfDC-00010B-Uv for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:17218) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hlfCt-0000mk-EJ; Thu, 11 Jul 2019 15:58:19 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3C5E3308213A; Thu, 11 Jul 2019 19:58:18 +0000 (UTC) Received: from localhost (unknown [10.40.205.198]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 928F360BE2; Thu, 11 Jul 2019 19:58:15 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Thu, 11 Jul 2019 21:58:01 +0200 Message-Id: <20190711195804.30703-3-mreitz@redhat.com> In-Reply-To: <20190711195804.30703-1-mreitz@redhat.com> References: <20190711195804.30703-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Thu, 11 Jul 2019 19:58:18 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC 2/5] block: Generic truncation fallback X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" If a protocol driver does not support truncation, we call fall back to effectively not doing anything if the new size is less than the actual file size. This is what we have been doing for some host device drivers already. The only caveat is that we have to zero out everything in the first sector that lies beyond the new "EOF" so we do not get any surprises with format probing. Signed-off-by: Max Reitz --- block/io.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/block/io.c b/block/io.c index 24a18759fd..382728fa9a 100644 --- a/block/io.c +++ b/block/io.c @@ -3064,6 +3064,57 @@ static void bdrv_parent_cb_resize(BlockDriverState *= bs) } } =20 +static int coroutine_fn bdrv_co_truncate_fallback(BdrvChild *child, + int64_t offset, + PreallocMode prealloc, + Error **errp) +{ + BlockDriverState *bs =3D child->bs; + int64_t cur_size =3D bdrv_getlength(bs); + + if (cur_size < 0) { + error_setg_errno(errp, -cur_size, + "Failed to inquire current file size"); + return cur_size; + } + + if (prealloc !=3D PREALLOC_MODE_OFF) { + error_setg(errp, "Unsupported preallocation mode: %s", + PreallocMode_str(prealloc)); + return -ENOTSUP; + } + + if (offset > cur_size) { + error_setg(errp, "Cannot grow this %s node", bs->drv->format_name); + return -ENOTSUP; + } + + /* + * Overwrite first "post-EOF" parts of the first sector with + * zeroes so raw images will not be misprobed + */ + if (offset < BDRV_SECTOR_SIZE && offset < cur_size) { + int64_t fill_len =3D MIN(BDRV_SECTOR_SIZE - offset, cur_size - off= set); + int ret; + + if (!(child->perm & BLK_PERM_WRITE)) { + error_setg(errp, "Cannot write to this node to clear the file = past " + "the truncated EOF"); + return -EPERM; + } + + ret =3D bdrv_co_pwrite_zeroes(child, offset, fill_len, 0); + if (ret < 0) { + error_setg_errno(errp, -ret, + "Failed to clear file past the truncated EOF"= ); + return ret; + } + } + + return 0; +} + + /** * Truncate file to 'offset' bytes (needed only for file protocols) */ @@ -3074,6 +3125,7 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, i= nt64_t offset, BlockDriver *drv =3D bs->drv; BdrvTrackedRequest req; int64_t old_size, new_bytes; + Error *local_err =3D NULL; int ret; =20 =20 @@ -3127,15 +3179,24 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child,= int64_t offset, ret =3D bdrv_co_truncate(bs->file, offset, prealloc, errp); goto out; } - error_setg(errp, "Image format driver does not support resize"); + error_setg(&local_err, "Image format driver does not support resiz= e"); ret =3D -ENOTSUP; - goto out; + } else { + ret =3D drv->bdrv_co_truncate(bs, offset, prealloc, &local_err); } =20 - ret =3D drv->bdrv_co_truncate(bs, offset, prealloc, errp); - if (ret < 0) { + if (ret =3D=3D -ENOTSUP && drv->bdrv_file_open) { + error_free(local_err); + + ret =3D bdrv_co_truncate_fallback(child, offset, prealloc, errp); + if (ret < 0) { + goto out; + } + } else if (ret < 0) { + error_propagate(errp, local_err); goto out; } + ret =3D refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); if (ret < 0) { error_setg_errno(errp, -ret, "Could not refresh total sector count= "); --=20 2.21.0 From nobody Fri Mar 29 10:32:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562875157; cv=none; d=zoho.com; s=zohoarc; b=XO8sTwtwOj7IAtylFnbCRFe63Aa0+o2Kis48ntFg+CGs6KFlw/O/38G6HTBF6OB1W356gmVvmJqdBkhATtuJXWA/M6q7A7+icBNGfd1MPYwDe/oRYTtEPU0tXhBMJ8TacVg+9GONFOE+eMjD/1A4KD3oflgXZBpNBIzT0pqUbrs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562875157; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=onYF98b0Sz14mWqEMwFJPGfwF7+eycZqx+L3i3eGW0w=; b=fBBG8v0jB6eQVUiFyMYjydxBJPj+GRNgOtjIYtprkI3Kf6pdLRh+7e9esqP0RTXVjuWzSjn5ya0JYP5wKM/uiBtzaKyho73OId0MP2gEkokP+wEMhh8LkhT6V39JOhqBkgHpuarMKvVTwPR7FRrMry+L9NJOVXInlkTwJoXuoA8= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562875157145498.1504438791213; Thu, 11 Jul 2019 12:59:17 -0700 (PDT) Received: from localhost ([::1]:44981 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlfDn-0006UT-NW for importer@patchew.org; Thu, 11 Jul 2019 15:59:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51458) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlfDP-0005Zq-F4 for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hlfDO-00015w-8m for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41366) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hlfCw-0000pD-HJ; Thu, 11 Jul 2019 15:58:25 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8D9FE308FFB1; Thu, 11 Jul 2019 19:58:20 +0000 (UTC) Received: from localhost (unknown [10.40.205.198]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 200CE600D1; Thu, 11 Jul 2019 19:58:19 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Thu, 11 Jul 2019 21:58:02 +0200 Message-Id: <20190711195804.30703-4-mreitz@redhat.com> In-Reply-To: <20190711195804.30703-1-mreitz@redhat.com> References: <20190711195804.30703-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Thu, 11 Jul 2019 19:58:20 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC 3/5] block: Fall back to fallback truncate function X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" file-posix does not need to basically duplicate our fallback truncate implementation; and sheepdog can fall back to it for "shrinking" files. Signed-off-by: Max Reitz --- block/file-posix.c | 21 +-------------------- block/sheepdog.c | 2 +- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index ab05b51a66..bcddfc7fbe 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2031,23 +2031,7 @@ static int coroutine_fn raw_co_truncate(BlockDriverS= tate *bs, int64_t offset, return raw_regular_truncate(bs, s->fd, offset, prealloc, errp); } =20 - if (prealloc !=3D PREALLOC_MODE_OFF) { - error_setg(errp, "Preallocation mode '%s' unsupported for this " - "non-regular file", PreallocMode_str(prealloc)); - return -ENOTSUP; - } - - if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { - if (offset > raw_getlength(bs)) { - error_setg(errp, "Cannot grow device files"); - return -EINVAL; - } - } else { - error_setg(errp, "Resizing this file is not supported"); - return -ENOTSUP; - } - - return 0; + return -ENOTSUP; } =20 #ifdef __OpenBSD__ @@ -3413,7 +3397,6 @@ static BlockDriver bdrv_host_device =3D { .bdrv_io_unplug =3D raw_aio_unplug, .bdrv_attach_aio_context =3D raw_aio_attach_aio_context, =20 - .bdrv_co_truncate =3D raw_co_truncate, .bdrv_getlength =3D raw_getlength, .bdrv_get_info =3D raw_get_info, .bdrv_get_allocated_file_size @@ -3537,7 +3520,6 @@ static BlockDriver bdrv_host_cdrom =3D { .bdrv_io_unplug =3D raw_aio_unplug, .bdrv_attach_aio_context =3D raw_aio_attach_aio_context, =20 - .bdrv_co_truncate =3D raw_co_truncate, .bdrv_getlength =3D raw_getlength, .has_variable_length =3D true, .bdrv_get_allocated_file_size @@ -3669,7 +3651,6 @@ static BlockDriver bdrv_host_cdrom =3D { .bdrv_io_unplug =3D raw_aio_unplug, .bdrv_attach_aio_context =3D raw_aio_attach_aio_context, =20 - .bdrv_co_truncate =3D raw_co_truncate, .bdrv_getlength =3D raw_getlength, .has_variable_length =3D true, .bdrv_get_allocated_file_size diff --git a/block/sheepdog.c b/block/sheepdog.c index 6f402e5d4d..4af4961cb7 100644 --- a/block/sheepdog.c +++ b/block/sheepdog.c @@ -2301,7 +2301,7 @@ static int coroutine_fn sd_co_truncate(BlockDriverSta= te *bs, int64_t offset, max_vdi_size =3D (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA= _OBJS; if (offset < old_size) { error_setg(errp, "shrinking is not supported"); - return -EINVAL; + return -ENOTSUP; } else if (offset > max_vdi_size) { error_setg(errp, "too big image size"); return -EINVAL; --=20 2.21.0 From nobody Fri Mar 29 10:32:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562875169; cv=none; d=zoho.com; s=zohoarc; b=cxD2fBtJJlBIvaCGv291lbxVqoJU06M4idSIyGgCAbwFPyxv/Oj2fgXWrxWpBiZKP6qY8dI+M8JOnt4YyB/iSs54TJPJ5M6eNjJPMaBMsuk8kjBDO/1Xw+/uGcB+60mbt4mfxhX58ttozrYVO4weWeFI3xTVIIyqgGgwH+OC50Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562875169; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=efP9Om4Z5BgRW8GjykOeVJ5Qn4xwUlPGfuVxImAxgmo=; b=gToZxhf3OU8pOn/+Sjw9Newf8Q9QhSibqSn43dMDP7pGZUqVpx6KGggrqP0g7xxVdL34Gc80peF3bksKtQmwHSmNnhU7Pm75vCPNBQWrfrXKnrTb13EZkFmspFW+1igYVHtveco2B7D9KrazlLIfkNeB6DVfD2oeskpDZRyd7CE= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562875169224574.6313893916484; Thu, 11 Jul 2019 12:59:29 -0700 (PDT) Received: from localhost ([::1]:44984 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlfDz-0007J9-Po for importer@patchew.org; Thu, 11 Jul 2019 15:59:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51752) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlfDW-0005sd-MF for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hlfDU-0001JV-Nc for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:58 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57714) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hlfDQ-0000rh-Dx; Thu, 11 Jul 2019 15:58:52 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6DB32308213A; Thu, 11 Jul 2019 19:58:24 +0000 (UTC) Received: from localhost (unknown [10.40.205.198]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6D77B60BE2; Thu, 11 Jul 2019 19:58:22 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Thu, 11 Jul 2019 21:58:03 +0200 Message-Id: <20190711195804.30703-5-mreitz@redhat.com> In-Reply-To: <20190711195804.30703-1-mreitz@redhat.com> References: <20190711195804.30703-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Thu, 11 Jul 2019 19:58:24 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC 4/5] block: Generic file creation fallback X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" If a protocol driver does not support image creation, we can see whether maybe the file exists already. If so, just truncating it will be sufficient. Signed-off-by: Max Reitz --- block.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/block.c b/block.c index c139540f2b..8fb8e4dfda 100644 --- a/block.c +++ b/block.c @@ -531,20 +531,57 @@ out: return ret; } =20 -int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) +static int bdrv_create_file_fallback(const char *filename, BlockDriver *dr= v, + QemuOpts *opts, Error **errp) { - BlockDriver *drv; + BlockBackend *blk; + QDict *options =3D qdict_new(); + int64_t size =3D 0; + char *buf =3D NULL; + PreallocMode prealloc; Error *local_err =3D NULL; int ret; =20 + size =3D qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + buf =3D qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); + prealloc =3D qapi_enum_parse(&PreallocMode_lookup, buf, + PREALLOC_MODE_OFF, &local_err); + g_free(buf); + if (local_err) { + error_propagate(errp, local_err); + return -EINVAL; + } + + qdict_put_str(options, "driver", drv->format_name); + + blk =3D blk_new_open(filename, NULL, options, + BDRV_O_RDWR | BDRV_O_RESIZE, errp); + if (!blk) { + error_prepend(errp, "Protocol driver '%s' does not support " + "image creation, and opening the image failed: ", + drv->format_name); + return -EINVAL; + } + + ret =3D blk_truncate(blk, size, prealloc, errp); + blk_unref(blk); + return ret; +} + +int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) +{ + BlockDriver *drv; + drv =3D bdrv_find_protocol(filename, true, errp); if (drv =3D=3D NULL) { return -ENOENT; } =20 - ret =3D bdrv_create(drv, filename, opts, &local_err); - error_propagate(errp, local_err); - return ret; + if (drv->bdrv_co_create_opts) { + return bdrv_create(drv, filename, opts, errp); + } else { + return bdrv_create_file_fallback(filename, drv, opts, errp); + } } =20 /** @@ -1420,6 +1457,24 @@ QemuOptsList bdrv_runtime_opts =3D { }, }; =20 +static QemuOptsList fallback_create_opts =3D { + .name =3D "fallback-create-opts", + .head =3D QTAILQ_HEAD_INITIALIZER(fallback_create_opts.head), + .desc =3D { + { + .name =3D BLOCK_OPT_SIZE, + .type =3D QEMU_OPT_SIZE, + .help =3D "Virtual disk size" + }, + { + .name =3D BLOCK_OPT_PREALLOC, + .type =3D QEMU_OPT_STRING, + .help =3D "Preallocation mode (allowed values: off)" + }, + { /* end of list */ } + } +}; + /* * Common part for opening disk images and files * @@ -5681,14 +5736,12 @@ void bdrv_img_create(const char *filename, const ch= ar *fmt, return; } =20 - if (!proto_drv->create_opts) { - error_setg(errp, "Protocol driver '%s' does not support image crea= tion", - proto_drv->format_name); - return; - } - create_opts =3D qemu_opts_append(create_opts, drv->create_opts); - create_opts =3D qemu_opts_append(create_opts, proto_drv->create_opts); + if (proto_drv->create_opts) { + create_opts =3D qemu_opts_append(create_opts, proto_drv->create_op= ts); + } else { + create_opts =3D qemu_opts_append(create_opts, &fallback_create_opt= s); + } =20 /* Create parameter list with default values */ opts =3D qemu_opts_create(create_opts, NULL, 0, &error_abort); --=20 2.21.0 From nobody Fri Mar 29 10:32:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562875187; cv=none; d=zoho.com; s=zohoarc; b=CiXOfTI1JtSaHoY6iiVT8pwg/4chtp6CTHj14B3th76TuvzlOtipLjkw7Xil3gu0evD5s6JnREBErjOUcLhtaucoaoDu2gotxrrUZrcQm8hKrcIk6NnrcVAlYmxUVivrqEZ7Pvf2fj8n3Vo5TowtsHd4uachLPoxzzJdArRQw14= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562875187; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=Qi0apO2YK/gb4buFjmwAAXxPSx72iRa1P+cYG+p53jM=; b=cBDS/QokKg7W9edLmMQwiOOPE7JUWWLG3CYNGcc2+WWhuOD3CFWGAGk45YZOJc3EGNw/pRTWZK4I8T16F9ke3HUWZ9iL8G9JQnqAhPw1k68c3BeLt+roVAlERkRPe9gDzZdf8Xgcy+BxoAGsDYxTkIzaZZyVmoyTRNAiffyTcn4= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562875187585151.04675111387166; Thu, 11 Jul 2019 12:59:47 -0700 (PDT) Received: from localhost ([::1]:44996 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlfEI-0000ON-Hr for importer@patchew.org; Thu, 11 Jul 2019 15:59:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51736) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlfDV-0005qF-TL for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hlfDS-0001FK-Tz for qemu-devel@nongnu.org; Thu, 11 Jul 2019 15:58:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58386) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hlfDP-0000sz-9d; Thu, 11 Jul 2019 15:58:51 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B65AC356C4; Thu, 11 Jul 2019 19:58:26 +0000 (UTC) Received: from localhost (unknown [10.40.205.198]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4EF3360BE2; Thu, 11 Jul 2019 19:58:26 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Thu, 11 Jul 2019 21:58:04 +0200 Message-Id: <20190711195804.30703-6-mreitz@redhat.com> In-Reply-To: <20190711195804.30703-1-mreitz@redhat.com> References: <20190711195804.30703-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 11 Jul 2019 19:58:26 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC 5/5] iotests: Add test for fallback truncate/create X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Signed-off-by: Max Reitz --- tests/qemu-iotests/259 | 71 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/259.out | 20 +++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 92 insertions(+) create mode 100755 tests/qemu-iotests/259 create mode 100644 tests/qemu-iotests/259.out diff --git a/tests/qemu-iotests/259 b/tests/qemu-iotests/259 new file mode 100755 index 0000000000..6e3941378f --- /dev/null +++ b/tests/qemu-iotests/259 @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +# +# Test generic image creation and truncation fallback (by using NBD) +# +# Copyright (C) 2019 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# creator +owner=3Dmreitz@redhat.com + +seq=3D$(basename $0) +echo "QA output created by $seq" + +status=3D1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +_supported_fmt raw +_supported_proto nbd +_supported_os Linux + + +_make_test_img 64M + +echo +echo '--- Testing no-op shrinking ---' + +$QEMU_IMG resize -f raw --shrink "$TEST_IMG" 32M + +echo +echo '--- Testing non-working growing ---' + +$QEMU_IMG resize -f raw "$TEST_IMG" 128M + +echo +echo '--- Testing creation ---' + +$QEMU_IMG create -f qcow2 "$TEST_IMG" 64M | _filter_img_create +$QEMU_IMG info "$TEST_IMG" | _filter_img_info + +echo +echo '--- Testing creation for which the node would need to grow ---' + +$QEMU_IMG create -f qcow2 -o preallocation=3Dmetadata "$TEST_IMG" 64M 2>&1= \ + | _filter_img_create + +# success, all done +echo "*** done" +rm -f $seq.full +status=3D0 diff --git a/tests/qemu-iotests/259.out b/tests/qemu-iotests/259.out new file mode 100644 index 0000000000..1e4b11055f --- /dev/null +++ b/tests/qemu-iotests/259.out @@ -0,0 +1,20 @@ +QA output created by 259 +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D67108864 + +--- Testing no-op shrinking --- +qemu-img: Image was not resized; resizing may not be supported for this im= age + +--- Testing non-working growing --- +qemu-img: Cannot grow this nbd node + +--- Testing creation --- +Formatting 'TEST_DIR/t.IMGFMT', fmt=3Dqcow2 size=3D67108864 +image: TEST_DIR/t.IMGFMT +file format: qcow2 +virtual size: 64 MiB (67108864 bytes) +disk size: unavailable + +--- Testing creation for which the node would need to grow --- +qemu-img: TEST_DIR/t.IMGFMT: Could not resize image: Cannot grow this nbd = node +Formatting 'TEST_DIR/t.IMGFMT', fmt=3Dqcow2 size=3D67108864 preallocation= =3Dmetadata +*** done diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index b34c8e3c0c..80e7603174 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -269,3 +269,4 @@ 254 rw auto backing quick 255 rw auto quick 256 rw auto quick +259 rw auto quick --=20 2.21.0