From patchwork Mon Jan 11 17:52:09 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 42649 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 870A9B7BF2 for ; Tue, 12 Jan 2010 05:06:27 +1100 (EST) Received: from localhost ([127.0.0.1]:46364 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NUOem-0001Fa-QU for incoming@patchwork.ozlabs.org; Mon, 11 Jan 2010 13:06:24 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NUOR6-0005Zy-Oe for qemu-devel@nongnu.org; Mon, 11 Jan 2010 12:52:16 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NUOR1-0005TD-W4 for qemu-devel@nongnu.org; Mon, 11 Jan 2010 12:52:16 -0500 Received: from [199.232.76.173] (port=60993 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NUOR1-0005T2-Rl for qemu-devel@nongnu.org; Mon, 11 Jan 2010 12:52:11 -0500 Received: from verein.lst.de ([213.95.11.210]:57075) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_3DES_EDE_CBC_SHA1:24) (Exim 4.60) (envelope-from ) id 1NUOR1-0000jG-5s for qemu-devel@nongnu.org; Mon, 11 Jan 2010 12:52:11 -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 o0BHq9WY007620 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Mon, 11 Jan 2010 18:52:09 +0100 Received: (from hch@localhost) by verein.lst.de (8.12.3/8.12.3/Debian-7.2) id o0BHq9un007619 for qemu-devel@nongnu.org; Mon, 11 Jan 2010 18:52:09 +0100 Date: Mon, 11 Jan 2010 18:52:09 +0100 From: Christoph Hellwig To: qemu-devel@nongnu.org Message-ID: <20100111175209.GB7571@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 2/2] block: untangle open flag manipulation in bdrv_open2 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 Untangle the open flag manipulation in bdrv_open2 and document why we are clearing the various flags in the different flag combinations. Signed-off-by: Christoph Hellwig Acked-by: Kevin Wolf Index: qemu/block.c =================================================================== --- qemu.orig/block.c 2010-01-11 17:52:54.273003789 +0100 +++ qemu/block.c 2010-01-11 18:19:22.485006278 +0100 @@ -356,7 +356,7 @@ int bdrv_open(BlockDriverState *bs, cons int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, BlockDriver *drv) { - int ret, open_flags, try_rw; + int ret, open_flags; char tmp_filename[PATH_MAX]; char backing_filename[PATH_MAX]; @@ -450,19 +450,39 @@ int bdrv_open2(BlockDriverState *bs, con if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) bs->enable_write_cache = 1; - /* Note: for compatibility, we open disk image files as RDWR, and - RDONLY as fallback */ - try_rw = !bs->read_only || bs->is_temporary; - if (!(flags & BDRV_O_FILE)) - open_flags = (try_rw ? BDRV_O_RDWR : 0) | - (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO)); - else - open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); + /* + * Always clear these flags as they are not destined for the drivers. + */ + open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); + + if (!(flags & BDRV_O_FILE)) { + /* + * For historical reasons we always try to open drive images as + * read-write first and only fall back to read-only later. + * + * This can be overriden using readonly suboption for the + * drive option. + */ + if (bs->read_only && !bs->is_temporary) { + open_flags &= ~BDRV_O_RDWR; + } else { + open_flags |= BDRV_O_RDWR; + } - 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; + /* + * Currently BDRV_O_CREAT is not supported by any image format, + * but I'm not sure that's reason enough to always clear it for + * the !BDRV_O_FILE case.. + */ + open_flags &= ~(BDRV_O_CREAT); + + ret = drv->bdrv_open(bs, filename, open_flags); + if (ret == -EACCES || ret == -EPERM) { + ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); + bs->read_only = 1; + } + } else { + ret = drv->bdrv_open(bs, filename, open_flags); } if (ret < 0) {