Merge remote-tracking branch 'remotes/stefanha-gitlab/tags/block-pull-request' into...
[qemu/ar7.git] / block / backup-top.c
blob589e8b651d221c4630d433ce0e9241a447dd514c
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 bool active;
41 int64_t cluster_size;
42 } BDRVBackupTopState;
44 static coroutine_fn int backup_top_co_preadv(
45 BlockDriverState *bs, uint64_t offset, uint64_t bytes,
46 QEMUIOVector *qiov, int flags)
48 BDRVBackupTopState *s = bs->opaque;
50 if (!s->active) {
51 return -EIO;
54 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
57 static coroutine_fn int backup_top_cbw(BlockDriverState *bs, uint64_t offset,
58 uint64_t bytes, BdrvRequestFlags flags)
60 BDRVBackupTopState *s = bs->opaque;
61 uint64_t off, end;
63 if (!s->active) {
64 return -EIO;
67 if (flags & BDRV_REQ_WRITE_UNCHANGED) {
68 return 0;
71 off = QEMU_ALIGN_DOWN(offset, s->cluster_size);
72 end = QEMU_ALIGN_UP(offset + bytes, s->cluster_size);
74 return block_copy(s->bcs, off, end - off, true);
77 static int coroutine_fn backup_top_co_pdiscard(BlockDriverState *bs,
78 int64_t offset, int bytes)
80 int ret = backup_top_cbw(bs, offset, bytes, 0);
81 if (ret < 0) {
82 return ret;
85 return bdrv_co_pdiscard(bs->backing, offset, bytes);
88 static int coroutine_fn backup_top_co_pwrite_zeroes(BlockDriverState *bs,
89 int64_t offset, int bytes, BdrvRequestFlags flags)
91 int ret = backup_top_cbw(bs, offset, bytes, flags);
92 if (ret < 0) {
93 return ret;
96 return bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags);
99 static coroutine_fn int backup_top_co_pwritev(BlockDriverState *bs,
100 uint64_t offset,
101 uint64_t bytes,
102 QEMUIOVector *qiov, int flags)
104 int ret = backup_top_cbw(bs, offset, bytes, flags);
105 if (ret < 0) {
106 return ret;
109 return bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags);
112 static int coroutine_fn backup_top_co_flush(BlockDriverState *bs)
114 if (!bs->backing) {
115 return 0;
118 return bdrv_co_flush(bs->backing->bs);
121 static void backup_top_refresh_filename(BlockDriverState *bs)
123 if (bs->backing == NULL) {
125 * we can be here after failed bdrv_attach_child in
126 * bdrv_set_backing_hd
128 return;
130 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
131 bs->backing->bs->filename);
134 static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c,
135 BdrvChildRole role,
136 BlockReopenQueue *reopen_queue,
137 uint64_t perm, uint64_t shared,
138 uint64_t *nperm, uint64_t *nshared)
140 BDRVBackupTopState *s = bs->opaque;
142 if (!s->active) {
144 * The filter node may be in process of bdrv_append(), which firstly do
145 * bdrv_set_backing_hd() and then bdrv_replace_node(). This means that
146 * we can't unshare BLK_PERM_WRITE during bdrv_append() operation. So,
147 * let's require nothing during bdrv_append() and refresh permissions
148 * after it (see bdrv_backup_top_append()).
150 *nperm = 0;
151 *nshared = BLK_PERM_ALL;
152 return;
155 if (!(role & BDRV_CHILD_FILTERED)) {
157 * Target child
159 * Share write to target (child_file), to not interfere
160 * with guest writes to its disk which may be in target backing chain.
161 * Can't resize during a backup block job because we check the size
162 * only upfront.
164 *nshared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
165 *nperm = BLK_PERM_WRITE;
166 } else {
167 /* Source child */
168 bdrv_default_perms(bs, c, role, reopen_queue,
169 perm, shared, nperm, nshared);
171 if (perm & BLK_PERM_WRITE) {
172 *nperm = *nperm | BLK_PERM_CONSISTENT_READ;
174 *nshared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
178 BlockDriver bdrv_backup_top_filter = {
179 .format_name = "backup-top",
180 .instance_size = sizeof(BDRVBackupTopState),
182 .bdrv_co_preadv = backup_top_co_preadv,
183 .bdrv_co_pwritev = backup_top_co_pwritev,
184 .bdrv_co_pwrite_zeroes = backup_top_co_pwrite_zeroes,
185 .bdrv_co_pdiscard = backup_top_co_pdiscard,
186 .bdrv_co_flush = backup_top_co_flush,
188 .bdrv_refresh_filename = backup_top_refresh_filename,
190 .bdrv_child_perm = backup_top_child_perm,
192 .is_filter = true,
195 BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
196 BlockDriverState *target,
197 const char *filter_node_name,
198 uint64_t cluster_size,
199 BackupPerf *perf,
200 BdrvRequestFlags write_flags,
201 BlockCopyState **bcs,
202 Error **errp)
204 ERRP_GUARD();
205 int ret;
206 BDRVBackupTopState *state;
207 BlockDriverState *top;
208 bool appended = false;
210 assert(source->total_sectors == target->total_sectors);
212 top = bdrv_new_open_driver(&bdrv_backup_top_filter, filter_node_name,
213 BDRV_O_RDWR, errp);
214 if (!top) {
215 return NULL;
218 state = top->opaque;
219 top->total_sectors = source->total_sectors;
220 top->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
221 (BDRV_REQ_FUA & source->supported_write_flags);
222 top->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
223 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
224 source->supported_zero_flags);
226 bdrv_ref(target);
227 state->target = bdrv_attach_child(top, target, "target", &child_of_bds,
228 BDRV_CHILD_DATA, errp);
229 if (!state->target) {
230 bdrv_unref(target);
231 bdrv_unref(top);
232 return NULL;
235 bdrv_drained_begin(source);
237 bdrv_ref(top);
238 ret = bdrv_append(top, source, errp);
239 if (ret < 0) {
240 error_prepend(errp, "Cannot append backup-top filter: ");
241 goto fail;
243 appended = true;
246 * bdrv_append() finished successfully, now we can require permissions
247 * we want.
249 state->active = true;
250 ret = bdrv_child_refresh_perms(top, top->backing, errp);
251 if (ret < 0) {
252 error_prepend(errp, "Cannot set permissions for backup-top filter: ");
253 goto fail;
256 state->cluster_size = cluster_size;
257 state->bcs = block_copy_state_new(top->backing, state->target,
258 cluster_size, perf->use_copy_range,
259 write_flags, errp);
260 if (!state->bcs) {
261 error_prepend(errp, "Cannot create block-copy-state: ");
262 goto fail;
264 *bcs = state->bcs;
266 bdrv_drained_end(source);
268 return top;
270 fail:
271 if (appended) {
272 state->active = false;
273 bdrv_backup_top_drop(top);
274 } else {
275 bdrv_unref(top);
278 bdrv_drained_end(source);
280 return NULL;
283 void bdrv_backup_top_drop(BlockDriverState *bs)
285 BDRVBackupTopState *s = bs->opaque;
287 bdrv_drained_begin(bs);
289 block_copy_state_free(s->bcs);
291 s->active = false;
292 bdrv_child_refresh_perms(bs, bs->backing, &error_abort);
293 bdrv_replace_node(bs, bs->backing->bs, &error_abort);
294 bdrv_set_backing_hd(bs, NULL, &error_abort);
296 bdrv_drained_end(bs);
298 bdrv_unref(bs);