1
The following changes since commit 8c1ecb590497b0349c550607db923972b37f6963:
1
The following changes since commit 6c769690ac845fa62642a5f93b4e4bd906adab95:
2
2
3
Merge remote-tracking branch 'remotes/stsquad/tags/pull-testing-next-280519-2' into staging (2019-05-28 17:38:32 +0100)
3
Merge remote-tracking branch 'remotes/vsementsov/tags/pull-simplebench-2021-05-04' into staging (2021-05-21 12:02:34 +0100)
4
4
5
are available in the Git repository at:
5
are available in the Git repository at:
6
6
7
https://github.com/XanClic/qemu.git tags/pull-block-2019-05-28
7
https://gitlab.com/stefanha/qemu.git tags/block-pull-request
8
8
9
for you to fetch changes up to a2d665c1bc3624a8375e2f9a7d569f7565cc1358:
9
for you to fetch changes up to 0a6f0c76a030710780ce10d6347a70f098024d21:
10
10
11
blockdev: loosen restrictions on drive-backup source node (2019-05-28 20:30:55 +0200)
11
coroutine-sleep: introduce qemu_co_sleep (2021-05-21 18:22:33 +0100)
12
12
13
----------------------------------------------------------------
13
----------------------------------------------------------------
14
Block patches:
14
Pull request
15
- qcow2: Use threads for encrypted I/O
15
16
- qemu-img rebase: Optimizations
16
(Resent due to an email preparation mistake.)
17
- backup job: Allow any source node, and some refactoring
18
- Some general simplifications in the block layer
19
17
20
----------------------------------------------------------------
18
----------------------------------------------------------------
21
Alberto Garcia (2):
22
block: Use bdrv_unref_child() for all children in bdrv_close()
23
block: Make bdrv_root_attach_child() unref child_bs on failure
24
19
25
Andrey Shinkevich (1):
20
Paolo Bonzini (6):
26
qcow2-bitmap: initialize bitmap directory alignment
21
coroutine-sleep: use a stack-allocated timer
22
coroutine-sleep: disallow NULL QemuCoSleepState** argument
23
coroutine-sleep: allow qemu_co_sleep_wake that wakes nothing
24
coroutine-sleep: move timer out of QemuCoSleepState
25
coroutine-sleep: replace QemuCoSleepState pointer with struct in the
26
API
27
coroutine-sleep: introduce qemu_co_sleep
27
28
28
Anton Nefedov (1):
29
Philippe Mathieu-Daudé (1):
29
qcow2: skip writing zero buffers to empty COW areas
30
bitops.h: Improve find_xxx_bit() documentation
30
31
31
John Snow (1):
32
Zenghui Yu (1):
32
blockdev: loosen restrictions on drive-backup source node
33
multi-process: Initialize variables declared with g_auto*
33
34
34
Sam Eiderman (3):
35
include/qemu/bitops.h | 15 ++++++--
35
qemu-img: rebase: Reuse parent BlockDriverState
36
include/qemu/coroutine.h | 27 ++++++++-----
36
qemu-img: rebase: Reduce reads on in-chain rebase
37
block/block-copy.c | 10 ++---
37
qemu-img: rebase: Reuse in-chain BlockDriverState
38
block/nbd.c | 14 +++----
38
39
hw/remote/memory.c | 5 +--
39
Vladimir Sementsov-Ogievskiy (13):
40
hw/remote/proxy.c | 3 +-
40
qcow2.h: add missing include
41
util/qemu-coroutine-sleep.c | 75 +++++++++++++++++++------------------
41
qcow2: add separate file for threaded data processing functions
42
7 files changed, 79 insertions(+), 70 deletions(-)
42
qcow2-threads: use thread_pool_submit_co
43
qcow2-threads: qcow2_co_do_compress: protect queuing by mutex
44
qcow2-threads: split out generic path
45
qcow2: qcow2_co_preadv: improve locking
46
qcow2: bdrv_co_pwritev: move encryption code out of the lock
47
qcow2: do encryption in threads
48
block/backup: simplify backup_incremental_init_copy_bitmap
49
block/backup: move to copy_bitmap with granularity
50
block/backup: refactor and tolerate unallocated cluster skipping
51
block/backup: unify different modes code path
52
block/backup: refactor: split out backup_calculate_cluster_size
53
54
block/Makefile.objs | 2 +-
55
qapi/block-core.json | 4 +-
56
block/qcow2.h | 26 ++-
57
block.c | 46 +++---
58
block/backup.c | 243 ++++++++++++---------------
59
block/block-backend.c | 3 +-
60
block/qcow2-bitmap.c | 3 +-
61
block/qcow2-cache.c | 1 -
62
block/qcow2-cluster.c | 10 +-
63
block/qcow2-refcount.c | 1 -
64
block/qcow2-snapshot.c | 1 -
65
block/qcow2-threads.c | 268 ++++++++++++++++++++++++++++++
66
block/qcow2.c | 320 +++++++++++++-----------------------
67
block/quorum.c | 1 -
68
blockdev.c | 7 +-
69
blockjob.c | 2 +-
70
qemu-img.c | 85 ++++++----
71
tests/test-bdrv-drain.c | 6 -
72
tests/test-bdrv-graph-mod.c | 1 -
73
block/trace-events | 1 +
74
tests/qemu-iotests/056 | 2 +-
75
tests/qemu-iotests/060 | 7 +-
76
tests/qemu-iotests/060.out | 5 +-
77
23 files changed, 615 insertions(+), 430 deletions(-)
78
create mode 100644 block/qcow2-threads.c
79
43
80
--
44
--
81
2.21.0
45
2.31.1
82
46
83
diff view generated by jsdifflib
Deleted patch
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2
1
3
qcow2.h depends on block_int.h. Compilation isn't broken currently only
4
due to block_int.h always included before qcow2.h. Though, it seems
5
better to directly include block_int.h in qcow2.h.
6
7
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
8
Reviewed-by: Alberto Garcia <berto@igalia.com>
9
Reviewed-by: Max Reitz <mreitz@redhat.com>
10
Message-id: 20190506142741.41731-2-vsementsov@virtuozzo.com
11
Signed-off-by: Max Reitz <mreitz@redhat.com>
12
---
13
block/qcow2.h | 1 +
14
block/qcow2-bitmap.c | 1 -
15
block/qcow2-cache.c | 1 -
16
block/qcow2-cluster.c | 1 -
17
block/qcow2-refcount.c | 1 -
18
block/qcow2-snapshot.c | 1 -
19
block/qcow2.c | 1 -
20
7 files changed, 1 insertion(+), 6 deletions(-)
21
22
diff --git a/block/qcow2.h b/block/qcow2.h
23
index XXXXXXX..XXXXXXX 100644
24
--- a/block/qcow2.h
25
+++ b/block/qcow2.h
26
@@ -XXX,XX +XXX,XX @@
27
#include "crypto/block.h"
28
#include "qemu/coroutine.h"
29
#include "qemu/units.h"
30
+#include "block/block_int.h"
31
32
//#define DEBUG_ALLOC
33
//#define DEBUG_ALLOC2
34
diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c
35
index XXXXXXX..XXXXXXX 100644
36
--- a/block/qcow2-bitmap.c
37
+++ b/block/qcow2-bitmap.c
38
@@ -XXX,XX +XXX,XX @@
39
#include "qapi/error.h"
40
#include "qemu/cutils.h"
41
42
-#include "block/block_int.h"
43
#include "qcow2.h"
44
45
/* NOTICE: BME here means Bitmaps Extension and used as a namespace for
46
diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c
47
index XXXXXXX..XXXXXXX 100644
48
--- a/block/qcow2-cache.c
49
+++ b/block/qcow2-cache.c
50
@@ -XXX,XX +XXX,XX @@
51
*/
52
53
#include "qemu/osdep.h"
54
-#include "block/block_int.h"
55
#include "qemu-common.h"
56
#include "qcow2.h"
57
#include "trace.h"
58
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
59
index XXXXXXX..XXXXXXX 100644
60
--- a/block/qcow2-cluster.c
61
+++ b/block/qcow2-cluster.c
62
@@ -XXX,XX +XXX,XX @@
63
64
#include "qapi/error.h"
65
#include "qemu-common.h"
66
-#include "block/block_int.h"
67
#include "qcow2.h"
68
#include "qemu/bswap.h"
69
#include "trace.h"
70
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
71
index XXXXXXX..XXXXXXX 100644
72
--- a/block/qcow2-refcount.c
73
+++ b/block/qcow2-refcount.c
74
@@ -XXX,XX +XXX,XX @@
75
#include "qemu/osdep.h"
76
#include "qapi/error.h"
77
#include "qemu-common.h"
78
-#include "block/block_int.h"
79
#include "qcow2.h"
80
#include "qemu/range.h"
81
#include "qemu/bswap.h"
82
diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c
83
index XXXXXXX..XXXXXXX 100644
84
--- a/block/qcow2-snapshot.c
85
+++ b/block/qcow2-snapshot.c
86
@@ -XXX,XX +XXX,XX @@
87
88
#include "qemu/osdep.h"
89
#include "qapi/error.h"
90
-#include "block/block_int.h"
91
#include "qcow2.h"
92
#include "qemu/bswap.h"
93
#include "qemu/error-report.h"
94
diff --git a/block/qcow2.c b/block/qcow2.c
95
index XXXXXXX..XXXXXXX 100644
96
--- a/block/qcow2.c
97
+++ b/block/qcow2.c
98
@@ -XXX,XX +XXX,XX @@
99
#define ZLIB_CONST
100
#include <zlib.h>
101
102
-#include "block/block_int.h"
103
#include "block/qdict.h"
104
#include "sysemu/block-backend.h"
105
#include "qemu/module.h"
106
--
107
2.21.0
108
109
diff view generated by jsdifflib
Deleted patch
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2
1
3
Move compression-on-threads to separate file. Encryption will be in it
4
too.
5
6
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7
Reviewed-by: Alberto Garcia <berto@igalia.com>
8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9
Message-id: 20190506142741.41731-3-vsementsov@virtuozzo.com
10
Signed-off-by: Max Reitz <mreitz@redhat.com>
11
---
12
block/Makefile.objs | 2 +-
13
block/qcow2.h | 7 ++
14
block/qcow2-threads.c | 201 ++++++++++++++++++++++++++++++++++++++++++
15
block/qcow2.c | 169 -----------------------------------
16
4 files changed, 209 insertions(+), 170 deletions(-)
17
create mode 100644 block/qcow2-threads.c
18
19
diff --git a/block/Makefile.objs b/block/Makefile.objs
20
index XXXXXXX..XXXXXXX 100644
21
--- a/block/Makefile.objs
22
+++ b/block/Makefile.objs
23
@@ -XXX,XX +XXX,XX @@ block-obj-$(CONFIG_BOCHS) += bochs.o
24
block-obj-$(CONFIG_VVFAT) += vvfat.o
25
block-obj-$(CONFIG_DMG) += dmg.o
26
27
-block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o qcow2-bitmap.o
28
+block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o qcow2-bitmap.o qcow2-threads.o
29
block-obj-$(CONFIG_QED) += qed.o qed-l2-cache.o qed-table.o qed-cluster.o
30
block-obj-$(CONFIG_QED) += qed-check.o
31
block-obj-y += vhdx.o vhdx-endian.o vhdx-log.o
32
diff --git a/block/qcow2.h b/block/qcow2.h
33
index XXXXXXX..XXXXXXX 100644
34
--- a/block/qcow2.h
35
+++ b/block/qcow2.h
36
@@ -XXX,XX +XXX,XX @@ void qcow2_remove_persistent_dirty_bitmap(BlockDriverState *bs,
37
const char *name,
38
Error **errp);
39
40
+ssize_t coroutine_fn
41
+qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size,
42
+ const void *src, size_t src_size);
43
+ssize_t coroutine_fn
44
+qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
45
+ const void *src, size_t src_size);
46
+
47
#endif
48
diff --git a/block/qcow2-threads.c b/block/qcow2-threads.c
49
new file mode 100644
50
index XXXXXXX..XXXXXXX
51
--- /dev/null
52
+++ b/block/qcow2-threads.c
53
@@ -XXX,XX +XXX,XX @@
54
+/*
55
+ * Threaded data processing for Qcow2: compression, encryption
56
+ *
57
+ * Copyright (c) 2004-2006 Fabrice Bellard
58
+ * Copyright (c) 2018 Virtuozzo International GmbH. All rights reserved.
59
+ *
60
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
61
+ * of this software and associated documentation files (the "Software"), to deal
62
+ * in the Software without restriction, including without limitation the rights
63
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
64
+ * copies of the Software, and to permit persons to whom the Software is
65
+ * furnished to do so, subject to the following conditions:
66
+ *
67
+ * The above copyright notice and this permission notice shall be included in
68
+ * all copies or substantial portions of the Software.
69
+ *
70
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
71
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
72
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
73
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
74
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
75
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
76
+ * THE SOFTWARE.
77
+ */
78
+
79
+#include "qemu/osdep.h"
80
+
81
+#define ZLIB_CONST
82
+#include <zlib.h>
83
+
84
+#include "qcow2.h"
85
+#include "block/thread-pool.h"
86
+
87
+#define MAX_COMPRESS_THREADS 4
88
+
89
+typedef ssize_t (*Qcow2CompressFunc)(void *dest, size_t dest_size,
90
+ const void *src, size_t src_size);
91
+typedef struct Qcow2CompressData {
92
+ void *dest;
93
+ size_t dest_size;
94
+ const void *src;
95
+ size_t src_size;
96
+ ssize_t ret;
97
+
98
+ Qcow2CompressFunc func;
99
+} Qcow2CompressData;
100
+
101
+/*
102
+ * qcow2_compress()
103
+ *
104
+ * @dest - destination buffer, @dest_size bytes
105
+ * @src - source buffer, @src_size bytes
106
+ *
107
+ * Returns: compressed size on success
108
+ * -ENOMEM destination buffer is not enough to store compressed data
109
+ * -EIO on any other error
110
+ */
111
+static ssize_t qcow2_compress(void *dest, size_t dest_size,
112
+ const void *src, size_t src_size)
113
+{
114
+ ssize_t ret;
115
+ z_stream strm;
116
+
117
+ /* best compression, small window, no zlib header */
118
+ memset(&strm, 0, sizeof(strm));
119
+ ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
120
+ -12, 9, Z_DEFAULT_STRATEGY);
121
+ if (ret != Z_OK) {
122
+ return -EIO;
123
+ }
124
+
125
+ /*
126
+ * strm.next_in is not const in old zlib versions, such as those used on
127
+ * OpenBSD/NetBSD, so cast the const away
128
+ */
129
+ strm.avail_in = src_size;
130
+ strm.next_in = (void *) src;
131
+ strm.avail_out = dest_size;
132
+ strm.next_out = dest;
133
+
134
+ ret = deflate(&strm, Z_FINISH);
135
+ if (ret == Z_STREAM_END) {
136
+ ret = dest_size - strm.avail_out;
137
+ } else {
138
+ ret = (ret == Z_OK ? -ENOMEM : -EIO);
139
+ }
140
+
141
+ deflateEnd(&strm);
142
+
143
+ return ret;
144
+}
145
+
146
+/*
147
+ * qcow2_decompress()
148
+ *
149
+ * Decompress some data (not more than @src_size bytes) to produce exactly
150
+ * @dest_size bytes.
151
+ *
152
+ * @dest - destination buffer, @dest_size bytes
153
+ * @src - source buffer, @src_size bytes
154
+ *
155
+ * Returns: 0 on success
156
+ * -1 on fail
157
+ */
158
+static ssize_t qcow2_decompress(void *dest, size_t dest_size,
159
+ const void *src, size_t src_size)
160
+{
161
+ int ret = 0;
162
+ z_stream strm;
163
+
164
+ memset(&strm, 0, sizeof(strm));
165
+ strm.avail_in = src_size;
166
+ strm.next_in = (void *) src;
167
+ strm.avail_out = dest_size;
168
+ strm.next_out = dest;
169
+
170
+ ret = inflateInit2(&strm, -12);
171
+ if (ret != Z_OK) {
172
+ return -1;
173
+ }
174
+
175
+ ret = inflate(&strm, Z_FINISH);
176
+ if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || strm.avail_out != 0) {
177
+ /*
178
+ * We approve Z_BUF_ERROR because we need @dest buffer to be filled, but
179
+ * @src buffer may be processed partly (because in qcow2 we know size of
180
+ * compressed data with precision of one sector)
181
+ */
182
+ ret = -1;
183
+ }
184
+
185
+ inflateEnd(&strm);
186
+
187
+ return ret;
188
+}
189
+
190
+static int qcow2_compress_pool_func(void *opaque)
191
+{
192
+ Qcow2CompressData *data = opaque;
193
+
194
+ data->ret = data->func(data->dest, data->dest_size,
195
+ data->src, data->src_size);
196
+
197
+ return 0;
198
+}
199
+
200
+static void qcow2_compress_complete(void *opaque, int ret)
201
+{
202
+ qemu_coroutine_enter(opaque);
203
+}
204
+
205
+static ssize_t coroutine_fn
206
+qcow2_co_do_compress(BlockDriverState *bs, void *dest, size_t dest_size,
207
+ const void *src, size_t src_size, Qcow2CompressFunc func)
208
+{
209
+ BDRVQcow2State *s = bs->opaque;
210
+ BlockAIOCB *acb;
211
+ ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
212
+ Qcow2CompressData arg = {
213
+ .dest = dest,
214
+ .dest_size = dest_size,
215
+ .src = src,
216
+ .src_size = src_size,
217
+ .func = func,
218
+ };
219
+
220
+ while (s->nb_compress_threads >= MAX_COMPRESS_THREADS) {
221
+ qemu_co_queue_wait(&s->compress_wait_queue, NULL);
222
+ }
223
+
224
+ s->nb_compress_threads++;
225
+ acb = thread_pool_submit_aio(pool, qcow2_compress_pool_func, &arg,
226
+ qcow2_compress_complete,
227
+ qemu_coroutine_self());
228
+
229
+ if (!acb) {
230
+ s->nb_compress_threads--;
231
+ return -EINVAL;
232
+ }
233
+ qemu_coroutine_yield();
234
+ s->nb_compress_threads--;
235
+ qemu_co_queue_next(&s->compress_wait_queue);
236
+
237
+ return arg.ret;
238
+}
239
+
240
+ssize_t coroutine_fn
241
+qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size,
242
+ const void *src, size_t src_size)
243
+{
244
+ return qcow2_co_do_compress(bs, dest, dest_size, src, src_size,
245
+ qcow2_compress);
246
+}
247
+
248
+ssize_t coroutine_fn
249
+qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
250
+ const void *src, size_t src_size)
251
+{
252
+ return qcow2_co_do_compress(bs, dest, dest_size, src, src_size,
253
+ qcow2_decompress);
254
+}
255
diff --git a/block/qcow2.c b/block/qcow2.c
256
index XXXXXXX..XXXXXXX 100644
257
--- a/block/qcow2.c
258
+++ b/block/qcow2.c
259
@@ -XXX,XX +XXX,XX @@
260
261
#include "qemu/osdep.h"
262
263
-#define ZLIB_CONST
264
-#include <zlib.h>
265
-
266
#include "block/qdict.h"
267
#include "sysemu/block-backend.h"
268
#include "qemu/module.h"
269
@@ -XXX,XX +XXX,XX @@
270
#include "qapi/qobject-input-visitor.h"
271
#include "qapi/qapi-visit-block-core.h"
272
#include "crypto.h"
273
-#include "block/thread-pool.h"
274
275
/*
276
Differences with QCOW:
277
@@ -XXX,XX +XXX,XX @@ fail:
278
return ret;
279
}
280
281
-/*
282
- * qcow2_compress()
283
- *
284
- * @dest - destination buffer, @dest_size bytes
285
- * @src - source buffer, @src_size bytes
286
- *
287
- * Returns: compressed size on success
288
- * -ENOMEM destination buffer is not enough to store compressed data
289
- * -EIO on any other error
290
- */
291
-static ssize_t qcow2_compress(void *dest, size_t dest_size,
292
- const void *src, size_t src_size)
293
-{
294
- ssize_t ret;
295
- z_stream strm;
296
-
297
- /* best compression, small window, no zlib header */
298
- memset(&strm, 0, sizeof(strm));
299
- ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
300
- -12, 9, Z_DEFAULT_STRATEGY);
301
- if (ret != Z_OK) {
302
- return -EIO;
303
- }
304
-
305
- /* strm.next_in is not const in old zlib versions, such as those used on
306
- * OpenBSD/NetBSD, so cast the const away */
307
- strm.avail_in = src_size;
308
- strm.next_in = (void *) src;
309
- strm.avail_out = dest_size;
310
- strm.next_out = dest;
311
-
312
- ret = deflate(&strm, Z_FINISH);
313
- if (ret == Z_STREAM_END) {
314
- ret = dest_size - strm.avail_out;
315
- } else {
316
- ret = (ret == Z_OK ? -ENOMEM : -EIO);
317
- }
318
-
319
- deflateEnd(&strm);
320
-
321
- return ret;
322
-}
323
-
324
-/*
325
- * qcow2_decompress()
326
- *
327
- * Decompress some data (not more than @src_size bytes) to produce exactly
328
- * @dest_size bytes.
329
- *
330
- * @dest - destination buffer, @dest_size bytes
331
- * @src - source buffer, @src_size bytes
332
- *
333
- * Returns: 0 on success
334
- * -1 on fail
335
- */
336
-static ssize_t qcow2_decompress(void *dest, size_t dest_size,
337
- const void *src, size_t src_size)
338
-{
339
- int ret = 0;
340
- z_stream strm;
341
-
342
- memset(&strm, 0, sizeof(strm));
343
- strm.avail_in = src_size;
344
- strm.next_in = (void *) src;
345
- strm.avail_out = dest_size;
346
- strm.next_out = dest;
347
-
348
- ret = inflateInit2(&strm, -12);
349
- if (ret != Z_OK) {
350
- return -1;
351
- }
352
-
353
- ret = inflate(&strm, Z_FINISH);
354
- if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || strm.avail_out != 0) {
355
- /* We approve Z_BUF_ERROR because we need @dest buffer to be filled, but
356
- * @src buffer may be processed partly (because in qcow2 we know size of
357
- * compressed data with precision of one sector) */
358
- ret = -1;
359
- }
360
-
361
- inflateEnd(&strm);
362
-
363
- return ret;
364
-}
365
-
366
-#define MAX_COMPRESS_THREADS 4
367
-
368
-typedef ssize_t (*Qcow2CompressFunc)(void *dest, size_t dest_size,
369
- const void *src, size_t src_size);
370
-typedef struct Qcow2CompressData {
371
- void *dest;
372
- size_t dest_size;
373
- const void *src;
374
- size_t src_size;
375
- ssize_t ret;
376
-
377
- Qcow2CompressFunc func;
378
-} Qcow2CompressData;
379
-
380
-static int qcow2_compress_pool_func(void *opaque)
381
-{
382
- Qcow2CompressData *data = opaque;
383
-
384
- data->ret = data->func(data->dest, data->dest_size,
385
- data->src, data->src_size);
386
-
387
- return 0;
388
-}
389
-
390
-static void qcow2_compress_complete(void *opaque, int ret)
391
-{
392
- qemu_coroutine_enter(opaque);
393
-}
394
-
395
-static ssize_t coroutine_fn
396
-qcow2_co_do_compress(BlockDriverState *bs, void *dest, size_t dest_size,
397
- const void *src, size_t src_size, Qcow2CompressFunc func)
398
-{
399
- BDRVQcow2State *s = bs->opaque;
400
- BlockAIOCB *acb;
401
- ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
402
- Qcow2CompressData arg = {
403
- .dest = dest,
404
- .dest_size = dest_size,
405
- .src = src,
406
- .src_size = src_size,
407
- .func = func,
408
- };
409
-
410
- while (s->nb_compress_threads >= MAX_COMPRESS_THREADS) {
411
- qemu_co_queue_wait(&s->compress_wait_queue, NULL);
412
- }
413
-
414
- s->nb_compress_threads++;
415
- acb = thread_pool_submit_aio(pool, qcow2_compress_pool_func, &arg,
416
- qcow2_compress_complete,
417
- qemu_coroutine_self());
418
-
419
- if (!acb) {
420
- s->nb_compress_threads--;
421
- return -EINVAL;
422
- }
423
- qemu_coroutine_yield();
424
- s->nb_compress_threads--;
425
- qemu_co_queue_next(&s->compress_wait_queue);
426
-
427
- return arg.ret;
428
-}
429
-
430
-static ssize_t coroutine_fn
431
-qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size,
432
- const void *src, size_t src_size)
433
-{
434
- return qcow2_co_do_compress(bs, dest, dest_size, src, src_size,
435
- qcow2_compress);
436
-}
437
-
438
-static ssize_t coroutine_fn
439
-qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
440
- const void *src, size_t src_size)
441
-{
442
- return qcow2_co_do_compress(bs, dest, dest_size, src, src_size,
443
- qcow2_decompress);
444
-}
445
-
446
/* XXX: put compressed sectors first, then all the cluster aligned
447
tables to avoid losing bytes in alignment */
448
static coroutine_fn int
449
--
450
2.21.0
451
452
diff view generated by jsdifflib
1
From: John Snow <jsnow@redhat.com>
1
From: Zenghui Yu <yuzenghui@huawei.com>
2
2
3
We mandate that the source node must be a root node; but there's no reason
3
Quote docs/devel/style.rst (section "Automatic memory deallocation"):
4
I am aware of that it needs to be restricted to such. In some cases, we need
5
to make sure that there's a medium present, but in the general case we can
6
allow the backup job itself to do the graph checking.
7
4
8
This patch helps improve the error message when you try to backup from
5
* Variables declared with g_auto* MUST always be initialized,
9
the same node more than once, which is reflected in the change to test
6
otherwise the cleanup function will use uninitialized stack memory
10
056.
11
7
12
For backups with bitmaps, it will also show a better error message that
8
Initialize @name properly to get rid of the compilation error (using
13
the bitmap is in use instead of giving you something cryptic like "need
9
gcc-7.3.0 on CentOS):
14
a root node."
15
10
16
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1707303
11
../hw/remote/proxy.c: In function 'pci_proxy_dev_realize':
17
Signed-off-by: John Snow <jsnow@redhat.com>
12
/usr/include/glib-2.0/glib/glib-autocleanups.h:28:3: error: 'name' may be used uninitialized in this function [-Werror=maybe-uninitialized]
18
Message-id: 20190521210053.8864-1-jsnow@redhat.com
13
g_free (*pp);
19
Signed-off-by: Max Reitz <mreitz@redhat.com>
14
^~~~~~~~~~~~
15
../hw/remote/proxy.c:350:30: note: 'name' was declared here
16
g_autofree char *name;
17
^~~~
18
19
Signed-off-by: Zenghui Yu <yuzenghui@huawei.com>
20
Reviewed-by: Jagannathan Raman <jag.raman@oracle.com>
21
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22
Reviewed-by: Miroslav Rezanina <mrezanin@redhat.com>
23
Message-id: 20210312112143.1369-1-yuzenghui@huawei.com
24
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
20
---
25
---
21
blockdev.c | 7 ++++++-
26
hw/remote/memory.c | 5 ++---
22
tests/qemu-iotests/056 | 2 +-
27
hw/remote/proxy.c | 3 +--
23
2 files changed, 7 insertions(+), 2 deletions(-)
28
2 files changed, 3 insertions(+), 5 deletions(-)
24
29
25
diff --git a/blockdev.c b/blockdev.c
30
diff --git a/hw/remote/memory.c b/hw/remote/memory.c
26
index XXXXXXX..XXXXXXX 100644
31
index XXXXXXX..XXXXXXX 100644
27
--- a/blockdev.c
32
--- a/hw/remote/memory.c
28
+++ b/blockdev.c
33
+++ b/hw/remote/memory.c
29
@@ -XXX,XX +XXX,XX @@ static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn,
34
@@ -XXX,XX +XXX,XX @@ void remote_sysmem_reconfig(MPQemuMsg *msg, Error **errp)
30
backup->compress = false;
35
31
}
36
remote_sysmem_reset();
32
37
33
- bs = qmp_get_root_bs(backup->device, errp);
38
- for (region = 0; region < msg->num_fds; region++) {
34
+ bs = bdrv_lookup_bs(backup->device, backup->device, errp);
39
- g_autofree char *name;
35
if (!bs) {
40
+ for (region = 0; region < msg->num_fds; region++, suffix++) {
36
return NULL;
41
+ g_autofree char *name = g_strdup_printf("remote-mem-%u", suffix);
37
}
42
subregion = g_new(MemoryRegion, 1);
38
43
- name = g_strdup_printf("remote-mem-%u", suffix++);
39
+ if (!bs->drv) {
44
memory_region_init_ram_from_fd(subregion, NULL,
40
+ error_setg(errp, "Device has no medium");
45
name, sysmem_info->sizes[region],
41
+ return NULL;
46
true, msg->fds[region],
42
+ }
47
diff --git a/hw/remote/proxy.c b/hw/remote/proxy.c
43
+
48
index XXXXXXX..XXXXXXX 100644
44
aio_context = bdrv_get_aio_context(bs);
49
--- a/hw/remote/proxy.c
45
aio_context_acquire(aio_context);
50
+++ b/hw/remote/proxy.c
46
51
@@ -XXX,XX +XXX,XX @@ static void probe_pci_info(PCIDevice *dev, Error **errp)
47
diff --git a/tests/qemu-iotests/056 b/tests/qemu-iotests/056
52
PCI_BASE_ADDRESS_SPACE_IO : PCI_BASE_ADDRESS_SPACE_MEMORY;
48
index XXXXXXX..XXXXXXX 100755
53
49
--- a/tests/qemu-iotests/056
54
if (size) {
50
+++ b/tests/qemu-iotests/056
55
- g_autofree char *name;
51
@@ -XXX,XX +XXX,XX @@ class BackupTest(iotests.QMPTestCase):
56
+ g_autofree char *name = g_strdup_printf("bar-region-%d", i);
52
res = self.vm.qmp('query-block-jobs')
57
pdev->region[i].dev = pdev;
53
self.assert_qmp(res, 'return[0]/status', 'concluded')
58
pdev->region[i].present = true;
54
# Leave zombie job un-dismissed, observe a failure:
59
if (type == PCI_BASE_ADDRESS_SPACE_MEMORY) {
55
- res = self.qmp_backup_and_wait(serror='Need a root block node',
60
pdev->region[i].memory = true;
56
+ res = self.qmp_backup_and_wait(serror="Node 'drive0' is busy: block device is in use by block job: backup",
61
}
57
device='drive0', format=iotests.imgfmt,
62
- name = g_strdup_printf("bar-region-%d", i);
58
sync='full', target=self.dest_img,
63
memory_region_init_io(&pdev->region[i].mr, OBJECT(pdev),
59
auto_dismiss=False)
64
&proxy_mr_ops, &pdev->region[i],
65
name, size);
60
--
66
--
61
2.21.0
67
2.31.1
62
68
63
diff view generated by jsdifflib
1
From: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
1
From: Philippe Mathieu-Daudé <philmd@redhat.com>
2
2
3
Valgrind detects multiple issues in QEMU iotests when the memory is
3
Document the following functions return the bitmap size
4
used without being initialized. Valgrind may dump lots of unnecessary
4
if no matching bit is found:
5
reports what makes the memory issue analysis harder. Particularly,
6
that is true for the aligned bitmap directory and can be seen while
7
running the iotest #169. Padding the aligned space with zeros eases
8
the pain.
9
5
10
Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
6
- find_first_bit
11
Message-id: 1558961521-131620-1-git-send-email-andrey.shinkevich@virtuozzo.com
7
- find_next_bit
12
Signed-off-by: Max Reitz <mreitz@redhat.com>
8
- find_last_bit
9
- find_first_zero_bit
10
- find_next_zero_bit
11
12
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
13
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
14
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
15
Message-id: 20210510200758.2623154-2-philmd@redhat.com
16
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
13
---
17
---
14
block/qcow2-bitmap.c | 2 +-
18
include/qemu/bitops.h | 15 ++++++++++++---
15
1 file changed, 1 insertion(+), 1 deletion(-)
19
1 file changed, 12 insertions(+), 3 deletions(-)
16
20
17
diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c
21
diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h
18
index XXXXXXX..XXXXXXX 100644
22
index XXXXXXX..XXXXXXX 100644
19
--- a/block/qcow2-bitmap.c
23
--- a/include/qemu/bitops.h
20
+++ b/block/qcow2-bitmap.c
24
+++ b/include/qemu/bitops.h
21
@@ -XXX,XX +XXX,XX @@ static int bitmap_list_store(BlockDriverState *bs, Qcow2BitmapList *bm_list,
25
@@ -XXX,XX +XXX,XX @@ static inline int test_bit(long nr, const unsigned long *addr)
22
dir_offset = *offset;
26
* @addr: The address to start the search at
23
}
27
* @size: The maximum size to search
24
28
*
25
- dir = g_try_malloc(dir_size);
29
- * Returns the bit number of the first set bit, or size.
26
+ dir = g_try_malloc0(dir_size);
30
+ * Returns the bit number of the last set bit,
27
if (dir == NULL) {
31
+ * or @size if there is no set bit in the bitmap.
28
return -ENOMEM;
32
*/
29
}
33
unsigned long find_last_bit(const unsigned long *addr,
34
unsigned long size);
35
@@ -XXX,XX +XXX,XX @@ unsigned long find_last_bit(const unsigned long *addr,
36
* @addr: The address to base the search on
37
* @offset: The bitnumber to start searching at
38
* @size: The bitmap size in bits
39
+ *
40
+ * Returns the bit number of the next set bit,
41
+ * or @size if there are no further set bits in the bitmap.
42
*/
43
unsigned long find_next_bit(const unsigned long *addr,
44
unsigned long size,
45
@@ -XXX,XX +XXX,XX @@ unsigned long find_next_bit(const unsigned long *addr,
46
* @addr: The address to base the search on
47
* @offset: The bitnumber to start searching at
48
* @size: The bitmap size in bits
49
+ *
50
+ * Returns the bit number of the next cleared bit,
51
+ * or @size if there are no further clear bits in the bitmap.
52
*/
53
54
unsigned long find_next_zero_bit(const unsigned long *addr,
55
@@ -XXX,XX +XXX,XX @@ unsigned long find_next_zero_bit(const unsigned long *addr,
56
* @addr: The address to start the search at
57
* @size: The maximum size to search
58
*
59
- * Returns the bit number of the first set bit.
60
+ * Returns the bit number of the first set bit,
61
+ * or @size if there is no set bit in the bitmap.
62
*/
63
static inline unsigned long find_first_bit(const unsigned long *addr,
64
unsigned long size)
65
@@ -XXX,XX +XXX,XX @@ static inline unsigned long find_first_bit(const unsigned long *addr,
66
* @addr: The address to start the search at
67
* @size: The maximum size to search
68
*
69
- * Returns the bit number of the first cleared bit.
70
+ * Returns the bit number of the first cleared bit,
71
+ * or @size if there is no clear bit in the bitmap.
72
*/
73
static inline unsigned long find_first_zero_bit(const unsigned long *addr,
74
unsigned long size)
30
--
75
--
31
2.21.0
76
2.31.1
32
77
33
diff view generated by jsdifflib
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
1
From: Paolo Bonzini <pbonzini@redhat.com>
2
2
3
Drop dependence on AioContext lock.
3
The lifetime of the timer is well-known (it cannot outlive
4
qemu_co_sleep_ns_wakeable, because it's deleted by the time the
5
coroutine resumes), so it is not necessary to place it on the heap.
4
6
5
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
6
Reviewed-by: Alberto Garcia <berto@igalia.com>
8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
7
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
9
Message-id: 20210517100548.28806-2-pbonzini@redhat.com
8
Reviewed-by: Max Reitz <mreitz@redhat.com>
10
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9
Message-id: 20190506142741.41731-5-vsementsov@virtuozzo.com
10
Signed-off-by: Max Reitz <mreitz@redhat.com>
11
---
11
---
12
block/qcow2-threads.c | 10 +++++++---
12
util/qemu-coroutine-sleep.c | 9 ++++-----
13
1 file changed, 7 insertions(+), 3 deletions(-)
13
1 file changed, 4 insertions(+), 5 deletions(-)
14
14
15
diff --git a/block/qcow2-threads.c b/block/qcow2-threads.c
15
diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c
16
index XXXXXXX..XXXXXXX 100644
16
index XXXXXXX..XXXXXXX 100644
17
--- a/block/qcow2-threads.c
17
--- a/util/qemu-coroutine-sleep.c
18
+++ b/block/qcow2-threads.c
18
+++ b/util/qemu-coroutine-sleep.c
19
@@ -XXX,XX +XXX,XX @@ qcow2_co_do_compress(BlockDriverState *bs, void *dest, size_t dest_size,
19
@@ -XXX,XX +XXX,XX @@ static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns";
20
.func = func,
20
21
struct QemuCoSleepState {
22
Coroutine *co;
23
- QEMUTimer *ts;
24
+ QEMUTimer ts;
25
QemuCoSleepState **user_state_pointer;
26
};
27
28
@@ -XXX,XX +XXX,XX @@ void qemu_co_sleep_wake(QemuCoSleepState *sleep_state)
29
if (sleep_state->user_state_pointer) {
30
*sleep_state->user_state_pointer = NULL;
31
}
32
- timer_del(sleep_state->ts);
33
+ timer_del(&sleep_state->ts);
34
aio_co_wake(sleep_state->co);
35
}
36
37
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
38
AioContext *ctx = qemu_get_current_aio_context();
39
QemuCoSleepState state = {
40
.co = qemu_coroutine_self(),
41
- .ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &state),
42
.user_state_pointer = sleep_state,
21
};
43
};
22
44
23
+ qemu_co_mutex_lock(&s->lock);
45
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
24
while (s->nb_compress_threads >= MAX_COMPRESS_THREADS) {
46
abort();
25
- qemu_co_queue_wait(&s->compress_wait_queue, NULL);
26
+ qemu_co_queue_wait(&s->compress_wait_queue, &s->lock);
27
}
47
}
28
-
48
29
s->nb_compress_threads++;
49
+ aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, &state);
30
+ qemu_co_mutex_unlock(&s->lock);
50
if (sleep_state) {
31
+
51
*sleep_state = &state;
32
thread_pool_submit_co(pool, qcow2_compress_pool_func, &arg);
52
}
33
- s->nb_compress_threads--;
53
- timer_mod(state.ts, qemu_clock_get_ns(type) + ns);
34
54
+ timer_mod(&state.ts, qemu_clock_get_ns(type) + ns);
35
+ qemu_co_mutex_lock(&s->lock);
55
qemu_coroutine_yield();
36
+ s->nb_compress_threads--;
56
if (sleep_state) {
37
qemu_co_queue_next(&s->compress_wait_queue);
57
/*
38
+ qemu_co_mutex_unlock(&s->lock);
58
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
39
59
*/
40
return arg.ret;
60
assert(*sleep_state == NULL);
61
}
62
- timer_free(state.ts);
41
}
63
}
42
--
64
--
43
2.21.0
65
2.31.1
44
66
45
diff view generated by jsdifflib
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
1
From: Paolo Bonzini <pbonzini@redhat.com>
2
2
3
Use thread_pool_submit_co, instead of reinventing it here. Note, that
3
Simplify the code by removing conditionals. qemu_co_sleep_ns
4
thread_pool_submit_aio() never returns NULL, so checking it was an
4
can simply point the argument to an on-stack temporary.
5
extra thing.
6
5
7
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
6
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
8
Reviewed-by: Alberto Garcia <berto@igalia.com>
7
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9
Reviewed-by: Max Reitz <mreitz@redhat.com>
8
Message-id: 20210517100548.28806-3-pbonzini@redhat.com
10
Message-id: 20190506142741.41731-4-vsementsov@virtuozzo.com
9
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
11
Signed-off-by: Max Reitz <mreitz@redhat.com>
12
---
10
---
13
block/qcow2-threads.c | 17 ++---------------
11
include/qemu/coroutine.h | 5 +++--
14
1 file changed, 2 insertions(+), 15 deletions(-)
12
util/qemu-coroutine-sleep.c | 18 +++++-------------
13
2 files changed, 8 insertions(+), 15 deletions(-)
15
14
16
diff --git a/block/qcow2-threads.c b/block/qcow2-threads.c
15
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
17
index XXXXXXX..XXXXXXX 100644
16
index XXXXXXX..XXXXXXX 100644
18
--- a/block/qcow2-threads.c
17
--- a/include/qemu/coroutine.h
19
+++ b/block/qcow2-threads.c
18
+++ b/include/qemu/coroutine.h
20
@@ -XXX,XX +XXX,XX @@ static int qcow2_compress_pool_func(void *opaque)
19
@@ -XXX,XX +XXX,XX @@ typedef struct QemuCoSleepState QemuCoSleepState;
21
return 0;
20
21
/**
22
* Yield the coroutine for a given duration. During this yield, @sleep_state
23
- * (if not NULL) is set to an opaque pointer, which may be used for
24
+ * is set to an opaque pointer, which may be used for
25
* qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when the
26
* timer fires. Don't save the obtained value to other variables and don't call
27
* qemu_co_sleep_wake from another aio context.
28
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
29
QemuCoSleepState **sleep_state);
30
static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
31
{
32
- qemu_co_sleep_ns_wakeable(type, ns, NULL);
33
+ QemuCoSleepState *unused = NULL;
34
+ qemu_co_sleep_ns_wakeable(type, ns, &unused);
22
}
35
}
23
36
24
-static void qcow2_compress_complete(void *opaque, int ret)
37
/**
25
-{
38
diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c
26
- qemu_coroutine_enter(opaque);
39
index XXXXXXX..XXXXXXX 100644
27
-}
40
--- a/util/qemu-coroutine-sleep.c
28
-
41
+++ b/util/qemu-coroutine-sleep.c
29
static ssize_t coroutine_fn
42
@@ -XXX,XX +XXX,XX @@ void qemu_co_sleep_wake(QemuCoSleepState *sleep_state)
30
qcow2_co_do_compress(BlockDriverState *bs, void *dest, size_t dest_size,
43
qemu_co_sleep_ns__scheduled, NULL);
31
const void *src, size_t src_size, Qcow2CompressFunc func)
44
32
{
45
assert(scheduled == qemu_co_sleep_ns__scheduled);
33
BDRVQcow2State *s = bs->opaque;
46
- if (sleep_state->user_state_pointer) {
34
- BlockAIOCB *acb;
47
- *sleep_state->user_state_pointer = NULL;
35
ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
48
- }
36
Qcow2CompressData arg = {
49
+ *sleep_state->user_state_pointer = NULL;
37
.dest = dest,
50
timer_del(&sleep_state->ts);
38
@@ -XXX,XX +XXX,XX @@ qcow2_co_do_compress(BlockDriverState *bs, void *dest, size_t dest_size,
51
aio_co_wake(sleep_state->co);
52
}
53
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
39
}
54
}
40
55
41
s->nb_compress_threads++;
56
aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, &state);
42
- acb = thread_pool_submit_aio(pool, qcow2_compress_pool_func, &arg,
57
- if (sleep_state) {
43
- qcow2_compress_complete,
58
- *sleep_state = &state;
44
- qemu_coroutine_self());
45
-
46
- if (!acb) {
47
- s->nb_compress_threads--;
48
- return -EINVAL;
49
- }
59
- }
50
- qemu_coroutine_yield();
60
+ *sleep_state = &state;
51
+ thread_pool_submit_co(pool, qcow2_compress_pool_func, &arg);
61
timer_mod(&state.ts, qemu_clock_get_ns(type) + ns);
52
s->nb_compress_threads--;
62
qemu_coroutine_yield();
63
- if (sleep_state) {
64
- /*
65
- * Note that *sleep_state is cleared during qemu_co_sleep_wake
66
- * before resuming this coroutine.
67
- */
68
- assert(*sleep_state == NULL);
69
- }
53
+
70
+
54
qemu_co_queue_next(&s->compress_wait_queue);
71
+ /* qemu_co_sleep_wake clears *sleep_state before resuming this coroutine. */
55
72
+ assert(*sleep_state == NULL);
56
return arg.ret;
73
}
57
--
74
--
58
2.21.0
75
2.31.1
59
76
60
diff view generated by jsdifflib
Deleted patch
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2
1
3
Move generic part out of qcow2_co_do_compress, to reuse it for
4
encryption and rename things that would be shared with encryption path.
5
6
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7
Reviewed-by: Alberto Garcia <berto@igalia.com>
8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9
Message-id: 20190506142741.41731-6-vsementsov@virtuozzo.com
10
Signed-off-by: Max Reitz <mreitz@redhat.com>
11
---
12
block/qcow2.h | 4 ++--
13
block/qcow2-threads.c | 47 ++++++++++++++++++++++++++++---------------
14
block/qcow2.c | 2 +-
15
3 files changed, 34 insertions(+), 19 deletions(-)
16
17
diff --git a/block/qcow2.h b/block/qcow2.h
18
index XXXXXXX..XXXXXXX 100644
19
--- a/block/qcow2.h
20
+++ b/block/qcow2.h
21
@@ -XXX,XX +XXX,XX @@ typedef struct BDRVQcow2State {
22
char *image_backing_format;
23
char *image_data_file;
24
25
- CoQueue compress_wait_queue;
26
- int nb_compress_threads;
27
+ CoQueue thread_task_queue;
28
+ int nb_threads;
29
30
BdrvChild *data_file;
31
} BDRVQcow2State;
32
diff --git a/block/qcow2-threads.c b/block/qcow2-threads.c
33
index XXXXXXX..XXXXXXX 100644
34
--- a/block/qcow2-threads.c
35
+++ b/block/qcow2-threads.c
36
@@ -XXX,XX +XXX,XX @@
37
#include "qcow2.h"
38
#include "block/thread-pool.h"
39
40
-#define MAX_COMPRESS_THREADS 4
41
+#define QCOW2_MAX_THREADS 4
42
+
43
+static int coroutine_fn
44
+qcow2_co_process(BlockDriverState *bs, ThreadPoolFunc *func, void *arg)
45
+{
46
+ int ret;
47
+ BDRVQcow2State *s = bs->opaque;
48
+ ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
49
+
50
+ qemu_co_mutex_lock(&s->lock);
51
+ while (s->nb_threads >= QCOW2_MAX_THREADS) {
52
+ qemu_co_queue_wait(&s->thread_task_queue, &s->lock);
53
+ }
54
+ s->nb_threads++;
55
+ qemu_co_mutex_unlock(&s->lock);
56
+
57
+ ret = thread_pool_submit_co(pool, func, arg);
58
+
59
+ qemu_co_mutex_lock(&s->lock);
60
+ s->nb_threads--;
61
+ qemu_co_queue_next(&s->thread_task_queue);
62
+ qemu_co_mutex_unlock(&s->lock);
63
+
64
+ return ret;
65
+}
66
+
67
+
68
+/*
69
+ * Compression
70
+ */
71
72
typedef ssize_t (*Qcow2CompressFunc)(void *dest, size_t dest_size,
73
const void *src, size_t src_size);
74
@@ -XXX,XX +XXX,XX @@ static ssize_t coroutine_fn
75
qcow2_co_do_compress(BlockDriverState *bs, void *dest, size_t dest_size,
76
const void *src, size_t src_size, Qcow2CompressFunc func)
77
{
78
- BDRVQcow2State *s = bs->opaque;
79
- ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
80
Qcow2CompressData arg = {
81
.dest = dest,
82
.dest_size = dest_size,
83
@@ -XXX,XX +XXX,XX @@ qcow2_co_do_compress(BlockDriverState *bs, void *dest, size_t dest_size,
84
.func = func,
85
};
86
87
- qemu_co_mutex_lock(&s->lock);
88
- while (s->nb_compress_threads >= MAX_COMPRESS_THREADS) {
89
- qemu_co_queue_wait(&s->compress_wait_queue, &s->lock);
90
- }
91
- s->nb_compress_threads++;
92
- qemu_co_mutex_unlock(&s->lock);
93
-
94
- thread_pool_submit_co(pool, qcow2_compress_pool_func, &arg);
95
-
96
- qemu_co_mutex_lock(&s->lock);
97
- s->nb_compress_threads--;
98
- qemu_co_queue_next(&s->compress_wait_queue);
99
- qemu_co_mutex_unlock(&s->lock);
100
+ qcow2_co_process(bs, qcow2_compress_pool_func, &arg);
101
102
return arg.ret;
103
}
104
diff --git a/block/qcow2.c b/block/qcow2.c
105
index XXXXXXX..XXXXXXX 100644
106
--- a/block/qcow2.c
107
+++ b/block/qcow2.c
108
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
109
}
110
#endif
111
112
- qemu_co_queue_init(&s->compress_wait_queue);
113
+ qemu_co_queue_init(&s->thread_task_queue);
114
115
return ret;
116
117
--
118
2.21.0
119
120
diff view generated by jsdifflib
Deleted patch
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2
1
3
Background: decryption will be done in threads, to take benefit of it,
4
we should move it out of the lock first.
5
6
But let's go further: it turns out, that only
7
qcow2_get_cluster_offset() needs locking, so reduce locking to it.
8
9
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
10
Message-id: 20190506142741.41731-7-vsementsov@virtuozzo.com
11
Reviewed-by: Alberto Garcia <berto@igalia.com>
12
Signed-off-by: Max Reitz <mreitz@redhat.com>
13
---
14
block/qcow2.c | 12 ++----------
15
1 file changed, 2 insertions(+), 10 deletions(-)
16
17
diff --git a/block/qcow2.c b/block/qcow2.c
18
index XXXXXXX..XXXXXXX 100644
19
--- a/block/qcow2.c
20
+++ b/block/qcow2.c
21
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
22
23
qemu_iovec_init(&hd_qiov, qiov->niov);
24
25
- qemu_co_mutex_lock(&s->lock);
26
-
27
while (bytes != 0) {
28
29
/* prepare next request */
30
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
31
QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
32
}
33
34
+ qemu_co_mutex_lock(&s->lock);
35
ret = qcow2_get_cluster_offset(bs, offset, &cur_bytes, &cluster_offset);
36
+ qemu_co_mutex_unlock(&s->lock);
37
if (ret < 0) {
38
goto fail;
39
}
40
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
41
42
if (bs->backing) {
43
BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
44
- qemu_co_mutex_unlock(&s->lock);
45
ret = bdrv_co_preadv(bs->backing, offset, cur_bytes,
46
&hd_qiov, 0);
47
- qemu_co_mutex_lock(&s->lock);
48
if (ret < 0) {
49
goto fail;
50
}
51
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
52
break;
53
54
case QCOW2_CLUSTER_COMPRESSED:
55
- qemu_co_mutex_unlock(&s->lock);
56
ret = qcow2_co_preadv_compressed(bs, cluster_offset,
57
offset, cur_bytes,
58
&hd_qiov);
59
- qemu_co_mutex_lock(&s->lock);
60
if (ret < 0) {
61
goto fail;
62
}
63
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
64
}
65
66
BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
67
- qemu_co_mutex_unlock(&s->lock);
68
ret = bdrv_co_preadv(s->data_file,
69
cluster_offset + offset_in_cluster,
70
cur_bytes, &hd_qiov, 0);
71
- qemu_co_mutex_lock(&s->lock);
72
if (ret < 0) {
73
goto fail;
74
}
75
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
76
ret = 0;
77
78
fail:
79
- qemu_co_mutex_unlock(&s->lock);
80
-
81
qemu_iovec_destroy(&hd_qiov);
82
qemu_vfree(cluster_data);
83
84
--
85
2.21.0
86
87
diff view generated by jsdifflib
Deleted patch
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2
1
3
Encryption will be done in threads, to take benefit of it, we should
4
move it out of the lock first.
5
6
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7
Reviewed-by: Alberto Garcia <berto@igalia.com>
8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9
Message-id: 20190506142741.41731-8-vsementsov@virtuozzo.com
10
Signed-off-by: Max Reitz <mreitz@redhat.com>
11
---
12
block/qcow2.c | 35 +++++++++++++++++++++--------------
13
1 file changed, 21 insertions(+), 14 deletions(-)
14
15
diff --git a/block/qcow2.c b/block/qcow2.c
16
index XXXXXXX..XXXXXXX 100644
17
--- a/block/qcow2.c
18
+++ b/block/qcow2.c
19
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
20
ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
21
&cluster_offset, &l2meta);
22
if (ret < 0) {
23
- goto fail;
24
+ goto out_locked;
25
}
26
27
assert((cluster_offset & 511) == 0);
28
29
+ ret = qcow2_pre_write_overlap_check(bs, 0,
30
+ cluster_offset + offset_in_cluster,
31
+ cur_bytes, true);
32
+ if (ret < 0) {
33
+ goto out_locked;
34
+ }
35
+
36
+ qemu_co_mutex_unlock(&s->lock);
37
+
38
qemu_iovec_reset(&hd_qiov);
39
qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
40
41
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
42
* s->cluster_size);
43
if (cluster_data == NULL) {
44
ret = -ENOMEM;
45
- goto fail;
46
+ goto out_unlocked;
47
}
48
}
49
50
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
51
cluster_data,
52
cur_bytes, NULL) < 0) {
53
ret = -EIO;
54
- goto fail;
55
+ goto out_unlocked;
56
}
57
58
qemu_iovec_reset(&hd_qiov);
59
qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
60
}
61
62
- ret = qcow2_pre_write_overlap_check(bs, 0,
63
- cluster_offset + offset_in_cluster, cur_bytes, true);
64
- if (ret < 0) {
65
- goto fail;
66
- }
67
-
68
/* If we need to do COW, check if it's possible to merge the
69
* writing of the guest data together with that of the COW regions.
70
* If it's not possible (or not necessary) then write the
71
* guest data now. */
72
if (!merge_cow(offset, cur_bytes, &hd_qiov, l2meta)) {
73
- qemu_co_mutex_unlock(&s->lock);
74
BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
75
trace_qcow2_writev_data(qemu_coroutine_self(),
76
cluster_offset + offset_in_cluster);
77
ret = bdrv_co_pwritev(s->data_file,
78
cluster_offset + offset_in_cluster,
79
cur_bytes, &hd_qiov, 0);
80
- qemu_co_mutex_lock(&s->lock);
81
if (ret < 0) {
82
- goto fail;
83
+ goto out_unlocked;
84
}
85
}
86
87
+ qemu_co_mutex_lock(&s->lock);
88
+
89
ret = qcow2_handle_l2meta(bs, &l2meta, true);
90
if (ret) {
91
- goto fail;
92
+ goto out_locked;
93
}
94
95
bytes -= cur_bytes;
96
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
97
trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes);
98
}
99
ret = 0;
100
+ goto out_locked;
101
102
-fail:
103
+out_unlocked:
104
+ qemu_co_mutex_lock(&s->lock);
105
+
106
+out_locked:
107
qcow2_handle_l2meta(bs, &l2meta, false);
108
109
qemu_co_mutex_unlock(&s->lock);
110
--
111
2.21.0
112
113
diff view generated by jsdifflib
Deleted patch
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2
1
3
Do encryption/decryption in threads, like it is already done for
4
compression. This improves asynchronous encrypted io.
5
6
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7
Reviewed-by: Alberto Garcia <berto@igalia.com>
8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9
Message-id: 20190506142741.41731-9-vsementsov@virtuozzo.com
10
Signed-off-by: Max Reitz <mreitz@redhat.com>
11
---
12
block/qcow2.h | 8 ++++++
13
block/qcow2-cluster.c | 7 ++---
14
block/qcow2-threads.c | 65 +++++++++++++++++++++++++++++++++++++++++--
15
block/qcow2.c | 22 +++++----------
16
4 files changed, 81 insertions(+), 21 deletions(-)
17
18
diff --git a/block/qcow2.h b/block/qcow2.h
19
index XXXXXXX..XXXXXXX 100644
20
--- a/block/qcow2.h
21
+++ b/block/qcow2.h
22
@@ -XXX,XX +XXX,XX @@ typedef struct Qcow2BitmapHeaderExt {
23
uint64_t bitmap_directory_offset;
24
} QEMU_PACKED Qcow2BitmapHeaderExt;
25
26
+#define QCOW2_MAX_THREADS 4
27
+
28
typedef struct BDRVQcow2State {
29
int cluster_bits;
30
int cluster_size;
31
@@ -XXX,XX +XXX,XX @@ qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size,
32
ssize_t coroutine_fn
33
qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
34
const void *src, size_t src_size);
35
+int coroutine_fn
36
+qcow2_co_encrypt(BlockDriverState *bs, uint64_t file_cluster_offset,
37
+ uint64_t offset, void *buf, size_t len);
38
+int coroutine_fn
39
+qcow2_co_decrypt(BlockDriverState *bs, uint64_t file_cluster_offset,
40
+ uint64_t offset, void *buf, size_t len);
41
42
#endif
43
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
44
index XXXXXXX..XXXXXXX 100644
45
--- a/block/qcow2-cluster.c
46
+++ b/block/qcow2-cluster.c
47
@@ -XXX,XX +XXX,XX @@ static bool coroutine_fn do_perform_cow_encrypt(BlockDriverState *bs,
48
{
49
if (bytes && bs->encrypted) {
50
BDRVQcow2State *s = bs->opaque;
51
- int64_t offset = (s->crypt_physical_offset ?
52
- (cluster_offset + offset_in_cluster) :
53
- (src_cluster_offset + offset_in_cluster));
54
assert((offset_in_cluster & ~BDRV_SECTOR_MASK) == 0);
55
assert((bytes & ~BDRV_SECTOR_MASK) == 0);
56
assert(s->crypto);
57
- if (qcrypto_block_encrypt(s->crypto, offset, buffer, bytes, NULL) < 0) {
58
+ if (qcow2_co_encrypt(bs, cluster_offset,
59
+ src_cluster_offset + offset_in_cluster,
60
+ buffer, bytes) < 0) {
61
return false;
62
}
63
}
64
diff --git a/block/qcow2-threads.c b/block/qcow2-threads.c
65
index XXXXXXX..XXXXXXX 100644
66
--- a/block/qcow2-threads.c
67
+++ b/block/qcow2-threads.c
68
@@ -XXX,XX +XXX,XX @@
69
70
#include "qcow2.h"
71
#include "block/thread-pool.h"
72
-
73
-#define QCOW2_MAX_THREADS 4
74
+#include "crypto.h"
75
76
static int coroutine_fn
77
qcow2_co_process(BlockDriverState *bs, ThreadPoolFunc *func, void *arg)
78
@@ -XXX,XX +XXX,XX @@ qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
79
return qcow2_co_do_compress(bs, dest, dest_size, src, src_size,
80
qcow2_decompress);
81
}
82
+
83
+
84
+/*
85
+ * Cryptography
86
+ */
87
+
88
+/*
89
+ * Qcow2EncDecFunc: common prototype of qcrypto_block_encrypt() and
90
+ * qcrypto_block_decrypt() functions.
91
+ */
92
+typedef int (*Qcow2EncDecFunc)(QCryptoBlock *block, uint64_t offset,
93
+ uint8_t *buf, size_t len, Error **errp);
94
+
95
+typedef struct Qcow2EncDecData {
96
+ QCryptoBlock *block;
97
+ uint64_t offset;
98
+ uint8_t *buf;
99
+ size_t len;
100
+
101
+ Qcow2EncDecFunc func;
102
+} Qcow2EncDecData;
103
+
104
+static int qcow2_encdec_pool_func(void *opaque)
105
+{
106
+ Qcow2EncDecData *data = opaque;
107
+
108
+ return data->func(data->block, data->offset, data->buf, data->len, NULL);
109
+}
110
+
111
+static int coroutine_fn
112
+qcow2_co_encdec(BlockDriverState *bs, uint64_t file_cluster_offset,
113
+ uint64_t offset, void *buf, size_t len, Qcow2EncDecFunc func)
114
+{
115
+ BDRVQcow2State *s = bs->opaque;
116
+ Qcow2EncDecData arg = {
117
+ .block = s->crypto,
118
+ .offset = s->crypt_physical_offset ?
119
+ file_cluster_offset + offset_into_cluster(s, offset) :
120
+ offset,
121
+ .buf = buf,
122
+ .len = len,
123
+ .func = func,
124
+ };
125
+
126
+ return qcow2_co_process(bs, qcow2_encdec_pool_func, &arg);
127
+}
128
+
129
+int coroutine_fn
130
+qcow2_co_encrypt(BlockDriverState *bs, uint64_t file_cluster_offset,
131
+ uint64_t offset, void *buf, size_t len)
132
+{
133
+ return qcow2_co_encdec(bs, file_cluster_offset, offset, buf, len,
134
+ qcrypto_block_encrypt);
135
+}
136
+
137
+int coroutine_fn
138
+qcow2_co_decrypt(BlockDriverState *bs, uint64_t file_cluster_offset,
139
+ uint64_t offset, void *buf, size_t len)
140
+{
141
+ return qcow2_co_encdec(bs, file_cluster_offset, offset, buf, len,
142
+ qcrypto_block_decrypt);
143
+}
144
diff --git a/block/qcow2.c b/block/qcow2.c
145
index XXXXXXX..XXXXXXX 100644
146
--- a/block/qcow2.c
147
+++ b/block/qcow2.c
148
@@ -XXX,XX +XXX,XX @@ static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
149
}
150
s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
151
qcow2_crypto_hdr_read_func,
152
- bs, cflags, 1, errp);
153
+ bs, cflags, QCOW2_MAX_THREADS, errp);
154
if (!s->crypto) {
155
return -EINVAL;
156
}
157
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
158
cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
159
}
160
s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
161
- NULL, NULL, cflags, 1, errp);
162
+ NULL, NULL, cflags,
163
+ QCOW2_MAX_THREADS, errp);
164
if (!s->crypto) {
165
ret = -EINVAL;
166
goto fail;
167
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
168
assert(s->crypto);
169
assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
170
assert((cur_bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
171
- if (qcrypto_block_decrypt(s->crypto,
172
- (s->crypt_physical_offset ?
173
- cluster_offset + offset_in_cluster :
174
- offset),
175
- cluster_data,
176
- cur_bytes,
177
- NULL) < 0) {
178
+ if (qcow2_co_decrypt(bs, cluster_offset, offset,
179
+ cluster_data, cur_bytes) < 0) {
180
ret = -EIO;
181
goto fail;
182
}
183
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
184
QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
185
qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
186
187
- if (qcrypto_block_encrypt(s->crypto,
188
- (s->crypt_physical_offset ?
189
- cluster_offset + offset_in_cluster :
190
- offset),
191
- cluster_data,
192
- cur_bytes, NULL) < 0) {
193
+ if (qcow2_co_encrypt(bs, cluster_offset, offset,
194
+ cluster_data, cur_bytes) < 0) {
195
ret = -EIO;
196
goto out_unlocked;
197
}
198
--
199
2.21.0
200
201
diff view generated by jsdifflib
Deleted patch
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2
1
3
Simplify backup_incremental_init_copy_bitmap using the function
4
bdrv_dirty_bitmap_next_dirty_area.
5
6
Note: move to job->len instead of bitmap size: it should not matter but
7
less code.
8
9
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
10
Reviewed-by: Max Reitz <mreitz@redhat.com>
11
Message-id: 20190429090842.57910-2-vsementsov@virtuozzo.com
12
Signed-off-by: Max Reitz <mreitz@redhat.com>
13
---
14
block/backup.c | 40 ++++++++++++----------------------------
15
1 file changed, 12 insertions(+), 28 deletions(-)
16
17
diff --git a/block/backup.c b/block/backup.c
18
index XXXXXXX..XXXXXXX 100644
19
--- a/block/backup.c
20
+++ b/block/backup.c
21
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
22
/* init copy_bitmap from sync_bitmap */
23
static void backup_incremental_init_copy_bitmap(BackupBlockJob *job)
24
{
25
- BdrvDirtyBitmapIter *dbi;
26
- int64_t offset;
27
- int64_t end = DIV_ROUND_UP(bdrv_dirty_bitmap_size(job->sync_bitmap),
28
- job->cluster_size);
29
-
30
- dbi = bdrv_dirty_iter_new(job->sync_bitmap);
31
- while ((offset = bdrv_dirty_iter_next(dbi)) != -1) {
32
- int64_t cluster = offset / job->cluster_size;
33
- int64_t next_cluster;
34
-
35
- offset += bdrv_dirty_bitmap_granularity(job->sync_bitmap);
36
- if (offset >= bdrv_dirty_bitmap_size(job->sync_bitmap)) {
37
- hbitmap_set(job->copy_bitmap, cluster, end - cluster);
38
- break;
39
- }
40
+ uint64_t offset = 0;
41
+ uint64_t bytes = job->len;
42
43
- offset = bdrv_dirty_bitmap_next_zero(job->sync_bitmap, offset,
44
- UINT64_MAX);
45
- if (offset == -1) {
46
- hbitmap_set(job->copy_bitmap, cluster, end - cluster);
47
- break;
48
- }
49
+ while (bdrv_dirty_bitmap_next_dirty_area(job->sync_bitmap,
50
+ &offset, &bytes))
51
+ {
52
+ uint64_t cluster = offset / job->cluster_size;
53
+ uint64_t end_cluster = DIV_ROUND_UP(offset + bytes, job->cluster_size);
54
55
- next_cluster = DIV_ROUND_UP(offset, job->cluster_size);
56
- hbitmap_set(job->copy_bitmap, cluster, next_cluster - cluster);
57
- if (next_cluster >= end) {
58
+ hbitmap_set(job->copy_bitmap, cluster, end_cluster - cluster);
59
+
60
+ offset = end_cluster * job->cluster_size;
61
+ if (offset >= job->len) {
62
break;
63
}
64
-
65
- bdrv_set_dirty_iter(dbi, next_cluster * job->cluster_size);
66
+ bytes = job->len - offset;
67
}
68
69
/* TODO job_progress_set_remaining() would make more sense */
70
job_progress_update(&job->common.job,
71
job->len - hbitmap_count(job->copy_bitmap) * job->cluster_size);
72
-
73
- bdrv_dirty_iter_free(dbi);
74
}
75
76
static int coroutine_fn backup_run(Job *job, Error **errp)
77
--
78
2.21.0
79
80
diff view generated by jsdifflib
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
1
From: Paolo Bonzini <pbonzini@redhat.com>
2
2
3
We are going to share this bitmap between backup and backup-top filter
3
All callers of qemu_co_sleep_wake are checking whether they are passing
4
driver, so let's share something more meaningful. It also simplifies
4
a NULL argument inside the pointer-to-pointer: do the check in
5
some calculations.
5
qemu_co_sleep_wake itself.
6
6
7
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7
As a side effect, qemu_co_sleep_wake can be called more than once and
8
Reviewed-by: Max Reitz <mreitz@redhat.com>
8
it will only wake the coroutine once; after the first time, the argument
9
Message-id: 20190429090842.57910-3-vsementsov@virtuozzo.com
9
will be set to NULL via *sleep_state->user_state_pointer. However, this
10
Signed-off-by: Max Reitz <mreitz@redhat.com>
10
would not be safe unless co_sleep_cb keeps using the QemuCoSleepState*
11
directly, so make it go through the pointer-to-pointer instead.
12
13
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
14
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
15
Message-id: 20210517100548.28806-4-pbonzini@redhat.com
16
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
11
---
17
---
12
block/backup.c | 48 +++++++++++++++++++++++-------------------------
18
block/block-copy.c | 4 +---
13
1 file changed, 23 insertions(+), 25 deletions(-)
19
block/nbd.c | 8 ++------
20
util/qemu-coroutine-sleep.c | 21 ++++++++++++---------
21
3 files changed, 15 insertions(+), 18 deletions(-)
14
22
15
diff --git a/block/backup.c b/block/backup.c
23
diff --git a/block/block-copy.c b/block/block-copy.c
16
index XXXXXXX..XXXXXXX 100644
24
index XXXXXXX..XXXXXXX 100644
17
--- a/block/backup.c
25
--- a/block/block-copy.c
18
+++ b/block/backup.c
26
+++ b/block/block-copy.c
19
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn backup_cow_with_bounce_buffer(BackupBlockJob *job,
27
@@ -XXX,XX +XXX,XX @@ out:
20
int read_flags = is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0;
28
21
int write_flags = job->serialize_target_writes ? BDRV_REQ_SERIALISING : 0;
29
void block_copy_kick(BlockCopyCallState *call_state)
22
30
{
23
- hbitmap_reset(job->copy_bitmap, start / job->cluster_size, 1);
31
- if (call_state->sleep_state) {
24
+ assert(QEMU_IS_ALIGNED(start, job->cluster_size));
32
- qemu_co_sleep_wake(call_state->sleep_state);
25
+ hbitmap_reset(job->copy_bitmap, start, job->cluster_size);
33
- }
26
nbytes = MIN(job->cluster_size, job->len - start);
34
+ qemu_co_sleep_wake(call_state->sleep_state);
27
if (!*bounce_buffer) {
28
*bounce_buffer = blk_blockalign(blk, job->cluster_size);
29
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn backup_cow_with_bounce_buffer(BackupBlockJob *job,
30
31
return nbytes;
32
fail:
33
- hbitmap_set(job->copy_bitmap, start / job->cluster_size, 1);
34
+ hbitmap_set(job->copy_bitmap, start, job->cluster_size);
35
return ret;
36
37
}
35
}
38
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn backup_cow_with_offload(BackupBlockJob *job,
36
39
int write_flags = job->serialize_target_writes ? BDRV_REQ_SERIALISING : 0;
37
/*
40
38
diff --git a/block/nbd.c b/block/nbd.c
41
assert(QEMU_IS_ALIGNED(job->copy_range_size, job->cluster_size));
39
index XXXXXXX..XXXXXXX 100644
42
+ assert(QEMU_IS_ALIGNED(start, job->cluster_size));
40
--- a/block/nbd.c
43
nbytes = MIN(job->copy_range_size, end - start);
41
+++ b/block/nbd.c
44
nr_clusters = DIV_ROUND_UP(nbytes, job->cluster_size);
42
@@ -XXX,XX +XXX,XX @@ static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs)
45
- hbitmap_reset(job->copy_bitmap, start / job->cluster_size,
43
BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
46
- nr_clusters);
44
47
+ hbitmap_reset(job->copy_bitmap, start, job->cluster_size * nr_clusters);
45
s->drained = true;
48
ret = blk_co_copy_range(blk, start, job->target, start, nbytes,
46
- if (s->connection_co_sleep_ns_state) {
49
read_flags, write_flags);
47
- qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
50
if (ret < 0) {
48
- }
51
trace_backup_do_cow_copy_range_fail(job, start, ret);
49
+ qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
52
- hbitmap_set(job->copy_bitmap, start / job->cluster_size,
50
53
- nr_clusters);
51
nbd_co_establish_connection_cancel(bs, false);
54
+ hbitmap_set(job->copy_bitmap, start, job->cluster_size * nr_clusters);
52
55
return ret;
53
@@ -XXX,XX +XXX,XX @@ static void nbd_teardown_connection(BlockDriverState *bs)
54
55
s->state = NBD_CLIENT_QUIT;
56
if (s->connection_co) {
57
- if (s->connection_co_sleep_ns_state) {
58
- qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
59
- }
60
+ qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
61
nbd_co_establish_connection_cancel(bs, true);
56
}
62
}
57
63
if (qemu_in_coroutine()) {
58
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn backup_do_cow(BackupBlockJob *job,
64
diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c
59
cow_request_begin(&cow_request, job, start, end);
65
index XXXXXXX..XXXXXXX 100644
60
66
--- a/util/qemu-coroutine-sleep.c
61
while (start < end) {
67
+++ b/util/qemu-coroutine-sleep.c
62
- if (!hbitmap_get(job->copy_bitmap, start / job->cluster_size)) {
68
@@ -XXX,XX +XXX,XX @@ struct QemuCoSleepState {
63
+ if (!hbitmap_get(job->copy_bitmap, start)) {
69
64
trace_backup_do_cow_skip(job, start);
70
void qemu_co_sleep_wake(QemuCoSleepState *sleep_state)
65
start += job->cluster_size;
71
{
66
continue; /* already copied */
72
- /* Write of schedule protected by barrier write in aio_co_schedule */
67
@@ -XXX,XX +XXX,XX @@ static void backup_clean(Job *job)
73
- const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled,
68
assert(s->target);
74
- qemu_co_sleep_ns__scheduled, NULL);
69
blk_unref(s->target);
75
+ if (sleep_state) {
70
s->target = NULL;
76
+ /* Write of schedule protected by barrier write in aio_co_schedule */
71
+
77
+ const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled,
72
+ if (s->copy_bitmap) {
78
+ qemu_co_sleep_ns__scheduled, NULL);
73
+ hbitmap_free(s->copy_bitmap);
79
74
+ s->copy_bitmap = NULL;
80
- assert(scheduled == qemu_co_sleep_ns__scheduled);
81
- *sleep_state->user_state_pointer = NULL;
82
- timer_del(&sleep_state->ts);
83
- aio_co_wake(sleep_state->co);
84
+ assert(scheduled == qemu_co_sleep_ns__scheduled);
85
+ *sleep_state->user_state_pointer = NULL;
86
+ timer_del(&sleep_state->ts);
87
+ aio_co_wake(sleep_state->co);
75
+ }
88
+ }
76
}
89
}
77
90
78
void backup_do_checkpoint(BlockJob *job, Error **errp)
91
static void co_sleep_cb(void *opaque)
79
{
92
{
80
BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
93
- qemu_co_sleep_wake(opaque);
81
- int64_t len;
94
+ QemuCoSleepState **sleep_state = opaque;
82
95
+ qemu_co_sleep_wake(*sleep_state);
83
assert(block_job_driver(job) == &backup_job_driver);
96
}
84
97
85
@@ -XXX,XX +XXX,XX @@ void backup_do_checkpoint(BlockJob *job, Error **errp)
98
void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
86
return;
99
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
100
abort();
87
}
101
}
88
102
89
- len = DIV_ROUND_UP(backup_job->len, backup_job->cluster_size);
103
- aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, &state);
90
- hbitmap_set(backup_job->copy_bitmap, 0, len);
104
+ aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, sleep_state);
91
+ hbitmap_set(backup_job->copy_bitmap, 0, backup_job->len);
105
*sleep_state = &state;
92
}
106
timer_mod(&state.ts, qemu_clock_get_ns(type) + ns);
93
107
qemu_coroutine_yield();
94
static void backup_drain(BlockJob *job)
95
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
96
{
97
int ret;
98
bool error_is_read;
99
- int64_t cluster;
100
+ int64_t offset;
101
HBitmapIter hbi;
102
103
hbitmap_iter_init(&hbi, job->copy_bitmap, 0);
104
- while ((cluster = hbitmap_iter_next(&hbi)) != -1) {
105
+ while ((offset = hbitmap_iter_next(&hbi)) != -1) {
106
do {
107
if (yield_and_check(job)) {
108
return 0;
109
}
110
- ret = backup_do_cow(job, cluster * job->cluster_size,
111
+ ret = backup_do_cow(job, offset,
112
job->cluster_size, &error_is_read, false);
113
if (ret < 0 && backup_error_action(job, error_is_read, -ret) ==
114
BLOCK_ERROR_ACTION_REPORT)
115
@@ -XXX,XX +XXX,XX @@ static void backup_incremental_init_copy_bitmap(BackupBlockJob *job)
116
while (bdrv_dirty_bitmap_next_dirty_area(job->sync_bitmap,
117
&offset, &bytes))
118
{
119
- uint64_t cluster = offset / job->cluster_size;
120
- uint64_t end_cluster = DIV_ROUND_UP(offset + bytes, job->cluster_size);
121
+ hbitmap_set(job->copy_bitmap, offset, bytes);
122
123
- hbitmap_set(job->copy_bitmap, cluster, end_cluster - cluster);
124
-
125
- offset = end_cluster * job->cluster_size;
126
+ offset += bytes;
127
if (offset >= job->len) {
128
break;
129
}
130
@@ -XXX,XX +XXX,XX @@ static void backup_incremental_init_copy_bitmap(BackupBlockJob *job)
131
132
/* TODO job_progress_set_remaining() would make more sense */
133
job_progress_update(&job->common.job,
134
- job->len - hbitmap_count(job->copy_bitmap) * job->cluster_size);
135
+ job->len - hbitmap_count(job->copy_bitmap));
136
}
137
138
static int coroutine_fn backup_run(Job *job, Error **errp)
139
{
140
BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
141
BlockDriverState *bs = blk_bs(s->common.blk);
142
- int64_t offset, nb_clusters;
143
+ int64_t offset;
144
int ret = 0;
145
146
QLIST_INIT(&s->inflight_reqs);
147
qemu_co_rwlock_init(&s->flush_rwlock);
148
149
- nb_clusters = DIV_ROUND_UP(s->len, s->cluster_size);
150
job_progress_set_remaining(job, s->len);
151
152
- s->copy_bitmap = hbitmap_alloc(nb_clusters, 0);
153
if (s->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
154
backup_incremental_init_copy_bitmap(s);
155
} else {
156
- hbitmap_set(s->copy_bitmap, 0, nb_clusters);
157
+ hbitmap_set(s->copy_bitmap, 0, s->len);
158
}
159
160
-
161
s->before_write.notify = backup_before_write_notify;
162
bdrv_add_before_write_notifier(bs, &s->before_write);
163
164
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn backup_run(Job *job, Error **errp)
165
/* wait until pending backup_do_cow() calls have completed */
166
qemu_co_rwlock_wrlock(&s->flush_rwlock);
167
qemu_co_rwlock_unlock(&s->flush_rwlock);
168
- hbitmap_free(s->copy_bitmap);
169
170
return ret;
171
}
172
@@ -XXX,XX +XXX,XX @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
173
} else {
174
job->cluster_size = MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
175
}
176
+
177
+ job->copy_bitmap = hbitmap_alloc(len, ctz32(job->cluster_size));
178
job->use_copy_range = true;
179
job->copy_range_size = MIN_NON_ZERO(blk_get_max_transfer(job->common.blk),
180
blk_get_max_transfer(job->target));
181
--
108
--
182
2.21.0
109
2.31.1
183
110
184
diff view generated by jsdifflib
Deleted patch
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2
1
3
Split allocation checking to separate function and reduce nesting.
4
Consider bdrv_is_allocated() fail as allocated area, as copying more
5
than needed is not wrong (and we do it anyway) and seems better than
6
fail the whole job. And, most probably we will fail on the next read,
7
if there are real problem with source.
8
9
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
10
Reviewed-by: Max Reitz <mreitz@redhat.com>
11
Message-id: 20190429090842.57910-4-vsementsov@virtuozzo.com
12
Signed-off-by: Max Reitz <mreitz@redhat.com>
13
---
14
block/backup.c | 60 +++++++++++++++++++-------------------------------
15
1 file changed, 23 insertions(+), 37 deletions(-)
16
17
diff --git a/block/backup.c b/block/backup.c
18
index XXXXXXX..XXXXXXX 100644
19
--- a/block/backup.c
20
+++ b/block/backup.c
21
@@ -XXX,XX +XXX,XX @@ static bool coroutine_fn yield_and_check(BackupBlockJob *job)
22
return false;
23
}
24
25
+static bool bdrv_is_unallocated_range(BlockDriverState *bs,
26
+ int64_t offset, int64_t bytes)
27
+{
28
+ int64_t end = offset + bytes;
29
+
30
+ while (offset < end && !bdrv_is_allocated(bs, offset, bytes, &bytes)) {
31
+ if (bytes == 0) {
32
+ return true;
33
+ }
34
+ offset += bytes;
35
+ bytes = end - offset;
36
+ }
37
+
38
+ return offset >= end;
39
+}
40
+
41
static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
42
{
43
int ret;
44
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn backup_run(Job *job, Error **errp)
45
for (offset = 0; offset < s->len;
46
offset += s->cluster_size) {
47
bool error_is_read;
48
- int alloced = 0;
49
50
if (yield_and_check(s)) {
51
break;
52
}
53
54
- if (s->sync_mode == MIRROR_SYNC_MODE_TOP) {
55
- int i;
56
- int64_t n;
57
-
58
- /* Check to see if these blocks are already in the
59
- * backing file. */
60
-
61
- for (i = 0; i < s->cluster_size;) {
62
- /* bdrv_is_allocated() only returns true/false based
63
- * on the first set of sectors it comes across that
64
- * are are all in the same state.
65
- * For that reason we must verify each sector in the
66
- * backup cluster length. We end up copying more than
67
- * needed but at some point that is always the case. */
68
- alloced =
69
- bdrv_is_allocated(bs, offset + i,
70
- s->cluster_size - i, &n);
71
- i += n;
72
-
73
- if (alloced || n == 0) {
74
- break;
75
- }
76
- }
77
-
78
- /* If the above loop never found any sectors that are in
79
- * the topmost image, skip this backup. */
80
- if (alloced == 0) {
81
- continue;
82
- }
83
- }
84
- /* FULL sync mode we copy the whole drive. */
85
- if (alloced < 0) {
86
- ret = alloced;
87
- } else {
88
- ret = backup_do_cow(s, offset, s->cluster_size,
89
- &error_is_read, false);
90
+ if (s->sync_mode == MIRROR_SYNC_MODE_TOP &&
91
+ bdrv_is_unallocated_range(bs, offset, s->cluster_size))
92
+ {
93
+ continue;
94
}
95
+
96
+ ret = backup_do_cow(s, offset, s->cluster_size,
97
+ &error_is_read, false);
98
if (ret < 0) {
99
/* Depending on error action, fail now or retry cluster */
100
BlockErrorAction action =
101
--
102
2.21.0
103
104
diff view generated by jsdifflib
Deleted patch
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2
1
3
Do full, top and incremental mode copying all in one place. This
4
unifies the code path and helps further improvements.
5
6
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7
Reviewed-by: Max Reitz <mreitz@redhat.com>
8
Message-id: 20190429090842.57910-5-vsementsov@virtuozzo.com
9
Signed-off-by: Max Reitz <mreitz@redhat.com>
10
---
11
block/backup.c | 43 ++++++++++---------------------------------
12
1 file changed, 10 insertions(+), 33 deletions(-)
13
14
diff --git a/block/backup.c b/block/backup.c
15
index XXXXXXX..XXXXXXX 100644
16
--- a/block/backup.c
17
+++ b/block/backup.c
18
@@ -XXX,XX +XXX,XX @@ static bool bdrv_is_unallocated_range(BlockDriverState *bs,
19
return offset >= end;
20
}
21
22
-static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
23
+static int coroutine_fn backup_loop(BackupBlockJob *job)
24
{
25
int ret;
26
bool error_is_read;
27
int64_t offset;
28
HBitmapIter hbi;
29
+ BlockDriverState *bs = blk_bs(job->common.blk);
30
31
hbitmap_iter_init(&hbi, job->copy_bitmap, 0);
32
while ((offset = hbitmap_iter_next(&hbi)) != -1) {
33
+ if (job->sync_mode == MIRROR_SYNC_MODE_TOP &&
34
+ bdrv_is_unallocated_range(bs, offset, job->cluster_size))
35
+ {
36
+ hbitmap_reset(job->copy_bitmap, offset, job->cluster_size);
37
+ continue;
38
+ }
39
+
40
do {
41
if (yield_and_check(job)) {
42
return 0;
43
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn backup_run(Job *job, Error **errp)
44
{
45
BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
46
BlockDriverState *bs = blk_bs(s->common.blk);
47
- int64_t offset;
48
int ret = 0;
49
50
QLIST_INIT(&s->inflight_reqs);
51
@@ -XXX,XX +XXX,XX @@ static int coroutine_fn backup_run(Job *job, Error **errp)
52
* notify callback service CoW requests. */
53
job_yield(job);
54
}
55
- } else if (s->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
56
- ret = backup_run_incremental(s);
57
} else {
58
- /* Both FULL and TOP SYNC_MODE's require copying.. */
59
- for (offset = 0; offset < s->len;
60
- offset += s->cluster_size) {
61
- bool error_is_read;
62
-
63
- if (yield_and_check(s)) {
64
- break;
65
- }
66
-
67
- if (s->sync_mode == MIRROR_SYNC_MODE_TOP &&
68
- bdrv_is_unallocated_range(bs, offset, s->cluster_size))
69
- {
70
- continue;
71
- }
72
-
73
- ret = backup_do_cow(s, offset, s->cluster_size,
74
- &error_is_read, false);
75
- if (ret < 0) {
76
- /* Depending on error action, fail now or retry cluster */
77
- BlockErrorAction action =
78
- backup_error_action(s, error_is_read, -ret);
79
- if (action == BLOCK_ERROR_ACTION_REPORT) {
80
- break;
81
- } else {
82
- offset -= s->cluster_size;
83
- continue;
84
- }
85
- }
86
- }
87
+ ret = backup_loop(s);
88
}
89
90
notifier_with_return_remove(&s->before_write);
91
--
92
2.21.0
93
94
diff view generated by jsdifflib
1
From: Alberto Garcia <berto@igalia.com>
1
From: Paolo Bonzini <pbonzini@redhat.com>
2
2
3
A consequence of the previous patch is that bdrv_attach_child()
3
This simplification is enabled by the previous patch. Now aio_co_wake
4
transfers the reference to child_bs from the caller to parent_bs,
4
will only be called once, therefore we do not care about a spurious
5
which will drop it on bdrv_close() or when someone calls
5
firing of the timer after a qemu_co_sleep_wake.
6
bdrv_unref_child().
7
6
8
But this only happens when bdrv_attach_child() succeeds. If it fails
7
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
9
then the caller is responsible for dropping the reference to child_bs.
8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9
Message-id: 20210517100548.28806-5-pbonzini@redhat.com
10
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
11
---
12
util/qemu-coroutine-sleep.c | 8 ++++----
13
1 file changed, 4 insertions(+), 4 deletions(-)
10
14
11
This patch makes bdrv_attach_child() take the reference also when
15
diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c
12
there is an error, freeing the caller for having to do it.
13
14
A similar situation happens with bdrv_root_attach_child(), so the
15
changes on this patch affect both functions.
16
17
Signed-off-by: Alberto Garcia <berto@igalia.com>
18
Message-id: 20dfb3d9ccec559cdd1a9690146abad5d204a186.1557754872.git.berto@igalia.com
19
[mreitz: Removed now superfluous BdrvChild * variable in
20
bdrv_open_child()]
21
Signed-off-by: Max Reitz <mreitz@redhat.com>
22
---
23
block.c | 30 ++++++++++++++++++------------
24
block/block-backend.c | 3 +--
25
block/quorum.c | 1 -
26
blockjob.c | 2 +-
27
4 files changed, 20 insertions(+), 16 deletions(-)
28
29
diff --git a/block.c b/block.c
30
index XXXXXXX..XXXXXXX 100644
16
index XXXXXXX..XXXXXXX 100644
31
--- a/block.c
17
--- a/util/qemu-coroutine-sleep.c
32
+++ b/block.c
18
+++ b/util/qemu-coroutine-sleep.c
33
@@ -XXX,XX +XXX,XX @@ static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs)
19
@@ -XXX,XX +XXX,XX @@ static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns";
20
21
struct QemuCoSleepState {
22
Coroutine *co;
23
- QEMUTimer ts;
24
QemuCoSleepState **user_state_pointer;
25
};
26
27
@@ -XXX,XX +XXX,XX @@ void qemu_co_sleep_wake(QemuCoSleepState *sleep_state)
28
29
assert(scheduled == qemu_co_sleep_ns__scheduled);
30
*sleep_state->user_state_pointer = NULL;
31
- timer_del(&sleep_state->ts);
32
aio_co_wake(sleep_state->co);
34
}
33
}
35
}
34
}
36
35
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
37
+/*
36
QemuCoSleepState **sleep_state)
38
+ * This function steals the reference to child_bs from the caller.
37
{
39
+ * That reference is later dropped by bdrv_root_unref_child().
38
AioContext *ctx = qemu_get_current_aio_context();
40
+ *
39
+ QEMUTimer ts;
41
+ * On failure NULL is returned, errp is set and the reference to
40
QemuCoSleepState state = {
42
+ * child_bs is also dropped.
41
.co = qemu_coroutine_self(),
43
+ */
42
.user_state_pointer = sleep_state,
44
BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
43
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
45
const char *child_name,
44
abort();
46
const BdrvChildRole *child_role,
47
@@ -XXX,XX +XXX,XX @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
48
ret = bdrv_check_update_perm(child_bs, NULL, perm, shared_perm, NULL, errp);
49
if (ret < 0) {
50
bdrv_abort_perm_update(child_bs);
51
+ bdrv_unref(child_bs);
52
return NULL;
53
}
45
}
54
46
55
@@ -XXX,XX +XXX,XX @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
47
- aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, sleep_state);
56
return child;
48
+ aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, sleep_state);
57
}
49
*sleep_state = &state;
58
50
- timer_mod(&state.ts, qemu_clock_get_ns(type) + ns);
59
+/*
51
+ timer_mod(&ts, qemu_clock_get_ns(type) + ns);
60
+ * This function transfers the reference to child_bs from the caller
52
qemu_coroutine_yield();
61
+ * to parent_bs. That reference is later dropped by parent_bs on
53
+ timer_del(&ts);
62
+ * bdrv_close() or if someone calls bdrv_unref_child().
54
63
+ *
55
/* qemu_co_sleep_wake clears *sleep_state before resuming this coroutine. */
64
+ * On failure NULL is returned, errp is set and the reference to
56
assert(*sleep_state == NULL);
65
+ * child_bs is also dropped.
66
+ */
67
BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
68
BlockDriverState *child_bs,
69
const char *child_name,
70
@@ -XXX,XX +XXX,XX @@ void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
71
/* If backing_hd was already part of bs's backing chain, and
72
* inherits_from pointed recursively to bs then let's update it to
73
* point directly to bs (else it will become NULL). */
74
- if (update_inherits_from) {
75
+ if (bs->backing && update_inherits_from) {
76
backing_hd->inherits_from = bs;
77
}
78
- if (!bs->backing) {
79
- bdrv_unref(backing_hd);
80
- }
81
82
out:
83
bdrv_refresh_limits(bs, NULL);
84
@@ -XXX,XX +XXX,XX @@ BdrvChild *bdrv_open_child(const char *filename,
85
const BdrvChildRole *child_role,
86
bool allow_none, Error **errp)
87
{
88
- BdrvChild *c;
89
BlockDriverState *bs;
90
91
bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_role,
92
@@ -XXX,XX +XXX,XX @@ BdrvChild *bdrv_open_child(const char *filename,
93
return NULL;
94
}
95
96
- c = bdrv_attach_child(parent, bs, bdref_key, child_role, errp);
97
- if (!c) {
98
- bdrv_unref(bs);
99
- return NULL;
100
- }
101
-
102
- return c;
103
+ return bdrv_attach_child(parent, bs, bdref_key, child_role, errp);
104
}
105
106
/* TODO Future callers may need to specify parent/child_role in order for
107
diff --git a/block/block-backend.c b/block/block-backend.c
108
index XXXXXXX..XXXXXXX 100644
109
--- a/block/block-backend.c
110
+++ b/block/block-backend.c
111
@@ -XXX,XX +XXX,XX @@ BlockBackend *blk_new_open(const char *filename, const char *reference,
112
blk->root = bdrv_root_attach_child(bs, "root", &child_root,
113
perm, BLK_PERM_ALL, blk, errp);
114
if (!blk->root) {
115
- bdrv_unref(bs);
116
blk_unref(blk);
117
return NULL;
118
}
119
@@ -XXX,XX +XXX,XX @@ void blk_remove_bs(BlockBackend *blk)
120
int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)
121
{
122
ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
123
+ bdrv_ref(bs);
124
blk->root = bdrv_root_attach_child(bs, "root", &child_root,
125
blk->perm, blk->shared_perm, blk, errp);
126
if (blk->root == NULL) {
127
return -EPERM;
128
}
129
- bdrv_ref(bs);
130
131
notifier_list_notify(&blk->insert_bs_notifiers, blk);
132
if (tgm->throttle_state) {
133
diff --git a/block/quorum.c b/block/quorum.c
134
index XXXXXXX..XXXXXXX 100644
135
--- a/block/quorum.c
136
+++ b/block/quorum.c
137
@@ -XXX,XX +XXX,XX @@ static void quorum_add_child(BlockDriverState *bs, BlockDriverState *child_bs,
138
child = bdrv_attach_child(bs, child_bs, indexstr, &child_format, errp);
139
if (child == NULL) {
140
s->next_child_index--;
141
- bdrv_unref(child_bs);
142
goto out;
143
}
144
s->children = g_renew(BdrvChild *, s->children, s->num_children + 1);
145
diff --git a/blockjob.c b/blockjob.c
146
index XXXXXXX..XXXXXXX 100644
147
--- a/blockjob.c
148
+++ b/blockjob.c
149
@@ -XXX,XX +XXX,XX @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
150
{
151
BdrvChild *c;
152
153
+ bdrv_ref(bs);
154
c = bdrv_root_attach_child(bs, name, &child_job, perm, shared_perm,
155
job, errp);
156
if (c == NULL) {
157
@@ -XXX,XX +XXX,XX @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
158
}
159
160
job->nodes = g_slist_prepend(job->nodes, c);
161
- bdrv_ref(bs);
162
bdrv_op_block_all(bs, job->blocker);
163
164
return 0;
165
--
57
--
166
2.21.0
58
2.31.1
167
59
168
diff view generated by jsdifflib
1
From: Sam Eiderman <shmuel.eiderman@oracle.com>
1
From: Paolo Bonzini <pbonzini@redhat.com>
2
2
3
In the following case:
3
Right now, users of qemu_co_sleep_ns_wakeable are simply passing
4
4
a pointer to QemuCoSleepState by reference to the function. But
5
(base) A <- B <- C (tip)
5
QemuCoSleepState really is just a Coroutine*; making the
6
6
content of the struct public is just as efficient and lets us
7
when running:
7
skip the user_state_pointer indirection.
8
8
9
qemu-img rebase -b A C
9
Since the usage is changed, take the occasion to rename the
10
10
struct to QemuCoSleep.
11
QEMU would read all sectors not allocated in the file being rebased (C)
11
12
and compare them to the new base image (A), regardless of whether they
12
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
13
were changed or even allocated anywhere along the chain between the new
13
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
14
base and the top image (B). This causes many unneeded reads when
14
Message-id: 20210517100548.28806-6-pbonzini@redhat.com
15
rebasing an image which represents a small diff of a large disk, as it
15
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
16
would read most of the disk's sectors.
17
18
Instead, use bdrv_is_allocated_above() to reduce the number of
19
unnecessary reads.
20
21
Reviewed-by: Karl Heubaum <karl.heubaum@oracle.com>
22
Signed-off-by: Sam Eiderman <shmuel.eiderman@oracle.com>
23
Signed-off-by: Eyal Moscovici <eyal.moscovici@oracle.com>
24
Message-id: 20190523163337.4497-3-shmuel.eiderman@oracle.com
25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26
---
16
---
27
qemu-img.c | 25 ++++++++++++++++++++++++-
17
include/qemu/coroutine.h | 23 +++++++++++----------
28
1 file changed, 24 insertions(+), 1 deletion(-)
18
block/block-copy.c | 8 ++++----
29
19
block/nbd.c | 10 ++++-----
30
diff --git a/qemu-img.c b/qemu-img.c
20
util/qemu-coroutine-sleep.c | 41 ++++++++++++++++---------------------
31
index XXXXXXX..XXXXXXX 100644
21
4 files changed, 39 insertions(+), 43 deletions(-)
32
--- a/qemu-img.c
22
33
+++ b/qemu-img.c
23
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
34
@@ -XXX,XX +XXX,XX @@ static int img_rebase(int argc, char **argv)
24
index XXXXXXX..XXXXXXX 100644
35
BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
25
--- a/include/qemu/coroutine.h
36
uint8_t *buf_old = NULL;
26
+++ b/include/qemu/coroutine.h
37
uint8_t *buf_new = NULL;
27
@@ -XXX,XX +XXX,XX @@ void qemu_co_rwlock_wrlock(CoRwlock *lock);
38
- BlockDriverState *bs = NULL;
28
*/
39
+ BlockDriverState *bs = NULL, *prefix_chain_bs = NULL;
29
void qemu_co_rwlock_unlock(CoRwlock *lock);
40
char *filename;
30
41
const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
31
-typedef struct QemuCoSleepState QemuCoSleepState;
42
int c, flags, src_flags, ret;
32
+typedef struct QemuCoSleep {
43
@@ -XXX,XX +XXX,XX @@ static int img_rebase(int argc, char **argv)
33
+ Coroutine *to_wake;
44
goto out;
34
+} QemuCoSleep;
35
36
/**
37
- * Yield the coroutine for a given duration. During this yield, @sleep_state
38
- * is set to an opaque pointer, which may be used for
39
- * qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when the
40
- * timer fires. Don't save the obtained value to other variables and don't call
41
- * qemu_co_sleep_wake from another aio context.
42
+ * Yield the coroutine for a given duration. Initializes @w so that,
43
+ * during this yield, it can be passed to qemu_co_sleep_wake() to
44
+ * terminate the sleep.
45
*/
46
-void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
47
- QemuCoSleepState **sleep_state);
48
+void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
49
+ QEMUClockType type, int64_t ns);
50
+
51
static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
52
{
53
- QemuCoSleepState *unused = NULL;
54
- qemu_co_sleep_ns_wakeable(type, ns, &unused);
55
+ QemuCoSleep w = { 0 };
56
+ qemu_co_sleep_ns_wakeable(&w, type, ns);
57
}
58
59
/**
60
@@ -XXX,XX +XXX,XX @@ static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
61
* qemu_co_sleep_ns() and should be checked to be non-NULL before calling
62
* qemu_co_sleep_wake().
63
*/
64
-void qemu_co_sleep_wake(QemuCoSleepState *sleep_state);
65
+void qemu_co_sleep_wake(QemuCoSleep *w);
66
67
/**
68
* Yield until a file descriptor becomes readable
69
diff --git a/block/block-copy.c b/block/block-copy.c
70
index XXXXXXX..XXXXXXX 100644
71
--- a/block/block-copy.c
72
+++ b/block/block-copy.c
73
@@ -XXX,XX +XXX,XX @@ typedef struct BlockCopyCallState {
74
/* State */
75
int ret;
76
bool finished;
77
- QemuCoSleepState *sleep_state;
78
+ QemuCoSleep sleep;
79
bool cancelled;
80
81
/* OUT parameters */
82
@@ -XXX,XX +XXX,XX @@ block_copy_dirty_clusters(BlockCopyCallState *call_state)
83
if (ns > 0) {
84
block_copy_task_end(task, -EAGAIN);
85
g_free(task);
86
- qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, ns,
87
- &call_state->sleep_state);
88
+ qemu_co_sleep_ns_wakeable(&call_state->sleep,
89
+ QEMU_CLOCK_REALTIME, ns);
90
continue;
91
}
45
}
92
}
46
93
@@ -XXX,XX +XXX,XX @@ out:
47
+ /*
94
48
+ * Find out whether we rebase an image on top of a previous image
95
void block_copy_kick(BlockCopyCallState *call_state)
49
+ * in its chain.
96
{
50
+ */
97
- qemu_co_sleep_wake(call_state->sleep_state);
51
+ prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path);
98
+ qemu_co_sleep_wake(&call_state->sleep);
52
+
99
}
53
blk_new_backing = blk_new_open(out_real_path, NULL,
100
54
options, src_flags, &local_err);
101
/*
55
g_free(out_real_path);
102
diff --git a/block/nbd.c b/block/nbd.c
56
@@ -XXX,XX +XXX,XX @@ static int img_rebase(int argc, char **argv)
103
index XXXXXXX..XXXXXXX 100644
104
--- a/block/nbd.c
105
+++ b/block/nbd.c
106
@@ -XXX,XX +XXX,XX @@ typedef struct BDRVNBDState {
107
CoQueue free_sema;
108
Coroutine *connection_co;
109
Coroutine *teardown_co;
110
- QemuCoSleepState *connection_co_sleep_ns_state;
111
+ QemuCoSleep reconnect_sleep;
112
bool drained;
113
bool wait_drained_end;
114
int in_flight;
115
@@ -XXX,XX +XXX,XX @@ static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs)
116
BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
117
118
s->drained = true;
119
- qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
120
+ qemu_co_sleep_wake(&s->reconnect_sleep);
121
122
nbd_co_establish_connection_cancel(bs, false);
123
124
@@ -XXX,XX +XXX,XX @@ static void nbd_teardown_connection(BlockDriverState *bs)
125
126
s->state = NBD_CLIENT_QUIT;
127
if (s->connection_co) {
128
- qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
129
+ qemu_co_sleep_wake(&s->reconnect_sleep);
130
nbd_co_establish_connection_cancel(bs, true);
131
}
132
if (qemu_in_coroutine()) {
133
@@ -XXX,XX +XXX,XX @@ static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s)
134
}
135
bdrv_inc_in_flight(s->bs);
136
} else {
137
- qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout,
138
- &s->connection_co_sleep_ns_state);
139
+ qemu_co_sleep_ns_wakeable(&s->reconnect_sleep,
140
+ QEMU_CLOCK_REALTIME, timeout);
141
if (s->drained) {
57
continue;
142
continue;
58
}
143
}
59
144
diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c
60
+ if (prefix_chain_bs) {
145
index XXXXXXX..XXXXXXX 100644
61
+ /*
146
--- a/util/qemu-coroutine-sleep.c
62
+ * If cluster wasn't changed since prefix_chain, we don't need
147
+++ b/util/qemu-coroutine-sleep.c
63
+ * to take action
148
@@ -XXX,XX +XXX,XX @@
64
+ */
149
65
+ ret = bdrv_is_allocated_above(backing_bs(bs), prefix_chain_bs,
150
static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns";
66
+ offset, n, &n);
151
67
+ if (ret < 0) {
152
-struct QemuCoSleepState {
68
+ error_report("error while reading image metadata: %s",
153
+void qemu_co_sleep_wake(QemuCoSleep *w)
69
+ strerror(-ret));
154
+{
70
+ goto out;
155
Coroutine *co;
71
+ }
156
- QemuCoSleepState **user_state_pointer;
72
+ if (!ret) {
157
-};
73
+ continue;
158
74
+ }
159
-void qemu_co_sleep_wake(QemuCoSleepState *sleep_state)
75
+ }
160
-{
76
+
161
- if (sleep_state) {
77
/*
162
+ co = w->to_wake;
78
* Read old and new backing file and take into consideration that
163
+ w->to_wake = NULL;
79
* backing files may be smaller than the COW image.
164
+ if (co) {
165
/* Write of schedule protected by barrier write in aio_co_schedule */
166
- const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled,
167
+ const char *scheduled = qatomic_cmpxchg(&co->scheduled,
168
qemu_co_sleep_ns__scheduled, NULL);
169
170
assert(scheduled == qemu_co_sleep_ns__scheduled);
171
- *sleep_state->user_state_pointer = NULL;
172
- aio_co_wake(sleep_state->co);
173
+ aio_co_wake(co);
174
}
175
}
176
177
static void co_sleep_cb(void *opaque)
178
{
179
- QemuCoSleepState **sleep_state = opaque;
180
- qemu_co_sleep_wake(*sleep_state);
181
+ QemuCoSleep *w = opaque;
182
+ qemu_co_sleep_wake(w);
183
}
184
185
-void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
186
- QemuCoSleepState **sleep_state)
187
+void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
188
+ QEMUClockType type, int64_t ns)
189
{
190
+ Coroutine *co = qemu_coroutine_self();
191
AioContext *ctx = qemu_get_current_aio_context();
192
QEMUTimer ts;
193
- QemuCoSleepState state = {
194
- .co = qemu_coroutine_self(),
195
- .user_state_pointer = sleep_state,
196
- };
197
198
- const char *scheduled = qatomic_cmpxchg(&state.co->scheduled, NULL,
199
- qemu_co_sleep_ns__scheduled);
200
+ const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
201
+ qemu_co_sleep_ns__scheduled);
202
if (scheduled) {
203
fprintf(stderr,
204
"%s: Co-routine was already scheduled in '%s'\n",
205
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
206
abort();
207
}
208
209
- aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, sleep_state);
210
- *sleep_state = &state;
211
+ w->to_wake = co;
212
+ aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w),
213
timer_mod(&ts, qemu_clock_get_ns(type) + ns);
214
qemu_coroutine_yield();
215
timer_del(&ts);
216
217
- /* qemu_co_sleep_wake clears *sleep_state before resuming this coroutine. */
218
- assert(*sleep_state == NULL);
219
+ /* w->to_wake is cleared before resuming this coroutine. */
220
+ assert(w->to_wake == NULL);
221
}
80
--
222
--
81
2.21.0
223
2.31.1
82
224
83
diff view generated by jsdifflib
1
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
1
From: Paolo Bonzini <pbonzini@redhat.com>
2
2
3
Split out cluster_size calculation. Move copy-bitmap creation above
3
Allow using QemuCoSleep to sleep forever until woken by qemu_co_sleep_wake.
4
block-job creation, as we are going to share it with upcoming
4
This makes the logic of qemu_co_sleep_ns_wakeable easy to understand.
5
backup-top filter, which also should be created before actual block job
6
creation.
7
5
8
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
6
In the future we will introduce an API that can work even if the
9
Message-id: 20190429090842.57910-6-vsementsov@virtuozzo.com
7
sleep and wake happen from different threads. For now, initializing
10
[mreitz: Dropped a paragraph from the commit message that was left over
8
w->to_wake after timer_mod is fine because the timer can only fire in
11
from a previous version]
9
the same AioContext.
12
Signed-off-by: Max Reitz <mreitz@redhat.com>
10
11
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
12
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
13
Message-id: 20210517100548.28806-7-pbonzini@redhat.com
14
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
13
---
15
---
14
block/backup.c | 82 ++++++++++++++++++++++++++++++++------------------
16
include/qemu/coroutine.h | 5 +++++
15
1 file changed, 52 insertions(+), 30 deletions(-)
17
util/qemu-coroutine-sleep.c | 26 +++++++++++++++++++-------
18
2 files changed, 24 insertions(+), 7 deletions(-)
16
19
17
diff --git a/block/backup.c b/block/backup.c
20
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
18
index XXXXXXX..XXXXXXX 100644
21
index XXXXXXX..XXXXXXX 100644
19
--- a/block/backup.c
22
--- a/include/qemu/coroutine.h
20
+++ b/block/backup.c
23
+++ b/include/qemu/coroutine.h
21
@@ -XXX,XX +XXX,XX @@ static const BlockJobDriver backup_job_driver = {
24
@@ -XXX,XX +XXX,XX @@ typedef struct QemuCoSleep {
22
.drain = backup_drain,
25
void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
23
};
26
QEMUClockType type, int64_t ns);
24
27
25
+static int64_t backup_calculate_cluster_size(BlockDriverState *target,
28
+/**
26
+ Error **errp)
29
+ * Yield the coroutine until the next call to qemu_co_sleep_wake.
30
+ */
31
+void coroutine_fn qemu_co_sleep(QemuCoSleep *w);
32
+
33
static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
34
{
35
QemuCoSleep w = { 0 };
36
diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c
37
index XXXXXXX..XXXXXXX 100644
38
--- a/util/qemu-coroutine-sleep.c
39
+++ b/util/qemu-coroutine-sleep.c
40
@@ -XXX,XX +XXX,XX @@ static void co_sleep_cb(void *opaque)
41
qemu_co_sleep_wake(w);
42
}
43
44
-void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
45
- QEMUClockType type, int64_t ns)
46
+void coroutine_fn qemu_co_sleep(QemuCoSleep *w)
47
{
48
Coroutine *co = qemu_coroutine_self();
49
- AioContext *ctx = qemu_get_current_aio_context();
50
- QEMUTimer ts;
51
52
const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
53
qemu_co_sleep_ns__scheduled);
54
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
55
}
56
57
w->to_wake = co;
58
- aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w),
59
- timer_mod(&ts, qemu_clock_get_ns(type) + ns);
60
qemu_coroutine_yield();
61
- timer_del(&ts);
62
63
/* w->to_wake is cleared before resuming this coroutine. */
64
assert(w->to_wake == NULL);
65
}
66
+
67
+void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
68
+ QEMUClockType type, int64_t ns)
27
+{
69
+{
28
+ int ret;
70
+ AioContext *ctx = qemu_get_current_aio_context();
29
+ BlockDriverInfo bdi;
71
+ QEMUTimer ts;
72
+
73
+ aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w);
74
+ timer_mod(&ts, qemu_clock_get_ns(type) + ns);
30
+
75
+
31
+ /*
76
+ /*
32
+ * If there is no backing file on the target, we cannot rely on COW if our
77
+ * The timer will fire in the current AiOContext, so the callback
33
+ * backup cluster size is smaller than the target cluster size. Even for
78
+ * must happen after qemu_co_sleep yields and there is no race
34
+ * targets with a backing file, try to avoid COW if possible.
79
+ * between timer_mod and qemu_co_sleep.
35
+ */
80
+ */
36
+ ret = bdrv_get_info(target, &bdi);
81
+ qemu_co_sleep(w);
37
+ if (ret == -ENOTSUP && !target->backing) {
82
+ timer_del(&ts);
38
+ /* Cluster size is not defined */
39
+ warn_report("The target block device doesn't provide "
40
+ "information about the block size and it doesn't have a "
41
+ "backing file. The default block size of %u bytes is "
42
+ "used. If the actual block size of the target exceeds "
43
+ "this default, the backup may be unusable",
44
+ BACKUP_CLUSTER_SIZE_DEFAULT);
45
+ return BACKUP_CLUSTER_SIZE_DEFAULT;
46
+ } else if (ret < 0 && !target->backing) {
47
+ error_setg_errno(errp, -ret,
48
+ "Couldn't determine the cluster size of the target image, "
49
+ "which has no backing file");
50
+ error_append_hint(errp,
51
+ "Aborting, since this may create an unusable destination image\n");
52
+ return ret;
53
+ } else if (ret < 0 && target->backing) {
54
+ /* Not fatal; just trudge on ahead. */
55
+ return BACKUP_CLUSTER_SIZE_DEFAULT;
56
+ }
57
+
58
+ return MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
59
+}
83
+}
60
+
61
BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
62
BlockDriverState *target, int64_t speed,
63
MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap,
64
@@ -XXX,XX +XXX,XX @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
65
JobTxn *txn, Error **errp)
66
{
67
int64_t len;
68
- BlockDriverInfo bdi;
69
BackupBlockJob *job = NULL;
70
int ret;
71
+ int64_t cluster_size;
72
+ HBitmap *copy_bitmap = NULL;
73
74
assert(bs);
75
assert(target);
76
@@ -XXX,XX +XXX,XX @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
77
goto error;
78
}
79
80
+ cluster_size = backup_calculate_cluster_size(target, errp);
81
+ if (cluster_size < 0) {
82
+ goto error;
83
+ }
84
+
85
+ copy_bitmap = hbitmap_alloc(len, ctz32(cluster_size));
86
+
87
/* job->len is fixed, so we can't allow resize */
88
job = block_job_create(job_id, &backup_job_driver, txn, bs,
89
BLK_PERM_CONSISTENT_READ,
90
@@ -XXX,XX +XXX,XX @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
91
92
/* Detect image-fleecing (and similar) schemes */
93
job->serialize_target_writes = bdrv_chain_contains(target, bs);
94
-
95
- /* If there is no backing file on the target, we cannot rely on COW if our
96
- * backup cluster size is smaller than the target cluster size. Even for
97
- * targets with a backing file, try to avoid COW if possible. */
98
- ret = bdrv_get_info(target, &bdi);
99
- if (ret == -ENOTSUP && !target->backing) {
100
- /* Cluster size is not defined */
101
- warn_report("The target block device doesn't provide "
102
- "information about the block size and it doesn't have a "
103
- "backing file. The default block size of %u bytes is "
104
- "used. If the actual block size of the target exceeds "
105
- "this default, the backup may be unusable",
106
- BACKUP_CLUSTER_SIZE_DEFAULT);
107
- job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
108
- } else if (ret < 0 && !target->backing) {
109
- error_setg_errno(errp, -ret,
110
- "Couldn't determine the cluster size of the target image, "
111
- "which has no backing file");
112
- error_append_hint(errp,
113
- "Aborting, since this may create an unusable destination image\n");
114
- goto error;
115
- } else if (ret < 0 && target->backing) {
116
- /* Not fatal; just trudge on ahead. */
117
- job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
118
- } else {
119
- job->cluster_size = MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
120
- }
121
-
122
- job->copy_bitmap = hbitmap_alloc(len, ctz32(job->cluster_size));
123
+ job->cluster_size = cluster_size;
124
+ job->copy_bitmap = copy_bitmap;
125
+ copy_bitmap = NULL;
126
job->use_copy_range = true;
127
job->copy_range_size = MIN_NON_ZERO(blk_get_max_transfer(job->common.blk),
128
blk_get_max_transfer(job->target));
129
@@ -XXX,XX +XXX,XX @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
130
return &job->common;
131
132
error:
133
+ if (copy_bitmap) {
134
+ assert(!job || !job->copy_bitmap);
135
+ hbitmap_free(copy_bitmap);
136
+ }
137
if (sync_bitmap) {
138
bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
139
}
140
--
84
--
141
2.21.0
85
2.31.1
142
86
143
diff view generated by jsdifflib
Deleted patch
1
From: Alberto Garcia <berto@igalia.com>
2
1
3
bdrv_unref_child() does the following things:
4
5
- Updates the child->bs->inherits_from pointer.
6
- Calls bdrv_detach_child() to remove the BdrvChild from bs->children.
7
- Calls bdrv_unref() to unref the child BlockDriverState.
8
9
When bdrv_unref_child() was introduced in commit 33a604075c it was not
10
used in bdrv_close() because the drivers that had additional children
11
(like quorum or blkverify) had already called bdrv_unref() on their
12
children during their own close functions.
13
14
This was changed later (in 0bd6e91a7e for quorum, in 3e586be0b2 for
15
blkverify) so there's no reason not to use bdrv_unref_child() in
16
bdrv_close() anymore.
17
18
After this there's also no need to remove bs->backing and bs->file
19
separately from the rest of the children, so bdrv_close() can be
20
simplified.
21
22
Now bdrv_close() unrefs all children (before this patch it was only
23
bs->file and bs->backing). As a result, none of the callers of
24
brvd_attach_child() should remove their reference to child_bs (because
25
this function effectively steals that reference). This patch updates a
26
couple of tests that were doing their own bdrv_unref().
27
28
Signed-off-by: Alberto Garcia <berto@igalia.com>
29
Message-id: 6d1d5feaa53aa1ab127adb73d605dc4503e3abd5.1557754872.git.berto@igalia.com
30
[mreitz: s/where/were/]
31
Signed-off-by: Max Reitz <mreitz@redhat.com>
32
---
33
block.c | 16 +++-------------
34
tests/test-bdrv-drain.c | 6 ------
35
tests/test-bdrv-graph-mod.c | 1 -
36
3 files changed, 3 insertions(+), 20 deletions(-)
37
38
diff --git a/block.c b/block.c
39
index XXXXXXX..XXXXXXX 100644
40
--- a/block.c
41
+++ b/block.c
42
@@ -XXX,XX +XXX,XX @@ static void bdrv_close(BlockDriverState *bs)
43
bs->drv = NULL;
44
}
45
46
- bdrv_set_backing_hd(bs, NULL, &error_abort);
47
-
48
- if (bs->file != NULL) {
49
- bdrv_unref_child(bs, bs->file);
50
- bs->file = NULL;
51
- }
52
-
53
QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
54
- /* TODO Remove bdrv_unref() from drivers' close function and use
55
- * bdrv_unref_child() here */
56
- if (child->bs->inherits_from == bs) {
57
- child->bs->inherits_from = NULL;
58
- }
59
- bdrv_detach_child(child);
60
+ bdrv_unref_child(bs, child);
61
}
62
63
+ bs->backing = NULL;
64
+ bs->file = NULL;
65
g_free(bs->opaque);
66
bs->opaque = NULL;
67
atomic_set(&bs->copy_on_read, 0);
68
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
69
index XXXXXXX..XXXXXXX 100644
70
--- a/tests/test-bdrv-drain.c
71
+++ b/tests/test-bdrv-drain.c
72
@@ -XXX,XX +XXX,XX @@ static void test_detach_indirect(bool by_parent_cb)
73
bdrv_unref(parent_b);
74
blk_unref(blk);
75
76
- /* XXX Once bdrv_close() unref's children instead of just detaching them,
77
- * this won't be necessary any more. */
78
- bdrv_unref(a);
79
- bdrv_unref(a);
80
- bdrv_unref(c);
81
-
82
g_assert_cmpint(a->refcnt, ==, 1);
83
g_assert_cmpint(b->refcnt, ==, 1);
84
g_assert_cmpint(c->refcnt, ==, 1);
85
diff --git a/tests/test-bdrv-graph-mod.c b/tests/test-bdrv-graph-mod.c
86
index XXXXXXX..XXXXXXX 100644
87
--- a/tests/test-bdrv-graph-mod.c
88
+++ b/tests/test-bdrv-graph-mod.c
89
@@ -XXX,XX +XXX,XX @@ static void test_update_perm_tree(void)
90
g_assert_nonnull(local_err);
91
error_free(local_err);
92
93
- bdrv_unref(bs);
94
blk_unref(root);
95
}
96
97
--
98
2.21.0
99
100
diff view generated by jsdifflib
Deleted patch
1
From: Sam Eiderman <shmuel.eiderman@oracle.com>
2
1
3
In safe mode we open the entire chain, including the parent backing
4
file of the rebased file.
5
Do not open a new BlockBackend for the parent backing file, which
6
saves opening the rest of the chain twice, which for long chains
7
saves many "pricy" bdrv_open() calls.
8
9
Permissions for blk_new() were copied from blk_new_open() when
10
flags = 0.
11
12
Reviewed-by: Karl Heubaum <karl.heubaum@oracle.com>
13
Reviewed-by: Eyal Moscovici <eyal.moscovici@oracle.com>
14
Signed-off-by: Sagi Amit <sagi.amit@oracle.com>
15
Co-developed-by: Sagi Amit <sagi.amit@oracle.com>
16
Signed-off-by: Sam Eiderman <shmuel.eiderman@oracle.com>
17
Message-id: 20190523163337.4497-2-shmuel.eiderman@oracle.com
18
Signed-off-by: Max Reitz <mreitz@redhat.com>
19
---
20
qemu-img.c | 29 +++++++++--------------------
21
1 file changed, 9 insertions(+), 20 deletions(-)
22
23
diff --git a/qemu-img.c b/qemu-img.c
24
index XXXXXXX..XXXXXXX 100644
25
--- a/qemu-img.c
26
+++ b/qemu-img.c
27
@@ -XXX,XX +XXX,XX @@ static int img_rebase(int argc, char **argv)
28
29
/* For safe rebasing we need to compare old and new backing file */
30
if (!unsafe) {
31
- char backing_name[PATH_MAX];
32
QDict *options = NULL;
33
+ BlockDriverState *base_bs = backing_bs(bs);
34
35
- if (bs->backing) {
36
- if (bs->backing_format[0] != '\0') {
37
- options = qdict_new();
38
- qdict_put_str(options, "driver", bs->backing_format);
39
- }
40
-
41
- if (force_share) {
42
- if (!options) {
43
- options = qdict_new();
44
- }
45
- qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
46
- }
47
- bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
48
- blk_old_backing = blk_new_open(backing_name, NULL,
49
- options, src_flags, &local_err);
50
- if (!blk_old_backing) {
51
+ if (base_bs) {
52
+ blk_old_backing = blk_new(BLK_PERM_CONSISTENT_READ,
53
+ BLK_PERM_ALL);
54
+ ret = blk_insert_bs(blk_old_backing, base_bs,
55
+ &local_err);
56
+ if (ret < 0) {
57
error_reportf_err(local_err,
58
- "Could not open old backing file '%s': ",
59
- backing_name);
60
- ret = -1;
61
+ "Could not reuse old backing file '%s': ",
62
+ base_bs->filename);
63
goto out;
64
}
65
} else {
66
--
67
2.21.0
68
69
diff view generated by jsdifflib
Deleted patch
1
From: Sam Eiderman <shmuel.eiderman@oracle.com>
2
1
3
If a chain was detected, don't open a new BlockBackend from the target
4
backing file which will create a new BlockDriverState. Instead, create
5
an empty BlockBackend and attach the already open BlockDriverState.
6
7
Permissions for blk_new() were copied from blk_new_open() when
8
flags = 0.
9
10
Reviewed-by: Karl Heubaum <karl.heubaum@oracle.com>
11
Reviewed-by: Eyal Moscovici <eyal.moscovici@oracle.com>
12
Signed-off-by: Sagi Amit <sagi.amit@oracle.com>
13
Co-developed-by: Sagi Amit <sagi.amit@oracle.com>
14
Signed-off-by: Sam Eiderman <shmuel.eiderman@oracle.com>
15
Message-id: 20190523163337.4497-4-shmuel.eiderman@oracle.com
16
Signed-off-by: Max Reitz <mreitz@redhat.com>
17
---
18
qemu-img.c | 33 +++++++++++++++++++++++----------
19
1 file changed, 23 insertions(+), 10 deletions(-)
20
21
diff --git a/qemu-img.c b/qemu-img.c
22
index XXXXXXX..XXXXXXX 100644
23
--- a/qemu-img.c
24
+++ b/qemu-img.c
25
@@ -XXX,XX +XXX,XX @@ static int img_rebase(int argc, char **argv)
26
* in its chain.
27
*/
28
prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path);
29
-
30
- blk_new_backing = blk_new_open(out_real_path, NULL,
31
- options, src_flags, &local_err);
32
- g_free(out_real_path);
33
- if (!blk_new_backing) {
34
- error_reportf_err(local_err,
35
- "Could not open new backing file '%s': ",
36
- out_baseimg);
37
- ret = -1;
38
- goto out;
39
+ if (prefix_chain_bs) {
40
+ g_free(out_real_path);
41
+ blk_new_backing = blk_new(BLK_PERM_CONSISTENT_READ,
42
+ BLK_PERM_ALL);
43
+ ret = blk_insert_bs(blk_new_backing, prefix_chain_bs,
44
+ &local_err);
45
+ if (ret < 0) {
46
+ error_reportf_err(local_err,
47
+ "Could not reuse backing file '%s': ",
48
+ out_baseimg);
49
+ goto out;
50
+ }
51
+ } else {
52
+ blk_new_backing = blk_new_open(out_real_path, NULL,
53
+ options, src_flags, &local_err);
54
+ g_free(out_real_path);
55
+ if (!blk_new_backing) {
56
+ error_reportf_err(local_err,
57
+ "Could not open new backing file '%s': ",
58
+ out_baseimg);
59
+ ret = -1;
60
+ goto out;
61
+ }
62
}
63
}
64
}
65
--
66
2.21.0
67
68
diff view generated by jsdifflib
Deleted patch
1
From: Anton Nefedov <anton.nefedov@virtuozzo.com>
2
1
3
If COW areas of the newly allocated clusters are zeroes on the backing
4
image, efficient bdrv_write_zeroes(flags=BDRV_REQ_NO_FALLBACK) can be
5
used on the whole cluster instead of writing explicit zero buffers later
6
in perform_cow().
7
8
iotest 060:
9
write to the discarded cluster does not trigger COW anymore.
10
Use a backing image instead.
11
12
Signed-off-by: Anton Nefedov <anton.nefedov@virtuozzo.com>
13
Message-id: 20190516142749.81019-2-anton.nefedov@virtuozzo.com
14
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
15
Reviewed-by: Alberto Garcia <berto@igalia.com>
16
Signed-off-by: Max Reitz <mreitz@redhat.com>
17
---
18
qapi/block-core.json | 4 +-
19
block/qcow2.h | 6 +++
20
block/qcow2-cluster.c | 2 +-
21
block/qcow2.c | 85 ++++++++++++++++++++++++++++++++++++++
22
block/trace-events | 1 +
23
tests/qemu-iotests/060 | 7 +++-
24
tests/qemu-iotests/060.out | 5 ++-
25
7 files changed, 106 insertions(+), 4 deletions(-)
26
27
diff --git a/qapi/block-core.json b/qapi/block-core.json
28
index XXXXXXX..XXXXXXX 100644
29
--- a/qapi/block-core.json
30
+++ b/qapi/block-core.json
31
@@ -XXX,XX +XXX,XX @@
32
#
33
# @cor_write: a write due to copy-on-read (since 2.11)
34
#
35
+# @cluster_alloc_space: an allocation of file space for a cluster (since 4.1)
36
+#
37
# Since: 2.9
38
##
39
{ 'enum': 'BlkdebugEvent', 'prefix': 'BLKDBG',
40
@@ -XXX,XX +XXX,XX @@
41
'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev',
42
'pwritev_zero', 'pwritev_done', 'empty_image_prepare',
43
'l1_shrink_write_table', 'l1_shrink_free_l2_clusters',
44
- 'cor_write'] }
45
+ 'cor_write', 'cluster_alloc_space'] }
46
47
##
48
# @BlkdebugInjectErrorOptions:
49
diff --git a/block/qcow2.h b/block/qcow2.h
50
index XXXXXXX..XXXXXXX 100644
51
--- a/block/qcow2.h
52
+++ b/block/qcow2.h
53
@@ -XXX,XX +XXX,XX @@ typedef struct QCowL2Meta
54
*/
55
Qcow2COWRegion cow_end;
56
57
+ /*
58
+ * Indicates that COW regions are already handled and do not require
59
+ * any more processing.
60
+ */
61
+ bool skip_cow;
62
+
63
/**
64
* The I/O vector with the data from the actual guest write request.
65
* If non-NULL, this is meant to be merged together with the data
66
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
67
index XXXXXXX..XXXXXXX 100644
68
--- a/block/qcow2-cluster.c
69
+++ b/block/qcow2-cluster.c
70
@@ -XXX,XX +XXX,XX @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)
71
assert(start->offset + start->nb_bytes <= end->offset);
72
assert(!m->data_qiov || m->data_qiov->size == data_bytes);
73
74
- if (start->nb_bytes == 0 && end->nb_bytes == 0) {
75
+ if ((start->nb_bytes == 0 && end->nb_bytes == 0) || m->skip_cow) {
76
return 0;
77
}
78
79
diff --git a/block/qcow2.c b/block/qcow2.c
80
index XXXXXXX..XXXXXXX 100644
81
--- a/block/qcow2.c
82
+++ b/block/qcow2.c
83
@@ -XXX,XX +XXX,XX @@ static bool merge_cow(uint64_t offset, unsigned bytes,
84
continue;
85
}
86
87
+ /* If COW regions are handled already, skip this too */
88
+ if (m->skip_cow) {
89
+ continue;
90
+ }
91
+
92
/* The data (middle) region must be immediately after the
93
* start region */
94
if (l2meta_cow_start(m) + m->cow_start.nb_bytes != offset) {
95
@@ -XXX,XX +XXX,XX @@ static bool merge_cow(uint64_t offset, unsigned bytes,
96
return false;
97
}
98
99
+static bool is_unallocated(BlockDriverState *bs, int64_t offset, int64_t bytes)
100
+{
101
+ int64_t nr;
102
+ return !bytes ||
103
+ (!bdrv_is_allocated_above(bs, NULL, offset, bytes, &nr) && nr == bytes);
104
+}
105
+
106
+static bool is_zero_cow(BlockDriverState *bs, QCowL2Meta *m)
107
+{
108
+ /*
109
+ * This check is designed for optimization shortcut so it must be
110
+ * efficient.
111
+ * Instead of is_zero(), use is_unallocated() as it is faster (but not
112
+ * as accurate and can result in false negatives).
113
+ */
114
+ return is_unallocated(bs, m->offset + m->cow_start.offset,
115
+ m->cow_start.nb_bytes) &&
116
+ is_unallocated(bs, m->offset + m->cow_end.offset,
117
+ m->cow_end.nb_bytes);
118
+}
119
+
120
+static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta)
121
+{
122
+ BDRVQcow2State *s = bs->opaque;
123
+ QCowL2Meta *m;
124
+
125
+ if (!(s->data_file->bs->supported_zero_flags & BDRV_REQ_NO_FALLBACK)) {
126
+ return 0;
127
+ }
128
+
129
+ if (bs->encrypted) {
130
+ return 0;
131
+ }
132
+
133
+ for (m = l2meta; m != NULL; m = m->next) {
134
+ int ret;
135
+
136
+ if (!m->cow_start.nb_bytes && !m->cow_end.nb_bytes) {
137
+ continue;
138
+ }
139
+
140
+ if (!is_zero_cow(bs, m)) {
141
+ continue;
142
+ }
143
+
144
+ /*
145
+ * instead of writing zero COW buffers,
146
+ * efficiently zero out the whole clusters
147
+ */
148
+
149
+ ret = qcow2_pre_write_overlap_check(bs, 0, m->alloc_offset,
150
+ m->nb_clusters * s->cluster_size,
151
+ true);
152
+ if (ret < 0) {
153
+ return ret;
154
+ }
155
+
156
+ BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_SPACE);
157
+ ret = bdrv_co_pwrite_zeroes(s->data_file, m->alloc_offset,
158
+ m->nb_clusters * s->cluster_size,
159
+ BDRV_REQ_NO_FALLBACK);
160
+ if (ret < 0) {
161
+ if (ret != -ENOTSUP && ret != -EAGAIN) {
162
+ return ret;
163
+ }
164
+ continue;
165
+ }
166
+
167
+ trace_qcow2_skip_cow(qemu_coroutine_self(), m->offset, m->nb_clusters);
168
+ m->skip_cow = true;
169
+ }
170
+ return 0;
171
+}
172
+
173
static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
174
uint64_t bytes, QEMUIOVector *qiov,
175
int flags)
176
@@ -XXX,XX +XXX,XX @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
177
qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
178
}
179
180
+ /* Try to efficiently initialize the physical space with zeroes */
181
+ ret = handle_alloc_space(bs, l2meta);
182
+ if (ret < 0) {
183
+ goto out_unlocked;
184
+ }
185
+
186
/* If we need to do COW, check if it's possible to merge the
187
* writing of the guest data together with that of the COW regions.
188
* If it's not possible (or not necessary) then write the
189
diff --git a/block/trace-events b/block/trace-events
190
index XXXXXXX..XXXXXXX 100644
191
--- a/block/trace-events
192
+++ b/block/trace-events
193
@@ -XXX,XX +XXX,XX @@ qcow2_writev_done_part(void *co, int cur_bytes) "co %p cur_bytes %d"
194
qcow2_writev_data(void *co, uint64_t offset) "co %p offset 0x%" PRIx64
195
qcow2_pwrite_zeroes_start_req(void *co, int64_t offset, int count) "co %p offset 0x%" PRIx64 " count %d"
196
qcow2_pwrite_zeroes(void *co, int64_t offset, int count) "co %p offset 0x%" PRIx64 " count %d"
197
+qcow2_skip_cow(void *co, uint64_t offset, int nb_clusters) "co %p offset 0x%" PRIx64 " nb_clusters %d"
198
199
# qcow2-cluster.c
200
qcow2_alloc_clusters_offset(void *co, uint64_t offset, int bytes) "co %p offset 0x%" PRIx64 " bytes %d"
201
diff --git a/tests/qemu-iotests/060 b/tests/qemu-iotests/060
202
index XXXXXXX..XXXXXXX 100755
203
--- a/tests/qemu-iotests/060
204
+++ b/tests/qemu-iotests/060
205
@@ -XXX,XX +XXX,XX @@ $QEMU_IO -c "$OPEN_RO" -c "read -P 1 0 512" | _filter_qemu_io
206
echo
207
echo "=== Testing overlap while COW is in flight ==="
208
echo
209
+BACKING_IMG=$TEST_IMG.base
210
+TEST_IMG=$BACKING_IMG _make_test_img 1G
211
+
212
+$QEMU_IO -c 'write 0k 64k' "$BACKING_IMG" | _filter_qemu_io
213
+
214
# compat=0.10 is required in order to make the following discard actually
215
# unallocate the sector rather than make it a zero sector - we want COW, after
216
# all.
217
-IMGOPTS='compat=0.10' _make_test_img 1G
218
+IMGOPTS='compat=0.10' _make_test_img -b "$BACKING_IMG" 1G
219
# Write two clusters, the second one enforces creation of an L2 table after
220
# the first data cluster.
221
$QEMU_IO -c 'write 0k 64k' -c 'write 512M 64k' "$TEST_IMG" | _filter_qemu_io
222
diff --git a/tests/qemu-iotests/060.out b/tests/qemu-iotests/060.out
223
index XXXXXXX..XXXXXXX 100644
224
--- a/tests/qemu-iotests/060.out
225
+++ b/tests/qemu-iotests/060.out
226
@@ -XXX,XX +XXX,XX @@ read 512/512 bytes at offset 0
227
228
=== Testing overlap while COW is in flight ===
229
230
-Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824
231
+Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=1073741824
232
+wrote 65536/65536 bytes at offset 0
233
+64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
234
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824 backing_file=TEST_DIR/t.IMGFMT.base
235
wrote 65536/65536 bytes at offset 0
236
64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
237
wrote 65536/65536 bytes at offset 536870912
238
--
239
2.21.0
240
241
diff view generated by jsdifflib