fdc: use IsaDma interface instead of global DMA_* functions
[qemu.git] / block / qed.c
blob404be1e9b984ceaab27754f9ba60f3ca55726fea
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/osdep.h"
16 #include "qemu/timer.h"
17 #include "trace.h"
18 #include "qed.h"
19 #include "qapi/qmp/qerror.h"
20 #include "migration/migration.h"
22 static const AIOCBInfo qed_aiocb_info = {
23 .aiocb_size = sizeof(QEDAIOCB),
26 static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
27 const char *filename)
29 const QEDHeader *header = (const QEDHeader *)buf;
31 if (buf_size < sizeof(*header)) {
32 return 0;
34 if (le32_to_cpu(header->magic) != QED_MAGIC) {
35 return 0;
37 return 100;
40 /**
41 * Check whether an image format is raw
43 * @fmt: Backing file format, may be NULL
45 static bool qed_fmt_is_raw(const char *fmt)
47 return fmt && strcmp(fmt, "raw") == 0;
50 static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
52 cpu->magic = le32_to_cpu(le->magic);
53 cpu->cluster_size = le32_to_cpu(le->cluster_size);
54 cpu->table_size = le32_to_cpu(le->table_size);
55 cpu->header_size = le32_to_cpu(le->header_size);
56 cpu->features = le64_to_cpu(le->features);
57 cpu->compat_features = le64_to_cpu(le->compat_features);
58 cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
59 cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
60 cpu->image_size = le64_to_cpu(le->image_size);
61 cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
62 cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
65 static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
67 le->magic = cpu_to_le32(cpu->magic);
68 le->cluster_size = cpu_to_le32(cpu->cluster_size);
69 le->table_size = cpu_to_le32(cpu->table_size);
70 le->header_size = cpu_to_le32(cpu->header_size);
71 le->features = cpu_to_le64(cpu->features);
72 le->compat_features = cpu_to_le64(cpu->compat_features);
73 le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
74 le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
75 le->image_size = cpu_to_le64(cpu->image_size);
76 le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
77 le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
80 int qed_write_header_sync(BDRVQEDState *s)
82 QEDHeader le;
83 int ret;
85 qed_header_cpu_to_le(&s->header, &le);
86 ret = bdrv_pwrite(s->bs->file->bs, 0, &le, sizeof(le));
87 if (ret != sizeof(le)) {
88 return ret;
90 return 0;
93 typedef struct {
94 GenericCB gencb;
95 BDRVQEDState *s;
96 struct iovec iov;
97 QEMUIOVector qiov;
98 int nsectors;
99 uint8_t *buf;
100 } QEDWriteHeaderCB;
102 static void qed_write_header_cb(void *opaque, int ret)
104 QEDWriteHeaderCB *write_header_cb = opaque;
106 qemu_vfree(write_header_cb->buf);
107 gencb_complete(write_header_cb, ret);
110 static void qed_write_header_read_cb(void *opaque, int ret)
112 QEDWriteHeaderCB *write_header_cb = opaque;
113 BDRVQEDState *s = write_header_cb->s;
115 if (ret) {
116 qed_write_header_cb(write_header_cb, ret);
117 return;
120 /* Update header */
121 qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf);
123 bdrv_aio_writev(s->bs->file->bs, 0, &write_header_cb->qiov,
124 write_header_cb->nsectors, qed_write_header_cb,
125 write_header_cb);
129 * Update header in-place (does not rewrite backing filename or other strings)
131 * This function only updates known header fields in-place and does not affect
132 * extra data after the QED header.
134 static void qed_write_header(BDRVQEDState *s, BlockCompletionFunc cb,
135 void *opaque)
137 /* We must write full sectors for O_DIRECT but cannot necessarily generate
138 * the data following the header if an unrecognized compat feature is
139 * active. Therefore, first read the sectors containing the header, update
140 * them, and write back.
143 int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) /
144 BDRV_SECTOR_SIZE;
145 size_t len = nsectors * BDRV_SECTOR_SIZE;
146 QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb),
147 cb, opaque);
149 write_header_cb->s = s;
150 write_header_cb->nsectors = nsectors;
151 write_header_cb->buf = qemu_blockalign(s->bs, len);
152 write_header_cb->iov.iov_base = write_header_cb->buf;
153 write_header_cb->iov.iov_len = len;
154 qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1);
156 bdrv_aio_readv(s->bs->file->bs, 0, &write_header_cb->qiov, nsectors,
157 qed_write_header_read_cb, write_header_cb);
160 static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
162 uint64_t table_entries;
163 uint64_t l2_size;
165 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
166 l2_size = table_entries * cluster_size;
168 return l2_size * table_entries;
171 static bool qed_is_cluster_size_valid(uint32_t cluster_size)
173 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
174 cluster_size > QED_MAX_CLUSTER_SIZE) {
175 return false;
177 if (cluster_size & (cluster_size - 1)) {
178 return false; /* not power of 2 */
180 return true;
183 static bool qed_is_table_size_valid(uint32_t table_size)
185 if (table_size < QED_MIN_TABLE_SIZE ||
186 table_size > QED_MAX_TABLE_SIZE) {
187 return false;
189 if (table_size & (table_size - 1)) {
190 return false; /* not power of 2 */
192 return true;
195 static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
196 uint32_t table_size)
198 if (image_size % BDRV_SECTOR_SIZE != 0) {
199 return false; /* not multiple of sector size */
201 if (image_size > qed_max_image_size(cluster_size, table_size)) {
202 return false; /* image is too large */
204 return true;
208 * Read a string of known length from the image file
210 * @file: Image file
211 * @offset: File offset to start of string, in bytes
212 * @n: String length in bytes
213 * @buf: Destination buffer
214 * @buflen: Destination buffer length in bytes
215 * @ret: 0 on success, -errno on failure
217 * The string is NUL-terminated.
219 static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n,
220 char *buf, size_t buflen)
222 int ret;
223 if (n >= buflen) {
224 return -EINVAL;
226 ret = bdrv_pread(file, offset, buf, n);
227 if (ret < 0) {
228 return ret;
230 buf[n] = '\0';
231 return 0;
235 * Allocate new clusters
237 * @s: QED state
238 * @n: Number of contiguous clusters to allocate
239 * @ret: Offset of first allocated cluster
241 * This function only produces the offset where the new clusters should be
242 * written. It updates BDRVQEDState but does not make any changes to the image
243 * file.
245 static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
247 uint64_t offset = s->file_size;
248 s->file_size += n * s->header.cluster_size;
249 return offset;
252 QEDTable *qed_alloc_table(BDRVQEDState *s)
254 /* Honor O_DIRECT memory alignment requirements */
255 return qemu_blockalign(s->bs,
256 s->header.cluster_size * s->header.table_size);
260 * Allocate a new zeroed L2 table
262 static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
264 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
266 l2_table->table = qed_alloc_table(s);
267 l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
269 memset(l2_table->table->offsets, 0,
270 s->header.cluster_size * s->header.table_size);
271 return l2_table;
274 static void qed_aio_next_io(void *opaque, int ret);
276 static void qed_plug_allocating_write_reqs(BDRVQEDState *s)
278 assert(!s->allocating_write_reqs_plugged);
280 s->allocating_write_reqs_plugged = true;
283 static void qed_unplug_allocating_write_reqs(BDRVQEDState *s)
285 QEDAIOCB *acb;
287 assert(s->allocating_write_reqs_plugged);
289 s->allocating_write_reqs_plugged = false;
291 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
292 if (acb) {
293 qed_aio_next_io(acb, 0);
297 static void qed_finish_clear_need_check(void *opaque, int ret)
299 /* Do nothing */
302 static void qed_flush_after_clear_need_check(void *opaque, int ret)
304 BDRVQEDState *s = opaque;
306 bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s);
308 /* No need to wait until flush completes */
309 qed_unplug_allocating_write_reqs(s);
312 static void qed_clear_need_check(void *opaque, int ret)
314 BDRVQEDState *s = opaque;
316 if (ret) {
317 qed_unplug_allocating_write_reqs(s);
318 return;
321 s->header.features &= ~QED_F_NEED_CHECK;
322 qed_write_header(s, qed_flush_after_clear_need_check, s);
325 static void qed_need_check_timer_cb(void *opaque)
327 BDRVQEDState *s = opaque;
329 /* The timer should only fire when allocating writes have drained */
330 assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs));
332 trace_qed_need_check_timer_cb(s);
334 qed_plug_allocating_write_reqs(s);
336 /* Ensure writes are on disk before clearing flag */
337 bdrv_aio_flush(s->bs, qed_clear_need_check, s);
340 static void qed_start_need_check_timer(BDRVQEDState *s)
342 trace_qed_start_need_check_timer(s);
344 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
345 * migration.
347 timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
348 get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT);
351 /* It's okay to call this multiple times or when no timer is started */
352 static void qed_cancel_need_check_timer(BDRVQEDState *s)
354 trace_qed_cancel_need_check_timer(s);
355 timer_del(s->need_check_timer);
358 static void bdrv_qed_detach_aio_context(BlockDriverState *bs)
360 BDRVQEDState *s = bs->opaque;
362 qed_cancel_need_check_timer(s);
363 timer_free(s->need_check_timer);
366 static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
367 AioContext *new_context)
369 BDRVQEDState *s = bs->opaque;
371 s->need_check_timer = aio_timer_new(new_context,
372 QEMU_CLOCK_VIRTUAL, SCALE_NS,
373 qed_need_check_timer_cb, s);
374 if (s->header.features & QED_F_NEED_CHECK) {
375 qed_start_need_check_timer(s);
379 static void bdrv_qed_drain(BlockDriverState *bs)
381 BDRVQEDState *s = bs->opaque;
383 /* Cancel timer and start doing I/O that were meant to happen as if it
384 * fired, that way we get bdrv_drain() taking care of the ongoing requests
385 * correctly. */
386 qed_cancel_need_check_timer(s);
387 qed_plug_allocating_write_reqs(s);
388 bdrv_aio_flush(s->bs, qed_clear_need_check, s);
391 static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
392 Error **errp)
394 BDRVQEDState *s = bs->opaque;
395 QEDHeader le_header;
396 int64_t file_size;
397 int ret;
399 s->bs = bs;
400 QSIMPLEQ_INIT(&s->allocating_write_reqs);
402 ret = bdrv_pread(bs->file->bs, 0, &le_header, sizeof(le_header));
403 if (ret < 0) {
404 return ret;
406 qed_header_le_to_cpu(&le_header, &s->header);
408 if (s->header.magic != QED_MAGIC) {
409 error_setg(errp, "Image not in QED format");
410 return -EINVAL;
412 if (s->header.features & ~QED_FEATURE_MASK) {
413 /* image uses unsupported feature bits */
414 char buf[64];
415 snprintf(buf, sizeof(buf), "%" PRIx64,
416 s->header.features & ~QED_FEATURE_MASK);
417 error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
418 bdrv_get_device_or_node_name(bs), "QED", buf);
419 return -ENOTSUP;
421 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
422 return -EINVAL;
425 /* Round down file size to the last cluster */
426 file_size = bdrv_getlength(bs->file->bs);
427 if (file_size < 0) {
428 return file_size;
430 s->file_size = qed_start_of_cluster(s, file_size);
432 if (!qed_is_table_size_valid(s->header.table_size)) {
433 return -EINVAL;
435 if (!qed_is_image_size_valid(s->header.image_size,
436 s->header.cluster_size,
437 s->header.table_size)) {
438 return -EINVAL;
440 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
441 return -EINVAL;
444 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
445 sizeof(uint64_t);
446 s->l2_shift = ctz32(s->header.cluster_size);
447 s->l2_mask = s->table_nelems - 1;
448 s->l1_shift = s->l2_shift + ctz32(s->table_nelems);
450 /* Header size calculation must not overflow uint32_t */
451 if (s->header.header_size > UINT32_MAX / s->header.cluster_size) {
452 return -EINVAL;
455 if ((s->header.features & QED_F_BACKING_FILE)) {
456 if ((uint64_t)s->header.backing_filename_offset +
457 s->header.backing_filename_size >
458 s->header.cluster_size * s->header.header_size) {
459 return -EINVAL;
462 ret = qed_read_string(bs->file->bs, s->header.backing_filename_offset,
463 s->header.backing_filename_size, bs->backing_file,
464 sizeof(bs->backing_file));
465 if (ret < 0) {
466 return ret;
469 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
470 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
474 /* Reset unknown autoclear feature bits. This is a backwards
475 * compatibility mechanism that allows images to be opened by older
476 * programs, which "knock out" unknown feature bits. When an image is
477 * opened by a newer program again it can detect that the autoclear
478 * feature is no longer valid.
480 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
481 !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) {
482 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
484 ret = qed_write_header_sync(s);
485 if (ret) {
486 return ret;
489 /* From here on only known autoclear feature bits are valid */
490 bdrv_flush(bs->file->bs);
493 s->l1_table = qed_alloc_table(s);
494 qed_init_l2_cache(&s->l2_cache);
496 ret = qed_read_l1_table_sync(s);
497 if (ret) {
498 goto out;
501 /* If image was not closed cleanly, check consistency */
502 if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
503 /* Read-only images cannot be fixed. There is no risk of corruption
504 * since write operations are not possible. Therefore, allow
505 * potentially inconsistent images to be opened read-only. This can
506 * aid data recovery from an otherwise inconsistent image.
508 if (!bdrv_is_read_only(bs->file->bs) &&
509 !(flags & BDRV_O_INACTIVE)) {
510 BdrvCheckResult result = {0};
512 ret = qed_check(s, &result, true);
513 if (ret) {
514 goto out;
519 bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
521 out:
522 if (ret) {
523 qed_free_l2_cache(&s->l2_cache);
524 qemu_vfree(s->l1_table);
526 return ret;
529 static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
531 BDRVQEDState *s = bs->opaque;
533 bs->bl.write_zeroes_alignment = s->header.cluster_size >> BDRV_SECTOR_BITS;
536 /* We have nothing to do for QED reopen, stubs just return
537 * success */
538 static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
539 BlockReopenQueue *queue, Error **errp)
541 return 0;
544 static void bdrv_qed_close(BlockDriverState *bs)
546 BDRVQEDState *s = bs->opaque;
548 bdrv_qed_detach_aio_context(bs);
550 /* Ensure writes reach stable storage */
551 bdrv_flush(bs->file->bs);
553 /* Clean shutdown, no check required on next open */
554 if (s->header.features & QED_F_NEED_CHECK) {
555 s->header.features &= ~QED_F_NEED_CHECK;
556 qed_write_header_sync(s);
559 qed_free_l2_cache(&s->l2_cache);
560 qemu_vfree(s->l1_table);
563 static int qed_create(const char *filename, uint32_t cluster_size,
564 uint64_t image_size, uint32_t table_size,
565 const char *backing_file, const char *backing_fmt,
566 QemuOpts *opts, Error **errp)
568 QEDHeader header = {
569 .magic = QED_MAGIC,
570 .cluster_size = cluster_size,
571 .table_size = table_size,
572 .header_size = 1,
573 .features = 0,
574 .compat_features = 0,
575 .l1_table_offset = cluster_size,
576 .image_size = image_size,
578 QEDHeader le_header;
579 uint8_t *l1_table = NULL;
580 size_t l1_size = header.cluster_size * header.table_size;
581 Error *local_err = NULL;
582 int ret = 0;
583 BlockDriverState *bs;
585 ret = bdrv_create_file(filename, opts, &local_err);
586 if (ret < 0) {
587 error_propagate(errp, local_err);
588 return ret;
591 bs = NULL;
592 ret = bdrv_open(&bs, filename, NULL, NULL,
593 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
594 &local_err);
595 if (ret < 0) {
596 error_propagate(errp, local_err);
597 return ret;
600 /* File must start empty and grow, check truncate is supported */
601 ret = bdrv_truncate(bs, 0);
602 if (ret < 0) {
603 goto out;
606 if (backing_file) {
607 header.features |= QED_F_BACKING_FILE;
608 header.backing_filename_offset = sizeof(le_header);
609 header.backing_filename_size = strlen(backing_file);
611 if (qed_fmt_is_raw(backing_fmt)) {
612 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
616 qed_header_cpu_to_le(&header, &le_header);
617 ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header));
618 if (ret < 0) {
619 goto out;
621 ret = bdrv_pwrite(bs, sizeof(le_header), backing_file,
622 header.backing_filename_size);
623 if (ret < 0) {
624 goto out;
627 l1_table = g_malloc0(l1_size);
628 ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size);
629 if (ret < 0) {
630 goto out;
633 ret = 0; /* success */
634 out:
635 g_free(l1_table);
636 bdrv_unref(bs);
637 return ret;
640 static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp)
642 uint64_t image_size = 0;
643 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
644 uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
645 char *backing_file = NULL;
646 char *backing_fmt = NULL;
647 int ret;
649 image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
650 BDRV_SECTOR_SIZE);
651 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
652 backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
653 cluster_size = qemu_opt_get_size_del(opts,
654 BLOCK_OPT_CLUSTER_SIZE,
655 QED_DEFAULT_CLUSTER_SIZE);
656 table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE,
657 QED_DEFAULT_TABLE_SIZE);
659 if (!qed_is_cluster_size_valid(cluster_size)) {
660 error_setg(errp, "QED cluster size must be within range [%u, %u] "
661 "and power of 2",
662 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
663 ret = -EINVAL;
664 goto finish;
666 if (!qed_is_table_size_valid(table_size)) {
667 error_setg(errp, "QED table size must be within range [%u, %u] "
668 "and power of 2",
669 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
670 ret = -EINVAL;
671 goto finish;
673 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
674 error_setg(errp, "QED image size must be a non-zero multiple of "
675 "cluster size and less than %" PRIu64 " bytes",
676 qed_max_image_size(cluster_size, table_size));
677 ret = -EINVAL;
678 goto finish;
681 ret = qed_create(filename, cluster_size, image_size, table_size,
682 backing_file, backing_fmt, opts, errp);
684 finish:
685 g_free(backing_file);
686 g_free(backing_fmt);
687 return ret;
690 typedef struct {
691 BlockDriverState *bs;
692 Coroutine *co;
693 uint64_t pos;
694 int64_t status;
695 int *pnum;
696 BlockDriverState **file;
697 } QEDIsAllocatedCB;
699 static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
701 QEDIsAllocatedCB *cb = opaque;
702 BDRVQEDState *s = cb->bs->opaque;
703 *cb->pnum = len / BDRV_SECTOR_SIZE;
704 switch (ret) {
705 case QED_CLUSTER_FOUND:
706 offset |= qed_offset_into_cluster(s, cb->pos);
707 cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
708 *cb->file = cb->bs->file->bs;
709 break;
710 case QED_CLUSTER_ZERO:
711 cb->status = BDRV_BLOCK_ZERO;
712 break;
713 case QED_CLUSTER_L2:
714 case QED_CLUSTER_L1:
715 cb->status = 0;
716 break;
717 default:
718 assert(ret < 0);
719 cb->status = ret;
720 break;
723 if (cb->co) {
724 qemu_coroutine_enter(cb->co, NULL);
728 static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs,
729 int64_t sector_num,
730 int nb_sectors, int *pnum,
731 BlockDriverState **file)
733 BDRVQEDState *s = bs->opaque;
734 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
735 QEDIsAllocatedCB cb = {
736 .bs = bs,
737 .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE,
738 .status = BDRV_BLOCK_OFFSET_MASK,
739 .pnum = pnum,
740 .file = file,
742 QEDRequest request = { .l2_table = NULL };
744 qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb);
746 /* Now sleep if the callback wasn't invoked immediately */
747 while (cb.status == BDRV_BLOCK_OFFSET_MASK) {
748 cb.co = qemu_coroutine_self();
749 qemu_coroutine_yield();
752 qed_unref_l2_cache_entry(request.l2_table);
754 return cb.status;
757 static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
759 return acb->common.bs->opaque;
763 * Read from the backing file or zero-fill if no backing file
765 * @s: QED state
766 * @pos: Byte position in device
767 * @qiov: Destination I/O vector
768 * @backing_qiov: Possibly shortened copy of qiov, to be allocated here
769 * @cb: Completion function
770 * @opaque: User data for completion function
772 * This function reads qiov->size bytes starting at pos from the backing file.
773 * If there is no backing file then zeroes are read.
775 static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
776 QEMUIOVector *qiov,
777 QEMUIOVector **backing_qiov,
778 BlockCompletionFunc *cb, void *opaque)
780 uint64_t backing_length = 0;
781 size_t size;
783 /* If there is a backing file, get its length. Treat the absence of a
784 * backing file like a zero length backing file.
786 if (s->bs->backing) {
787 int64_t l = bdrv_getlength(s->bs->backing->bs);
788 if (l < 0) {
789 cb(opaque, l);
790 return;
792 backing_length = l;
795 /* Zero all sectors if reading beyond the end of the backing file */
796 if (pos >= backing_length ||
797 pos + qiov->size > backing_length) {
798 qemu_iovec_memset(qiov, 0, 0, qiov->size);
801 /* Complete now if there are no backing file sectors to read */
802 if (pos >= backing_length) {
803 cb(opaque, 0);
804 return;
807 /* If the read straddles the end of the backing file, shorten it */
808 size = MIN((uint64_t)backing_length - pos, qiov->size);
810 assert(*backing_qiov == NULL);
811 *backing_qiov = g_new(QEMUIOVector, 1);
812 qemu_iovec_init(*backing_qiov, qiov->niov);
813 qemu_iovec_concat(*backing_qiov, qiov, 0, size);
815 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
816 bdrv_aio_readv(s->bs->backing->bs, pos / BDRV_SECTOR_SIZE,
817 *backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
820 typedef struct {
821 GenericCB gencb;
822 BDRVQEDState *s;
823 QEMUIOVector qiov;
824 QEMUIOVector *backing_qiov;
825 struct iovec iov;
826 uint64_t offset;
827 } CopyFromBackingFileCB;
829 static void qed_copy_from_backing_file_cb(void *opaque, int ret)
831 CopyFromBackingFileCB *copy_cb = opaque;
832 qemu_vfree(copy_cb->iov.iov_base);
833 gencb_complete(&copy_cb->gencb, ret);
836 static void qed_copy_from_backing_file_write(void *opaque, int ret)
838 CopyFromBackingFileCB *copy_cb = opaque;
839 BDRVQEDState *s = copy_cb->s;
841 if (copy_cb->backing_qiov) {
842 qemu_iovec_destroy(copy_cb->backing_qiov);
843 g_free(copy_cb->backing_qiov);
844 copy_cb->backing_qiov = NULL;
847 if (ret) {
848 qed_copy_from_backing_file_cb(copy_cb, ret);
849 return;
852 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
853 bdrv_aio_writev(s->bs->file->bs, copy_cb->offset / BDRV_SECTOR_SIZE,
854 &copy_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE,
855 qed_copy_from_backing_file_cb, copy_cb);
859 * Copy data from backing file into the image
861 * @s: QED state
862 * @pos: Byte position in device
863 * @len: Number of bytes
864 * @offset: Byte offset in image file
865 * @cb: Completion function
866 * @opaque: User data for completion function
868 static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
869 uint64_t len, uint64_t offset,
870 BlockCompletionFunc *cb,
871 void *opaque)
873 CopyFromBackingFileCB *copy_cb;
875 /* Skip copy entirely if there is no work to do */
876 if (len == 0) {
877 cb(opaque, 0);
878 return;
881 copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
882 copy_cb->s = s;
883 copy_cb->offset = offset;
884 copy_cb->backing_qiov = NULL;
885 copy_cb->iov.iov_base = qemu_blockalign(s->bs, len);
886 copy_cb->iov.iov_len = len;
887 qemu_iovec_init_external(&copy_cb->qiov, &copy_cb->iov, 1);
889 qed_read_backing_file(s, pos, &copy_cb->qiov, &copy_cb->backing_qiov,
890 qed_copy_from_backing_file_write, copy_cb);
894 * Link one or more contiguous clusters into a table
896 * @s: QED state
897 * @table: L2 table
898 * @index: First cluster index
899 * @n: Number of contiguous clusters
900 * @cluster: First cluster offset
902 * The cluster offset may be an allocated byte offset in the image file, the
903 * zero cluster marker, or the unallocated cluster marker.
905 static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
906 unsigned int n, uint64_t cluster)
908 int i;
909 for (i = index; i < index + n; i++) {
910 table->offsets[i] = cluster;
911 if (!qed_offset_is_unalloc_cluster(cluster) &&
912 !qed_offset_is_zero_cluster(cluster)) {
913 cluster += s->header.cluster_size;
918 static void qed_aio_complete_bh(void *opaque)
920 QEDAIOCB *acb = opaque;
921 BlockCompletionFunc *cb = acb->common.cb;
922 void *user_opaque = acb->common.opaque;
923 int ret = acb->bh_ret;
925 qemu_bh_delete(acb->bh);
926 qemu_aio_unref(acb);
928 /* Invoke callback */
929 cb(user_opaque, ret);
932 static void qed_aio_complete(QEDAIOCB *acb, int ret)
934 BDRVQEDState *s = acb_to_s(acb);
936 trace_qed_aio_complete(s, acb, ret);
938 /* Free resources */
939 qemu_iovec_destroy(&acb->cur_qiov);
940 qed_unref_l2_cache_entry(acb->request.l2_table);
942 /* Free the buffer we may have allocated for zero writes */
943 if (acb->flags & QED_AIOCB_ZERO) {
944 qemu_vfree(acb->qiov->iov[0].iov_base);
945 acb->qiov->iov[0].iov_base = NULL;
948 /* Arrange for a bh to invoke the completion function */
949 acb->bh_ret = ret;
950 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
951 qed_aio_complete_bh, acb);
952 qemu_bh_schedule(acb->bh);
954 /* Start next allocating write request waiting behind this one. Note that
955 * requests enqueue themselves when they first hit an unallocated cluster
956 * but they wait until the entire request is finished before waking up the
957 * next request in the queue. This ensures that we don't cycle through
958 * requests multiple times but rather finish one at a time completely.
960 if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
961 QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
962 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
963 if (acb) {
964 qed_aio_next_io(acb, 0);
965 } else if (s->header.features & QED_F_NEED_CHECK) {
966 qed_start_need_check_timer(s);
972 * Commit the current L2 table to the cache
974 static void qed_commit_l2_update(void *opaque, int ret)
976 QEDAIOCB *acb = opaque;
977 BDRVQEDState *s = acb_to_s(acb);
978 CachedL2Table *l2_table = acb->request.l2_table;
979 uint64_t l2_offset = l2_table->offset;
981 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
983 /* This is guaranteed to succeed because we just committed the entry to the
984 * cache.
986 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
987 assert(acb->request.l2_table != NULL);
989 qed_aio_next_io(opaque, ret);
993 * Update L1 table with new L2 table offset and write it out
995 static void qed_aio_write_l1_update(void *opaque, int ret)
997 QEDAIOCB *acb = opaque;
998 BDRVQEDState *s = acb_to_s(acb);
999 int index;
1001 if (ret) {
1002 qed_aio_complete(acb, ret);
1003 return;
1006 index = qed_l1_index(s, acb->cur_pos);
1007 s->l1_table->offsets[index] = acb->request.l2_table->offset;
1009 qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
1013 * Update L2 table with new cluster offsets and write them out
1015 static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset)
1017 BDRVQEDState *s = acb_to_s(acb);
1018 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
1019 int index;
1021 if (ret) {
1022 goto err;
1025 if (need_alloc) {
1026 qed_unref_l2_cache_entry(acb->request.l2_table);
1027 acb->request.l2_table = qed_new_l2_table(s);
1030 index = qed_l2_index(s, acb->cur_pos);
1031 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
1032 offset);
1034 if (need_alloc) {
1035 /* Write out the whole new L2 table */
1036 qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true,
1037 qed_aio_write_l1_update, acb);
1038 } else {
1039 /* Write out only the updated part of the L2 table */
1040 qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false,
1041 qed_aio_next_io, acb);
1043 return;
1045 err:
1046 qed_aio_complete(acb, ret);
1049 static void qed_aio_write_l2_update_cb(void *opaque, int ret)
1051 QEDAIOCB *acb = opaque;
1052 qed_aio_write_l2_update(acb, ret, acb->cur_cluster);
1056 * Flush new data clusters before updating the L2 table
1058 * This flush is necessary when a backing file is in use. A crash during an
1059 * allocating write could result in empty clusters in the image. If the write
1060 * only touched a subregion of the cluster, then backing image sectors have
1061 * been lost in the untouched region. The solution is to flush after writing a
1062 * new data cluster and before updating the L2 table.
1064 static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
1066 QEDAIOCB *acb = opaque;
1067 BDRVQEDState *s = acb_to_s(acb);
1069 if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) {
1070 qed_aio_complete(acb, -EIO);
1075 * Write data to the image file
1077 static void qed_aio_write_main(void *opaque, int ret)
1079 QEDAIOCB *acb = opaque;
1080 BDRVQEDState *s = acb_to_s(acb);
1081 uint64_t offset = acb->cur_cluster +
1082 qed_offset_into_cluster(s, acb->cur_pos);
1083 BlockCompletionFunc *next_fn;
1085 trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
1087 if (ret) {
1088 qed_aio_complete(acb, ret);
1089 return;
1092 if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
1093 next_fn = qed_aio_next_io;
1094 } else {
1095 if (s->bs->backing) {
1096 next_fn = qed_aio_write_flush_before_l2_update;
1097 } else {
1098 next_fn = qed_aio_write_l2_update_cb;
1102 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
1103 bdrv_aio_writev(s->bs->file->bs, offset / BDRV_SECTOR_SIZE,
1104 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1105 next_fn, acb);
1109 * Populate back untouched region of new data cluster
1111 static void qed_aio_write_postfill(void *opaque, int ret)
1113 QEDAIOCB *acb = opaque;
1114 BDRVQEDState *s = acb_to_s(acb);
1115 uint64_t start = acb->cur_pos + acb->cur_qiov.size;
1116 uint64_t len =
1117 qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1118 uint64_t offset = acb->cur_cluster +
1119 qed_offset_into_cluster(s, acb->cur_pos) +
1120 acb->cur_qiov.size;
1122 if (ret) {
1123 qed_aio_complete(acb, ret);
1124 return;
1127 trace_qed_aio_write_postfill(s, acb, start, len, offset);
1128 qed_copy_from_backing_file(s, start, len, offset,
1129 qed_aio_write_main, acb);
1133 * Populate front untouched region of new data cluster
1135 static void qed_aio_write_prefill(void *opaque, int ret)
1137 QEDAIOCB *acb = opaque;
1138 BDRVQEDState *s = acb_to_s(acb);
1139 uint64_t start = qed_start_of_cluster(s, acb->cur_pos);
1140 uint64_t len = qed_offset_into_cluster(s, acb->cur_pos);
1142 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1143 qed_copy_from_backing_file(s, start, len, acb->cur_cluster,
1144 qed_aio_write_postfill, acb);
1148 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1150 static bool qed_should_set_need_check(BDRVQEDState *s)
1152 /* The flush before L2 update path ensures consistency */
1153 if (s->bs->backing) {
1154 return false;
1157 return !(s->header.features & QED_F_NEED_CHECK);
1160 static void qed_aio_write_zero_cluster(void *opaque, int ret)
1162 QEDAIOCB *acb = opaque;
1164 if (ret) {
1165 qed_aio_complete(acb, ret);
1166 return;
1169 qed_aio_write_l2_update(acb, 0, 1);
1173 * Write new data cluster
1175 * @acb: Write request
1176 * @len: Length in bytes
1178 * This path is taken when writing to previously unallocated clusters.
1180 static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1182 BDRVQEDState *s = acb_to_s(acb);
1183 BlockCompletionFunc *cb;
1185 /* Cancel timer when the first allocating request comes in */
1186 if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) {
1187 qed_cancel_need_check_timer(s);
1190 /* Freeze this request if another allocating write is in progress */
1191 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
1192 QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
1194 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) ||
1195 s->allocating_write_reqs_plugged) {
1196 return; /* wait for existing request to finish */
1199 acb->cur_nclusters = qed_bytes_to_clusters(s,
1200 qed_offset_into_cluster(s, acb->cur_pos) + len);
1201 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1203 if (acb->flags & QED_AIOCB_ZERO) {
1204 /* Skip ahead if the clusters are already zero */
1205 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
1206 qed_aio_next_io(acb, 0);
1207 return;
1210 cb = qed_aio_write_zero_cluster;
1211 } else {
1212 cb = qed_aio_write_prefill;
1213 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1216 if (qed_should_set_need_check(s)) {
1217 s->header.features |= QED_F_NEED_CHECK;
1218 qed_write_header(s, cb, acb);
1219 } else {
1220 cb(acb, 0);
1225 * Write data cluster in place
1227 * @acb: Write request
1228 * @offset: Cluster offset in bytes
1229 * @len: Length in bytes
1231 * This path is taken when writing to already allocated clusters.
1233 static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1235 /* Allocate buffer for zero writes */
1236 if (acb->flags & QED_AIOCB_ZERO) {
1237 struct iovec *iov = acb->qiov->iov;
1239 if (!iov->iov_base) {
1240 iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len);
1241 if (iov->iov_base == NULL) {
1242 qed_aio_complete(acb, -ENOMEM);
1243 return;
1245 memset(iov->iov_base, 0, iov->iov_len);
1249 /* Calculate the I/O vector */
1250 acb->cur_cluster = offset;
1251 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1253 /* Do the actual write */
1254 qed_aio_write_main(acb, 0);
1258 * Write data cluster
1260 * @opaque: Write request
1261 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1262 * or -errno
1263 * @offset: Cluster offset in bytes
1264 * @len: Length in bytes
1266 * Callback from qed_find_cluster().
1268 static void qed_aio_write_data(void *opaque, int ret,
1269 uint64_t offset, size_t len)
1271 QEDAIOCB *acb = opaque;
1273 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1275 acb->find_cluster_ret = ret;
1277 switch (ret) {
1278 case QED_CLUSTER_FOUND:
1279 qed_aio_write_inplace(acb, offset, len);
1280 break;
1282 case QED_CLUSTER_L2:
1283 case QED_CLUSTER_L1:
1284 case QED_CLUSTER_ZERO:
1285 qed_aio_write_alloc(acb, len);
1286 break;
1288 default:
1289 qed_aio_complete(acb, ret);
1290 break;
1295 * Read data cluster
1297 * @opaque: Read request
1298 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1299 * or -errno
1300 * @offset: Cluster offset in bytes
1301 * @len: Length in bytes
1303 * Callback from qed_find_cluster().
1305 static void qed_aio_read_data(void *opaque, int ret,
1306 uint64_t offset, size_t len)
1308 QEDAIOCB *acb = opaque;
1309 BDRVQEDState *s = acb_to_s(acb);
1310 BlockDriverState *bs = acb->common.bs;
1312 /* Adjust offset into cluster */
1313 offset += qed_offset_into_cluster(s, acb->cur_pos);
1315 trace_qed_aio_read_data(s, acb, ret, offset, len);
1317 if (ret < 0) {
1318 goto err;
1321 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1323 /* Handle zero cluster and backing file reads */
1324 if (ret == QED_CLUSTER_ZERO) {
1325 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
1326 qed_aio_next_io(acb, 0);
1327 return;
1328 } else if (ret != QED_CLUSTER_FOUND) {
1329 qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
1330 &acb->backing_qiov, qed_aio_next_io, acb);
1331 return;
1334 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1335 bdrv_aio_readv(bs->file->bs, offset / BDRV_SECTOR_SIZE,
1336 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1337 qed_aio_next_io, acb);
1338 return;
1340 err:
1341 qed_aio_complete(acb, ret);
1345 * Begin next I/O or complete the request
1347 static void qed_aio_next_io(void *opaque, int ret)
1349 QEDAIOCB *acb = opaque;
1350 BDRVQEDState *s = acb_to_s(acb);
1351 QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ?
1352 qed_aio_write_data : qed_aio_read_data;
1354 trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
1356 if (acb->backing_qiov) {
1357 qemu_iovec_destroy(acb->backing_qiov);
1358 g_free(acb->backing_qiov);
1359 acb->backing_qiov = NULL;
1362 /* Handle I/O error */
1363 if (ret) {
1364 qed_aio_complete(acb, ret);
1365 return;
1368 acb->qiov_offset += acb->cur_qiov.size;
1369 acb->cur_pos += acb->cur_qiov.size;
1370 qemu_iovec_reset(&acb->cur_qiov);
1372 /* Complete request */
1373 if (acb->cur_pos >= acb->end_pos) {
1374 qed_aio_complete(acb, 0);
1375 return;
1378 /* Find next cluster and start I/O */
1379 qed_find_cluster(s, &acb->request,
1380 acb->cur_pos, acb->end_pos - acb->cur_pos,
1381 io_fn, acb);
1384 static BlockAIOCB *qed_aio_setup(BlockDriverState *bs,
1385 int64_t sector_num,
1386 QEMUIOVector *qiov, int nb_sectors,
1387 BlockCompletionFunc *cb,
1388 void *opaque, int flags)
1390 QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque);
1392 trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
1393 opaque, flags);
1395 acb->flags = flags;
1396 acb->qiov = qiov;
1397 acb->qiov_offset = 0;
1398 acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
1399 acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
1400 acb->backing_qiov = NULL;
1401 acb->request.l2_table = NULL;
1402 qemu_iovec_init(&acb->cur_qiov, qiov->niov);
1404 /* Start request */
1405 qed_aio_next_io(acb, 0);
1406 return &acb->common;
1409 static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
1410 int64_t sector_num,
1411 QEMUIOVector *qiov, int nb_sectors,
1412 BlockCompletionFunc *cb,
1413 void *opaque)
1415 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1418 static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
1419 int64_t sector_num,
1420 QEMUIOVector *qiov, int nb_sectors,
1421 BlockCompletionFunc *cb,
1422 void *opaque)
1424 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb,
1425 opaque, QED_AIOCB_WRITE);
1428 typedef struct {
1429 Coroutine *co;
1430 int ret;
1431 bool done;
1432 } QEDWriteZeroesCB;
1434 static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret)
1436 QEDWriteZeroesCB *cb = opaque;
1438 cb->done = true;
1439 cb->ret = ret;
1440 if (cb->co) {
1441 qemu_coroutine_enter(cb->co, NULL);
1445 static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs,
1446 int64_t sector_num,
1447 int nb_sectors,
1448 BdrvRequestFlags flags)
1450 BlockAIOCB *blockacb;
1451 BDRVQEDState *s = bs->opaque;
1452 QEDWriteZeroesCB cb = { .done = false };
1453 QEMUIOVector qiov;
1454 struct iovec iov;
1456 /* Refuse if there are untouched backing file sectors */
1457 if (bs->backing) {
1458 if (qed_offset_into_cluster(s, sector_num * BDRV_SECTOR_SIZE) != 0) {
1459 return -ENOTSUP;
1461 if (qed_offset_into_cluster(s, nb_sectors * BDRV_SECTOR_SIZE) != 0) {
1462 return -ENOTSUP;
1466 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1467 * then it will be allocated during request processing.
1469 iov.iov_base = NULL,
1470 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1472 qemu_iovec_init_external(&qiov, &iov, 1);
1473 blockacb = qed_aio_setup(bs, sector_num, &qiov, nb_sectors,
1474 qed_co_write_zeroes_cb, &cb,
1475 QED_AIOCB_WRITE | QED_AIOCB_ZERO);
1476 if (!blockacb) {
1477 return -EIO;
1479 if (!cb.done) {
1480 cb.co = qemu_coroutine_self();
1481 qemu_coroutine_yield();
1483 assert(cb.done);
1484 return cb.ret;
1487 static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset)
1489 BDRVQEDState *s = bs->opaque;
1490 uint64_t old_image_size;
1491 int ret;
1493 if (!qed_is_image_size_valid(offset, s->header.cluster_size,
1494 s->header.table_size)) {
1495 return -EINVAL;
1498 /* Shrinking is currently not supported */
1499 if ((uint64_t)offset < s->header.image_size) {
1500 return -ENOTSUP;
1503 old_image_size = s->header.image_size;
1504 s->header.image_size = offset;
1505 ret = qed_write_header_sync(s);
1506 if (ret < 0) {
1507 s->header.image_size = old_image_size;
1509 return ret;
1512 static int64_t bdrv_qed_getlength(BlockDriverState *bs)
1514 BDRVQEDState *s = bs->opaque;
1515 return s->header.image_size;
1518 static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1520 BDRVQEDState *s = bs->opaque;
1522 memset(bdi, 0, sizeof(*bdi));
1523 bdi->cluster_size = s->header.cluster_size;
1524 bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
1525 bdi->unallocated_blocks_are_zero = true;
1526 bdi->can_write_zeroes_with_unmap = true;
1527 return 0;
1530 static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1531 const char *backing_file,
1532 const char *backing_fmt)
1534 BDRVQEDState *s = bs->opaque;
1535 QEDHeader new_header, le_header;
1536 void *buffer;
1537 size_t buffer_len, backing_file_len;
1538 int ret;
1540 /* Refuse to set backing filename if unknown compat feature bits are
1541 * active. If the image uses an unknown compat feature then we may not
1542 * know the layout of data following the header structure and cannot safely
1543 * add a new string.
1545 if (backing_file && (s->header.compat_features &
1546 ~QED_COMPAT_FEATURE_MASK)) {
1547 return -ENOTSUP;
1550 memcpy(&new_header, &s->header, sizeof(new_header));
1552 new_header.features &= ~(QED_F_BACKING_FILE |
1553 QED_F_BACKING_FORMAT_NO_PROBE);
1555 /* Adjust feature flags */
1556 if (backing_file) {
1557 new_header.features |= QED_F_BACKING_FILE;
1559 if (qed_fmt_is_raw(backing_fmt)) {
1560 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1564 /* Calculate new header size */
1565 backing_file_len = 0;
1567 if (backing_file) {
1568 backing_file_len = strlen(backing_file);
1571 buffer_len = sizeof(new_header);
1572 new_header.backing_filename_offset = buffer_len;
1573 new_header.backing_filename_size = backing_file_len;
1574 buffer_len += backing_file_len;
1576 /* Make sure we can rewrite header without failing */
1577 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1578 return -ENOSPC;
1581 /* Prepare new header */
1582 buffer = g_malloc(buffer_len);
1584 qed_header_cpu_to_le(&new_header, &le_header);
1585 memcpy(buffer, &le_header, sizeof(le_header));
1586 buffer_len = sizeof(le_header);
1588 if (backing_file) {
1589 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1590 buffer_len += backing_file_len;
1593 /* Write new header */
1594 ret = bdrv_pwrite_sync(bs->file->bs, 0, buffer, buffer_len);
1595 g_free(buffer);
1596 if (ret == 0) {
1597 memcpy(&s->header, &new_header, sizeof(new_header));
1599 return ret;
1602 static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp)
1604 BDRVQEDState *s = bs->opaque;
1605 Error *local_err = NULL;
1606 int ret;
1608 bdrv_qed_close(bs);
1610 bdrv_invalidate_cache(bs->file->bs, &local_err);
1611 if (local_err) {
1612 error_propagate(errp, local_err);
1613 return;
1616 memset(s, 0, sizeof(BDRVQEDState));
1617 ret = bdrv_qed_open(bs, NULL, bs->open_flags, &local_err);
1618 if (local_err) {
1619 error_propagate(errp, local_err);
1620 error_prepend(errp, "Could not reopen qed layer: ");
1621 return;
1622 } else if (ret < 0) {
1623 error_setg_errno(errp, -ret, "Could not reopen qed layer");
1624 return;
1628 static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result,
1629 BdrvCheckMode fix)
1631 BDRVQEDState *s = bs->opaque;
1633 return qed_check(s, result, !!fix);
1636 static QemuOptsList qed_create_opts = {
1637 .name = "qed-create-opts",
1638 .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
1639 .desc = {
1641 .name = BLOCK_OPT_SIZE,
1642 .type = QEMU_OPT_SIZE,
1643 .help = "Virtual disk size"
1646 .name = BLOCK_OPT_BACKING_FILE,
1647 .type = QEMU_OPT_STRING,
1648 .help = "File name of a base image"
1651 .name = BLOCK_OPT_BACKING_FMT,
1652 .type = QEMU_OPT_STRING,
1653 .help = "Image format of the base image"
1656 .name = BLOCK_OPT_CLUSTER_SIZE,
1657 .type = QEMU_OPT_SIZE,
1658 .help = "Cluster size (in bytes)",
1659 .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE)
1662 .name = BLOCK_OPT_TABLE_SIZE,
1663 .type = QEMU_OPT_SIZE,
1664 .help = "L1/L2 table size (in clusters)"
1666 { /* end of list */ }
1670 static BlockDriver bdrv_qed = {
1671 .format_name = "qed",
1672 .instance_size = sizeof(BDRVQEDState),
1673 .create_opts = &qed_create_opts,
1674 .supports_backing = true,
1676 .bdrv_probe = bdrv_qed_probe,
1677 .bdrv_open = bdrv_qed_open,
1678 .bdrv_close = bdrv_qed_close,
1679 .bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
1680 .bdrv_create = bdrv_qed_create,
1681 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1682 .bdrv_co_get_block_status = bdrv_qed_co_get_block_status,
1683 .bdrv_aio_readv = bdrv_qed_aio_readv,
1684 .bdrv_aio_writev = bdrv_qed_aio_writev,
1685 .bdrv_co_write_zeroes = bdrv_qed_co_write_zeroes,
1686 .bdrv_truncate = bdrv_qed_truncate,
1687 .bdrv_getlength = bdrv_qed_getlength,
1688 .bdrv_get_info = bdrv_qed_get_info,
1689 .bdrv_refresh_limits = bdrv_qed_refresh_limits,
1690 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
1691 .bdrv_invalidate_cache = bdrv_qed_invalidate_cache,
1692 .bdrv_check = bdrv_qed_check,
1693 .bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
1694 .bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
1695 .bdrv_drain = bdrv_qed_drain,
1698 static void bdrv_qed_init(void)
1700 bdrv_register(&bdrv_qed);
1703 block_init(bdrv_qed_init);