/src/qemu/block/copy-before-write.c
Line | Count | Source |
1 | | /* |
2 | | * copy-before-write filter driver |
3 | | * |
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. |
6 | | * |
7 | | * Copyright (c) 2018-2021 Virtuozzo International GmbH. |
8 | | * |
9 | | * Author: |
10 | | * Sementsov-Ogievskiy Vladimir <vsementsov@virtuozzo.com> |
11 | | * |
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. |
16 | | * |
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. |
21 | | * |
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/>. |
24 | | */ |
25 | | |
26 | | #include "qemu/osdep.h" |
27 | | #include "qobject/qjson.h" |
28 | | |
29 | | #include "system/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" |
36 | | |
37 | | #include "block/copy-before-write.h" |
38 | | #include "block/reqlist.h" |
39 | | |
40 | | #include "qapi/qapi-visit-block-core.h" |
41 | | |
42 | | typedef struct BDRVCopyBeforeWriteState { |
43 | | BlockCopyState *bcs; |
44 | | BdrvChild *target; |
45 | | OnCbwError on_cbw_error; |
46 | | uint64_t cbw_timeout_ns; |
47 | | bool discard_source; |
48 | | |
49 | | /* |
50 | | * @lock: protects access to @access_bitmap, @done_bitmap and |
51 | | * @frozen_read_reqs |
52 | | */ |
53 | | CoMutex lock; |
54 | | |
55 | | /* |
56 | | * @access_bitmap: represents areas allowed for reading by fleecing user. |
57 | | * Reading from non-dirty areas leads to -EACCES. |
58 | | */ |
59 | | BdrvDirtyBitmap *access_bitmap; |
60 | | |
61 | | /* |
62 | | * @done_bitmap: represents areas that was successfully copied to @target by |
63 | | * copy-before-write operations. |
64 | | */ |
65 | | BdrvDirtyBitmap *done_bitmap; |
66 | | |
67 | | /* |
68 | | * @frozen_read_reqs: current read requests for fleecing user in bs->file |
69 | | * node. These areas must not be rewritten by guest. There can be multiple |
70 | | * overlapping read requests. |
71 | | */ |
72 | | BlockReqList frozen_read_reqs; |
73 | | |
74 | | /* |
75 | | * @snapshot_error is normally zero. But on first copy-before-write failure |
76 | | * when @on_cbw_error == ON_CBW_ERROR_BREAK_SNAPSHOT, @snapshot_error takes |
77 | | * value of this error (<0). After that all in-flight and further |
78 | | * snapshot-API requests will fail with that error. |
79 | | */ |
80 | | int snapshot_error; |
81 | | } BDRVCopyBeforeWriteState; |
82 | | |
83 | | static int coroutine_fn GRAPH_RDLOCK |
84 | | cbw_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, |
85 | | QEMUIOVector *qiov, BdrvRequestFlags flags) |
86 | 0 | { |
87 | 0 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
88 | 0 | } |
89 | | |
90 | | static void block_copy_cb(void *opaque) |
91 | 0 | { |
92 | 0 | BlockDriverState *bs = opaque; |
93 | |
|
94 | 0 | bdrv_dec_in_flight(bs); |
95 | 0 | } |
96 | | |
97 | | /* |
98 | | * Do copy-before-write operation. |
99 | | * |
100 | | * On failure guest request must be failed too. |
101 | | * |
102 | | * On success, we also wait for all in-flight fleecing read requests in source |
103 | | * node, and it's guaranteed that after cbw_do_copy_before_write() successful |
104 | | * return there are no such requests and they will never appear. |
105 | | */ |
106 | | static coroutine_fn int cbw_do_copy_before_write(BlockDriverState *bs, |
107 | | uint64_t offset, uint64_t bytes, BdrvRequestFlags flags) |
108 | 0 | { |
109 | 0 | BDRVCopyBeforeWriteState *s = bs->opaque; |
110 | 0 | int ret; |
111 | 0 | uint64_t off, end; |
112 | 0 | int64_t cluster_size = block_copy_cluster_size(s->bcs); |
113 | |
|
114 | 0 | if (flags & BDRV_REQ_WRITE_UNCHANGED) { |
115 | 0 | return 0; |
116 | 0 | } |
117 | | |
118 | 0 | if (s->snapshot_error) { |
119 | 0 | return 0; |
120 | 0 | } |
121 | | |
122 | 0 | off = QEMU_ALIGN_DOWN(offset, cluster_size); |
123 | 0 | end = QEMU_ALIGN_UP(offset + bytes, cluster_size); |
124 | | |
125 | | /* |
126 | | * Increase in_flight, so that in case of timed-out block-copy, the |
127 | | * remaining background block_copy() request (which can't be immediately |
128 | | * cancelled by timeout) is presented in bs->in_flight. This way we are |
129 | | * sure that on bs close() we'll previously wait for all timed-out but yet |
130 | | * running block_copy calls. |
131 | | */ |
132 | 0 | bdrv_inc_in_flight(bs); |
133 | 0 | ret = block_copy(s->bcs, off, end - off, true, s->cbw_timeout_ns, |
134 | 0 | block_copy_cb, bs); |
135 | 0 | if (ret < 0 && s->on_cbw_error == ON_CBW_ERROR_BREAK_GUEST_WRITE) { |
136 | 0 | return ret; |
137 | 0 | } |
138 | | |
139 | 0 | WITH_QEMU_LOCK_GUARD(&s->lock) { |
140 | 0 | if (ret < 0) { |
141 | 0 | assert(s->on_cbw_error == ON_CBW_ERROR_BREAK_SNAPSHOT); |
142 | 0 | if (!s->snapshot_error) { |
143 | 0 | s->snapshot_error = ret; |
144 | 0 | } |
145 | 0 | } else { |
146 | 0 | bdrv_set_dirty_bitmap(s->done_bitmap, off, end - off); |
147 | 0 | } |
148 | 0 | reqlist_wait_all(&s->frozen_read_reqs, off, end - off, &s->lock); |
149 | 0 | } |
150 | |
|
151 | 0 | return 0; |
152 | 0 | } |
153 | | |
154 | | static int coroutine_fn GRAPH_RDLOCK |
155 | | cbw_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes) |
156 | 0 | { |
157 | 0 | int ret = cbw_do_copy_before_write(bs, offset, bytes, 0); |
158 | 0 | if (ret < 0) { |
159 | 0 | return ret; |
160 | 0 | } |
161 | | |
162 | 0 | return bdrv_co_pdiscard(bs->file, offset, bytes); |
163 | 0 | } |
164 | | |
165 | | static int coroutine_fn GRAPH_RDLOCK |
166 | | cbw_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, |
167 | | BdrvRequestFlags flags) |
168 | 0 | { |
169 | 0 | int ret = cbw_do_copy_before_write(bs, offset, bytes, flags); |
170 | 0 | if (ret < 0) { |
171 | 0 | return ret; |
172 | 0 | } |
173 | | |
174 | 0 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); |
175 | 0 | } |
176 | | |
177 | | static coroutine_fn GRAPH_RDLOCK |
178 | | int cbw_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, |
179 | | QEMUIOVector *qiov, BdrvRequestFlags flags) |
180 | 0 | { |
181 | 0 | int ret = cbw_do_copy_before_write(bs, offset, bytes, flags); |
182 | 0 | if (ret < 0) { |
183 | 0 | return ret; |
184 | 0 | } |
185 | | |
186 | 0 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); |
187 | 0 | } |
188 | | |
189 | | static int coroutine_fn GRAPH_RDLOCK cbw_co_flush(BlockDriverState *bs) |
190 | 0 | { |
191 | 0 | if (!bs->file) { |
192 | 0 | return 0; |
193 | 0 | } |
194 | | |
195 | 0 | return bdrv_co_flush(bs->file->bs); |
196 | 0 | } |
197 | | |
198 | | /* |
199 | | * If @offset not accessible - return NULL. |
200 | | * |
201 | | * Otherwise, set @pnum to some bytes that accessible from @file (@file is set |
202 | | * to bs->file or to s->target). Return newly allocated BlockReq object that |
203 | | * should be than passed to cbw_snapshot_read_unlock(). |
204 | | * |
205 | | * It's guaranteed that guest writes will not interact in the region until |
206 | | * cbw_snapshot_read_unlock() called. |
207 | | */ |
208 | | static BlockReq * coroutine_fn GRAPH_RDLOCK |
209 | | cbw_snapshot_read_lock(BlockDriverState *bs, int64_t offset, int64_t bytes, |
210 | | int64_t *pnum, BdrvChild **file) |
211 | 0 | { |
212 | 0 | BDRVCopyBeforeWriteState *s = bs->opaque; |
213 | 0 | BlockReq *req = g_new(BlockReq, 1); |
214 | 0 | bool done; |
215 | |
|
216 | 0 | QEMU_LOCK_GUARD(&s->lock); |
217 | |
|
218 | 0 | if (s->snapshot_error) { |
219 | 0 | g_free(req); |
220 | 0 | return NULL; |
221 | 0 | } |
222 | | |
223 | 0 | if (bdrv_dirty_bitmap_next_zero(s->access_bitmap, offset, bytes) != -1) { |
224 | 0 | g_free(req); |
225 | 0 | return NULL; |
226 | 0 | } |
227 | | |
228 | 0 | done = bdrv_dirty_bitmap_status(s->done_bitmap, offset, bytes, pnum); |
229 | 0 | if (done) { |
230 | | /* |
231 | | * Special invalid BlockReq, that is handled in |
232 | | * cbw_snapshot_read_unlock(). We don't need to lock something to read |
233 | | * from s->target. |
234 | | */ |
235 | 0 | *req = (BlockReq) {.offset = -1, .bytes = -1}; |
236 | 0 | *file = s->target; |
237 | 0 | } else { |
238 | 0 | reqlist_init_req(&s->frozen_read_reqs, req, offset, bytes); |
239 | 0 | *file = bs->file; |
240 | 0 | } |
241 | |
|
242 | 0 | return req; |
243 | 0 | } |
244 | | |
245 | | static coroutine_fn void |
246 | | cbw_snapshot_read_unlock(BlockDriverState *bs, BlockReq *req) |
247 | 0 | { |
248 | 0 | BDRVCopyBeforeWriteState *s = bs->opaque; |
249 | |
|
250 | 0 | if (req->offset == -1 && req->bytes == -1) { |
251 | 0 | g_free(req); |
252 | 0 | return; |
253 | 0 | } |
254 | | |
255 | 0 | QEMU_LOCK_GUARD(&s->lock); |
256 | |
|
257 | 0 | reqlist_remove_req(req); |
258 | 0 | g_free(req); |
259 | 0 | } |
260 | | |
261 | | static int coroutine_fn GRAPH_RDLOCK |
262 | | cbw_co_preadv_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes, |
263 | | QEMUIOVector *qiov, size_t qiov_offset) |
264 | 0 | { |
265 | 0 | BlockReq *req; |
266 | 0 | BdrvChild *file; |
267 | 0 | int ret; |
268 | | |
269 | | /* TODO: upgrade to async loop using AioTask */ |
270 | 0 | while (bytes) { |
271 | 0 | int64_t cur_bytes; |
272 | |
|
273 | 0 | req = cbw_snapshot_read_lock(bs, offset, bytes, &cur_bytes, &file); |
274 | 0 | if (!req) { |
275 | 0 | return -EACCES; |
276 | 0 | } |
277 | | |
278 | 0 | ret = bdrv_co_preadv_part(file, offset, cur_bytes, |
279 | 0 | qiov, qiov_offset, 0); |
280 | 0 | cbw_snapshot_read_unlock(bs, req); |
281 | 0 | if (ret < 0) { |
282 | 0 | return ret; |
283 | 0 | } |
284 | | |
285 | 0 | bytes -= cur_bytes; |
286 | 0 | offset += cur_bytes; |
287 | 0 | qiov_offset += cur_bytes; |
288 | 0 | } |
289 | | |
290 | 0 | return 0; |
291 | 0 | } |
292 | | |
293 | | static int coroutine_fn GRAPH_RDLOCK |
294 | | cbw_co_snapshot_block_status(BlockDriverState *bs, unsigned int mode, |
295 | | int64_t offset, int64_t bytes, |
296 | | int64_t *pnum, int64_t *map, |
297 | | BlockDriverState **file) |
298 | 0 | { |
299 | 0 | BDRVCopyBeforeWriteState *s = bs->opaque; |
300 | 0 | BlockReq *req; |
301 | 0 | int ret; |
302 | 0 | int64_t cur_bytes; |
303 | 0 | BdrvChild *child; |
304 | |
|
305 | 0 | req = cbw_snapshot_read_lock(bs, offset, bytes, &cur_bytes, &child); |
306 | 0 | if (!req) { |
307 | 0 | return -EACCES; |
308 | 0 | } |
309 | | |
310 | 0 | ret = bdrv_co_block_status(child->bs, offset, cur_bytes, pnum, map, file); |
311 | 0 | if (child == s->target) { |
312 | | /* |
313 | | * We refer to s->target only for areas that we've written to it. |
314 | | * And we can not report unallocated blocks in s->target: this will |
315 | | * break generic block-status-above logic, that will go to |
316 | | * copy-before-write filtered child in this case. |
317 | | */ |
318 | 0 | assert(ret & BDRV_BLOCK_ALLOCATED); |
319 | 0 | } |
320 | |
|
321 | 0 | cbw_snapshot_read_unlock(bs, req); |
322 | |
|
323 | 0 | return ret; |
324 | 0 | } |
325 | | |
326 | | static int coroutine_fn GRAPH_RDLOCK |
327 | | cbw_co_pdiscard_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes) |
328 | 0 | { |
329 | 0 | BDRVCopyBeforeWriteState *s = bs->opaque; |
330 | 0 | uint32_t cluster_size = block_copy_cluster_size(s->bcs); |
331 | 0 | int64_t aligned_offset = QEMU_ALIGN_UP(offset, cluster_size); |
332 | 0 | int64_t aligned_end = QEMU_ALIGN_DOWN(offset + bytes, cluster_size); |
333 | 0 | int64_t aligned_bytes; |
334 | |
|
335 | 0 | if (aligned_end <= aligned_offset) { |
336 | 0 | return 0; |
337 | 0 | } |
338 | 0 | aligned_bytes = aligned_end - aligned_offset; |
339 | |
|
340 | 0 | WITH_QEMU_LOCK_GUARD(&s->lock) { |
341 | 0 | bdrv_reset_dirty_bitmap(s->access_bitmap, aligned_offset, |
342 | 0 | aligned_bytes); |
343 | 0 | } |
344 | |
|
345 | 0 | block_copy_reset(s->bcs, aligned_offset, aligned_bytes); |
346 | |
|
347 | 0 | return bdrv_co_pdiscard(s->target, aligned_offset, aligned_bytes); |
348 | 0 | } |
349 | | |
350 | | static void GRAPH_RDLOCK cbw_refresh_filename(BlockDriverState *bs) |
351 | 0 | { |
352 | 0 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), |
353 | 0 | bs->file->bs->filename); |
354 | 0 | } |
355 | | |
356 | | static void GRAPH_RDLOCK |
357 | | cbw_child_perm(BlockDriverState *bs, BdrvChild *c, BdrvChildRole role, |
358 | | BlockReopenQueue *reopen_queue, |
359 | | uint64_t perm, uint64_t shared, |
360 | | uint64_t *nperm, uint64_t *nshared) |
361 | 0 | { |
362 | 0 | BDRVCopyBeforeWriteState *s = bs->opaque; |
363 | |
|
364 | 0 | if (!(role & BDRV_CHILD_FILTERED)) { |
365 | | /* |
366 | | * Target child |
367 | | * |
368 | | * Share write to target (child_file), to not interfere |
369 | | * with guest writes to its disk which may be in target backing chain. |
370 | | * Can't resize during a backup block job because we check the size |
371 | | * only upfront. |
372 | | */ |
373 | 0 | *nshared = BLK_PERM_ALL & ~BLK_PERM_RESIZE; |
374 | 0 | *nperm = BLK_PERM_WRITE; |
375 | 0 | } else { |
376 | | /* Source child */ |
377 | 0 | bdrv_default_perms(bs, c, role, reopen_queue, |
378 | 0 | perm, shared, nperm, nshared); |
379 | |
|
380 | 0 | if (!QLIST_EMPTY(&bs->parents)) { |
381 | | /* |
382 | | * Note, that source child may be shared with backup job. Backup job |
383 | | * does create own blk parent on copy-before-write node, so this |
384 | | * works even if source node does not have any parents before backup |
385 | | * start |
386 | | */ |
387 | 0 | *nperm = *nperm | BLK_PERM_CONSISTENT_READ; |
388 | 0 | if (s->discard_source) { |
389 | 0 | *nperm = *nperm | BLK_PERM_WRITE; |
390 | 0 | } |
391 | |
|
392 | 0 | *nshared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE); |
393 | 0 | } |
394 | 0 | } |
395 | 0 | } |
396 | | |
397 | | static BlockdevOptions *cbw_parse_options(QDict *options, Error **errp) |
398 | 0 | { |
399 | 0 | BlockdevOptions *opts = NULL; |
400 | 0 | Visitor *v = NULL; |
401 | |
|
402 | 0 | qdict_put_str(options, "driver", "copy-before-write"); |
403 | |
|
404 | 0 | v = qobject_input_visitor_new_flat_confused(options, errp); |
405 | 0 | if (!v) { |
406 | 0 | goto out; |
407 | 0 | } |
408 | | |
409 | 0 | visit_type_BlockdevOptions(v, NULL, &opts, errp); |
410 | 0 | if (!opts) { |
411 | 0 | goto out; |
412 | 0 | } |
413 | | |
414 | | /* |
415 | | * Delete options which we are going to parse through BlockdevOptions |
416 | | * object for original options. |
417 | | */ |
418 | 0 | qdict_extract_subqdict(options, NULL, "bitmap"); |
419 | 0 | qdict_del(options, "on-cbw-error"); |
420 | 0 | qdict_del(options, "cbw-timeout"); |
421 | 0 | qdict_del(options, "min-cluster-size"); |
422 | |
|
423 | 0 | out: |
424 | 0 | visit_free(v); |
425 | 0 | qdict_del(options, "driver"); |
426 | |
|
427 | 0 | return opts; |
428 | 0 | } |
429 | | |
430 | | static int cbw_open(BlockDriverState *bs, QDict *options, int flags, |
431 | | Error **errp) |
432 | 0 | { |
433 | 0 | ERRP_GUARD(); |
434 | 0 | BDRVCopyBeforeWriteState *s = bs->opaque; |
435 | 0 | BdrvDirtyBitmap *bitmap = NULL; |
436 | 0 | int64_t cluster_size; |
437 | 0 | g_autoptr(BlockdevOptions) full_opts = NULL; |
438 | 0 | BlockdevOptionsCbw *opts; |
439 | 0 | int ret; |
440 | |
|
441 | 0 | full_opts = cbw_parse_options(options, errp); |
442 | 0 | if (!full_opts) { |
443 | 0 | return -EINVAL; |
444 | 0 | } |
445 | 0 | assert(full_opts->driver == BLOCKDEV_DRIVER_COPY_BEFORE_WRITE); |
446 | 0 | opts = &full_opts->u.copy_before_write; |
447 | |
|
448 | 0 | ret = bdrv_open_file_child(NULL, options, "file", bs, errp); |
449 | 0 | if (ret < 0) { |
450 | 0 | return ret; |
451 | 0 | } |
452 | | |
453 | 0 | s->target = bdrv_open_child(NULL, options, "target", bs, &child_of_bds, |
454 | 0 | BDRV_CHILD_DATA, false, errp); |
455 | 0 | if (!s->target) { |
456 | 0 | return -EINVAL; |
457 | 0 | } |
458 | | |
459 | 0 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
460 | |
|
461 | 0 | if (opts->bitmap) { |
462 | 0 | bitmap = block_dirty_bitmap_lookup(opts->bitmap->node, |
463 | 0 | opts->bitmap->name, NULL, errp); |
464 | 0 | if (!bitmap) { |
465 | 0 | return -EINVAL; |
466 | 0 | } |
467 | 0 | } |
468 | 0 | s->on_cbw_error = opts->has_on_cbw_error ? opts->on_cbw_error : |
469 | 0 | ON_CBW_ERROR_BREAK_GUEST_WRITE; |
470 | 0 | s->cbw_timeout_ns = opts->has_cbw_timeout ? |
471 | 0 | opts->cbw_timeout * NANOSECONDS_PER_SECOND : 0; |
472 | |
|
473 | 0 | bs->total_sectors = bs->file->bs->total_sectors; |
474 | 0 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED | |
475 | 0 | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); |
476 | 0 | bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | |
477 | 0 | ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) & |
478 | 0 | bs->file->bs->supported_zero_flags); |
479 | |
|
480 | 0 | s->discard_source = flags & BDRV_O_CBW_DISCARD_SOURCE; |
481 | |
|
482 | 0 | s->bcs = block_copy_state_new(bs->file, s->target, bs, bitmap, |
483 | 0 | flags & BDRV_O_CBW_DISCARD_SOURCE, |
484 | 0 | opts->min_cluster_size, errp); |
485 | 0 | if (!s->bcs) { |
486 | 0 | error_prepend(errp, "Cannot create block-copy-state: "); |
487 | 0 | return -EINVAL; |
488 | 0 | } |
489 | | |
490 | 0 | cluster_size = block_copy_cluster_size(s->bcs); |
491 | |
|
492 | 0 | s->done_bitmap = bdrv_create_dirty_bitmap(bs, cluster_size, NULL, errp); |
493 | 0 | if (!s->done_bitmap) { |
494 | 0 | return -EINVAL; |
495 | 0 | } |
496 | 0 | bdrv_disable_dirty_bitmap(s->done_bitmap); |
497 | | |
498 | | /* s->access_bitmap starts equal to bcs bitmap */ |
499 | 0 | s->access_bitmap = bdrv_create_dirty_bitmap(bs, cluster_size, NULL, errp); |
500 | 0 | if (!s->access_bitmap) { |
501 | 0 | return -EINVAL; |
502 | 0 | } |
503 | 0 | bdrv_disable_dirty_bitmap(s->access_bitmap); |
504 | 0 | bdrv_dirty_bitmap_merge_internal(s->access_bitmap, |
505 | 0 | block_copy_dirty_bitmap(s->bcs), NULL, |
506 | 0 | true); |
507 | |
|
508 | 0 | qemu_co_mutex_init(&s->lock); |
509 | 0 | QLIST_INIT(&s->frozen_read_reqs); |
510 | 0 | return 0; |
511 | 0 | } |
512 | | |
513 | | static void cbw_close(BlockDriverState *bs) |
514 | 0 | { |
515 | 0 | BDRVCopyBeforeWriteState *s = bs->opaque; |
516 | |
|
517 | 0 | bdrv_release_dirty_bitmap(s->access_bitmap); |
518 | 0 | bdrv_release_dirty_bitmap(s->done_bitmap); |
519 | |
|
520 | 0 | block_copy_state_free(s->bcs); |
521 | 0 | s->bcs = NULL; |
522 | 0 | } |
523 | | |
524 | | static BlockDriver bdrv_cbw_filter = { |
525 | | .format_name = "copy-before-write", |
526 | | .instance_size = sizeof(BDRVCopyBeforeWriteState), |
527 | | |
528 | | .bdrv_open = cbw_open, |
529 | | .bdrv_close = cbw_close, |
530 | | |
531 | | .bdrv_co_preadv = cbw_co_preadv, |
532 | | .bdrv_co_pwritev = cbw_co_pwritev, |
533 | | .bdrv_co_pwrite_zeroes = cbw_co_pwrite_zeroes, |
534 | | .bdrv_co_pdiscard = cbw_co_pdiscard, |
535 | | .bdrv_co_flush = cbw_co_flush, |
536 | | |
537 | | .bdrv_co_preadv_snapshot = cbw_co_preadv_snapshot, |
538 | | .bdrv_co_pdiscard_snapshot = cbw_co_pdiscard_snapshot, |
539 | | .bdrv_co_snapshot_block_status = cbw_co_snapshot_block_status, |
540 | | |
541 | | .bdrv_refresh_filename = cbw_refresh_filename, |
542 | | |
543 | | .bdrv_child_perm = cbw_child_perm, |
544 | | |
545 | | .is_filter = true, |
546 | | }; |
547 | | |
548 | | BlockDriverState *bdrv_cbw_append(BlockDriverState *source, |
549 | | BlockDriverState *target, |
550 | | const char *filter_node_name, |
551 | | bool discard_source, |
552 | | uint64_t min_cluster_size, |
553 | | BlockCopyState **bcs, |
554 | | OnCbwError on_cbw_error, |
555 | | Error **errp) |
556 | 0 | { |
557 | 0 | BDRVCopyBeforeWriteState *state; |
558 | 0 | BlockDriverState *top; |
559 | 0 | QDict *opts; |
560 | 0 | int flags = BDRV_O_RDWR | (discard_source ? BDRV_O_CBW_DISCARD_SOURCE : 0); |
561 | |
|
562 | 0 | assert(source->total_sectors == target->total_sectors); |
563 | 0 | GLOBAL_STATE_CODE(); |
564 | |
|
565 | 0 | opts = qdict_new(); |
566 | 0 | qdict_put_str(opts, "driver", "copy-before-write"); |
567 | 0 | if (filter_node_name) { |
568 | 0 | qdict_put_str(opts, "node-name", filter_node_name); |
569 | 0 | } |
570 | 0 | qdict_put_str(opts, "file", bdrv_get_node_name(source)); |
571 | 0 | qdict_put_str(opts, "target", bdrv_get_node_name(target)); |
572 | 0 | qdict_put_str(opts, "on-cbw-error", OnCbwError_str(on_cbw_error)); |
573 | |
|
574 | 0 | if (min_cluster_size > INT64_MAX) { |
575 | 0 | error_setg(errp, "min-cluster-size too large: %" PRIu64 " > %" PRIi64, |
576 | 0 | min_cluster_size, INT64_MAX); |
577 | 0 | qobject_unref(opts); |
578 | 0 | return NULL; |
579 | 0 | } |
580 | 0 | qdict_put_int(opts, "min-cluster-size", (int64_t)min_cluster_size); |
581 | |
|
582 | 0 | top = bdrv_insert_node(source, opts, flags, errp); |
583 | 0 | if (!top) { |
584 | 0 | return NULL; |
585 | 0 | } |
586 | | |
587 | 0 | state = top->opaque; |
588 | 0 | *bcs = state->bcs; |
589 | |
|
590 | 0 | return top; |
591 | 0 | } |
592 | | |
593 | | void bdrv_cbw_drop(BlockDriverState *bs) |
594 | 0 | { |
595 | 0 | GLOBAL_STATE_CODE(); |
596 | 0 | bdrv_drop_filter(bs, &error_abort); |
597 | 0 | bdrv_unref(bs); |
598 | 0 | } |
599 | | |
600 | | static void cbw_init(void) |
601 | 61 | { |
602 | 61 | bdrv_register(&bdrv_cbw_filter); |
603 | 61 | } |
604 | | |
605 | | block_init(cbw_init); |