From patchwork Wed Feb 1 03:07:26 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Supriya Kannery X-Patchwork-Id: 138895 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 15BD0B6EF3 for ; Wed, 1 Feb 2012 14:08:12 +1100 (EST) Received: from localhost ([::1]:55539 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsQYI-0000da-2o for incoming@patchwork.ozlabs.org; Tue, 31 Jan 2012 22:08:06 -0500 Received: from eggs.gnu.org ([140.186.70.92]:55548) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsQY9-0000cS-06 for qemu-devel@nongnu.org; Tue, 31 Jan 2012 22:07:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RsQY3-0006bZ-3p for qemu-devel@nongnu.org; Tue, 31 Jan 2012 22:07:53 -0500 Received: from e35.co.us.ibm.com ([32.97.110.153]:32786) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsQY2-0006ZJ-UL for qemu-devel@nongnu.org; Tue, 31 Jan 2012 22:07:51 -0500 Received: from /spool/local by e35.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 31 Jan 2012 20:07:34 -0700 Received: from d01dlp03.pok.ibm.com (9.56.224.17) by e35.co.us.ibm.com (192.168.1.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 31 Jan 2012 20:07:31 -0700 Received: from d01relay04.pok.ibm.com (d01relay04.pok.ibm.com [9.56.227.236]) by d01dlp03.pok.ibm.com (Postfix) with ESMTP id 15453C9004B for ; Tue, 31 Jan 2012 22:07:31 -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 q1137Ui2286982 for ; Tue, 31 Jan 2012 22:07:30 -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 q1137TX4017957 for ; Tue, 31 Jan 2012 20:07:30 -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 q1137Qo8017492; Tue, 31 Jan 2012 20:07:27 -0700 From: Supriya Kannery To: qemu-devel@nongnu.org Date: Wed, 01 Feb 2012 08:37:26 +0530 Message-Id: <20120201030726.2990.68290.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-6148-0000-0000-0000030EDFBC X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.110.153 Cc: Kevin Wolf , Stefan Hajnoczi , Christoph Hellwig , Luiz Capitulino Subject: [Qemu-devel] [RFC Patch 6/7]Qemu: raw-win32 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 win32 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-win32.c =================================================================== --- qemu.orig/block/raw-win32.c +++ qemu/block/raw-win32.c @@ -26,18 +26,27 @@ #include "block_int.h" #include "module.h" #include +#include #include #define FTYPE_FILE 0 #define FTYPE_CD 1 #define FTYPE_HARDDISK 2 +#define WINDOWS_VISTA 6 typedef struct BDRVRawState { HANDLE hfile; int type; char drive_path[16]; /* format: "d:\" */ + DWORD overlapped; } BDRVRawState; +typedef struct BDRVRawReopenState { + BDRVReopenState reopen_state; + HANDLE stash_hfile; + DWORD stash_overlapped; +} BDRVRawReopenState; + int qemu_ftruncate64(int fd, int64_t length) { LARGE_INTEGER li; @@ -106,9 +115,96 @@ static int raw_open(BlockDriverState *bs return -EACCES; return -1; } + s->overlapped = overlapped; return 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; + OSVERSIONINFO osvi; + BOOL bIsWindowsVistaorLater; + + raw_rs->bs = bs; + raw_rs->stash_hfile = s->hfile; + raw_rs->stash_overlapped = s->overlapped; + *prs = raw_rs; + + if (flags & BDRV_O_NOCACHE) { + s->overlapped |= FILE_FLAG_NO_BUFFERING; + } else { + s->overlapped &= ~FILE_FLAG_NO_BUFFERING; + } + + if (!(flags & BDRV_O_CACHE_WB)) { + s->overlapped |= FILE_FLAG_WRITE_THROUGH; + } else { + s->overlapped &= ~FILE_FLAG_WRITE_THROUGH; + } + + ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); + osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + + GetVersionEx(&osvi); + + if (osvi.dwMajorVersion >= WINDOWS_VISTA) { + s->hfile = ReOpenFile(raw_rs->stash_hfile, 0, FILE_SHARE_READ, + overlapped); + if (s->hfile == INVALID_HANDLE_VALUE) { + int err = GetLastError(); + if (err == ERROR_ACCESS_DENIED) { + ret = -EACCES; + } else { + ret = -1; + } + } + } else { + + DuplicateHandle(GetCurrentProcess(), + raw_rs->stash_hfile, + GetCurrentProcess(), + &s->hfile, + 0, + FALSE, + DUPLICATE_SAME_ACCESS); + bs->drv->bdrv_close(bs); + ret = bs->drv->bdrv_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 handle */ + CloseHandle(raw_rs->stash_hfile); + 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); + + if (s->hfile && (s->hfile != INVALID_HANDLE_VALUE)) { + CloseHandle(s->hfile); + } + s->hfile = raw_rs->stash_hfile; + s->overlapped = raw_rs->stash_overlapped; + g_free(raw_rs); +} + static int raw_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors) {