1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
41} BDRVBackupTopState;
42
43static 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
50static coroutine_fn int backup_top_cbw(BlockDriverState *bs, uint64_t offset,
51 uint64_t bytes)
52{
53 BDRVBackupTopState *s = bs->opaque;
54 uint64_t end = QEMU_ALIGN_UP(offset + bytes, s->bcs->cluster_size);
55 uint64_t off = QEMU_ALIGN_DOWN(offset, s->bcs->cluster_size);
56
57 return block_copy(s->bcs, off, end - off, NULL);
58}
59
60static int coroutine_fn backup_top_co_pdiscard(BlockDriverState *bs,
61 int64_t offset, int bytes)
62{
63 int ret = backup_top_cbw(bs, offset, bytes);
64 if (ret < 0) {
65 return ret;
66 }
67
68 return bdrv_co_pdiscard(bs->backing, offset, bytes);
69}
70
71static int coroutine_fn backup_top_co_pwrite_zeroes(BlockDriverState *bs,
72 int64_t offset, int bytes, BdrvRequestFlags flags)
73{
74 int ret = backup_top_cbw(bs, offset, bytes);
75 if (ret < 0) {
76 return ret;
77 }
78
79 return bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags);
80}
81
82static coroutine_fn int backup_top_co_pwritev(BlockDriverState *bs,
83 uint64_t offset,
84 uint64_t bytes,
85 QEMUIOVector *qiov, int flags)
86{
87 if (!(flags & BDRV_REQ_WRITE_UNCHANGED)) {
88 int ret = backup_top_cbw(bs, offset, bytes);
89 if (ret < 0) {
90 return ret;
91 }
92 }
93
94 return bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags);
95}
96
97static int coroutine_fn backup_top_co_flush(BlockDriverState *bs)
98{
99 if (!bs->backing) {
100 return 0;
101 }
102
103 return bdrv_co_flush(bs->backing->bs);
104}
105
106static void backup_top_refresh_filename(BlockDriverState *bs)
107{
108 if (bs->backing == NULL) {
109
110
111
112
113 return;
114 }
115 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
116 bs->backing->bs->filename);
117}
118
119static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c,
120 const BdrvChildRole *role,
121 BlockReopenQueue *reopen_queue,
122 uint64_t perm, uint64_t shared,
123 uint64_t *nperm, uint64_t *nshared)
124{
125 BDRVBackupTopState *s = bs->opaque;
126
127 if (!s->active) {
128
129
130
131
132
133
134
135 *nperm = 0;
136 *nshared = BLK_PERM_ALL;
137 return;
138 }
139
140 if (role == &child_file) {
141
142
143
144
145
146
147 *nshared = BLK_PERM_ALL;
148 *nperm = BLK_PERM_WRITE;
149 } else {
150
151 bdrv_filter_default_perms(bs, c, role, reopen_queue, perm, shared,
152 nperm, nshared);
153
154 if (perm & BLK_PERM_WRITE) {
155 *nperm = *nperm | BLK_PERM_CONSISTENT_READ;
156 }
157 *nshared &= ~BLK_PERM_WRITE;
158 }
159}
160
161BlockDriver bdrv_backup_top_filter = {
162 .format_name = "backup-top",
163 .instance_size = sizeof(BDRVBackupTopState),
164
165 .bdrv_co_preadv = backup_top_co_preadv,
166 .bdrv_co_pwritev = backup_top_co_pwritev,
167 .bdrv_co_pwrite_zeroes = backup_top_co_pwrite_zeroes,
168 .bdrv_co_pdiscard = backup_top_co_pdiscard,
169 .bdrv_co_flush = backup_top_co_flush,
170
171 .bdrv_co_block_status = bdrv_co_block_status_from_backing,
172
173 .bdrv_refresh_filename = backup_top_refresh_filename,
174
175 .bdrv_child_perm = backup_top_child_perm,
176
177 .is_filter = true,
178};
179
180BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
181 BlockDriverState *target,
182 const char *filter_node_name,
183 uint64_t cluster_size,
184 BdrvRequestFlags write_flags,
185 BlockCopyState **bcs,
186 Error **errp)
187{
188 Error *local_err = NULL;
189 BDRVBackupTopState *state;
190 BlockDriverState *top = bdrv_new_open_driver(&bdrv_backup_top_filter,
191 filter_node_name,
192 BDRV_O_RDWR, errp);
193 bool appended = false;
194
195 if (!top) {
196 return NULL;
197 }
198
199 top->total_sectors = source->total_sectors;
200 state = top->opaque;
201
202 bdrv_ref(target);
203 state->target = bdrv_attach_child(top, target, "target", &child_file, errp);
204 if (!state->target) {
205 bdrv_unref(target);
206 bdrv_unref(top);
207 return NULL;
208 }
209
210 bdrv_drained_begin(source);
211
212 bdrv_ref(top);
213 bdrv_append(top, source, &local_err);
214 if (local_err) {
215 error_prepend(&local_err, "Cannot append backup-top filter: ");
216 goto fail;
217 }
218 appended = true;
219
220
221
222
223
224 state->active = true;
225 bdrv_child_refresh_perms(top, top->backing, &local_err);
226 if (local_err) {
227 error_prepend(&local_err,
228 "Cannot set permissions for backup-top filter: ");
229 goto fail;
230 }
231
232 state->bcs = block_copy_state_new(top->backing, state->target,
233 cluster_size, write_flags, &local_err);
234 if (local_err) {
235 error_prepend(&local_err, "Cannot create block-copy-state: ");
236 goto fail;
237 }
238 *bcs = state->bcs;
239
240 bdrv_drained_end(source);
241
242 return top;
243
244fail:
245 if (appended) {
246 state->active = false;
247 bdrv_backup_top_drop(top);
248 } else {
249 bdrv_unref(top);
250 }
251
252 bdrv_drained_end(source);
253 error_propagate(errp, local_err);
254
255 return NULL;
256}
257
258void bdrv_backup_top_drop(BlockDriverState *bs)
259{
260 BDRVBackupTopState *s = bs->opaque;
261 AioContext *aio_context = bdrv_get_aio_context(bs);
262
263 aio_context_acquire(aio_context);
264
265 bdrv_drained_begin(bs);
266
267 block_copy_state_free(s->bcs);
268
269 s->active = false;
270 bdrv_child_refresh_perms(bs, bs->backing, &error_abort);
271 bdrv_replace_node(bs, backing_bs(bs), &error_abort);
272 bdrv_set_backing_hd(bs, NULL, &error_abort);
273
274 bdrv_drained_end(bs);
275
276 bdrv_unref(bs);
277
278 aio_context_release(aio_context);
279}
280