xref: /openbmc/qemu/block/copy-before-write.c (revision 7eefbf8bb72c1bec0972ca19901207dc6d2acf5a)
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;
4610b1e09eSFiona Ebner     uint64_t cbw_timeout_ns;
470fd05c8dSVladimir Sementsov-Ogievskiy     bool discard_source;
48af5bcd77SVladimir Sementsov-Ogievskiy 
49af5bcd77SVladimir Sementsov-Ogievskiy     /*
50af5bcd77SVladimir Sementsov-Ogievskiy      * @lock: protects access to @access_bitmap, @done_bitmap and
51af5bcd77SVladimir Sementsov-Ogievskiy      * @frozen_read_reqs
52af5bcd77SVladimir Sementsov-Ogievskiy      */
53af5bcd77SVladimir Sementsov-Ogievskiy     CoMutex lock;
54af5bcd77SVladimir Sementsov-Ogievskiy 
55af5bcd77SVladimir Sementsov-Ogievskiy     /*
56af5bcd77SVladimir Sementsov-Ogievskiy      * @access_bitmap: represents areas allowed for reading by fleecing user.
57af5bcd77SVladimir Sementsov-Ogievskiy      * Reading from non-dirty areas leads to -EACCES.
58af5bcd77SVladimir Sementsov-Ogievskiy      */
59af5bcd77SVladimir Sementsov-Ogievskiy     BdrvDirtyBitmap *access_bitmap;
60af5bcd77SVladimir Sementsov-Ogievskiy 
61af5bcd77SVladimir Sementsov-Ogievskiy     /*
62af5bcd77SVladimir Sementsov-Ogievskiy      * @done_bitmap: represents areas that was successfully copied to @target by
63af5bcd77SVladimir Sementsov-Ogievskiy      * copy-before-write operations.
64af5bcd77SVladimir Sementsov-Ogievskiy      */
65af5bcd77SVladimir Sementsov-Ogievskiy     BdrvDirtyBitmap *done_bitmap;
66af5bcd77SVladimir Sementsov-Ogievskiy 
67af5bcd77SVladimir Sementsov-Ogievskiy     /*
68af5bcd77SVladimir Sementsov-Ogievskiy      * @frozen_read_reqs: current read requests for fleecing user in bs->file
69*7eefbf8bSFiona Ebner      * node. These areas must not be rewritten by guest. There can be multiple
70*7eefbf8bSFiona Ebner      * overlapping read requests.
71af5bcd77SVladimir Sementsov-Ogievskiy      */
72af5bcd77SVladimir Sementsov-Ogievskiy     BlockReqList frozen_read_reqs;
73f1bb39a8SVladimir Sementsov-Ogievskiy 
74f1bb39a8SVladimir Sementsov-Ogievskiy     /*
75f1bb39a8SVladimir Sementsov-Ogievskiy      * @snapshot_error is normally zero. But on first copy-before-write failure
76f1bb39a8SVladimir Sementsov-Ogievskiy      * when @on_cbw_error == ON_CBW_ERROR_BREAK_SNAPSHOT, @snapshot_error takes
77f1bb39a8SVladimir Sementsov-Ogievskiy      * value of this error (<0). After that all in-flight and further
78f1bb39a8SVladimir Sementsov-Ogievskiy      * snapshot-API requests will fail with that error.
79f1bb39a8SVladimir Sementsov-Ogievskiy      */
80f1bb39a8SVladimir Sementsov-Ogievskiy     int snapshot_error;
81d003e0aeSVladimir Sementsov-Ogievskiy } BDRVCopyBeforeWriteState;
82d003e0aeSVladimir Sementsov-Ogievskiy 
83b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
cbw_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)84b9b10c35SKevin Wolf cbw_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
85f7ef38ddSVladimir Sementsov-Ogievskiy               QEMUIOVector *qiov, BdrvRequestFlags flags)
86d003e0aeSVladimir Sementsov-Ogievskiy {
873c1e6327SVladimir Sementsov-Ogievskiy     return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
88d003e0aeSVladimir Sementsov-Ogievskiy }
89d003e0aeSVladimir Sementsov-Ogievskiy 
block_copy_cb(void * opaque)906db7fd1cSVladimir Sementsov-Ogievskiy static void block_copy_cb(void *opaque)
916db7fd1cSVladimir Sementsov-Ogievskiy {
926db7fd1cSVladimir Sementsov-Ogievskiy     BlockDriverState *bs = opaque;
936db7fd1cSVladimir Sementsov-Ogievskiy 
946db7fd1cSVladimir Sementsov-Ogievskiy     bdrv_dec_in_flight(bs);
956db7fd1cSVladimir Sementsov-Ogievskiy }
966db7fd1cSVladimir Sementsov-Ogievskiy 
97af5bcd77SVladimir Sementsov-Ogievskiy /*
98af5bcd77SVladimir Sementsov-Ogievskiy  * Do copy-before-write operation.
99af5bcd77SVladimir Sementsov-Ogievskiy  *
100af5bcd77SVladimir Sementsov-Ogievskiy  * On failure guest request must be failed too.
101af5bcd77SVladimir Sementsov-Ogievskiy  *
102af5bcd77SVladimir Sementsov-Ogievskiy  * On success, we also wait for all in-flight fleecing read requests in source
103af5bcd77SVladimir Sementsov-Ogievskiy  * node, and it's guaranteed that after cbw_do_copy_before_write() successful
104af5bcd77SVladimir Sementsov-Ogievskiy  * return there are no such requests and they will never appear.
105af5bcd77SVladimir Sementsov-Ogievskiy  */
cbw_do_copy_before_write(BlockDriverState * bs,uint64_t offset,uint64_t bytes,BdrvRequestFlags flags)106d003e0aeSVladimir Sementsov-Ogievskiy static coroutine_fn int cbw_do_copy_before_write(BlockDriverState *bs,
107d003e0aeSVladimir Sementsov-Ogievskiy         uint64_t offset, uint64_t bytes, BdrvRequestFlags flags)
108d003e0aeSVladimir Sementsov-Ogievskiy {
109d003e0aeSVladimir Sementsov-Ogievskiy     BDRVCopyBeforeWriteState *s = bs->opaque;
110af5bcd77SVladimir Sementsov-Ogievskiy     int ret;
111d003e0aeSVladimir Sementsov-Ogievskiy     uint64_t off, end;
112b518e9e9SVladimir Sementsov-Ogievskiy     int64_t cluster_size = block_copy_cluster_size(s->bcs);
113d003e0aeSVladimir Sementsov-Ogievskiy 
114d003e0aeSVladimir Sementsov-Ogievskiy     if (flags & BDRV_REQ_WRITE_UNCHANGED) {
115d003e0aeSVladimir Sementsov-Ogievskiy         return 0;
116d003e0aeSVladimir Sementsov-Ogievskiy     }
117d003e0aeSVladimir Sementsov-Ogievskiy 
118f1bb39a8SVladimir Sementsov-Ogievskiy     if (s->snapshot_error) {
119f1bb39a8SVladimir Sementsov-Ogievskiy         return 0;
120f1bb39a8SVladimir Sementsov-Ogievskiy     }
121f1bb39a8SVladimir Sementsov-Ogievskiy 
122b518e9e9SVladimir Sementsov-Ogievskiy     off = QEMU_ALIGN_DOWN(offset, cluster_size);
123b518e9e9SVladimir Sementsov-Ogievskiy     end = QEMU_ALIGN_UP(offset + bytes, cluster_size);
124d003e0aeSVladimir Sementsov-Ogievskiy 
1256db7fd1cSVladimir Sementsov-Ogievskiy     /*
1266db7fd1cSVladimir Sementsov-Ogievskiy      * Increase in_flight, so that in case of timed-out block-copy, the
1276db7fd1cSVladimir Sementsov-Ogievskiy      * remaining background block_copy() request (which can't be immediately
1286db7fd1cSVladimir Sementsov-Ogievskiy      * cancelled by timeout) is presented in bs->in_flight. This way we are
1296db7fd1cSVladimir Sementsov-Ogievskiy      * sure that on bs close() we'll previously wait for all timed-out but yet
1306db7fd1cSVladimir Sementsov-Ogievskiy      * running block_copy calls.
1316db7fd1cSVladimir Sementsov-Ogievskiy      */
1326db7fd1cSVladimir Sementsov-Ogievskiy     bdrv_inc_in_flight(bs);
1336db7fd1cSVladimir Sementsov-Ogievskiy     ret = block_copy(s->bcs, off, end - off, true, s->cbw_timeout_ns,
1346db7fd1cSVladimir Sementsov-Ogievskiy                      block_copy_cb, bs);
135f1bb39a8SVladimir Sementsov-Ogievskiy     if (ret < 0 && s->on_cbw_error == ON_CBW_ERROR_BREAK_GUEST_WRITE) {
136af5bcd77SVladimir Sementsov-Ogievskiy         return ret;
137af5bcd77SVladimir Sementsov-Ogievskiy     }
138af5bcd77SVladimir Sementsov-Ogievskiy 
139af5bcd77SVladimir Sementsov-Ogievskiy     WITH_QEMU_LOCK_GUARD(&s->lock) {
140f1bb39a8SVladimir Sementsov-Ogievskiy         if (ret < 0) {
141f1bb39a8SVladimir Sementsov-Ogievskiy             assert(s->on_cbw_error == ON_CBW_ERROR_BREAK_SNAPSHOT);
142f1bb39a8SVladimir Sementsov-Ogievskiy             if (!s->snapshot_error) {
143f1bb39a8SVladimir Sementsov-Ogievskiy                 s->snapshot_error = ret;
144f1bb39a8SVladimir Sementsov-Ogievskiy             }
145f1bb39a8SVladimir Sementsov-Ogievskiy         } else {
146af5bcd77SVladimir Sementsov-Ogievskiy             bdrv_set_dirty_bitmap(s->done_bitmap, off, end - off);
147f1bb39a8SVladimir Sementsov-Ogievskiy         }
148af5bcd77SVladimir Sementsov-Ogievskiy         reqlist_wait_all(&s->frozen_read_reqs, off, end - off, &s->lock);
149af5bcd77SVladimir Sementsov-Ogievskiy     }
150af5bcd77SVladimir Sementsov-Ogievskiy 
151af5bcd77SVladimir Sementsov-Ogievskiy     return 0;
152d003e0aeSVladimir Sementsov-Ogievskiy }
153d003e0aeSVladimir Sementsov-Ogievskiy 
1549a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
cbw_co_pdiscard(BlockDriverState * bs,int64_t offset,int64_t bytes)1559a5a1c62SEmanuele Giuseppe Esposito cbw_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
156d003e0aeSVladimir Sementsov-Ogievskiy {
157d003e0aeSVladimir Sementsov-Ogievskiy     int ret = cbw_do_copy_before_write(bs, offset, bytes, 0);
158d003e0aeSVladimir Sementsov-Ogievskiy     if (ret < 0) {
159d003e0aeSVladimir Sementsov-Ogievskiy         return ret;
160d003e0aeSVladimir Sementsov-Ogievskiy     }
161d003e0aeSVladimir Sementsov-Ogievskiy 
1623c1e6327SVladimir Sementsov-Ogievskiy     return bdrv_co_pdiscard(bs->file, offset, bytes);
163d003e0aeSVladimir Sementsov-Ogievskiy }
164d003e0aeSVladimir Sementsov-Ogievskiy 
165abaf8b75SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
cbw_co_pwrite_zeroes(BlockDriverState * bs,int64_t offset,int64_t bytes,BdrvRequestFlags flags)166abaf8b75SKevin Wolf cbw_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
167abaf8b75SKevin Wolf                      BdrvRequestFlags flags)
168d003e0aeSVladimir Sementsov-Ogievskiy {
169d003e0aeSVladimir Sementsov-Ogievskiy     int ret = cbw_do_copy_before_write(bs, offset, bytes, flags);
170d003e0aeSVladimir Sementsov-Ogievskiy     if (ret < 0) {
171d003e0aeSVladimir Sementsov-Ogievskiy         return ret;
172d003e0aeSVladimir Sementsov-Ogievskiy     }
173d003e0aeSVladimir Sementsov-Ogievskiy 
1743c1e6327SVladimir Sementsov-Ogievskiy     return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
175d003e0aeSVladimir Sementsov-Ogievskiy }
176d003e0aeSVladimir Sementsov-Ogievskiy 
177b9b10c35SKevin Wolf static coroutine_fn GRAPH_RDLOCK
cbw_co_pwritev(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)178b9b10c35SKevin Wolf int cbw_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
179b9b10c35SKevin Wolf                    QEMUIOVector *qiov, BdrvRequestFlags flags)
180d003e0aeSVladimir Sementsov-Ogievskiy {
181d003e0aeSVladimir Sementsov-Ogievskiy     int ret = cbw_do_copy_before_write(bs, offset, bytes, flags);
182d003e0aeSVladimir Sementsov-Ogievskiy     if (ret < 0) {
183d003e0aeSVladimir Sementsov-Ogievskiy         return ret;
184d003e0aeSVladimir Sementsov-Ogievskiy     }
185d003e0aeSVladimir Sementsov-Ogievskiy 
1863c1e6327SVladimir Sementsov-Ogievskiy     return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
187d003e0aeSVladimir Sementsov-Ogievskiy }
188d003e0aeSVladimir Sementsov-Ogievskiy 
cbw_co_flush(BlockDriverState * bs)18988095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK cbw_co_flush(BlockDriverState *bs)
190d003e0aeSVladimir Sementsov-Ogievskiy {
1913c1e6327SVladimir Sementsov-Ogievskiy     if (!bs->file) {
192d003e0aeSVladimir Sementsov-Ogievskiy         return 0;
193d003e0aeSVladimir Sementsov-Ogievskiy     }
194d003e0aeSVladimir Sementsov-Ogievskiy 
1953c1e6327SVladimir Sementsov-Ogievskiy     return bdrv_co_flush(bs->file->bs);
196d003e0aeSVladimir Sementsov-Ogievskiy }
197d003e0aeSVladimir Sementsov-Ogievskiy 
198af5bcd77SVladimir Sementsov-Ogievskiy /*
199af5bcd77SVladimir Sementsov-Ogievskiy  * If @offset not accessible - return NULL.
200af5bcd77SVladimir Sementsov-Ogievskiy  *
201af5bcd77SVladimir Sementsov-Ogievskiy  * Otherwise, set @pnum to some bytes that accessible from @file (@file is set
202af5bcd77SVladimir Sementsov-Ogievskiy  * to bs->file or to s->target). Return newly allocated BlockReq object that
203af5bcd77SVladimir Sementsov-Ogievskiy  * should be than passed to cbw_snapshot_read_unlock().
204af5bcd77SVladimir Sementsov-Ogievskiy  *
205af5bcd77SVladimir Sementsov-Ogievskiy  * It's guaranteed that guest writes will not interact in the region until
206af5bcd77SVladimir Sementsov-Ogievskiy  * cbw_snapshot_read_unlock() called.
207af5bcd77SVladimir Sementsov-Ogievskiy  */
2081f051dcbSKevin Wolf static BlockReq * coroutine_fn GRAPH_RDLOCK
cbw_snapshot_read_lock(BlockDriverState * bs,int64_t offset,int64_t bytes,int64_t * pnum,BdrvChild ** file)2093e614042SPaolo Bonzini cbw_snapshot_read_lock(BlockDriverState *bs, int64_t offset, int64_t bytes,
210af5bcd77SVladimir Sementsov-Ogievskiy                        int64_t *pnum, BdrvChild **file)
211af5bcd77SVladimir Sementsov-Ogievskiy {
212af5bcd77SVladimir Sementsov-Ogievskiy     BDRVCopyBeforeWriteState *s = bs->opaque;
213af5bcd77SVladimir Sementsov-Ogievskiy     BlockReq *req = g_new(BlockReq, 1);
214af5bcd77SVladimir Sementsov-Ogievskiy     bool done;
215af5bcd77SVladimir Sementsov-Ogievskiy 
216af5bcd77SVladimir Sementsov-Ogievskiy     QEMU_LOCK_GUARD(&s->lock);
217af5bcd77SVladimir Sementsov-Ogievskiy 
218f1bb39a8SVladimir Sementsov-Ogievskiy     if (s->snapshot_error) {
219f1bb39a8SVladimir Sementsov-Ogievskiy         g_free(req);
220f1bb39a8SVladimir Sementsov-Ogievskiy         return NULL;
221f1bb39a8SVladimir Sementsov-Ogievskiy     }
222f1bb39a8SVladimir Sementsov-Ogievskiy 
223af5bcd77SVladimir Sementsov-Ogievskiy     if (bdrv_dirty_bitmap_next_zero(s->access_bitmap, offset, bytes) != -1) {
224af5bcd77SVladimir Sementsov-Ogievskiy         g_free(req);
225af5bcd77SVladimir Sementsov-Ogievskiy         return NULL;
226af5bcd77SVladimir Sementsov-Ogievskiy     }
227af5bcd77SVladimir Sementsov-Ogievskiy 
228af5bcd77SVladimir Sementsov-Ogievskiy     done = bdrv_dirty_bitmap_status(s->done_bitmap, offset, bytes, pnum);
229af5bcd77SVladimir Sementsov-Ogievskiy     if (done) {
230af5bcd77SVladimir Sementsov-Ogievskiy         /*
231af5bcd77SVladimir Sementsov-Ogievskiy          * Special invalid BlockReq, that is handled in
232af5bcd77SVladimir Sementsov-Ogievskiy          * cbw_snapshot_read_unlock(). We don't need to lock something to read
233af5bcd77SVladimir Sementsov-Ogievskiy          * from s->target.
234af5bcd77SVladimir Sementsov-Ogievskiy          */
235af5bcd77SVladimir Sementsov-Ogievskiy         *req = (BlockReq) {.offset = -1, .bytes = -1};
236af5bcd77SVladimir Sementsov-Ogievskiy         *file = s->target;
237af5bcd77SVladimir Sementsov-Ogievskiy     } else {
238af5bcd77SVladimir Sementsov-Ogievskiy         reqlist_init_req(&s->frozen_read_reqs, req, offset, bytes);
239af5bcd77SVladimir Sementsov-Ogievskiy         *file = bs->file;
240af5bcd77SVladimir Sementsov-Ogievskiy     }
241af5bcd77SVladimir Sementsov-Ogievskiy 
242af5bcd77SVladimir Sementsov-Ogievskiy     return req;
243af5bcd77SVladimir Sementsov-Ogievskiy }
244af5bcd77SVladimir Sementsov-Ogievskiy 
2453e614042SPaolo Bonzini static coroutine_fn void
cbw_snapshot_read_unlock(BlockDriverState * bs,BlockReq * req)2463e614042SPaolo Bonzini cbw_snapshot_read_unlock(BlockDriverState *bs, BlockReq *req)
247af5bcd77SVladimir Sementsov-Ogievskiy {
248af5bcd77SVladimir Sementsov-Ogievskiy     BDRVCopyBeforeWriteState *s = bs->opaque;
249af5bcd77SVladimir Sementsov-Ogievskiy 
250af5bcd77SVladimir Sementsov-Ogievskiy     if (req->offset == -1 && req->bytes == -1) {
251af5bcd77SVladimir Sementsov-Ogievskiy         g_free(req);
252af5bcd77SVladimir Sementsov-Ogievskiy         return;
253af5bcd77SVladimir Sementsov-Ogievskiy     }
254af5bcd77SVladimir Sementsov-Ogievskiy 
255af5bcd77SVladimir Sementsov-Ogievskiy     QEMU_LOCK_GUARD(&s->lock);
256af5bcd77SVladimir Sementsov-Ogievskiy 
257af5bcd77SVladimir Sementsov-Ogievskiy     reqlist_remove_req(req);
258af5bcd77SVladimir Sementsov-Ogievskiy     g_free(req);
259af5bcd77SVladimir Sementsov-Ogievskiy }
260af5bcd77SVladimir Sementsov-Ogievskiy 
2617b9e8b22SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
cbw_co_preadv_snapshot(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,size_t qiov_offset)262af5bcd77SVladimir Sementsov-Ogievskiy cbw_co_preadv_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes,
263af5bcd77SVladimir Sementsov-Ogievskiy                        QEMUIOVector *qiov, size_t qiov_offset)
264af5bcd77SVladimir Sementsov-Ogievskiy {
265af5bcd77SVladimir Sementsov-Ogievskiy     BlockReq *req;
266af5bcd77SVladimir Sementsov-Ogievskiy     BdrvChild *file;
267af5bcd77SVladimir Sementsov-Ogievskiy     int ret;
268af5bcd77SVladimir Sementsov-Ogievskiy 
269af5bcd77SVladimir Sementsov-Ogievskiy     /* TODO: upgrade to async loop using AioTask */
270af5bcd77SVladimir Sementsov-Ogievskiy     while (bytes) {
271af5bcd77SVladimir Sementsov-Ogievskiy         int64_t cur_bytes;
272af5bcd77SVladimir Sementsov-Ogievskiy 
273af5bcd77SVladimir Sementsov-Ogievskiy         req = cbw_snapshot_read_lock(bs, offset, bytes, &cur_bytes, &file);
274af5bcd77SVladimir Sementsov-Ogievskiy         if (!req) {
275af5bcd77SVladimir Sementsov-Ogievskiy             return -EACCES;
276af5bcd77SVladimir Sementsov-Ogievskiy         }
277af5bcd77SVladimir Sementsov-Ogievskiy 
278af5bcd77SVladimir Sementsov-Ogievskiy         ret = bdrv_co_preadv_part(file, offset, cur_bytes,
279af5bcd77SVladimir Sementsov-Ogievskiy                                   qiov, qiov_offset, 0);
280af5bcd77SVladimir Sementsov-Ogievskiy         cbw_snapshot_read_unlock(bs, req);
281af5bcd77SVladimir Sementsov-Ogievskiy         if (ret < 0) {
282af5bcd77SVladimir Sementsov-Ogievskiy             return ret;
283af5bcd77SVladimir Sementsov-Ogievskiy         }
284af5bcd77SVladimir Sementsov-Ogievskiy 
285af5bcd77SVladimir Sementsov-Ogievskiy         bytes -= cur_bytes;
286af5bcd77SVladimir Sementsov-Ogievskiy         offset += cur_bytes;
287af5bcd77SVladimir Sementsov-Ogievskiy         qiov_offset += cur_bytes;
288af5bcd77SVladimir Sementsov-Ogievskiy     }
289af5bcd77SVladimir Sementsov-Ogievskiy 
290af5bcd77SVladimir Sementsov-Ogievskiy     return 0;
291af5bcd77SVladimir Sementsov-Ogievskiy }
292af5bcd77SVladimir Sementsov-Ogievskiy 
2937b9e8b22SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
cbw_co_snapshot_block_status(BlockDriverState * bs,bool want_zero,int64_t offset,int64_t bytes,int64_t * pnum,int64_t * map,BlockDriverState ** file)294af5bcd77SVladimir Sementsov-Ogievskiy cbw_co_snapshot_block_status(BlockDriverState *bs,
295af5bcd77SVladimir Sementsov-Ogievskiy                              bool want_zero, int64_t offset, int64_t bytes,
296af5bcd77SVladimir Sementsov-Ogievskiy                              int64_t *pnum, int64_t *map,
297af5bcd77SVladimir Sementsov-Ogievskiy                              BlockDriverState **file)
298af5bcd77SVladimir Sementsov-Ogievskiy {
299af5bcd77SVladimir Sementsov-Ogievskiy     BDRVCopyBeforeWriteState *s = bs->opaque;
300af5bcd77SVladimir Sementsov-Ogievskiy     BlockReq *req;
301af5bcd77SVladimir Sementsov-Ogievskiy     int ret;
302af5bcd77SVladimir Sementsov-Ogievskiy     int64_t cur_bytes;
303af5bcd77SVladimir Sementsov-Ogievskiy     BdrvChild *child;
304af5bcd77SVladimir Sementsov-Ogievskiy 
305af5bcd77SVladimir Sementsov-Ogievskiy     req = cbw_snapshot_read_lock(bs, offset, bytes, &cur_bytes, &child);
306af5bcd77SVladimir Sementsov-Ogievskiy     if (!req) {
307af5bcd77SVladimir Sementsov-Ogievskiy         return -EACCES;
308af5bcd77SVladimir Sementsov-Ogievskiy     }
309af5bcd77SVladimir Sementsov-Ogievskiy 
310cc323997SPaolo Bonzini     ret = bdrv_co_block_status(child->bs, offset, cur_bytes, pnum, map, file);
311af5bcd77SVladimir Sementsov-Ogievskiy     if (child == s->target) {
312af5bcd77SVladimir Sementsov-Ogievskiy         /*
313af5bcd77SVladimir Sementsov-Ogievskiy          * We refer to s->target only for areas that we've written to it.
314af5bcd77SVladimir Sementsov-Ogievskiy          * And we can not report unallocated blocks in s->target: this will
315af5bcd77SVladimir Sementsov-Ogievskiy          * break generic block-status-above logic, that will go to
316af5bcd77SVladimir Sementsov-Ogievskiy          * copy-before-write filtered child in this case.
317af5bcd77SVladimir Sementsov-Ogievskiy          */
318af5bcd77SVladimir Sementsov-Ogievskiy         assert(ret & BDRV_BLOCK_ALLOCATED);
319af5bcd77SVladimir Sementsov-Ogievskiy     }
320af5bcd77SVladimir Sementsov-Ogievskiy 
321af5bcd77SVladimir Sementsov-Ogievskiy     cbw_snapshot_read_unlock(bs, req);
322af5bcd77SVladimir Sementsov-Ogievskiy 
323af5bcd77SVladimir Sementsov-Ogievskiy     return ret;
324af5bcd77SVladimir Sementsov-Ogievskiy }
325af5bcd77SVladimir Sementsov-Ogievskiy 
3269a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
cbw_co_pdiscard_snapshot(BlockDriverState * bs,int64_t offset,int64_t bytes)3279a5a1c62SEmanuele Giuseppe Esposito cbw_co_pdiscard_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes)
328af5bcd77SVladimir Sementsov-Ogievskiy {
329af5bcd77SVladimir Sementsov-Ogievskiy     BDRVCopyBeforeWriteState *s = bs->opaque;
33050717519SVladimir Sementsov-Ogievskiy     uint32_t cluster_size = block_copy_cluster_size(s->bcs);
33150717519SVladimir Sementsov-Ogievskiy     int64_t aligned_offset = QEMU_ALIGN_UP(offset, cluster_size);
33250717519SVladimir Sementsov-Ogievskiy     int64_t aligned_end = QEMU_ALIGN_DOWN(offset + bytes, cluster_size);
33350717519SVladimir Sementsov-Ogievskiy     int64_t aligned_bytes;
33450717519SVladimir Sementsov-Ogievskiy 
33550717519SVladimir Sementsov-Ogievskiy     if (aligned_end <= aligned_offset) {
33650717519SVladimir Sementsov-Ogievskiy         return 0;
33750717519SVladimir Sementsov-Ogievskiy     }
33850717519SVladimir Sementsov-Ogievskiy     aligned_bytes = aligned_end - aligned_offset;
339af5bcd77SVladimir Sementsov-Ogievskiy 
340af5bcd77SVladimir Sementsov-Ogievskiy     WITH_QEMU_LOCK_GUARD(&s->lock) {
34150717519SVladimir Sementsov-Ogievskiy         bdrv_reset_dirty_bitmap(s->access_bitmap, aligned_offset,
34250717519SVladimir Sementsov-Ogievskiy                                 aligned_bytes);
343af5bcd77SVladimir Sementsov-Ogievskiy     }
344af5bcd77SVladimir Sementsov-Ogievskiy 
34550717519SVladimir Sementsov-Ogievskiy     block_copy_reset(s->bcs, aligned_offset, aligned_bytes);
346af5bcd77SVladimir Sementsov-Ogievskiy 
34750717519SVladimir Sementsov-Ogievskiy     return bdrv_co_pdiscard(s->target, aligned_offset, aligned_bytes);
348af5bcd77SVladimir Sementsov-Ogievskiy }
349af5bcd77SVladimir Sementsov-Ogievskiy 
cbw_refresh_filename(BlockDriverState * bs)35079a55866SKevin Wolf static void GRAPH_RDLOCK cbw_refresh_filename(BlockDriverState *bs)
351d003e0aeSVladimir Sementsov-Ogievskiy {
352d003e0aeSVladimir Sementsov-Ogievskiy     pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
3533c1e6327SVladimir Sementsov-Ogievskiy             bs->file->bs->filename);
354d003e0aeSVladimir Sementsov-Ogievskiy }
355d003e0aeSVladimir Sementsov-Ogievskiy 
356c629b6d2SKevin Wolf static void GRAPH_RDLOCK
cbw_child_perm(BlockDriverState * bs,BdrvChild * c,BdrvChildRole role,BlockReopenQueue * reopen_queue,uint64_t perm,uint64_t shared,uint64_t * nperm,uint64_t * nshared)357c629b6d2SKevin Wolf cbw_child_perm(BlockDriverState *bs, BdrvChild *c, BdrvChildRole role,
358d003e0aeSVladimir Sementsov-Ogievskiy                BlockReopenQueue *reopen_queue,
359d003e0aeSVladimir Sementsov-Ogievskiy                uint64_t perm, uint64_t shared,
360d003e0aeSVladimir Sementsov-Ogievskiy                uint64_t *nperm, uint64_t *nshared)
361d003e0aeSVladimir Sementsov-Ogievskiy {
3620fd05c8dSVladimir Sementsov-Ogievskiy     BDRVCopyBeforeWriteState *s = bs->opaque;
3630fd05c8dSVladimir Sementsov-Ogievskiy 
364d003e0aeSVladimir Sementsov-Ogievskiy     if (!(role & BDRV_CHILD_FILTERED)) {
365d003e0aeSVladimir Sementsov-Ogievskiy         /*
366d003e0aeSVladimir Sementsov-Ogievskiy          * Target child
367d003e0aeSVladimir Sementsov-Ogievskiy          *
368d003e0aeSVladimir Sementsov-Ogievskiy          * Share write to target (child_file), to not interfere
369d003e0aeSVladimir Sementsov-Ogievskiy          * with guest writes to its disk which may be in target backing chain.
370d003e0aeSVladimir Sementsov-Ogievskiy          * Can't resize during a backup block job because we check the size
371d003e0aeSVladimir Sementsov-Ogievskiy          * only upfront.
372d003e0aeSVladimir Sementsov-Ogievskiy          */
373d003e0aeSVladimir Sementsov-Ogievskiy         *nshared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
374d003e0aeSVladimir Sementsov-Ogievskiy         *nperm = BLK_PERM_WRITE;
375d003e0aeSVladimir Sementsov-Ogievskiy     } else {
376d003e0aeSVladimir Sementsov-Ogievskiy         /* Source child */
377d003e0aeSVladimir Sementsov-Ogievskiy         bdrv_default_perms(bs, c, role, reopen_queue,
378d003e0aeSVladimir Sementsov-Ogievskiy                            perm, shared, nperm, nshared);
379d003e0aeSVladimir Sementsov-Ogievskiy 
3803860c020SVladimir Sementsov-Ogievskiy         if (!QLIST_EMPTY(&bs->parents)) {
381137b4d4bSVladimir Sementsov-Ogievskiy             /*
382137b4d4bSVladimir Sementsov-Ogievskiy              * Note, that source child may be shared with backup job. Backup job
383137b4d4bSVladimir Sementsov-Ogievskiy              * does create own blk parent on copy-before-write node, so this
384137b4d4bSVladimir Sementsov-Ogievskiy              * works even if source node does not have any parents before backup
385137b4d4bSVladimir Sementsov-Ogievskiy              * start
386137b4d4bSVladimir Sementsov-Ogievskiy              */
387d003e0aeSVladimir Sementsov-Ogievskiy             *nperm = *nperm | BLK_PERM_CONSISTENT_READ;
3880fd05c8dSVladimir Sementsov-Ogievskiy             if (s->discard_source) {
3890fd05c8dSVladimir Sementsov-Ogievskiy                 *nperm = *nperm | BLK_PERM_WRITE;
3900fd05c8dSVladimir Sementsov-Ogievskiy             }
3910fd05c8dSVladimir Sementsov-Ogievskiy 
392d003e0aeSVladimir Sementsov-Ogievskiy             *nshared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
393d003e0aeSVladimir Sementsov-Ogievskiy         }
394d003e0aeSVladimir Sementsov-Ogievskiy     }
3953860c020SVladimir Sementsov-Ogievskiy }
396d003e0aeSVladimir Sementsov-Ogievskiy 
cbw_parse_options(QDict * options,Error ** errp)39779ef0cebSVladimir Sementsov-Ogievskiy static BlockdevOptions *cbw_parse_options(QDict *options, Error **errp)
3985f3a3cd7SVladimir Sementsov-Ogievskiy {
39979ef0cebSVladimir Sementsov-Ogievskiy     BlockdevOptions *opts = NULL;
4005f3a3cd7SVladimir Sementsov-Ogievskiy     Visitor *v = NULL;
4015f3a3cd7SVladimir Sementsov-Ogievskiy 
40279ef0cebSVladimir Sementsov-Ogievskiy     qdict_put_str(options, "driver", "copy-before-write");
4035f3a3cd7SVladimir Sementsov-Ogievskiy 
40479ef0cebSVladimir Sementsov-Ogievskiy     v = qobject_input_visitor_new_flat_confused(options, errp);
4055f3a3cd7SVladimir Sementsov-Ogievskiy     if (!v) {
4065f3a3cd7SVladimir Sementsov-Ogievskiy         goto out;
4075f3a3cd7SVladimir Sementsov-Ogievskiy     }
4085f3a3cd7SVladimir Sementsov-Ogievskiy 
40979ef0cebSVladimir Sementsov-Ogievskiy     visit_type_BlockdevOptions(v, NULL, &opts, errp);
41079ef0cebSVladimir Sementsov-Ogievskiy     if (!opts) {
4115f3a3cd7SVladimir Sementsov-Ogievskiy         goto out;
4125f3a3cd7SVladimir Sementsov-Ogievskiy     }
4135f3a3cd7SVladimir Sementsov-Ogievskiy 
41479ef0cebSVladimir Sementsov-Ogievskiy     /*
41579ef0cebSVladimir Sementsov-Ogievskiy      * Delete options which we are going to parse through BlockdevOptions
41679ef0cebSVladimir Sementsov-Ogievskiy      * object for original options.
41779ef0cebSVladimir Sementsov-Ogievskiy      */
41879ef0cebSVladimir Sementsov-Ogievskiy     qdict_extract_subqdict(options, NULL, "bitmap");
419f1bb39a8SVladimir Sementsov-Ogievskiy     qdict_del(options, "on-cbw-error");
4206db7fd1cSVladimir Sementsov-Ogievskiy     qdict_del(options, "cbw-timeout");
4215f3a3cd7SVladimir Sementsov-Ogievskiy     qdict_del(options, "min-cluster-size");
4225f3a3cd7SVladimir Sementsov-Ogievskiy 
4235f3a3cd7SVladimir Sementsov-Ogievskiy out:
42479ef0cebSVladimir Sementsov-Ogievskiy     visit_free(v);
4255f3a3cd7SVladimir Sementsov-Ogievskiy     qdict_del(options, "driver");
42679ef0cebSVladimir Sementsov-Ogievskiy 
4275f3a3cd7SVladimir Sementsov-Ogievskiy     return opts;
4285f3a3cd7SVladimir Sementsov-Ogievskiy }
429751cec7aSVladimir Sementsov-Ogievskiy 
cbw_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)430751cec7aSVladimir Sementsov-Ogievskiy static int cbw_open(BlockDriverState *bs, QDict *options, int flags,
4311f0cacb9SVladimir Sementsov-Ogievskiy                     Error **errp)
432c79eabc5SZhao Liu {
433fe7ea40cSVladimir Sementsov-Ogievskiy     ERRP_GUARD();
4345f3a3cd7SVladimir Sementsov-Ogievskiy     BDRVCopyBeforeWriteState *s = bs->opaque;
435af5bcd77SVladimir Sementsov-Ogievskiy     BdrvDirtyBitmap *bitmap = NULL;
43679ef0cebSVladimir Sementsov-Ogievskiy     int64_t cluster_size;
43779ef0cebSVladimir Sementsov-Ogievskiy     g_autoptr(BlockdevOptions) full_opts = NULL;
43883930780SVladimir Sementsov-Ogievskiy     BlockdevOptionsCbw *opts;
43979ef0cebSVladimir Sementsov-Ogievskiy     int ret;
44079ef0cebSVladimir Sementsov-Ogievskiy 
44179ef0cebSVladimir Sementsov-Ogievskiy     full_opts = cbw_parse_options(options, errp);
44279ef0cebSVladimir Sementsov-Ogievskiy     if (!full_opts) {
44379ef0cebSVladimir Sementsov-Ogievskiy         return -EINVAL;
44479ef0cebSVladimir Sementsov-Ogievskiy     }
44579ef0cebSVladimir Sementsov-Ogievskiy     assert(full_opts->driver == BLOCKDEV_DRIVER_COPY_BEFORE_WRITE);
4461f0cacb9SVladimir Sementsov-Ogievskiy     opts = &full_opts->u.copy_before_write;
44783930780SVladimir Sementsov-Ogievskiy 
44883930780SVladimir Sementsov-Ogievskiy     ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
44983930780SVladimir Sementsov-Ogievskiy     if (ret < 0) {
4501f0cacb9SVladimir Sementsov-Ogievskiy         return ret;
4511f0cacb9SVladimir Sementsov-Ogievskiy     }
452f44fd739SVladimir Sementsov-Ogievskiy 
453f44fd739SVladimir Sementsov-Ogievskiy     s->target = bdrv_open_child(NULL, options, "target", bs, &child_of_bds,
454f44fd739SVladimir Sementsov-Ogievskiy                                 BDRV_CHILD_DATA, false, errp);
4551f0cacb9SVladimir Sementsov-Ogievskiy     if (!s->target) {
4561f0cacb9SVladimir Sementsov-Ogievskiy         return -EINVAL;
4571f0cacb9SVladimir Sementsov-Ogievskiy     }
458a4b740dbSKevin Wolf 
459a4b740dbSKevin Wolf     GRAPH_RDLOCK_GUARD_MAINLOOP();
46054fde4ffSMarkus Armbruster 
46179ef0cebSVladimir Sementsov-Ogievskiy     if (opts->bitmap) {
46279ef0cebSVladimir Sementsov-Ogievskiy         bitmap = block_dirty_bitmap_lookup(opts->bitmap->node,
46379ef0cebSVladimir Sementsov-Ogievskiy                                            opts->bitmap->name, NULL, errp);
464b49f4755SStefan Hajnoczi         if (!bitmap) {
4655f3a3cd7SVladimir Sementsov-Ogievskiy             return -EINVAL;
46679ef0cebSVladimir Sementsov-Ogievskiy         }
467f1bb39a8SVladimir Sementsov-Ogievskiy     }
468f1bb39a8SVladimir Sementsov-Ogievskiy     s->on_cbw_error = opts->has_on_cbw_error ? opts->on_cbw_error :
4696db7fd1cSVladimir Sementsov-Ogievskiy             ON_CBW_ERROR_BREAK_GUEST_WRITE;
4706db7fd1cSVladimir Sementsov-Ogievskiy     s->cbw_timeout_ns = opts->has_cbw_timeout ?
4715f3a3cd7SVladimir Sementsov-Ogievskiy         opts->cbw_timeout * NANOSECONDS_PER_SECOND : 0;
4725a507426SVladimir Sementsov-Ogievskiy 
4735a507426SVladimir Sementsov-Ogievskiy     bs->total_sectors = bs->file->bs->total_sectors;
4745a507426SVladimir Sementsov-Ogievskiy     bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
4755a507426SVladimir Sementsov-Ogievskiy             (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
4765a507426SVladimir Sementsov-Ogievskiy     bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
4775a507426SVladimir Sementsov-Ogievskiy             ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
4785a507426SVladimir Sementsov-Ogievskiy              bs->file->bs->supported_zero_flags);
4790fd05c8dSVladimir Sementsov-Ogievskiy 
4800fd05c8dSVladimir Sementsov-Ogievskiy     s->discard_source = flags & BDRV_O_CBW_DISCARD_SOURCE;
4810fd05c8dSVladimir Sementsov-Ogievskiy 
482fe7ea40cSVladimir Sementsov-Ogievskiy     s->bcs = block_copy_state_new(bs->file, s->target, bs, bitmap,
4831f0cacb9SVladimir Sementsov-Ogievskiy                                   flags & BDRV_O_CBW_DISCARD_SOURCE,
484b49f4755SStefan Hajnoczi                                   opts->min_cluster_size, errp);
4851f0cacb9SVladimir Sementsov-Ogievskiy     if (!s->bcs) {
4861f0cacb9SVladimir Sementsov-Ogievskiy         error_prepend(errp, "Cannot create block-copy-state: ");
487af5bcd77SVladimir Sementsov-Ogievskiy         return -EINVAL;
488af5bcd77SVladimir Sementsov-Ogievskiy     }
489af5bcd77SVladimir Sementsov-Ogievskiy 
490af5bcd77SVladimir Sementsov-Ogievskiy     cluster_size = block_copy_cluster_size(s->bcs);
491b49f4755SStefan Hajnoczi 
492af5bcd77SVladimir Sementsov-Ogievskiy     s->done_bitmap = bdrv_create_dirty_bitmap(bs, cluster_size, NULL, errp);
493af5bcd77SVladimir Sementsov-Ogievskiy     if (!s->done_bitmap) {
494af5bcd77SVladimir Sementsov-Ogievskiy         return -EINVAL;
495af5bcd77SVladimir Sementsov-Ogievskiy     }
496af5bcd77SVladimir Sementsov-Ogievskiy     bdrv_disable_dirty_bitmap(s->done_bitmap);
497af5bcd77SVladimir Sementsov-Ogievskiy 
498b49f4755SStefan Hajnoczi     /* s->access_bitmap starts equal to bcs bitmap */
499af5bcd77SVladimir Sementsov-Ogievskiy     s->access_bitmap = bdrv_create_dirty_bitmap(bs, cluster_size, NULL, errp);
500af5bcd77SVladimir Sementsov-Ogievskiy     if (!s->access_bitmap) {
501af5bcd77SVladimir Sementsov-Ogievskiy         return -EINVAL;
502af5bcd77SVladimir Sementsov-Ogievskiy     }
503af5bcd77SVladimir Sementsov-Ogievskiy     bdrv_disable_dirty_bitmap(s->access_bitmap);
504af5bcd77SVladimir Sementsov-Ogievskiy     bdrv_dirty_bitmap_merge_internal(s->access_bitmap,
505af5bcd77SVladimir Sementsov-Ogievskiy                                      block_copy_dirty_bitmap(s->bcs), NULL,
506af5bcd77SVladimir Sementsov-Ogievskiy                                      true);
507b49f4755SStefan Hajnoczi 
5081f0cacb9SVladimir Sementsov-Ogievskiy     qemu_co_mutex_init(&s->lock);
5091f0cacb9SVladimir Sementsov-Ogievskiy     QLIST_INIT(&s->frozen_read_reqs);
510751cec7aSVladimir Sementsov-Ogievskiy     return 0;
511751cec7aSVladimir Sementsov-Ogievskiy }
512751cec7aSVladimir Sementsov-Ogievskiy 
cbw_close(BlockDriverState * bs)513751cec7aSVladimir Sementsov-Ogievskiy static void cbw_close(BlockDriverState *bs)
514af5bcd77SVladimir Sementsov-Ogievskiy {
515af5bcd77SVladimir Sementsov-Ogievskiy     BDRVCopyBeforeWriteState *s = bs->opaque;
516af5bcd77SVladimir Sementsov-Ogievskiy 
517751cec7aSVladimir Sementsov-Ogievskiy     bdrv_release_dirty_bitmap(s->access_bitmap);
518751cec7aSVladimir Sementsov-Ogievskiy     bdrv_release_dirty_bitmap(s->done_bitmap);
519751cec7aSVladimir Sementsov-Ogievskiy 
520751cec7aSVladimir Sementsov-Ogievskiy     block_copy_state_free(s->bcs);
5219ea473fbSKevin Wolf     s->bcs = NULL;
522d003e0aeSVladimir Sementsov-Ogievskiy }
523d003e0aeSVladimir Sementsov-Ogievskiy 
524d003e0aeSVladimir Sementsov-Ogievskiy static BlockDriver bdrv_cbw_filter = {
525751cec7aSVladimir Sementsov-Ogievskiy     .format_name = "copy-before-write",
526751cec7aSVladimir Sementsov-Ogievskiy     .instance_size = sizeof(BDRVCopyBeforeWriteState),
527751cec7aSVladimir Sementsov-Ogievskiy 
528d003e0aeSVladimir Sementsov-Ogievskiy     .bdrv_open                  = cbw_open,
529d003e0aeSVladimir Sementsov-Ogievskiy     .bdrv_close                 = cbw_close,
530d003e0aeSVladimir Sementsov-Ogievskiy 
531d003e0aeSVladimir Sementsov-Ogievskiy     .bdrv_co_preadv             = cbw_co_preadv,
532d003e0aeSVladimir Sementsov-Ogievskiy     .bdrv_co_pwritev            = cbw_co_pwritev,
533d003e0aeSVladimir Sementsov-Ogievskiy     .bdrv_co_pwrite_zeroes      = cbw_co_pwrite_zeroes,
534af5bcd77SVladimir Sementsov-Ogievskiy     .bdrv_co_pdiscard           = cbw_co_pdiscard,
535af5bcd77SVladimir Sementsov-Ogievskiy     .bdrv_co_flush              = cbw_co_flush,
536af5bcd77SVladimir Sementsov-Ogievskiy 
537af5bcd77SVladimir Sementsov-Ogievskiy     .bdrv_co_preadv_snapshot       = cbw_co_preadv_snapshot,
538d003e0aeSVladimir Sementsov-Ogievskiy     .bdrv_co_pdiscard_snapshot     = cbw_co_pdiscard_snapshot,
539d003e0aeSVladimir Sementsov-Ogievskiy     .bdrv_co_snapshot_block_status = cbw_co_snapshot_block_status,
540d003e0aeSVladimir Sementsov-Ogievskiy 
541d003e0aeSVladimir Sementsov-Ogievskiy     .bdrv_refresh_filename      = cbw_refresh_filename,
542d003e0aeSVladimir Sementsov-Ogievskiy 
543d003e0aeSVladimir Sementsov-Ogievskiy     .bdrv_child_perm            = cbw_child_perm,
544d003e0aeSVladimir Sementsov-Ogievskiy 
545d003e0aeSVladimir Sementsov-Ogievskiy     .is_filter = true,
546d003e0aeSVladimir Sementsov-Ogievskiy };
547d003e0aeSVladimir Sementsov-Ogievskiy 
bdrv_cbw_append(BlockDriverState * source,BlockDriverState * target,const char * filter_node_name,bool discard_source,uint64_t min_cluster_size,BlockCopyState ** bcs,Error ** errp)5480fd05c8dSVladimir Sementsov-Ogievskiy BlockDriverState *bdrv_cbw_append(BlockDriverState *source,
549d003e0aeSVladimir Sementsov-Ogievskiy                                   BlockDriverState *target,
550d003e0aeSVladimir Sementsov-Ogievskiy                                   const char *filter_node_name,
551d003e0aeSVladimir Sementsov-Ogievskiy                                   bool discard_source,
552d003e0aeSVladimir Sementsov-Ogievskiy                                   uint64_t min_cluster_size,
553d003e0aeSVladimir Sementsov-Ogievskiy                                   BlockCopyState **bcs,
554f44fd739SVladimir Sementsov-Ogievskiy                                   Error **errp)
5550fd05c8dSVladimir Sementsov-Ogievskiy {
556d003e0aeSVladimir Sementsov-Ogievskiy     BDRVCopyBeforeWriteState *state;
557d003e0aeSVladimir Sementsov-Ogievskiy     BlockDriverState *top;
558377cc15bSEmanuele Giuseppe Esposito     QDict *opts;
559d003e0aeSVladimir Sementsov-Ogievskiy     int flags = BDRV_O_RDWR | (discard_source ? BDRV_O_CBW_DISCARD_SOURCE : 0);
560f44fd739SVladimir Sementsov-Ogievskiy 
561751cec7aSVladimir Sementsov-Ogievskiy     assert(source->total_sectors == target->total_sectors);
562751cec7aSVladimir Sementsov-Ogievskiy     GLOBAL_STATE_CODE();
563751cec7aSVladimir Sementsov-Ogievskiy 
564751cec7aSVladimir Sementsov-Ogievskiy     opts = qdict_new();
565f44fd739SVladimir Sementsov-Ogievskiy     qdict_put_str(opts, "driver", "copy-before-write");
566f44fd739SVladimir Sementsov-Ogievskiy     if (filter_node_name) {
567f44fd739SVladimir Sementsov-Ogievskiy         qdict_put_str(opts, "node-name", filter_node_name);
5680fd05c8dSVladimir Sementsov-Ogievskiy     }
569751cec7aSVladimir Sementsov-Ogievskiy     qdict_put_str(opts, "file", bdrv_get_node_name(source));
570751cec7aSVladimir Sementsov-Ogievskiy     qdict_put_str(opts, "target", bdrv_get_node_name(target));
571d003e0aeSVladimir Sementsov-Ogievskiy 
572d003e0aeSVladimir Sementsov-Ogievskiy     if (min_cluster_size > INT64_MAX) {
573751cec7aSVladimir Sementsov-Ogievskiy         error_setg(errp, "min-cluster-size too large: %" PRIu64 " > %" PRIi64,
5747ddbce2dSVladimir Sementsov-Ogievskiy                    min_cluster_size, INT64_MAX);
575d003e0aeSVladimir Sementsov-Ogievskiy         qobject_unref(opts);
576d003e0aeSVladimir Sementsov-Ogievskiy         return NULL;
577d003e0aeSVladimir Sementsov-Ogievskiy     }
578d003e0aeSVladimir Sementsov-Ogievskiy     qdict_put_int(opts, "min-cluster-size", (int64_t)min_cluster_size);
579d003e0aeSVladimir Sementsov-Ogievskiy 
580d003e0aeSVladimir Sementsov-Ogievskiy     top = bdrv_insert_node(source, opts, flags, errp);
581377cc15bSEmanuele Giuseppe Esposito     if (!top) {
582d003e0aeSVladimir Sementsov-Ogievskiy         return NULL;
583d003e0aeSVladimir Sementsov-Ogievskiy     }
584d003e0aeSVladimir Sementsov-Ogievskiy 
585751cec7aSVladimir Sementsov-Ogievskiy     state = top->opaque;
586751cec7aSVladimir Sementsov-Ogievskiy     *bcs = state->bcs;
587751cec7aSVladimir Sementsov-Ogievskiy 
588751cec7aSVladimir Sementsov-Ogievskiy     return top;
589751cec7aSVladimir Sementsov-Ogievskiy }
590751cec7aSVladimir Sementsov-Ogievskiy 
bdrv_cbw_drop(BlockDriverState * bs)591751cec7aSVladimir Sementsov-Ogievskiy void bdrv_cbw_drop(BlockDriverState *bs)
592 {
593     GLOBAL_STATE_CODE();
594     bdrv_drop_filter(bs, &error_abort);
595     bdrv_unref(bs);
596 }
597 
cbw_init(void)598 static void cbw_init(void)
599 {
600     bdrv_register(&bdrv_cbw_filter);
601 }
602 
603 block_init(cbw_init);
604