]> git.proxmox.com Git - mirror_qemu.git/blame - block/qed.c
qemu-iotests: Reduce racy output in 028
[mirror_qemu.git] / block / qed.c
CommitLineData
75411d23
SH
1/*
2 * QEMU Enhanced Disk Format
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Anthony Liguori <aliguori@us.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
1de7afc9 15#include "qemu/timer.h"
eabba580 16#include "trace.h"
75411d23 17#include "qed.h"
7b1b5d19 18#include "qapi/qmp/qerror.h"
caf71f86 19#include "migration/migration.h"
75411d23 20
d7331bed 21static const AIOCBInfo qed_aiocb_info = {
eabba580 22 .aiocb_size = sizeof(QEDAIOCB),
eabba580
SH
23};
24
75411d23
SH
25static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
26 const char *filename)
27{
28 const QEDHeader *header = (const QEDHeader *)buf;
29
30 if (buf_size < sizeof(*header)) {
31 return 0;
32 }
33 if (le32_to_cpu(header->magic) != QED_MAGIC) {
34 return 0;
35 }
36 return 100;
37}
38
39/**
40 * Check whether an image format is raw
41 *
42 * @fmt: Backing file format, may be NULL
43 */
44static bool qed_fmt_is_raw(const char *fmt)
45{
46 return fmt && strcmp(fmt, "raw") == 0;
47}
48
49static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
50{
51 cpu->magic = le32_to_cpu(le->magic);
52 cpu->cluster_size = le32_to_cpu(le->cluster_size);
53 cpu->table_size = le32_to_cpu(le->table_size);
54 cpu->header_size = le32_to_cpu(le->header_size);
55 cpu->features = le64_to_cpu(le->features);
56 cpu->compat_features = le64_to_cpu(le->compat_features);
57 cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
58 cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
59 cpu->image_size = le64_to_cpu(le->image_size);
60 cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
61 cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
62}
63
64static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
65{
66 le->magic = cpu_to_le32(cpu->magic);
67 le->cluster_size = cpu_to_le32(cpu->cluster_size);
68 le->table_size = cpu_to_le32(cpu->table_size);
69 le->header_size = cpu_to_le32(cpu->header_size);
70 le->features = cpu_to_le64(cpu->features);
71 le->compat_features = cpu_to_le64(cpu->compat_features);
72 le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
73 le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
74 le->image_size = cpu_to_le64(cpu->image_size);
75 le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
76 le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
77}
78
b10170ac 79int qed_write_header_sync(BDRVQEDState *s)
75411d23
SH
80{
81 QEDHeader le;
82 int ret;
83
84 qed_header_cpu_to_le(&s->header, &le);
9a4f4c31 85 ret = bdrv_pwrite(s->bs->file->bs, 0, &le, sizeof(le));
75411d23
SH
86 if (ret != sizeof(le)) {
87 return ret;
88 }
89 return 0;
90}
91
01979a98
SH
92typedef struct {
93 GenericCB gencb;
94 BDRVQEDState *s;
95 struct iovec iov;
96 QEMUIOVector qiov;
97 int nsectors;
98 uint8_t *buf;
99} QEDWriteHeaderCB;
100
101static void qed_write_header_cb(void *opaque, int ret)
102{
103 QEDWriteHeaderCB *write_header_cb = opaque;
104
105 qemu_vfree(write_header_cb->buf);
106 gencb_complete(write_header_cb, ret);
107}
108
109static void qed_write_header_read_cb(void *opaque, int ret)
110{
111 QEDWriteHeaderCB *write_header_cb = opaque;
112 BDRVQEDState *s = write_header_cb->s;
01979a98
SH
113
114 if (ret) {
115 qed_write_header_cb(write_header_cb, ret);
116 return;
117 }
118
119 /* Update header */
120 qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf);
121
9a4f4c31 122 bdrv_aio_writev(s->bs->file->bs, 0, &write_header_cb->qiov,
ad54ae80
PB
123 write_header_cb->nsectors, qed_write_header_cb,
124 write_header_cb);
01979a98
SH
125}
126
127/**
128 * Update header in-place (does not rewrite backing filename or other strings)
129 *
130 * This function only updates known header fields in-place and does not affect
131 * extra data after the QED header.
132 */
097310b5 133static void qed_write_header(BDRVQEDState *s, BlockCompletionFunc cb,
01979a98
SH
134 void *opaque)
135{
136 /* We must write full sectors for O_DIRECT but cannot necessarily generate
137 * the data following the header if an unrecognized compat feature is
138 * active. Therefore, first read the sectors containing the header, update
139 * them, and write back.
140 */
141
01979a98
SH
142 int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) /
143 BDRV_SECTOR_SIZE;
144 size_t len = nsectors * BDRV_SECTOR_SIZE;
145 QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb),
146 cb, opaque);
147
148 write_header_cb->s = s;
149 write_header_cb->nsectors = nsectors;
150 write_header_cb->buf = qemu_blockalign(s->bs, len);
151 write_header_cb->iov.iov_base = write_header_cb->buf;
152 write_header_cb->iov.iov_len = len;
153 qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1);
154
9a4f4c31 155 bdrv_aio_readv(s->bs->file->bs, 0, &write_header_cb->qiov, nsectors,
ad54ae80 156 qed_write_header_read_cb, write_header_cb);
01979a98
SH
157}
158
75411d23
SH
159static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
160{
161 uint64_t table_entries;
162 uint64_t l2_size;
163
164 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
165 l2_size = table_entries * cluster_size;
166
167 return l2_size * table_entries;
168}
169
170static bool qed_is_cluster_size_valid(uint32_t cluster_size)
171{
172 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
173 cluster_size > QED_MAX_CLUSTER_SIZE) {
174 return false;
175 }
176 if (cluster_size & (cluster_size - 1)) {
177 return false; /* not power of 2 */
178 }
179 return true;
180}
181
182static bool qed_is_table_size_valid(uint32_t table_size)
183{
184 if (table_size < QED_MIN_TABLE_SIZE ||
185 table_size > QED_MAX_TABLE_SIZE) {
186 return false;
187 }
188 if (table_size & (table_size - 1)) {
189 return false; /* not power of 2 */
190 }
191 return true;
192}
193
194static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
195 uint32_t table_size)
196{
197 if (image_size % BDRV_SECTOR_SIZE != 0) {
198 return false; /* not multiple of sector size */
199 }
200 if (image_size > qed_max_image_size(cluster_size, table_size)) {
201 return false; /* image is too large */
202 }
203 return true;
204}
205
206/**
207 * Read a string of known length from the image file
208 *
209 * @file: Image file
210 * @offset: File offset to start of string, in bytes
211 * @n: String length in bytes
212 * @buf: Destination buffer
213 * @buflen: Destination buffer length in bytes
214 * @ret: 0 on success, -errno on failure
215 *
216 * The string is NUL-terminated.
217 */
218static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n,
219 char *buf, size_t buflen)
220{
221 int ret;
222 if (n >= buflen) {
223 return -EINVAL;
224 }
225 ret = bdrv_pread(file, offset, buf, n);
226 if (ret < 0) {
227 return ret;
228 }
229 buf[n] = '\0';
230 return 0;
231}
232
eabba580
SH
233/**
234 * Allocate new clusters
235 *
236 * @s: QED state
237 * @n: Number of contiguous clusters to allocate
238 * @ret: Offset of first allocated cluster
239 *
240 * This function only produces the offset where the new clusters should be
241 * written. It updates BDRVQEDState but does not make any changes to the image
242 * file.
243 */
244static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
245{
246 uint64_t offset = s->file_size;
247 s->file_size += n * s->header.cluster_size;
248 return offset;
249}
250
298800ca
SH
251QEDTable *qed_alloc_table(BDRVQEDState *s)
252{
253 /* Honor O_DIRECT memory alignment requirements */
254 return qemu_blockalign(s->bs,
255 s->header.cluster_size * s->header.table_size);
256}
257
eabba580
SH
258/**
259 * Allocate a new zeroed L2 table
260 */
261static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
262{
263 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
264
265 l2_table->table = qed_alloc_table(s);
266 l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
267
268 memset(l2_table->table->offsets, 0,
269 s->header.cluster_size * s->header.table_size);
270 return l2_table;
271}
272
273static void qed_aio_next_io(void *opaque, int ret);
274
6f321e93
SH
275static void qed_plug_allocating_write_reqs(BDRVQEDState *s)
276{
277 assert(!s->allocating_write_reqs_plugged);
278
279 s->allocating_write_reqs_plugged = true;
280}
281
282static void qed_unplug_allocating_write_reqs(BDRVQEDState *s)
283{
284 QEDAIOCB *acb;
285
286 assert(s->allocating_write_reqs_plugged);
287
288 s->allocating_write_reqs_plugged = false;
289
290 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
291 if (acb) {
292 qed_aio_next_io(acb, 0);
293 }
294}
295
296static void qed_finish_clear_need_check(void *opaque, int ret)
297{
298 /* Do nothing */
299}
300
301static void qed_flush_after_clear_need_check(void *opaque, int ret)
302{
303 BDRVQEDState *s = opaque;
304
305 bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s);
306
307 /* No need to wait until flush completes */
308 qed_unplug_allocating_write_reqs(s);
309}
310
311static void qed_clear_need_check(void *opaque, int ret)
312{
313 BDRVQEDState *s = opaque;
314
315 if (ret) {
316 qed_unplug_allocating_write_reqs(s);
317 return;
318 }
319
320 s->header.features &= ~QED_F_NEED_CHECK;
321 qed_write_header(s, qed_flush_after_clear_need_check, s);
322}
323
324static void qed_need_check_timer_cb(void *opaque)
325{
326 BDRVQEDState *s = opaque;
327
328 /* The timer should only fire when allocating writes have drained */
329 assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs));
330
331 trace_qed_need_check_timer_cb(s);
332
333 qed_plug_allocating_write_reqs(s);
334
335 /* Ensure writes are on disk before clearing flag */
336 bdrv_aio_flush(s->bs, qed_clear_need_check, s);
337}
338
339static void qed_start_need_check_timer(BDRVQEDState *s)
340{
341 trace_qed_start_need_check_timer(s);
342
bc72ad67 343 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
6f321e93
SH
344 * migration.
345 */
bc72ad67 346 timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
6f321e93
SH
347 get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT);
348}
349
350/* It's okay to call this multiple times or when no timer is started */
351static void qed_cancel_need_check_timer(BDRVQEDState *s)
352{
353 trace_qed_cancel_need_check_timer(s);
bc72ad67 354 timer_del(s->need_check_timer);
6f321e93
SH
355}
356
a8c868c3
SH
357static void bdrv_qed_detach_aio_context(BlockDriverState *bs)
358{
359 BDRVQEDState *s = bs->opaque;
360
361 qed_cancel_need_check_timer(s);
362 timer_free(s->need_check_timer);
363}
364
365static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
366 AioContext *new_context)
367{
368 BDRVQEDState *s = bs->opaque;
369
370 s->need_check_timer = aio_timer_new(new_context,
371 QEMU_CLOCK_VIRTUAL, SCALE_NS,
372 qed_need_check_timer_cb, s);
373 if (s->header.features & QED_F_NEED_CHECK) {
374 qed_start_need_check_timer(s);
375 }
376}
377
df9a681d
FZ
378static void bdrv_qed_drain(BlockDriverState *bs)
379{
380 BDRVQEDState *s = bs->opaque;
381
382 /* Cancel timer and start doing I/O that were meant to happen as if it
383 * fired, that way we get bdrv_drain() taking care of the ongoing requests
384 * correctly. */
385 qed_cancel_need_check_timer(s);
386 qed_plug_allocating_write_reqs(s);
387 bdrv_aio_flush(s->bs, qed_clear_need_check, s);
388}
389
015a1036
HR
390static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
391 Error **errp)
75411d23
SH
392{
393 BDRVQEDState *s = bs->opaque;
394 QEDHeader le_header;
395 int64_t file_size;
396 int ret;
397
398 s->bs = bs;
eabba580 399 QSIMPLEQ_INIT(&s->allocating_write_reqs);
75411d23 400
9a4f4c31 401 ret = bdrv_pread(bs->file->bs, 0, &le_header, sizeof(le_header));
75411d23
SH
402 if (ret < 0) {
403 return ret;
404 }
75411d23
SH
405 qed_header_le_to_cpu(&le_header, &s->header);
406
407 if (s->header.magic != QED_MAGIC) {
76abe407
PB
408 error_setg(errp, "Image not in QED format");
409 return -EINVAL;
75411d23
SH
410 }
411 if (s->header.features & ~QED_FEATURE_MASK) {
10b758e8
KW
412 /* image uses unsupported feature bits */
413 char buf[64];
414 snprintf(buf, sizeof(buf), "%" PRIx64,
415 s->header.features & ~QED_FEATURE_MASK);
c6bd8c70
MA
416 error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
417 bdrv_get_device_or_node_name(bs), "QED", buf);
10b758e8 418 return -ENOTSUP;
75411d23
SH
419 }
420 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
421 return -EINVAL;
422 }
423
424 /* Round down file size to the last cluster */
9a4f4c31 425 file_size = bdrv_getlength(bs->file->bs);
75411d23
SH
426 if (file_size < 0) {
427 return file_size;
428 }
429 s->file_size = qed_start_of_cluster(s, file_size);
430
431 if (!qed_is_table_size_valid(s->header.table_size)) {
432 return -EINVAL;
433 }
434 if (!qed_is_image_size_valid(s->header.image_size,
435 s->header.cluster_size,
436 s->header.table_size)) {
437 return -EINVAL;
438 }
439 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
440 return -EINVAL;
441 }
442
443 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
444 sizeof(uint64_t);
786a4ea8 445 s->l2_shift = ctz32(s->header.cluster_size);
75411d23 446 s->l2_mask = s->table_nelems - 1;
786a4ea8 447 s->l1_shift = s->l2_shift + ctz32(s->table_nelems);
75411d23 448
0adfa1ed
SH
449 /* Header size calculation must not overflow uint32_t */
450 if (s->header.header_size > UINT32_MAX / s->header.cluster_size) {
451 return -EINVAL;
452 }
453
75411d23
SH
454 if ((s->header.features & QED_F_BACKING_FILE)) {
455 if ((uint64_t)s->header.backing_filename_offset +
456 s->header.backing_filename_size >
457 s->header.cluster_size * s->header.header_size) {
458 return -EINVAL;
459 }
460
9a4f4c31 461 ret = qed_read_string(bs->file->bs, s->header.backing_filename_offset,
75411d23
SH
462 s->header.backing_filename_size, bs->backing_file,
463 sizeof(bs->backing_file));
464 if (ret < 0) {
465 return ret;
466 }
467
468 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
469 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
470 }
471 }
472
473 /* Reset unknown autoclear feature bits. This is a backwards
474 * compatibility mechanism that allows images to be opened by older
475 * programs, which "knock out" unknown feature bits. When an image is
476 * opened by a newer program again it can detect that the autoclear
477 * feature is no longer valid.
478 */
479 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
9a4f4c31 480 !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INCOMING)) {
75411d23
SH
481 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
482
483 ret = qed_write_header_sync(s);
484 if (ret) {
485 return ret;
486 }
487
488 /* From here on only known autoclear feature bits are valid */
9a4f4c31 489 bdrv_flush(bs->file->bs);
75411d23
SH
490 }
491
298800ca
SH
492 s->l1_table = qed_alloc_table(s);
493 qed_init_l2_cache(&s->l2_cache);
494
495 ret = qed_read_l1_table_sync(s);
01979a98
SH
496 if (ret) {
497 goto out;
498 }
499
500 /* If image was not closed cleanly, check consistency */
058f8f16 501 if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
01979a98
SH
502 /* Read-only images cannot be fixed. There is no risk of corruption
503 * since write operations are not possible. Therefore, allow
504 * potentially inconsistent images to be opened read-only. This can
505 * aid data recovery from an otherwise inconsistent image.
506 */
9a4f4c31 507 if (!bdrv_is_read_only(bs->file->bs) &&
2d1f3c23 508 !(flags & BDRV_O_INCOMING)) {
01979a98
SH
509 BdrvCheckResult result = {0};
510
511 ret = qed_check(s, &result, true);
6f321e93
SH
512 if (ret) {
513 goto out;
514 }
01979a98
SH
515 }
516 }
517
a8c868c3 518 bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
6f321e93 519
01979a98 520out:
298800ca
SH
521 if (ret) {
522 qed_free_l2_cache(&s->l2_cache);
523 qemu_vfree(s->l1_table);
524 }
75411d23
SH
525 return ret;
526}
527
3baca891 528static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
d34682cd
KW
529{
530 BDRVQEDState *s = bs->opaque;
531
532 bs->bl.write_zeroes_alignment = s->header.cluster_size >> BDRV_SECTOR_BITS;
d34682cd
KW
533}
534
f9cb20f1
JC
535/* We have nothing to do for QED reopen, stubs just return
536 * success */
537static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
538 BlockReopenQueue *queue, Error **errp)
539{
540 return 0;
541}
542
75411d23
SH
543static void bdrv_qed_close(BlockDriverState *bs)
544{
298800ca
SH
545 BDRVQEDState *s = bs->opaque;
546
a8c868c3 547 bdrv_qed_detach_aio_context(bs);
6f321e93 548
01979a98 549 /* Ensure writes reach stable storage */
9a4f4c31 550 bdrv_flush(bs->file->bs);
01979a98
SH
551
552 /* Clean shutdown, no check required on next open */
553 if (s->header.features & QED_F_NEED_CHECK) {
554 s->header.features &= ~QED_F_NEED_CHECK;
555 qed_write_header_sync(s);
556 }
557
298800ca
SH
558 qed_free_l2_cache(&s->l2_cache);
559 qemu_vfree(s->l1_table);
75411d23
SH
560}
561
75411d23
SH
562static int qed_create(const char *filename, uint32_t cluster_size,
563 uint64_t image_size, uint32_t table_size,
0fea6b79 564 const char *backing_file, const char *backing_fmt,
4ab15590 565 QemuOpts *opts, Error **errp)
75411d23
SH
566{
567 QEDHeader header = {
568 .magic = QED_MAGIC,
569 .cluster_size = cluster_size,
570 .table_size = table_size,
571 .header_size = 1,
572 .features = 0,
573 .compat_features = 0,
574 .l1_table_offset = cluster_size,
575 .image_size = image_size,
576 };
577 QEDHeader le_header;
578 uint8_t *l1_table = NULL;
579 size_t l1_size = header.cluster_size * header.table_size;
34b5d2c6 580 Error *local_err = NULL;
75411d23 581 int ret = 0;
2e40134b 582 BlockDriverState *bs;
75411d23 583
4ab15590 584 ret = bdrv_create_file(filename, opts, &local_err);
75411d23 585 if (ret < 0) {
0fea6b79 586 error_propagate(errp, local_err);
75411d23
SH
587 return ret;
588 }
589
2e40134b
HR
590 bs = NULL;
591 ret = bdrv_open(&bs, filename, NULL, NULL,
6ebf9aa2 592 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
2e40134b 593 &local_err);
75411d23 594 if (ret < 0) {
0fea6b79 595 error_propagate(errp, local_err);
75411d23
SH
596 return ret;
597 }
598
c743849b
SH
599 /* File must start empty and grow, check truncate is supported */
600 ret = bdrv_truncate(bs, 0);
601 if (ret < 0) {
602 goto out;
603 }
604
75411d23
SH
605 if (backing_file) {
606 header.features |= QED_F_BACKING_FILE;
607 header.backing_filename_offset = sizeof(le_header);
608 header.backing_filename_size = strlen(backing_file);
609
610 if (qed_fmt_is_raw(backing_fmt)) {
611 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
612 }
613 }
614
615 qed_header_cpu_to_le(&header, &le_header);
616 ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header));
617 if (ret < 0) {
618 goto out;
619 }
620 ret = bdrv_pwrite(bs, sizeof(le_header), backing_file,
621 header.backing_filename_size);
622 if (ret < 0) {
623 goto out;
624 }
625
7267c094 626 l1_table = g_malloc0(l1_size);
75411d23
SH
627 ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size);
628 if (ret < 0) {
629 goto out;
630 }
631
632 ret = 0; /* success */
633out:
7267c094 634 g_free(l1_table);
4f6fd349 635 bdrv_unref(bs);
75411d23
SH
636 return ret;
637}
638
7ab74849 639static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp)
75411d23
SH
640{
641 uint64_t image_size = 0;
642 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
643 uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
7ab74849
CL
644 char *backing_file = NULL;
645 char *backing_fmt = NULL;
646 int ret;
647
c2eb918e
HT
648 image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
649 BDRV_SECTOR_SIZE);
7ab74849
CL
650 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
651 backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
652 cluster_size = qemu_opt_get_size_del(opts,
653 BLOCK_OPT_CLUSTER_SIZE,
654 QED_DEFAULT_CLUSTER_SIZE);
655 table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE,
656 QED_DEFAULT_TABLE_SIZE);
75411d23
SH
657
658 if (!qed_is_cluster_size_valid(cluster_size)) {
5ff679b4
AG
659 error_setg(errp, "QED cluster size must be within range [%u, %u] "
660 "and power of 2",
661 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
7ab74849
CL
662 ret = -EINVAL;
663 goto finish;
75411d23
SH
664 }
665 if (!qed_is_table_size_valid(table_size)) {
5ff679b4
AG
666 error_setg(errp, "QED table size must be within range [%u, %u] "
667 "and power of 2",
668 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
7ab74849
CL
669 ret = -EINVAL;
670 goto finish;
75411d23
SH
671 }
672 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
5ff679b4
AG
673 error_setg(errp, "QED image size must be a non-zero multiple of "
674 "cluster size and less than %" PRIu64 " bytes",
675 qed_max_image_size(cluster_size, table_size));
7ab74849
CL
676 ret = -EINVAL;
677 goto finish;
75411d23
SH
678 }
679
7ab74849 680 ret = qed_create(filename, cluster_size, image_size, table_size,
4ab15590 681 backing_file, backing_fmt, opts, errp);
7ab74849
CL
682
683finish:
684 g_free(backing_file);
685 g_free(backing_fmt);
686 return ret;
75411d23
SH
687}
688
298800ca 689typedef struct {
4bc74be9 690 BlockDriverState *bs;
b7d5a5b8 691 Coroutine *co;
4bc74be9
PB
692 uint64_t pos;
693 int64_t status;
298800ca
SH
694 int *pnum;
695} QEDIsAllocatedCB;
696
697static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
698{
699 QEDIsAllocatedCB *cb = opaque;
4bc74be9 700 BDRVQEDState *s = cb->bs->opaque;
298800ca 701 *cb->pnum = len / BDRV_SECTOR_SIZE;
4bc74be9
PB
702 switch (ret) {
703 case QED_CLUSTER_FOUND:
704 offset |= qed_offset_into_cluster(s, cb->pos);
705 cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
706 break;
707 case QED_CLUSTER_ZERO:
708 cb->status = BDRV_BLOCK_ZERO;
709 break;
710 case QED_CLUSTER_L2:
711 case QED_CLUSTER_L1:
712 cb->status = 0;
713 break;
714 default:
715 assert(ret < 0);
716 cb->status = ret;
717 break;
718 }
719
b7d5a5b8
SH
720 if (cb->co) {
721 qemu_coroutine_enter(cb->co, NULL);
722 }
298800ca
SH
723}
724
b6b8a333 725static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs,
b7d5a5b8
SH
726 int64_t sector_num,
727 int nb_sectors, int *pnum)
75411d23 728{
298800ca 729 BDRVQEDState *s = bs->opaque;
298800ca
SH
730 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
731 QEDIsAllocatedCB cb = {
4bc74be9
PB
732 .bs = bs,
733 .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE,
734 .status = BDRV_BLOCK_OFFSET_MASK,
298800ca
SH
735 .pnum = pnum,
736 };
737 QEDRequest request = { .l2_table = NULL };
738
4bc74be9 739 qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb);
298800ca 740
b7d5a5b8 741 /* Now sleep if the callback wasn't invoked immediately */
4bc74be9 742 while (cb.status == BDRV_BLOCK_OFFSET_MASK) {
b7d5a5b8
SH
743 cb.co = qemu_coroutine_self();
744 qemu_coroutine_yield();
298800ca
SH
745 }
746
298800ca
SH
747 qed_unref_l2_cache_entry(request.l2_table);
748
4bc74be9 749 return cb.status;
75411d23
SH
750}
751
eabba580
SH
752static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
753{
754 return acb->common.bs->opaque;
755}
756
757/**
758 * Read from the backing file or zero-fill if no backing file
759 *
f06ee3d4
KW
760 * @s: QED state
761 * @pos: Byte position in device
762 * @qiov: Destination I/O vector
763 * @backing_qiov: Possibly shortened copy of qiov, to be allocated here
764 * @cb: Completion function
765 * @opaque: User data for completion function
eabba580
SH
766 *
767 * This function reads qiov->size bytes starting at pos from the backing file.
768 * If there is no backing file then zeroes are read.
769 */
770static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
771 QEMUIOVector *qiov,
f06ee3d4 772 QEMUIOVector **backing_qiov,
097310b5 773 BlockCompletionFunc *cb, void *opaque)
eabba580 774{
eabba580
SH
775 uint64_t backing_length = 0;
776 size_t size;
777
778 /* If there is a backing file, get its length. Treat the absence of a
779 * backing file like a zero length backing file.
780 */
760e0063
KW
781 if (s->bs->backing) {
782 int64_t l = bdrv_getlength(s->bs->backing->bs);
eabba580
SH
783 if (l < 0) {
784 cb(opaque, l);
785 return;
786 }
787 backing_length = l;
788 }
789
790 /* Zero all sectors if reading beyond the end of the backing file */
791 if (pos >= backing_length ||
792 pos + qiov->size > backing_length) {
3d9b4925 793 qemu_iovec_memset(qiov, 0, 0, qiov->size);
eabba580
SH
794 }
795
796 /* Complete now if there are no backing file sectors to read */
797 if (pos >= backing_length) {
798 cb(opaque, 0);
799 return;
800 }
801
802 /* If the read straddles the end of the backing file, shorten it */
803 size = MIN((uint64_t)backing_length - pos, qiov->size);
804
f06ee3d4
KW
805 assert(*backing_qiov == NULL);
806 *backing_qiov = g_new(QEMUIOVector, 1);
807 qemu_iovec_init(*backing_qiov, qiov->niov);
808 qemu_iovec_concat(*backing_qiov, qiov, 0, size);
809
820100fd 810 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
760e0063 811 bdrv_aio_readv(s->bs->backing->bs, pos / BDRV_SECTOR_SIZE,
f06ee3d4 812 *backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
eabba580
SH
813}
814
815typedef struct {
816 GenericCB gencb;
817 BDRVQEDState *s;
818 QEMUIOVector qiov;
f06ee3d4 819 QEMUIOVector *backing_qiov;
eabba580
SH
820 struct iovec iov;
821 uint64_t offset;
822} CopyFromBackingFileCB;
823
824static void qed_copy_from_backing_file_cb(void *opaque, int ret)
825{
826 CopyFromBackingFileCB *copy_cb = opaque;
827 qemu_vfree(copy_cb->iov.iov_base);
828 gencb_complete(&copy_cb->gencb, ret);
829}
830
831static void qed_copy_from_backing_file_write(void *opaque, int ret)
832{
833 CopyFromBackingFileCB *copy_cb = opaque;
834 BDRVQEDState *s = copy_cb->s;
eabba580 835
f06ee3d4
KW
836 if (copy_cb->backing_qiov) {
837 qemu_iovec_destroy(copy_cb->backing_qiov);
838 g_free(copy_cb->backing_qiov);
839 copy_cb->backing_qiov = NULL;
840 }
841
eabba580
SH
842 if (ret) {
843 qed_copy_from_backing_file_cb(copy_cb, ret);
844 return;
845 }
846
847 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
9a4f4c31 848 bdrv_aio_writev(s->bs->file->bs, copy_cb->offset / BDRV_SECTOR_SIZE,
ad54ae80
PB
849 &copy_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE,
850 qed_copy_from_backing_file_cb, copy_cb);
eabba580
SH
851}
852
853/**
854 * Copy data from backing file into the image
855 *
856 * @s: QED state
857 * @pos: Byte position in device
858 * @len: Number of bytes
859 * @offset: Byte offset in image file
860 * @cb: Completion function
861 * @opaque: User data for completion function
862 */
863static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
864 uint64_t len, uint64_t offset,
097310b5 865 BlockCompletionFunc *cb,
eabba580
SH
866 void *opaque)
867{
868 CopyFromBackingFileCB *copy_cb;
869
870 /* Skip copy entirely if there is no work to do */
871 if (len == 0) {
872 cb(opaque, 0);
873 return;
874 }
875
876 copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
877 copy_cb->s = s;
878 copy_cb->offset = offset;
f06ee3d4 879 copy_cb->backing_qiov = NULL;
eabba580
SH
880 copy_cb->iov.iov_base = qemu_blockalign(s->bs, len);
881 copy_cb->iov.iov_len = len;
882 qemu_iovec_init_external(&copy_cb->qiov, &copy_cb->iov, 1);
883
f06ee3d4 884 qed_read_backing_file(s, pos, &copy_cb->qiov, &copy_cb->backing_qiov,
eabba580
SH
885 qed_copy_from_backing_file_write, copy_cb);
886}
887
888/**
889 * Link one or more contiguous clusters into a table
890 *
891 * @s: QED state
892 * @table: L2 table
893 * @index: First cluster index
894 * @n: Number of contiguous clusters
21df65b6
AL
895 * @cluster: First cluster offset
896 *
897 * The cluster offset may be an allocated byte offset in the image file, the
898 * zero cluster marker, or the unallocated cluster marker.
eabba580
SH
899 */
900static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
901 unsigned int n, uint64_t cluster)
902{
903 int i;
904 for (i = index; i < index + n; i++) {
905 table->offsets[i] = cluster;
21df65b6
AL
906 if (!qed_offset_is_unalloc_cluster(cluster) &&
907 !qed_offset_is_zero_cluster(cluster)) {
908 cluster += s->header.cluster_size;
909 }
eabba580
SH
910 }
911}
912
913static void qed_aio_complete_bh(void *opaque)
914{
915 QEDAIOCB *acb = opaque;
097310b5 916 BlockCompletionFunc *cb = acb->common.cb;
eabba580
SH
917 void *user_opaque = acb->common.opaque;
918 int ret = acb->bh_ret;
eabba580
SH
919
920 qemu_bh_delete(acb->bh);
8007429a 921 qemu_aio_unref(acb);
eabba580
SH
922
923 /* Invoke callback */
924 cb(user_opaque, ret);
eabba580
SH
925}
926
927static void qed_aio_complete(QEDAIOCB *acb, int ret)
928{
929 BDRVQEDState *s = acb_to_s(acb);
930
931 trace_qed_aio_complete(s, acb, ret);
932
933 /* Free resources */
934 qemu_iovec_destroy(&acb->cur_qiov);
935 qed_unref_l2_cache_entry(acb->request.l2_table);
936
0e71be19
SH
937 /* Free the buffer we may have allocated for zero writes */
938 if (acb->flags & QED_AIOCB_ZERO) {
939 qemu_vfree(acb->qiov->iov[0].iov_base);
940 acb->qiov->iov[0].iov_base = NULL;
941 }
942
eabba580
SH
943 /* Arrange for a bh to invoke the completion function */
944 acb->bh_ret = ret;
a8c868c3
SH
945 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
946 qed_aio_complete_bh, acb);
eabba580
SH
947 qemu_bh_schedule(acb->bh);
948
949 /* Start next allocating write request waiting behind this one. Note that
950 * requests enqueue themselves when they first hit an unallocated cluster
951 * but they wait until the entire request is finished before waking up the
952 * next request in the queue. This ensures that we don't cycle through
953 * requests multiple times but rather finish one at a time completely.
954 */
955 if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
956 QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
957 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
958 if (acb) {
959 qed_aio_next_io(acb, 0);
6f321e93
SH
960 } else if (s->header.features & QED_F_NEED_CHECK) {
961 qed_start_need_check_timer(s);
eabba580
SH
962 }
963 }
964}
965
966/**
967 * Commit the current L2 table to the cache
968 */
969static void qed_commit_l2_update(void *opaque, int ret)
970{
971 QEDAIOCB *acb = opaque;
972 BDRVQEDState *s = acb_to_s(acb);
973 CachedL2Table *l2_table = acb->request.l2_table;
e4fc8781 974 uint64_t l2_offset = l2_table->offset;
eabba580
SH
975
976 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
977
978 /* This is guaranteed to succeed because we just committed the entry to the
979 * cache.
980 */
e4fc8781 981 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
eabba580
SH
982 assert(acb->request.l2_table != NULL);
983
984 qed_aio_next_io(opaque, ret);
985}
986
987/**
988 * Update L1 table with new L2 table offset and write it out
989 */
990static void qed_aio_write_l1_update(void *opaque, int ret)
991{
992 QEDAIOCB *acb = opaque;
993 BDRVQEDState *s = acb_to_s(acb);
994 int index;
995
996 if (ret) {
997 qed_aio_complete(acb, ret);
998 return;
999 }
1000
1001 index = qed_l1_index(s, acb->cur_pos);
1002 s->l1_table->offsets[index] = acb->request.l2_table->offset;
1003
1004 qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
1005}
1006
1007/**
1008 * Update L2 table with new cluster offsets and write them out
1009 */
0e71be19 1010static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset)
eabba580 1011{
eabba580
SH
1012 BDRVQEDState *s = acb_to_s(acb);
1013 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
1014 int index;
1015
1016 if (ret) {
1017 goto err;
1018 }
1019
1020 if (need_alloc) {
1021 qed_unref_l2_cache_entry(acb->request.l2_table);
1022 acb->request.l2_table = qed_new_l2_table(s);
1023 }
1024
1025 index = qed_l2_index(s, acb->cur_pos);
1026 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
0e71be19 1027 offset);
eabba580
SH
1028
1029 if (need_alloc) {
1030 /* Write out the whole new L2 table */
1031 qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true,
1032 qed_aio_write_l1_update, acb);
1033 } else {
1034 /* Write out only the updated part of the L2 table */
1035 qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false,
1036 qed_aio_next_io, acb);
1037 }
1038 return;
1039
1040err:
1041 qed_aio_complete(acb, ret);
1042}
1043
0e71be19
SH
1044static void qed_aio_write_l2_update_cb(void *opaque, int ret)
1045{
1046 QEDAIOCB *acb = opaque;
1047 qed_aio_write_l2_update(acb, ret, acb->cur_cluster);
1048}
1049
eabba580
SH
1050/**
1051 * Flush new data clusters before updating the L2 table
1052 *
1053 * This flush is necessary when a backing file is in use. A crash during an
1054 * allocating write could result in empty clusters in the image. If the write
1055 * only touched a subregion of the cluster, then backing image sectors have
1056 * been lost in the untouched region. The solution is to flush after writing a
1057 * new data cluster and before updating the L2 table.
1058 */
1059static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
1060{
1061 QEDAIOCB *acb = opaque;
1062 BDRVQEDState *s = acb_to_s(acb);
1063
9a4f4c31 1064 if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) {
eabba580
SH
1065 qed_aio_complete(acb, -EIO);
1066 }
1067}
1068
1069/**
1070 * Write data to the image file
1071 */
1072static void qed_aio_write_main(void *opaque, int ret)
1073{
1074 QEDAIOCB *acb = opaque;
1075 BDRVQEDState *s = acb_to_s(acb);
1076 uint64_t offset = acb->cur_cluster +
1077 qed_offset_into_cluster(s, acb->cur_pos);
097310b5 1078 BlockCompletionFunc *next_fn;
eabba580
SH
1079
1080 trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
1081
1082 if (ret) {
1083 qed_aio_complete(acb, ret);
1084 return;
1085 }
1086
1087 if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
1088 next_fn = qed_aio_next_io;
1089 } else {
760e0063 1090 if (s->bs->backing) {
eabba580
SH
1091 next_fn = qed_aio_write_flush_before_l2_update;
1092 } else {
0e71be19 1093 next_fn = qed_aio_write_l2_update_cb;
eabba580
SH
1094 }
1095 }
1096
1097 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
9a4f4c31 1098 bdrv_aio_writev(s->bs->file->bs, offset / BDRV_SECTOR_SIZE,
ad54ae80
PB
1099 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1100 next_fn, acb);
eabba580
SH
1101}
1102
1103/**
1104 * Populate back untouched region of new data cluster
1105 */
1106static void qed_aio_write_postfill(void *opaque, int ret)
1107{
1108 QEDAIOCB *acb = opaque;
1109 BDRVQEDState *s = acb_to_s(acb);
1110 uint64_t start = acb->cur_pos + acb->cur_qiov.size;
1111 uint64_t len =
1112 qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1113 uint64_t offset = acb->cur_cluster +
1114 qed_offset_into_cluster(s, acb->cur_pos) +
1115 acb->cur_qiov.size;
1116
1117 if (ret) {
1118 qed_aio_complete(acb, ret);
1119 return;
1120 }
1121
1122 trace_qed_aio_write_postfill(s, acb, start, len, offset);
1123 qed_copy_from_backing_file(s, start, len, offset,
1124 qed_aio_write_main, acb);
1125}
1126
1127/**
1128 * Populate front untouched region of new data cluster
1129 */
1130static void qed_aio_write_prefill(void *opaque, int ret)
1131{
1132 QEDAIOCB *acb = opaque;
1133 BDRVQEDState *s = acb_to_s(acb);
1134 uint64_t start = qed_start_of_cluster(s, acb->cur_pos);
1135 uint64_t len = qed_offset_into_cluster(s, acb->cur_pos);
1136
1137 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1138 qed_copy_from_backing_file(s, start, len, acb->cur_cluster,
1139 qed_aio_write_postfill, acb);
1140}
1141
0d09c797
SH
1142/**
1143 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1144 */
1145static bool qed_should_set_need_check(BDRVQEDState *s)
1146{
1147 /* The flush before L2 update path ensures consistency */
760e0063 1148 if (s->bs->backing) {
0d09c797
SH
1149 return false;
1150 }
1151
1152 return !(s->header.features & QED_F_NEED_CHECK);
1153}
1154
0e71be19
SH
1155static void qed_aio_write_zero_cluster(void *opaque, int ret)
1156{
1157 QEDAIOCB *acb = opaque;
1158
1159 if (ret) {
1160 qed_aio_complete(acb, ret);
1161 return;
1162 }
1163
1164 qed_aio_write_l2_update(acb, 0, 1);
1165}
1166
eabba580
SH
1167/**
1168 * Write new data cluster
1169 *
1170 * @acb: Write request
1171 * @len: Length in bytes
1172 *
1173 * This path is taken when writing to previously unallocated clusters.
1174 */
1175static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1176{
1177 BDRVQEDState *s = acb_to_s(acb);
097310b5 1178 BlockCompletionFunc *cb;
eabba580 1179
6f321e93
SH
1180 /* Cancel timer when the first allocating request comes in */
1181 if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) {
1182 qed_cancel_need_check_timer(s);
1183 }
1184
eabba580
SH
1185 /* Freeze this request if another allocating write is in progress */
1186 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
1187 QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
1188 }
6f321e93
SH
1189 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) ||
1190 s->allocating_write_reqs_plugged) {
eabba580
SH
1191 return; /* wait for existing request to finish */
1192 }
1193
1194 acb->cur_nclusters = qed_bytes_to_clusters(s,
1195 qed_offset_into_cluster(s, acb->cur_pos) + len);
1b093c48 1196 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
eabba580 1197
0e71be19
SH
1198 if (acb->flags & QED_AIOCB_ZERO) {
1199 /* Skip ahead if the clusters are already zero */
1200 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
1201 qed_aio_next_io(acb, 0);
1202 return;
1203 }
1204
1205 cb = qed_aio_write_zero_cluster;
1206 } else {
1207 cb = qed_aio_write_prefill;
1208 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1209 }
1210
0d09c797
SH
1211 if (qed_should_set_need_check(s)) {
1212 s->header.features |= QED_F_NEED_CHECK;
0e71be19 1213 qed_write_header(s, cb, acb);
0d09c797 1214 } else {
0e71be19 1215 cb(acb, 0);
01979a98 1216 }
eabba580
SH
1217}
1218
1219/**
1220 * Write data cluster in place
1221 *
1222 * @acb: Write request
1223 * @offset: Cluster offset in bytes
1224 * @len: Length in bytes
1225 *
1226 * This path is taken when writing to already allocated clusters.
1227 */
1228static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1229{
0e71be19
SH
1230 /* Allocate buffer for zero writes */
1231 if (acb->flags & QED_AIOCB_ZERO) {
1232 struct iovec *iov = acb->qiov->iov;
1233
1234 if (!iov->iov_base) {
4f4896db
KW
1235 iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len);
1236 if (iov->iov_base == NULL) {
1237 qed_aio_complete(acb, -ENOMEM);
1238 return;
1239 }
0e71be19
SH
1240 memset(iov->iov_base, 0, iov->iov_len);
1241 }
1242 }
1243
eabba580
SH
1244 /* Calculate the I/O vector */
1245 acb->cur_cluster = offset;
1b093c48 1246 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
eabba580
SH
1247
1248 /* Do the actual write */
1249 qed_aio_write_main(acb, 0);
1250}
1251
1252/**
1253 * Write data cluster
1254 *
1255 * @opaque: Write request
1256 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1257 * or -errno
1258 * @offset: Cluster offset in bytes
1259 * @len: Length in bytes
1260 *
1261 * Callback from qed_find_cluster().
1262 */
1263static void qed_aio_write_data(void *opaque, int ret,
1264 uint64_t offset, size_t len)
1265{
1266 QEDAIOCB *acb = opaque;
1267
1268 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1269
1270 acb->find_cluster_ret = ret;
1271
1272 switch (ret) {
1273 case QED_CLUSTER_FOUND:
1274 qed_aio_write_inplace(acb, offset, len);
1275 break;
1276
1277 case QED_CLUSTER_L2:
1278 case QED_CLUSTER_L1:
21df65b6 1279 case QED_CLUSTER_ZERO:
eabba580
SH
1280 qed_aio_write_alloc(acb, len);
1281 break;
1282
1283 default:
1284 qed_aio_complete(acb, ret);
1285 break;
1286 }
1287}
1288
1289/**
1290 * Read data cluster
1291 *
1292 * @opaque: Read request
1293 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1294 * or -errno
1295 * @offset: Cluster offset in bytes
1296 * @len: Length in bytes
1297 *
1298 * Callback from qed_find_cluster().
1299 */
1300static void qed_aio_read_data(void *opaque, int ret,
1301 uint64_t offset, size_t len)
1302{
1303 QEDAIOCB *acb = opaque;
1304 BDRVQEDState *s = acb_to_s(acb);
1305 BlockDriverState *bs = acb->common.bs;
eabba580
SH
1306
1307 /* Adjust offset into cluster */
1308 offset += qed_offset_into_cluster(s, acb->cur_pos);
1309
1310 trace_qed_aio_read_data(s, acb, ret, offset, len);
1311
1312 if (ret < 0) {
1313 goto err;
1314 }
1315
1b093c48 1316 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
eabba580 1317
21df65b6
AL
1318 /* Handle zero cluster and backing file reads */
1319 if (ret == QED_CLUSTER_ZERO) {
3d9b4925 1320 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
21df65b6
AL
1321 qed_aio_next_io(acb, 0);
1322 return;
1323 } else if (ret != QED_CLUSTER_FOUND) {
eabba580 1324 qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
f06ee3d4 1325 &acb->backing_qiov, qed_aio_next_io, acb);
eabba580
SH
1326 return;
1327 }
1328
1329 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
9a4f4c31 1330 bdrv_aio_readv(bs->file->bs, offset / BDRV_SECTOR_SIZE,
ad54ae80
PB
1331 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1332 qed_aio_next_io, acb);
eabba580
SH
1333 return;
1334
1335err:
1336 qed_aio_complete(acb, ret);
1337}
1338
1339/**
1340 * Begin next I/O or complete the request
1341 */
1342static void qed_aio_next_io(void *opaque, int ret)
1343{
1344 QEDAIOCB *acb = opaque;
1345 BDRVQEDState *s = acb_to_s(acb);
6e4f59bd
SH
1346 QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ?
1347 qed_aio_write_data : qed_aio_read_data;
eabba580
SH
1348
1349 trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
1350
f06ee3d4
KW
1351 if (acb->backing_qiov) {
1352 qemu_iovec_destroy(acb->backing_qiov);
1353 g_free(acb->backing_qiov);
1354 acb->backing_qiov = NULL;
1355 }
1356
eabba580
SH
1357 /* Handle I/O error */
1358 if (ret) {
1359 qed_aio_complete(acb, ret);
1360 return;
1361 }
1362
1363 acb->qiov_offset += acb->cur_qiov.size;
1364 acb->cur_pos += acb->cur_qiov.size;
1365 qemu_iovec_reset(&acb->cur_qiov);
1366
1367 /* Complete request */
1368 if (acb->cur_pos >= acb->end_pos) {
1369 qed_aio_complete(acb, 0);
1370 return;
1371 }
1372
1373 /* Find next cluster and start I/O */
1374 qed_find_cluster(s, &acb->request,
1375 acb->cur_pos, acb->end_pos - acb->cur_pos,
1376 io_fn, acb);
1377}
1378
7c84b1b8
MA
1379static BlockAIOCB *qed_aio_setup(BlockDriverState *bs,
1380 int64_t sector_num,
1381 QEMUIOVector *qiov, int nb_sectors,
097310b5 1382 BlockCompletionFunc *cb,
7c84b1b8 1383 void *opaque, int flags)
eabba580 1384{
d7331bed 1385 QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque);
eabba580
SH
1386
1387 trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
6e4f59bd 1388 opaque, flags);
eabba580 1389
6e4f59bd 1390 acb->flags = flags;
eabba580
SH
1391 acb->qiov = qiov;
1392 acb->qiov_offset = 0;
1393 acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
1394 acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
f06ee3d4 1395 acb->backing_qiov = NULL;
eabba580
SH
1396 acb->request.l2_table = NULL;
1397 qemu_iovec_init(&acb->cur_qiov, qiov->niov);
1398
1399 /* Start request */
1400 qed_aio_next_io(acb, 0);
1401 return &acb->common;
1402}
1403
7c84b1b8
MA
1404static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
1405 int64_t sector_num,
1406 QEMUIOVector *qiov, int nb_sectors,
097310b5 1407 BlockCompletionFunc *cb,
7c84b1b8 1408 void *opaque)
75411d23 1409{
6e4f59bd 1410 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
75411d23
SH
1411}
1412
7c84b1b8
MA
1413static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
1414 int64_t sector_num,
1415 QEMUIOVector *qiov, int nb_sectors,
097310b5 1416 BlockCompletionFunc *cb,
7c84b1b8 1417 void *opaque)
75411d23 1418{
6e4f59bd
SH
1419 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb,
1420 opaque, QED_AIOCB_WRITE);
75411d23
SH
1421}
1422
0e71be19
SH
1423typedef struct {
1424 Coroutine *co;
1425 int ret;
1426 bool done;
1427} QEDWriteZeroesCB;
1428
1429static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret)
1430{
1431 QEDWriteZeroesCB *cb = opaque;
1432
1433 cb->done = true;
1434 cb->ret = ret;
1435 if (cb->co) {
1436 qemu_coroutine_enter(cb->co, NULL);
1437 }
1438}
1439
1440static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs,
1441 int64_t sector_num,
aa7bfbff
PL
1442 int nb_sectors,
1443 BdrvRequestFlags flags)
0e71be19 1444{
7c84b1b8 1445 BlockAIOCB *blockacb;
ef72f76e 1446 BDRVQEDState *s = bs->opaque;
0e71be19
SH
1447 QEDWriteZeroesCB cb = { .done = false };
1448 QEMUIOVector qiov;
1449 struct iovec iov;
1450
ef72f76e 1451 /* Refuse if there are untouched backing file sectors */
760e0063 1452 if (bs->backing) {
ef72f76e
SH
1453 if (qed_offset_into_cluster(s, sector_num * BDRV_SECTOR_SIZE) != 0) {
1454 return -ENOTSUP;
1455 }
1456 if (qed_offset_into_cluster(s, nb_sectors * BDRV_SECTOR_SIZE) != 0) {
1457 return -ENOTSUP;
1458 }
1459 }
1460
0e71be19
SH
1461 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1462 * then it will be allocated during request processing.
1463 */
1464 iov.iov_base = NULL,
1465 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1466
1467 qemu_iovec_init_external(&qiov, &iov, 1);
1468 blockacb = qed_aio_setup(bs, sector_num, &qiov, nb_sectors,
1469 qed_co_write_zeroes_cb, &cb,
1470 QED_AIOCB_WRITE | QED_AIOCB_ZERO);
1471 if (!blockacb) {
1472 return -EIO;
1473 }
1474 if (!cb.done) {
1475 cb.co = qemu_coroutine_self();
1476 qemu_coroutine_yield();
1477 }
1478 assert(cb.done);
1479 return cb.ret;
1480}
1481
75411d23
SH
1482static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset)
1483{
77a5a000
SH
1484 BDRVQEDState *s = bs->opaque;
1485 uint64_t old_image_size;
1486 int ret;
1487
1488 if (!qed_is_image_size_valid(offset, s->header.cluster_size,
1489 s->header.table_size)) {
1490 return -EINVAL;
1491 }
1492
1493 /* Shrinking is currently not supported */
1494 if ((uint64_t)offset < s->header.image_size) {
1495 return -ENOTSUP;
1496 }
1497
1498 old_image_size = s->header.image_size;
1499 s->header.image_size = offset;
1500 ret = qed_write_header_sync(s);
1501 if (ret < 0) {
1502 s->header.image_size = old_image_size;
1503 }
1504 return ret;
75411d23
SH
1505}
1506
1507static int64_t bdrv_qed_getlength(BlockDriverState *bs)
1508{
1509 BDRVQEDState *s = bs->opaque;
1510 return s->header.image_size;
1511}
1512
1513static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1514{
1515 BDRVQEDState *s = bs->opaque;
1516
1517 memset(bdi, 0, sizeof(*bdi));
1518 bdi->cluster_size = s->header.cluster_size;
d68dbee8 1519 bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
95de6d70
PB
1520 bdi->unallocated_blocks_are_zero = true;
1521 bdi->can_write_zeroes_with_unmap = true;
75411d23
SH
1522 return 0;
1523}
1524
1525static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1526 const char *backing_file,
1527 const char *backing_fmt)
1528{
1529 BDRVQEDState *s = bs->opaque;
1530 QEDHeader new_header, le_header;
1531 void *buffer;
1532 size_t buffer_len, backing_file_len;
1533 int ret;
1534
1535 /* Refuse to set backing filename if unknown compat feature bits are
1536 * active. If the image uses an unknown compat feature then we may not
1537 * know the layout of data following the header structure and cannot safely
1538 * add a new string.
1539 */
1540 if (backing_file && (s->header.compat_features &
1541 ~QED_COMPAT_FEATURE_MASK)) {
1542 return -ENOTSUP;
1543 }
1544
1545 memcpy(&new_header, &s->header, sizeof(new_header));
1546
1547 new_header.features &= ~(QED_F_BACKING_FILE |
1548 QED_F_BACKING_FORMAT_NO_PROBE);
1549
1550 /* Adjust feature flags */
1551 if (backing_file) {
1552 new_header.features |= QED_F_BACKING_FILE;
1553
1554 if (qed_fmt_is_raw(backing_fmt)) {
1555 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1556 }
1557 }
1558
1559 /* Calculate new header size */
1560 backing_file_len = 0;
1561
1562 if (backing_file) {
1563 backing_file_len = strlen(backing_file);
1564 }
1565
1566 buffer_len = sizeof(new_header);
1567 new_header.backing_filename_offset = buffer_len;
1568 new_header.backing_filename_size = backing_file_len;
1569 buffer_len += backing_file_len;
1570
1571 /* Make sure we can rewrite header without failing */
1572 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1573 return -ENOSPC;
1574 }
1575
1576 /* Prepare new header */
7267c094 1577 buffer = g_malloc(buffer_len);
75411d23
SH
1578
1579 qed_header_cpu_to_le(&new_header, &le_header);
1580 memcpy(buffer, &le_header, sizeof(le_header));
1581 buffer_len = sizeof(le_header);
1582
feba23b1
PB
1583 if (backing_file) {
1584 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1585 buffer_len += backing_file_len;
1586 }
75411d23
SH
1587
1588 /* Write new header */
9a4f4c31 1589 ret = bdrv_pwrite_sync(bs->file->bs, 0, buffer, buffer_len);
7267c094 1590 g_free(buffer);
75411d23
SH
1591 if (ret == 0) {
1592 memcpy(&s->header, &new_header, sizeof(new_header));
1593 }
1594 return ret;
1595}
1596
5a8a30db 1597static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp)
c82954e5
BC
1598{
1599 BDRVQEDState *s = bs->opaque;
5a8a30db
KW
1600 Error *local_err = NULL;
1601 int ret;
c82954e5
BC
1602
1603 bdrv_qed_close(bs);
3456a8d1 1604
9a4f4c31 1605 bdrv_invalidate_cache(bs->file->bs, &local_err);
5a8a30db
KW
1606 if (local_err) {
1607 error_propagate(errp, local_err);
1608 return;
1609 }
3456a8d1 1610
c82954e5 1611 memset(s, 0, sizeof(BDRVQEDState));
5a8a30db
KW
1612 ret = bdrv_qed_open(bs, NULL, bs->open_flags, &local_err);
1613 if (local_err) {
e43bfd9c
MA
1614 error_propagate(errp, local_err);
1615 error_prepend(errp, "Could not reopen qed layer: ");
5a8a30db
KW
1616 return;
1617 } else if (ret < 0) {
1618 error_setg_errno(errp, -ret, "Could not reopen qed layer");
1619 return;
1620 }
c82954e5
BC
1621}
1622
4534ff54
KW
1623static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result,
1624 BdrvCheckMode fix)
75411d23 1625{
01979a98
SH
1626 BDRVQEDState *s = bs->opaque;
1627
4534ff54 1628 return qed_check(s, result, !!fix);
75411d23
SH
1629}
1630
7ab74849
CL
1631static QemuOptsList qed_create_opts = {
1632 .name = "qed-create-opts",
1633 .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
1634 .desc = {
1635 {
1636 .name = BLOCK_OPT_SIZE,
1637 .type = QEMU_OPT_SIZE,
1638 .help = "Virtual disk size"
1639 },
1640 {
1641 .name = BLOCK_OPT_BACKING_FILE,
1642 .type = QEMU_OPT_STRING,
1643 .help = "File name of a base image"
1644 },
1645 {
1646 .name = BLOCK_OPT_BACKING_FMT,
1647 .type = QEMU_OPT_STRING,
1648 .help = "Image format of the base image"
1649 },
1650 {
1651 .name = BLOCK_OPT_CLUSTER_SIZE,
1652 .type = QEMU_OPT_SIZE,
1653 .help = "Cluster size (in bytes)",
1654 .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE)
1655 },
1656 {
1657 .name = BLOCK_OPT_TABLE_SIZE,
1658 .type = QEMU_OPT_SIZE,
1659 .help = "L1/L2 table size (in clusters)"
1660 },
1661 { /* end of list */ }
1662 }
75411d23
SH
1663};
1664
1665static BlockDriver bdrv_qed = {
1666 .format_name = "qed",
1667 .instance_size = sizeof(BDRVQEDState),
7ab74849 1668 .create_opts = &qed_create_opts,
8ee79e70 1669 .supports_backing = true,
75411d23
SH
1670
1671 .bdrv_probe = bdrv_qed_probe,
1672 .bdrv_open = bdrv_qed_open,
1673 .bdrv_close = bdrv_qed_close,
f9cb20f1 1674 .bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
c282e1fd 1675 .bdrv_create = bdrv_qed_create,
3ac21627 1676 .bdrv_has_zero_init = bdrv_has_zero_init_1,
b6b8a333 1677 .bdrv_co_get_block_status = bdrv_qed_co_get_block_status,
75411d23
SH
1678 .bdrv_aio_readv = bdrv_qed_aio_readv,
1679 .bdrv_aio_writev = bdrv_qed_aio_writev,
0e71be19 1680 .bdrv_co_write_zeroes = bdrv_qed_co_write_zeroes,
75411d23
SH
1681 .bdrv_truncate = bdrv_qed_truncate,
1682 .bdrv_getlength = bdrv_qed_getlength,
1683 .bdrv_get_info = bdrv_qed_get_info,
d34682cd 1684 .bdrv_refresh_limits = bdrv_qed_refresh_limits,
75411d23 1685 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
c82954e5 1686 .bdrv_invalidate_cache = bdrv_qed_invalidate_cache,
75411d23 1687 .bdrv_check = bdrv_qed_check,
a8c868c3
SH
1688 .bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
1689 .bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
df9a681d 1690 .bdrv_drain = bdrv_qed_drain,
75411d23
SH
1691};
1692
1693static void bdrv_qed_init(void)
1694{
1695 bdrv_register(&bdrv_qed);
1696}
1697
1698block_init(bdrv_qed_init);