block/copy-before-write: bdrv_cbw_append(): drop unused compress arg
[qemu.git] / block / copy-before-write.c
blob1e7180760abb5f83203fe08707f18282beeb82d9
1 /*
2 * copy-before-write filter driver
4 * The driver performs Copy-Before-Write (CBW) operation: it is injected above
5 * some node, and before each write it copies _old_ data to the target node.
7 * Copyright (c) 2018-2021 Virtuozzo International GmbH.
9 * Author:
10 * Sementsov-Ogievskiy Vladimir <vsementsov@virtuozzo.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "qemu/osdep.h"
28 #include "sysemu/block-backend.h"
29 #include "qemu/cutils.h"
30 #include "qapi/error.h"
31 #include "block/block_int.h"
32 #include "block/qdict.h"
33 #include "block/block-copy.h"
35 #include "block/copy-before-write.h"
37 typedef struct BDRVCopyBeforeWriteState {
38 BlockCopyState *bcs;
39 BdrvChild *target;
40 } BDRVCopyBeforeWriteState;
42 static coroutine_fn int cbw_co_preadv(
43 BlockDriverState *bs, uint64_t offset, uint64_t bytes,
44 QEMUIOVector *qiov, int flags)
46 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
49 static coroutine_fn int cbw_do_copy_before_write(BlockDriverState *bs,
50 uint64_t offset, uint64_t bytes, BdrvRequestFlags flags)
52 BDRVCopyBeforeWriteState *s = bs->opaque;
53 uint64_t off, end;
54 int64_t cluster_size = block_copy_cluster_size(s->bcs);
56 if (flags & BDRV_REQ_WRITE_UNCHANGED) {
57 return 0;
60 off = QEMU_ALIGN_DOWN(offset, cluster_size);
61 end = QEMU_ALIGN_UP(offset + bytes, cluster_size);
63 return block_copy(s->bcs, off, end - off, true);
66 static int coroutine_fn cbw_co_pdiscard(BlockDriverState *bs,
67 int64_t offset, int bytes)
69 int ret = cbw_do_copy_before_write(bs, offset, bytes, 0);
70 if (ret < 0) {
71 return ret;
74 return bdrv_co_pdiscard(bs->file, offset, bytes);
77 static int coroutine_fn cbw_co_pwrite_zeroes(BlockDriverState *bs,
78 int64_t offset, int bytes, BdrvRequestFlags flags)
80 int ret = cbw_do_copy_before_write(bs, offset, bytes, flags);
81 if (ret < 0) {
82 return ret;
85 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
88 static coroutine_fn int cbw_co_pwritev(BlockDriverState *bs,
89 uint64_t offset,
90 uint64_t bytes,
91 QEMUIOVector *qiov, int flags)
93 int ret = cbw_do_copy_before_write(bs, offset, bytes, flags);
94 if (ret < 0) {
95 return ret;
98 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
101 static int coroutine_fn cbw_co_flush(BlockDriverState *bs)
103 if (!bs->file) {
104 return 0;
107 return bdrv_co_flush(bs->file->bs);
110 static void cbw_refresh_filename(BlockDriverState *bs)
112 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
113 bs->file->bs->filename);
116 static void cbw_child_perm(BlockDriverState *bs, BdrvChild *c,
117 BdrvChildRole role,
118 BlockReopenQueue *reopen_queue,
119 uint64_t perm, uint64_t shared,
120 uint64_t *nperm, uint64_t *nshared)
122 if (!(role & BDRV_CHILD_FILTERED)) {
124 * Target child
126 * Share write to target (child_file), to not interfere
127 * with guest writes to its disk which may be in target backing chain.
128 * Can't resize during a backup block job because we check the size
129 * only upfront.
131 *nshared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
132 *nperm = BLK_PERM_WRITE;
133 } else {
134 /* Source child */
135 bdrv_default_perms(bs, c, role, reopen_queue,
136 perm, shared, nperm, nshared);
138 if (!QLIST_EMPTY(&bs->parents)) {
139 if (perm & BLK_PERM_WRITE) {
140 *nperm = *nperm | BLK_PERM_CONSISTENT_READ;
142 *nshared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
147 static int cbw_init(BlockDriverState *bs, BlockDriverState *source,
148 BlockDriverState *target, Error **errp)
150 BDRVCopyBeforeWriteState *s = bs->opaque;
152 bdrv_ref(target);
153 s->target = bdrv_attach_child(bs, target, "target", &child_of_bds,
154 BDRV_CHILD_DATA, errp);
155 if (!s->target) {
156 error_prepend(errp, "Cannot attach target child: ");
157 return -EINVAL;
160 bdrv_ref(source);
161 bs->file = bdrv_attach_child(bs, source, "file", &child_of_bds,
162 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
163 errp);
164 if (!bs->file) {
165 error_prepend(errp, "Cannot attach file child: ");
166 return -EINVAL;
169 bs->total_sectors = bs->file->bs->total_sectors;
170 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
171 (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
172 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
173 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
174 bs->file->bs->supported_zero_flags);
176 s->bcs = block_copy_state_new(bs->file, s->target, false, false, errp);
177 if (!s->bcs) {
178 error_prepend(errp, "Cannot create block-copy-state: ");
179 return -EINVAL;
182 return 0;
185 BlockDriver bdrv_cbw_filter = {
186 .format_name = "copy-before-write",
187 .instance_size = sizeof(BDRVCopyBeforeWriteState),
189 .bdrv_co_preadv = cbw_co_preadv,
190 .bdrv_co_pwritev = cbw_co_pwritev,
191 .bdrv_co_pwrite_zeroes = cbw_co_pwrite_zeroes,
192 .bdrv_co_pdiscard = cbw_co_pdiscard,
193 .bdrv_co_flush = cbw_co_flush,
195 .bdrv_refresh_filename = cbw_refresh_filename,
197 .bdrv_child_perm = cbw_child_perm,
199 .is_filter = true,
202 BlockDriverState *bdrv_cbw_append(BlockDriverState *source,
203 BlockDriverState *target,
204 const char *filter_node_name,
205 BlockCopyState **bcs,
206 Error **errp)
208 ERRP_GUARD();
209 int ret;
210 BDRVCopyBeforeWriteState *state;
211 BlockDriverState *top;
213 assert(source->total_sectors == target->total_sectors);
215 top = bdrv_new_open_driver(&bdrv_cbw_filter, filter_node_name,
216 BDRV_O_RDWR, errp);
217 if (!top) {
218 error_prepend(errp, "Cannot open driver: ");
219 return NULL;
221 state = top->opaque;
223 ret = cbw_init(top, source, target, errp);
224 if (ret < 0) {
225 goto fail;
228 bdrv_drained_begin(source);
229 ret = bdrv_replace_node(source, top, errp);
230 bdrv_drained_end(source);
231 if (ret < 0) {
232 error_prepend(errp, "Cannot append copy-before-write filter: ");
233 goto fail;
236 *bcs = state->bcs;
238 return top;
240 fail:
241 block_copy_state_free(state->bcs);
242 bdrv_unref(top);
243 return NULL;
246 void bdrv_cbw_drop(BlockDriverState *bs)
248 BDRVCopyBeforeWriteState *s = bs->opaque;
250 bdrv_drop_filter(bs, &error_abort);
252 block_copy_state_free(s->bcs);
254 bdrv_unref(bs);