From patchwork Fri Sep 18 15:22:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 519397 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 7106A140E54 for ; Sat, 19 Sep 2015 01:38:07 +1000 (AEST) Received: from localhost ([::1]:39753 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zcxjh-0004U9-3O for incoming@patchwork.ozlabs.org; Fri, 18 Sep 2015 11:38:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38355) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZcxWL-0007CY-5p for qemu-devel@nongnu.org; Fri, 18 Sep 2015 11:24:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZcxWK-00063Q-6A for qemu-devel@nongnu.org; Fri, 18 Sep 2015 11:24:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41348) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZcxWE-00061g-VD; Fri, 18 Sep 2015 11:24:11 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 9AFFE83F9B; Fri, 18 Sep 2015 15:24:10 +0000 (UTC) Received: from localhost (ovpn-116-139.ams2.redhat.com [10.36.116.139]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t8IFO8Ii023912 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 18 Sep 2015 11:24:09 -0400 From: Max Reitz To: qemu-block@nongnu.org Date: Fri, 18 Sep 2015 17:22:54 +0200 Message-Id: <1442589793-7105-20-git-send-email-mreitz@redhat.com> In-Reply-To: <1442589793-7105-1-git-send-email-mreitz@redhat.com> References: <1442589793-7105-1-git-send-email-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Alberto Garcia , Markus Armbruster , qemu-devel@nongnu.org, Max Reitz , John Snow , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v5 19/38] block: Fail requests to empty BlockBackend 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 If there is no BlockDriverState in a BlockBackend or if the tray of the guest device is open, fail all requests (where that is possible) with -ENOMEDIUM. The reason the status of the guest device is taken into account is because once the guest device's tray is opened, any request on the same BlockBackend as the guest uses should fail. If the BDS tree is supposed to be usable even after ejecting it from the guest, a different BlockBackend must be used. Signed-off-by: Max Reitz Reviewed-by: Eric Blake --- block/block-backend.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/block/block-backend.c b/block/block-backend.c index 417374f..0da4be9 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -509,7 +509,7 @@ static int blk_check_byte_request(BlockBackend *blk, int64_t offset, return -EIO; } - if (!blk_is_inserted(blk)) { + if (!blk_is_available(blk)) { return -ENOMEDIUM; } @@ -648,6 +648,10 @@ int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count) int64_t blk_getlength(BlockBackend *blk) { + if (!blk_is_available(blk)) { + return -ENOMEDIUM; + } + return bdrv_getlength(blk->bs); } @@ -658,6 +662,10 @@ void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr) int64_t blk_nb_sectors(BlockBackend *blk) { + if (!blk_is_available(blk)) { + return -ENOMEDIUM; + } + return bdrv_nb_sectors(blk->bs); } @@ -688,6 +696,10 @@ BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num, BlockAIOCB *blk_aio_flush(BlockBackend *blk, BlockCompletionFunc *cb, void *opaque) { + if (!blk_is_available(blk)) { + return abort_aio_request(blk, cb, opaque, -ENOMEDIUM); + } + return bdrv_aio_flush(blk->bs, cb, opaque); } @@ -729,12 +741,20 @@ int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs) int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf) { + if (!blk_is_available(blk)) { + return -ENOMEDIUM; + } + return bdrv_ioctl(blk->bs, req, buf); } BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf, BlockCompletionFunc *cb, void *opaque) { + if (!blk_is_available(blk)) { + return abort_aio_request(blk, cb, opaque, -ENOMEDIUM); + } + return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque); } @@ -750,11 +770,19 @@ int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) int blk_co_flush(BlockBackend *blk) { + if (!blk_is_available(blk)) { + return -ENOMEDIUM; + } + return bdrv_co_flush(blk->bs); } int blk_flush(BlockBackend *blk) { + if (!blk_is_available(blk)) { + return -ENOMEDIUM; + } + return bdrv_flush(blk->bs); } @@ -876,6 +904,11 @@ void blk_set_enable_write_cache(BlockBackend *blk, bool wce) void blk_invalidate_cache(BlockBackend *blk, Error **errp) { + if (!blk->bs) { + error_setg(errp, "Device '%s' has no medium", blk->name); + return; + } + bdrv_invalidate_cache(blk->bs, errp); } @@ -1031,6 +1064,10 @@ int blk_write_compressed(BlockBackend *blk, int64_t sector_num, int blk_truncate(BlockBackend *blk, int64_t offset) { + if (!blk_is_available(blk)) { + return -ENOMEDIUM; + } + return bdrv_truncate(blk->bs, offset); } @@ -1047,21 +1084,37 @@ int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf, int64_t pos, int size) { + if (!blk_is_available(blk)) { + return -ENOMEDIUM; + } + return bdrv_save_vmstate(blk->bs, buf, pos, size); } int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size) { + if (!blk_is_available(blk)) { + return -ENOMEDIUM; + } + return bdrv_load_vmstate(blk->bs, buf, pos, size); } int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz) { + if (!blk_is_available(blk)) { + return -ENOMEDIUM; + } + return bdrv_probe_blocksizes(blk->bs, bsz); } int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo) { + if (!blk_is_available(blk)) { + return -ENOMEDIUM; + } + return bdrv_probe_geometry(blk->bs, geo); }