From patchwork Mon Jan 11 17:51:37 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 42650 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 45745B7BC3 for ; Tue, 12 Jan 2010 05:08:43 +1100 (EST) Received: from localhost ([127.0.0.1]:54450 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NUOdd-0008P1-7O for incoming@patchwork.ozlabs.org; Mon, 11 Jan 2010 13:05:13 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NUOQb-0004u1-Kg for qemu-devel@nongnu.org; Mon, 11 Jan 2010 12:51:45 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NUOQW-0004oM-L4 for qemu-devel@nongnu.org; Mon, 11 Jan 2010 12:51:45 -0500 Received: from [199.232.76.173] (port=60986 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NUOQW-0004o9-E0 for qemu-devel@nongnu.org; Mon, 11 Jan 2010 12:51:40 -0500 Received: from verein.lst.de ([213.95.11.210]:42671) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_3DES_EDE_CBC_SHA1:24) (Exim 4.60) (envelope-from ) id 1NUOQV-0000aF-IP for qemu-devel@nongnu.org; Mon, 11 Jan 2010 12:51:40 -0500 Received: from verein.lst.de (localhost [127.0.0.1]) by verein.lst.de (8.12.3/8.12.3/Debian-7.1) with ESMTP id o0BHpbWY007593 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Mon, 11 Jan 2010 18:51:37 +0100 Received: (from hch@localhost) by verein.lst.de (8.12.3/8.12.3/Debian-7.2) id o0BHpbOs007592 for qemu-devel@nongnu.org; Mon, 11 Jan 2010 18:51:37 +0100 Date: Mon, 11 Jan 2010 18:51:37 +0100 From: Christoph Hellwig To: qemu-devel@nongnu.org Message-ID: <20100111175137.GA7571@lst.de> Mime-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.3.28i X-Spam-Score: 0 () X-Scanned-By: MIMEDefang 2.39 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) Subject: [Qemu-devel] [PATCH 1/2] block: clean up bdrv_open2 structure a bit X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Check the whitelist as early as possible instead of continuing the setup, and move all the error handling code to the end of the function. Signed-off-by: Christoph Hellwig Acked-by: Kevin Wolf Index: qemu/block.c =================================================================== --- qemu.orig/block.c 2010-01-11 17:48:27.811004054 +0100 +++ qemu/block.c 2010-01-11 17:52:15.578006158 +0100 @@ -428,10 +428,16 @@ int bdrv_open2(BlockDriverState *bs, con drv = find_image_format(filename); } } + if (!drv) { ret = -ENOENT; goto unlink_and_fail; } + if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { + ret = -ENOTSUP; + goto unlink_and_fail; + } + bs->drv = drv; bs->opaque = qemu_mallocz(drv->instance_size); @@ -452,23 +458,17 @@ int bdrv_open2(BlockDriverState *bs, con (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO)); else open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); - if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) - ret = -ENOTSUP; - else - ret = drv->bdrv_open(bs, filename, open_flags); + + ret = drv->bdrv_open(bs, filename, open_flags); if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); bs->read_only = 1; } + if (ret < 0) { - qemu_free(bs->opaque); - bs->opaque = NULL; - bs->drv = NULL; - unlink_and_fail: - if (bs->is_temporary) - unlink(filename); - return ret; + goto free_and_fail; } + if (drv->bdrv_getlength) { bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; } @@ -502,6 +502,15 @@ int bdrv_open2(BlockDriverState *bs, con bs->change_cb(bs->change_opaque); } return 0; + +free_and_fail: + qemu_free(bs->opaque); + bs->opaque = NULL; + bs->drv = NULL; +unlink_and_fail: + if (bs->is_temporary) + unlink(filename); + return ret; } void bdrv_close(BlockDriverState *bs)