sockets: Drop sockets_debug debug code
[qemu/agraf.git] / block / qed.c
bloba041d31e661a652a79c1c02c096cd563a4b6efb0
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)) {
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 BdrvCheckResult result = {0};
483 ret = qed_check(s, &result, true);
484 if (ret) {
485 goto out;
487 if (!result.corruptions && !result.check_errors) {
488 /* Ensure fixes reach storage before clearing check bit */
489 bdrv_flush(s->bs);
491 s->header.features &= ~QED_F_NEED_CHECK;
492 qed_write_header_sync(s);
497 s->need_check_timer = qemu_new_timer_ns(vm_clock,
498 qed_need_check_timer_cb, s);
500 error_set(&s->migration_blocker,
501 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
502 "qed", bs->device_name, "live migration");
503 migrate_add_blocker(s->migration_blocker);
506 out:
507 if (ret) {
508 qed_free_l2_cache(&s->l2_cache);
509 qemu_vfree(s->l1_table);
511 return ret;
514 static void bdrv_qed_close(BlockDriverState *bs)
516 BDRVQEDState *s = bs->opaque;
518 migrate_del_blocker(s->migration_blocker);
519 error_free(s->migration_blocker);
521 qed_cancel_need_check_timer(s);
522 qemu_free_timer(s->need_check_timer);
524 /* Ensure writes reach stable storage */
525 bdrv_flush(bs->file);
527 /* Clean shutdown, no check required on next open */
528 if (s->header.features & QED_F_NEED_CHECK) {
529 s->header.features &= ~QED_F_NEED_CHECK;
530 qed_write_header_sync(s);
533 qed_free_l2_cache(&s->l2_cache);
534 qemu_vfree(s->l1_table);
537 static int qed_create(const char *filename, uint32_t cluster_size,
538 uint64_t image_size, uint32_t table_size,
539 const char *backing_file, const char *backing_fmt)
541 QEDHeader header = {
542 .magic = QED_MAGIC,
543 .cluster_size = cluster_size,
544 .table_size = table_size,
545 .header_size = 1,
546 .features = 0,
547 .compat_features = 0,
548 .l1_table_offset = cluster_size,
549 .image_size = image_size,
551 QEDHeader le_header;
552 uint8_t *l1_table = NULL;
553 size_t l1_size = header.cluster_size * header.table_size;
554 int ret = 0;
555 BlockDriverState *bs = NULL;
557 ret = bdrv_create_file(filename, NULL);
558 if (ret < 0) {
559 return ret;
562 ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR | BDRV_O_CACHE_WB);
563 if (ret < 0) {
564 return ret;
567 /* File must start empty and grow, check truncate is supported */
568 ret = bdrv_truncate(bs, 0);
569 if (ret < 0) {
570 goto out;
573 if (backing_file) {
574 header.features |= QED_F_BACKING_FILE;
575 header.backing_filename_offset = sizeof(le_header);
576 header.backing_filename_size = strlen(backing_file);
578 if (qed_fmt_is_raw(backing_fmt)) {
579 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
583 qed_header_cpu_to_le(&header, &le_header);
584 ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header));
585 if (ret < 0) {
586 goto out;
588 ret = bdrv_pwrite(bs, sizeof(le_header), backing_file,
589 header.backing_filename_size);
590 if (ret < 0) {
591 goto out;
594 l1_table = g_malloc0(l1_size);
595 ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size);
596 if (ret < 0) {
597 goto out;
600 ret = 0; /* success */
601 out:
602 g_free(l1_table);
603 bdrv_delete(bs);
604 return ret;
607 static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options)
609 uint64_t image_size = 0;
610 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
611 uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
612 const char *backing_file = NULL;
613 const char *backing_fmt = NULL;
615 while (options && options->name) {
616 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
617 image_size = options->value.n;
618 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
619 backing_file = options->value.s;
620 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
621 backing_fmt = options->value.s;
622 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
623 if (options->value.n) {
624 cluster_size = options->value.n;
626 } else if (!strcmp(options->name, BLOCK_OPT_TABLE_SIZE)) {
627 if (options->value.n) {
628 table_size = options->value.n;
631 options++;
634 if (!qed_is_cluster_size_valid(cluster_size)) {
635 fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n",
636 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
637 return -EINVAL;
639 if (!qed_is_table_size_valid(table_size)) {
640 fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n",
641 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
642 return -EINVAL;
644 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
645 fprintf(stderr, "QED image size must be a non-zero multiple of "
646 "cluster size and less than %" PRIu64 " bytes\n",
647 qed_max_image_size(cluster_size, table_size));
648 return -EINVAL;
651 return qed_create(filename, cluster_size, image_size, table_size,
652 backing_file, backing_fmt);
655 typedef struct {
656 Coroutine *co;
657 int is_allocated;
658 int *pnum;
659 } QEDIsAllocatedCB;
661 static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
663 QEDIsAllocatedCB *cb = opaque;
664 *cb->pnum = len / BDRV_SECTOR_SIZE;
665 cb->is_allocated = (ret == QED_CLUSTER_FOUND || ret == QED_CLUSTER_ZERO);
666 if (cb->co) {
667 qemu_coroutine_enter(cb->co, NULL);
671 static int coroutine_fn bdrv_qed_co_is_allocated(BlockDriverState *bs,
672 int64_t sector_num,
673 int nb_sectors, int *pnum)
675 BDRVQEDState *s = bs->opaque;
676 uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
677 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
678 QEDIsAllocatedCB cb = {
679 .is_allocated = -1,
680 .pnum = pnum,
682 QEDRequest request = { .l2_table = NULL };
684 qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb);
686 /* Now sleep if the callback wasn't invoked immediately */
687 while (cb.is_allocated == -1) {
688 cb.co = qemu_coroutine_self();
689 qemu_coroutine_yield();
692 qed_unref_l2_cache_entry(request.l2_table);
694 return cb.is_allocated;
697 static int bdrv_qed_make_empty(BlockDriverState *bs)
699 return -ENOTSUP;
702 static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
704 return acb->common.bs->opaque;
708 * Read from the backing file or zero-fill if no backing file
710 * @s: QED state
711 * @pos: Byte position in device
712 * @qiov: Destination I/O vector
713 * @cb: Completion function
714 * @opaque: User data for completion function
716 * This function reads qiov->size bytes starting at pos from the backing file.
717 * If there is no backing file then zeroes are read.
719 static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
720 QEMUIOVector *qiov,
721 BlockDriverCompletionFunc *cb, void *opaque)
723 uint64_t backing_length = 0;
724 size_t size;
726 /* If there is a backing file, get its length. Treat the absence of a
727 * backing file like a zero length backing file.
729 if (s->bs->backing_hd) {
730 int64_t l = bdrv_getlength(s->bs->backing_hd);
731 if (l < 0) {
732 cb(opaque, l);
733 return;
735 backing_length = l;
738 /* Zero all sectors if reading beyond the end of the backing file */
739 if (pos >= backing_length ||
740 pos + qiov->size > backing_length) {
741 qemu_iovec_memset(qiov, 0, qiov->size);
744 /* Complete now if there are no backing file sectors to read */
745 if (pos >= backing_length) {
746 cb(opaque, 0);
747 return;
750 /* If the read straddles the end of the backing file, shorten it */
751 size = MIN((uint64_t)backing_length - pos, qiov->size);
753 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING);
754 bdrv_aio_readv(s->bs->backing_hd, pos / BDRV_SECTOR_SIZE,
755 qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
758 typedef struct {
759 GenericCB gencb;
760 BDRVQEDState *s;
761 QEMUIOVector qiov;
762 struct iovec iov;
763 uint64_t offset;
764 } CopyFromBackingFileCB;
766 static void qed_copy_from_backing_file_cb(void *opaque, int ret)
768 CopyFromBackingFileCB *copy_cb = opaque;
769 qemu_vfree(copy_cb->iov.iov_base);
770 gencb_complete(&copy_cb->gencb, ret);
773 static void qed_copy_from_backing_file_write(void *opaque, int ret)
775 CopyFromBackingFileCB *copy_cb = opaque;
776 BDRVQEDState *s = copy_cb->s;
778 if (ret) {
779 qed_copy_from_backing_file_cb(copy_cb, ret);
780 return;
783 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
784 bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE,
785 &copy_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE,
786 qed_copy_from_backing_file_cb, copy_cb);
790 * Copy data from backing file into the image
792 * @s: QED state
793 * @pos: Byte position in device
794 * @len: Number of bytes
795 * @offset: Byte offset in image file
796 * @cb: Completion function
797 * @opaque: User data for completion function
799 static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
800 uint64_t len, uint64_t offset,
801 BlockDriverCompletionFunc *cb,
802 void *opaque)
804 CopyFromBackingFileCB *copy_cb;
806 /* Skip copy entirely if there is no work to do */
807 if (len == 0) {
808 cb(opaque, 0);
809 return;
812 copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
813 copy_cb->s = s;
814 copy_cb->offset = offset;
815 copy_cb->iov.iov_base = qemu_blockalign(s->bs, len);
816 copy_cb->iov.iov_len = len;
817 qemu_iovec_init_external(&copy_cb->qiov, &copy_cb->iov, 1);
819 qed_read_backing_file(s, pos, &copy_cb->qiov,
820 qed_copy_from_backing_file_write, copy_cb);
824 * Link one or more contiguous clusters into a table
826 * @s: QED state
827 * @table: L2 table
828 * @index: First cluster index
829 * @n: Number of contiguous clusters
830 * @cluster: First cluster offset
832 * The cluster offset may be an allocated byte offset in the image file, the
833 * zero cluster marker, or the unallocated cluster marker.
835 static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
836 unsigned int n, uint64_t cluster)
838 int i;
839 for (i = index; i < index + n; i++) {
840 table->offsets[i] = cluster;
841 if (!qed_offset_is_unalloc_cluster(cluster) &&
842 !qed_offset_is_zero_cluster(cluster)) {
843 cluster += s->header.cluster_size;
848 static void qed_aio_complete_bh(void *opaque)
850 QEDAIOCB *acb = opaque;
851 BlockDriverCompletionFunc *cb = acb->common.cb;
852 void *user_opaque = acb->common.opaque;
853 int ret = acb->bh_ret;
854 bool *finished = acb->finished;
856 qemu_bh_delete(acb->bh);
857 qemu_aio_release(acb);
859 /* Invoke callback */
860 cb(user_opaque, ret);
862 /* Signal cancel completion */
863 if (finished) {
864 *finished = true;
868 static void qed_aio_complete(QEDAIOCB *acb, int ret)
870 BDRVQEDState *s = acb_to_s(acb);
872 trace_qed_aio_complete(s, acb, ret);
874 /* Free resources */
875 qemu_iovec_destroy(&acb->cur_qiov);
876 qed_unref_l2_cache_entry(acb->request.l2_table);
878 /* Free the buffer we may have allocated for zero writes */
879 if (acb->flags & QED_AIOCB_ZERO) {
880 qemu_vfree(acb->qiov->iov[0].iov_base);
881 acb->qiov->iov[0].iov_base = NULL;
884 /* Arrange for a bh to invoke the completion function */
885 acb->bh_ret = ret;
886 acb->bh = qemu_bh_new(qed_aio_complete_bh, acb);
887 qemu_bh_schedule(acb->bh);
889 /* Start next allocating write request waiting behind this one. Note that
890 * requests enqueue themselves when they first hit an unallocated cluster
891 * but they wait until the entire request is finished before waking up the
892 * next request in the queue. This ensures that we don't cycle through
893 * requests multiple times but rather finish one at a time completely.
895 if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
896 QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
897 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
898 if (acb) {
899 qed_aio_next_io(acb, 0);
900 } else if (s->header.features & QED_F_NEED_CHECK) {
901 qed_start_need_check_timer(s);
907 * Commit the current L2 table to the cache
909 static void qed_commit_l2_update(void *opaque, int ret)
911 QEDAIOCB *acb = opaque;
912 BDRVQEDState *s = acb_to_s(acb);
913 CachedL2Table *l2_table = acb->request.l2_table;
914 uint64_t l2_offset = l2_table->offset;
916 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
918 /* This is guaranteed to succeed because we just committed the entry to the
919 * cache.
921 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
922 assert(acb->request.l2_table != NULL);
924 qed_aio_next_io(opaque, ret);
928 * Update L1 table with new L2 table offset and write it out
930 static void qed_aio_write_l1_update(void *opaque, int ret)
932 QEDAIOCB *acb = opaque;
933 BDRVQEDState *s = acb_to_s(acb);
934 int index;
936 if (ret) {
937 qed_aio_complete(acb, ret);
938 return;
941 index = qed_l1_index(s, acb->cur_pos);
942 s->l1_table->offsets[index] = acb->request.l2_table->offset;
944 qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
948 * Update L2 table with new cluster offsets and write them out
950 static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset)
952 BDRVQEDState *s = acb_to_s(acb);
953 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
954 int index;
956 if (ret) {
957 goto err;
960 if (need_alloc) {
961 qed_unref_l2_cache_entry(acb->request.l2_table);
962 acb->request.l2_table = qed_new_l2_table(s);
965 index = qed_l2_index(s, acb->cur_pos);
966 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
967 offset);
969 if (need_alloc) {
970 /* Write out the whole new L2 table */
971 qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true,
972 qed_aio_write_l1_update, acb);
973 } else {
974 /* Write out only the updated part of the L2 table */
975 qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false,
976 qed_aio_next_io, acb);
978 return;
980 err:
981 qed_aio_complete(acb, ret);
984 static void qed_aio_write_l2_update_cb(void *opaque, int ret)
986 QEDAIOCB *acb = opaque;
987 qed_aio_write_l2_update(acb, ret, acb->cur_cluster);
991 * Flush new data clusters before updating the L2 table
993 * This flush is necessary when a backing file is in use. A crash during an
994 * allocating write could result in empty clusters in the image. If the write
995 * only touched a subregion of the cluster, then backing image sectors have
996 * been lost in the untouched region. The solution is to flush after writing a
997 * new data cluster and before updating the L2 table.
999 static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
1001 QEDAIOCB *acb = opaque;
1002 BDRVQEDState *s = acb_to_s(acb);
1004 if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update_cb, opaque)) {
1005 qed_aio_complete(acb, -EIO);
1010 * Write data to the image file
1012 static void qed_aio_write_main(void *opaque, int ret)
1014 QEDAIOCB *acb = opaque;
1015 BDRVQEDState *s = acb_to_s(acb);
1016 uint64_t offset = acb->cur_cluster +
1017 qed_offset_into_cluster(s, acb->cur_pos);
1018 BlockDriverCompletionFunc *next_fn;
1020 trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
1022 if (ret) {
1023 qed_aio_complete(acb, ret);
1024 return;
1027 if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
1028 next_fn = qed_aio_next_io;
1029 } else {
1030 if (s->bs->backing_hd) {
1031 next_fn = qed_aio_write_flush_before_l2_update;
1032 } else {
1033 next_fn = qed_aio_write_l2_update_cb;
1037 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
1038 bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE,
1039 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1040 next_fn, acb);
1044 * Populate back untouched region of new data cluster
1046 static void qed_aio_write_postfill(void *opaque, int ret)
1048 QEDAIOCB *acb = opaque;
1049 BDRVQEDState *s = acb_to_s(acb);
1050 uint64_t start = acb->cur_pos + acb->cur_qiov.size;
1051 uint64_t len =
1052 qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1053 uint64_t offset = acb->cur_cluster +
1054 qed_offset_into_cluster(s, acb->cur_pos) +
1055 acb->cur_qiov.size;
1057 if (ret) {
1058 qed_aio_complete(acb, ret);
1059 return;
1062 trace_qed_aio_write_postfill(s, acb, start, len, offset);
1063 qed_copy_from_backing_file(s, start, len, offset,
1064 qed_aio_write_main, acb);
1068 * Populate front untouched region of new data cluster
1070 static void qed_aio_write_prefill(void *opaque, int ret)
1072 QEDAIOCB *acb = opaque;
1073 BDRVQEDState *s = acb_to_s(acb);
1074 uint64_t start = qed_start_of_cluster(s, acb->cur_pos);
1075 uint64_t len = qed_offset_into_cluster(s, acb->cur_pos);
1077 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1078 qed_copy_from_backing_file(s, start, len, acb->cur_cluster,
1079 qed_aio_write_postfill, acb);
1083 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1085 static bool qed_should_set_need_check(BDRVQEDState *s)
1087 /* The flush before L2 update path ensures consistency */
1088 if (s->bs->backing_hd) {
1089 return false;
1092 return !(s->header.features & QED_F_NEED_CHECK);
1095 static void qed_aio_write_zero_cluster(void *opaque, int ret)
1097 QEDAIOCB *acb = opaque;
1099 if (ret) {
1100 qed_aio_complete(acb, ret);
1101 return;
1104 qed_aio_write_l2_update(acb, 0, 1);
1108 * Write new data cluster
1110 * @acb: Write request
1111 * @len: Length in bytes
1113 * This path is taken when writing to previously unallocated clusters.
1115 static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1117 BDRVQEDState *s = acb_to_s(acb);
1118 BlockDriverCompletionFunc *cb;
1120 /* Cancel timer when the first allocating request comes in */
1121 if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) {
1122 qed_cancel_need_check_timer(s);
1125 /* Freeze this request if another allocating write is in progress */
1126 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
1127 QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
1129 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) ||
1130 s->allocating_write_reqs_plugged) {
1131 return; /* wait for existing request to finish */
1134 acb->cur_nclusters = qed_bytes_to_clusters(s,
1135 qed_offset_into_cluster(s, acb->cur_pos) + len);
1136 qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1138 if (acb->flags & QED_AIOCB_ZERO) {
1139 /* Skip ahead if the clusters are already zero */
1140 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
1141 qed_aio_next_io(acb, 0);
1142 return;
1145 cb = qed_aio_write_zero_cluster;
1146 } else {
1147 cb = qed_aio_write_prefill;
1148 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1151 if (qed_should_set_need_check(s)) {
1152 s->header.features |= QED_F_NEED_CHECK;
1153 qed_write_header(s, cb, acb);
1154 } else {
1155 cb(acb, 0);
1160 * Write data cluster in place
1162 * @acb: Write request
1163 * @offset: Cluster offset in bytes
1164 * @len: Length in bytes
1166 * This path is taken when writing to already allocated clusters.
1168 static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1170 /* Allocate buffer for zero writes */
1171 if (acb->flags & QED_AIOCB_ZERO) {
1172 struct iovec *iov = acb->qiov->iov;
1174 if (!iov->iov_base) {
1175 iov->iov_base = qemu_blockalign(acb->common.bs, iov->iov_len);
1176 memset(iov->iov_base, 0, iov->iov_len);
1180 /* Calculate the I/O vector */
1181 acb->cur_cluster = offset;
1182 qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1184 /* Do the actual write */
1185 qed_aio_write_main(acb, 0);
1189 * Write data cluster
1191 * @opaque: Write request
1192 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1193 * or -errno
1194 * @offset: Cluster offset in bytes
1195 * @len: Length in bytes
1197 * Callback from qed_find_cluster().
1199 static void qed_aio_write_data(void *opaque, int ret,
1200 uint64_t offset, size_t len)
1202 QEDAIOCB *acb = opaque;
1204 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1206 acb->find_cluster_ret = ret;
1208 switch (ret) {
1209 case QED_CLUSTER_FOUND:
1210 qed_aio_write_inplace(acb, offset, len);
1211 break;
1213 case QED_CLUSTER_L2:
1214 case QED_CLUSTER_L1:
1215 case QED_CLUSTER_ZERO:
1216 qed_aio_write_alloc(acb, len);
1217 break;
1219 default:
1220 qed_aio_complete(acb, ret);
1221 break;
1226 * Read data cluster
1228 * @opaque: Read request
1229 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1230 * or -errno
1231 * @offset: Cluster offset in bytes
1232 * @len: Length in bytes
1234 * Callback from qed_find_cluster().
1236 static void qed_aio_read_data(void *opaque, int ret,
1237 uint64_t offset, size_t len)
1239 QEDAIOCB *acb = opaque;
1240 BDRVQEDState *s = acb_to_s(acb);
1241 BlockDriverState *bs = acb->common.bs;
1243 /* Adjust offset into cluster */
1244 offset += qed_offset_into_cluster(s, acb->cur_pos);
1246 trace_qed_aio_read_data(s, acb, ret, offset, len);
1248 if (ret < 0) {
1249 goto err;
1252 qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1254 /* Handle zero cluster and backing file reads */
1255 if (ret == QED_CLUSTER_ZERO) {
1256 qemu_iovec_memset(&acb->cur_qiov, 0, acb->cur_qiov.size);
1257 qed_aio_next_io(acb, 0);
1258 return;
1259 } else if (ret != QED_CLUSTER_FOUND) {
1260 qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
1261 qed_aio_next_io, acb);
1262 return;
1265 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1266 bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE,
1267 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1268 qed_aio_next_io, acb);
1269 return;
1271 err:
1272 qed_aio_complete(acb, ret);
1276 * Begin next I/O or complete the request
1278 static void qed_aio_next_io(void *opaque, int ret)
1280 QEDAIOCB *acb = opaque;
1281 BDRVQEDState *s = acb_to_s(acb);
1282 QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ?
1283 qed_aio_write_data : qed_aio_read_data;
1285 trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
1287 /* Handle I/O error */
1288 if (ret) {
1289 qed_aio_complete(acb, ret);
1290 return;
1293 acb->qiov_offset += acb->cur_qiov.size;
1294 acb->cur_pos += acb->cur_qiov.size;
1295 qemu_iovec_reset(&acb->cur_qiov);
1297 /* Complete request */
1298 if (acb->cur_pos >= acb->end_pos) {
1299 qed_aio_complete(acb, 0);
1300 return;
1303 /* Find next cluster and start I/O */
1304 qed_find_cluster(s, &acb->request,
1305 acb->cur_pos, acb->end_pos - acb->cur_pos,
1306 io_fn, acb);
1309 static BlockDriverAIOCB *qed_aio_setup(BlockDriverState *bs,
1310 int64_t sector_num,
1311 QEMUIOVector *qiov, int nb_sectors,
1312 BlockDriverCompletionFunc *cb,
1313 void *opaque, int flags)
1315 QEDAIOCB *acb = qemu_aio_get(&qed_aio_pool, bs, cb, opaque);
1317 trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
1318 opaque, flags);
1320 acb->flags = flags;
1321 acb->finished = NULL;
1322 acb->qiov = qiov;
1323 acb->qiov_offset = 0;
1324 acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
1325 acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
1326 acb->request.l2_table = NULL;
1327 qemu_iovec_init(&acb->cur_qiov, qiov->niov);
1329 /* Start request */
1330 qed_aio_next_io(acb, 0);
1331 return &acb->common;
1334 static BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
1335 int64_t sector_num,
1336 QEMUIOVector *qiov, int nb_sectors,
1337 BlockDriverCompletionFunc *cb,
1338 void *opaque)
1340 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1343 static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
1344 int64_t sector_num,
1345 QEMUIOVector *qiov, int nb_sectors,
1346 BlockDriverCompletionFunc *cb,
1347 void *opaque)
1349 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb,
1350 opaque, QED_AIOCB_WRITE);
1353 static BlockDriverAIOCB *bdrv_qed_aio_flush(BlockDriverState *bs,
1354 BlockDriverCompletionFunc *cb,
1355 void *opaque)
1357 return bdrv_aio_flush(bs->file, cb, opaque);
1360 typedef struct {
1361 Coroutine *co;
1362 int ret;
1363 bool done;
1364 } QEDWriteZeroesCB;
1366 static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret)
1368 QEDWriteZeroesCB *cb = opaque;
1370 cb->done = true;
1371 cb->ret = ret;
1372 if (cb->co) {
1373 qemu_coroutine_enter(cb->co, NULL);
1377 static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs,
1378 int64_t sector_num,
1379 int nb_sectors)
1381 BlockDriverAIOCB *blockacb;
1382 QEDWriteZeroesCB cb = { .done = false };
1383 QEMUIOVector qiov;
1384 struct iovec iov;
1386 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1387 * then it will be allocated during request processing.
1389 iov.iov_base = NULL,
1390 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1392 qemu_iovec_init_external(&qiov, &iov, 1);
1393 blockacb = qed_aio_setup(bs, sector_num, &qiov, nb_sectors,
1394 qed_co_write_zeroes_cb, &cb,
1395 QED_AIOCB_WRITE | QED_AIOCB_ZERO);
1396 if (!blockacb) {
1397 return -EIO;
1399 if (!cb.done) {
1400 cb.co = qemu_coroutine_self();
1401 qemu_coroutine_yield();
1403 assert(cb.done);
1404 return cb.ret;
1407 static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset)
1409 BDRVQEDState *s = bs->opaque;
1410 uint64_t old_image_size;
1411 int ret;
1413 if (!qed_is_image_size_valid(offset, s->header.cluster_size,
1414 s->header.table_size)) {
1415 return -EINVAL;
1418 /* Shrinking is currently not supported */
1419 if ((uint64_t)offset < s->header.image_size) {
1420 return -ENOTSUP;
1423 old_image_size = s->header.image_size;
1424 s->header.image_size = offset;
1425 ret = qed_write_header_sync(s);
1426 if (ret < 0) {
1427 s->header.image_size = old_image_size;
1429 return ret;
1432 static int64_t bdrv_qed_getlength(BlockDriverState *bs)
1434 BDRVQEDState *s = bs->opaque;
1435 return s->header.image_size;
1438 static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1440 BDRVQEDState *s = bs->opaque;
1442 memset(bdi, 0, sizeof(*bdi));
1443 bdi->cluster_size = s->header.cluster_size;
1444 return 0;
1447 static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1448 const char *backing_file,
1449 const char *backing_fmt)
1451 BDRVQEDState *s = bs->opaque;
1452 QEDHeader new_header, le_header;
1453 void *buffer;
1454 size_t buffer_len, backing_file_len;
1455 int ret;
1457 /* Refuse to set backing filename if unknown compat feature bits are
1458 * active. If the image uses an unknown compat feature then we may not
1459 * know the layout of data following the header structure and cannot safely
1460 * add a new string.
1462 if (backing_file && (s->header.compat_features &
1463 ~QED_COMPAT_FEATURE_MASK)) {
1464 return -ENOTSUP;
1467 memcpy(&new_header, &s->header, sizeof(new_header));
1469 new_header.features &= ~(QED_F_BACKING_FILE |
1470 QED_F_BACKING_FORMAT_NO_PROBE);
1472 /* Adjust feature flags */
1473 if (backing_file) {
1474 new_header.features |= QED_F_BACKING_FILE;
1476 if (qed_fmt_is_raw(backing_fmt)) {
1477 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1481 /* Calculate new header size */
1482 backing_file_len = 0;
1484 if (backing_file) {
1485 backing_file_len = strlen(backing_file);
1488 buffer_len = sizeof(new_header);
1489 new_header.backing_filename_offset = buffer_len;
1490 new_header.backing_filename_size = backing_file_len;
1491 buffer_len += backing_file_len;
1493 /* Make sure we can rewrite header without failing */
1494 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1495 return -ENOSPC;
1498 /* Prepare new header */
1499 buffer = g_malloc(buffer_len);
1501 qed_header_cpu_to_le(&new_header, &le_header);
1502 memcpy(buffer, &le_header, sizeof(le_header));
1503 buffer_len = sizeof(le_header);
1505 if (backing_file) {
1506 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1507 buffer_len += backing_file_len;
1510 /* Write new header */
1511 ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len);
1512 g_free(buffer);
1513 if (ret == 0) {
1514 memcpy(&s->header, &new_header, sizeof(new_header));
1516 return ret;
1519 static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result)
1521 BDRVQEDState *s = bs->opaque;
1523 return qed_check(s, result, false);
1526 static QEMUOptionParameter qed_create_options[] = {
1528 .name = BLOCK_OPT_SIZE,
1529 .type = OPT_SIZE,
1530 .help = "Virtual disk size (in bytes)"
1531 }, {
1532 .name = BLOCK_OPT_BACKING_FILE,
1533 .type = OPT_STRING,
1534 .help = "File name of a base image"
1535 }, {
1536 .name = BLOCK_OPT_BACKING_FMT,
1537 .type = OPT_STRING,
1538 .help = "Image format of the base image"
1539 }, {
1540 .name = BLOCK_OPT_CLUSTER_SIZE,
1541 .type = OPT_SIZE,
1542 .help = "Cluster size (in bytes)",
1543 .value = { .n = QED_DEFAULT_CLUSTER_SIZE },
1544 }, {
1545 .name = BLOCK_OPT_TABLE_SIZE,
1546 .type = OPT_SIZE,
1547 .help = "L1/L2 table size (in clusters)"
1549 { /* end of list */ }
1552 static BlockDriver bdrv_qed = {
1553 .format_name = "qed",
1554 .instance_size = sizeof(BDRVQEDState),
1555 .create_options = qed_create_options,
1557 .bdrv_probe = bdrv_qed_probe,
1558 .bdrv_open = bdrv_qed_open,
1559 .bdrv_close = bdrv_qed_close,
1560 .bdrv_create = bdrv_qed_create,
1561 .bdrv_co_is_allocated = bdrv_qed_co_is_allocated,
1562 .bdrv_make_empty = bdrv_qed_make_empty,
1563 .bdrv_aio_readv = bdrv_qed_aio_readv,
1564 .bdrv_aio_writev = bdrv_qed_aio_writev,
1565 .bdrv_aio_flush = bdrv_qed_aio_flush,
1566 .bdrv_co_write_zeroes = bdrv_qed_co_write_zeroes,
1567 .bdrv_truncate = bdrv_qed_truncate,
1568 .bdrv_getlength = bdrv_qed_getlength,
1569 .bdrv_get_info = bdrv_qed_get_info,
1570 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
1571 .bdrv_check = bdrv_qed_check,
1574 static void bdrv_qed_init(void)
1576 bdrv_register(&bdrv_qed);
1579 block_init(bdrv_qed_init);