From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:39964) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SmHvy-0002PW-6p for qemu-devel@nongnu.org; Wed, 04 Jul 2012 01:15:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SmHvv-0001kl-5s for qemu-devel@nongnu.org; Wed, 04 Jul 2012 01:15:25 -0400 Received: from mail-pb0-f45.google.com ([209.85.160.45]:40634) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SmHvu-0001jY-Mu for qemu-devel@nongnu.org; Wed, 04 Jul 2012 01:15:23 -0400 Received: by pbbro12 with SMTP id ro12so11532455pbb.4 for ; Tue, 03 Jul 2012 22:15:20 -0700 (PDT) Message-ID: <4FF3D161.9050802@gmail.com> Date: Wed, 04 Jul 2012 10:45:13 +0530 From: Shrinidhi Joshi MIME-Version: 1.0 References: <20120615204648.9853.1225.sendpatchset@skannery.in.ibm.com> <20120615204800.9853.19740.sendpatchset@skannery.in.ibm.com> <4FDBB309.1050704@redhat.com> In-Reply-To: <4FDBB309.1050704@redhat.com> Content-Type: multipart/alternative; boundary="------------030200000700090108080508" Subject: Re: [Qemu-devel] [v1 Patch 5/10]Qemu: raw-posix image file reopen List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: Kevin Wolf , Stefan Hajnoczi , jcody@redhat.com, qemu-devel@nongnu.org, Luiz Capitulino , Christoph Hellwig This is a multi-part message in MIME format. --------------030200000700090108080508 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On Saturday 16 June 2012 03:41 AM, Eric Blake wrote: > On 06/15/2012 02:48 PM, Supriya Kannery wrote: > > 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 > > > > > > > +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)); */ > > Why the comment? > > > + raw_stash_state(raw_rs->stash_s, s); > > + s->fd = dup(raw_rs->stash_s->fd); > > Needs to handle O_CLOEXEC open flag, which means using > fcntl(F_DUPFD_CLOEXEC) when available for atomic support, and a fallback > to fcntl(F_GETFD/F_SETFD) if not. > > > + > > + *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); > > Is this a case where Paolo's proposed bdrv_swap command would be useful? > > > + /* revert to stashed state */ > > + if (s->fd != -1) { > > + close(s->fd); > > + } > > +/* memcpy(s, raw_rs->stash_s, sizeof(BDRVRawState)); */ > > Why the comment? > 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 Changed the dup function to dup3 to handle the O_CLOEXEC flag. Signed-off-by: Shrinidhi Joshi Index: qemu/block/raw.c =================================================================== --- qemu.orig/block/raw.c 2012-06-19 15:52:40.512586150 +0530 +++ qemu/block/raw.c 2012-06-19 17:39:33.572386768 +0530 @@ -9,6 +9,22 @@ 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) { @@ -111,6 +127,10 @@ .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 2012-06-19 15:52:40.520586178 +0530 +++ qemu/block/raw-posix.c 2012-06-20 15:59:09.036535061 +0530 @@ -140,8 +140,15 @@ #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,119 @@ 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 */ + bdrv_flush(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 { @@ -728,6 +848,9 @@ .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, --------------030200000700090108080508 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit On Saturday 16 June 2012 03:41 AM, Eric Blake wrote:
> On 06/15/2012 02:48 PM, Supriya Kannery wrote:
>> 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 <supriyak@linux.vnet.ibm.com>
>>
>
>>
>> +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)); */
>
> Why the comment?
>
>> + raw_stash_state(raw_rs->stash_s, s);
>> + s->fd = dup(raw_rs->stash_s->fd);
>
> Needs to handle O_CLOEXEC open flag, which means using
> fcntl(F_DUPFD_CLOEXEC) when available for atomic support, and a fallback
> to fcntl(F_GETFD/F_SETFD) if not.
>
>> +
>> + *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);
>
> Is this a case where Paolo's proposed bdrv_swap command would be useful?
>
>> + /* revert to stashed state */
>> + if (s->fd != -1) {
>> + close(s->fd);
>> + }
>> +/* memcpy(s, raw_rs->stash_s, sizeof(BDRVRawState)); */
>
> Why the comment?
>

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 <supriyak@linux.vnet.ibm.com>

Changed the dup function to dup3 to handle the O_CLOEXEC flag.

Signed-off-by: Shrinidhi Joshi <spjoshi31@gmail.com>

Index: qemu/block/raw.c
===================================================================
--- qemu.orig/block/raw.c    2012-06-19 15:52:40.512586150 +0530
+++ qemu/block/raw.c    2012-06-19 17:39:33.572386768 +0530
@@ -9,6 +9,22 @@
     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)
 {
@@ -111,6 +127,10 @@
     .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    2012-06-19 15:52:40.520586178 +0530
+++ qemu/block/raw-posix.c    2012-06-20 15:59:09.036535061 +0530
@@ -140,8 +140,15 @@
 #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,119 @@
     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 */
+      bdrv_flush(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
         {
@@ -728,6 +848,9 @@
     .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,

--------------030200000700090108080508--