Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / block / qed.c
blob79448321814a2e1d26221ed5508c368a518e99f4
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 void qed_aio_cancel(BlockDriverAIOCB *blockacb)
23 QEDAIOCB *acb = (QEDAIOCB *)blockacb;
24 AioContext *aio_context = bdrv_get_aio_context(blockacb->bs);
25 bool finished = false;
27 /* Wait for the request to finish */
28 acb->finished = &finished;
29 while (!finished) {
30 aio_poll(aio_context, true);
34 static const AIOCBInfo qed_aiocb_info = {
35 .aiocb_size = sizeof(QEDAIOCB),
36 .cancel = qed_aio_cancel,
39 static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
40 const char *filename)
42 const QEDHeader *header = (const QEDHeader *)buf;
44 if (buf_size < sizeof(*header)) {
45 return 0;
47 if (le32_to_cpu(header->magic) != QED_MAGIC) {
48 return 0;
50 return 100;
53 /**
54 * Check whether an image format is raw
56 * @fmt: Backing file format, may be NULL
58 static bool qed_fmt_is_raw(const char *fmt)
60 return fmt && strcmp(fmt, "raw") == 0;
63 static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
65 cpu->magic = le32_to_cpu(le->magic);
66 cpu->cluster_size = le32_to_cpu(le->cluster_size);
67 cpu->table_size = le32_to_cpu(le->table_size);
68 cpu->header_size = le32_to_cpu(le->header_size);
69 cpu->features = le64_to_cpu(le->features);
70 cpu->compat_features = le64_to_cpu(le->compat_features);
71 cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
72 cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
73 cpu->image_size = le64_to_cpu(le->image_size);
74 cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
75 cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
78 static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
80 le->magic = cpu_to_le32(cpu->magic);
81 le->cluster_size = cpu_to_le32(cpu->cluster_size);
82 le->table_size = cpu_to_le32(cpu->table_size);
83 le->header_size = cpu_to_le32(cpu->header_size);
84 le->features = cpu_to_le64(cpu->features);
85 le->compat_features = cpu_to_le64(cpu->compat_features);
86 le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
87 le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
88 le->image_size = cpu_to_le64(cpu->image_size);
89 le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
90 le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
93 int qed_write_header_sync(BDRVQEDState *s)
95 QEDHeader le;
96 int ret;
98 qed_header_cpu_to_le(&s->header, &le);
99 ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le));
100 if (ret != sizeof(le)) {
101 return ret;
103 return 0;
106 typedef struct {
107 GenericCB gencb;
108 BDRVQEDState *s;
109 struct iovec iov;
110 QEMUIOVector qiov;
111 int nsectors;
112 uint8_t *buf;
113 } QEDWriteHeaderCB;
115 static void qed_write_header_cb(void *opaque, int ret)
117 QEDWriteHeaderCB *write_header_cb = opaque;
119 qemu_vfree(write_header_cb->buf);
120 gencb_complete(write_header_cb, ret);
123 static void qed_write_header_read_cb(void *opaque, int ret)
125 QEDWriteHeaderCB *write_header_cb = opaque;
126 BDRVQEDState *s = write_header_cb->s;
128 if (ret) {
129 qed_write_header_cb(write_header_cb, ret);
130 return;
133 /* Update header */
134 qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf);
136 bdrv_aio_writev(s->bs->file, 0, &write_header_cb->qiov,
137 write_header_cb->nsectors, qed_write_header_cb,
138 write_header_cb);
142 * Update header in-place (does not rewrite backing filename or other strings)
144 * This function only updates known header fields in-place and does not affect
145 * extra data after the QED header.
147 static void qed_write_header(BDRVQEDState *s, BlockDriverCompletionFunc cb,
148 void *opaque)
150 /* We must write full sectors for O_DIRECT but cannot necessarily generate
151 * the data following the header if an unrecognized compat feature is
152 * active. Therefore, first read the sectors containing the header, update
153 * them, and write back.
156 int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) /
157 BDRV_SECTOR_SIZE;
158 size_t len = nsectors * BDRV_SECTOR_SIZE;
159 QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb),
160 cb, opaque);
162 write_header_cb->s = s;
163 write_header_cb->nsectors = nsectors;
164 write_header_cb->buf = qemu_blockalign(s->bs, len);
165 write_header_cb->iov.iov_base = write_header_cb->buf;
166 write_header_cb->iov.iov_len = len;
167 qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1);
169 bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors,
170 qed_write_header_read_cb, write_header_cb);
173 static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
175 uint64_t table_entries;
176 uint64_t l2_size;
178 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
179 l2_size = table_entries * cluster_size;
181 return l2_size * table_entries;
184 static bool qed_is_cluster_size_valid(uint32_t cluster_size)
186 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
187 cluster_size > QED_MAX_CLUSTER_SIZE) {
188 return false;
190 if (cluster_size & (cluster_size - 1)) {
191 return false; /* not power of 2 */
193 return true;
196 static bool qed_is_table_size_valid(uint32_t table_size)
198 if (table_size < QED_MIN_TABLE_SIZE ||
199 table_size > QED_MAX_TABLE_SIZE) {
200 return false;
202 if (table_size & (table_size - 1)) {
203 return false; /* not power of 2 */
205 return true;
208 static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
209 uint32_t table_size)
211 if (image_size % BDRV_SECTOR_SIZE != 0) {
212 return false; /* not multiple of sector size */
214 if (image_size > qed_max_image_size(cluster_size, table_size)) {
215 return false; /* image is too large */
217 return true;
221 * Read a string of known length from the image file
223 * @file: Image file
224 * @offset: File offset to start of string, in bytes
225 * @n: String length in bytes
226 * @buf: Destination buffer
227 * @buflen: Destination buffer length in bytes
228 * @ret: 0 on success, -errno on failure
230 * The string is NUL-terminated.
232 static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n,
233 char *buf, size_t buflen)
235 int ret;
236 if (n >= buflen) {
237 return -EINVAL;
239 ret = bdrv_pread(file, offset, buf, n);
240 if (ret < 0) {
241 return ret;
243 buf[n] = '\0';
244 return 0;
248 * Allocate new clusters
250 * @s: QED state
251 * @n: Number of contiguous clusters to allocate
252 * @ret: Offset of first allocated cluster
254 * This function only produces the offset where the new clusters should be
255 * written. It updates BDRVQEDState but does not make any changes to the image
256 * file.
258 static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
260 uint64_t offset = s->file_size;
261 s->file_size += n * s->header.cluster_size;
262 return offset;
265 QEDTable *qed_alloc_table(BDRVQEDState *s)
267 /* Honor O_DIRECT memory alignment requirements */
268 return qemu_blockalign(s->bs,
269 s->header.cluster_size * s->header.table_size);
273 * Allocate a new zeroed L2 table
275 static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
277 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
279 l2_table->table = qed_alloc_table(s);
280 l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
282 memset(l2_table->table->offsets, 0,
283 s->header.cluster_size * s->header.table_size);
284 return l2_table;
287 static void qed_aio_next_io(void *opaque, int ret);
289 static void qed_plug_allocating_write_reqs(BDRVQEDState *s)
291 assert(!s->allocating_write_reqs_plugged);
293 s->allocating_write_reqs_plugged = true;
296 static void qed_unplug_allocating_write_reqs(BDRVQEDState *s)
298 QEDAIOCB *acb;
300 assert(s->allocating_write_reqs_plugged);
302 s->allocating_write_reqs_plugged = false;
304 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
305 if (acb) {
306 qed_aio_next_io(acb, 0);
310 static void qed_finish_clear_need_check(void *opaque, int ret)
312 /* Do nothing */
315 static void qed_flush_after_clear_need_check(void *opaque, int ret)
317 BDRVQEDState *s = opaque;
319 bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s);
321 /* No need to wait until flush completes */
322 qed_unplug_allocating_write_reqs(s);
325 static void qed_clear_need_check(void *opaque, int ret)
327 BDRVQEDState *s = opaque;
329 if (ret) {
330 qed_unplug_allocating_write_reqs(s);
331 return;
334 s->header.features &= ~QED_F_NEED_CHECK;
335 qed_write_header(s, qed_flush_after_clear_need_check, s);
338 static void qed_need_check_timer_cb(void *opaque)
340 BDRVQEDState *s = opaque;
342 /* The timer should only fire when allocating writes have drained */
343 assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs));
345 trace_qed_need_check_timer_cb(s);
347 qed_plug_allocating_write_reqs(s);
349 /* Ensure writes are on disk before clearing flag */
350 bdrv_aio_flush(s->bs, qed_clear_need_check, s);
353 static void qed_start_need_check_timer(BDRVQEDState *s)
355 trace_qed_start_need_check_timer(s);
357 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
358 * migration.
360 timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
361 get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT);
364 /* It's okay to call this multiple times or when no timer is started */
365 static void qed_cancel_need_check_timer(BDRVQEDState *s)
367 trace_qed_cancel_need_check_timer(s);
368 timer_del(s->need_check_timer);
371 static void bdrv_qed_rebind(BlockDriverState *bs)
373 BDRVQEDState *s = bs->opaque;
374 s->bs = bs;
377 static void bdrv_qed_detach_aio_context(BlockDriverState *bs)
379 BDRVQEDState *s = bs->opaque;
381 qed_cancel_need_check_timer(s);
382 timer_free(s->need_check_timer);
385 static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
386 AioContext *new_context)
388 BDRVQEDState *s = bs->opaque;
390 s->need_check_timer = aio_timer_new(new_context,
391 QEMU_CLOCK_VIRTUAL, SCALE_NS,
392 qed_need_check_timer_cb, s);
393 if (s->header.features & QED_F_NEED_CHECK) {
394 qed_start_need_check_timer(s);
398 static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
399 Error **errp)
401 BDRVQEDState *s = bs->opaque;
402 QEDHeader le_header;
403 int64_t file_size;
404 int ret;
406 s->bs = bs;
407 QSIMPLEQ_INIT(&s->allocating_write_reqs);
409 ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header));
410 if (ret < 0) {
411 return ret;
413 qed_header_le_to_cpu(&le_header, &s->header);
415 if (s->header.magic != QED_MAGIC) {
416 error_setg(errp, "Image not in QED format");
417 return -EINVAL;
419 if (s->header.features & ~QED_FEATURE_MASK) {
420 /* image uses unsupported feature bits */
421 char buf[64];
422 snprintf(buf, sizeof(buf), "%" PRIx64,
423 s->header.features & ~QED_FEATURE_MASK);
424 error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
425 bs->device_name, "QED", buf);
426 return -ENOTSUP;
428 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
429 return -EINVAL;
432 /* Round down file size to the last cluster */
433 file_size = bdrv_getlength(bs->file);
434 if (file_size < 0) {
435 return file_size;
437 s->file_size = qed_start_of_cluster(s, file_size);
439 if (!qed_is_table_size_valid(s->header.table_size)) {
440 return -EINVAL;
442 if (!qed_is_image_size_valid(s->header.image_size,
443 s->header.cluster_size,
444 s->header.table_size)) {
445 return -EINVAL;
447 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
448 return -EINVAL;
451 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
452 sizeof(uint64_t);
453 s->l2_shift = ffs(s->header.cluster_size) - 1;
454 s->l2_mask = s->table_nelems - 1;
455 s->l1_shift = s->l2_shift + ffs(s->table_nelems) - 1;
457 if ((s->header.features & QED_F_BACKING_FILE)) {
458 if ((uint64_t)s->header.backing_filename_offset +
459 s->header.backing_filename_size >
460 s->header.cluster_size * s->header.header_size) {
461 return -EINVAL;
464 ret = qed_read_string(bs->file, s->header.backing_filename_offset,
465 s->header.backing_filename_size, bs->backing_file,
466 sizeof(bs->backing_file));
467 if (ret < 0) {
468 return ret;
471 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
472 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
476 /* Reset unknown autoclear feature bits. This is a backwards
477 * compatibility mechanism that allows images to be opened by older
478 * programs, which "knock out" unknown feature bits. When an image is
479 * opened by a newer program again it can detect that the autoclear
480 * feature is no longer valid.
482 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
483 !bdrv_is_read_only(bs->file) && !(flags & BDRV_O_INCOMING)) {
484 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
486 ret = qed_write_header_sync(s);
487 if (ret) {
488 return ret;
491 /* From here on only known autoclear feature bits are valid */
492 bdrv_flush(bs->file);
495 s->l1_table = qed_alloc_table(s);
496 qed_init_l2_cache(&s->l2_cache);
498 ret = qed_read_l1_table_sync(s);
499 if (ret) {
500 goto out;
503 /* If image was not closed cleanly, check consistency */
504 if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
505 /* Read-only images cannot be fixed. There is no risk of corruption
506 * since write operations are not possible. Therefore, allow
507 * potentially inconsistent images to be opened read-only. This can
508 * aid data recovery from an otherwise inconsistent image.
510 if (!bdrv_is_read_only(bs->file) &&
511 !(flags & BDRV_O_INCOMING)) {
512 BdrvCheckResult result = {0};
514 ret = qed_check(s, &result, true);
515 if (ret) {
516 goto out;
521 bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
523 out:
524 if (ret) {
525 qed_free_l2_cache(&s->l2_cache);
526 qemu_vfree(s->l1_table);
528 return ret;
531 static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
533 BDRVQEDState *s = bs->opaque;
535 bs->bl.write_zeroes_alignment = s->header.cluster_size >> BDRV_SECTOR_BITS;
538 /* We have nothing to do for QED reopen, stubs just return
539 * success */
540 static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
541 BlockReopenQueue *queue, Error **errp)
543 return 0;
546 static void bdrv_qed_close(BlockDriverState *bs)
548 BDRVQEDState *s = bs->opaque;
550 bdrv_qed_detach_aio_context(bs);
552 /* Ensure writes reach stable storage */
553 bdrv_flush(bs->file);
555 /* Clean shutdown, no check required on next open */
556 if (s->header.features & QED_F_NEED_CHECK) {
557 s->header.features &= ~QED_F_NEED_CHECK;
558 qed_write_header_sync(s);
561 qed_free_l2_cache(&s->l2_cache);
562 qemu_vfree(s->l1_table);
565 static int qed_create(const char *filename, uint32_t cluster_size,
566 uint64_t image_size, uint32_t table_size,
567 const char *backing_file, const char *backing_fmt,
568 QemuOpts *opts, Error **errp)
570 QEDHeader header = {
571 .magic = QED_MAGIC,
572 .cluster_size = cluster_size,
573 .table_size = table_size,
574 .header_size = 1,
575 .features = 0,
576 .compat_features = 0,
577 .l1_table_offset = cluster_size,
578 .image_size = image_size,
580 QEDHeader le_header;
581 uint8_t *l1_table = NULL;
582 size_t l1_size = header.cluster_size * header.table_size;
583 Error *local_err = NULL;
584 int ret = 0;
585 BlockDriverState *bs;
587 ret = bdrv_create_file(filename, opts, &local_err);
588 if (ret < 0) {
589 error_propagate(errp, local_err);
590 return ret;
593 bs = NULL;
594 ret = bdrv_open(&bs, filename, NULL, NULL,
595 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL, NULL,
596 &local_err);
597 if (ret < 0) {
598 error_propagate(errp, local_err);
599 return ret;
602 /* File must start empty and grow, check truncate is supported */
603 ret = bdrv_truncate(bs, 0);
604 if (ret < 0) {
605 goto out;
608 if (backing_file) {
609 header.features |= QED_F_BACKING_FILE;
610 header.backing_filename_offset = sizeof(le_header);
611 header.backing_filename_size = strlen(backing_file);
613 if (qed_fmt_is_raw(backing_fmt)) {
614 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
618 qed_header_cpu_to_le(&header, &le_header);
619 ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header));
620 if (ret < 0) {
621 goto out;
623 ret = bdrv_pwrite(bs, sizeof(le_header), backing_file,
624 header.backing_filename_size);
625 if (ret < 0) {
626 goto out;
629 l1_table = g_malloc0(l1_size);
630 ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size);
631 if (ret < 0) {
632 goto out;
635 ret = 0; /* success */
636 out:
637 g_free(l1_table);
638 bdrv_unref(bs);
639 return ret;
642 static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp)
644 uint64_t image_size = 0;
645 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
646 uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
647 char *backing_file = NULL;
648 char *backing_fmt = NULL;
649 int ret;
651 image_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
652 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
653 backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
654 cluster_size = qemu_opt_get_size_del(opts,
655 BLOCK_OPT_CLUSTER_SIZE,
656 QED_DEFAULT_CLUSTER_SIZE);
657 table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE,
658 QED_DEFAULT_TABLE_SIZE);
660 if (!qed_is_cluster_size_valid(cluster_size)) {
661 error_setg(errp, "QED cluster size must be within range [%u, %u] "
662 "and power of 2",
663 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
664 ret = -EINVAL;
665 goto finish;
667 if (!qed_is_table_size_valid(table_size)) {
668 error_setg(errp, "QED table size must be within range [%u, %u] "
669 "and power of 2",
670 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
671 ret = -EINVAL;
672 goto finish;
674 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
675 error_setg(errp, "QED image size must be a non-zero multiple of "
676 "cluster size and less than %" PRIu64 " bytes",
677 qed_max_image_size(cluster_size, table_size));
678 ret = -EINVAL;
679 goto finish;
682 ret = qed_create(filename, cluster_size, image_size, table_size,
683 backing_file, backing_fmt, opts, errp);
685 finish:
686 g_free(backing_file);
687 g_free(backing_fmt);
688 return ret;
691 typedef struct {
692 BlockDriverState *bs;
693 Coroutine *co;
694 uint64_t pos;
695 int64_t status;
696 int *pnum;
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 break;
709 case QED_CLUSTER_ZERO:
710 cb->status = BDRV_BLOCK_ZERO;
711 break;
712 case QED_CLUSTER_L2:
713 case QED_CLUSTER_L1:
714 cb->status = 0;
715 break;
716 default:
717 assert(ret < 0);
718 cb->status = ret;
719 break;
722 if (cb->co) {
723 qemu_coroutine_enter(cb->co, NULL);
727 static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs,
728 int64_t sector_num,
729 int nb_sectors, int *pnum)
731 BDRVQEDState *s = bs->opaque;
732 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
733 QEDIsAllocatedCB cb = {
734 .bs = bs,
735 .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE,
736 .status = BDRV_BLOCK_OFFSET_MASK,
737 .pnum = pnum,
739 QEDRequest request = { .l2_table = NULL };
741 qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb);
743 /* Now sleep if the callback wasn't invoked immediately */
744 while (cb.status == BDRV_BLOCK_OFFSET_MASK) {
745 cb.co = qemu_coroutine_self();
746 qemu_coroutine_yield();
749 qed_unref_l2_cache_entry(request.l2_table);
751 return cb.status;
754 static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
756 return acb->common.bs->opaque;
760 * Read from the backing file or zero-fill if no backing file
762 * @s: QED state
763 * @pos: Byte position in device
764 * @qiov: Destination I/O vector
765 * @backing_qiov: Possibly shortened copy of qiov, to be allocated here
766 * @cb: Completion function
767 * @opaque: User data for completion function
769 * This function reads qiov->size bytes starting at pos from the backing file.
770 * If there is no backing file then zeroes are read.
772 static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
773 QEMUIOVector *qiov,
774 QEMUIOVector **backing_qiov,
775 BlockDriverCompletionFunc *cb, void *opaque)
777 uint64_t backing_length = 0;
778 size_t size;
780 /* If there is a backing file, get its length. Treat the absence of a
781 * backing file like a zero length backing file.
783 if (s->bs->backing_hd) {
784 int64_t l = bdrv_getlength(s->bs->backing_hd);
785 if (l < 0) {
786 cb(opaque, l);
787 return;
789 backing_length = l;
792 /* Zero all sectors if reading beyond the end of the backing file */
793 if (pos >= backing_length ||
794 pos + qiov->size > backing_length) {
795 qemu_iovec_memset(qiov, 0, 0, qiov->size);
798 /* Complete now if there are no backing file sectors to read */
799 if (pos >= backing_length) {
800 cb(opaque, 0);
801 return;
804 /* If the read straddles the end of the backing file, shorten it */
805 size = MIN((uint64_t)backing_length - pos, qiov->size);
807 assert(*backing_qiov == NULL);
808 *backing_qiov = g_new(QEMUIOVector, 1);
809 qemu_iovec_init(*backing_qiov, qiov->niov);
810 qemu_iovec_concat(*backing_qiov, qiov, 0, size);
812 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
813 bdrv_aio_readv(s->bs->backing_hd, pos / BDRV_SECTOR_SIZE,
814 *backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
817 typedef struct {
818 GenericCB gencb;
819 BDRVQEDState *s;
820 QEMUIOVector qiov;
821 QEMUIOVector *backing_qiov;
822 struct iovec iov;
823 uint64_t offset;
824 } CopyFromBackingFileCB;
826 static void qed_copy_from_backing_file_cb(void *opaque, int ret)
828 CopyFromBackingFileCB *copy_cb = opaque;
829 qemu_vfree(copy_cb->iov.iov_base);
830 gencb_complete(&copy_cb->gencb, ret);
833 static void qed_copy_from_backing_file_write(void *opaque, int ret)
835 CopyFromBackingFileCB *copy_cb = opaque;
836 BDRVQEDState *s = copy_cb->s;
838 if (copy_cb->backing_qiov) {
839 qemu_iovec_destroy(copy_cb->backing_qiov);
840 g_free(copy_cb->backing_qiov);
841 copy_cb->backing_qiov = NULL;
844 if (ret) {
845 qed_copy_from_backing_file_cb(copy_cb, ret);
846 return;
849 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
850 bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE,
851 &copy_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE,
852 qed_copy_from_backing_file_cb, copy_cb);
856 * Copy data from backing file into the image
858 * @s: QED state
859 * @pos: Byte position in device
860 * @len: Number of bytes
861 * @offset: Byte offset in image file
862 * @cb: Completion function
863 * @opaque: User data for completion function
865 static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
866 uint64_t len, uint64_t offset,
867 BlockDriverCompletionFunc *cb,
868 void *opaque)
870 CopyFromBackingFileCB *copy_cb;
872 /* Skip copy entirely if there is no work to do */
873 if (len == 0) {
874 cb(opaque, 0);
875 return;
878 copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
879 copy_cb->s = s;
880 copy_cb->offset = offset;
881 copy_cb->backing_qiov = NULL;
882 copy_cb->iov.iov_base = qemu_blockalign(s->bs, len);
883 copy_cb->iov.iov_len = len;
884 qemu_iovec_init_external(&copy_cb->qiov, &copy_cb->iov, 1);
886 qed_read_backing_file(s, pos, &copy_cb->qiov, &copy_cb->backing_qiov,
887 qed_copy_from_backing_file_write, copy_cb);
891 * Link one or more contiguous clusters into a table
893 * @s: QED state
894 * @table: L2 table
895 * @index: First cluster index
896 * @n: Number of contiguous clusters
897 * @cluster: First cluster offset
899 * The cluster offset may be an allocated byte offset in the image file, the
900 * zero cluster marker, or the unallocated cluster marker.
902 static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
903 unsigned int n, uint64_t cluster)
905 int i;
906 for (i = index; i < index + n; i++) {
907 table->offsets[i] = cluster;
908 if (!qed_offset_is_unalloc_cluster(cluster) &&
909 !qed_offset_is_zero_cluster(cluster)) {
910 cluster += s->header.cluster_size;
915 static void qed_aio_complete_bh(void *opaque)
917 QEDAIOCB *acb = opaque;
918 BlockDriverCompletionFunc *cb = acb->common.cb;
919 void *user_opaque = acb->common.opaque;
920 int ret = acb->bh_ret;
921 bool *finished = acb->finished;
923 qemu_bh_delete(acb->bh);
924 qemu_aio_release(acb);
926 /* Invoke callback */
927 cb(user_opaque, ret);
929 /* Signal cancel completion */
930 if (finished) {
931 *finished = true;
935 static void qed_aio_complete(QEDAIOCB *acb, int ret)
937 BDRVQEDState *s = acb_to_s(acb);
939 trace_qed_aio_complete(s, acb, ret);
941 /* Free resources */
942 qemu_iovec_destroy(&acb->cur_qiov);
943 qed_unref_l2_cache_entry(acb->request.l2_table);
945 /* Free the buffer we may have allocated for zero writes */
946 if (acb->flags & QED_AIOCB_ZERO) {
947 qemu_vfree(acb->qiov->iov[0].iov_base);
948 acb->qiov->iov[0].iov_base = NULL;
951 /* Arrange for a bh to invoke the completion function */
952 acb->bh_ret = ret;
953 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
954 qed_aio_complete_bh, acb);
955 qemu_bh_schedule(acb->bh);
957 /* Start next allocating write request waiting behind this one. Note that
958 * requests enqueue themselves when they first hit an unallocated cluster
959 * but they wait until the entire request is finished before waking up the
960 * next request in the queue. This ensures that we don't cycle through
961 * requests multiple times but rather finish one at a time completely.
963 if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
964 QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
965 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
966 if (acb) {
967 qed_aio_next_io(acb, 0);
968 } else if (s->header.features & QED_F_NEED_CHECK) {
969 qed_start_need_check_timer(s);
975 * Commit the current L2 table to the cache
977 static void qed_commit_l2_update(void *opaque, int ret)
979 QEDAIOCB *acb = opaque;
980 BDRVQEDState *s = acb_to_s(acb);
981 CachedL2Table *l2_table = acb->request.l2_table;
982 uint64_t l2_offset = l2_table->offset;
984 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
986 /* This is guaranteed to succeed because we just committed the entry to the
987 * cache.
989 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
990 assert(acb->request.l2_table != NULL);
992 qed_aio_next_io(opaque, ret);
996 * Update L1 table with new L2 table offset and write it out
998 static void qed_aio_write_l1_update(void *opaque, int ret)
1000 QEDAIOCB *acb = opaque;
1001 BDRVQEDState *s = acb_to_s(acb);
1002 int index;
1004 if (ret) {
1005 qed_aio_complete(acb, ret);
1006 return;
1009 index = qed_l1_index(s, acb->cur_pos);
1010 s->l1_table->offsets[index] = acb->request.l2_table->offset;
1012 qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
1016 * Update L2 table with new cluster offsets and write them out
1018 static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset)
1020 BDRVQEDState *s = acb_to_s(acb);
1021 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
1022 int index;
1024 if (ret) {
1025 goto err;
1028 if (need_alloc) {
1029 qed_unref_l2_cache_entry(acb->request.l2_table);
1030 acb->request.l2_table = qed_new_l2_table(s);
1033 index = qed_l2_index(s, acb->cur_pos);
1034 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
1035 offset);
1037 if (need_alloc) {
1038 /* Write out the whole new L2 table */
1039 qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true,
1040 qed_aio_write_l1_update, acb);
1041 } else {
1042 /* Write out only the updated part of the L2 table */
1043 qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false,
1044 qed_aio_next_io, acb);
1046 return;
1048 err:
1049 qed_aio_complete(acb, ret);
1052 static void qed_aio_write_l2_update_cb(void *opaque, int ret)
1054 QEDAIOCB *acb = opaque;
1055 qed_aio_write_l2_update(acb, ret, acb->cur_cluster);
1059 * Flush new data clusters before updating the L2 table
1061 * This flush is necessary when a backing file is in use. A crash during an
1062 * allocating write could result in empty clusters in the image. If the write
1063 * only touched a subregion of the cluster, then backing image sectors have
1064 * been lost in the untouched region. The solution is to flush after writing a
1065 * new data cluster and before updating the L2 table.
1067 static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
1069 QEDAIOCB *acb = opaque;
1070 BDRVQEDState *s = acb_to_s(acb);
1072 if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update_cb, opaque)) {
1073 qed_aio_complete(acb, -EIO);
1078 * Write data to the image file
1080 static void qed_aio_write_main(void *opaque, int ret)
1082 QEDAIOCB *acb = opaque;
1083 BDRVQEDState *s = acb_to_s(acb);
1084 uint64_t offset = acb->cur_cluster +
1085 qed_offset_into_cluster(s, acb->cur_pos);
1086 BlockDriverCompletionFunc *next_fn;
1088 trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
1090 if (ret) {
1091 qed_aio_complete(acb, ret);
1092 return;
1095 if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
1096 next_fn = qed_aio_next_io;
1097 } else {
1098 if (s->bs->backing_hd) {
1099 next_fn = qed_aio_write_flush_before_l2_update;
1100 } else {
1101 next_fn = qed_aio_write_l2_update_cb;
1105 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
1106 bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE,
1107 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1108 next_fn, acb);
1112 * Populate back untouched region of new data cluster
1114 static void qed_aio_write_postfill(void *opaque, int ret)
1116 QEDAIOCB *acb = opaque;
1117 BDRVQEDState *s = acb_to_s(acb);
1118 uint64_t start = acb->cur_pos + acb->cur_qiov.size;
1119 uint64_t len =
1120 qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1121 uint64_t offset = acb->cur_cluster +
1122 qed_offset_into_cluster(s, acb->cur_pos) +
1123 acb->cur_qiov.size;
1125 if (ret) {
1126 qed_aio_complete(acb, ret);
1127 return;
1130 trace_qed_aio_write_postfill(s, acb, start, len, offset);
1131 qed_copy_from_backing_file(s, start, len, offset,
1132 qed_aio_write_main, acb);
1136 * Populate front untouched region of new data cluster
1138 static void qed_aio_write_prefill(void *opaque, int ret)
1140 QEDAIOCB *acb = opaque;
1141 BDRVQEDState *s = acb_to_s(acb);
1142 uint64_t start = qed_start_of_cluster(s, acb->cur_pos);
1143 uint64_t len = qed_offset_into_cluster(s, acb->cur_pos);
1145 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1146 qed_copy_from_backing_file(s, start, len, acb->cur_cluster,
1147 qed_aio_write_postfill, acb);
1151 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1153 static bool qed_should_set_need_check(BDRVQEDState *s)
1155 /* The flush before L2 update path ensures consistency */
1156 if (s->bs->backing_hd) {
1157 return false;
1160 return !(s->header.features & QED_F_NEED_CHECK);
1163 static void qed_aio_write_zero_cluster(void *opaque, int ret)
1165 QEDAIOCB *acb = opaque;
1167 if (ret) {
1168 qed_aio_complete(acb, ret);
1169 return;
1172 qed_aio_write_l2_update(acb, 0, 1);
1176 * Write new data cluster
1178 * @acb: Write request
1179 * @len: Length in bytes
1181 * This path is taken when writing to previously unallocated clusters.
1183 static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1185 BDRVQEDState *s = acb_to_s(acb);
1186 BlockDriverCompletionFunc *cb;
1188 /* Cancel timer when the first allocating request comes in */
1189 if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) {
1190 qed_cancel_need_check_timer(s);
1193 /* Freeze this request if another allocating write is in progress */
1194 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
1195 QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
1197 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) ||
1198 s->allocating_write_reqs_plugged) {
1199 return; /* wait for existing request to finish */
1202 acb->cur_nclusters = qed_bytes_to_clusters(s,
1203 qed_offset_into_cluster(s, acb->cur_pos) + len);
1204 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1206 if (acb->flags & QED_AIOCB_ZERO) {
1207 /* Skip ahead if the clusters are already zero */
1208 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
1209 qed_aio_next_io(acb, 0);
1210 return;
1213 cb = qed_aio_write_zero_cluster;
1214 } else {
1215 cb = qed_aio_write_prefill;
1216 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1219 if (qed_should_set_need_check(s)) {
1220 s->header.features |= QED_F_NEED_CHECK;
1221 qed_write_header(s, cb, acb);
1222 } else {
1223 cb(acb, 0);
1228 * Write data cluster in place
1230 * @acb: Write request
1231 * @offset: Cluster offset in bytes
1232 * @len: Length in bytes
1234 * This path is taken when writing to already allocated clusters.
1236 static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1238 /* Allocate buffer for zero writes */
1239 if (acb->flags & QED_AIOCB_ZERO) {
1240 struct iovec *iov = acb->qiov->iov;
1242 if (!iov->iov_base) {
1243 iov->iov_base = qemu_blockalign(acb->common.bs, iov->iov_len);
1244 memset(iov->iov_base, 0, iov->iov_len);
1248 /* Calculate the I/O vector */
1249 acb->cur_cluster = offset;
1250 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1252 /* Do the actual write */
1253 qed_aio_write_main(acb, 0);
1257 * Write data cluster
1259 * @opaque: Write request
1260 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1261 * or -errno
1262 * @offset: Cluster offset in bytes
1263 * @len: Length in bytes
1265 * Callback from qed_find_cluster().
1267 static void qed_aio_write_data(void *opaque, int ret,
1268 uint64_t offset, size_t len)
1270 QEDAIOCB *acb = opaque;
1272 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1274 acb->find_cluster_ret = ret;
1276 switch (ret) {
1277 case QED_CLUSTER_FOUND:
1278 qed_aio_write_inplace(acb, offset, len);
1279 break;
1281 case QED_CLUSTER_L2:
1282 case QED_CLUSTER_L1:
1283 case QED_CLUSTER_ZERO:
1284 qed_aio_write_alloc(acb, len);
1285 break;
1287 default:
1288 qed_aio_complete(acb, ret);
1289 break;
1294 * Read data cluster
1296 * @opaque: Read request
1297 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1298 * or -errno
1299 * @offset: Cluster offset in bytes
1300 * @len: Length in bytes
1302 * Callback from qed_find_cluster().
1304 static void qed_aio_read_data(void *opaque, int ret,
1305 uint64_t offset, size_t len)
1307 QEDAIOCB *acb = opaque;
1308 BDRVQEDState *s = acb_to_s(acb);
1309 BlockDriverState *bs = acb->common.bs;
1311 /* Adjust offset into cluster */
1312 offset += qed_offset_into_cluster(s, acb->cur_pos);
1314 trace_qed_aio_read_data(s, acb, ret, offset, len);
1316 if (ret < 0) {
1317 goto err;
1320 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1322 /* Handle zero cluster and backing file reads */
1323 if (ret == QED_CLUSTER_ZERO) {
1324 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
1325 qed_aio_next_io(acb, 0);
1326 return;
1327 } else if (ret != QED_CLUSTER_FOUND) {
1328 qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
1329 &acb->backing_qiov, qed_aio_next_io, acb);
1330 return;
1333 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1334 bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE,
1335 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1336 qed_aio_next_io, acb);
1337 return;
1339 err:
1340 qed_aio_complete(acb, ret);
1344 * Begin next I/O or complete the request
1346 static void qed_aio_next_io(void *opaque, int ret)
1348 QEDAIOCB *acb = opaque;
1349 BDRVQEDState *s = acb_to_s(acb);
1350 QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ?
1351 qed_aio_write_data : qed_aio_read_data;
1353 trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
1355 if (acb->backing_qiov) {
1356 qemu_iovec_destroy(acb->backing_qiov);
1357 g_free(acb->backing_qiov);
1358 acb->backing_qiov = NULL;
1361 /* Handle I/O error */
1362 if (ret) {
1363 qed_aio_complete(acb, ret);
1364 return;
1367 acb->qiov_offset += acb->cur_qiov.size;
1368 acb->cur_pos += acb->cur_qiov.size;
1369 qemu_iovec_reset(&acb->cur_qiov);
1371 /* Complete request */
1372 if (acb->cur_pos >= acb->end_pos) {
1373 qed_aio_complete(acb, 0);
1374 return;
1377 /* Find next cluster and start I/O */
1378 qed_find_cluster(s, &acb->request,
1379 acb->cur_pos, acb->end_pos - acb->cur_pos,
1380 io_fn, acb);
1383 static BlockDriverAIOCB *qed_aio_setup(BlockDriverState *bs,
1384 int64_t sector_num,
1385 QEMUIOVector *qiov, int nb_sectors,
1386 BlockDriverCompletionFunc *cb,
1387 void *opaque, int flags)
1389 QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque);
1391 trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
1392 opaque, flags);
1394 acb->flags = flags;
1395 acb->finished = NULL;
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 BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
1410 int64_t sector_num,
1411 QEMUIOVector *qiov, int nb_sectors,
1412 BlockDriverCompletionFunc *cb,
1413 void *opaque)
1415 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1418 static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
1419 int64_t sector_num,
1420 QEMUIOVector *qiov, int nb_sectors,
1421 BlockDriverCompletionFunc *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 BlockDriverAIOCB *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_hd) {
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, 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, &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_setg(errp, "Could not reopen qed layer: %s",
1620 error_get_pretty(local_err));
1621 error_free(local_err);
1622 return;
1623 } else if (ret < 0) {
1624 error_setg_errno(errp, -ret, "Could not reopen qed layer");
1625 return;
1629 static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result,
1630 BdrvCheckMode fix)
1632 BDRVQEDState *s = bs->opaque;
1634 return qed_check(s, result, !!fix);
1637 static QemuOptsList qed_create_opts = {
1638 .name = "qed-create-opts",
1639 .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
1640 .desc = {
1642 .name = BLOCK_OPT_SIZE,
1643 .type = QEMU_OPT_SIZE,
1644 .help = "Virtual disk size"
1647 .name = BLOCK_OPT_BACKING_FILE,
1648 .type = QEMU_OPT_STRING,
1649 .help = "File name of a base image"
1652 .name = BLOCK_OPT_BACKING_FMT,
1653 .type = QEMU_OPT_STRING,
1654 .help = "Image format of the base image"
1657 .name = BLOCK_OPT_CLUSTER_SIZE,
1658 .type = QEMU_OPT_SIZE,
1659 .help = "Cluster size (in bytes)",
1660 .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE)
1663 .name = BLOCK_OPT_TABLE_SIZE,
1664 .type = QEMU_OPT_SIZE,
1665 .help = "L1/L2 table size (in clusters)"
1667 { /* end of list */ }
1671 static BlockDriver bdrv_qed = {
1672 .format_name = "qed",
1673 .instance_size = sizeof(BDRVQEDState),
1674 .create_opts = &qed_create_opts,
1675 .supports_backing = true,
1677 .bdrv_probe = bdrv_qed_probe,
1678 .bdrv_rebind = bdrv_qed_rebind,
1679 .bdrv_open = bdrv_qed_open,
1680 .bdrv_close = bdrv_qed_close,
1681 .bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
1682 .bdrv_create = bdrv_qed_create,
1683 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1684 .bdrv_co_get_block_status = bdrv_qed_co_get_block_status,
1685 .bdrv_aio_readv = bdrv_qed_aio_readv,
1686 .bdrv_aio_writev = bdrv_qed_aio_writev,
1687 .bdrv_co_write_zeroes = bdrv_qed_co_write_zeroes,
1688 .bdrv_truncate = bdrv_qed_truncate,
1689 .bdrv_getlength = bdrv_qed_getlength,
1690 .bdrv_get_info = bdrv_qed_get_info,
1691 .bdrv_refresh_limits = bdrv_qed_refresh_limits,
1692 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
1693 .bdrv_invalidate_cache = bdrv_qed_invalidate_cache,
1694 .bdrv_check = bdrv_qed_check,
1695 .bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
1696 .bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
1699 static void bdrv_qed_init(void)
1701 bdrv_register(&bdrv_qed);
1704 block_init(bdrv_qed_init);