kvm: Introduce basic MSI support for in-kernel irqchips
[qemu/agraf.git] / block / qed.c
blob366cde7ad83fa4c1087e439c1ea017f7e1b2f137
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 "qerror.h"
19 #include "migration.h"
21 static void qed_aio_cancel(BlockDriverAIOCB *blockacb)
23 QEDAIOCB *acb = (QEDAIOCB *)blockacb;
24 bool finished = false;
26 /* Wait for the request to finish */
27 acb->finished = &finished;
28 while (!finished) {
29 qemu_aio_wait();
33 static AIOPool qed_aio_pool = {
34 .aiocb_size = sizeof(QEDAIOCB),
35 .cancel = qed_aio_cancel,
38 static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
39 const char *filename)
41 const QEDHeader *header = (const QEDHeader *)buf;
43 if (buf_size < sizeof(*header)) {
44 return 0;
46 if (le32_to_cpu(header->magic) != QED_MAGIC) {
47 return 0;
49 return 100;
52 /**
53 * Check whether an image format is raw
55 * @fmt: Backing file format, may be NULL
57 static bool qed_fmt_is_raw(const char *fmt)
59 return fmt && strcmp(fmt, "raw") == 0;
62 static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
64 cpu->magic = le32_to_cpu(le->magic);
65 cpu->cluster_size = le32_to_cpu(le->cluster_size);
66 cpu->table_size = le32_to_cpu(le->table_size);
67 cpu->header_size = le32_to_cpu(le->header_size);
68 cpu->features = le64_to_cpu(le->features);
69 cpu->compat_features = le64_to_cpu(le->compat_features);
70 cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
71 cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
72 cpu->image_size = le64_to_cpu(le->image_size);
73 cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
74 cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
77 static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
79 le->magic = cpu_to_le32(cpu->magic);
80 le->cluster_size = cpu_to_le32(cpu->cluster_size);
81 le->table_size = cpu_to_le32(cpu->table_size);
82 le->header_size = cpu_to_le32(cpu->header_size);
83 le->features = cpu_to_le64(cpu->features);
84 le->compat_features = cpu_to_le64(cpu->compat_features);
85 le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
86 le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
87 le->image_size = cpu_to_le64(cpu->image_size);
88 le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
89 le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
92 static int qed_write_header_sync(BDRVQEDState *s)
94 QEDHeader le;
95 int ret;
97 qed_header_cpu_to_le(&s->header, &le);
98 ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le));
99 if (ret != sizeof(le)) {
100 return ret;
102 return 0;
105 typedef struct {
106 GenericCB gencb;
107 BDRVQEDState *s;
108 struct iovec iov;
109 QEMUIOVector qiov;
110 int nsectors;
111 uint8_t *buf;
112 } QEDWriteHeaderCB;
114 static void qed_write_header_cb(void *opaque, int ret)
116 QEDWriteHeaderCB *write_header_cb = opaque;
118 qemu_vfree(write_header_cb->buf);
119 gencb_complete(write_header_cb, ret);
122 static void qed_write_header_read_cb(void *opaque, int ret)
124 QEDWriteHeaderCB *write_header_cb = opaque;
125 BDRVQEDState *s = write_header_cb->s;
127 if (ret) {
128 qed_write_header_cb(write_header_cb, ret);
129 return;
132 /* Update header */
133 qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf);
135 bdrv_aio_writev(s->bs->file, 0, &write_header_cb->qiov,
136 write_header_cb->nsectors, qed_write_header_cb,
137 write_header_cb);
141 * Update header in-place (does not rewrite backing filename or other strings)
143 * This function only updates known header fields in-place and does not affect
144 * extra data after the QED header.
146 static void qed_write_header(BDRVQEDState *s, BlockDriverCompletionFunc cb,
147 void *opaque)
149 /* We must write full sectors for O_DIRECT but cannot necessarily generate
150 * the data following the header if an unrecognized compat feature is
151 * active. Therefore, first read the sectors containing the header, update
152 * them, and write back.
155 int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) /
156 BDRV_SECTOR_SIZE;
157 size_t len = nsectors * BDRV_SECTOR_SIZE;
158 QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb),
159 cb, opaque);
161 write_header_cb->s = s;
162 write_header_cb->nsectors = nsectors;
163 write_header_cb->buf = qemu_blockalign(s->bs, len);
164 write_header_cb->iov.iov_base = write_header_cb->buf;
165 write_header_cb->iov.iov_len = len;
166 qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1);
168 bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors,
169 qed_write_header_read_cb, write_header_cb);
172 static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
174 uint64_t table_entries;
175 uint64_t l2_size;
177 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
178 l2_size = table_entries * cluster_size;
180 return l2_size * table_entries;
183 static bool qed_is_cluster_size_valid(uint32_t cluster_size)
185 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
186 cluster_size > QED_MAX_CLUSTER_SIZE) {
187 return false;
189 if (cluster_size & (cluster_size - 1)) {
190 return false; /* not power of 2 */
192 return true;
195 static bool qed_is_table_size_valid(uint32_t table_size)
197 if (table_size < QED_MIN_TABLE_SIZE ||
198 table_size > QED_MAX_TABLE_SIZE) {
199 return false;
201 if (table_size & (table_size - 1)) {
202 return false; /* not power of 2 */
204 return true;
207 static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
208 uint32_t table_size)
210 if (image_size % BDRV_SECTOR_SIZE != 0) {
211 return false; /* not multiple of sector size */
213 if (image_size > qed_max_image_size(cluster_size, table_size)) {
214 return false; /* image is too large */
216 return true;
220 * Read a string of known length from the image file
222 * @file: Image file
223 * @offset: File offset to start of string, in bytes
224 * @n: String length in bytes
225 * @buf: Destination buffer
226 * @buflen: Destination buffer length in bytes
227 * @ret: 0 on success, -errno on failure
229 * The string is NUL-terminated.
231 static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n,
232 char *buf, size_t buflen)
234 int ret;
235 if (n >= buflen) {
236 return -EINVAL;
238 ret = bdrv_pread(file, offset, buf, n);
239 if (ret < 0) {
240 return ret;
242 buf[n] = '\0';
243 return 0;
247 * Allocate new clusters
249 * @s: QED state
250 * @n: Number of contiguous clusters to allocate
251 * @ret: Offset of first allocated cluster
253 * This function only produces the offset where the new clusters should be
254 * written. It updates BDRVQEDState but does not make any changes to the image
255 * file.
257 static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
259 uint64_t offset = s->file_size;
260 s->file_size += n * s->header.cluster_size;
261 return offset;
264 QEDTable *qed_alloc_table(BDRVQEDState *s)
266 /* Honor O_DIRECT memory alignment requirements */
267 return qemu_blockalign(s->bs,
268 s->header.cluster_size * s->header.table_size);
272 * Allocate a new zeroed L2 table
274 static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
276 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
278 l2_table->table = qed_alloc_table(s);
279 l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
281 memset(l2_table->table->offsets, 0,
282 s->header.cluster_size * s->header.table_size);
283 return l2_table;
286 static void qed_aio_next_io(void *opaque, int ret);
288 static void qed_plug_allocating_write_reqs(BDRVQEDState *s)
290 assert(!s->allocating_write_reqs_plugged);
292 s->allocating_write_reqs_plugged = true;
295 static void qed_unplug_allocating_write_reqs(BDRVQEDState *s)
297 QEDAIOCB *acb;
299 assert(s->allocating_write_reqs_plugged);
301 s->allocating_write_reqs_plugged = false;
303 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
304 if (acb) {
305 qed_aio_next_io(acb, 0);
309 static void qed_finish_clear_need_check(void *opaque, int ret)
311 /* Do nothing */
314 static void qed_flush_after_clear_need_check(void *opaque, int ret)
316 BDRVQEDState *s = opaque;
318 bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s);
320 /* No need to wait until flush completes */
321 qed_unplug_allocating_write_reqs(s);
324 static void qed_clear_need_check(void *opaque, int ret)
326 BDRVQEDState *s = opaque;
328 if (ret) {
329 qed_unplug_allocating_write_reqs(s);
330 return;
333 s->header.features &= ~QED_F_NEED_CHECK;
334 qed_write_header(s, qed_flush_after_clear_need_check, s);
337 static void qed_need_check_timer_cb(void *opaque)
339 BDRVQEDState *s = opaque;
341 /* The timer should only fire when allocating writes have drained */
342 assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs));
344 trace_qed_need_check_timer_cb(s);
346 qed_plug_allocating_write_reqs(s);
348 /* Ensure writes are on disk before clearing flag */
349 bdrv_aio_flush(s->bs, qed_clear_need_check, s);
352 static void qed_start_need_check_timer(BDRVQEDState *s)
354 trace_qed_start_need_check_timer(s);
356 /* Use vm_clock so we don't alter the image file while suspended for
357 * migration.
359 qemu_mod_timer(s->need_check_timer, qemu_get_clock_ns(vm_clock) +
360 get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT);
363 /* It's okay to call this multiple times or when no timer is started */
364 static void qed_cancel_need_check_timer(BDRVQEDState *s)
366 trace_qed_cancel_need_check_timer(s);
367 qemu_del_timer(s->need_check_timer);
370 static int bdrv_qed_open(BlockDriverState *bs, int flags)
372 BDRVQEDState *s = bs->opaque;
373 QEDHeader le_header;
374 int64_t file_size;
375 int ret;
377 s->bs = bs;
378 QSIMPLEQ_INIT(&s->allocating_write_reqs);
380 ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header));
381 if (ret < 0) {
382 return ret;
384 qed_header_le_to_cpu(&le_header, &s->header);
386 if (s->header.magic != QED_MAGIC) {
387 return -EINVAL;
389 if (s->header.features & ~QED_FEATURE_MASK) {
390 /* image uses unsupported feature bits */
391 char buf[64];
392 snprintf(buf, sizeof(buf), "%" PRIx64,
393 s->header.features & ~QED_FEATURE_MASK);
394 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
395 bs->device_name, "QED", buf);
396 return -ENOTSUP;
398 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
399 return -EINVAL;
402 /* Round down file size to the last cluster */
403 file_size = bdrv_getlength(bs->file);
404 if (file_size < 0) {
405 return file_size;
407 s->file_size = qed_start_of_cluster(s, file_size);
409 if (!qed_is_table_size_valid(s->header.table_size)) {
410 return -EINVAL;
412 if (!qed_is_image_size_valid(s->header.image_size,
413 s->header.cluster_size,
414 s->header.table_size)) {
415 return -EINVAL;
417 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
418 return -EINVAL;
421 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
422 sizeof(uint64_t);
423 s->l2_shift = ffs(s->header.cluster_size) - 1;
424 s->l2_mask = s->table_nelems - 1;
425 s->l1_shift = s->l2_shift + ffs(s->table_nelems) - 1;
427 if ((s->header.features & QED_F_BACKING_FILE)) {
428 if ((uint64_t)s->header.backing_filename_offset +
429 s->header.backing_filename_size >
430 s->header.cluster_size * s->header.header_size) {
431 return -EINVAL;
434 ret = qed_read_string(bs->file, s->header.backing_filename_offset,
435 s->header.backing_filename_size, bs->backing_file,
436 sizeof(bs->backing_file));
437 if (ret < 0) {
438 return ret;
441 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
442 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
446 /* Reset unknown autoclear feature bits. This is a backwards
447 * compatibility mechanism that allows images to be opened by older
448 * programs, which "knock out" unknown feature bits. When an image is
449 * opened by a newer program again it can detect that the autoclear
450 * feature is no longer valid.
452 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
453 !bdrv_is_read_only(bs->file) && !(flags & BDRV_O_INCOMING)) {
454 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
456 ret = qed_write_header_sync(s);
457 if (ret) {
458 return ret;
461 /* From here on only known autoclear feature bits are valid */
462 bdrv_flush(bs->file);
465 s->l1_table = qed_alloc_table(s);
466 qed_init_l2_cache(&s->l2_cache);
468 ret = qed_read_l1_table_sync(s);
469 if (ret) {
470 goto out;
473 /* If image was not closed cleanly, check consistency */
474 if (s->header.features & QED_F_NEED_CHECK) {
475 /* Read-only images cannot be fixed. There is no risk of corruption
476 * since write operations are not possible. Therefore, allow
477 * potentially inconsistent images to be opened read-only. This can
478 * aid data recovery from an otherwise inconsistent image.
480 if (!bdrv_is_read_only(bs->file) &&
481 !(flags & BDRV_O_INCOMING)) {
482 BdrvCheckResult result = {0};
484 ret = qed_check(s, &result, true);
485 if (ret) {
486 goto out;
488 if (!result.corruptions && !result.check_errors) {
489 /* Ensure fixes reach storage before clearing check bit */
490 bdrv_flush(s->bs);
492 s->header.features &= ~QED_F_NEED_CHECK;
493 qed_write_header_sync(s);
498 s->need_check_timer = qemu_new_timer_ns(vm_clock,
499 qed_need_check_timer_cb, s);
501 out:
502 if (ret) {
503 qed_free_l2_cache(&s->l2_cache);
504 qemu_vfree(s->l1_table);
506 return ret;
509 static void bdrv_qed_close(BlockDriverState *bs)
511 BDRVQEDState *s = bs->opaque;
513 qed_cancel_need_check_timer(s);
514 qemu_free_timer(s->need_check_timer);
516 /* Ensure writes reach stable storage */
517 bdrv_flush(bs->file);
519 /* Clean shutdown, no check required on next open */
520 if (s->header.features & QED_F_NEED_CHECK) {
521 s->header.features &= ~QED_F_NEED_CHECK;
522 qed_write_header_sync(s);
525 qed_free_l2_cache(&s->l2_cache);
526 qemu_vfree(s->l1_table);
529 static int qed_create(const char *filename, uint32_t cluster_size,
530 uint64_t image_size, uint32_t table_size,
531 const char *backing_file, const char *backing_fmt)
533 QEDHeader header = {
534 .magic = QED_MAGIC,
535 .cluster_size = cluster_size,
536 .table_size = table_size,
537 .header_size = 1,
538 .features = 0,
539 .compat_features = 0,
540 .l1_table_offset = cluster_size,
541 .image_size = image_size,
543 QEDHeader le_header;
544 uint8_t *l1_table = NULL;
545 size_t l1_size = header.cluster_size * header.table_size;
546 int ret = 0;
547 BlockDriverState *bs = NULL;
549 ret = bdrv_create_file(filename, NULL);
550 if (ret < 0) {
551 return ret;
554 ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR | BDRV_O_CACHE_WB);
555 if (ret < 0) {
556 return ret;
559 /* File must start empty and grow, check truncate is supported */
560 ret = bdrv_truncate(bs, 0);
561 if (ret < 0) {
562 goto out;
565 if (backing_file) {
566 header.features |= QED_F_BACKING_FILE;
567 header.backing_filename_offset = sizeof(le_header);
568 header.backing_filename_size = strlen(backing_file);
570 if (qed_fmt_is_raw(backing_fmt)) {
571 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
575 qed_header_cpu_to_le(&header, &le_header);
576 ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header));
577 if (ret < 0) {
578 goto out;
580 ret = bdrv_pwrite(bs, sizeof(le_header), backing_file,
581 header.backing_filename_size);
582 if (ret < 0) {
583 goto out;
586 l1_table = g_malloc0(l1_size);
587 ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size);
588 if (ret < 0) {
589 goto out;
592 ret = 0; /* success */
593 out:
594 g_free(l1_table);
595 bdrv_delete(bs);
596 return ret;
599 static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options)
601 uint64_t image_size = 0;
602 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
603 uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
604 const char *backing_file = NULL;
605 const char *backing_fmt = NULL;
607 while (options && options->name) {
608 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
609 image_size = options->value.n;
610 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
611 backing_file = options->value.s;
612 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
613 backing_fmt = options->value.s;
614 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
615 if (options->value.n) {
616 cluster_size = options->value.n;
618 } else if (!strcmp(options->name, BLOCK_OPT_TABLE_SIZE)) {
619 if (options->value.n) {
620 table_size = options->value.n;
623 options++;
626 if (!qed_is_cluster_size_valid(cluster_size)) {
627 fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n",
628 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
629 return -EINVAL;
631 if (!qed_is_table_size_valid(table_size)) {
632 fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n",
633 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
634 return -EINVAL;
636 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
637 fprintf(stderr, "QED image size must be a non-zero multiple of "
638 "cluster size and less than %" PRIu64 " bytes\n",
639 qed_max_image_size(cluster_size, table_size));
640 return -EINVAL;
643 return qed_create(filename, cluster_size, image_size, table_size,
644 backing_file, backing_fmt);
647 typedef struct {
648 Coroutine *co;
649 int is_allocated;
650 int *pnum;
651 } QEDIsAllocatedCB;
653 static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
655 QEDIsAllocatedCB *cb = opaque;
656 *cb->pnum = len / BDRV_SECTOR_SIZE;
657 cb->is_allocated = (ret == QED_CLUSTER_FOUND || ret == QED_CLUSTER_ZERO);
658 if (cb->co) {
659 qemu_coroutine_enter(cb->co, NULL);
663 static int coroutine_fn bdrv_qed_co_is_allocated(BlockDriverState *bs,
664 int64_t sector_num,
665 int nb_sectors, int *pnum)
667 BDRVQEDState *s = bs->opaque;
668 uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
669 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
670 QEDIsAllocatedCB cb = {
671 .is_allocated = -1,
672 .pnum = pnum,
674 QEDRequest request = { .l2_table = NULL };
676 qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb);
678 /* Now sleep if the callback wasn't invoked immediately */
679 while (cb.is_allocated == -1) {
680 cb.co = qemu_coroutine_self();
681 qemu_coroutine_yield();
684 qed_unref_l2_cache_entry(request.l2_table);
686 return cb.is_allocated;
689 static int bdrv_qed_make_empty(BlockDriverState *bs)
691 return -ENOTSUP;
694 static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
696 return acb->common.bs->opaque;
700 * Read from the backing file or zero-fill if no backing file
702 * @s: QED state
703 * @pos: Byte position in device
704 * @qiov: Destination I/O vector
705 * @cb: Completion function
706 * @opaque: User data for completion function
708 * This function reads qiov->size bytes starting at pos from the backing file.
709 * If there is no backing file then zeroes are read.
711 static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
712 QEMUIOVector *qiov,
713 BlockDriverCompletionFunc *cb, void *opaque)
715 uint64_t backing_length = 0;
716 size_t size;
718 /* If there is a backing file, get its length. Treat the absence of a
719 * backing file like a zero length backing file.
721 if (s->bs->backing_hd) {
722 int64_t l = bdrv_getlength(s->bs->backing_hd);
723 if (l < 0) {
724 cb(opaque, l);
725 return;
727 backing_length = l;
730 /* Zero all sectors if reading beyond the end of the backing file */
731 if (pos >= backing_length ||
732 pos + qiov->size > backing_length) {
733 qemu_iovec_memset(qiov, 0, qiov->size);
736 /* Complete now if there are no backing file sectors to read */
737 if (pos >= backing_length) {
738 cb(opaque, 0);
739 return;
742 /* If the read straddles the end of the backing file, shorten it */
743 size = MIN((uint64_t)backing_length - pos, qiov->size);
745 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING);
746 bdrv_aio_readv(s->bs->backing_hd, pos / BDRV_SECTOR_SIZE,
747 qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
750 typedef struct {
751 GenericCB gencb;
752 BDRVQEDState *s;
753 QEMUIOVector qiov;
754 struct iovec iov;
755 uint64_t offset;
756 } CopyFromBackingFileCB;
758 static void qed_copy_from_backing_file_cb(void *opaque, int ret)
760 CopyFromBackingFileCB *copy_cb = opaque;
761 qemu_vfree(copy_cb->iov.iov_base);
762 gencb_complete(&copy_cb->gencb, ret);
765 static void qed_copy_from_backing_file_write(void *opaque, int ret)
767 CopyFromBackingFileCB *copy_cb = opaque;
768 BDRVQEDState *s = copy_cb->s;
770 if (ret) {
771 qed_copy_from_backing_file_cb(copy_cb, ret);
772 return;
775 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
776 bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE,
777 &copy_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE,
778 qed_copy_from_backing_file_cb, copy_cb);
782 * Copy data from backing file into the image
784 * @s: QED state
785 * @pos: Byte position in device
786 * @len: Number of bytes
787 * @offset: Byte offset in image file
788 * @cb: Completion function
789 * @opaque: User data for completion function
791 static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
792 uint64_t len, uint64_t offset,
793 BlockDriverCompletionFunc *cb,
794 void *opaque)
796 CopyFromBackingFileCB *copy_cb;
798 /* Skip copy entirely if there is no work to do */
799 if (len == 0) {
800 cb(opaque, 0);
801 return;
804 copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
805 copy_cb->s = s;
806 copy_cb->offset = offset;
807 copy_cb->iov.iov_base = qemu_blockalign(s->bs, len);
808 copy_cb->iov.iov_len = len;
809 qemu_iovec_init_external(&copy_cb->qiov, &copy_cb->iov, 1);
811 qed_read_backing_file(s, pos, &copy_cb->qiov,
812 qed_copy_from_backing_file_write, copy_cb);
816 * Link one or more contiguous clusters into a table
818 * @s: QED state
819 * @table: L2 table
820 * @index: First cluster index
821 * @n: Number of contiguous clusters
822 * @cluster: First cluster offset
824 * The cluster offset may be an allocated byte offset in the image file, the
825 * zero cluster marker, or the unallocated cluster marker.
827 static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
828 unsigned int n, uint64_t cluster)
830 int i;
831 for (i = index; i < index + n; i++) {
832 table->offsets[i] = cluster;
833 if (!qed_offset_is_unalloc_cluster(cluster) &&
834 !qed_offset_is_zero_cluster(cluster)) {
835 cluster += s->header.cluster_size;
840 static void qed_aio_complete_bh(void *opaque)
842 QEDAIOCB *acb = opaque;
843 BlockDriverCompletionFunc *cb = acb->common.cb;
844 void *user_opaque = acb->common.opaque;
845 int ret = acb->bh_ret;
846 bool *finished = acb->finished;
848 qemu_bh_delete(acb->bh);
849 qemu_aio_release(acb);
851 /* Invoke callback */
852 cb(user_opaque, ret);
854 /* Signal cancel completion */
855 if (finished) {
856 *finished = true;
860 static void qed_aio_complete(QEDAIOCB *acb, int ret)
862 BDRVQEDState *s = acb_to_s(acb);
864 trace_qed_aio_complete(s, acb, ret);
866 /* Free resources */
867 qemu_iovec_destroy(&acb->cur_qiov);
868 qed_unref_l2_cache_entry(acb->request.l2_table);
870 /* Free the buffer we may have allocated for zero writes */
871 if (acb->flags & QED_AIOCB_ZERO) {
872 qemu_vfree(acb->qiov->iov[0].iov_base);
873 acb->qiov->iov[0].iov_base = NULL;
876 /* Arrange for a bh to invoke the completion function */
877 acb->bh_ret = ret;
878 acb->bh = qemu_bh_new(qed_aio_complete_bh, acb);
879 qemu_bh_schedule(acb->bh);
881 /* Start next allocating write request waiting behind this one. Note that
882 * requests enqueue themselves when they first hit an unallocated cluster
883 * but they wait until the entire request is finished before waking up the
884 * next request in the queue. This ensures that we don't cycle through
885 * requests multiple times but rather finish one at a time completely.
887 if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
888 QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
889 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
890 if (acb) {
891 qed_aio_next_io(acb, 0);
892 } else if (s->header.features & QED_F_NEED_CHECK) {
893 qed_start_need_check_timer(s);
899 * Commit the current L2 table to the cache
901 static void qed_commit_l2_update(void *opaque, int ret)
903 QEDAIOCB *acb = opaque;
904 BDRVQEDState *s = acb_to_s(acb);
905 CachedL2Table *l2_table = acb->request.l2_table;
906 uint64_t l2_offset = l2_table->offset;
908 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
910 /* This is guaranteed to succeed because we just committed the entry to the
911 * cache.
913 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
914 assert(acb->request.l2_table != NULL);
916 qed_aio_next_io(opaque, ret);
920 * Update L1 table with new L2 table offset and write it out
922 static void qed_aio_write_l1_update(void *opaque, int ret)
924 QEDAIOCB *acb = opaque;
925 BDRVQEDState *s = acb_to_s(acb);
926 int index;
928 if (ret) {
929 qed_aio_complete(acb, ret);
930 return;
933 index = qed_l1_index(s, acb->cur_pos);
934 s->l1_table->offsets[index] = acb->request.l2_table->offset;
936 qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
940 * Update L2 table with new cluster offsets and write them out
942 static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset)
944 BDRVQEDState *s = acb_to_s(acb);
945 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
946 int index;
948 if (ret) {
949 goto err;
952 if (need_alloc) {
953 qed_unref_l2_cache_entry(acb->request.l2_table);
954 acb->request.l2_table = qed_new_l2_table(s);
957 index = qed_l2_index(s, acb->cur_pos);
958 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
959 offset);
961 if (need_alloc) {
962 /* Write out the whole new L2 table */
963 qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true,
964 qed_aio_write_l1_update, acb);
965 } else {
966 /* Write out only the updated part of the L2 table */
967 qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false,
968 qed_aio_next_io, acb);
970 return;
972 err:
973 qed_aio_complete(acb, ret);
976 static void qed_aio_write_l2_update_cb(void *opaque, int ret)
978 QEDAIOCB *acb = opaque;
979 qed_aio_write_l2_update(acb, ret, acb->cur_cluster);
983 * Flush new data clusters before updating the L2 table
985 * This flush is necessary when a backing file is in use. A crash during an
986 * allocating write could result in empty clusters in the image. If the write
987 * only touched a subregion of the cluster, then backing image sectors have
988 * been lost in the untouched region. The solution is to flush after writing a
989 * new data cluster and before updating the L2 table.
991 static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
993 QEDAIOCB *acb = opaque;
994 BDRVQEDState *s = acb_to_s(acb);
996 if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update_cb, opaque)) {
997 qed_aio_complete(acb, -EIO);
1002 * Write data to the image file
1004 static void qed_aio_write_main(void *opaque, int ret)
1006 QEDAIOCB *acb = opaque;
1007 BDRVQEDState *s = acb_to_s(acb);
1008 uint64_t offset = acb->cur_cluster +
1009 qed_offset_into_cluster(s, acb->cur_pos);
1010 BlockDriverCompletionFunc *next_fn;
1012 trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
1014 if (ret) {
1015 qed_aio_complete(acb, ret);
1016 return;
1019 if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
1020 next_fn = qed_aio_next_io;
1021 } else {
1022 if (s->bs->backing_hd) {
1023 next_fn = qed_aio_write_flush_before_l2_update;
1024 } else {
1025 next_fn = qed_aio_write_l2_update_cb;
1029 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
1030 bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE,
1031 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1032 next_fn, acb);
1036 * Populate back untouched region of new data cluster
1038 static void qed_aio_write_postfill(void *opaque, int ret)
1040 QEDAIOCB *acb = opaque;
1041 BDRVQEDState *s = acb_to_s(acb);
1042 uint64_t start = acb->cur_pos + acb->cur_qiov.size;
1043 uint64_t len =
1044 qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1045 uint64_t offset = acb->cur_cluster +
1046 qed_offset_into_cluster(s, acb->cur_pos) +
1047 acb->cur_qiov.size;
1049 if (ret) {
1050 qed_aio_complete(acb, ret);
1051 return;
1054 trace_qed_aio_write_postfill(s, acb, start, len, offset);
1055 qed_copy_from_backing_file(s, start, len, offset,
1056 qed_aio_write_main, acb);
1060 * Populate front untouched region of new data cluster
1062 static void qed_aio_write_prefill(void *opaque, int ret)
1064 QEDAIOCB *acb = opaque;
1065 BDRVQEDState *s = acb_to_s(acb);
1066 uint64_t start = qed_start_of_cluster(s, acb->cur_pos);
1067 uint64_t len = qed_offset_into_cluster(s, acb->cur_pos);
1069 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1070 qed_copy_from_backing_file(s, start, len, acb->cur_cluster,
1071 qed_aio_write_postfill, acb);
1075 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1077 static bool qed_should_set_need_check(BDRVQEDState *s)
1079 /* The flush before L2 update path ensures consistency */
1080 if (s->bs->backing_hd) {
1081 return false;
1084 return !(s->header.features & QED_F_NEED_CHECK);
1087 static void qed_aio_write_zero_cluster(void *opaque, int ret)
1089 QEDAIOCB *acb = opaque;
1091 if (ret) {
1092 qed_aio_complete(acb, ret);
1093 return;
1096 qed_aio_write_l2_update(acb, 0, 1);
1100 * Write new data cluster
1102 * @acb: Write request
1103 * @len: Length in bytes
1105 * This path is taken when writing to previously unallocated clusters.
1107 static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1109 BDRVQEDState *s = acb_to_s(acb);
1110 BlockDriverCompletionFunc *cb;
1112 /* Cancel timer when the first allocating request comes in */
1113 if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) {
1114 qed_cancel_need_check_timer(s);
1117 /* Freeze this request if another allocating write is in progress */
1118 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
1119 QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
1121 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) ||
1122 s->allocating_write_reqs_plugged) {
1123 return; /* wait for existing request to finish */
1126 acb->cur_nclusters = qed_bytes_to_clusters(s,
1127 qed_offset_into_cluster(s, acb->cur_pos) + len);
1128 qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1130 if (acb->flags & QED_AIOCB_ZERO) {
1131 /* Skip ahead if the clusters are already zero */
1132 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
1133 qed_aio_next_io(acb, 0);
1134 return;
1137 cb = qed_aio_write_zero_cluster;
1138 } else {
1139 cb = qed_aio_write_prefill;
1140 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1143 if (qed_should_set_need_check(s)) {
1144 s->header.features |= QED_F_NEED_CHECK;
1145 qed_write_header(s, cb, acb);
1146 } else {
1147 cb(acb, 0);
1152 * Write data cluster in place
1154 * @acb: Write request
1155 * @offset: Cluster offset in bytes
1156 * @len: Length in bytes
1158 * This path is taken when writing to already allocated clusters.
1160 static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1162 /* Allocate buffer for zero writes */
1163 if (acb->flags & QED_AIOCB_ZERO) {
1164 struct iovec *iov = acb->qiov->iov;
1166 if (!iov->iov_base) {
1167 iov->iov_base = qemu_blockalign(acb->common.bs, iov->iov_len);
1168 memset(iov->iov_base, 0, iov->iov_len);
1172 /* Calculate the I/O vector */
1173 acb->cur_cluster = offset;
1174 qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1176 /* Do the actual write */
1177 qed_aio_write_main(acb, 0);
1181 * Write data cluster
1183 * @opaque: Write request
1184 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1185 * or -errno
1186 * @offset: Cluster offset in bytes
1187 * @len: Length in bytes
1189 * Callback from qed_find_cluster().
1191 static void qed_aio_write_data(void *opaque, int ret,
1192 uint64_t offset, size_t len)
1194 QEDAIOCB *acb = opaque;
1196 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1198 acb->find_cluster_ret = ret;
1200 switch (ret) {
1201 case QED_CLUSTER_FOUND:
1202 qed_aio_write_inplace(acb, offset, len);
1203 break;
1205 case QED_CLUSTER_L2:
1206 case QED_CLUSTER_L1:
1207 case QED_CLUSTER_ZERO:
1208 qed_aio_write_alloc(acb, len);
1209 break;
1211 default:
1212 qed_aio_complete(acb, ret);
1213 break;
1218 * Read data cluster
1220 * @opaque: Read request
1221 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1222 * or -errno
1223 * @offset: Cluster offset in bytes
1224 * @len: Length in bytes
1226 * Callback from qed_find_cluster().
1228 static void qed_aio_read_data(void *opaque, int ret,
1229 uint64_t offset, size_t len)
1231 QEDAIOCB *acb = opaque;
1232 BDRVQEDState *s = acb_to_s(acb);
1233 BlockDriverState *bs = acb->common.bs;
1235 /* Adjust offset into cluster */
1236 offset += qed_offset_into_cluster(s, acb->cur_pos);
1238 trace_qed_aio_read_data(s, acb, ret, offset, len);
1240 if (ret < 0) {
1241 goto err;
1244 qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1246 /* Handle zero cluster and backing file reads */
1247 if (ret == QED_CLUSTER_ZERO) {
1248 qemu_iovec_memset(&acb->cur_qiov, 0, acb->cur_qiov.size);
1249 qed_aio_next_io(acb, 0);
1250 return;
1251 } else if (ret != QED_CLUSTER_FOUND) {
1252 qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
1253 qed_aio_next_io, acb);
1254 return;
1257 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1258 bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE,
1259 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1260 qed_aio_next_io, acb);
1261 return;
1263 err:
1264 qed_aio_complete(acb, ret);
1268 * Begin next I/O or complete the request
1270 static void qed_aio_next_io(void *opaque, int ret)
1272 QEDAIOCB *acb = opaque;
1273 BDRVQEDState *s = acb_to_s(acb);
1274 QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ?
1275 qed_aio_write_data : qed_aio_read_data;
1277 trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
1279 /* Handle I/O error */
1280 if (ret) {
1281 qed_aio_complete(acb, ret);
1282 return;
1285 acb->qiov_offset += acb->cur_qiov.size;
1286 acb->cur_pos += acb->cur_qiov.size;
1287 qemu_iovec_reset(&acb->cur_qiov);
1289 /* Complete request */
1290 if (acb->cur_pos >= acb->end_pos) {
1291 qed_aio_complete(acb, 0);
1292 return;
1295 /* Find next cluster and start I/O */
1296 qed_find_cluster(s, &acb->request,
1297 acb->cur_pos, acb->end_pos - acb->cur_pos,
1298 io_fn, acb);
1301 static BlockDriverAIOCB *qed_aio_setup(BlockDriverState *bs,
1302 int64_t sector_num,
1303 QEMUIOVector *qiov, int nb_sectors,
1304 BlockDriverCompletionFunc *cb,
1305 void *opaque, int flags)
1307 QEDAIOCB *acb = qemu_aio_get(&qed_aio_pool, bs, cb, opaque);
1309 trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
1310 opaque, flags);
1312 acb->flags = flags;
1313 acb->finished = NULL;
1314 acb->qiov = qiov;
1315 acb->qiov_offset = 0;
1316 acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
1317 acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
1318 acb->request.l2_table = NULL;
1319 qemu_iovec_init(&acb->cur_qiov, qiov->niov);
1321 /* Start request */
1322 qed_aio_next_io(acb, 0);
1323 return &acb->common;
1326 static BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
1327 int64_t sector_num,
1328 QEMUIOVector *qiov, int nb_sectors,
1329 BlockDriverCompletionFunc *cb,
1330 void *opaque)
1332 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1335 static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
1336 int64_t sector_num,
1337 QEMUIOVector *qiov, int nb_sectors,
1338 BlockDriverCompletionFunc *cb,
1339 void *opaque)
1341 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb,
1342 opaque, QED_AIOCB_WRITE);
1345 typedef struct {
1346 Coroutine *co;
1347 int ret;
1348 bool done;
1349 } QEDWriteZeroesCB;
1351 static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret)
1353 QEDWriteZeroesCB *cb = opaque;
1355 cb->done = true;
1356 cb->ret = ret;
1357 if (cb->co) {
1358 qemu_coroutine_enter(cb->co, NULL);
1362 static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs,
1363 int64_t sector_num,
1364 int nb_sectors)
1366 BlockDriverAIOCB *blockacb;
1367 QEDWriteZeroesCB cb = { .done = false };
1368 QEMUIOVector qiov;
1369 struct iovec iov;
1371 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1372 * then it will be allocated during request processing.
1374 iov.iov_base = NULL,
1375 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1377 qemu_iovec_init_external(&qiov, &iov, 1);
1378 blockacb = qed_aio_setup(bs, sector_num, &qiov, nb_sectors,
1379 qed_co_write_zeroes_cb, &cb,
1380 QED_AIOCB_WRITE | QED_AIOCB_ZERO);
1381 if (!blockacb) {
1382 return -EIO;
1384 if (!cb.done) {
1385 cb.co = qemu_coroutine_self();
1386 qemu_coroutine_yield();
1388 assert(cb.done);
1389 return cb.ret;
1392 static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset)
1394 BDRVQEDState *s = bs->opaque;
1395 uint64_t old_image_size;
1396 int ret;
1398 if (!qed_is_image_size_valid(offset, s->header.cluster_size,
1399 s->header.table_size)) {
1400 return -EINVAL;
1403 /* Shrinking is currently not supported */
1404 if ((uint64_t)offset < s->header.image_size) {
1405 return -ENOTSUP;
1408 old_image_size = s->header.image_size;
1409 s->header.image_size = offset;
1410 ret = qed_write_header_sync(s);
1411 if (ret < 0) {
1412 s->header.image_size = old_image_size;
1414 return ret;
1417 static int64_t bdrv_qed_getlength(BlockDriverState *bs)
1419 BDRVQEDState *s = bs->opaque;
1420 return s->header.image_size;
1423 static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1425 BDRVQEDState *s = bs->opaque;
1427 memset(bdi, 0, sizeof(*bdi));
1428 bdi->cluster_size = s->header.cluster_size;
1429 bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
1430 return 0;
1433 static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1434 const char *backing_file,
1435 const char *backing_fmt)
1437 BDRVQEDState *s = bs->opaque;
1438 QEDHeader new_header, le_header;
1439 void *buffer;
1440 size_t buffer_len, backing_file_len;
1441 int ret;
1443 /* Refuse to set backing filename if unknown compat feature bits are
1444 * active. If the image uses an unknown compat feature then we may not
1445 * know the layout of data following the header structure and cannot safely
1446 * add a new string.
1448 if (backing_file && (s->header.compat_features &
1449 ~QED_COMPAT_FEATURE_MASK)) {
1450 return -ENOTSUP;
1453 memcpy(&new_header, &s->header, sizeof(new_header));
1455 new_header.features &= ~(QED_F_BACKING_FILE |
1456 QED_F_BACKING_FORMAT_NO_PROBE);
1458 /* Adjust feature flags */
1459 if (backing_file) {
1460 new_header.features |= QED_F_BACKING_FILE;
1462 if (qed_fmt_is_raw(backing_fmt)) {
1463 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1467 /* Calculate new header size */
1468 backing_file_len = 0;
1470 if (backing_file) {
1471 backing_file_len = strlen(backing_file);
1474 buffer_len = sizeof(new_header);
1475 new_header.backing_filename_offset = buffer_len;
1476 new_header.backing_filename_size = backing_file_len;
1477 buffer_len += backing_file_len;
1479 /* Make sure we can rewrite header without failing */
1480 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1481 return -ENOSPC;
1484 /* Prepare new header */
1485 buffer = g_malloc(buffer_len);
1487 qed_header_cpu_to_le(&new_header, &le_header);
1488 memcpy(buffer, &le_header, sizeof(le_header));
1489 buffer_len = sizeof(le_header);
1491 if (backing_file) {
1492 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1493 buffer_len += backing_file_len;
1496 /* Write new header */
1497 ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len);
1498 g_free(buffer);
1499 if (ret == 0) {
1500 memcpy(&s->header, &new_header, sizeof(new_header));
1502 return ret;
1505 static void bdrv_qed_invalidate_cache(BlockDriverState *bs)
1507 BDRVQEDState *s = bs->opaque;
1509 bdrv_qed_close(bs);
1510 memset(s, 0, sizeof(BDRVQEDState));
1511 bdrv_qed_open(bs, bs->open_flags);
1514 static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result)
1516 BDRVQEDState *s = bs->opaque;
1518 return qed_check(s, result, false);
1521 static QEMUOptionParameter qed_create_options[] = {
1523 .name = BLOCK_OPT_SIZE,
1524 .type = OPT_SIZE,
1525 .help = "Virtual disk size (in bytes)"
1526 }, {
1527 .name = BLOCK_OPT_BACKING_FILE,
1528 .type = OPT_STRING,
1529 .help = "File name of a base image"
1530 }, {
1531 .name = BLOCK_OPT_BACKING_FMT,
1532 .type = OPT_STRING,
1533 .help = "Image format of the base image"
1534 }, {
1535 .name = BLOCK_OPT_CLUSTER_SIZE,
1536 .type = OPT_SIZE,
1537 .help = "Cluster size (in bytes)",
1538 .value = { .n = QED_DEFAULT_CLUSTER_SIZE },
1539 }, {
1540 .name = BLOCK_OPT_TABLE_SIZE,
1541 .type = OPT_SIZE,
1542 .help = "L1/L2 table size (in clusters)"
1544 { /* end of list */ }
1547 static BlockDriver bdrv_qed = {
1548 .format_name = "qed",
1549 .instance_size = sizeof(BDRVQEDState),
1550 .create_options = qed_create_options,
1552 .bdrv_probe = bdrv_qed_probe,
1553 .bdrv_open = bdrv_qed_open,
1554 .bdrv_close = bdrv_qed_close,
1555 .bdrv_create = bdrv_qed_create,
1556 .bdrv_co_is_allocated = bdrv_qed_co_is_allocated,
1557 .bdrv_make_empty = bdrv_qed_make_empty,
1558 .bdrv_aio_readv = bdrv_qed_aio_readv,
1559 .bdrv_aio_writev = bdrv_qed_aio_writev,
1560 .bdrv_co_write_zeroes = bdrv_qed_co_write_zeroes,
1561 .bdrv_truncate = bdrv_qed_truncate,
1562 .bdrv_getlength = bdrv_qed_getlength,
1563 .bdrv_get_info = bdrv_qed_get_info,
1564 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
1565 .bdrv_invalidate_cache = bdrv_qed_invalidate_cache,
1566 .bdrv_check = bdrv_qed_check,
1569 static void bdrv_qed_init(void)
1571 bdrv_register(&bdrv_qed);
1574 block_init(bdrv_qed_init);