]> git.proxmox.com Git - mirror_qemu.git/blame - block/backup-top.c
block: introduce bdrv_drop_filter()
[mirror_qemu.git] / block / backup-top.c
CommitLineData
7df7868b
VSO
1/*
2 * backup-top 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-2019 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
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"
34
35#include "block/backup-top.h"
36
37typedef struct BDRVBackupTopState {
38 BlockCopyState *bcs;
39 BdrvChild *target;
40 bool active;
397f4e9d 41 int64_t cluster_size;
7df7868b
VSO
42} BDRVBackupTopState;
43
44static coroutine_fn int backup_top_co_preadv(
45 BlockDriverState *bs, uint64_t offset, uint64_t bytes,
46 QEMUIOVector *qiov, int flags)
47{
705dde27
HR
48 BDRVBackupTopState *s = bs->opaque;
49
50 if (!s->active) {
51 return -EIO;
52 }
53
7df7868b
VSO
54 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
55}
56
57static coroutine_fn int backup_top_cbw(BlockDriverState *bs, uint64_t offset,
4bc267a7 58 uint64_t bytes, BdrvRequestFlags flags)
7df7868b 59{
00e30f05 60 BDRVBackupTopState *s = bs->opaque;
4bc267a7
VSO
61 uint64_t off, end;
62
705dde27
HR
63 if (!s->active) {
64 return -EIO;
65 }
66
4bc267a7
VSO
67 if (flags & BDRV_REQ_WRITE_UNCHANGED) {
68 return 0;
69 }
70
397f4e9d
VSO
71 off = QEMU_ALIGN_DOWN(offset, s->cluster_size);
72 end = QEMU_ALIGN_UP(offset + bytes, s->cluster_size);
7df7868b 73
143a6384 74 return block_copy(s->bcs, off, end - off, true);
7df7868b
VSO
75}
76
77static int coroutine_fn backup_top_co_pdiscard(BlockDriverState *bs,
78 int64_t offset, int bytes)
79{
4bc267a7 80 int ret = backup_top_cbw(bs, offset, bytes, 0);
7df7868b
VSO
81 if (ret < 0) {
82 return ret;
83 }
84
85 return bdrv_co_pdiscard(bs->backing, offset, bytes);
86}
87
88static int coroutine_fn backup_top_co_pwrite_zeroes(BlockDriverState *bs,
89 int64_t offset, int bytes, BdrvRequestFlags flags)
90{
4bc267a7 91 int ret = backup_top_cbw(bs, offset, bytes, flags);
7df7868b
VSO
92 if (ret < 0) {
93 return ret;
94 }
95
96 return bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags);
97}
98
99static coroutine_fn int backup_top_co_pwritev(BlockDriverState *bs,
100 uint64_t offset,
101 uint64_t bytes,
102 QEMUIOVector *qiov, int flags)
103{
4bc267a7
VSO
104 int ret = backup_top_cbw(bs, offset, bytes, flags);
105 if (ret < 0) {
106 return ret;
7df7868b
VSO
107 }
108
109 return bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags);
110}
111
112static int coroutine_fn backup_top_co_flush(BlockDriverState *bs)
113{
114 if (!bs->backing) {
115 return 0;
116 }
117
118 return bdrv_co_flush(bs->backing->bs);
119}
120
121static void backup_top_refresh_filename(BlockDriverState *bs)
122{
123 if (bs->backing == NULL) {
124 /*
125 * we can be here after failed bdrv_attach_child in
126 * bdrv_set_backing_hd
127 */
128 return;
129 }
130 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
131 bs->backing->bs->filename);
132}
133
134static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c,
bf8e925e 135 BdrvChildRole role,
7df7868b
VSO
136 BlockReopenQueue *reopen_queue,
137 uint64_t perm, uint64_t shared,
138 uint64_t *nperm, uint64_t *nshared)
139{
140 BDRVBackupTopState *s = bs->opaque;
141
142 if (!s->active) {
143 /*
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()).
149 */
150 *nperm = 0;
151 *nshared = BLK_PERM_ALL;
152 return;
153 }
154
25191e5f 155 if (!(role & BDRV_CHILD_FILTERED)) {
7df7868b
VSO
156 /*
157 * Target child
158 *
159 * Share write to target (child_file), to not interfere
160 * with guest writes to its disk which may be in target backing chain.
958a04bd
KW
161 * Can't resize during a backup block job because we check the size
162 * only upfront.
7df7868b 163 */
958a04bd 164 *nshared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
7df7868b
VSO
165 *nperm = BLK_PERM_WRITE;
166 } else {
167 /* Source child */
e5d8a406 168 bdrv_default_perms(bs, c, role, reopen_queue,
69dca43d 169 perm, shared, nperm, nshared);
7df7868b
VSO
170
171 if (perm & BLK_PERM_WRITE) {
172 *nperm = *nperm | BLK_PERM_CONSISTENT_READ;
173 }
958a04bd 174 *nshared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
7df7868b
VSO
175 }
176}
177
178BlockDriver bdrv_backup_top_filter = {
179 .format_name = "backup-top",
180 .instance_size = sizeof(BDRVBackupTopState),
181
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,
187
7df7868b
VSO
188 .bdrv_refresh_filename = backup_top_refresh_filename,
189
190 .bdrv_child_perm = backup_top_child_perm,
191
192 .is_filter = true,
193};
194
195BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
196 BlockDriverState *target,
197 const char *filter_node_name,
198 uint64_t cluster_size,
86c6a3b6 199 BackupPerf *perf,
7df7868b
VSO
200 BdrvRequestFlags write_flags,
201 BlockCopyState **bcs,
202 Error **errp)
203{
934aee14
VSO
204 ERRP_GUARD();
205 int ret;
7df7868b 206 BDRVBackupTopState *state;
958a04bd 207 BlockDriverState *top;
0df62f45 208 bool appended = false;
7df7868b 209
958a04bd
KW
210 assert(source->total_sectors == target->total_sectors);
211
212 top = bdrv_new_open_driver(&bdrv_backup_top_filter, filter_node_name,
213 BDRV_O_RDWR, errp);
7df7868b
VSO
214 if (!top) {
215 return NULL;
216 }
217
fb574de8 218 state = top->opaque;
4bc267a7
VSO
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);
7df7868b
VSO
225
226 bdrv_ref(target);
58944401
HR
227 state->target = bdrv_attach_child(top, target, "target", &child_of_bds,
228 BDRV_CHILD_DATA, errp);
7df7868b
VSO
229 if (!state->target) {
230 bdrv_unref(target);
231 bdrv_unref(top);
232 return NULL;
233 }
234
235 bdrv_drained_begin(source);
236
934aee14
VSO
237 ret = bdrv_append(top, source, errp);
238 if (ret < 0) {
239 error_prepend(errp, "Cannot append backup-top filter: ");
0df62f45 240 goto fail;
7df7868b 241 }
0df62f45 242 appended = true;
7df7868b
VSO
243
244 /*
245 * bdrv_append() finished successfully, now we can require permissions
246 * we want.
247 */
248 state->active = true;
934aee14
VSO
249 ret = bdrv_child_refresh_perms(top, top->backing, errp);
250 if (ret < 0) {
251 error_prepend(errp, "Cannot set permissions for backup-top filter: ");
0df62f45 252 goto fail;
7df7868b
VSO
253 }
254
397f4e9d 255 state->cluster_size = cluster_size;
00e30f05 256 state->bcs = block_copy_state_new(top->backing, state->target,
86c6a3b6 257 cluster_size, perf->use_copy_range,
934aee14
VSO
258 write_flags, errp);
259 if (!state->bcs) {
260 error_prepend(errp, "Cannot create block-copy-state: ");
0df62f45 261 goto fail;
7df7868b
VSO
262 }
263 *bcs = state->bcs;
264
265 bdrv_drained_end(source);
266
267 return top;
268
0df62f45
VSO
269fail:
270 if (appended) {
271 state->active = false;
272 bdrv_backup_top_drop(top);
273 } else {
274 bdrv_unref(top);
275 }
7df7868b 276
7df7868b 277 bdrv_drained_end(source);
7df7868b
VSO
278
279 return NULL;
280}
281
282void bdrv_backup_top_drop(BlockDriverState *bs)
283{
284 BDRVBackupTopState *s = bs->opaque;
7df7868b
VSO
285
286 bdrv_drained_begin(bs);
287
503ca126
HR
288 block_copy_state_free(s->bcs);
289
7df7868b
VSO
290 s->active = false;
291 bdrv_child_refresh_perms(bs, bs->backing, &error_abort);
2b088c60 292 bdrv_replace_node(bs, bs->backing->bs, &error_abort);
7df7868b
VSO
293 bdrv_set_backing_hd(bs, NULL, &error_abort);
294
295 bdrv_drained_end(bs);
296
297 bdrv_unref(bs);
7df7868b 298}