scripts/dump-guest-memory.py: Move constants to the top
[qemu/ar7.git] / block / qed.c
blob31f4cc9e60b405b00e74d0b8659fa97b9c402702
1 /*
2 * QEMU Enhanced Disk Format
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Anthony Liguori <aliguori@us.ibm.com>
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.
15 #include "qemu/timer.h"
16 #include "trace.h"
17 #include "qed.h"
18 #include "qapi/qmp/qerror.h"
19 #include "migration/migration.h"
21 static const AIOCBInfo qed_aiocb_info = {
22 .aiocb_size = sizeof(QEDAIOCB),
25 static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
26 const char *filename)
28 const QEDHeader *header = (const QEDHeader *)buf;
30 if (buf_size < sizeof(*header)) {
31 return 0;
33 if (le32_to_cpu(header->magic) != QED_MAGIC) {
34 return 0;
36 return 100;
39 /**
40 * Check whether an image format is raw
42 * @fmt: Backing file format, may be NULL
44 static bool qed_fmt_is_raw(const char *fmt)
46 return fmt && strcmp(fmt, "raw") == 0;
49 static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
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);
64 static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
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);
79 int qed_write_header_sync(BDRVQEDState *s)
81 QEDHeader le;
82 int ret;
84 qed_header_cpu_to_le(&s->header, &le);
85 ret = bdrv_pwrite(s->bs->file->bs, 0, &le, sizeof(le));
86 if (ret != sizeof(le)) {
87 return ret;
89 return 0;
92 typedef struct {
93 GenericCB gencb;
94 BDRVQEDState *s;
95 struct iovec iov;
96 QEMUIOVector qiov;
97 int nsectors;
98 uint8_t *buf;
99 } QEDWriteHeaderCB;
101 static void qed_write_header_cb(void *opaque, int ret)
103 QEDWriteHeaderCB *write_header_cb = opaque;
105 qemu_vfree(write_header_cb->buf);
106 gencb_complete(write_header_cb, ret);
109 static void qed_write_header_read_cb(void *opaque, int ret)
111 QEDWriteHeaderCB *write_header_cb = opaque;
112 BDRVQEDState *s = write_header_cb->s;
114 if (ret) {
115 qed_write_header_cb(write_header_cb, ret);
116 return;
119 /* Update header */
120 qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf);
122 bdrv_aio_writev(s->bs->file->bs, 0, &write_header_cb->qiov,
123 write_header_cb->nsectors, qed_write_header_cb,
124 write_header_cb);
128 * Update header in-place (does not rewrite backing filename or other strings)
130 * This function only updates known header fields in-place and does not affect
131 * extra data after the QED header.
133 static void qed_write_header(BDRVQEDState *s, BlockCompletionFunc cb,
134 void *opaque)
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.
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);
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);
155 bdrv_aio_readv(s->bs->file->bs, 0, &write_header_cb->qiov, nsectors,
156 qed_write_header_read_cb, write_header_cb);
159 static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
161 uint64_t table_entries;
162 uint64_t l2_size;
164 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
165 l2_size = table_entries * cluster_size;
167 return l2_size * table_entries;
170 static bool qed_is_cluster_size_valid(uint32_t cluster_size)
172 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
173 cluster_size > QED_MAX_CLUSTER_SIZE) {
174 return false;
176 if (cluster_size & (cluster_size - 1)) {
177 return false; /* not power of 2 */
179 return true;
182 static bool qed_is_table_size_valid(uint32_t table_size)
184 if (table_size < QED_MIN_TABLE_SIZE ||
185 table_size > QED_MAX_TABLE_SIZE) {
186 return false;
188 if (table_size & (table_size - 1)) {
189 return false; /* not power of 2 */
191 return true;
194 static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
195 uint32_t table_size)
197 if (image_size % BDRV_SECTOR_SIZE != 0) {
198 return false; /* not multiple of sector size */
200 if (image_size > qed_max_image_size(cluster_size, table_size)) {
201 return false; /* image is too large */
203 return true;
207 * Read a string of known length from the image file
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
216 * The string is NUL-terminated.
218 static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n,
219 char *buf, size_t buflen)
221 int ret;
222 if (n >= buflen) {
223 return -EINVAL;
225 ret = bdrv_pread(file, offset, buf, n);
226 if (ret < 0) {
227 return ret;
229 buf[n] = '\0';
230 return 0;
234 * Allocate new clusters
236 * @s: QED state
237 * @n: Number of contiguous clusters to allocate
238 * @ret: Offset of first allocated cluster
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.
244 static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
246 uint64_t offset = s->file_size;
247 s->file_size += n * s->header.cluster_size;
248 return offset;
251 QEDTable *qed_alloc_table(BDRVQEDState *s)
253 /* Honor O_DIRECT memory alignment requirements */
254 return qemu_blockalign(s->bs,
255 s->header.cluster_size * s->header.table_size);
259 * Allocate a new zeroed L2 table
261 static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
263 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
265 l2_table->table = qed_alloc_table(s);
266 l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
268 memset(l2_table->table->offsets, 0,
269 s->header.cluster_size * s->header.table_size);
270 return l2_table;
273 static void qed_aio_next_io(void *opaque, int ret);
275 static void qed_plug_allocating_write_reqs(BDRVQEDState *s)
277 assert(!s->allocating_write_reqs_plugged);
279 s->allocating_write_reqs_plugged = true;
282 static void qed_unplug_allocating_write_reqs(BDRVQEDState *s)
284 QEDAIOCB *acb;
286 assert(s->allocating_write_reqs_plugged);
288 s->allocating_write_reqs_plugged = false;
290 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
291 if (acb) {
292 qed_aio_next_io(acb, 0);
296 static void qed_finish_clear_need_check(void *opaque, int ret)
298 /* Do nothing */
301 static void qed_flush_after_clear_need_check(void *opaque, int ret)
303 BDRVQEDState *s = opaque;
305 bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s);
307 /* No need to wait until flush completes */
308 qed_unplug_allocating_write_reqs(s);
311 static void qed_clear_need_check(void *opaque, int ret)
313 BDRVQEDState *s = opaque;
315 if (ret) {
316 qed_unplug_allocating_write_reqs(s);
317 return;
320 s->header.features &= ~QED_F_NEED_CHECK;
321 qed_write_header(s, qed_flush_after_clear_need_check, s);
324 static void qed_need_check_timer_cb(void *opaque)
326 BDRVQEDState *s = opaque;
328 /* The timer should only fire when allocating writes have drained */
329 assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs));
331 trace_qed_need_check_timer_cb(s);
333 qed_plug_allocating_write_reqs(s);
335 /* Ensure writes are on disk before clearing flag */
336 bdrv_aio_flush(s->bs, qed_clear_need_check, s);
339 static void qed_start_need_check_timer(BDRVQEDState *s)
341 trace_qed_start_need_check_timer(s);
343 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
344 * migration.
346 timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
347 get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT);
350 /* It's okay to call this multiple times or when no timer is started */
351 static void qed_cancel_need_check_timer(BDRVQEDState *s)
353 trace_qed_cancel_need_check_timer(s);
354 timer_del(s->need_check_timer);
357 static void bdrv_qed_detach_aio_context(BlockDriverState *bs)
359 BDRVQEDState *s = bs->opaque;
361 qed_cancel_need_check_timer(s);
362 timer_free(s->need_check_timer);
365 static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
366 AioContext *new_context)
368 BDRVQEDState *s = bs->opaque;
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);
378 static void bdrv_qed_drain(BlockDriverState *bs)
380 BDRVQEDState *s = bs->opaque;
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);
390 static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
391 Error **errp)
393 BDRVQEDState *s = bs->opaque;
394 QEDHeader le_header;
395 int64_t file_size;
396 int ret;
398 s->bs = bs;
399 QSIMPLEQ_INIT(&s->allocating_write_reqs);
401 ret = bdrv_pread(bs->file->bs, 0, &le_header, sizeof(le_header));
402 if (ret < 0) {
403 return ret;
405 qed_header_le_to_cpu(&le_header, &s->header);
407 if (s->header.magic != QED_MAGIC) {
408 error_setg(errp, "Image not in QED format");
409 return -EINVAL;
411 if (s->header.features & ~QED_FEATURE_MASK) {
412 /* image uses unsupported feature bits */
413 char buf[64];
414 snprintf(buf, sizeof(buf), "%" PRIx64,
415 s->header.features & ~QED_FEATURE_MASK);
416 error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
417 bdrv_get_device_or_node_name(bs), "QED", buf);
418 return -ENOTSUP;
420 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
421 return -EINVAL;
424 /* Round down file size to the last cluster */
425 file_size = bdrv_getlength(bs->file->bs);
426 if (file_size < 0) {
427 return file_size;
429 s->file_size = qed_start_of_cluster(s, file_size);
431 if (!qed_is_table_size_valid(s->header.table_size)) {
432 return -EINVAL;
434 if (!qed_is_image_size_valid(s->header.image_size,
435 s->header.cluster_size,
436 s->header.table_size)) {
437 return -EINVAL;
439 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
440 return -EINVAL;
443 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
444 sizeof(uint64_t);
445 s->l2_shift = ctz32(s->header.cluster_size);
446 s->l2_mask = s->table_nelems - 1;
447 s->l1_shift = s->l2_shift + ctz32(s->table_nelems);
449 /* Header size calculation must not overflow uint32_t */
450 if (s->header.header_size > UINT32_MAX / s->header.cluster_size) {
451 return -EINVAL;
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;
461 ret = qed_read_string(bs->file->bs, s->header.backing_filename_offset,
462 s->header.backing_filename_size, bs->backing_file,
463 sizeof(bs->backing_file));
464 if (ret < 0) {
465 return ret;
468 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
469 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
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.
479 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
480 !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INCOMING)) {
481 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
483 ret = qed_write_header_sync(s);
484 if (ret) {
485 return ret;
488 /* From here on only known autoclear feature bits are valid */
489 bdrv_flush(bs->file->bs);
492 s->l1_table = qed_alloc_table(s);
493 qed_init_l2_cache(&s->l2_cache);
495 ret = qed_read_l1_table_sync(s);
496 if (ret) {
497 goto out;
500 /* If image was not closed cleanly, check consistency */
501 if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
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.
507 if (!bdrv_is_read_only(bs->file->bs) &&
508 !(flags & BDRV_O_INCOMING)) {
509 BdrvCheckResult result = {0};
511 ret = qed_check(s, &result, true);
512 if (ret) {
513 goto out;
518 bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
520 out:
521 if (ret) {
522 qed_free_l2_cache(&s->l2_cache);
523 qemu_vfree(s->l1_table);
525 return ret;
528 static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
530 BDRVQEDState *s = bs->opaque;
532 bs->bl.write_zeroes_alignment = s->header.cluster_size >> BDRV_SECTOR_BITS;
535 /* We have nothing to do for QED reopen, stubs just return
536 * success */
537 static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
538 BlockReopenQueue *queue, Error **errp)
540 return 0;
543 static void bdrv_qed_close(BlockDriverState *bs)
545 BDRVQEDState *s = bs->opaque;
547 bdrv_qed_detach_aio_context(bs);
549 /* Ensure writes reach stable storage */
550 bdrv_flush(bs->file->bs);
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);
558 qed_free_l2_cache(&s->l2_cache);
559 qemu_vfree(s->l1_table);
562 static int qed_create(const char *filename, uint32_t cluster_size,
563 uint64_t image_size, uint32_t table_size,
564 const char *backing_file, const char *backing_fmt,
565 QemuOpts *opts, Error **errp)
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,
577 QEDHeader le_header;
578 uint8_t *l1_table = NULL;
579 size_t l1_size = header.cluster_size * header.table_size;
580 Error *local_err = NULL;
581 int ret = 0;
582 BlockDriverState *bs;
584 ret = bdrv_create_file(filename, opts, &local_err);
585 if (ret < 0) {
586 error_propagate(errp, local_err);
587 return ret;
590 bs = NULL;
591 ret = bdrv_open(&bs, filename, NULL, NULL,
592 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
593 &local_err);
594 if (ret < 0) {
595 error_propagate(errp, local_err);
596 return ret;
599 /* File must start empty and grow, check truncate is supported */
600 ret = bdrv_truncate(bs, 0);
601 if (ret < 0) {
602 goto out;
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);
610 if (qed_fmt_is_raw(backing_fmt)) {
611 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
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;
620 ret = bdrv_pwrite(bs, sizeof(le_header), backing_file,
621 header.backing_filename_size);
622 if (ret < 0) {
623 goto out;
626 l1_table = g_malloc0(l1_size);
627 ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size);
628 if (ret < 0) {
629 goto out;
632 ret = 0; /* success */
633 out:
634 g_free(l1_table);
635 bdrv_unref(bs);
636 return ret;
639 static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp)
641 uint64_t image_size = 0;
642 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
643 uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
644 char *backing_file = NULL;
645 char *backing_fmt = NULL;
646 int ret;
648 image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
649 BDRV_SECTOR_SIZE);
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);
658 if (!qed_is_cluster_size_valid(cluster_size)) {
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);
662 ret = -EINVAL;
663 goto finish;
665 if (!qed_is_table_size_valid(table_size)) {
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);
669 ret = -EINVAL;
670 goto finish;
672 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
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));
676 ret = -EINVAL;
677 goto finish;
680 ret = qed_create(filename, cluster_size, image_size, table_size,
681 backing_file, backing_fmt, opts, errp);
683 finish:
684 g_free(backing_file);
685 g_free(backing_fmt);
686 return ret;
689 typedef struct {
690 BlockDriverState *bs;
691 Coroutine *co;
692 uint64_t pos;
693 int64_t status;
694 int *pnum;
695 } QEDIsAllocatedCB;
697 static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
699 QEDIsAllocatedCB *cb = opaque;
700 BDRVQEDState *s = cb->bs->opaque;
701 *cb->pnum = len / BDRV_SECTOR_SIZE;
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;
720 if (cb->co) {
721 qemu_coroutine_enter(cb->co, NULL);
725 static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs,
726 int64_t sector_num,
727 int nb_sectors, int *pnum)
729 BDRVQEDState *s = bs->opaque;
730 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
731 QEDIsAllocatedCB cb = {
732 .bs = bs,
733 .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE,
734 .status = BDRV_BLOCK_OFFSET_MASK,
735 .pnum = pnum,
737 QEDRequest request = { .l2_table = NULL };
739 qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb);
741 /* Now sleep if the callback wasn't invoked immediately */
742 while (cb.status == BDRV_BLOCK_OFFSET_MASK) {
743 cb.co = qemu_coroutine_self();
744 qemu_coroutine_yield();
747 qed_unref_l2_cache_entry(request.l2_table);
749 return cb.status;
752 static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
754 return acb->common.bs->opaque;
758 * Read from the backing file or zero-fill if no backing file
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
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.
770 static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
771 QEMUIOVector *qiov,
772 QEMUIOVector **backing_qiov,
773 BlockCompletionFunc *cb, void *opaque)
775 uint64_t backing_length = 0;
776 size_t size;
778 /* If there is a backing file, get its length. Treat the absence of a
779 * backing file like a zero length backing file.
781 if (s->bs->backing) {
782 int64_t l = bdrv_getlength(s->bs->backing->bs);
783 if (l < 0) {
784 cb(opaque, l);
785 return;
787 backing_length = l;
790 /* Zero all sectors if reading beyond the end of the backing file */
791 if (pos >= backing_length ||
792 pos + qiov->size > backing_length) {
793 qemu_iovec_memset(qiov, 0, 0, qiov->size);
796 /* Complete now if there are no backing file sectors to read */
797 if (pos >= backing_length) {
798 cb(opaque, 0);
799 return;
802 /* If the read straddles the end of the backing file, shorten it */
803 size = MIN((uint64_t)backing_length - pos, qiov->size);
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);
810 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
811 bdrv_aio_readv(s->bs->backing->bs, pos / BDRV_SECTOR_SIZE,
812 *backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
815 typedef struct {
816 GenericCB gencb;
817 BDRVQEDState *s;
818 QEMUIOVector qiov;
819 QEMUIOVector *backing_qiov;
820 struct iovec iov;
821 uint64_t offset;
822 } CopyFromBackingFileCB;
824 static void qed_copy_from_backing_file_cb(void *opaque, int ret)
826 CopyFromBackingFileCB *copy_cb = opaque;
827 qemu_vfree(copy_cb->iov.iov_base);
828 gencb_complete(&copy_cb->gencb, ret);
831 static void qed_copy_from_backing_file_write(void *opaque, int ret)
833 CopyFromBackingFileCB *copy_cb = opaque;
834 BDRVQEDState *s = copy_cb->s;
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;
842 if (ret) {
843 qed_copy_from_backing_file_cb(copy_cb, ret);
844 return;
847 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
848 bdrv_aio_writev(s->bs->file->bs, copy_cb->offset / BDRV_SECTOR_SIZE,
849 &copy_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE,
850 qed_copy_from_backing_file_cb, copy_cb);
854 * Copy data from backing file into the image
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
863 static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
864 uint64_t len, uint64_t offset,
865 BlockCompletionFunc *cb,
866 void *opaque)
868 CopyFromBackingFileCB *copy_cb;
870 /* Skip copy entirely if there is no work to do */
871 if (len == 0) {
872 cb(opaque, 0);
873 return;
876 copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
877 copy_cb->s = s;
878 copy_cb->offset = offset;
879 copy_cb->backing_qiov = NULL;
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);
884 qed_read_backing_file(s, pos, &copy_cb->qiov, &copy_cb->backing_qiov,
885 qed_copy_from_backing_file_write, copy_cb);
889 * Link one or more contiguous clusters into a table
891 * @s: QED state
892 * @table: L2 table
893 * @index: First cluster index
894 * @n: Number of contiguous clusters
895 * @cluster: First cluster offset
897 * The cluster offset may be an allocated byte offset in the image file, the
898 * zero cluster marker, or the unallocated cluster marker.
900 static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
901 unsigned int n, uint64_t cluster)
903 int i;
904 for (i = index; i < index + n; i++) {
905 table->offsets[i] = cluster;
906 if (!qed_offset_is_unalloc_cluster(cluster) &&
907 !qed_offset_is_zero_cluster(cluster)) {
908 cluster += s->header.cluster_size;
913 static void qed_aio_complete_bh(void *opaque)
915 QEDAIOCB *acb = opaque;
916 BlockCompletionFunc *cb = acb->common.cb;
917 void *user_opaque = acb->common.opaque;
918 int ret = acb->bh_ret;
920 qemu_bh_delete(acb->bh);
921 qemu_aio_unref(acb);
923 /* Invoke callback */
924 cb(user_opaque, ret);
927 static void qed_aio_complete(QEDAIOCB *acb, int ret)
929 BDRVQEDState *s = acb_to_s(acb);
931 trace_qed_aio_complete(s, acb, ret);
933 /* Free resources */
934 qemu_iovec_destroy(&acb->cur_qiov);
935 qed_unref_l2_cache_entry(acb->request.l2_table);
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;
943 /* Arrange for a bh to invoke the completion function */
944 acb->bh_ret = ret;
945 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
946 qed_aio_complete_bh, acb);
947 qemu_bh_schedule(acb->bh);
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.
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);
960 } else if (s->header.features & QED_F_NEED_CHECK) {
961 qed_start_need_check_timer(s);
967 * Commit the current L2 table to the cache
969 static void qed_commit_l2_update(void *opaque, int ret)
971 QEDAIOCB *acb = opaque;
972 BDRVQEDState *s = acb_to_s(acb);
973 CachedL2Table *l2_table = acb->request.l2_table;
974 uint64_t l2_offset = l2_table->offset;
976 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
978 /* This is guaranteed to succeed because we just committed the entry to the
979 * cache.
981 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
982 assert(acb->request.l2_table != NULL);
984 qed_aio_next_io(opaque, ret);
988 * Update L1 table with new L2 table offset and write it out
990 static void qed_aio_write_l1_update(void *opaque, int ret)
992 QEDAIOCB *acb = opaque;
993 BDRVQEDState *s = acb_to_s(acb);
994 int index;
996 if (ret) {
997 qed_aio_complete(acb, ret);
998 return;
1001 index = qed_l1_index(s, acb->cur_pos);
1002 s->l1_table->offsets[index] = acb->request.l2_table->offset;
1004 qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
1008 * Update L2 table with new cluster offsets and write them out
1010 static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset)
1012 BDRVQEDState *s = acb_to_s(acb);
1013 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
1014 int index;
1016 if (ret) {
1017 goto err;
1020 if (need_alloc) {
1021 qed_unref_l2_cache_entry(acb->request.l2_table);
1022 acb->request.l2_table = qed_new_l2_table(s);
1025 index = qed_l2_index(s, acb->cur_pos);
1026 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
1027 offset);
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);
1038 return;
1040 err:
1041 qed_aio_complete(acb, ret);
1044 static void qed_aio_write_l2_update_cb(void *opaque, int ret)
1046 QEDAIOCB *acb = opaque;
1047 qed_aio_write_l2_update(acb, ret, acb->cur_cluster);
1051 * Flush new data clusters before updating the L2 table
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.
1059 static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
1061 QEDAIOCB *acb = opaque;
1062 BDRVQEDState *s = acb_to_s(acb);
1064 if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) {
1065 qed_aio_complete(acb, -EIO);
1070 * Write data to the image file
1072 static void qed_aio_write_main(void *opaque, int ret)
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);
1078 BlockCompletionFunc *next_fn;
1080 trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
1082 if (ret) {
1083 qed_aio_complete(acb, ret);
1084 return;
1087 if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
1088 next_fn = qed_aio_next_io;
1089 } else {
1090 if (s->bs->backing) {
1091 next_fn = qed_aio_write_flush_before_l2_update;
1092 } else {
1093 next_fn = qed_aio_write_l2_update_cb;
1097 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
1098 bdrv_aio_writev(s->bs->file->bs, offset / BDRV_SECTOR_SIZE,
1099 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1100 next_fn, acb);
1104 * Populate back untouched region of new data cluster
1106 static void qed_aio_write_postfill(void *opaque, int ret)
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;
1117 if (ret) {
1118 qed_aio_complete(acb, ret);
1119 return;
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);
1128 * Populate front untouched region of new data cluster
1130 static void qed_aio_write_prefill(void *opaque, int ret)
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);
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);
1143 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1145 static bool qed_should_set_need_check(BDRVQEDState *s)
1147 /* The flush before L2 update path ensures consistency */
1148 if (s->bs->backing) {
1149 return false;
1152 return !(s->header.features & QED_F_NEED_CHECK);
1155 static void qed_aio_write_zero_cluster(void *opaque, int ret)
1157 QEDAIOCB *acb = opaque;
1159 if (ret) {
1160 qed_aio_complete(acb, ret);
1161 return;
1164 qed_aio_write_l2_update(acb, 0, 1);
1168 * Write new data cluster
1170 * @acb: Write request
1171 * @len: Length in bytes
1173 * This path is taken when writing to previously unallocated clusters.
1175 static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1177 BDRVQEDState *s = acb_to_s(acb);
1178 BlockCompletionFunc *cb;
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);
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);
1189 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) ||
1190 s->allocating_write_reqs_plugged) {
1191 return; /* wait for existing request to finish */
1194 acb->cur_nclusters = qed_bytes_to_clusters(s,
1195 qed_offset_into_cluster(s, acb->cur_pos) + len);
1196 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
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;
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);
1211 if (qed_should_set_need_check(s)) {
1212 s->header.features |= QED_F_NEED_CHECK;
1213 qed_write_header(s, cb, acb);
1214 } else {
1215 cb(acb, 0);
1220 * Write data cluster in place
1222 * @acb: Write request
1223 * @offset: Cluster offset in bytes
1224 * @len: Length in bytes
1226 * This path is taken when writing to already allocated clusters.
1228 static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1230 /* Allocate buffer for zero writes */
1231 if (acb->flags & QED_AIOCB_ZERO) {
1232 struct iovec *iov = acb->qiov->iov;
1234 if (!iov->iov_base) {
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;
1240 memset(iov->iov_base, 0, iov->iov_len);
1244 /* Calculate the I/O vector */
1245 acb->cur_cluster = offset;
1246 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1248 /* Do the actual write */
1249 qed_aio_write_main(acb, 0);
1253 * Write data cluster
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
1261 * Callback from qed_find_cluster().
1263 static void qed_aio_write_data(void *opaque, int ret,
1264 uint64_t offset, size_t len)
1266 QEDAIOCB *acb = opaque;
1268 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1270 acb->find_cluster_ret = ret;
1272 switch (ret) {
1273 case QED_CLUSTER_FOUND:
1274 qed_aio_write_inplace(acb, offset, len);
1275 break;
1277 case QED_CLUSTER_L2:
1278 case QED_CLUSTER_L1:
1279 case QED_CLUSTER_ZERO:
1280 qed_aio_write_alloc(acb, len);
1281 break;
1283 default:
1284 qed_aio_complete(acb, ret);
1285 break;
1290 * Read data cluster
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
1298 * Callback from qed_find_cluster().
1300 static void qed_aio_read_data(void *opaque, int ret,
1301 uint64_t offset, size_t len)
1303 QEDAIOCB *acb = opaque;
1304 BDRVQEDState *s = acb_to_s(acb);
1305 BlockDriverState *bs = acb->common.bs;
1307 /* Adjust offset into cluster */
1308 offset += qed_offset_into_cluster(s, acb->cur_pos);
1310 trace_qed_aio_read_data(s, acb, ret, offset, len);
1312 if (ret < 0) {
1313 goto err;
1316 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1318 /* Handle zero cluster and backing file reads */
1319 if (ret == QED_CLUSTER_ZERO) {
1320 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
1321 qed_aio_next_io(acb, 0);
1322 return;
1323 } else if (ret != QED_CLUSTER_FOUND) {
1324 qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
1325 &acb->backing_qiov, qed_aio_next_io, acb);
1326 return;
1329 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1330 bdrv_aio_readv(bs->file->bs, offset / BDRV_SECTOR_SIZE,
1331 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1332 qed_aio_next_io, acb);
1333 return;
1335 err:
1336 qed_aio_complete(acb, ret);
1340 * Begin next I/O or complete the request
1342 static void qed_aio_next_io(void *opaque, int ret)
1344 QEDAIOCB *acb = opaque;
1345 BDRVQEDState *s = acb_to_s(acb);
1346 QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ?
1347 qed_aio_write_data : qed_aio_read_data;
1349 trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
1351 if (acb->backing_qiov) {
1352 qemu_iovec_destroy(acb->backing_qiov);
1353 g_free(acb->backing_qiov);
1354 acb->backing_qiov = NULL;
1357 /* Handle I/O error */
1358 if (ret) {
1359 qed_aio_complete(acb, ret);
1360 return;
1363 acb->qiov_offset += acb->cur_qiov.size;
1364 acb->cur_pos += acb->cur_qiov.size;
1365 qemu_iovec_reset(&acb->cur_qiov);
1367 /* Complete request */
1368 if (acb->cur_pos >= acb->end_pos) {
1369 qed_aio_complete(acb, 0);
1370 return;
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);
1379 static BlockAIOCB *qed_aio_setup(BlockDriverState *bs,
1380 int64_t sector_num,
1381 QEMUIOVector *qiov, int nb_sectors,
1382 BlockCompletionFunc *cb,
1383 void *opaque, int flags)
1385 QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque);
1387 trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
1388 opaque, flags);
1390 acb->flags = flags;
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;
1395 acb->backing_qiov = NULL;
1396 acb->request.l2_table = NULL;
1397 qemu_iovec_init(&acb->cur_qiov, qiov->niov);
1399 /* Start request */
1400 qed_aio_next_io(acb, 0);
1401 return &acb->common;
1404 static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
1405 int64_t sector_num,
1406 QEMUIOVector *qiov, int nb_sectors,
1407 BlockCompletionFunc *cb,
1408 void *opaque)
1410 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1413 static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
1414 int64_t sector_num,
1415 QEMUIOVector *qiov, int nb_sectors,
1416 BlockCompletionFunc *cb,
1417 void *opaque)
1419 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb,
1420 opaque, QED_AIOCB_WRITE);
1423 typedef struct {
1424 Coroutine *co;
1425 int ret;
1426 bool done;
1427 } QEDWriteZeroesCB;
1429 static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret)
1431 QEDWriteZeroesCB *cb = opaque;
1433 cb->done = true;
1434 cb->ret = ret;
1435 if (cb->co) {
1436 qemu_coroutine_enter(cb->co, NULL);
1440 static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs,
1441 int64_t sector_num,
1442 int nb_sectors,
1443 BdrvRequestFlags flags)
1445 BlockAIOCB *blockacb;
1446 BDRVQEDState *s = bs->opaque;
1447 QEDWriteZeroesCB cb = { .done = false };
1448 QEMUIOVector qiov;
1449 struct iovec iov;
1451 /* Refuse if there are untouched backing file sectors */
1452 if (bs->backing) {
1453 if (qed_offset_into_cluster(s, sector_num * BDRV_SECTOR_SIZE) != 0) {
1454 return -ENOTSUP;
1456 if (qed_offset_into_cluster(s, nb_sectors * BDRV_SECTOR_SIZE) != 0) {
1457 return -ENOTSUP;
1461 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1462 * then it will be allocated during request processing.
1464 iov.iov_base = NULL,
1465 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE,
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;
1474 if (!cb.done) {
1475 cb.co = qemu_coroutine_self();
1476 qemu_coroutine_yield();
1478 assert(cb.done);
1479 return cb.ret;
1482 static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset)
1484 BDRVQEDState *s = bs->opaque;
1485 uint64_t old_image_size;
1486 int ret;
1488 if (!qed_is_image_size_valid(offset, s->header.cluster_size,
1489 s->header.table_size)) {
1490 return -EINVAL;
1493 /* Shrinking is currently not supported */
1494 if ((uint64_t)offset < s->header.image_size) {
1495 return -ENOTSUP;
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;
1504 return ret;
1507 static int64_t bdrv_qed_getlength(BlockDriverState *bs)
1509 BDRVQEDState *s = bs->opaque;
1510 return s->header.image_size;
1513 static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1515 BDRVQEDState *s = bs->opaque;
1517 memset(bdi, 0, sizeof(*bdi));
1518 bdi->cluster_size = s->header.cluster_size;
1519 bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
1520 bdi->unallocated_blocks_are_zero = true;
1521 bdi->can_write_zeroes_with_unmap = true;
1522 return 0;
1525 static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1526 const char *backing_file,
1527 const char *backing_fmt)
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;
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.
1540 if (backing_file && (s->header.compat_features &
1541 ~QED_COMPAT_FEATURE_MASK)) {
1542 return -ENOTSUP;
1545 memcpy(&new_header, &s->header, sizeof(new_header));
1547 new_header.features &= ~(QED_F_BACKING_FILE |
1548 QED_F_BACKING_FORMAT_NO_PROBE);
1550 /* Adjust feature flags */
1551 if (backing_file) {
1552 new_header.features |= QED_F_BACKING_FILE;
1554 if (qed_fmt_is_raw(backing_fmt)) {
1555 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1559 /* Calculate new header size */
1560 backing_file_len = 0;
1562 if (backing_file) {
1563 backing_file_len = strlen(backing_file);
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;
1571 /* Make sure we can rewrite header without failing */
1572 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1573 return -ENOSPC;
1576 /* Prepare new header */
1577 buffer = g_malloc(buffer_len);
1579 qed_header_cpu_to_le(&new_header, &le_header);
1580 memcpy(buffer, &le_header, sizeof(le_header));
1581 buffer_len = sizeof(le_header);
1583 if (backing_file) {
1584 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1585 buffer_len += backing_file_len;
1588 /* Write new header */
1589 ret = bdrv_pwrite_sync(bs->file->bs, 0, buffer, buffer_len);
1590 g_free(buffer);
1591 if (ret == 0) {
1592 memcpy(&s->header, &new_header, sizeof(new_header));
1594 return ret;
1597 static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp)
1599 BDRVQEDState *s = bs->opaque;
1600 Error *local_err = NULL;
1601 int ret;
1603 bdrv_qed_close(bs);
1605 bdrv_invalidate_cache(bs->file->bs, &local_err);
1606 if (local_err) {
1607 error_propagate(errp, local_err);
1608 return;
1611 memset(s, 0, sizeof(BDRVQEDState));
1612 ret = bdrv_qed_open(bs, NULL, bs->open_flags, &local_err);
1613 if (local_err) {
1614 error_propagate(errp, local_err);
1615 error_prepend(errp, "Could not reopen qed layer: ");
1616 return;
1617 } else if (ret < 0) {
1618 error_setg_errno(errp, -ret, "Could not reopen qed layer");
1619 return;
1623 static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result,
1624 BdrvCheckMode fix)
1626 BDRVQEDState *s = bs->opaque;
1628 return qed_check(s, result, !!fix);
1631 static QemuOptsList qed_create_opts = {
1632 .name = "qed-create-opts",
1633 .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
1634 .desc = {
1636 .name = BLOCK_OPT_SIZE,
1637 .type = QEMU_OPT_SIZE,
1638 .help = "Virtual disk size"
1641 .name = BLOCK_OPT_BACKING_FILE,
1642 .type = QEMU_OPT_STRING,
1643 .help = "File name of a base image"
1646 .name = BLOCK_OPT_BACKING_FMT,
1647 .type = QEMU_OPT_STRING,
1648 .help = "Image format of the base image"
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)
1657 .name = BLOCK_OPT_TABLE_SIZE,
1658 .type = QEMU_OPT_SIZE,
1659 .help = "L1/L2 table size (in clusters)"
1661 { /* end of list */ }
1665 static BlockDriver bdrv_qed = {
1666 .format_name = "qed",
1667 .instance_size = sizeof(BDRVQEDState),
1668 .create_opts = &qed_create_opts,
1669 .supports_backing = true,
1671 .bdrv_probe = bdrv_qed_probe,
1672 .bdrv_open = bdrv_qed_open,
1673 .bdrv_close = bdrv_qed_close,
1674 .bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
1675 .bdrv_create = bdrv_qed_create,
1676 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1677 .bdrv_co_get_block_status = bdrv_qed_co_get_block_status,
1678 .bdrv_aio_readv = bdrv_qed_aio_readv,
1679 .bdrv_aio_writev = bdrv_qed_aio_writev,
1680 .bdrv_co_write_zeroes = bdrv_qed_co_write_zeroes,
1681 .bdrv_truncate = bdrv_qed_truncate,
1682 .bdrv_getlength = bdrv_qed_getlength,
1683 .bdrv_get_info = bdrv_qed_get_info,
1684 .bdrv_refresh_limits = bdrv_qed_refresh_limits,
1685 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
1686 .bdrv_invalidate_cache = bdrv_qed_invalidate_cache,
1687 .bdrv_check = bdrv_qed_check,
1688 .bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
1689 .bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
1690 .bdrv_drain = bdrv_qed_drain,
1693 static void bdrv_qed_init(void)
1695 bdrv_register(&bdrv_qed);
1698 block_init(bdrv_qed_init);