]> git.proxmox.com Git - mirror_qemu.git/blob - block/backup-top.c
q800: fix coverity warning CID 1412799
[mirror_qemu.git] / block / backup-top.c
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
37 typedef struct BDRVBackupTopState {
38 BlockCopyState *bcs;
39 BdrvChild *target;
40 bool active;
41 } BDRVBackupTopState;
42
43 static coroutine_fn int backup_top_co_preadv(
44 BlockDriverState *bs, uint64_t offset, uint64_t bytes,
45 QEMUIOVector *qiov, int flags)
46 {
47 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
48 }
49
50 static coroutine_fn int backup_top_cbw(BlockDriverState *bs, uint64_t offset,
51 uint64_t bytes, BdrvRequestFlags flags)
52 {
53 BDRVBackupTopState *s = bs->opaque;
54 uint64_t off, end;
55
56 if (flags & BDRV_REQ_WRITE_UNCHANGED) {
57 return 0;
58 }
59
60 off = QEMU_ALIGN_DOWN(offset, s->bcs->cluster_size);
61 end = QEMU_ALIGN_UP(offset + bytes, s->bcs->cluster_size);
62
63 return block_copy(s->bcs, off, end - off, NULL);
64 }
65
66 static int coroutine_fn backup_top_co_pdiscard(BlockDriverState *bs,
67 int64_t offset, int bytes)
68 {
69 int ret = backup_top_cbw(bs, offset, bytes, 0);
70 if (ret < 0) {
71 return ret;
72 }
73
74 return bdrv_co_pdiscard(bs->backing, offset, bytes);
75 }
76
77 static int coroutine_fn backup_top_co_pwrite_zeroes(BlockDriverState *bs,
78 int64_t offset, int bytes, BdrvRequestFlags flags)
79 {
80 int ret = backup_top_cbw(bs, offset, bytes, flags);
81 if (ret < 0) {
82 return ret;
83 }
84
85 return bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags);
86 }
87
88 static coroutine_fn int backup_top_co_pwritev(BlockDriverState *bs,
89 uint64_t offset,
90 uint64_t bytes,
91 QEMUIOVector *qiov, int flags)
92 {
93 int ret = backup_top_cbw(bs, offset, bytes, flags);
94 if (ret < 0) {
95 return ret;
96 }
97
98 return bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags);
99 }
100
101 static int coroutine_fn backup_top_co_flush(BlockDriverState *bs)
102 {
103 if (!bs->backing) {
104 return 0;
105 }
106
107 return bdrv_co_flush(bs->backing->bs);
108 }
109
110 static void backup_top_refresh_filename(BlockDriverState *bs)
111 {
112 if (bs->backing == NULL) {
113 /*
114 * we can be here after failed bdrv_attach_child in
115 * bdrv_set_backing_hd
116 */
117 return;
118 }
119 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
120 bs->backing->bs->filename);
121 }
122
123 static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c,
124 const BdrvChildRole *role,
125 BlockReopenQueue *reopen_queue,
126 uint64_t perm, uint64_t shared,
127 uint64_t *nperm, uint64_t *nshared)
128 {
129 BDRVBackupTopState *s = bs->opaque;
130
131 if (!s->active) {
132 /*
133 * The filter node may be in process of bdrv_append(), which firstly do
134 * bdrv_set_backing_hd() and then bdrv_replace_node(). This means that
135 * we can't unshare BLK_PERM_WRITE during bdrv_append() operation. So,
136 * let's require nothing during bdrv_append() and refresh permissions
137 * after it (see bdrv_backup_top_append()).
138 */
139 *nperm = 0;
140 *nshared = BLK_PERM_ALL;
141 return;
142 }
143
144 if (role == &child_file) {
145 /*
146 * Target child
147 *
148 * Share write to target (child_file), to not interfere
149 * with guest writes to its disk which may be in target backing chain.
150 */
151 *nshared = BLK_PERM_ALL;
152 *nperm = BLK_PERM_WRITE;
153 } else {
154 /* Source child */
155 bdrv_filter_default_perms(bs, c, role, reopen_queue, perm, shared,
156 nperm, nshared);
157
158 if (perm & BLK_PERM_WRITE) {
159 *nperm = *nperm | BLK_PERM_CONSISTENT_READ;
160 }
161 *nshared &= ~BLK_PERM_WRITE;
162 }
163 }
164
165 BlockDriver bdrv_backup_top_filter = {
166 .format_name = "backup-top",
167 .instance_size = sizeof(BDRVBackupTopState),
168
169 .bdrv_co_preadv = backup_top_co_preadv,
170 .bdrv_co_pwritev = backup_top_co_pwritev,
171 .bdrv_co_pwrite_zeroes = backup_top_co_pwrite_zeroes,
172 .bdrv_co_pdiscard = backup_top_co_pdiscard,
173 .bdrv_co_flush = backup_top_co_flush,
174
175 .bdrv_co_block_status = bdrv_co_block_status_from_backing,
176
177 .bdrv_refresh_filename = backup_top_refresh_filename,
178
179 .bdrv_child_perm = backup_top_child_perm,
180
181 .is_filter = true,
182 };
183
184 BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
185 BlockDriverState *target,
186 const char *filter_node_name,
187 uint64_t cluster_size,
188 BdrvRequestFlags write_flags,
189 BlockCopyState **bcs,
190 Error **errp)
191 {
192 Error *local_err = NULL;
193 BDRVBackupTopState *state;
194 BlockDriverState *top = bdrv_new_open_driver(&bdrv_backup_top_filter,
195 filter_node_name,
196 BDRV_O_RDWR, errp);
197 bool appended = false;
198
199 if (!top) {
200 return NULL;
201 }
202
203 state = top->opaque;
204 top->total_sectors = source->total_sectors;
205 top->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
206 (BDRV_REQ_FUA & source->supported_write_flags);
207 top->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
208 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
209 source->supported_zero_flags);
210
211 bdrv_ref(target);
212 state->target = bdrv_attach_child(top, target, "target", &child_file, errp);
213 if (!state->target) {
214 bdrv_unref(target);
215 bdrv_unref(top);
216 return NULL;
217 }
218
219 bdrv_drained_begin(source);
220
221 bdrv_ref(top);
222 bdrv_append(top, source, &local_err);
223 if (local_err) {
224 error_prepend(&local_err, "Cannot append backup-top filter: ");
225 goto fail;
226 }
227 appended = true;
228
229 /*
230 * bdrv_append() finished successfully, now we can require permissions
231 * we want.
232 */
233 state->active = true;
234 bdrv_child_refresh_perms(top, top->backing, &local_err);
235 if (local_err) {
236 error_prepend(&local_err,
237 "Cannot set permissions for backup-top filter: ");
238 goto fail;
239 }
240
241 state->bcs = block_copy_state_new(top->backing, state->target,
242 cluster_size, write_flags, &local_err);
243 if (local_err) {
244 error_prepend(&local_err, "Cannot create block-copy-state: ");
245 goto fail;
246 }
247 *bcs = state->bcs;
248
249 bdrv_drained_end(source);
250
251 return top;
252
253 fail:
254 if (appended) {
255 state->active = false;
256 bdrv_backup_top_drop(top);
257 } else {
258 bdrv_unref(top);
259 }
260
261 bdrv_drained_end(source);
262 error_propagate(errp, local_err);
263
264 return NULL;
265 }
266
267 void bdrv_backup_top_drop(BlockDriverState *bs)
268 {
269 BDRVBackupTopState *s = bs->opaque;
270
271 bdrv_drained_begin(bs);
272
273 block_copy_state_free(s->bcs);
274
275 s->active = false;
276 bdrv_child_refresh_perms(bs, bs->backing, &error_abort);
277 bdrv_replace_node(bs, backing_bs(bs), &error_abort);
278 bdrv_set_backing_hd(bs, NULL, &error_abort);
279
280 bdrv_drained_end(bs);
281
282 bdrv_unref(bs);
283 }