From patchwork Wed Feb 1 03:07:12 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Supriya Kannery X-Patchwork-Id: 138898 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8C220B6EF7 for ; Wed, 1 Feb 2012 14:20:27 +1100 (EST) Received: from localhost ([::1]:56337 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsQkD-0003d5-6z for incoming@patchwork.ozlabs.org; Tue, 31 Jan 2012 22:20:25 -0500 Received: from eggs.gnu.org ([140.186.70.92]:55617) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsQYj-0001cb-AH for qemu-devel@nongnu.org; Tue, 31 Jan 2012 22:08:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RsQYd-0006fL-GB for qemu-devel@nongnu.org; Tue, 31 Jan 2012 22:08:33 -0500 Received: from e1.ny.us.ibm.com ([32.97.182.141]:38210) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsQYd-0006eH-DQ for qemu-devel@nongnu.org; Tue, 31 Jan 2012 22:08:27 -0500 Received: from /spool/local by e1.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 31 Jan 2012 22:08:11 -0500 Received: from d01dlp01.pok.ibm.com (9.56.224.56) by e1.ny.us.ibm.com (192.168.1.101) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 31 Jan 2012 22:07:18 -0500 Received: from d01relay04.pok.ibm.com (d01relay04.pok.ibm.com [9.56.227.236]) by d01dlp01.pok.ibm.com (Postfix) with ESMTP id 4464338C8054 for ; Tue, 31 Jan 2012 22:07:18 -0500 (EST) Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by d01relay04.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q1137H1S297684 for ; Tue, 31 Jan 2012 22:07:18 -0500 Received: from d03av04.boulder.ibm.com (loopback [127.0.0.1]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q1137GWM016859 for ; Tue, 31 Jan 2012 20:07:17 -0700 Received: from skannery.in.ibm.com ([9.77.213.207]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q1137CDA016594; Tue, 31 Jan 2012 20:07:13 -0700 From: Supriya Kannery To: qemu-devel@nongnu.org Date: Wed, 01 Feb 2012 08:37:12 +0530 Message-Id: <20120201030712.2990.41527.sendpatchset@skannery.in.ibm.com> In-Reply-To: <20120201030557.2990.74150.sendpatchset@skannery.in.ibm.com> References: <20120201030557.2990.74150.sendpatchset@skannery.in.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12020103-6078-0000-0000-00000751A2A4 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.182.141 Cc: Kevin Wolf , Stefan Hajnoczi , Christoph Hellwig , Luiz Capitulino Subject: [Qemu-devel] [RFC Patch 5/7]Qemu: raw-posix image file reopen 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 raw-posix driver changes for bdrv_reopen_xx functions to safely reopen image files. Reopening of image files while changing hostcache dynamically is handled here. Signed-off-by: Supriya Kannery Index: qemu/block/raw.c =================================================================== --- qemu.orig/block/raw.c +++ qemu/block/raw.c @@ -9,6 +9,22 @@ static int raw_open(BlockDriverState *bs return 0; } +static int raw_reopen_prepare(BlockDriverState *bs, BDRVReopenState **prs, + int flags) +{ + return bdrv_reopen_prepare(bs->file, prs, flags); +} + +static void raw_reopen_commit(BlockDriverState *bs, BDRVReopenState *rs) +{ + bdrv_reopen_commit(bs->file, rs); +} + +static void raw_reopen_abort(BlockDriverState *bs, BDRVReopenState *rs) +{ + bdrv_reopen_abort(bs->file, rs); +} + static int coroutine_fn raw_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) { @@ -109,6 +125,10 @@ static BlockDriver bdrv_raw = { .instance_size = 1, .bdrv_open = raw_open, + .bdrv_reopen_prepare + = raw_reopen_prepare, + .bdrv_reopen_commit = raw_reopen_commit, + .bdrv_reopen_abort = raw_reopen_abort, .bdrv_close = raw_close, .bdrv_co_readv = raw_co_readv, Index: qemu/block/raw-posix.c =================================================================== --- qemu.orig/block/raw-posix.c +++ qemu/block/raw-posix.c @@ -136,6 +136,11 @@ typedef struct BDRVRawState { #endif } BDRVRawState; +typedef struct BDRVRawReopenState { + BDRVReopenState reopen_state; + BDRVRawState *stash_s; +} BDRVRawReopenState; + static int fd_open(BlockDriverState *bs); static int64_t raw_getlength(BlockDriverState *bs); @@ -279,6 +284,71 @@ static int raw_open(BlockDriverState *bs return raw_open_common(bs, filename, flags, 0); } +static int raw_reopen_prepare(BlockDriverState *bs, BDRVReopenState **prs, + int flags) +{ + BDRVRawReopenState *raw_rs = g_malloc0(sizeof(BDRVRawReopenState)); + BDRVRawState *s = bs->opaque; + int ret = 0; + + raw_rs->reopen_state.bs = bs; + + /* stash state before reopen */ + raw_rs->stash_s = g_malloc0(sizeof(BDRVRawState)); + memcpy(raw_rs->stash_s, s, sizeof(BDRVRawState)); + s->fd = dup(raw_rs->stash_s->fd); + + *prs = &(raw_rs->reopen_state); + + /* Flags that can be set using fcntl */ + int fcntl_flags = BDRV_O_NOCACHE; + + if ((bs->open_flags & ~fcntl_flags) == (flags & ~fcntl_flags)) { + if ((flags & BDRV_O_NOCACHE)) { + s->open_flags |= O_DIRECT; + } else { + s->open_flags &= ~O_DIRECT; + } + printf("O_DIRECT flag\n"); + ret = fcntl_setfl(s->fd, s->open_flags); + } else { + + printf("close and open with new flags\n"); + /* close and reopen using new flags */ + bs->drv->bdrv_close(bs); + ret = bs->drv->bdrv_file_open(bs, bs->filename, flags); + } + return ret; +} + +static void raw_reopen_commit(BlockDriverState *bs, BDRVReopenState *rs) +{ + BDRVRawReopenState *raw_rs; + + raw_rs = container_of(rs, BDRVRawReopenState, reopen_state); + + /* clean up stashed state */ + close(raw_rs->stash_s->fd); + g_free(raw_rs->stash_s); + g_free(raw_rs); +} + +static void raw_reopen_abort(BlockDriverState *bs, BDRVReopenState *rs) +{ + BDRVRawReopenState *raw_rs; + BDRVRawState *s = bs->opaque; + + raw_rs = container_of(rs, BDRVRawReopenState, reopen_state); + + /* revert to stashed state */ + if (s->fd != -1) { + close(s->fd); + } + memcpy(s, raw_rs->stash_s, sizeof(BDRVRawState)); + g_free(raw_rs->stash_s); + g_free(raw_rs); +} + /* XXX: use host sector size if necessary with: #ifdef DIOCGSECTORSIZE { @@ -631,6 +701,9 @@ static BlockDriver bdrv_file = { .instance_size = sizeof(BDRVRawState), .bdrv_probe = NULL, /* no probe for protocols */ .bdrv_file_open = raw_open, + .bdrv_reopen_prepare = raw_reopen_prepare, + .bdrv_reopen_commit = raw_reopen_commit, + .bdrv_reopen_abort = raw_reopen_abort, .bdrv_close = raw_close, .bdrv_create = raw_create, .bdrv_co_discard = raw_co_discard,