virtio-gpu: rename virgl source file.
[qemu/ar7.git] / block / backup-top.c
blob425e3778bec57f3b3fd345d22b85ef1704b98cb3
1 /*
2 * backup-top 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-2019 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/backup-top.h"
37 typedef struct BDRVBackupTopState {
38 BlockCopyState *bcs;
39 BdrvChild *target;
40 int64_t cluster_size;
41 } BDRVBackupTopState;
43 static coroutine_fn int backup_top_co_preadv(
44 BlockDriverState *bs, uint64_t offset, uint64_t bytes,
45 QEMUIOVector *qiov, int flags)
47 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
50 static coroutine_fn int backup_top_cbw(BlockDriverState *bs, uint64_t offset,
51 uint64_t bytes, BdrvRequestFlags flags)
53 BDRVBackupTopState *s = bs->opaque;
54 uint64_t off, end;
56 if (flags & BDRV_REQ_WRITE_UNCHANGED) {
57 return 0;
60 off = QEMU_ALIGN_DOWN(offset, s->cluster_size);
61 end = QEMU_ALIGN_UP(offset + bytes, s->cluster_size);
63 return block_copy(s->bcs, off, end - off, true);
66 static int coroutine_fn backup_top_co_pdiscard(BlockDriverState *bs,
67 int64_t offset, int bytes)
69 int ret = backup_top_cbw(bs, offset, bytes, 0);
70 if (ret < 0) {
71 return ret;
74 return bdrv_co_pdiscard(bs->backing, offset, bytes);
77 static int coroutine_fn backup_top_co_pwrite_zeroes(BlockDriverState *bs,
78 int64_t offset, int bytes, BdrvRequestFlags flags)
80 int ret = backup_top_cbw(bs, offset, bytes, flags);
81 if (ret < 0) {
82 return ret;
85 return bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags);
88 static coroutine_fn int backup_top_co_pwritev(BlockDriverState *bs,
89 uint64_t offset,
90 uint64_t bytes,
91 QEMUIOVector *qiov, int flags)
93 int ret = backup_top_cbw(bs, offset, bytes, flags);
94 if (ret < 0) {
95 return ret;
98 return bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags);
101 static int coroutine_fn backup_top_co_flush(BlockDriverState *bs)
103 if (!bs->backing) {
104 return 0;
107 return bdrv_co_flush(bs->backing->bs);
110 static void backup_top_refresh_filename(BlockDriverState *bs)
112 if (bs->backing == NULL) {
114 * we can be here after failed bdrv_attach_child in
115 * bdrv_set_backing_hd
117 return;
119 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
120 bs->backing->bs->filename);
123 static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c,
124 BdrvChildRole role,
125 BlockReopenQueue *reopen_queue,
126 uint64_t perm, uint64_t shared,
127 uint64_t *nperm, uint64_t *nshared)
129 if (!(role & BDRV_CHILD_FILTERED)) {
131 * Target child
133 * Share write to target (child_file), to not interfere
134 * with guest writes to its disk which may be in target backing chain.
135 * Can't resize during a backup block job because we check the size
136 * only upfront.
138 *nshared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
139 *nperm = BLK_PERM_WRITE;
140 } else {
141 /* Source child */
142 bdrv_default_perms(bs, c, role, reopen_queue,
143 perm, shared, nperm, nshared);
145 if (perm & BLK_PERM_WRITE) {
146 *nperm = *nperm | BLK_PERM_CONSISTENT_READ;
148 *nshared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
152 BlockDriver bdrv_backup_top_filter = {
153 .format_name = "backup-top",
154 .instance_size = sizeof(BDRVBackupTopState),
156 .bdrv_co_preadv = backup_top_co_preadv,
157 .bdrv_co_pwritev = backup_top_co_pwritev,
158 .bdrv_co_pwrite_zeroes = backup_top_co_pwrite_zeroes,
159 .bdrv_co_pdiscard = backup_top_co_pdiscard,
160 .bdrv_co_flush = backup_top_co_flush,
162 .bdrv_refresh_filename = backup_top_refresh_filename,
164 .bdrv_child_perm = backup_top_child_perm,
166 .is_filter = true,
169 BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
170 BlockDriverState *target,
171 const char *filter_node_name,
172 uint64_t cluster_size,
173 BackupPerf *perf,
174 BdrvRequestFlags write_flags,
175 BlockCopyState **bcs,
176 Error **errp)
178 ERRP_GUARD();
179 int ret;
180 BDRVBackupTopState *state;
181 BlockDriverState *top;
182 bool appended = false;
184 assert(source->total_sectors == target->total_sectors);
186 top = bdrv_new_open_driver(&bdrv_backup_top_filter, filter_node_name,
187 BDRV_O_RDWR, errp);
188 if (!top) {
189 return NULL;
192 state = top->opaque;
193 top->total_sectors = source->total_sectors;
194 top->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
195 (BDRV_REQ_FUA & source->supported_write_flags);
196 top->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
197 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
198 source->supported_zero_flags);
200 bdrv_ref(target);
201 state->target = bdrv_attach_child(top, target, "target", &child_of_bds,
202 BDRV_CHILD_DATA, errp);
203 if (!state->target) {
204 bdrv_unref(target);
205 bdrv_unref(top);
206 return NULL;
209 bdrv_drained_begin(source);
211 ret = bdrv_append(top, source, errp);
212 if (ret < 0) {
213 error_prepend(errp, "Cannot append backup-top filter: ");
214 goto fail;
216 appended = true;
218 state->cluster_size = cluster_size;
219 state->bcs = block_copy_state_new(top->backing, state->target,
220 cluster_size, perf->use_copy_range,
221 write_flags, errp);
222 if (!state->bcs) {
223 error_prepend(errp, "Cannot create block-copy-state: ");
224 goto fail;
226 *bcs = state->bcs;
228 bdrv_drained_end(source);
230 return top;
232 fail:
233 if (appended) {
234 bdrv_backup_top_drop(top);
235 } else {
236 bdrv_unref(top);
239 bdrv_drained_end(source);
241 return NULL;
244 void bdrv_backup_top_drop(BlockDriverState *bs)
246 BDRVBackupTopState *s = bs->opaque;
248 bdrv_drop_filter(bs, &error_abort);
250 block_copy_state_free(s->bcs);
252 bdrv_unref(bs);