block.c (ab5b522879e2a7880418cbd29340675e5427572f) block.c (6cf42ca2f9782f0335abf3e6b611fbced40cd099)
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2020 Virtuozzo International GmbH.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal

--- 4110 unchanged lines hidden (view full) ---

4119 * device will cause all device changes to be abandoned, and intermediate
4120 * data cleaned up.
4121 *
4122 * If all devices prepare successfully, then the changes are committed
4123 * to all devices.
4124 *
4125 * All affected nodes must be drained between bdrv_reopen_queue() and
4126 * bdrv_reopen_multiple().
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2020 Virtuozzo International GmbH.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal

--- 4110 unchanged lines hidden (view full) ---

4119 * device will cause all device changes to be abandoned, and intermediate
4120 * data cleaned up.
4121 *
4122 * If all devices prepare successfully, then the changes are committed
4123 * to all devices.
4124 *
4125 * All affected nodes must be drained between bdrv_reopen_queue() and
4126 * bdrv_reopen_multiple().
4127 *
4128 * To be called from the main thread, with all other AioContexts unlocked.
4127 */
4128int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
4129{
4130 int ret = -1;
4131 BlockReopenQueueEntry *bs_entry, *next;
4129 */
4130int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
4131{
4132 int ret = -1;
4133 BlockReopenQueueEntry *bs_entry, *next;
4134 AioContext *ctx;
4132 Transaction *tran = tran_new();
4133 g_autoptr(GHashTable) found = NULL;
4134 g_autoptr(GSList) refresh_list = NULL;
4135
4135 Transaction *tran = tran_new();
4136 g_autoptr(GHashTable) found = NULL;
4137 g_autoptr(GSList) refresh_list = NULL;
4138
4139 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
4136 assert(bs_queue != NULL);
4137
4138 QTAILQ_FOREACH(bs_entry, bs_queue, entry) {
4140 assert(bs_queue != NULL);
4141
4142 QTAILQ_FOREACH(bs_entry, bs_queue, entry) {
4143 ctx = bdrv_get_aio_context(bs_entry->state.bs);
4144 aio_context_acquire(ctx);
4139 ret = bdrv_flush(bs_entry->state.bs);
4145 ret = bdrv_flush(bs_entry->state.bs);
4146 aio_context_release(ctx);
4140 if (ret < 0) {
4141 error_setg_errno(errp, -ret, "Error flushing drive");
4142 goto abort;
4143 }
4144 }
4145
4146 QTAILQ_FOREACH(bs_entry, bs_queue, entry) {
4147 assert(bs_entry->state.bs->quiesce_counter > 0);
4147 if (ret < 0) {
4148 error_setg_errno(errp, -ret, "Error flushing drive");
4149 goto abort;
4150 }
4151 }
4152
4153 QTAILQ_FOREACH(bs_entry, bs_queue, entry) {
4154 assert(bs_entry->state.bs->quiesce_counter > 0);
4155 ctx = bdrv_get_aio_context(bs_entry->state.bs);
4156 aio_context_acquire(ctx);
4148 ret = bdrv_reopen_prepare(&bs_entry->state, bs_queue, tran, errp);
4157 ret = bdrv_reopen_prepare(&bs_entry->state, bs_queue, tran, errp);
4158 aio_context_release(ctx);
4149 if (ret < 0) {
4150 goto abort;
4151 }
4152 bs_entry->prepared = true;
4153 }
4154
4155 found = g_hash_table_new(NULL, NULL);
4156 QTAILQ_FOREACH(bs_entry, bs_queue, entry) {

--- 26 unchanged lines hidden (view full) ---

4183 * changes.
4184 *
4185 * Reverse order is used to comfort qcow2 driver: on commit it need to write
4186 * IN_USE flag to the image, to mark bitmaps in the image as invalid. But
4187 * children are usually goes after parents in reopen-queue, so go from last
4188 * to first element.
4189 */
4190 QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) {
4159 if (ret < 0) {
4160 goto abort;
4161 }
4162 bs_entry->prepared = true;
4163 }
4164
4165 found = g_hash_table_new(NULL, NULL);
4166 QTAILQ_FOREACH(bs_entry, bs_queue, entry) {

--- 26 unchanged lines hidden (view full) ---

4193 * changes.
4194 *
4195 * Reverse order is used to comfort qcow2 driver: on commit it need to write
4196 * IN_USE flag to the image, to mark bitmaps in the image as invalid. But
4197 * children are usually goes after parents in reopen-queue, so go from last
4198 * to first element.
4199 */
4200 QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) {
4201 ctx = bdrv_get_aio_context(bs_entry->state.bs);
4202 aio_context_acquire(ctx);
4191 bdrv_reopen_commit(&bs_entry->state);
4203 bdrv_reopen_commit(&bs_entry->state);
4204 aio_context_release(ctx);
4192 }
4193
4194 tran_commit(tran);
4195
4196 QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) {
4197 BlockDriverState *bs = bs_entry->state.bs;
4198
4199 if (bs->drv->bdrv_reopen_commit_post) {
4205 }
4206
4207 tran_commit(tran);
4208
4209 QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) {
4210 BlockDriverState *bs = bs_entry->state.bs;
4211
4212 if (bs->drv->bdrv_reopen_commit_post) {
4213 ctx = bdrv_get_aio_context(bs);
4214 aio_context_acquire(ctx);
4200 bs->drv->bdrv_reopen_commit_post(&bs_entry->state);
4215 bs->drv->bdrv_reopen_commit_post(&bs_entry->state);
4216 aio_context_release(ctx);
4201 }
4202 }
4203
4204 ret = 0;
4205 goto cleanup;
4206
4207abort:
4208 tran_abort(tran);
4209 QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
4210 if (bs_entry->prepared) {
4217 }
4218 }
4219
4220 ret = 0;
4221 goto cleanup;
4222
4223abort:
4224 tran_abort(tran);
4225 QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
4226 if (bs_entry->prepared) {
4227 ctx = bdrv_get_aio_context(bs_entry->state.bs);
4228 aio_context_acquire(ctx);
4211 bdrv_reopen_abort(&bs_entry->state);
4229 bdrv_reopen_abort(&bs_entry->state);
4230 aio_context_release(ctx);
4212 }
4213 }
4214
4215cleanup:
4216 bdrv_reopen_queue_free(bs_queue);
4217
4218 return ret;
4219}
4220
4231 }
4232 }
4233
4234cleanup:
4235 bdrv_reopen_queue_free(bs_queue);
4236
4237 return ret;
4238}
4239
4221int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only,
4222 Error **errp)
4240int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,
4241 Error **errp)
4223{
4242{
4224 int ret;
4243 AioContext *ctx = bdrv_get_aio_context(bs);
4225 BlockReopenQueue *queue;
4244 BlockReopenQueue *queue;
4226 QDict *opts = qdict_new();
4245 int ret;
4227
4246
4228 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, read_only);
4229
4230 bdrv_subtree_drained_begin(bs);
4247 bdrv_subtree_drained_begin(bs);
4231 queue = bdrv_reopen_queue(NULL, bs, opts, true);
4248 if (ctx != qemu_get_aio_context()) {
4249 aio_context_release(ctx);
4250 }
4251
4252 queue = bdrv_reopen_queue(NULL, bs, opts, keep_old_opts);
4232 ret = bdrv_reopen_multiple(queue, errp);
4253 ret = bdrv_reopen_multiple(queue, errp);
4254
4255 if (ctx != qemu_get_aio_context()) {
4256 aio_context_acquire(ctx);
4257 }
4233 bdrv_subtree_drained_end(bs);
4234
4235 return ret;
4236}
4237
4258 bdrv_subtree_drained_end(bs);
4259
4260 return ret;
4261}
4262
4263int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only,
4264 Error **errp)
4265{
4266 QDict *opts = qdict_new();
4267
4268 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, read_only);
4269
4270 return bdrv_reopen(bs, opts, true, errp);
4271}
4272
4238/*
4239 * Take a BDRVReopenState and check if the value of 'backing' in the
4240 * reopen_state->options QDict is valid or not.
4241 *
4242 * If 'backing' is missing from the QDict then return 0.
4243 *
4244 * If 'backing' contains the node name of the backing file of
4245 * reopen_state->bs then return 0.

--- 3372 unchanged lines hidden ---
4273/*
4274 * Take a BDRVReopenState and check if the value of 'backing' in the
4275 * reopen_state->options QDict is valid or not.
4276 *
4277 * If 'backing' is missing from the QDict then return 0.
4278 *
4279 * If 'backing' contains the node name of the backing file of
4280 * reopen_state->bs then return 0.

--- 3372 unchanged lines hidden ---