char: serial: check divider value against baud base
[qemu.git] / block / qed.c
blob3ee879b52ed7f4294b4cefcaa7b725076acdef1b
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 "qapi/error.h"
17 #include "qemu/timer.h"
18 #include "qemu/bswap.h"
19 #include "trace.h"
20 #include "qed.h"
21 #include "qapi/qmp/qerror.h"
22 #include "migration/migration.h"
23 #include "sysemu/block-backend.h"
25 static const AIOCBInfo qed_aiocb_info = {
26 .aiocb_size = sizeof(QEDAIOCB),
29 static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
30 const char *filename)
32 const QEDHeader *header = (const QEDHeader *)buf;
34 if (buf_size < sizeof(*header)) {
35 return 0;
37 if (le32_to_cpu(header->magic) != QED_MAGIC) {
38 return 0;
40 return 100;
43 /**
44 * Check whether an image format is raw
46 * @fmt: Backing file format, may be NULL
48 static bool qed_fmt_is_raw(const char *fmt)
50 return fmt && strcmp(fmt, "raw") == 0;
53 static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
55 cpu->magic = le32_to_cpu(le->magic);
56 cpu->cluster_size = le32_to_cpu(le->cluster_size);
57 cpu->table_size = le32_to_cpu(le->table_size);
58 cpu->header_size = le32_to_cpu(le->header_size);
59 cpu->features = le64_to_cpu(le->features);
60 cpu->compat_features = le64_to_cpu(le->compat_features);
61 cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
62 cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
63 cpu->image_size = le64_to_cpu(le->image_size);
64 cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
65 cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
68 static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
70 le->magic = cpu_to_le32(cpu->magic);
71 le->cluster_size = cpu_to_le32(cpu->cluster_size);
72 le->table_size = cpu_to_le32(cpu->table_size);
73 le->header_size = cpu_to_le32(cpu->header_size);
74 le->features = cpu_to_le64(cpu->features);
75 le->compat_features = cpu_to_le64(cpu->compat_features);
76 le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
77 le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
78 le->image_size = cpu_to_le64(cpu->image_size);
79 le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
80 le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
83 int qed_write_header_sync(BDRVQEDState *s)
85 QEDHeader le;
86 int ret;
88 qed_header_cpu_to_le(&s->header, &le);
89 ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le));
90 if (ret != sizeof(le)) {
91 return ret;
93 return 0;
96 typedef struct {
97 GenericCB gencb;
98 BDRVQEDState *s;
99 struct iovec iov;
100 QEMUIOVector qiov;
101 int nsectors;
102 uint8_t *buf;
103 } QEDWriteHeaderCB;
105 static void qed_write_header_cb(void *opaque, int ret)
107 QEDWriteHeaderCB *write_header_cb = opaque;
109 qemu_vfree(write_header_cb->buf);
110 gencb_complete(write_header_cb, ret);
113 static void qed_write_header_read_cb(void *opaque, int ret)
115 QEDWriteHeaderCB *write_header_cb = opaque;
116 BDRVQEDState *s = write_header_cb->s;
118 if (ret) {
119 qed_write_header_cb(write_header_cb, ret);
120 return;
123 /* Update header */
124 qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf);
126 bdrv_aio_writev(s->bs->file, 0, &write_header_cb->qiov,
127 write_header_cb->nsectors, qed_write_header_cb,
128 write_header_cb);
132 * Update header in-place (does not rewrite backing filename or other strings)
134 * This function only updates known header fields in-place and does not affect
135 * extra data after the QED header.
137 static void qed_write_header(BDRVQEDState *s, BlockCompletionFunc cb,
138 void *opaque)
140 /* We must write full sectors for O_DIRECT but cannot necessarily generate
141 * the data following the header if an unrecognized compat feature is
142 * active. Therefore, first read the sectors containing the header, update
143 * them, and write back.
146 int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE);
147 size_t len = nsectors * BDRV_SECTOR_SIZE;
148 QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb),
149 cb, opaque);
151 write_header_cb->s = s;
152 write_header_cb->nsectors = nsectors;
153 write_header_cb->buf = qemu_blockalign(s->bs, len);
154 write_header_cb->iov.iov_base = write_header_cb->buf;
155 write_header_cb->iov.iov_len = len;
156 qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1);
158 bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors,
159 qed_write_header_read_cb, write_header_cb);
162 static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
164 uint64_t table_entries;
165 uint64_t l2_size;
167 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
168 l2_size = table_entries * cluster_size;
170 return l2_size * table_entries;
173 static bool qed_is_cluster_size_valid(uint32_t cluster_size)
175 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
176 cluster_size > QED_MAX_CLUSTER_SIZE) {
177 return false;
179 if (cluster_size & (cluster_size - 1)) {
180 return false; /* not power of 2 */
182 return true;
185 static bool qed_is_table_size_valid(uint32_t table_size)
187 if (table_size < QED_MIN_TABLE_SIZE ||
188 table_size > QED_MAX_TABLE_SIZE) {
189 return false;
191 if (table_size & (table_size - 1)) {
192 return false; /* not power of 2 */
194 return true;
197 static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
198 uint32_t table_size)
200 if (image_size % BDRV_SECTOR_SIZE != 0) {
201 return false; /* not multiple of sector size */
203 if (image_size > qed_max_image_size(cluster_size, table_size)) {
204 return false; /* image is too large */
206 return true;
210 * Read a string of known length from the image file
212 * @file: Image file
213 * @offset: File offset to start of string, in bytes
214 * @n: String length in bytes
215 * @buf: Destination buffer
216 * @buflen: Destination buffer length in bytes
217 * @ret: 0 on success, -errno on failure
219 * The string is NUL-terminated.
221 static int qed_read_string(BdrvChild *file, uint64_t offset, size_t n,
222 char *buf, size_t buflen)
224 int ret;
225 if (n >= buflen) {
226 return -EINVAL;
228 ret = bdrv_pread(file, offset, buf, n);
229 if (ret < 0) {
230 return ret;
232 buf[n] = '\0';
233 return 0;
237 * Allocate new clusters
239 * @s: QED state
240 * @n: Number of contiguous clusters to allocate
241 * @ret: Offset of first allocated cluster
243 * This function only produces the offset where the new clusters should be
244 * written. It updates BDRVQEDState but does not make any changes to the image
245 * file.
247 static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
249 uint64_t offset = s->file_size;
250 s->file_size += n * s->header.cluster_size;
251 return offset;
254 QEDTable *qed_alloc_table(BDRVQEDState *s)
256 /* Honor O_DIRECT memory alignment requirements */
257 return qemu_blockalign(s->bs,
258 s->header.cluster_size * s->header.table_size);
262 * Allocate a new zeroed L2 table
264 static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
266 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
268 l2_table->table = qed_alloc_table(s);
269 l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
271 memset(l2_table->table->offsets, 0,
272 s->header.cluster_size * s->header.table_size);
273 return l2_table;
276 static void qed_aio_next_io(void *opaque, int ret);
278 static void qed_plug_allocating_write_reqs(BDRVQEDState *s)
280 assert(!s->allocating_write_reqs_plugged);
282 s->allocating_write_reqs_plugged = true;
285 static void qed_unplug_allocating_write_reqs(BDRVQEDState *s)
287 QEDAIOCB *acb;
289 assert(s->allocating_write_reqs_plugged);
291 s->allocating_write_reqs_plugged = false;
293 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
294 if (acb) {
295 qed_aio_next_io(acb, 0);
299 static void qed_finish_clear_need_check(void *opaque, int ret)
301 /* Do nothing */
304 static void qed_flush_after_clear_need_check(void *opaque, int ret)
306 BDRVQEDState *s = opaque;
308 bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s);
310 /* No need to wait until flush completes */
311 qed_unplug_allocating_write_reqs(s);
314 static void qed_clear_need_check(void *opaque, int ret)
316 BDRVQEDState *s = opaque;
318 if (ret) {
319 qed_unplug_allocating_write_reqs(s);
320 return;
323 s->header.features &= ~QED_F_NEED_CHECK;
324 qed_write_header(s, qed_flush_after_clear_need_check, s);
327 static void qed_need_check_timer_cb(void *opaque)
329 BDRVQEDState *s = opaque;
331 /* The timer should only fire when allocating writes have drained */
332 assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs));
334 trace_qed_need_check_timer_cb(s);
336 qed_plug_allocating_write_reqs(s);
338 /* Ensure writes are on disk before clearing flag */
339 bdrv_aio_flush(s->bs, qed_clear_need_check, s);
342 static void qed_start_need_check_timer(BDRVQEDState *s)
344 trace_qed_start_need_check_timer(s);
346 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
347 * migration.
349 timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
350 NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT);
353 /* It's okay to call this multiple times or when no timer is started */
354 static void qed_cancel_need_check_timer(BDRVQEDState *s)
356 trace_qed_cancel_need_check_timer(s);
357 timer_del(s->need_check_timer);
360 static void bdrv_qed_detach_aio_context(BlockDriverState *bs)
362 BDRVQEDState *s = bs->opaque;
364 qed_cancel_need_check_timer(s);
365 timer_free(s->need_check_timer);
368 static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
369 AioContext *new_context)
371 BDRVQEDState *s = bs->opaque;
373 s->need_check_timer = aio_timer_new(new_context,
374 QEMU_CLOCK_VIRTUAL, SCALE_NS,
375 qed_need_check_timer_cb, s);
376 if (s->header.features & QED_F_NEED_CHECK) {
377 qed_start_need_check_timer(s);
381 static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
382 Error **errp)
384 BDRVQEDState *s = bs->opaque;
385 QEDHeader le_header;
386 int64_t file_size;
387 int ret;
389 s->bs = bs;
390 QSIMPLEQ_INIT(&s->allocating_write_reqs);
392 ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header));
393 if (ret < 0) {
394 return ret;
396 qed_header_le_to_cpu(&le_header, &s->header);
398 if (s->header.magic != QED_MAGIC) {
399 error_setg(errp, "Image not in QED format");
400 return -EINVAL;
402 if (s->header.features & ~QED_FEATURE_MASK) {
403 /* image uses unsupported feature bits */
404 error_setg(errp, "Unsupported QED features: %" PRIx64,
405 s->header.features & ~QED_FEATURE_MASK);
406 return -ENOTSUP;
408 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
409 return -EINVAL;
412 /* Round down file size to the last cluster */
413 file_size = bdrv_getlength(bs->file->bs);
414 if (file_size < 0) {
415 return file_size;
417 s->file_size = qed_start_of_cluster(s, file_size);
419 if (!qed_is_table_size_valid(s->header.table_size)) {
420 return -EINVAL;
422 if (!qed_is_image_size_valid(s->header.image_size,
423 s->header.cluster_size,
424 s->header.table_size)) {
425 return -EINVAL;
427 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
428 return -EINVAL;
431 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
432 sizeof(uint64_t);
433 s->l2_shift = ctz32(s->header.cluster_size);
434 s->l2_mask = s->table_nelems - 1;
435 s->l1_shift = s->l2_shift + ctz32(s->table_nelems);
437 /* Header size calculation must not overflow uint32_t */
438 if (s->header.header_size > UINT32_MAX / s->header.cluster_size) {
439 return -EINVAL;
442 if ((s->header.features & QED_F_BACKING_FILE)) {
443 if ((uint64_t)s->header.backing_filename_offset +
444 s->header.backing_filename_size >
445 s->header.cluster_size * s->header.header_size) {
446 return -EINVAL;
449 ret = qed_read_string(bs->file, s->header.backing_filename_offset,
450 s->header.backing_filename_size, bs->backing_file,
451 sizeof(bs->backing_file));
452 if (ret < 0) {
453 return ret;
456 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
457 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
461 /* Reset unknown autoclear feature bits. This is a backwards
462 * compatibility mechanism that allows images to be opened by older
463 * programs, which "knock out" unknown feature bits. When an image is
464 * opened by a newer program again it can detect that the autoclear
465 * feature is no longer valid.
467 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
468 !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) {
469 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
471 ret = qed_write_header_sync(s);
472 if (ret) {
473 return ret;
476 /* From here on only known autoclear feature bits are valid */
477 bdrv_flush(bs->file->bs);
480 s->l1_table = qed_alloc_table(s);
481 qed_init_l2_cache(&s->l2_cache);
483 ret = qed_read_l1_table_sync(s);
484 if (ret) {
485 goto out;
488 /* If image was not closed cleanly, check consistency */
489 if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
490 /* Read-only images cannot be fixed. There is no risk of corruption
491 * since write operations are not possible. Therefore, allow
492 * potentially inconsistent images to be opened read-only. This can
493 * aid data recovery from an otherwise inconsistent image.
495 if (!bdrv_is_read_only(bs->file->bs) &&
496 !(flags & BDRV_O_INACTIVE)) {
497 BdrvCheckResult result = {0};
499 ret = qed_check(s, &result, true);
500 if (ret) {
501 goto out;
506 bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
508 out:
509 if (ret) {
510 qed_free_l2_cache(&s->l2_cache);
511 qemu_vfree(s->l1_table);
513 return ret;
516 static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
518 BDRVQEDState *s = bs->opaque;
520 bs->bl.pwrite_zeroes_alignment = s->header.cluster_size;
523 /* We have nothing to do for QED reopen, stubs just return
524 * success */
525 static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
526 BlockReopenQueue *queue, Error **errp)
528 return 0;
531 static void bdrv_qed_close(BlockDriverState *bs)
533 BDRVQEDState *s = bs->opaque;
535 bdrv_qed_detach_aio_context(bs);
537 /* Ensure writes reach stable storage */
538 bdrv_flush(bs->file->bs);
540 /* Clean shutdown, no check required on next open */
541 if (s->header.features & QED_F_NEED_CHECK) {
542 s->header.features &= ~QED_F_NEED_CHECK;
543 qed_write_header_sync(s);
546 qed_free_l2_cache(&s->l2_cache);
547 qemu_vfree(s->l1_table);
550 static int qed_create(const char *filename, uint32_t cluster_size,
551 uint64_t image_size, uint32_t table_size,
552 const char *backing_file, const char *backing_fmt,
553 QemuOpts *opts, Error **errp)
555 QEDHeader header = {
556 .magic = QED_MAGIC,
557 .cluster_size = cluster_size,
558 .table_size = table_size,
559 .header_size = 1,
560 .features = 0,
561 .compat_features = 0,
562 .l1_table_offset = cluster_size,
563 .image_size = image_size,
565 QEDHeader le_header;
566 uint8_t *l1_table = NULL;
567 size_t l1_size = header.cluster_size * header.table_size;
568 Error *local_err = NULL;
569 int ret = 0;
570 BlockBackend *blk;
572 ret = bdrv_create_file(filename, opts, &local_err);
573 if (ret < 0) {
574 error_propagate(errp, local_err);
575 return ret;
578 blk = blk_new_open(filename, NULL, NULL,
579 BDRV_O_RDWR | BDRV_O_PROTOCOL, &local_err);
580 if (blk == NULL) {
581 error_propagate(errp, local_err);
582 return -EIO;
585 blk_set_allow_write_beyond_eof(blk, true);
587 /* File must start empty and grow, check truncate is supported */
588 ret = blk_truncate(blk, 0);
589 if (ret < 0) {
590 goto out;
593 if (backing_file) {
594 header.features |= QED_F_BACKING_FILE;
595 header.backing_filename_offset = sizeof(le_header);
596 header.backing_filename_size = strlen(backing_file);
598 if (qed_fmt_is_raw(backing_fmt)) {
599 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
603 qed_header_cpu_to_le(&header, &le_header);
604 ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0);
605 if (ret < 0) {
606 goto out;
608 ret = blk_pwrite(blk, sizeof(le_header), backing_file,
609 header.backing_filename_size, 0);
610 if (ret < 0) {
611 goto out;
614 l1_table = g_malloc0(l1_size);
615 ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0);
616 if (ret < 0) {
617 goto out;
620 ret = 0; /* success */
621 out:
622 g_free(l1_table);
623 blk_unref(blk);
624 return ret;
627 static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp)
629 uint64_t image_size = 0;
630 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
631 uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
632 char *backing_file = NULL;
633 char *backing_fmt = NULL;
634 int ret;
636 image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
637 BDRV_SECTOR_SIZE);
638 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
639 backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
640 cluster_size = qemu_opt_get_size_del(opts,
641 BLOCK_OPT_CLUSTER_SIZE,
642 QED_DEFAULT_CLUSTER_SIZE);
643 table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE,
644 QED_DEFAULT_TABLE_SIZE);
646 if (!qed_is_cluster_size_valid(cluster_size)) {
647 error_setg(errp, "QED cluster size must be within range [%u, %u] "
648 "and power of 2",
649 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
650 ret = -EINVAL;
651 goto finish;
653 if (!qed_is_table_size_valid(table_size)) {
654 error_setg(errp, "QED table size must be within range [%u, %u] "
655 "and power of 2",
656 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
657 ret = -EINVAL;
658 goto finish;
660 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
661 error_setg(errp, "QED image size must be a non-zero multiple of "
662 "cluster size and less than %" PRIu64 " bytes",
663 qed_max_image_size(cluster_size, table_size));
664 ret = -EINVAL;
665 goto finish;
668 ret = qed_create(filename, cluster_size, image_size, table_size,
669 backing_file, backing_fmt, opts, errp);
671 finish:
672 g_free(backing_file);
673 g_free(backing_fmt);
674 return ret;
677 typedef struct {
678 BlockDriverState *bs;
679 Coroutine *co;
680 uint64_t pos;
681 int64_t status;
682 int *pnum;
683 BlockDriverState **file;
684 } QEDIsAllocatedCB;
686 static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
688 QEDIsAllocatedCB *cb = opaque;
689 BDRVQEDState *s = cb->bs->opaque;
690 *cb->pnum = len / BDRV_SECTOR_SIZE;
691 switch (ret) {
692 case QED_CLUSTER_FOUND:
693 offset |= qed_offset_into_cluster(s, cb->pos);
694 cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
695 *cb->file = cb->bs->file->bs;
696 break;
697 case QED_CLUSTER_ZERO:
698 cb->status = BDRV_BLOCK_ZERO;
699 break;
700 case QED_CLUSTER_L2:
701 case QED_CLUSTER_L1:
702 cb->status = 0;
703 break;
704 default:
705 assert(ret < 0);
706 cb->status = ret;
707 break;
710 if (cb->co) {
711 qemu_coroutine_enter(cb->co);
715 static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs,
716 int64_t sector_num,
717 int nb_sectors, int *pnum,
718 BlockDriverState **file)
720 BDRVQEDState *s = bs->opaque;
721 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
722 QEDIsAllocatedCB cb = {
723 .bs = bs,
724 .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE,
725 .status = BDRV_BLOCK_OFFSET_MASK,
726 .pnum = pnum,
727 .file = file,
729 QEDRequest request = { .l2_table = NULL };
731 qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb);
733 /* Now sleep if the callback wasn't invoked immediately */
734 while (cb.status == BDRV_BLOCK_OFFSET_MASK) {
735 cb.co = qemu_coroutine_self();
736 qemu_coroutine_yield();
739 qed_unref_l2_cache_entry(request.l2_table);
741 return cb.status;
744 static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
746 return acb->common.bs->opaque;
750 * Read from the backing file or zero-fill if no backing file
752 * @s: QED state
753 * @pos: Byte position in device
754 * @qiov: Destination I/O vector
755 * @backing_qiov: Possibly shortened copy of qiov, to be allocated here
756 * @cb: Completion function
757 * @opaque: User data for completion function
759 * This function reads qiov->size bytes starting at pos from the backing file.
760 * If there is no backing file then zeroes are read.
762 static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
763 QEMUIOVector *qiov,
764 QEMUIOVector **backing_qiov,
765 BlockCompletionFunc *cb, void *opaque)
767 uint64_t backing_length = 0;
768 size_t size;
770 /* If there is a backing file, get its length. Treat the absence of a
771 * backing file like a zero length backing file.
773 if (s->bs->backing) {
774 int64_t l = bdrv_getlength(s->bs->backing->bs);
775 if (l < 0) {
776 cb(opaque, l);
777 return;
779 backing_length = l;
782 /* Zero all sectors if reading beyond the end of the backing file */
783 if (pos >= backing_length ||
784 pos + qiov->size > backing_length) {
785 qemu_iovec_memset(qiov, 0, 0, qiov->size);
788 /* Complete now if there are no backing file sectors to read */
789 if (pos >= backing_length) {
790 cb(opaque, 0);
791 return;
794 /* If the read straddles the end of the backing file, shorten it */
795 size = MIN((uint64_t)backing_length - pos, qiov->size);
797 assert(*backing_qiov == NULL);
798 *backing_qiov = g_new(QEMUIOVector, 1);
799 qemu_iovec_init(*backing_qiov, qiov->niov);
800 qemu_iovec_concat(*backing_qiov, qiov, 0, size);
802 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
803 bdrv_aio_readv(s->bs->backing, pos / BDRV_SECTOR_SIZE,
804 *backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
807 typedef struct {
808 GenericCB gencb;
809 BDRVQEDState *s;
810 QEMUIOVector qiov;
811 QEMUIOVector *backing_qiov;
812 struct iovec iov;
813 uint64_t offset;
814 } CopyFromBackingFileCB;
816 static void qed_copy_from_backing_file_cb(void *opaque, int ret)
818 CopyFromBackingFileCB *copy_cb = opaque;
819 qemu_vfree(copy_cb->iov.iov_base);
820 gencb_complete(&copy_cb->gencb, ret);
823 static void qed_copy_from_backing_file_write(void *opaque, int ret)
825 CopyFromBackingFileCB *copy_cb = opaque;
826 BDRVQEDState *s = copy_cb->s;
828 if (copy_cb->backing_qiov) {
829 qemu_iovec_destroy(copy_cb->backing_qiov);
830 g_free(copy_cb->backing_qiov);
831 copy_cb->backing_qiov = NULL;
834 if (ret) {
835 qed_copy_from_backing_file_cb(copy_cb, ret);
836 return;
839 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
840 bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE,
841 &copy_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE,
842 qed_copy_from_backing_file_cb, copy_cb);
846 * Copy data from backing file into the image
848 * @s: QED state
849 * @pos: Byte position in device
850 * @len: Number of bytes
851 * @offset: Byte offset in image file
852 * @cb: Completion function
853 * @opaque: User data for completion function
855 static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
856 uint64_t len, uint64_t offset,
857 BlockCompletionFunc *cb,
858 void *opaque)
860 CopyFromBackingFileCB *copy_cb;
862 /* Skip copy entirely if there is no work to do */
863 if (len == 0) {
864 cb(opaque, 0);
865 return;
868 copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
869 copy_cb->s = s;
870 copy_cb->offset = offset;
871 copy_cb->backing_qiov = NULL;
872 copy_cb->iov.iov_base = qemu_blockalign(s->bs, len);
873 copy_cb->iov.iov_len = len;
874 qemu_iovec_init_external(&copy_cb->qiov, &copy_cb->iov, 1);
876 qed_read_backing_file(s, pos, &copy_cb->qiov, &copy_cb->backing_qiov,
877 qed_copy_from_backing_file_write, copy_cb);
881 * Link one or more contiguous clusters into a table
883 * @s: QED state
884 * @table: L2 table
885 * @index: First cluster index
886 * @n: Number of contiguous clusters
887 * @cluster: First cluster offset
889 * The cluster offset may be an allocated byte offset in the image file, the
890 * zero cluster marker, or the unallocated cluster marker.
892 static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
893 unsigned int n, uint64_t cluster)
895 int i;
896 for (i = index; i < index + n; i++) {
897 table->offsets[i] = cluster;
898 if (!qed_offset_is_unalloc_cluster(cluster) &&
899 !qed_offset_is_zero_cluster(cluster)) {
900 cluster += s->header.cluster_size;
905 static void qed_aio_complete_bh(void *opaque)
907 QEDAIOCB *acb = opaque;
908 BlockCompletionFunc *cb = acb->common.cb;
909 void *user_opaque = acb->common.opaque;
910 int ret = acb->bh_ret;
912 qemu_aio_unref(acb);
914 /* Invoke callback */
915 cb(user_opaque, ret);
918 static void qed_aio_complete(QEDAIOCB *acb, int ret)
920 BDRVQEDState *s = acb_to_s(acb);
922 trace_qed_aio_complete(s, acb, ret);
924 /* Free resources */
925 qemu_iovec_destroy(&acb->cur_qiov);
926 qed_unref_l2_cache_entry(acb->request.l2_table);
928 /* Free the buffer we may have allocated for zero writes */
929 if (acb->flags & QED_AIOCB_ZERO) {
930 qemu_vfree(acb->qiov->iov[0].iov_base);
931 acb->qiov->iov[0].iov_base = NULL;
934 /* Arrange for a bh to invoke the completion function */
935 acb->bh_ret = ret;
936 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
937 qed_aio_complete_bh, acb);
939 /* Start next allocating write request waiting behind this one. Note that
940 * requests enqueue themselves when they first hit an unallocated cluster
941 * but they wait until the entire request is finished before waking up the
942 * next request in the queue. This ensures that we don't cycle through
943 * requests multiple times but rather finish one at a time completely.
945 if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
946 QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
947 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
948 if (acb) {
949 qed_aio_next_io(acb, 0);
950 } else if (s->header.features & QED_F_NEED_CHECK) {
951 qed_start_need_check_timer(s);
957 * Commit the current L2 table to the cache
959 static void qed_commit_l2_update(void *opaque, int ret)
961 QEDAIOCB *acb = opaque;
962 BDRVQEDState *s = acb_to_s(acb);
963 CachedL2Table *l2_table = acb->request.l2_table;
964 uint64_t l2_offset = l2_table->offset;
966 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
968 /* This is guaranteed to succeed because we just committed the entry to the
969 * cache.
971 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
972 assert(acb->request.l2_table != NULL);
974 qed_aio_next_io(opaque, ret);
978 * Update L1 table with new L2 table offset and write it out
980 static void qed_aio_write_l1_update(void *opaque, int ret)
982 QEDAIOCB *acb = opaque;
983 BDRVQEDState *s = acb_to_s(acb);
984 int index;
986 if (ret) {
987 qed_aio_complete(acb, ret);
988 return;
991 index = qed_l1_index(s, acb->cur_pos);
992 s->l1_table->offsets[index] = acb->request.l2_table->offset;
994 qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
998 * Update L2 table with new cluster offsets and write them out
1000 static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset)
1002 BDRVQEDState *s = acb_to_s(acb);
1003 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
1004 int index;
1006 if (ret) {
1007 goto err;
1010 if (need_alloc) {
1011 qed_unref_l2_cache_entry(acb->request.l2_table);
1012 acb->request.l2_table = qed_new_l2_table(s);
1015 index = qed_l2_index(s, acb->cur_pos);
1016 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
1017 offset);
1019 if (need_alloc) {
1020 /* Write out the whole new L2 table */
1021 qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true,
1022 qed_aio_write_l1_update, acb);
1023 } else {
1024 /* Write out only the updated part of the L2 table */
1025 qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false,
1026 qed_aio_next_io, acb);
1028 return;
1030 err:
1031 qed_aio_complete(acb, ret);
1034 static void qed_aio_write_l2_update_cb(void *opaque, int ret)
1036 QEDAIOCB *acb = opaque;
1037 qed_aio_write_l2_update(acb, ret, acb->cur_cluster);
1041 * Flush new data clusters before updating the L2 table
1043 * This flush is necessary when a backing file is in use. A crash during an
1044 * allocating write could result in empty clusters in the image. If the write
1045 * only touched a subregion of the cluster, then backing image sectors have
1046 * been lost in the untouched region. The solution is to flush after writing a
1047 * new data cluster and before updating the L2 table.
1049 static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
1051 QEDAIOCB *acb = opaque;
1052 BDRVQEDState *s = acb_to_s(acb);
1054 if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) {
1055 qed_aio_complete(acb, -EIO);
1060 * Write data to the image file
1062 static void qed_aio_write_main(void *opaque, int ret)
1064 QEDAIOCB *acb = opaque;
1065 BDRVQEDState *s = acb_to_s(acb);
1066 uint64_t offset = acb->cur_cluster +
1067 qed_offset_into_cluster(s, acb->cur_pos);
1068 BlockCompletionFunc *next_fn;
1070 trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
1072 if (ret) {
1073 qed_aio_complete(acb, ret);
1074 return;
1077 if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
1078 next_fn = qed_aio_next_io;
1079 } else {
1080 if (s->bs->backing) {
1081 next_fn = qed_aio_write_flush_before_l2_update;
1082 } else {
1083 next_fn = qed_aio_write_l2_update_cb;
1087 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
1088 bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE,
1089 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1090 next_fn, acb);
1094 * Populate back untouched region of new data cluster
1096 static void qed_aio_write_postfill(void *opaque, int ret)
1098 QEDAIOCB *acb = opaque;
1099 BDRVQEDState *s = acb_to_s(acb);
1100 uint64_t start = acb->cur_pos + acb->cur_qiov.size;
1101 uint64_t len =
1102 qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1103 uint64_t offset = acb->cur_cluster +
1104 qed_offset_into_cluster(s, acb->cur_pos) +
1105 acb->cur_qiov.size;
1107 if (ret) {
1108 qed_aio_complete(acb, ret);
1109 return;
1112 trace_qed_aio_write_postfill(s, acb, start, len, offset);
1113 qed_copy_from_backing_file(s, start, len, offset,
1114 qed_aio_write_main, acb);
1118 * Populate front untouched region of new data cluster
1120 static void qed_aio_write_prefill(void *opaque, int ret)
1122 QEDAIOCB *acb = opaque;
1123 BDRVQEDState *s = acb_to_s(acb);
1124 uint64_t start = qed_start_of_cluster(s, acb->cur_pos);
1125 uint64_t len = qed_offset_into_cluster(s, acb->cur_pos);
1127 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1128 qed_copy_from_backing_file(s, start, len, acb->cur_cluster,
1129 qed_aio_write_postfill, acb);
1133 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1135 static bool qed_should_set_need_check(BDRVQEDState *s)
1137 /* The flush before L2 update path ensures consistency */
1138 if (s->bs->backing) {
1139 return false;
1142 return !(s->header.features & QED_F_NEED_CHECK);
1145 static void qed_aio_write_zero_cluster(void *opaque, int ret)
1147 QEDAIOCB *acb = opaque;
1149 if (ret) {
1150 qed_aio_complete(acb, ret);
1151 return;
1154 qed_aio_write_l2_update(acb, 0, 1);
1158 * Write new data cluster
1160 * @acb: Write request
1161 * @len: Length in bytes
1163 * This path is taken when writing to previously unallocated clusters.
1165 static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1167 BDRVQEDState *s = acb_to_s(acb);
1168 BlockCompletionFunc *cb;
1170 /* Cancel timer when the first allocating request comes in */
1171 if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) {
1172 qed_cancel_need_check_timer(s);
1175 /* Freeze this request if another allocating write is in progress */
1176 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
1177 QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
1179 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) ||
1180 s->allocating_write_reqs_plugged) {
1181 return; /* wait for existing request to finish */
1184 acb->cur_nclusters = qed_bytes_to_clusters(s,
1185 qed_offset_into_cluster(s, acb->cur_pos) + len);
1186 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1188 if (acb->flags & QED_AIOCB_ZERO) {
1189 /* Skip ahead if the clusters are already zero */
1190 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
1191 qed_aio_next_io(acb, 0);
1192 return;
1195 cb = qed_aio_write_zero_cluster;
1196 } else {
1197 cb = qed_aio_write_prefill;
1198 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1201 if (qed_should_set_need_check(s)) {
1202 s->header.features |= QED_F_NEED_CHECK;
1203 qed_write_header(s, cb, acb);
1204 } else {
1205 cb(acb, 0);
1210 * Write data cluster in place
1212 * @acb: Write request
1213 * @offset: Cluster offset in bytes
1214 * @len: Length in bytes
1216 * This path is taken when writing to already allocated clusters.
1218 static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1220 /* Allocate buffer for zero writes */
1221 if (acb->flags & QED_AIOCB_ZERO) {
1222 struct iovec *iov = acb->qiov->iov;
1224 if (!iov->iov_base) {
1225 iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len);
1226 if (iov->iov_base == NULL) {
1227 qed_aio_complete(acb, -ENOMEM);
1228 return;
1230 memset(iov->iov_base, 0, iov->iov_len);
1234 /* Calculate the I/O vector */
1235 acb->cur_cluster = offset;
1236 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1238 /* Do the actual write */
1239 qed_aio_write_main(acb, 0);
1243 * Write data cluster
1245 * @opaque: Write request
1246 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1247 * or -errno
1248 * @offset: Cluster offset in bytes
1249 * @len: Length in bytes
1251 * Callback from qed_find_cluster().
1253 static void qed_aio_write_data(void *opaque, int ret,
1254 uint64_t offset, size_t len)
1256 QEDAIOCB *acb = opaque;
1258 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1260 acb->find_cluster_ret = ret;
1262 switch (ret) {
1263 case QED_CLUSTER_FOUND:
1264 qed_aio_write_inplace(acb, offset, len);
1265 break;
1267 case QED_CLUSTER_L2:
1268 case QED_CLUSTER_L1:
1269 case QED_CLUSTER_ZERO:
1270 qed_aio_write_alloc(acb, len);
1271 break;
1273 default:
1274 qed_aio_complete(acb, ret);
1275 break;
1280 * Read data cluster
1282 * @opaque: Read request
1283 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1284 * or -errno
1285 * @offset: Cluster offset in bytes
1286 * @len: Length in bytes
1288 * Callback from qed_find_cluster().
1290 static void qed_aio_read_data(void *opaque, int ret,
1291 uint64_t offset, size_t len)
1293 QEDAIOCB *acb = opaque;
1294 BDRVQEDState *s = acb_to_s(acb);
1295 BlockDriverState *bs = acb->common.bs;
1297 /* Adjust offset into cluster */
1298 offset += qed_offset_into_cluster(s, acb->cur_pos);
1300 trace_qed_aio_read_data(s, acb, ret, offset, len);
1302 if (ret < 0) {
1303 goto err;
1306 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1308 /* Handle zero cluster and backing file reads */
1309 if (ret == QED_CLUSTER_ZERO) {
1310 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
1311 qed_aio_next_io(acb, 0);
1312 return;
1313 } else if (ret != QED_CLUSTER_FOUND) {
1314 qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
1315 &acb->backing_qiov, qed_aio_next_io, acb);
1316 return;
1319 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1320 bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE,
1321 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1322 qed_aio_next_io, acb);
1323 return;
1325 err:
1326 qed_aio_complete(acb, ret);
1330 * Begin next I/O or complete the request
1332 static void qed_aio_next_io(void *opaque, int ret)
1334 QEDAIOCB *acb = opaque;
1335 BDRVQEDState *s = acb_to_s(acb);
1336 QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ?
1337 qed_aio_write_data : qed_aio_read_data;
1339 trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
1341 if (acb->backing_qiov) {
1342 qemu_iovec_destroy(acb->backing_qiov);
1343 g_free(acb->backing_qiov);
1344 acb->backing_qiov = NULL;
1347 /* Handle I/O error */
1348 if (ret) {
1349 qed_aio_complete(acb, ret);
1350 return;
1353 acb->qiov_offset += acb->cur_qiov.size;
1354 acb->cur_pos += acb->cur_qiov.size;
1355 qemu_iovec_reset(&acb->cur_qiov);
1357 /* Complete request */
1358 if (acb->cur_pos >= acb->end_pos) {
1359 qed_aio_complete(acb, 0);
1360 return;
1363 /* Find next cluster and start I/O */
1364 qed_find_cluster(s, &acb->request,
1365 acb->cur_pos, acb->end_pos - acb->cur_pos,
1366 io_fn, acb);
1369 static BlockAIOCB *qed_aio_setup(BlockDriverState *bs,
1370 int64_t sector_num,
1371 QEMUIOVector *qiov, int nb_sectors,
1372 BlockCompletionFunc *cb,
1373 void *opaque, int flags)
1375 QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque);
1377 trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
1378 opaque, flags);
1380 acb->flags = flags;
1381 acb->qiov = qiov;
1382 acb->qiov_offset = 0;
1383 acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
1384 acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
1385 acb->backing_qiov = NULL;
1386 acb->request.l2_table = NULL;
1387 qemu_iovec_init(&acb->cur_qiov, qiov->niov);
1389 /* Start request */
1390 qed_aio_next_io(acb, 0);
1391 return &acb->common;
1394 static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
1395 int64_t sector_num,
1396 QEMUIOVector *qiov, int nb_sectors,
1397 BlockCompletionFunc *cb,
1398 void *opaque)
1400 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1403 static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
1404 int64_t sector_num,
1405 QEMUIOVector *qiov, int nb_sectors,
1406 BlockCompletionFunc *cb,
1407 void *opaque)
1409 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb,
1410 opaque, QED_AIOCB_WRITE);
1413 typedef struct {
1414 Coroutine *co;
1415 int ret;
1416 bool done;
1417 } QEDWriteZeroesCB;
1419 static void coroutine_fn qed_co_pwrite_zeroes_cb(void *opaque, int ret)
1421 QEDWriteZeroesCB *cb = opaque;
1423 cb->done = true;
1424 cb->ret = ret;
1425 if (cb->co) {
1426 qemu_coroutine_enter(cb->co);
1430 static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs,
1431 int64_t offset,
1432 int count,
1433 BdrvRequestFlags flags)
1435 BlockAIOCB *blockacb;
1436 BDRVQEDState *s = bs->opaque;
1437 QEDWriteZeroesCB cb = { .done = false };
1438 QEMUIOVector qiov;
1439 struct iovec iov;
1441 /* Fall back if the request is not aligned */
1442 if (qed_offset_into_cluster(s, offset) ||
1443 qed_offset_into_cluster(s, count)) {
1444 return -ENOTSUP;
1447 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1448 * then it will be allocated during request processing.
1450 iov.iov_base = NULL;
1451 iov.iov_len = count;
1453 qemu_iovec_init_external(&qiov, &iov, 1);
1454 blockacb = qed_aio_setup(bs, offset >> BDRV_SECTOR_BITS, &qiov,
1455 count >> BDRV_SECTOR_BITS,
1456 qed_co_pwrite_zeroes_cb, &cb,
1457 QED_AIOCB_WRITE | QED_AIOCB_ZERO);
1458 if (!blockacb) {
1459 return -EIO;
1461 if (!cb.done) {
1462 cb.co = qemu_coroutine_self();
1463 qemu_coroutine_yield();
1465 assert(cb.done);
1466 return cb.ret;
1469 static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset)
1471 BDRVQEDState *s = bs->opaque;
1472 uint64_t old_image_size;
1473 int ret;
1475 if (!qed_is_image_size_valid(offset, s->header.cluster_size,
1476 s->header.table_size)) {
1477 return -EINVAL;
1480 /* Shrinking is currently not supported */
1481 if ((uint64_t)offset < s->header.image_size) {
1482 return -ENOTSUP;
1485 old_image_size = s->header.image_size;
1486 s->header.image_size = offset;
1487 ret = qed_write_header_sync(s);
1488 if (ret < 0) {
1489 s->header.image_size = old_image_size;
1491 return ret;
1494 static int64_t bdrv_qed_getlength(BlockDriverState *bs)
1496 BDRVQEDState *s = bs->opaque;
1497 return s->header.image_size;
1500 static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1502 BDRVQEDState *s = bs->opaque;
1504 memset(bdi, 0, sizeof(*bdi));
1505 bdi->cluster_size = s->header.cluster_size;
1506 bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
1507 bdi->unallocated_blocks_are_zero = true;
1508 bdi->can_write_zeroes_with_unmap = true;
1509 return 0;
1512 static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1513 const char *backing_file,
1514 const char *backing_fmt)
1516 BDRVQEDState *s = bs->opaque;
1517 QEDHeader new_header, le_header;
1518 void *buffer;
1519 size_t buffer_len, backing_file_len;
1520 int ret;
1522 /* Refuse to set backing filename if unknown compat feature bits are
1523 * active. If the image uses an unknown compat feature then we may not
1524 * know the layout of data following the header structure and cannot safely
1525 * add a new string.
1527 if (backing_file && (s->header.compat_features &
1528 ~QED_COMPAT_FEATURE_MASK)) {
1529 return -ENOTSUP;
1532 memcpy(&new_header, &s->header, sizeof(new_header));
1534 new_header.features &= ~(QED_F_BACKING_FILE |
1535 QED_F_BACKING_FORMAT_NO_PROBE);
1537 /* Adjust feature flags */
1538 if (backing_file) {
1539 new_header.features |= QED_F_BACKING_FILE;
1541 if (qed_fmt_is_raw(backing_fmt)) {
1542 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1546 /* Calculate new header size */
1547 backing_file_len = 0;
1549 if (backing_file) {
1550 backing_file_len = strlen(backing_file);
1553 buffer_len = sizeof(new_header);
1554 new_header.backing_filename_offset = buffer_len;
1555 new_header.backing_filename_size = backing_file_len;
1556 buffer_len += backing_file_len;
1558 /* Make sure we can rewrite header without failing */
1559 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1560 return -ENOSPC;
1563 /* Prepare new header */
1564 buffer = g_malloc(buffer_len);
1566 qed_header_cpu_to_le(&new_header, &le_header);
1567 memcpy(buffer, &le_header, sizeof(le_header));
1568 buffer_len = sizeof(le_header);
1570 if (backing_file) {
1571 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1572 buffer_len += backing_file_len;
1575 /* Write new header */
1576 ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len);
1577 g_free(buffer);
1578 if (ret == 0) {
1579 memcpy(&s->header, &new_header, sizeof(new_header));
1581 return ret;
1584 static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp)
1586 BDRVQEDState *s = bs->opaque;
1587 Error *local_err = NULL;
1588 int ret;
1590 bdrv_qed_close(bs);
1592 memset(s, 0, sizeof(BDRVQEDState));
1593 ret = bdrv_qed_open(bs, NULL, bs->open_flags, &local_err);
1594 if (local_err) {
1595 error_propagate(errp, local_err);
1596 error_prepend(errp, "Could not reopen qed layer: ");
1597 return;
1598 } else if (ret < 0) {
1599 error_setg_errno(errp, -ret, "Could not reopen qed layer");
1600 return;
1604 static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result,
1605 BdrvCheckMode fix)
1607 BDRVQEDState *s = bs->opaque;
1609 return qed_check(s, result, !!fix);
1612 static QemuOptsList qed_create_opts = {
1613 .name = "qed-create-opts",
1614 .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
1615 .desc = {
1617 .name = BLOCK_OPT_SIZE,
1618 .type = QEMU_OPT_SIZE,
1619 .help = "Virtual disk size"
1622 .name = BLOCK_OPT_BACKING_FILE,
1623 .type = QEMU_OPT_STRING,
1624 .help = "File name of a base image"
1627 .name = BLOCK_OPT_BACKING_FMT,
1628 .type = QEMU_OPT_STRING,
1629 .help = "Image format of the base image"
1632 .name = BLOCK_OPT_CLUSTER_SIZE,
1633 .type = QEMU_OPT_SIZE,
1634 .help = "Cluster size (in bytes)",
1635 .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE)
1638 .name = BLOCK_OPT_TABLE_SIZE,
1639 .type = QEMU_OPT_SIZE,
1640 .help = "L1/L2 table size (in clusters)"
1642 { /* end of list */ }
1646 static BlockDriver bdrv_qed = {
1647 .format_name = "qed",
1648 .instance_size = sizeof(BDRVQEDState),
1649 .create_opts = &qed_create_opts,
1650 .supports_backing = true,
1652 .bdrv_probe = bdrv_qed_probe,
1653 .bdrv_open = bdrv_qed_open,
1654 .bdrv_close = bdrv_qed_close,
1655 .bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
1656 .bdrv_create = bdrv_qed_create,
1657 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1658 .bdrv_co_get_block_status = bdrv_qed_co_get_block_status,
1659 .bdrv_aio_readv = bdrv_qed_aio_readv,
1660 .bdrv_aio_writev = bdrv_qed_aio_writev,
1661 .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes,
1662 .bdrv_truncate = bdrv_qed_truncate,
1663 .bdrv_getlength = bdrv_qed_getlength,
1664 .bdrv_get_info = bdrv_qed_get_info,
1665 .bdrv_refresh_limits = bdrv_qed_refresh_limits,
1666 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
1667 .bdrv_invalidate_cache = bdrv_qed_invalidate_cache,
1668 .bdrv_check = bdrv_qed_check,
1669 .bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
1670 .bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
1673 static void bdrv_qed_init(void)
1675 bdrv_register(&bdrv_qed);
1678 block_init(bdrv_qed_init);