1d003e0aeSVladimir Sementsov-Ogievskiy /* 2d003e0aeSVladimir Sementsov-Ogievskiy * copy-before-write filter driver 3d003e0aeSVladimir Sementsov-Ogievskiy * 4d003e0aeSVladimir Sementsov-Ogievskiy * The driver performs Copy-Before-Write (CBW) operation: it is injected above 5d003e0aeSVladimir Sementsov-Ogievskiy * some node, and before each write it copies _old_ data to the target node. 6d003e0aeSVladimir Sementsov-Ogievskiy * 7d003e0aeSVladimir Sementsov-Ogievskiy * Copyright (c) 2018-2021 Virtuozzo International GmbH. 8d003e0aeSVladimir Sementsov-Ogievskiy * 9d003e0aeSVladimir Sementsov-Ogievskiy * Author: 10d003e0aeSVladimir Sementsov-Ogievskiy * Sementsov-Ogievskiy Vladimir <vsementsov@virtuozzo.com> 11d003e0aeSVladimir Sementsov-Ogievskiy * 12d003e0aeSVladimir Sementsov-Ogievskiy * This program is free software; you can redistribute it and/or modify 13d003e0aeSVladimir Sementsov-Ogievskiy * it under the terms of the GNU General Public License as published by 14d003e0aeSVladimir Sementsov-Ogievskiy * the Free Software Foundation; either version 2 of the License, or 15d003e0aeSVladimir Sementsov-Ogievskiy * (at your option) any later version. 16d003e0aeSVladimir Sementsov-Ogievskiy * 17d003e0aeSVladimir Sementsov-Ogievskiy * This program is distributed in the hope that it will be useful, 18d003e0aeSVladimir Sementsov-Ogievskiy * but WITHOUT ANY WARRANTY; without even the implied warranty of 19d003e0aeSVladimir Sementsov-Ogievskiy * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20d003e0aeSVladimir Sementsov-Ogievskiy * GNU General Public License for more details. 21d003e0aeSVladimir Sementsov-Ogievskiy * 22d003e0aeSVladimir Sementsov-Ogievskiy * You should have received a copy of the GNU General Public License 23d003e0aeSVladimir Sementsov-Ogievskiy * along with this program. If not, see <http://www.gnu.org/licenses/>. 24d003e0aeSVladimir Sementsov-Ogievskiy */ 25d003e0aeSVladimir Sementsov-Ogievskiy 26d003e0aeSVladimir Sementsov-Ogievskiy #include "qemu/osdep.h" 2779ef0cebSVladimir Sementsov-Ogievskiy #include "qapi/qmp/qjson.h" 28d003e0aeSVladimir Sementsov-Ogievskiy 29d003e0aeSVladimir Sementsov-Ogievskiy #include "sysemu/block-backend.h" 30d003e0aeSVladimir Sementsov-Ogievskiy #include "qemu/cutils.h" 31d003e0aeSVladimir Sementsov-Ogievskiy #include "qapi/error.h" 32d003e0aeSVladimir Sementsov-Ogievskiy #include "block/block_int.h" 33d003e0aeSVladimir Sementsov-Ogievskiy #include "block/qdict.h" 34d003e0aeSVladimir Sementsov-Ogievskiy #include "block/block-copy.h" 35e2c1c34fSMarkus Armbruster #include "block/dirty-bitmap.h" 36d003e0aeSVladimir Sementsov-Ogievskiy 37d003e0aeSVladimir Sementsov-Ogievskiy #include "block/copy-before-write.h" 38af5bcd77SVladimir Sementsov-Ogievskiy #include "block/reqlist.h" 39d003e0aeSVladimir Sementsov-Ogievskiy 405f3a3cd7SVladimir Sementsov-Ogievskiy #include "qapi/qapi-visit-block-core.h" 415f3a3cd7SVladimir Sementsov-Ogievskiy 42d003e0aeSVladimir Sementsov-Ogievskiy typedef struct BDRVCopyBeforeWriteState { 43d003e0aeSVladimir Sementsov-Ogievskiy BlockCopyState *bcs; 44d003e0aeSVladimir Sementsov-Ogievskiy BdrvChild *target; 45f1bb39a8SVladimir Sementsov-Ogievskiy OnCbwError on_cbw_error; 466db7fd1cSVladimir Sementsov-Ogievskiy uint32_t cbw_timeout_ns; 47af5bcd77SVladimir Sementsov-Ogievskiy 48af5bcd77SVladimir Sementsov-Ogievskiy /* 49af5bcd77SVladimir Sementsov-Ogievskiy * @lock: protects access to @access_bitmap, @done_bitmap and 50af5bcd77SVladimir Sementsov-Ogievskiy * @frozen_read_reqs 51af5bcd77SVladimir Sementsov-Ogievskiy */ 52af5bcd77SVladimir Sementsov-Ogievskiy CoMutex lock; 53af5bcd77SVladimir Sementsov-Ogievskiy 54af5bcd77SVladimir Sementsov-Ogievskiy /* 55af5bcd77SVladimir Sementsov-Ogievskiy * @access_bitmap: represents areas allowed for reading by fleecing user. 56af5bcd77SVladimir Sementsov-Ogievskiy * Reading from non-dirty areas leads to -EACCES. 57af5bcd77SVladimir Sementsov-Ogievskiy */ 58af5bcd77SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *access_bitmap; 59af5bcd77SVladimir Sementsov-Ogievskiy 60af5bcd77SVladimir Sementsov-Ogievskiy /* 61af5bcd77SVladimir Sementsov-Ogievskiy * @done_bitmap: represents areas that was successfully copied to @target by 62af5bcd77SVladimir Sementsov-Ogievskiy * copy-before-write operations. 63af5bcd77SVladimir Sementsov-Ogievskiy */ 64af5bcd77SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *done_bitmap; 65af5bcd77SVladimir Sementsov-Ogievskiy 66af5bcd77SVladimir Sementsov-Ogievskiy /* 67af5bcd77SVladimir Sementsov-Ogievskiy * @frozen_read_reqs: current read requests for fleecing user in bs->file 68af5bcd77SVladimir Sementsov-Ogievskiy * node. These areas must not be rewritten by guest. 69af5bcd77SVladimir Sementsov-Ogievskiy */ 70af5bcd77SVladimir Sementsov-Ogievskiy BlockReqList frozen_read_reqs; 71f1bb39a8SVladimir Sementsov-Ogievskiy 72f1bb39a8SVladimir Sementsov-Ogievskiy /* 73f1bb39a8SVladimir Sementsov-Ogievskiy * @snapshot_error is normally zero. But on first copy-before-write failure 74f1bb39a8SVladimir Sementsov-Ogievskiy * when @on_cbw_error == ON_CBW_ERROR_BREAK_SNAPSHOT, @snapshot_error takes 75f1bb39a8SVladimir Sementsov-Ogievskiy * value of this error (<0). After that all in-flight and further 76f1bb39a8SVladimir Sementsov-Ogievskiy * snapshot-API requests will fail with that error. 77f1bb39a8SVladimir Sementsov-Ogievskiy */ 78f1bb39a8SVladimir Sementsov-Ogievskiy int snapshot_error; 79d003e0aeSVladimir Sementsov-Ogievskiy } BDRVCopyBeforeWriteState; 80d003e0aeSVladimir Sementsov-Ogievskiy 81b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 82b9b10c35SKevin Wolf cbw_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, 83f7ef38ddSVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, BdrvRequestFlags flags) 84d003e0aeSVladimir Sementsov-Ogievskiy { 853c1e6327SVladimir Sementsov-Ogievskiy return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); 86d003e0aeSVladimir Sementsov-Ogievskiy } 87d003e0aeSVladimir Sementsov-Ogievskiy 886db7fd1cSVladimir Sementsov-Ogievskiy static void block_copy_cb(void *opaque) 896db7fd1cSVladimir Sementsov-Ogievskiy { 906db7fd1cSVladimir Sementsov-Ogievskiy BlockDriverState *bs = opaque; 916db7fd1cSVladimir Sementsov-Ogievskiy 926db7fd1cSVladimir Sementsov-Ogievskiy bdrv_dec_in_flight(bs); 936db7fd1cSVladimir Sementsov-Ogievskiy } 946db7fd1cSVladimir Sementsov-Ogievskiy 95af5bcd77SVladimir Sementsov-Ogievskiy /* 96af5bcd77SVladimir Sementsov-Ogievskiy * Do copy-before-write operation. 97af5bcd77SVladimir Sementsov-Ogievskiy * 98af5bcd77SVladimir Sementsov-Ogievskiy * On failure guest request must be failed too. 99af5bcd77SVladimir Sementsov-Ogievskiy * 100af5bcd77SVladimir Sementsov-Ogievskiy * On success, we also wait for all in-flight fleecing read requests in source 101af5bcd77SVladimir Sementsov-Ogievskiy * node, and it's guaranteed that after cbw_do_copy_before_write() successful 102af5bcd77SVladimir Sementsov-Ogievskiy * return there are no such requests and they will never appear. 103af5bcd77SVladimir Sementsov-Ogievskiy */ 104d003e0aeSVladimir Sementsov-Ogievskiy static coroutine_fn int cbw_do_copy_before_write(BlockDriverState *bs, 105d003e0aeSVladimir Sementsov-Ogievskiy uint64_t offset, uint64_t bytes, BdrvRequestFlags flags) 106d003e0aeSVladimir Sementsov-Ogievskiy { 107d003e0aeSVladimir Sementsov-Ogievskiy BDRVCopyBeforeWriteState *s = bs->opaque; 108af5bcd77SVladimir Sementsov-Ogievskiy int ret; 109d003e0aeSVladimir Sementsov-Ogievskiy uint64_t off, end; 110b518e9e9SVladimir Sementsov-Ogievskiy int64_t cluster_size = block_copy_cluster_size(s->bcs); 111d003e0aeSVladimir Sementsov-Ogievskiy 112d003e0aeSVladimir Sementsov-Ogievskiy if (flags & BDRV_REQ_WRITE_UNCHANGED) { 113d003e0aeSVladimir Sementsov-Ogievskiy return 0; 114d003e0aeSVladimir Sementsov-Ogievskiy } 115d003e0aeSVladimir Sementsov-Ogievskiy 116f1bb39a8SVladimir Sementsov-Ogievskiy if (s->snapshot_error) { 117f1bb39a8SVladimir Sementsov-Ogievskiy return 0; 118f1bb39a8SVladimir Sementsov-Ogievskiy } 119f1bb39a8SVladimir Sementsov-Ogievskiy 120b518e9e9SVladimir Sementsov-Ogievskiy off = QEMU_ALIGN_DOWN(offset, cluster_size); 121b518e9e9SVladimir Sementsov-Ogievskiy end = QEMU_ALIGN_UP(offset + bytes, cluster_size); 122d003e0aeSVladimir Sementsov-Ogievskiy 1236db7fd1cSVladimir Sementsov-Ogievskiy /* 1246db7fd1cSVladimir Sementsov-Ogievskiy * Increase in_flight, so that in case of timed-out block-copy, the 1256db7fd1cSVladimir Sementsov-Ogievskiy * remaining background block_copy() request (which can't be immediately 1266db7fd1cSVladimir Sementsov-Ogievskiy * cancelled by timeout) is presented in bs->in_flight. This way we are 1276db7fd1cSVladimir Sementsov-Ogievskiy * sure that on bs close() we'll previously wait for all timed-out but yet 1286db7fd1cSVladimir Sementsov-Ogievskiy * running block_copy calls. 1296db7fd1cSVladimir Sementsov-Ogievskiy */ 1306db7fd1cSVladimir Sementsov-Ogievskiy bdrv_inc_in_flight(bs); 1316db7fd1cSVladimir Sementsov-Ogievskiy ret = block_copy(s->bcs, off, end - off, true, s->cbw_timeout_ns, 1326db7fd1cSVladimir Sementsov-Ogievskiy block_copy_cb, bs); 133f1bb39a8SVladimir Sementsov-Ogievskiy if (ret < 0 && s->on_cbw_error == ON_CBW_ERROR_BREAK_GUEST_WRITE) { 134af5bcd77SVladimir Sementsov-Ogievskiy return ret; 135af5bcd77SVladimir Sementsov-Ogievskiy } 136af5bcd77SVladimir Sementsov-Ogievskiy 137af5bcd77SVladimir Sementsov-Ogievskiy WITH_QEMU_LOCK_GUARD(&s->lock) { 138f1bb39a8SVladimir Sementsov-Ogievskiy if (ret < 0) { 139f1bb39a8SVladimir Sementsov-Ogievskiy assert(s->on_cbw_error == ON_CBW_ERROR_BREAK_SNAPSHOT); 140f1bb39a8SVladimir Sementsov-Ogievskiy if (!s->snapshot_error) { 141f1bb39a8SVladimir Sementsov-Ogievskiy s->snapshot_error = ret; 142f1bb39a8SVladimir Sementsov-Ogievskiy } 143f1bb39a8SVladimir Sementsov-Ogievskiy } else { 144af5bcd77SVladimir Sementsov-Ogievskiy bdrv_set_dirty_bitmap(s->done_bitmap, off, end - off); 145f1bb39a8SVladimir Sementsov-Ogievskiy } 146af5bcd77SVladimir Sementsov-Ogievskiy reqlist_wait_all(&s->frozen_read_reqs, off, end - off, &s->lock); 147af5bcd77SVladimir Sementsov-Ogievskiy } 148af5bcd77SVladimir Sementsov-Ogievskiy 149af5bcd77SVladimir Sementsov-Ogievskiy return 0; 150d003e0aeSVladimir Sementsov-Ogievskiy } 151d003e0aeSVladimir Sementsov-Ogievskiy 1529a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 1539a5a1c62SEmanuele Giuseppe Esposito cbw_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes) 154d003e0aeSVladimir Sementsov-Ogievskiy { 155d003e0aeSVladimir Sementsov-Ogievskiy int ret = cbw_do_copy_before_write(bs, offset, bytes, 0); 156d003e0aeSVladimir Sementsov-Ogievskiy if (ret < 0) { 157d003e0aeSVladimir Sementsov-Ogievskiy return ret; 158d003e0aeSVladimir Sementsov-Ogievskiy } 159d003e0aeSVladimir Sementsov-Ogievskiy 1603c1e6327SVladimir Sementsov-Ogievskiy return bdrv_co_pdiscard(bs->file, offset, bytes); 161d003e0aeSVladimir Sementsov-Ogievskiy } 162d003e0aeSVladimir Sementsov-Ogievskiy 163abaf8b75SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 164abaf8b75SKevin Wolf cbw_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, 165abaf8b75SKevin Wolf BdrvRequestFlags flags) 166d003e0aeSVladimir Sementsov-Ogievskiy { 167d003e0aeSVladimir Sementsov-Ogievskiy int ret = cbw_do_copy_before_write(bs, offset, bytes, flags); 168d003e0aeSVladimir Sementsov-Ogievskiy if (ret < 0) { 169d003e0aeSVladimir Sementsov-Ogievskiy return ret; 170d003e0aeSVladimir Sementsov-Ogievskiy } 171d003e0aeSVladimir Sementsov-Ogievskiy 1723c1e6327SVladimir Sementsov-Ogievskiy return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); 173d003e0aeSVladimir Sementsov-Ogievskiy } 174d003e0aeSVladimir Sementsov-Ogievskiy 175b9b10c35SKevin Wolf static coroutine_fn GRAPH_RDLOCK 176b9b10c35SKevin Wolf int cbw_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, 177b9b10c35SKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags) 178d003e0aeSVladimir Sementsov-Ogievskiy { 179d003e0aeSVladimir Sementsov-Ogievskiy int ret = cbw_do_copy_before_write(bs, offset, bytes, flags); 180d003e0aeSVladimir Sementsov-Ogievskiy if (ret < 0) { 181d003e0aeSVladimir Sementsov-Ogievskiy return ret; 182d003e0aeSVladimir Sementsov-Ogievskiy } 183d003e0aeSVladimir Sementsov-Ogievskiy 1843c1e6327SVladimir Sementsov-Ogievskiy return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); 185d003e0aeSVladimir Sementsov-Ogievskiy } 186d003e0aeSVladimir Sementsov-Ogievskiy 18788095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK cbw_co_flush(BlockDriverState *bs) 188d003e0aeSVladimir Sementsov-Ogievskiy { 1893c1e6327SVladimir Sementsov-Ogievskiy if (!bs->file) { 190d003e0aeSVladimir Sementsov-Ogievskiy return 0; 191d003e0aeSVladimir Sementsov-Ogievskiy } 192d003e0aeSVladimir Sementsov-Ogievskiy 1933c1e6327SVladimir Sementsov-Ogievskiy return bdrv_co_flush(bs->file->bs); 194d003e0aeSVladimir Sementsov-Ogievskiy } 195d003e0aeSVladimir Sementsov-Ogievskiy 196af5bcd77SVladimir Sementsov-Ogievskiy /* 197af5bcd77SVladimir Sementsov-Ogievskiy * If @offset not accessible - return NULL. 198af5bcd77SVladimir Sementsov-Ogievskiy * 199af5bcd77SVladimir Sementsov-Ogievskiy * Otherwise, set @pnum to some bytes that accessible from @file (@file is set 200af5bcd77SVladimir Sementsov-Ogievskiy * to bs->file or to s->target). Return newly allocated BlockReq object that 201af5bcd77SVladimir Sementsov-Ogievskiy * should be than passed to cbw_snapshot_read_unlock(). 202af5bcd77SVladimir Sementsov-Ogievskiy * 203af5bcd77SVladimir Sementsov-Ogievskiy * It's guaranteed that guest writes will not interact in the region until 204af5bcd77SVladimir Sementsov-Ogievskiy * cbw_snapshot_read_unlock() called. 205af5bcd77SVladimir Sementsov-Ogievskiy */ 2063e614042SPaolo Bonzini static coroutine_fn BlockReq * 2073e614042SPaolo Bonzini cbw_snapshot_read_lock(BlockDriverState *bs, int64_t offset, int64_t bytes, 208af5bcd77SVladimir Sementsov-Ogievskiy int64_t *pnum, BdrvChild **file) 209af5bcd77SVladimir Sementsov-Ogievskiy { 210af5bcd77SVladimir Sementsov-Ogievskiy BDRVCopyBeforeWriteState *s = bs->opaque; 211af5bcd77SVladimir Sementsov-Ogievskiy BlockReq *req = g_new(BlockReq, 1); 212af5bcd77SVladimir Sementsov-Ogievskiy bool done; 213af5bcd77SVladimir Sementsov-Ogievskiy 214af5bcd77SVladimir Sementsov-Ogievskiy QEMU_LOCK_GUARD(&s->lock); 215af5bcd77SVladimir Sementsov-Ogievskiy 216f1bb39a8SVladimir Sementsov-Ogievskiy if (s->snapshot_error) { 217f1bb39a8SVladimir Sementsov-Ogievskiy g_free(req); 218f1bb39a8SVladimir Sementsov-Ogievskiy return NULL; 219f1bb39a8SVladimir Sementsov-Ogievskiy } 220f1bb39a8SVladimir Sementsov-Ogievskiy 221af5bcd77SVladimir Sementsov-Ogievskiy if (bdrv_dirty_bitmap_next_zero(s->access_bitmap, offset, bytes) != -1) { 222af5bcd77SVladimir Sementsov-Ogievskiy g_free(req); 223af5bcd77SVladimir Sementsov-Ogievskiy return NULL; 224af5bcd77SVladimir Sementsov-Ogievskiy } 225af5bcd77SVladimir Sementsov-Ogievskiy 226af5bcd77SVladimir Sementsov-Ogievskiy done = bdrv_dirty_bitmap_status(s->done_bitmap, offset, bytes, pnum); 227af5bcd77SVladimir Sementsov-Ogievskiy if (done) { 228af5bcd77SVladimir Sementsov-Ogievskiy /* 229af5bcd77SVladimir Sementsov-Ogievskiy * Special invalid BlockReq, that is handled in 230af5bcd77SVladimir Sementsov-Ogievskiy * cbw_snapshot_read_unlock(). We don't need to lock something to read 231af5bcd77SVladimir Sementsov-Ogievskiy * from s->target. 232af5bcd77SVladimir Sementsov-Ogievskiy */ 233af5bcd77SVladimir Sementsov-Ogievskiy *req = (BlockReq) {.offset = -1, .bytes = -1}; 234af5bcd77SVladimir Sementsov-Ogievskiy *file = s->target; 235af5bcd77SVladimir Sementsov-Ogievskiy } else { 236af5bcd77SVladimir Sementsov-Ogievskiy reqlist_init_req(&s->frozen_read_reqs, req, offset, bytes); 237af5bcd77SVladimir Sementsov-Ogievskiy *file = bs->file; 238af5bcd77SVladimir Sementsov-Ogievskiy } 239af5bcd77SVladimir Sementsov-Ogievskiy 240af5bcd77SVladimir Sementsov-Ogievskiy return req; 241af5bcd77SVladimir Sementsov-Ogievskiy } 242af5bcd77SVladimir Sementsov-Ogievskiy 2433e614042SPaolo Bonzini static coroutine_fn void 2443e614042SPaolo Bonzini cbw_snapshot_read_unlock(BlockDriverState *bs, BlockReq *req) 245af5bcd77SVladimir Sementsov-Ogievskiy { 246af5bcd77SVladimir Sementsov-Ogievskiy BDRVCopyBeforeWriteState *s = bs->opaque; 247af5bcd77SVladimir Sementsov-Ogievskiy 248af5bcd77SVladimir Sementsov-Ogievskiy if (req->offset == -1 && req->bytes == -1) { 249af5bcd77SVladimir Sementsov-Ogievskiy g_free(req); 250af5bcd77SVladimir Sementsov-Ogievskiy return; 251af5bcd77SVladimir Sementsov-Ogievskiy } 252af5bcd77SVladimir Sementsov-Ogievskiy 253af5bcd77SVladimir Sementsov-Ogievskiy QEMU_LOCK_GUARD(&s->lock); 254af5bcd77SVladimir Sementsov-Ogievskiy 255af5bcd77SVladimir Sementsov-Ogievskiy reqlist_remove_req(req); 256af5bcd77SVladimir Sementsov-Ogievskiy g_free(req); 257af5bcd77SVladimir Sementsov-Ogievskiy } 258af5bcd77SVladimir Sementsov-Ogievskiy 2597b9e8b22SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 260af5bcd77SVladimir Sementsov-Ogievskiy cbw_co_preadv_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes, 261af5bcd77SVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, size_t qiov_offset) 262af5bcd77SVladimir Sementsov-Ogievskiy { 263af5bcd77SVladimir Sementsov-Ogievskiy BlockReq *req; 264af5bcd77SVladimir Sementsov-Ogievskiy BdrvChild *file; 265af5bcd77SVladimir Sementsov-Ogievskiy int ret; 266af5bcd77SVladimir Sementsov-Ogievskiy 267af5bcd77SVladimir Sementsov-Ogievskiy /* TODO: upgrade to async loop using AioTask */ 268af5bcd77SVladimir Sementsov-Ogievskiy while (bytes) { 269af5bcd77SVladimir Sementsov-Ogievskiy int64_t cur_bytes; 270af5bcd77SVladimir Sementsov-Ogievskiy 271af5bcd77SVladimir Sementsov-Ogievskiy req = cbw_snapshot_read_lock(bs, offset, bytes, &cur_bytes, &file); 272af5bcd77SVladimir Sementsov-Ogievskiy if (!req) { 273af5bcd77SVladimir Sementsov-Ogievskiy return -EACCES; 274af5bcd77SVladimir Sementsov-Ogievskiy } 275af5bcd77SVladimir Sementsov-Ogievskiy 276af5bcd77SVladimir Sementsov-Ogievskiy ret = bdrv_co_preadv_part(file, offset, cur_bytes, 277af5bcd77SVladimir Sementsov-Ogievskiy qiov, qiov_offset, 0); 278af5bcd77SVladimir Sementsov-Ogievskiy cbw_snapshot_read_unlock(bs, req); 279af5bcd77SVladimir Sementsov-Ogievskiy if (ret < 0) { 280af5bcd77SVladimir Sementsov-Ogievskiy return ret; 281af5bcd77SVladimir Sementsov-Ogievskiy } 282af5bcd77SVladimir Sementsov-Ogievskiy 283af5bcd77SVladimir Sementsov-Ogievskiy bytes -= cur_bytes; 284af5bcd77SVladimir Sementsov-Ogievskiy offset += cur_bytes; 285af5bcd77SVladimir Sementsov-Ogievskiy qiov_offset += cur_bytes; 286af5bcd77SVladimir Sementsov-Ogievskiy } 287af5bcd77SVladimir Sementsov-Ogievskiy 288af5bcd77SVladimir Sementsov-Ogievskiy return 0; 289af5bcd77SVladimir Sementsov-Ogievskiy } 290af5bcd77SVladimir Sementsov-Ogievskiy 2917b9e8b22SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 292af5bcd77SVladimir Sementsov-Ogievskiy cbw_co_snapshot_block_status(BlockDriverState *bs, 293af5bcd77SVladimir Sementsov-Ogievskiy bool want_zero, int64_t offset, int64_t bytes, 294af5bcd77SVladimir Sementsov-Ogievskiy int64_t *pnum, int64_t *map, 295af5bcd77SVladimir Sementsov-Ogievskiy BlockDriverState **file) 296af5bcd77SVladimir Sementsov-Ogievskiy { 297af5bcd77SVladimir Sementsov-Ogievskiy BDRVCopyBeforeWriteState *s = bs->opaque; 298af5bcd77SVladimir Sementsov-Ogievskiy BlockReq *req; 299af5bcd77SVladimir Sementsov-Ogievskiy int ret; 300af5bcd77SVladimir Sementsov-Ogievskiy int64_t cur_bytes; 301af5bcd77SVladimir Sementsov-Ogievskiy BdrvChild *child; 302af5bcd77SVladimir Sementsov-Ogievskiy 303af5bcd77SVladimir Sementsov-Ogievskiy req = cbw_snapshot_read_lock(bs, offset, bytes, &cur_bytes, &child); 304af5bcd77SVladimir Sementsov-Ogievskiy if (!req) { 305af5bcd77SVladimir Sementsov-Ogievskiy return -EACCES; 306af5bcd77SVladimir Sementsov-Ogievskiy } 307af5bcd77SVladimir Sementsov-Ogievskiy 308af5bcd77SVladimir Sementsov-Ogievskiy ret = bdrv_block_status(child->bs, offset, cur_bytes, pnum, map, file); 309af5bcd77SVladimir Sementsov-Ogievskiy if (child == s->target) { 310af5bcd77SVladimir Sementsov-Ogievskiy /* 311af5bcd77SVladimir Sementsov-Ogievskiy * We refer to s->target only for areas that we've written to it. 312af5bcd77SVladimir Sementsov-Ogievskiy * And we can not report unallocated blocks in s->target: this will 313af5bcd77SVladimir Sementsov-Ogievskiy * break generic block-status-above logic, that will go to 314af5bcd77SVladimir Sementsov-Ogievskiy * copy-before-write filtered child in this case. 315af5bcd77SVladimir Sementsov-Ogievskiy */ 316af5bcd77SVladimir Sementsov-Ogievskiy assert(ret & BDRV_BLOCK_ALLOCATED); 317af5bcd77SVladimir Sementsov-Ogievskiy } 318af5bcd77SVladimir Sementsov-Ogievskiy 319af5bcd77SVladimir Sementsov-Ogievskiy cbw_snapshot_read_unlock(bs, req); 320af5bcd77SVladimir Sementsov-Ogievskiy 321af5bcd77SVladimir Sementsov-Ogievskiy return ret; 322af5bcd77SVladimir Sementsov-Ogievskiy } 323af5bcd77SVladimir Sementsov-Ogievskiy 3249a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 3259a5a1c62SEmanuele Giuseppe Esposito cbw_co_pdiscard_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes) 326af5bcd77SVladimir Sementsov-Ogievskiy { 327af5bcd77SVladimir Sementsov-Ogievskiy BDRVCopyBeforeWriteState *s = bs->opaque; 328af5bcd77SVladimir Sementsov-Ogievskiy 329af5bcd77SVladimir Sementsov-Ogievskiy WITH_QEMU_LOCK_GUARD(&s->lock) { 330af5bcd77SVladimir Sementsov-Ogievskiy bdrv_reset_dirty_bitmap(s->access_bitmap, offset, bytes); 331af5bcd77SVladimir Sementsov-Ogievskiy } 332af5bcd77SVladimir Sementsov-Ogievskiy 333af5bcd77SVladimir Sementsov-Ogievskiy block_copy_reset(s->bcs, offset, bytes); 334af5bcd77SVladimir Sementsov-Ogievskiy 335af5bcd77SVladimir Sementsov-Ogievskiy return bdrv_co_pdiscard(s->target, offset, bytes); 336af5bcd77SVladimir Sementsov-Ogievskiy } 337af5bcd77SVladimir Sementsov-Ogievskiy 338d003e0aeSVladimir Sementsov-Ogievskiy static void cbw_refresh_filename(BlockDriverState *bs) 339d003e0aeSVladimir Sementsov-Ogievskiy { 340d003e0aeSVladimir Sementsov-Ogievskiy pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), 3413c1e6327SVladimir Sementsov-Ogievskiy bs->file->bs->filename); 342d003e0aeSVladimir Sementsov-Ogievskiy } 343d003e0aeSVladimir Sementsov-Ogievskiy 344*c629b6d2SKevin Wolf static void GRAPH_RDLOCK 345*c629b6d2SKevin Wolf cbw_child_perm(BlockDriverState *bs, BdrvChild *c, BdrvChildRole role, 346d003e0aeSVladimir Sementsov-Ogievskiy BlockReopenQueue *reopen_queue, 347d003e0aeSVladimir Sementsov-Ogievskiy uint64_t perm, uint64_t shared, 348d003e0aeSVladimir Sementsov-Ogievskiy uint64_t *nperm, uint64_t *nshared) 349d003e0aeSVladimir Sementsov-Ogievskiy { 350d003e0aeSVladimir Sementsov-Ogievskiy if (!(role & BDRV_CHILD_FILTERED)) { 351d003e0aeSVladimir Sementsov-Ogievskiy /* 352d003e0aeSVladimir Sementsov-Ogievskiy * Target child 353d003e0aeSVladimir Sementsov-Ogievskiy * 354d003e0aeSVladimir Sementsov-Ogievskiy * Share write to target (child_file), to not interfere 355d003e0aeSVladimir Sementsov-Ogievskiy * with guest writes to its disk which may be in target backing chain. 356d003e0aeSVladimir Sementsov-Ogievskiy * Can't resize during a backup block job because we check the size 357d003e0aeSVladimir Sementsov-Ogievskiy * only upfront. 358d003e0aeSVladimir Sementsov-Ogievskiy */ 359d003e0aeSVladimir Sementsov-Ogievskiy *nshared = BLK_PERM_ALL & ~BLK_PERM_RESIZE; 360d003e0aeSVladimir Sementsov-Ogievskiy *nperm = BLK_PERM_WRITE; 361d003e0aeSVladimir Sementsov-Ogievskiy } else { 362d003e0aeSVladimir Sementsov-Ogievskiy /* Source child */ 363d003e0aeSVladimir Sementsov-Ogievskiy bdrv_default_perms(bs, c, role, reopen_queue, 364d003e0aeSVladimir Sementsov-Ogievskiy perm, shared, nperm, nshared); 365d003e0aeSVladimir Sementsov-Ogievskiy 3663860c020SVladimir Sementsov-Ogievskiy if (!QLIST_EMPTY(&bs->parents)) { 367d003e0aeSVladimir Sementsov-Ogievskiy if (perm & BLK_PERM_WRITE) { 368d003e0aeSVladimir Sementsov-Ogievskiy *nperm = *nperm | BLK_PERM_CONSISTENT_READ; 369d003e0aeSVladimir Sementsov-Ogievskiy } 370d003e0aeSVladimir Sementsov-Ogievskiy *nshared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE); 371d003e0aeSVladimir Sementsov-Ogievskiy } 372d003e0aeSVladimir Sementsov-Ogievskiy } 3733860c020SVladimir Sementsov-Ogievskiy } 374d003e0aeSVladimir Sementsov-Ogievskiy 37579ef0cebSVladimir Sementsov-Ogievskiy static BlockdevOptions *cbw_parse_options(QDict *options, Error **errp) 3765f3a3cd7SVladimir Sementsov-Ogievskiy { 37779ef0cebSVladimir Sementsov-Ogievskiy BlockdevOptions *opts = NULL; 3785f3a3cd7SVladimir Sementsov-Ogievskiy Visitor *v = NULL; 3795f3a3cd7SVladimir Sementsov-Ogievskiy 38079ef0cebSVladimir Sementsov-Ogievskiy qdict_put_str(options, "driver", "copy-before-write"); 3815f3a3cd7SVladimir Sementsov-Ogievskiy 38279ef0cebSVladimir Sementsov-Ogievskiy v = qobject_input_visitor_new_flat_confused(options, errp); 3835f3a3cd7SVladimir Sementsov-Ogievskiy if (!v) { 3845f3a3cd7SVladimir Sementsov-Ogievskiy goto out; 3855f3a3cd7SVladimir Sementsov-Ogievskiy } 3865f3a3cd7SVladimir Sementsov-Ogievskiy 38779ef0cebSVladimir Sementsov-Ogievskiy visit_type_BlockdevOptions(v, NULL, &opts, errp); 38879ef0cebSVladimir Sementsov-Ogievskiy if (!opts) { 3895f3a3cd7SVladimir Sementsov-Ogievskiy goto out; 3905f3a3cd7SVladimir Sementsov-Ogievskiy } 3915f3a3cd7SVladimir Sementsov-Ogievskiy 39279ef0cebSVladimir Sementsov-Ogievskiy /* 39379ef0cebSVladimir Sementsov-Ogievskiy * Delete options which we are going to parse through BlockdevOptions 39479ef0cebSVladimir Sementsov-Ogievskiy * object for original options. 39579ef0cebSVladimir Sementsov-Ogievskiy */ 39679ef0cebSVladimir Sementsov-Ogievskiy qdict_extract_subqdict(options, NULL, "bitmap"); 397f1bb39a8SVladimir Sementsov-Ogievskiy qdict_del(options, "on-cbw-error"); 3986db7fd1cSVladimir Sementsov-Ogievskiy qdict_del(options, "cbw-timeout"); 3995f3a3cd7SVladimir Sementsov-Ogievskiy 4005f3a3cd7SVladimir Sementsov-Ogievskiy out: 4015f3a3cd7SVladimir Sementsov-Ogievskiy visit_free(v); 40279ef0cebSVladimir Sementsov-Ogievskiy qdict_del(options, "driver"); 4035f3a3cd7SVladimir Sementsov-Ogievskiy 40479ef0cebSVladimir Sementsov-Ogievskiy return opts; 4055f3a3cd7SVladimir Sementsov-Ogievskiy } 4065f3a3cd7SVladimir Sementsov-Ogievskiy 407751cec7aSVladimir Sementsov-Ogievskiy static int cbw_open(BlockDriverState *bs, QDict *options, int flags, 408751cec7aSVladimir Sementsov-Ogievskiy Error **errp) 4091f0cacb9SVladimir Sementsov-Ogievskiy { 410fe7ea40cSVladimir Sementsov-Ogievskiy BDRVCopyBeforeWriteState *s = bs->opaque; 4115f3a3cd7SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap = NULL; 412af5bcd77SVladimir Sementsov-Ogievskiy int64_t cluster_size; 41379ef0cebSVladimir Sementsov-Ogievskiy g_autoptr(BlockdevOptions) full_opts = NULL; 41479ef0cebSVladimir Sementsov-Ogievskiy BlockdevOptionsCbw *opts; 4159102f2ebSKevin Wolf AioContext *ctx; 41683930780SVladimir Sementsov-Ogievskiy int ret; 41779ef0cebSVladimir Sementsov-Ogievskiy 41879ef0cebSVladimir Sementsov-Ogievskiy full_opts = cbw_parse_options(options, errp); 41979ef0cebSVladimir Sementsov-Ogievskiy if (!full_opts) { 42079ef0cebSVladimir Sementsov-Ogievskiy return -EINVAL; 42179ef0cebSVladimir Sementsov-Ogievskiy } 42279ef0cebSVladimir Sementsov-Ogievskiy assert(full_opts->driver == BLOCKDEV_DRIVER_COPY_BEFORE_WRITE); 42379ef0cebSVladimir Sementsov-Ogievskiy opts = &full_opts->u.copy_before_write; 4241f0cacb9SVladimir Sementsov-Ogievskiy 42583930780SVladimir Sementsov-Ogievskiy ret = bdrv_open_file_child(NULL, options, "file", bs, errp); 42683930780SVladimir Sementsov-Ogievskiy if (ret < 0) { 42783930780SVladimir Sementsov-Ogievskiy return ret; 4281f0cacb9SVladimir Sementsov-Ogievskiy } 4291f0cacb9SVladimir Sementsov-Ogievskiy 430f44fd739SVladimir Sementsov-Ogievskiy s->target = bdrv_open_child(NULL, options, "target", bs, &child_of_bds, 431f44fd739SVladimir Sementsov-Ogievskiy BDRV_CHILD_DATA, false, errp); 432f44fd739SVladimir Sementsov-Ogievskiy if (!s->target) { 4331f0cacb9SVladimir Sementsov-Ogievskiy return -EINVAL; 4341f0cacb9SVladimir Sementsov-Ogievskiy } 4351f0cacb9SVladimir Sementsov-Ogievskiy 4369102f2ebSKevin Wolf ctx = bdrv_get_aio_context(bs); 4379102f2ebSKevin Wolf aio_context_acquire(ctx); 4389102f2ebSKevin Wolf 43954fde4ffSMarkus Armbruster if (opts->bitmap) { 44079ef0cebSVladimir Sementsov-Ogievskiy bitmap = block_dirty_bitmap_lookup(opts->bitmap->node, 44179ef0cebSVladimir Sementsov-Ogievskiy opts->bitmap->name, NULL, errp); 44279ef0cebSVladimir Sementsov-Ogievskiy if (!bitmap) { 4439102f2ebSKevin Wolf ret = -EINVAL; 4449102f2ebSKevin Wolf goto out; 4455f3a3cd7SVladimir Sementsov-Ogievskiy } 44679ef0cebSVladimir Sementsov-Ogievskiy } 447f1bb39a8SVladimir Sementsov-Ogievskiy s->on_cbw_error = opts->has_on_cbw_error ? opts->on_cbw_error : 448f1bb39a8SVladimir Sementsov-Ogievskiy ON_CBW_ERROR_BREAK_GUEST_WRITE; 4496db7fd1cSVladimir Sementsov-Ogievskiy s->cbw_timeout_ns = opts->has_cbw_timeout ? 4506db7fd1cSVladimir Sementsov-Ogievskiy opts->cbw_timeout * NANOSECONDS_PER_SECOND : 0; 4515f3a3cd7SVladimir Sementsov-Ogievskiy 4525a507426SVladimir Sementsov-Ogievskiy bs->total_sectors = bs->file->bs->total_sectors; 4535a507426SVladimir Sementsov-Ogievskiy bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED | 4545a507426SVladimir Sementsov-Ogievskiy (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); 4555a507426SVladimir Sementsov-Ogievskiy bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | 4565a507426SVladimir Sementsov-Ogievskiy ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) & 4575a507426SVladimir Sementsov-Ogievskiy bs->file->bs->supported_zero_flags); 4585a507426SVladimir Sementsov-Ogievskiy 4595f3a3cd7SVladimir Sementsov-Ogievskiy s->bcs = block_copy_state_new(bs->file, s->target, bitmap, errp); 460fe7ea40cSVladimir Sementsov-Ogievskiy if (!s->bcs) { 4611f0cacb9SVladimir Sementsov-Ogievskiy error_prepend(errp, "Cannot create block-copy-state: "); 4629102f2ebSKevin Wolf ret = -EINVAL; 4639102f2ebSKevin Wolf goto out; 4641f0cacb9SVladimir Sementsov-Ogievskiy } 4651f0cacb9SVladimir Sementsov-Ogievskiy 466af5bcd77SVladimir Sementsov-Ogievskiy cluster_size = block_copy_cluster_size(s->bcs); 467af5bcd77SVladimir Sementsov-Ogievskiy 468af5bcd77SVladimir Sementsov-Ogievskiy s->done_bitmap = bdrv_create_dirty_bitmap(bs, cluster_size, NULL, errp); 469af5bcd77SVladimir Sementsov-Ogievskiy if (!s->done_bitmap) { 4709102f2ebSKevin Wolf ret = -EINVAL; 4719102f2ebSKevin Wolf goto out; 472af5bcd77SVladimir Sementsov-Ogievskiy } 473af5bcd77SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(s->done_bitmap); 474af5bcd77SVladimir Sementsov-Ogievskiy 475af5bcd77SVladimir Sementsov-Ogievskiy /* s->access_bitmap starts equal to bcs bitmap */ 476af5bcd77SVladimir Sementsov-Ogievskiy s->access_bitmap = bdrv_create_dirty_bitmap(bs, cluster_size, NULL, errp); 477af5bcd77SVladimir Sementsov-Ogievskiy if (!s->access_bitmap) { 4789102f2ebSKevin Wolf ret = -EINVAL; 4799102f2ebSKevin Wolf goto out; 480af5bcd77SVladimir Sementsov-Ogievskiy } 481af5bcd77SVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(s->access_bitmap); 482af5bcd77SVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_merge_internal(s->access_bitmap, 483af5bcd77SVladimir Sementsov-Ogievskiy block_copy_dirty_bitmap(s->bcs), NULL, 484af5bcd77SVladimir Sementsov-Ogievskiy true); 485af5bcd77SVladimir Sementsov-Ogievskiy 486af5bcd77SVladimir Sementsov-Ogievskiy qemu_co_mutex_init(&s->lock); 487af5bcd77SVladimir Sementsov-Ogievskiy QLIST_INIT(&s->frozen_read_reqs); 488af5bcd77SVladimir Sementsov-Ogievskiy 4899102f2ebSKevin Wolf ret = 0; 4909102f2ebSKevin Wolf out: 4919102f2ebSKevin Wolf aio_context_release(ctx); 4929102f2ebSKevin Wolf return ret; 4931f0cacb9SVladimir Sementsov-Ogievskiy } 4941f0cacb9SVladimir Sementsov-Ogievskiy 495751cec7aSVladimir Sementsov-Ogievskiy static void cbw_close(BlockDriverState *bs) 496751cec7aSVladimir Sementsov-Ogievskiy { 497751cec7aSVladimir Sementsov-Ogievskiy BDRVCopyBeforeWriteState *s = bs->opaque; 498751cec7aSVladimir Sementsov-Ogievskiy 499af5bcd77SVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(s->access_bitmap); 500af5bcd77SVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(s->done_bitmap); 501af5bcd77SVladimir Sementsov-Ogievskiy 502751cec7aSVladimir Sementsov-Ogievskiy block_copy_state_free(s->bcs); 503751cec7aSVladimir Sementsov-Ogievskiy s->bcs = NULL; 504751cec7aSVladimir Sementsov-Ogievskiy } 505751cec7aSVladimir Sementsov-Ogievskiy 5069ea473fbSKevin Wolf static BlockDriver bdrv_cbw_filter = { 507d003e0aeSVladimir Sementsov-Ogievskiy .format_name = "copy-before-write", 508d003e0aeSVladimir Sementsov-Ogievskiy .instance_size = sizeof(BDRVCopyBeforeWriteState), 509d003e0aeSVladimir Sementsov-Ogievskiy 510751cec7aSVladimir Sementsov-Ogievskiy .bdrv_open = cbw_open, 511751cec7aSVladimir Sementsov-Ogievskiy .bdrv_close = cbw_close, 512751cec7aSVladimir Sementsov-Ogievskiy 513d003e0aeSVladimir Sementsov-Ogievskiy .bdrv_co_preadv = cbw_co_preadv, 514d003e0aeSVladimir Sementsov-Ogievskiy .bdrv_co_pwritev = cbw_co_pwritev, 515d003e0aeSVladimir Sementsov-Ogievskiy .bdrv_co_pwrite_zeroes = cbw_co_pwrite_zeroes, 516d003e0aeSVladimir Sementsov-Ogievskiy .bdrv_co_pdiscard = cbw_co_pdiscard, 517d003e0aeSVladimir Sementsov-Ogievskiy .bdrv_co_flush = cbw_co_flush, 518d003e0aeSVladimir Sementsov-Ogievskiy 519af5bcd77SVladimir Sementsov-Ogievskiy .bdrv_co_preadv_snapshot = cbw_co_preadv_snapshot, 520af5bcd77SVladimir Sementsov-Ogievskiy .bdrv_co_pdiscard_snapshot = cbw_co_pdiscard_snapshot, 521af5bcd77SVladimir Sementsov-Ogievskiy .bdrv_co_snapshot_block_status = cbw_co_snapshot_block_status, 522af5bcd77SVladimir Sementsov-Ogievskiy 523d003e0aeSVladimir Sementsov-Ogievskiy .bdrv_refresh_filename = cbw_refresh_filename, 524d003e0aeSVladimir Sementsov-Ogievskiy 525d003e0aeSVladimir Sementsov-Ogievskiy .bdrv_child_perm = cbw_child_perm, 526d003e0aeSVladimir Sementsov-Ogievskiy 527d003e0aeSVladimir Sementsov-Ogievskiy .is_filter = true, 528d003e0aeSVladimir Sementsov-Ogievskiy }; 529d003e0aeSVladimir Sementsov-Ogievskiy 530d003e0aeSVladimir Sementsov-Ogievskiy BlockDriverState *bdrv_cbw_append(BlockDriverState *source, 531d003e0aeSVladimir Sementsov-Ogievskiy BlockDriverState *target, 532d003e0aeSVladimir Sementsov-Ogievskiy const char *filter_node_name, 533d003e0aeSVladimir Sementsov-Ogievskiy BlockCopyState **bcs, 534d003e0aeSVladimir Sementsov-Ogievskiy Error **errp) 535d003e0aeSVladimir Sementsov-Ogievskiy { 536d003e0aeSVladimir Sementsov-Ogievskiy BDRVCopyBeforeWriteState *state; 537d003e0aeSVladimir Sementsov-Ogievskiy BlockDriverState *top; 538f44fd739SVladimir Sementsov-Ogievskiy QDict *opts; 539d003e0aeSVladimir Sementsov-Ogievskiy 540d003e0aeSVladimir Sementsov-Ogievskiy assert(source->total_sectors == target->total_sectors); 541377cc15bSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE(); 542d003e0aeSVladimir Sementsov-Ogievskiy 543f44fd739SVladimir Sementsov-Ogievskiy opts = qdict_new(); 544751cec7aSVladimir Sementsov-Ogievskiy qdict_put_str(opts, "driver", "copy-before-write"); 545751cec7aSVladimir Sementsov-Ogievskiy if (filter_node_name) { 546751cec7aSVladimir Sementsov-Ogievskiy qdict_put_str(opts, "node-name", filter_node_name); 547751cec7aSVladimir Sementsov-Ogievskiy } 548f44fd739SVladimir Sementsov-Ogievskiy qdict_put_str(opts, "file", bdrv_get_node_name(source)); 549f44fd739SVladimir Sementsov-Ogievskiy qdict_put_str(opts, "target", bdrv_get_node_name(target)); 550f44fd739SVladimir Sementsov-Ogievskiy 551751cec7aSVladimir Sementsov-Ogievskiy top = bdrv_insert_node(source, opts, BDRV_O_RDWR, errp); 552751cec7aSVladimir Sementsov-Ogievskiy if (!top) { 553751cec7aSVladimir Sementsov-Ogievskiy return NULL; 554d003e0aeSVladimir Sementsov-Ogievskiy } 555d003e0aeSVladimir Sementsov-Ogievskiy 556751cec7aSVladimir Sementsov-Ogievskiy state = top->opaque; 5577ddbce2dSVladimir Sementsov-Ogievskiy *bcs = state->bcs; 558d003e0aeSVladimir Sementsov-Ogievskiy 559d003e0aeSVladimir Sementsov-Ogievskiy return top; 560d003e0aeSVladimir Sementsov-Ogievskiy } 561d003e0aeSVladimir Sementsov-Ogievskiy 562d003e0aeSVladimir Sementsov-Ogievskiy void bdrv_cbw_drop(BlockDriverState *bs) 563d003e0aeSVladimir Sementsov-Ogievskiy { 564377cc15bSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE(); 565d003e0aeSVladimir Sementsov-Ogievskiy bdrv_drop_filter(bs, &error_abort); 566d003e0aeSVladimir Sementsov-Ogievskiy bdrv_unref(bs); 567d003e0aeSVladimir Sementsov-Ogievskiy } 568751cec7aSVladimir Sementsov-Ogievskiy 569751cec7aSVladimir Sementsov-Ogievskiy static void cbw_init(void) 570751cec7aSVladimir Sementsov-Ogievskiy { 571751cec7aSVladimir Sementsov-Ogievskiy bdrv_register(&bdrv_cbw_filter); 572751cec7aSVladimir Sementsov-Ogievskiy } 573751cec7aSVladimir Sementsov-Ogievskiy 574751cec7aSVladimir Sementsov-Ogievskiy block_init(cbw_init); 575