From patchwork Mon Jul 30 21:34:37 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Supriya Kannery X-Patchwork-Id: 174099 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 6593D2C008F for ; Tue, 31 Jul 2012 07:36:19 +1000 (EST) Received: from localhost ([::1]:35291 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SvxdR-0001JF-G6 for incoming@patchwork.ozlabs.org; Mon, 30 Jul 2012 17:36:17 -0400 Received: from eggs.gnu.org ([208.118.235.92]:37813) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SvxdF-0001Dm-FZ for qemu-devel@nongnu.org; Mon, 30 Jul 2012 17:36:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SvxdA-0001xK-OB for qemu-devel@nongnu.org; Mon, 30 Jul 2012 17:36:05 -0400 Received: from e3.ny.us.ibm.com ([32.97.182.143]:44627) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SvxdA-0001x6-Iy for qemu-devel@nongnu.org; Mon, 30 Jul 2012 17:36:00 -0400 Received: from /spool/local by e3.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 30 Jul 2012 17:36:00 -0400 Received: from d01dlp03.pok.ibm.com (9.56.224.17) by e3.ny.us.ibm.com (192.168.1.103) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 30 Jul 2012 17:35:58 -0400 Received: from d01relay03.pok.ibm.com (d01relay03.pok.ibm.com [9.56.227.235]) by d01dlp03.pok.ibm.com (Postfix) with ESMTP id 0AC1EC9002F for ; Mon, 30 Jul 2012 17:35:58 -0400 (EDT) Received: from d01av04.pok.ibm.com (d01av04.pok.ibm.com [9.56.224.64]) by d01relay03.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q6ULYfDq255292 for ; Mon, 30 Jul 2012 17:34:41 -0400 Received: from d01av04.pok.ibm.com (loopback [127.0.0.1]) by d01av04.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q6ULYfVQ017378 for ; Mon, 30 Jul 2012 17:34:41 -0400 Received: from skannery.in.ibm.com ([9.79.207.23]) by d01av04.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q6ULYbgZ017155; Mon, 30 Jul 2012 17:34:38 -0400 From: Supriya Kannery To: qemu-devel@nongnu.org Date: Tue, 31 Jul 2012 03:04:37 +0530 Message-Id: <20120730213437.21536.87232.sendpatchset@skannery.in.ibm.com> In-Reply-To: <20120730213409.21536.7589.sendpatchset@skannery.in.ibm.com> References: <20120730213409.21536.7589.sendpatchset@skannery.in.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12073021-8974-0000-0000-00000BA86EBE X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.182.143 Cc: Kevin Wolf , Shrinidhi Joshi , Stefan Hajnoczi , Jeff Cody , Luiz Capitulino , Christoph Hellwig Subject: [Qemu-devel] [v2 Patch 2/9]block: 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) { @@ -113,6 +129,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 @@ -140,8 +140,15 @@ 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); +static void raw_stash_state(BDRVRawState *stashed_state, BDRVRawState *s); +static void raw_revert_state(BDRVRawState *s, BDRVRawState *stashed_state); #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) static int cdrom_reopen(BlockDriverState *bs); @@ -283,6 +290,117 @@ 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)); + raw_stash_state(raw_rs->stash_s, s); + s->fd = dup3(raw_rs->stash_s->fd, s->fd, O_CLOEXEC); + + *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; + } + ret = fcntl_setfl(s->fd, s->open_flags); + } else { + + /* 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); + } + raw_revert_state(s, raw_rs->stash_s); + g_free(raw_rs->stash_s); + g_free(raw_rs); +} + +static void raw_stash_state(BDRVRawState *stashed_s, BDRVRawState *s) +{ + stashed_s->fd = -1; + stashed_s->type = s->type; + stashed_s->open_flags = s->open_flags; +#if defined(__linux__) + /* linux floppy specific */ + stashed_s->fd_open_time = s->fd_open_time; + stashed_s->fd_error_time = s->fd_error_time; + stashed_s->fd_got_error = s->fd_got_error; + stashed_s->fd_media_changed = s->fd_media_changed; +#endif +#ifdef CONFIG_LINUX_AIO + stashed_s->use_aio = s->use_aio; + stashed_s->aio_ctx = s->aio_ctx; +#endif + stashed_s->aligned_buf = s->aligned_buf; + stashed_s->aligned_buf_size = s->aligned_buf_size; +#ifdef CONFIG_XFS + stashed_s->is_xfs = s->is_xfs; +#endif + +} + +static void raw_revert_state(BDRVRawState *s, BDRVRawState *stashed_s) +{ + + s->fd = stashed_s->fd; + s->type = stashed_s->type; + s->open_flags = stashed_s->open_flags; +#if defined(__linux__) + /* linux floppy specific */ + s->fd_open_time = stashed_s->fd_open_time; + s->fd_error_time = stashed_s->fd_error_time; + s->fd_got_error = stashed_s->fd_got_error; + s->fd_media_changed = stashed_s->fd_media_changed; +#endif +#ifdef CONFIG_LINUX_AIO + s->use_aio = stashed_s->use_aio; + s->aio_ctx = stashed_s->aio_ctx; +#endif + s->aligned_buf = stashed_s->aligned_buf; + s->aligned_buf_size = stashed_s->aligned_buf_size; +#ifdef CONFIG_XFS + s->is_xfs = stashed_s->is_xfs; +#endif +} + /* XXX: use host sector size if necessary with: #ifdef DIOCGSECTORSIZE { @@ -735,6 +853,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,