file-posix: Lock new fd in raw_reopen_prepare()

There is no reason why we can take locks on the new file descriptor only
in raw_reopen_commit() where error handling isn't possible any more.
Instead, we can already do this in raw_reopen_prepare().

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Kevin Wolf 2019-03-01 22:48:45 +01:00
parent e0c9cf3a48
commit a6aeca0ca5

View file

@ -906,7 +906,7 @@ static int raw_reopen_prepare(BDRVReopenState *state,
BDRVRawState *s; BDRVRawState *s;
BDRVRawReopenState *rs; BDRVRawReopenState *rs;
QemuOpts *opts; QemuOpts *opts;
int ret = 0; int ret;
Error *local_err = NULL; Error *local_err = NULL;
assert(state != NULL); assert(state != NULL);
@ -947,14 +947,27 @@ static int raw_reopen_prepare(BDRVReopenState *state,
if (rs->fd != -1) { if (rs->fd != -1) {
raw_probe_alignment(state->bs, rs->fd, &local_err); raw_probe_alignment(state->bs, rs->fd, &local_err);
if (local_err) { if (local_err) {
qemu_close(rs->fd);
rs->fd = -1;
error_propagate(errp, local_err); error_propagate(errp, local_err);
ret = -EINVAL; ret = -EINVAL;
goto out_fd;
}
/* Copy locks to the new fd */
ret = raw_apply_lock_bytes(NULL, rs->fd, s->locked_perm,
s->locked_shared_perm, false, errp);
if (ret < 0) {
ret = -EINVAL;
goto out_fd;
} }
} }
s->reopen_state = state; s->reopen_state = state;
ret = 0;
out_fd:
if (ret < 0) {
qemu_close(rs->fd);
rs->fd = -1;
}
out: out:
qemu_opts_del(opts); qemu_opts_del(opts);
return ret; return ret;
@ -964,18 +977,10 @@ static void raw_reopen_commit(BDRVReopenState *state)
{ {
BDRVRawReopenState *rs = state->opaque; BDRVRawReopenState *rs = state->opaque;
BDRVRawState *s = state->bs->opaque; BDRVRawState *s = state->bs->opaque;
Error *local_err = NULL;
s->check_cache_dropped = rs->check_cache_dropped; s->check_cache_dropped = rs->check_cache_dropped;
s->open_flags = rs->open_flags; s->open_flags = rs->open_flags;
/* Copy locks to the new fd before closing the old one. */
raw_apply_lock_bytes(NULL, rs->fd, s->locked_perm,
s->locked_shared_perm, false, &local_err);
if (local_err) {
/* shouldn't fail in a sane host, but report it just in case. */
error_report_err(local_err);
}
qemu_close(s->fd); qemu_close(s->fd);
s->fd = rs->fd; s->fd = rs->fd;