From: Jeff Cody <jcody@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
Supriya Kannery <supriyak@linux.vnet.ibm.com>,
Shrinidhi Joshi <spjoshi31@gmail.com>,
Stefan Hajnoczi <stefanha@gmail.com>,
qemu-devel@nongnu.org, Luiz Capitulino <lcapitulino@redhat.com>,
Christoph Hellwig <hch@lst.de>
Subject: Re: [Qemu-devel] [v2 Patch 2/9]block: raw-posix image file reopen
Date: Fri, 03 Aug 2012 18:32:16 -0400 [thread overview]
Message-ID: <501C5170.7020305@redhat.com> (raw)
In-Reply-To: <50181314.1040909@redhat.com>
On 07/31/2012 01:17 PM, Eric Blake wrote:
> On 07/30/2012 03:34 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>
>>
>> ---
>> Index: qemu/block/raw.c
>> ===================================================================
>
>> +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);
>
> You called it out in your cover letter, but just pointing out that this
> is one of the locations that needs a conditional fallback to
> dup2/qemu_set_cloexec if dup3 and O_CLOEXEC are missing.
>
> More importantly, though, you want this to be fcntl(F_DUP_CLOEXEC) and
> NOT dup3, so that you are duplicating to the first available fd rather
> than accidentally overwriting whatever s->fd happened to be on entry.
> Also, where is your error checking that you didn't just assign s->fd to
> -1? It looks like your goal here is to stash a copy of the fd, so that
> on failure you can then pivot over to your copy.
In addition, as it is written above will always assign -1 to s->fd, and
stash_s->fd every time, with errno "Bad file descriptor".
The function raw_stash_state() does not save s->fd, but instead sets the
stashed copy (raw_rs->stash_s->fd) to be -1.
Then this:
s->fd = dup3(raw_rs->stash_s->fd, s->fd, O_CLOEXEC);
assigns s->fd to -1.
I don't think dup3() will work at all, because if -1 is used for one of the
file descriptors, that is EBADF, and if stash_s->fd == s->fd, that is
EINVAL.
So I agree, I think we definitely want fcntl(F_DUP_CLOEXEC) here.
>
>> +
>> + *prs = &(raw_rs->reopen_state);
>> +
>> + /* Flags that can be set using fcntl */
>> + int fcntl_flags = BDRV_O_NOCACHE;
>
> This variable name is misleading; fcntl_flags in my mind implies O_* not
> BDRV_O_*, as we are not guaranteed that they are the same values. Maybe
> bdrv_flags is a better name. Or are you trying to list the subset of
> BDRV_O flags that can be changed via mapping to the appropriate O_ flag
> during fcntl?
>
>> +
>> + 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);
>> + }
>
> At any rate, in spite of the poor choice of naming for fcntl_flags, the
> logic here looked correct - if the only BDRV_O_ bits that changed
> between existing flags and the requested flags can be handled by fcntl,
> then use fcntl to change them, otherwise open from scratch.
>
>> +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);
>> + }
>
> At first, I worried that you needed to use dup3() here to restore s->fd
> from the cached fd...
>
>> + raw_revert_state(s, raw_rs->stash_s);
>
> then I read the code for raw_revert_state, where you are rewriting the
> state to permanently use the dup'd copy rather than trying to revert
> back to the old fd number. As long as we are sure that all other points
> in the code base always go through s->fd rather than caching the old
> value, and therefore don't have a stale reference, then swapping the
> struct instead of using dup3 to restore the exact fd value we previously
> had should work.
>
next prev parent reply other threads:[~2012-08-03 22:32 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-07-30 21:34 [Qemu-devel] [v2 Patch 0/9]block: Image file reopen and dynamic host pagecache change Supriya Kannery
2012-07-30 21:34 ` [Qemu-devel] [v2 Patch 1/9]block: Framework for reopening image files safely Supriya Kannery
2012-08-01 15:51 ` Stefan Hajnoczi
2012-08-02 20:19 ` Luiz Capitulino
2012-08-14 8:54 ` Supriya Kannery
2012-08-08 21:13 ` Jeff Cody
2012-08-14 9:21 ` Supriya Kannery
2012-08-09 4:26 ` Jeff Cody
2012-08-09 9:20 ` Kevin Wolf
2012-08-09 13:02 ` Jeff Cody
2012-08-14 10:24 ` Supriya Kannery
2012-07-30 21:34 ` [Qemu-devel] [v2 Patch 2/9]block: raw-posix image file reopen Supriya Kannery
2012-07-31 17:17 ` Eric Blake
2012-08-01 6:18 ` Kevin Wolf
2012-08-03 22:32 ` Jeff Cody [this message]
2012-08-14 10:53 ` Supriya Kannery
2012-08-09 4:26 ` Jeff Cody
2012-08-10 13:45 ` Corey Bryant
2012-08-14 11:13 ` Supriya Kannery
2012-08-14 11:35 ` Kevin Wolf
2012-07-30 21:34 ` [Qemu-devel] [v2 Patch 3/9]block: raw-win32 " Supriya Kannery
2012-07-30 21:35 ` [Qemu-devel] [v2 Patch 4/9]block: vmdk " Supriya Kannery
2012-07-31 17:43 ` Eric Blake
2012-07-30 21:35 ` [Qemu-devel] [v2 Patch 5/9]block: qcow2 " Supriya Kannery
2012-08-09 4:26 ` Jeff Cody
2012-08-09 9:37 ` Kevin Wolf
2012-07-30 21:35 ` [Qemu-devel] [v2 Patch 7/9]block: qed " Supriya Kannery
2012-07-30 21:36 ` [Qemu-devel] [v2 Patch 9/9]block: Enhance "info block" to display host cache setting Supriya Kannery
2012-07-31 17:47 ` Eric Blake
2012-07-31 20:33 ` [Qemu-devel] [v2 Patch 0/9]block: Image file reopen and dynamic host pagecache change Jeff Cody
2012-08-01 17:11 ` Supriya Kannery
2012-08-01 18:36 ` [Qemu-devel] [v2 Patch 6/9]block: qcow image file reopen Supriya Kannery
2012-08-01 18:44 ` [Qemu-devel] [v2 Patch 8/9]block: Cmd "block_set_hostcache" for dynamic cache change Supriya Kannery
2012-08-01 19:21 ` Eric Blake
2012-08-02 20:36 ` Luiz Capitulino
2012-08-31 19:32 ` Jeff Cody
2012-08-09 4:26 ` [Qemu-devel] [v2 Patch 0/9]block: Image file reopen and dynamic host pagecache change Jeff Cody
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=501C5170.7020305@redhat.com \
--to=jcody@redhat.com \
--cc=eblake@redhat.com \
--cc=hch@lst.de \
--cc=kwolf@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=spjoshi31@gmail.com \
--cc=stefanha@gmail.com \
--cc=supriyak@linux.vnet.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).