pcihp: generate populated non-hotpluggble slot descriptions on non-hotplug path
[qemu/armbru.git] / block / copy-before-write.c
blobc9fb809ba0f5c3a6dd1542cc88730f7704479df1
1 /*
2 * copy-before-write filter driver
4 * The driver performs Copy-Before-Write (CBW) operation: it is injected above
5 * some node, and before each write it copies _old_ data to the target node.
7 * Copyright (c) 2018-2021 Virtuozzo International GmbH.
9 * Author:
10 * Sementsov-Ogievskiy Vladimir <vsementsov@virtuozzo.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "qemu/osdep.h"
27 #include "qapi/qmp/qjson.h"
29 #include "sysemu/block-backend.h"
30 #include "qemu/cutils.h"
31 #include "qapi/error.h"
32 #include "block/block_int.h"
33 #include "block/qdict.h"
34 #include "block/block-copy.h"
35 #include "block/dirty-bitmap.h"
37 #include "block/copy-before-write.h"
38 #include "block/reqlist.h"
40 #include "qapi/qapi-visit-block-core.h"
42 typedef struct BDRVCopyBeforeWriteState {
43 BlockCopyState *bcs;
44 BdrvChild *target;
45 OnCbwError on_cbw_error;
46 uint32_t cbw_timeout_ns;
49 * @lock: protects access to @access_bitmap, @done_bitmap and
50 * @frozen_read_reqs
52 CoMutex lock;
55 * @access_bitmap: represents areas allowed for reading by fleecing user.
56 * Reading from non-dirty areas leads to -EACCES.
58 BdrvDirtyBitmap *access_bitmap;
61 * @done_bitmap: represents areas that was successfully copied to @target by
62 * copy-before-write operations.
64 BdrvDirtyBitmap *done_bitmap;
67 * @frozen_read_reqs: current read requests for fleecing user in bs->file
68 * node. These areas must not be rewritten by guest.
70 BlockReqList frozen_read_reqs;
73 * @snapshot_error is normally zero. But on first copy-before-write failure
74 * when @on_cbw_error == ON_CBW_ERROR_BREAK_SNAPSHOT, @snapshot_error takes
75 * value of this error (<0). After that all in-flight and further
76 * snapshot-API requests will fail with that error.
78 int snapshot_error;
79 } BDRVCopyBeforeWriteState;
81 static coroutine_fn int cbw_co_preadv(
82 BlockDriverState *bs, int64_t offset, int64_t bytes,
83 QEMUIOVector *qiov, BdrvRequestFlags flags)
85 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
88 static void block_copy_cb(void *opaque)
90 BlockDriverState *bs = opaque;
92 bdrv_dec_in_flight(bs);
96 * Do copy-before-write operation.
98 * On failure guest request must be failed too.
100 * On success, we also wait for all in-flight fleecing read requests in source
101 * node, and it's guaranteed that after cbw_do_copy_before_write() successful
102 * return there are no such requests and they will never appear.
104 static coroutine_fn int cbw_do_copy_before_write(BlockDriverState *bs,
105 uint64_t offset, uint64_t bytes, BdrvRequestFlags flags)
107 BDRVCopyBeforeWriteState *s = bs->opaque;
108 int ret;
109 uint64_t off, end;
110 int64_t cluster_size = block_copy_cluster_size(s->bcs);
112 if (flags & BDRV_REQ_WRITE_UNCHANGED) {
113 return 0;
116 if (s->snapshot_error) {
117 return 0;
120 off = QEMU_ALIGN_DOWN(offset, cluster_size);
121 end = QEMU_ALIGN_UP(offset + bytes, cluster_size);
124 * Increase in_flight, so that in case of timed-out block-copy, the
125 * remaining background block_copy() request (which can't be immediately
126 * cancelled by timeout) is presented in bs->in_flight. This way we are
127 * sure that on bs close() we'll previously wait for all timed-out but yet
128 * running block_copy calls.
130 bdrv_inc_in_flight(bs);
131 ret = block_copy(s->bcs, off, end - off, true, s->cbw_timeout_ns,
132 block_copy_cb, bs);
133 if (ret < 0 && s->on_cbw_error == ON_CBW_ERROR_BREAK_GUEST_WRITE) {
134 return ret;
137 WITH_QEMU_LOCK_GUARD(&s->lock) {
138 if (ret < 0) {
139 assert(s->on_cbw_error == ON_CBW_ERROR_BREAK_SNAPSHOT);
140 if (!s->snapshot_error) {
141 s->snapshot_error = ret;
143 } else {
144 bdrv_set_dirty_bitmap(s->done_bitmap, off, end - off);
146 reqlist_wait_all(&s->frozen_read_reqs, off, end - off, &s->lock);
149 return 0;
152 static int coroutine_fn cbw_co_pdiscard(BlockDriverState *bs,
153 int64_t offset, int64_t bytes)
155 int ret = cbw_do_copy_before_write(bs, offset, bytes, 0);
156 if (ret < 0) {
157 return ret;
160 return bdrv_co_pdiscard(bs->file, offset, bytes);
163 static int coroutine_fn cbw_co_pwrite_zeroes(BlockDriverState *bs,
164 int64_t offset, int64_t bytes, BdrvRequestFlags flags)
166 int ret = cbw_do_copy_before_write(bs, offset, bytes, flags);
167 if (ret < 0) {
168 return ret;
171 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
174 static coroutine_fn int cbw_co_pwritev(BlockDriverState *bs,
175 int64_t offset,
176 int64_t bytes,
177 QEMUIOVector *qiov,
178 BdrvRequestFlags flags)
180 int ret = cbw_do_copy_before_write(bs, offset, bytes, flags);
181 if (ret < 0) {
182 return ret;
185 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
188 static int coroutine_fn cbw_co_flush(BlockDriverState *bs)
190 if (!bs->file) {
191 return 0;
194 return bdrv_co_flush(bs->file->bs);
198 * If @offset not accessible - return NULL.
200 * Otherwise, set @pnum to some bytes that accessible from @file (@file is set
201 * to bs->file or to s->target). Return newly allocated BlockReq object that
202 * should be than passed to cbw_snapshot_read_unlock().
204 * It's guaranteed that guest writes will not interact in the region until
205 * cbw_snapshot_read_unlock() called.
207 static coroutine_fn BlockReq *
208 cbw_snapshot_read_lock(BlockDriverState *bs, int64_t offset, int64_t bytes,
209 int64_t *pnum, BdrvChild **file)
211 BDRVCopyBeforeWriteState *s = bs->opaque;
212 BlockReq *req = g_new(BlockReq, 1);
213 bool done;
215 QEMU_LOCK_GUARD(&s->lock);
217 if (s->snapshot_error) {
218 g_free(req);
219 return NULL;
222 if (bdrv_dirty_bitmap_next_zero(s->access_bitmap, offset, bytes) != -1) {
223 g_free(req);
224 return NULL;
227 done = bdrv_dirty_bitmap_status(s->done_bitmap, offset, bytes, pnum);
228 if (done) {
230 * Special invalid BlockReq, that is handled in
231 * cbw_snapshot_read_unlock(). We don't need to lock something to read
232 * from s->target.
234 *req = (BlockReq) {.offset = -1, .bytes = -1};
235 *file = s->target;
236 } else {
237 reqlist_init_req(&s->frozen_read_reqs, req, offset, bytes);
238 *file = bs->file;
241 return req;
244 static coroutine_fn void
245 cbw_snapshot_read_unlock(BlockDriverState *bs, BlockReq *req)
247 BDRVCopyBeforeWriteState *s = bs->opaque;
249 if (req->offset == -1 && req->bytes == -1) {
250 g_free(req);
251 return;
254 QEMU_LOCK_GUARD(&s->lock);
256 reqlist_remove_req(req);
257 g_free(req);
260 static coroutine_fn int
261 cbw_co_preadv_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes,
262 QEMUIOVector *qiov, size_t qiov_offset)
264 BlockReq *req;
265 BdrvChild *file;
266 int ret;
268 /* TODO: upgrade to async loop using AioTask */
269 while (bytes) {
270 int64_t cur_bytes;
272 req = cbw_snapshot_read_lock(bs, offset, bytes, &cur_bytes, &file);
273 if (!req) {
274 return -EACCES;
277 ret = bdrv_co_preadv_part(file, offset, cur_bytes,
278 qiov, qiov_offset, 0);
279 cbw_snapshot_read_unlock(bs, req);
280 if (ret < 0) {
281 return ret;
284 bytes -= cur_bytes;
285 offset += cur_bytes;
286 qiov_offset += cur_bytes;
289 return 0;
292 static int coroutine_fn
293 cbw_co_snapshot_block_status(BlockDriverState *bs,
294 bool want_zero, int64_t offset, int64_t bytes,
295 int64_t *pnum, int64_t *map,
296 BlockDriverState **file)
298 BDRVCopyBeforeWriteState *s = bs->opaque;
299 BlockReq *req;
300 int ret;
301 int64_t cur_bytes;
302 BdrvChild *child;
304 req = cbw_snapshot_read_lock(bs, offset, bytes, &cur_bytes, &child);
305 if (!req) {
306 return -EACCES;
309 ret = bdrv_block_status(child->bs, offset, cur_bytes, pnum, map, file);
310 if (child == s->target) {
312 * We refer to s->target only for areas that we've written to it.
313 * And we can not report unallocated blocks in s->target: this will
314 * break generic block-status-above logic, that will go to
315 * copy-before-write filtered child in this case.
317 assert(ret & BDRV_BLOCK_ALLOCATED);
320 cbw_snapshot_read_unlock(bs, req);
322 return ret;
325 static int coroutine_fn cbw_co_pdiscard_snapshot(BlockDriverState *bs,
326 int64_t offset, int64_t bytes)
328 BDRVCopyBeforeWriteState *s = bs->opaque;
330 WITH_QEMU_LOCK_GUARD(&s->lock) {
331 bdrv_reset_dirty_bitmap(s->access_bitmap, offset, bytes);
334 block_copy_reset(s->bcs, offset, bytes);
336 return bdrv_co_pdiscard(s->target, offset, bytes);
339 static void cbw_refresh_filename(BlockDriverState *bs)
341 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
342 bs->file->bs->filename);
345 static void cbw_child_perm(BlockDriverState *bs, BdrvChild *c,
346 BdrvChildRole role,
347 BlockReopenQueue *reopen_queue,
348 uint64_t perm, uint64_t shared,
349 uint64_t *nperm, uint64_t *nshared)
351 if (!(role & BDRV_CHILD_FILTERED)) {
353 * Target child
355 * Share write to target (child_file), to not interfere
356 * with guest writes to its disk which may be in target backing chain.
357 * Can't resize during a backup block job because we check the size
358 * only upfront.
360 *nshared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
361 *nperm = BLK_PERM_WRITE;
362 } else {
363 /* Source child */
364 bdrv_default_perms(bs, c, role, reopen_queue,
365 perm, shared, nperm, nshared);
367 if (!QLIST_EMPTY(&bs->parents)) {
368 if (perm & BLK_PERM_WRITE) {
369 *nperm = *nperm | BLK_PERM_CONSISTENT_READ;
371 *nshared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
376 static BlockdevOptions *cbw_parse_options(QDict *options, Error **errp)
378 BlockdevOptions *opts = NULL;
379 Visitor *v = NULL;
381 qdict_put_str(options, "driver", "copy-before-write");
383 v = qobject_input_visitor_new_flat_confused(options, errp);
384 if (!v) {
385 goto out;
388 visit_type_BlockdevOptions(v, NULL, &opts, errp);
389 if (!opts) {
390 goto out;
394 * Delete options which we are going to parse through BlockdevOptions
395 * object for original options.
397 qdict_extract_subqdict(options, NULL, "bitmap");
398 qdict_del(options, "on-cbw-error");
399 qdict_del(options, "cbw-timeout");
401 out:
402 visit_free(v);
403 qdict_del(options, "driver");
405 return opts;
408 static int cbw_open(BlockDriverState *bs, QDict *options, int flags,
409 Error **errp)
411 BDRVCopyBeforeWriteState *s = bs->opaque;
412 BdrvDirtyBitmap *bitmap = NULL;
413 int64_t cluster_size;
414 g_autoptr(BlockdevOptions) full_opts = NULL;
415 BlockdevOptionsCbw *opts;
416 int ret;
418 full_opts = cbw_parse_options(options, errp);
419 if (!full_opts) {
420 return -EINVAL;
422 assert(full_opts->driver == BLOCKDEV_DRIVER_COPY_BEFORE_WRITE);
423 opts = &full_opts->u.copy_before_write;
425 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
426 if (ret < 0) {
427 return ret;
430 s->target = bdrv_open_child(NULL, options, "target", bs, &child_of_bds,
431 BDRV_CHILD_DATA, false, errp);
432 if (!s->target) {
433 return -EINVAL;
436 if (opts->bitmap) {
437 bitmap = block_dirty_bitmap_lookup(opts->bitmap->node,
438 opts->bitmap->name, NULL, errp);
439 if (!bitmap) {
440 return -EINVAL;
443 s->on_cbw_error = opts->has_on_cbw_error ? opts->on_cbw_error :
444 ON_CBW_ERROR_BREAK_GUEST_WRITE;
445 s->cbw_timeout_ns = opts->has_cbw_timeout ?
446 opts->cbw_timeout * NANOSECONDS_PER_SECOND : 0;
448 bs->total_sectors = bs->file->bs->total_sectors;
449 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
450 (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
451 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
452 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
453 bs->file->bs->supported_zero_flags);
455 s->bcs = block_copy_state_new(bs->file, s->target, bitmap, errp);
456 if (!s->bcs) {
457 error_prepend(errp, "Cannot create block-copy-state: ");
458 return -EINVAL;
461 cluster_size = block_copy_cluster_size(s->bcs);
463 s->done_bitmap = bdrv_create_dirty_bitmap(bs, cluster_size, NULL, errp);
464 if (!s->done_bitmap) {
465 return -EINVAL;
467 bdrv_disable_dirty_bitmap(s->done_bitmap);
469 /* s->access_bitmap starts equal to bcs bitmap */
470 s->access_bitmap = bdrv_create_dirty_bitmap(bs, cluster_size, NULL, errp);
471 if (!s->access_bitmap) {
472 return -EINVAL;
474 bdrv_disable_dirty_bitmap(s->access_bitmap);
475 bdrv_dirty_bitmap_merge_internal(s->access_bitmap,
476 block_copy_dirty_bitmap(s->bcs), NULL,
477 true);
479 qemu_co_mutex_init(&s->lock);
480 QLIST_INIT(&s->frozen_read_reqs);
482 return 0;
485 static void cbw_close(BlockDriverState *bs)
487 BDRVCopyBeforeWriteState *s = bs->opaque;
489 bdrv_release_dirty_bitmap(s->access_bitmap);
490 bdrv_release_dirty_bitmap(s->done_bitmap);
492 block_copy_state_free(s->bcs);
493 s->bcs = NULL;
496 BlockDriver bdrv_cbw_filter = {
497 .format_name = "copy-before-write",
498 .instance_size = sizeof(BDRVCopyBeforeWriteState),
500 .bdrv_open = cbw_open,
501 .bdrv_close = cbw_close,
503 .bdrv_co_preadv = cbw_co_preadv,
504 .bdrv_co_pwritev = cbw_co_pwritev,
505 .bdrv_co_pwrite_zeroes = cbw_co_pwrite_zeroes,
506 .bdrv_co_pdiscard = cbw_co_pdiscard,
507 .bdrv_co_flush = cbw_co_flush,
509 .bdrv_co_preadv_snapshot = cbw_co_preadv_snapshot,
510 .bdrv_co_pdiscard_snapshot = cbw_co_pdiscard_snapshot,
511 .bdrv_co_snapshot_block_status = cbw_co_snapshot_block_status,
513 .bdrv_refresh_filename = cbw_refresh_filename,
515 .bdrv_child_perm = cbw_child_perm,
517 .is_filter = true,
520 BlockDriverState *bdrv_cbw_append(BlockDriverState *source,
521 BlockDriverState *target,
522 const char *filter_node_name,
523 BlockCopyState **bcs,
524 Error **errp)
526 BDRVCopyBeforeWriteState *state;
527 BlockDriverState *top;
528 QDict *opts;
530 assert(source->total_sectors == target->total_sectors);
531 GLOBAL_STATE_CODE();
533 opts = qdict_new();
534 qdict_put_str(opts, "driver", "copy-before-write");
535 if (filter_node_name) {
536 qdict_put_str(opts, "node-name", filter_node_name);
538 qdict_put_str(opts, "file", bdrv_get_node_name(source));
539 qdict_put_str(opts, "target", bdrv_get_node_name(target));
541 top = bdrv_insert_node(source, opts, BDRV_O_RDWR, errp);
542 if (!top) {
543 return NULL;
546 state = top->opaque;
547 *bcs = state->bcs;
549 return top;
552 void bdrv_cbw_drop(BlockDriverState *bs)
554 GLOBAL_STATE_CODE();
555 bdrv_drop_filter(bs, &error_abort);
556 bdrv_unref(bs);
559 static void cbw_init(void)
561 bdrv_register(&bdrv_cbw_filter);
564 block_init(cbw_init);