]> git.proxmox.com Git - pve-qemu.git/blob - debian/patches/pve/0047-block-add-alloc-track-driver.patch
backup: add patch to initialize bcs bitmap early enough for PBS
[pve-qemu.git] / debian / patches / pve / 0047-block-add-alloc-track-driver.patch
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Stefan Reiter <s.reiter@proxmox.com>
3 Date: Mon, 7 Dec 2020 15:21:03 +0100
4 Subject: [PATCH] block: add alloc-track driver
5
6 Add a new filter node 'alloc-track', which seperates reads and writes to
7 different children, thus allowing to put a backing image behind any
8 blockdev (regardless of driver support). Since we can't detect any
9 pre-allocated blocks, we can only track new writes, hence the write
10 target ('file') for this node must always be empty.
11
12 Intended use case is for live restoring, i.e. add a backup image as a
13 block device into a VM, then put an alloc-track on the restore target
14 and set the backup as backing. With this, one can use a regular
15 'block-stream' to restore the image, while the VM can already run in the
16 background. Copy-on-read will help make progress as the VM reads as
17 well.
18
19 This only worked if the target supports backing images, so up until now
20 only for qcow2, with alloc-track any driver for the target can be used.
21
22 If 'auto-remove' is set, alloc-track will automatically detach itself
23 once the backing image is removed. It will be replaced by 'file'.
24
25 Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
26 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
27 [adapt to changed function signatures]
28 Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
29 ---
30 block/alloc-track.c | 350 ++++++++++++++++++++++++++++++++++++++++++++
31 block/meson.build | 1 +
32 2 files changed, 351 insertions(+)
33 create mode 100644 block/alloc-track.c
34
35 diff --git a/block/alloc-track.c b/block/alloc-track.c
36 new file mode 100644
37 index 0000000000..6b50fbe537
38 --- /dev/null
39 +++ b/block/alloc-track.c
40 @@ -0,0 +1,350 @@
41 +/*
42 + * Node to allow backing images to be applied to any node. Assumes a blank
43 + * image to begin with, only new writes are tracked as allocated, thus this
44 + * must never be put on a node that already contains data.
45 + *
46 + * Copyright (c) 2020 Proxmox Server Solutions GmbH
47 + * Copyright (c) 2020 Stefan Reiter <s.reiter@proxmox.com>
48 + *
49 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
50 + * See the COPYING file in the top-level directory.
51 + */
52 +
53 +#include "qemu/osdep.h"
54 +#include "qapi/error.h"
55 +#include "block/block_int.h"
56 +#include "qapi/qmp/qdict.h"
57 +#include "qapi/qmp/qstring.h"
58 +#include "qemu/cutils.h"
59 +#include "qemu/option.h"
60 +#include "qemu/module.h"
61 +#include "sysemu/block-backend.h"
62 +
63 +#define TRACK_OPT_AUTO_REMOVE "auto-remove"
64 +
65 +typedef enum DropState {
66 + DropNone,
67 + DropRequested,
68 + DropInProgress,
69 +} DropState;
70 +
71 +typedef struct {
72 + BdrvDirtyBitmap *bitmap;
73 + DropState drop_state;
74 + bool auto_remove;
75 +} BDRVAllocTrackState;
76 +
77 +static QemuOptsList runtime_opts = {
78 + .name = "alloc-track",
79 + .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
80 + .desc = {
81 + {
82 + .name = TRACK_OPT_AUTO_REMOVE,
83 + .type = QEMU_OPT_BOOL,
84 + .help = "automatically replace this node with 'file' when 'backing'"
85 + "is detached",
86 + },
87 + { /* end of list */ }
88 + },
89 +};
90 +
91 +static void track_refresh_limits(BlockDriverState *bs, Error **errp)
92 +{
93 + BlockDriverInfo bdi;
94 +
95 + if (!bs->file) {
96 + return;
97 + }
98 +
99 + /* always use alignment from underlying write device so RMW cycle for
100 + * bdrv_pwritev reads data from our backing via track_co_preadv (no partial
101 + * cluster allocation in 'file') */
102 + bdrv_get_info(bs->file->bs, &bdi);
103 + bs->bl.request_alignment = MAX(bs->file->bs->bl.request_alignment,
104 + MAX(bdi.cluster_size, BDRV_SECTOR_SIZE));
105 +}
106 +
107 +static int track_open(BlockDriverState *bs, QDict *options, int flags,
108 + Error **errp)
109 +{
110 + BDRVAllocTrackState *s = bs->opaque;
111 + QemuOpts *opts;
112 + Error *local_err = NULL;
113 + int ret = 0;
114 +
115 + opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
116 + qemu_opts_absorb_qdict(opts, options, &local_err);
117 + if (local_err) {
118 + error_propagate(errp, local_err);
119 + ret = -EINVAL;
120 + goto fail;
121 + }
122 +
123 + s->auto_remove = qemu_opt_get_bool(opts, TRACK_OPT_AUTO_REMOVE, false);
124 +
125 + /* open the target (write) node, backing will be attached by block layer */
126 + bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
127 + BDRV_CHILD_DATA | BDRV_CHILD_METADATA, false,
128 + &local_err);
129 + if (local_err) {
130 + ret = -EINVAL;
131 + error_propagate(errp, local_err);
132 + goto fail;
133 + }
134 +
135 + track_refresh_limits(bs, errp);
136 + uint64_t gran = bs->bl.request_alignment;
137 + s->bitmap = bdrv_create_dirty_bitmap(bs->file->bs, gran, NULL, &local_err);
138 + if (local_err) {
139 + ret = -EIO;
140 + error_propagate(errp, local_err);
141 + goto fail;
142 + }
143 +
144 + s->drop_state = DropNone;
145 +
146 +fail:
147 + if (ret < 0) {
148 + bdrv_unref_child(bs, bs->file);
149 + if (s->bitmap) {
150 + bdrv_release_dirty_bitmap(s->bitmap);
151 + }
152 + }
153 + qemu_opts_del(opts);
154 + return ret;
155 +}
156 +
157 +static void track_close(BlockDriverState *bs)
158 +{
159 + BDRVAllocTrackState *s = bs->opaque;
160 + if (s->bitmap) {
161 + bdrv_release_dirty_bitmap(s->bitmap);
162 + }
163 +}
164 +
165 +static int64_t track_getlength(BlockDriverState *bs)
166 +{
167 + return bdrv_getlength(bs->file->bs);
168 +}
169 +
170 +static int coroutine_fn track_co_preadv(BlockDriverState *bs,
171 + int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags)
172 +{
173 + BDRVAllocTrackState *s = bs->opaque;
174 + QEMUIOVector local_qiov;
175 + int ret;
176 +
177 + /* 'cur_offset' is relative to 'offset', 'local_offset' to image start */
178 + uint64_t cur_offset, local_offset;
179 + int64_t local_bytes;
180 + bool alloc;
181 +
182 + if (offset < 0 || bytes < 0) {
183 + fprintf(stderr, "unexpected negative 'offset' or 'bytes' value!\n");
184 + return -EINVAL;
185 + }
186 +
187 + /* a read request can span multiple granularity-sized chunks, and can thus
188 + * contain blocks with different allocation status - we could just iterate
189 + * granularity-wise, but for better performance use bdrv_dirty_bitmap_next_X
190 + * to find the next flip and consider everything up to that in one go */
191 + for (cur_offset = 0; cur_offset < bytes; cur_offset += local_bytes) {
192 + local_offset = offset + cur_offset;
193 + alloc = bdrv_dirty_bitmap_get(s->bitmap, local_offset);
194 + if (alloc) {
195 + local_bytes = bdrv_dirty_bitmap_next_zero(s->bitmap, local_offset,
196 + bytes - cur_offset);
197 + } else {
198 + local_bytes = bdrv_dirty_bitmap_next_dirty(s->bitmap, local_offset,
199 + bytes - cur_offset);
200 + }
201 +
202 + /* _bitmap_next_X return is -1 if no end found within limit, otherwise
203 + * offset of next flip (to start of image) */
204 + local_bytes = local_bytes < 0 ?
205 + bytes - cur_offset :
206 + local_bytes - local_offset;
207 +
208 + qemu_iovec_init_slice(&local_qiov, qiov, cur_offset, local_bytes);
209 +
210 + if (alloc) {
211 + ret = bdrv_co_preadv(bs->file, local_offset, local_bytes,
212 + &local_qiov, flags);
213 + } else if (bs->backing) {
214 + ret = bdrv_co_preadv(bs->backing, local_offset, local_bytes,
215 + &local_qiov, flags);
216 + } else {
217 + ret = qemu_iovec_memset(&local_qiov, cur_offset, 0, local_bytes);
218 + }
219 +
220 + if (ret != 0) {
221 + break;
222 + }
223 + }
224 +
225 + return ret;
226 +}
227 +
228 +static int coroutine_fn track_co_pwritev(BlockDriverState *bs,
229 + int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags)
230 +{
231 + return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
232 +}
233 +
234 +static int coroutine_fn track_co_pwrite_zeroes(BlockDriverState *bs,
235 + int64_t offset, int64_t bytes, BdrvRequestFlags flags)
236 +{
237 + return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
238 +}
239 +
240 +static int coroutine_fn track_co_pdiscard(BlockDriverState *bs,
241 + int64_t offset, int64_t bytes)
242 +{
243 + return bdrv_co_pdiscard(bs->file, offset, bytes);
244 +}
245 +
246 +static coroutine_fn int track_co_flush(BlockDriverState *bs)
247 +{
248 + return bdrv_co_flush(bs->file->bs);
249 +}
250 +
251 +static int coroutine_fn track_co_block_status(BlockDriverState *bs,
252 + bool want_zero,
253 + int64_t offset,
254 + int64_t bytes,
255 + int64_t *pnum,
256 + int64_t *map,
257 + BlockDriverState **file)
258 +{
259 + BDRVAllocTrackState *s = bs->opaque;
260 +
261 + bool alloc = bdrv_dirty_bitmap_get(s->bitmap, offset);
262 + int64_t next_flipped;
263 + if (alloc) {
264 + next_flipped = bdrv_dirty_bitmap_next_zero(s->bitmap, offset, bytes);
265 + } else {
266 + next_flipped = bdrv_dirty_bitmap_next_dirty(s->bitmap, offset, bytes);
267 + }
268 +
269 + /* in case not the entire region has the same state, we need to set pnum to
270 + * indicate for how many bytes our result is valid */
271 + *pnum = next_flipped == -1 ? bytes : next_flipped - offset;
272 + *map = offset;
273 +
274 + if (alloc) {
275 + *file = bs->file->bs;
276 + return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID;
277 + } else if (bs->backing) {
278 + *file = bs->backing->bs;
279 + }
280 + return 0;
281 +}
282 +
283 +static void track_child_perm(BlockDriverState *bs, BdrvChild *c,
284 + BdrvChildRole role, BlockReopenQueue *reopen_queue,
285 + uint64_t perm, uint64_t shared,
286 + uint64_t *nperm, uint64_t *nshared)
287 +{
288 + BDRVAllocTrackState *s = bs->opaque;
289 +
290 + *nshared = BLK_PERM_ALL;
291 +
292 + /* in case we're currently dropping ourselves, claim to not use any
293 + * permissions at all - which is fine, since from this point on we will
294 + * never issue a read or write anymore */
295 + if (s->drop_state == DropInProgress) {
296 + *nperm = 0;
297 + return;
298 + }
299 +
300 + if (role & BDRV_CHILD_DATA) {
301 + *nperm = perm & DEFAULT_PERM_PASSTHROUGH;
302 + } else {
303 + /* 'backing' is also a child of our BDS, but we don't expect it to be
304 + * writeable, so we only forward 'consistent read' */
305 + *nperm = perm & BLK_PERM_CONSISTENT_READ;
306 + }
307 +}
308 +
309 +static void track_drop(void *opaque)
310 +{
311 + BlockDriverState *bs = (BlockDriverState*)opaque;
312 + BlockDriverState *file = bs->file->bs;
313 + BDRVAllocTrackState *s = bs->opaque;
314 +
315 + assert(file);
316 +
317 + /* we rely on the fact that we're not used anywhere else, so let's wait
318 + * until we're only used once - in the drive connected to the guest (and one
319 + * ref is held by bdrv_ref in track_change_backing_file) */
320 + if (bs->refcnt > 2) {
321 + aio_bh_schedule_oneshot(qemu_get_aio_context(), track_drop, opaque);
322 + return;
323 + }
324 + AioContext *aio_context = bdrv_get_aio_context(bs);
325 + aio_context_acquire(aio_context);
326 +
327 + bdrv_drained_begin(bs);
328 +
329 + /* now that we're drained, we can safely set 'DropInProgress' */
330 + s->drop_state = DropInProgress;
331 + bdrv_child_refresh_perms(bs, bs->file, &error_abort);
332 +
333 + bdrv_replace_node(bs, file, &error_abort);
334 + bdrv_set_backing_hd(bs, NULL, &error_abort);
335 + bdrv_drained_end(bs);
336 + bdrv_unref(bs);
337 + aio_context_release(aio_context);
338 +}
339 +
340 +static int track_change_backing_file(BlockDriverState *bs,
341 + const char *backing_file,
342 + const char *backing_fmt)
343 +{
344 + BDRVAllocTrackState *s = bs->opaque;
345 + if (s->auto_remove && s->drop_state == DropNone &&
346 + backing_file == NULL && backing_fmt == NULL)
347 + {
348 + /* backing file has been disconnected, there's no longer any use for
349 + * this node, so let's remove ourselves from the block graph - we need
350 + * to schedule this for later however, since when this function is
351 + * called, the blockjob modifying us is probably not done yet and has a
352 + * blocker on 'bs' */
353 + s->drop_state = DropRequested;
354 + bdrv_ref(bs);
355 + aio_bh_schedule_oneshot(qemu_get_aio_context(), track_drop, (void*)bs);
356 + }
357 +
358 + return 0;
359 +}
360 +
361 +static BlockDriver bdrv_alloc_track = {
362 + .format_name = "alloc-track",
363 + .instance_size = sizeof(BDRVAllocTrackState),
364 +
365 + .bdrv_file_open = track_open,
366 + .bdrv_close = track_close,
367 + .bdrv_getlength = track_getlength,
368 + .bdrv_child_perm = track_child_perm,
369 + .bdrv_refresh_limits = track_refresh_limits,
370 +
371 + .bdrv_co_pwrite_zeroes = track_co_pwrite_zeroes,
372 + .bdrv_co_pwritev = track_co_pwritev,
373 + .bdrv_co_preadv = track_co_preadv,
374 + .bdrv_co_pdiscard = track_co_pdiscard,
375 +
376 + .bdrv_co_flush = track_co_flush,
377 + .bdrv_co_flush_to_disk = track_co_flush,
378 +
379 + .supports_backing = true,
380 +
381 + .bdrv_co_block_status = track_co_block_status,
382 + .bdrv_change_backing_file = track_change_backing_file,
383 +};
384 +
385 +static void bdrv_alloc_track_init(void)
386 +{
387 + bdrv_register(&bdrv_alloc_track);
388 +}
389 +
390 +block_init(bdrv_alloc_track_init);
391 diff --git a/block/meson.build b/block/meson.build
392 index 8c758c0218..45b72e10f1 100644
393 --- a/block/meson.build
394 +++ b/block/meson.build
395 @@ -2,6 +2,7 @@ block_ss.add(genh)
396 block_ss.add(files(
397 'accounting.c',
398 'aio_task.c',
399 + 'alloc-track.c',
400 'amend.c',
401 'backup.c',
402 'backup-dump.c',