target/ppc: Move Vector Compare Equal/Not Equal/Greater Than to decodetree
[qemu/rayw.git] / block / qed.c
blob558d3646c4b2b8e2db6de13e11034c167d106e85
1 /*
2 * QEMU Enhanced Disk Format
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "block/qdict.h"
17 #include "qapi/error.h"
18 #include "qemu/timer.h"
19 #include "qemu/bswap.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/module.h"
22 #include "qemu/option.h"
23 #include "trace.h"
24 #include "qed.h"
25 #include "sysemu/block-backend.h"
26 #include "qapi/qmp/qdict.h"
27 #include "qapi/qobject-input-visitor.h"
28 #include "qapi/qapi-visit-block-core.h"
30 static QemuOptsList qed_create_opts;
32 static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
33 const char *filename)
35 const QEDHeader *header = (const QEDHeader *)buf;
37 if (buf_size < sizeof(*header)) {
38 return 0;
40 if (le32_to_cpu(header->magic) != QED_MAGIC) {
41 return 0;
43 return 100;
46 /**
47 * Check whether an image format is raw
49 * @fmt: Backing file format, may be NULL
51 static bool qed_fmt_is_raw(const char *fmt)
53 return fmt && strcmp(fmt, "raw") == 0;
56 static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
58 cpu->magic = le32_to_cpu(le->magic);
59 cpu->cluster_size = le32_to_cpu(le->cluster_size);
60 cpu->table_size = le32_to_cpu(le->table_size);
61 cpu->header_size = le32_to_cpu(le->header_size);
62 cpu->features = le64_to_cpu(le->features);
63 cpu->compat_features = le64_to_cpu(le->compat_features);
64 cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
65 cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
66 cpu->image_size = le64_to_cpu(le->image_size);
67 cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
68 cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
71 static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
73 le->magic = cpu_to_le32(cpu->magic);
74 le->cluster_size = cpu_to_le32(cpu->cluster_size);
75 le->table_size = cpu_to_le32(cpu->table_size);
76 le->header_size = cpu_to_le32(cpu->header_size);
77 le->features = cpu_to_le64(cpu->features);
78 le->compat_features = cpu_to_le64(cpu->compat_features);
79 le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
80 le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
81 le->image_size = cpu_to_le64(cpu->image_size);
82 le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
83 le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
86 int qed_write_header_sync(BDRVQEDState *s)
88 QEDHeader le;
89 int ret;
91 qed_header_cpu_to_le(&s->header, &le);
92 ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le));
93 if (ret != sizeof(le)) {
94 return ret;
96 return 0;
99 /**
100 * Update header in-place (does not rewrite backing filename or other strings)
102 * This function only updates known header fields in-place and does not affect
103 * extra data after the QED header.
105 * No new allocating reqs can start while this function runs.
107 static int coroutine_fn qed_write_header(BDRVQEDState *s)
109 /* We must write full sectors for O_DIRECT but cannot necessarily generate
110 * the data following the header if an unrecognized compat feature is
111 * active. Therefore, first read the sectors containing the header, update
112 * them, and write back.
115 int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE);
116 size_t len = nsectors * BDRV_SECTOR_SIZE;
117 uint8_t *buf;
118 int ret;
120 assert(s->allocating_acb || s->allocating_write_reqs_plugged);
122 buf = qemu_blockalign(s->bs, len);
124 ret = bdrv_co_pread(s->bs->file, 0, len, buf, 0);
125 if (ret < 0) {
126 goto out;
129 /* Update header */
130 qed_header_cpu_to_le(&s->header, (QEDHeader *) buf);
132 ret = bdrv_co_pwrite(s->bs->file, 0, len, buf, 0);
133 if (ret < 0) {
134 goto out;
137 ret = 0;
138 out:
139 qemu_vfree(buf);
140 return ret;
143 static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
145 uint64_t table_entries;
146 uint64_t l2_size;
148 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
149 l2_size = table_entries * cluster_size;
151 return l2_size * table_entries;
154 static bool qed_is_cluster_size_valid(uint32_t cluster_size)
156 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
157 cluster_size > QED_MAX_CLUSTER_SIZE) {
158 return false;
160 if (cluster_size & (cluster_size - 1)) {
161 return false; /* not power of 2 */
163 return true;
166 static bool qed_is_table_size_valid(uint32_t table_size)
168 if (table_size < QED_MIN_TABLE_SIZE ||
169 table_size > QED_MAX_TABLE_SIZE) {
170 return false;
172 if (table_size & (table_size - 1)) {
173 return false; /* not power of 2 */
175 return true;
178 static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
179 uint32_t table_size)
181 if (image_size % BDRV_SECTOR_SIZE != 0) {
182 return false; /* not multiple of sector size */
184 if (image_size > qed_max_image_size(cluster_size, table_size)) {
185 return false; /* image is too large */
187 return true;
191 * Read a string of known length from the image file
193 * @file: Image file
194 * @offset: File offset to start of string, in bytes
195 * @n: String length in bytes
196 * @buf: Destination buffer
197 * @buflen: Destination buffer length in bytes
198 * @ret: 0 on success, -errno on failure
200 * The string is NUL-terminated.
202 static int qed_read_string(BdrvChild *file, uint64_t offset, size_t n,
203 char *buf, size_t buflen)
205 int ret;
206 if (n >= buflen) {
207 return -EINVAL;
209 ret = bdrv_pread(file, offset, buf, n);
210 if (ret < 0) {
211 return ret;
213 buf[n] = '\0';
214 return 0;
218 * Allocate new clusters
220 * @s: QED state
221 * @n: Number of contiguous clusters to allocate
222 * @ret: Offset of first allocated cluster
224 * This function only produces the offset where the new clusters should be
225 * written. It updates BDRVQEDState but does not make any changes to the image
226 * file.
228 * Called with table_lock held.
230 static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
232 uint64_t offset = s->file_size;
233 s->file_size += n * s->header.cluster_size;
234 return offset;
237 QEDTable *qed_alloc_table(BDRVQEDState *s)
239 /* Honor O_DIRECT memory alignment requirements */
240 return qemu_blockalign(s->bs,
241 s->header.cluster_size * s->header.table_size);
245 * Allocate a new zeroed L2 table
247 * Called with table_lock held.
249 static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
251 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
253 l2_table->table = qed_alloc_table(s);
254 l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
256 memset(l2_table->table->offsets, 0,
257 s->header.cluster_size * s->header.table_size);
258 return l2_table;
261 static bool qed_plug_allocating_write_reqs(BDRVQEDState *s)
263 qemu_co_mutex_lock(&s->table_lock);
265 /* No reentrancy is allowed. */
266 assert(!s->allocating_write_reqs_plugged);
267 if (s->allocating_acb != NULL) {
268 /* Another allocating write came concurrently. This cannot happen
269 * from bdrv_qed_co_drain_begin, but it can happen when the timer runs.
271 qemu_co_mutex_unlock(&s->table_lock);
272 return false;
275 s->allocating_write_reqs_plugged = true;
276 qemu_co_mutex_unlock(&s->table_lock);
277 return true;
280 static void qed_unplug_allocating_write_reqs(BDRVQEDState *s)
282 qemu_co_mutex_lock(&s->table_lock);
283 assert(s->allocating_write_reqs_plugged);
284 s->allocating_write_reqs_plugged = false;
285 qemu_co_queue_next(&s->allocating_write_reqs);
286 qemu_co_mutex_unlock(&s->table_lock);
289 static void coroutine_fn qed_need_check_timer_entry(void *opaque)
291 BDRVQEDState *s = opaque;
292 int ret;
294 trace_qed_need_check_timer_cb(s);
296 if (!qed_plug_allocating_write_reqs(s)) {
297 return;
300 /* Ensure writes are on disk before clearing flag */
301 ret = bdrv_co_flush(s->bs->file->bs);
302 if (ret < 0) {
303 qed_unplug_allocating_write_reqs(s);
304 return;
307 s->header.features &= ~QED_F_NEED_CHECK;
308 ret = qed_write_header(s);
309 (void) ret;
311 qed_unplug_allocating_write_reqs(s);
313 ret = bdrv_co_flush(s->bs);
314 (void) ret;
317 static void qed_need_check_timer_cb(void *opaque)
319 Coroutine *co = qemu_coroutine_create(qed_need_check_timer_entry, opaque);
320 qemu_coroutine_enter(co);
323 static void qed_start_need_check_timer(BDRVQEDState *s)
325 trace_qed_start_need_check_timer(s);
327 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
328 * migration.
330 timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
331 NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT);
334 /* It's okay to call this multiple times or when no timer is started */
335 static void qed_cancel_need_check_timer(BDRVQEDState *s)
337 trace_qed_cancel_need_check_timer(s);
338 timer_del(s->need_check_timer);
341 static void bdrv_qed_detach_aio_context(BlockDriverState *bs)
343 BDRVQEDState *s = bs->opaque;
345 qed_cancel_need_check_timer(s);
346 timer_free(s->need_check_timer);
349 static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
350 AioContext *new_context)
352 BDRVQEDState *s = bs->opaque;
354 s->need_check_timer = aio_timer_new(new_context,
355 QEMU_CLOCK_VIRTUAL, SCALE_NS,
356 qed_need_check_timer_cb, s);
357 if (s->header.features & QED_F_NEED_CHECK) {
358 qed_start_need_check_timer(s);
362 static void coroutine_fn bdrv_qed_co_drain_begin(BlockDriverState *bs)
364 BDRVQEDState *s = bs->opaque;
366 /* Fire the timer immediately in order to start doing I/O as soon as the
367 * header is flushed.
369 if (s->need_check_timer && timer_pending(s->need_check_timer)) {
370 qed_cancel_need_check_timer(s);
371 qed_need_check_timer_entry(s);
375 static void bdrv_qed_init_state(BlockDriverState *bs)
377 BDRVQEDState *s = bs->opaque;
379 memset(s, 0, sizeof(BDRVQEDState));
380 s->bs = bs;
381 qemu_co_mutex_init(&s->table_lock);
382 qemu_co_queue_init(&s->allocating_write_reqs);
385 /* Called with table_lock held. */
386 static int coroutine_fn bdrv_qed_do_open(BlockDriverState *bs, QDict *options,
387 int flags, Error **errp)
389 BDRVQEDState *s = bs->opaque;
390 QEDHeader le_header;
391 int64_t file_size;
392 int ret;
394 ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header));
395 if (ret < 0) {
396 error_setg(errp, "Failed to read QED header");
397 return ret;
399 qed_header_le_to_cpu(&le_header, &s->header);
401 if (s->header.magic != QED_MAGIC) {
402 error_setg(errp, "Image not in QED format");
403 return -EINVAL;
405 if (s->header.features & ~QED_FEATURE_MASK) {
406 /* image uses unsupported feature bits */
407 error_setg(errp, "Unsupported QED features: %" PRIx64,
408 s->header.features & ~QED_FEATURE_MASK);
409 return -ENOTSUP;
411 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
412 error_setg(errp, "QED cluster size is invalid");
413 return -EINVAL;
416 /* Round down file size to the last cluster */
417 file_size = bdrv_getlength(bs->file->bs);
418 if (file_size < 0) {
419 error_setg(errp, "Failed to get file length");
420 return file_size;
422 s->file_size = qed_start_of_cluster(s, file_size);
424 if (!qed_is_table_size_valid(s->header.table_size)) {
425 error_setg(errp, "QED table size is invalid");
426 return -EINVAL;
428 if (!qed_is_image_size_valid(s->header.image_size,
429 s->header.cluster_size,
430 s->header.table_size)) {
431 error_setg(errp, "QED image size is invalid");
432 return -EINVAL;
434 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
435 error_setg(errp, "QED table offset is invalid");
436 return -EINVAL;
439 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
440 sizeof(uint64_t);
441 s->l2_shift = ctz32(s->header.cluster_size);
442 s->l2_mask = s->table_nelems - 1;
443 s->l1_shift = s->l2_shift + ctz32(s->table_nelems);
445 /* Header size calculation must not overflow uint32_t */
446 if (s->header.header_size > UINT32_MAX / s->header.cluster_size) {
447 error_setg(errp, "QED header size is too large");
448 return -EINVAL;
451 if ((s->header.features & QED_F_BACKING_FILE)) {
452 if ((uint64_t)s->header.backing_filename_offset +
453 s->header.backing_filename_size >
454 s->header.cluster_size * s->header.header_size) {
455 error_setg(errp, "QED backing filename offset is invalid");
456 return -EINVAL;
459 ret = qed_read_string(bs->file, s->header.backing_filename_offset,
460 s->header.backing_filename_size,
461 bs->auto_backing_file,
462 sizeof(bs->auto_backing_file));
463 if (ret < 0) {
464 error_setg(errp, "Failed to read backing filename");
465 return ret;
467 pstrcpy(bs->backing_file, sizeof(bs->backing_file),
468 bs->auto_backing_file);
470 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
471 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
475 /* Reset unknown autoclear feature bits. This is a backwards
476 * compatibility mechanism that allows images to be opened by older
477 * programs, which "knock out" unknown feature bits. When an image is
478 * opened by a newer program again it can detect that the autoclear
479 * feature is no longer valid.
481 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
482 !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) {
483 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
485 ret = qed_write_header_sync(s);
486 if (ret) {
487 error_setg(errp, "Failed to update header");
488 return ret;
491 /* From here on only known autoclear feature bits are valid */
492 bdrv_flush(bs->file->bs);
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 error_setg(errp, "Failed to read L1 table");
501 goto out;
504 /* If image was not closed cleanly, check consistency */
505 if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
506 /* Read-only images cannot be fixed. There is no risk of corruption
507 * since write operations are not possible. Therefore, allow
508 * potentially inconsistent images to be opened read-only. This can
509 * aid data recovery from an otherwise inconsistent image.
511 if (!bdrv_is_read_only(bs->file->bs) &&
512 !(flags & BDRV_O_INACTIVE)) {
513 BdrvCheckResult result = {0};
515 ret = qed_check(s, &result, true);
516 if (ret) {
517 error_setg(errp, "Image corrupted");
518 goto out;
523 bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
525 out:
526 if (ret) {
527 qed_free_l2_cache(&s->l2_cache);
528 qemu_vfree(s->l1_table);
530 return ret;
533 typedef struct QEDOpenCo {
534 BlockDriverState *bs;
535 QDict *options;
536 int flags;
537 Error **errp;
538 int ret;
539 } QEDOpenCo;
541 static void coroutine_fn bdrv_qed_open_entry(void *opaque)
543 QEDOpenCo *qoc = opaque;
544 BDRVQEDState *s = qoc->bs->opaque;
546 qemu_co_mutex_lock(&s->table_lock);
547 qoc->ret = bdrv_qed_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp);
548 qemu_co_mutex_unlock(&s->table_lock);
551 static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
552 Error **errp)
554 QEDOpenCo qoc = {
555 .bs = bs,
556 .options = options,
557 .flags = flags,
558 .errp = errp,
559 .ret = -EINPROGRESS
562 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
563 BDRV_CHILD_IMAGE, false, errp);
564 if (!bs->file) {
565 return -EINVAL;
568 bdrv_qed_init_state(bs);
569 if (qemu_in_coroutine()) {
570 bdrv_qed_open_entry(&qoc);
571 } else {
572 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
573 qemu_coroutine_enter(qemu_coroutine_create(bdrv_qed_open_entry, &qoc));
574 BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
576 BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
577 return qoc.ret;
580 static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
582 BDRVQEDState *s = bs->opaque;
584 bs->bl.pwrite_zeroes_alignment = s->header.cluster_size;
585 bs->bl.max_pwrite_zeroes = QEMU_ALIGN_DOWN(INT_MAX, s->header.cluster_size);
588 /* We have nothing to do for QED reopen, stubs just return
589 * success */
590 static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
591 BlockReopenQueue *queue, Error **errp)
593 return 0;
596 static void bdrv_qed_close(BlockDriverState *bs)
598 BDRVQEDState *s = bs->opaque;
600 bdrv_qed_detach_aio_context(bs);
602 /* Ensure writes reach stable storage */
603 bdrv_flush(bs->file->bs);
605 /* Clean shutdown, no check required on next open */
606 if (s->header.features & QED_F_NEED_CHECK) {
607 s->header.features &= ~QED_F_NEED_CHECK;
608 qed_write_header_sync(s);
611 qed_free_l2_cache(&s->l2_cache);
612 qemu_vfree(s->l1_table);
615 static int coroutine_fn bdrv_qed_co_create(BlockdevCreateOptions *opts,
616 Error **errp)
618 BlockdevCreateOptionsQed *qed_opts;
619 BlockBackend *blk = NULL;
620 BlockDriverState *bs = NULL;
622 QEDHeader header;
623 QEDHeader le_header;
624 uint8_t *l1_table = NULL;
625 size_t l1_size;
626 int ret = 0;
628 assert(opts->driver == BLOCKDEV_DRIVER_QED);
629 qed_opts = &opts->u.qed;
631 /* Validate options and set default values */
632 if (!qed_opts->has_cluster_size) {
633 qed_opts->cluster_size = QED_DEFAULT_CLUSTER_SIZE;
635 if (!qed_opts->has_table_size) {
636 qed_opts->table_size = QED_DEFAULT_TABLE_SIZE;
639 if (!qed_is_cluster_size_valid(qed_opts->cluster_size)) {
640 error_setg(errp, "QED cluster size must be within range [%u, %u] "
641 "and power of 2",
642 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
643 return -EINVAL;
645 if (!qed_is_table_size_valid(qed_opts->table_size)) {
646 error_setg(errp, "QED table size must be within range [%u, %u] "
647 "and power of 2",
648 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
649 return -EINVAL;
651 if (!qed_is_image_size_valid(qed_opts->size, qed_opts->cluster_size,
652 qed_opts->table_size))
654 error_setg(errp, "QED image size must be a non-zero multiple of "
655 "cluster size and less than %" PRIu64 " bytes",
656 qed_max_image_size(qed_opts->cluster_size,
657 qed_opts->table_size));
658 return -EINVAL;
661 /* Create BlockBackend to write to the image */
662 bs = bdrv_open_blockdev_ref(qed_opts->file, errp);
663 if (bs == NULL) {
664 return -EIO;
667 blk = blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
668 errp);
669 if (!blk) {
670 ret = -EPERM;
671 goto out;
673 blk_set_allow_write_beyond_eof(blk, true);
675 /* Prepare image format */
676 header = (QEDHeader) {
677 .magic = QED_MAGIC,
678 .cluster_size = qed_opts->cluster_size,
679 .table_size = qed_opts->table_size,
680 .header_size = 1,
681 .features = 0,
682 .compat_features = 0,
683 .l1_table_offset = qed_opts->cluster_size,
684 .image_size = qed_opts->size,
687 l1_size = header.cluster_size * header.table_size;
690 * The QED format associates file length with allocation status,
691 * so a new file (which is empty) must have a length of 0.
693 ret = blk_truncate(blk, 0, true, PREALLOC_MODE_OFF, 0, errp);
694 if (ret < 0) {
695 goto out;
698 if (qed_opts->has_backing_file) {
699 header.features |= QED_F_BACKING_FILE;
700 header.backing_filename_offset = sizeof(le_header);
701 header.backing_filename_size = strlen(qed_opts->backing_file);
703 if (qed_opts->has_backing_fmt) {
704 const char *backing_fmt = BlockdevDriver_str(qed_opts->backing_fmt);
705 if (qed_fmt_is_raw(backing_fmt)) {
706 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
711 qed_header_cpu_to_le(&header, &le_header);
712 ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0);
713 if (ret < 0) {
714 goto out;
716 ret = blk_pwrite(blk, sizeof(le_header), qed_opts->backing_file,
717 header.backing_filename_size, 0);
718 if (ret < 0) {
719 goto out;
722 l1_table = g_malloc0(l1_size);
723 ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0);
724 if (ret < 0) {
725 goto out;
728 ret = 0; /* success */
729 out:
730 g_free(l1_table);
731 blk_unref(blk);
732 bdrv_unref(bs);
733 return ret;
736 static int coroutine_fn bdrv_qed_co_create_opts(BlockDriver *drv,
737 const char *filename,
738 QemuOpts *opts,
739 Error **errp)
741 BlockdevCreateOptions *create_options = NULL;
742 QDict *qdict;
743 Visitor *v;
744 BlockDriverState *bs = NULL;
745 int ret;
747 static const QDictRenames opt_renames[] = {
748 { BLOCK_OPT_BACKING_FILE, "backing-file" },
749 { BLOCK_OPT_BACKING_FMT, "backing-fmt" },
750 { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" },
751 { BLOCK_OPT_TABLE_SIZE, "table-size" },
752 { NULL, NULL },
755 /* Parse options and convert legacy syntax */
756 qdict = qemu_opts_to_qdict_filtered(opts, NULL, &qed_create_opts, true);
758 if (!qdict_rename_keys(qdict, opt_renames, errp)) {
759 ret = -EINVAL;
760 goto fail;
763 /* Create and open the file (protocol layer) */
764 ret = bdrv_create_file(filename, opts, errp);
765 if (ret < 0) {
766 goto fail;
769 bs = bdrv_open(filename, NULL, NULL,
770 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
771 if (bs == NULL) {
772 ret = -EIO;
773 goto fail;
776 /* Now get the QAPI type BlockdevCreateOptions */
777 qdict_put_str(qdict, "driver", "qed");
778 qdict_put_str(qdict, "file", bs->node_name);
780 v = qobject_input_visitor_new_flat_confused(qdict, errp);
781 if (!v) {
782 ret = -EINVAL;
783 goto fail;
786 visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
787 visit_free(v);
788 if (!create_options) {
789 ret = -EINVAL;
790 goto fail;
793 /* Silently round up size */
794 assert(create_options->driver == BLOCKDEV_DRIVER_QED);
795 create_options->u.qed.size =
796 ROUND_UP(create_options->u.qed.size, BDRV_SECTOR_SIZE);
798 /* Create the qed image (format layer) */
799 ret = bdrv_qed_co_create(create_options, errp);
801 fail:
802 qobject_unref(qdict);
803 bdrv_unref(bs);
804 qapi_free_BlockdevCreateOptions(create_options);
805 return ret;
808 static int coroutine_fn bdrv_qed_co_block_status(BlockDriverState *bs,
809 bool want_zero,
810 int64_t pos, int64_t bytes,
811 int64_t *pnum, int64_t *map,
812 BlockDriverState **file)
814 BDRVQEDState *s = bs->opaque;
815 size_t len = MIN(bytes, SIZE_MAX);
816 int status;
817 QEDRequest request = { .l2_table = NULL };
818 uint64_t offset;
819 int ret;
821 qemu_co_mutex_lock(&s->table_lock);
822 ret = qed_find_cluster(s, &request, pos, &len, &offset);
824 *pnum = len;
825 switch (ret) {
826 case QED_CLUSTER_FOUND:
827 *map = offset | qed_offset_into_cluster(s, pos);
828 status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
829 *file = bs->file->bs;
830 break;
831 case QED_CLUSTER_ZERO:
832 status = BDRV_BLOCK_ZERO;
833 break;
834 case QED_CLUSTER_L2:
835 case QED_CLUSTER_L1:
836 status = 0;
837 break;
838 default:
839 assert(ret < 0);
840 status = ret;
841 break;
844 qed_unref_l2_cache_entry(request.l2_table);
845 qemu_co_mutex_unlock(&s->table_lock);
847 return status;
850 static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
852 return acb->bs->opaque;
856 * Read from the backing file or zero-fill if no backing file
858 * @s: QED state
859 * @pos: Byte position in device
860 * @qiov: Destination I/O vector
862 * This function reads qiov->size bytes starting at pos from the backing file.
863 * If there is no backing file then zeroes are read.
865 static int coroutine_fn qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
866 QEMUIOVector *qiov)
868 if (s->bs->backing) {
869 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
870 return bdrv_co_preadv(s->bs->backing, pos, qiov->size, qiov, 0);
872 qemu_iovec_memset(qiov, 0, 0, qiov->size);
873 return 0;
877 * Copy data from backing file into the image
879 * @s: QED state
880 * @pos: Byte position in device
881 * @len: Number of bytes
882 * @offset: Byte offset in image file
884 static int coroutine_fn qed_copy_from_backing_file(BDRVQEDState *s,
885 uint64_t pos, uint64_t len,
886 uint64_t offset)
888 QEMUIOVector qiov;
889 int ret;
891 /* Skip copy entirely if there is no work to do */
892 if (len == 0) {
893 return 0;
896 qemu_iovec_init_buf(&qiov, qemu_blockalign(s->bs, len), len);
898 ret = qed_read_backing_file(s, pos, &qiov);
900 if (ret) {
901 goto out;
904 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
905 ret = bdrv_co_pwritev(s->bs->file, offset, qiov.size, &qiov, 0);
906 if (ret < 0) {
907 goto out;
909 ret = 0;
910 out:
911 qemu_vfree(qemu_iovec_buf(&qiov));
912 return ret;
916 * Link one or more contiguous clusters into a table
918 * @s: QED state
919 * @table: L2 table
920 * @index: First cluster index
921 * @n: Number of contiguous clusters
922 * @cluster: First cluster offset
924 * The cluster offset may be an allocated byte offset in the image file, the
925 * zero cluster marker, or the unallocated cluster marker.
927 * Called with table_lock held.
929 static void coroutine_fn qed_update_l2_table(BDRVQEDState *s, QEDTable *table,
930 int index, unsigned int n,
931 uint64_t cluster)
933 int i;
934 for (i = index; i < index + n; i++) {
935 table->offsets[i] = cluster;
936 if (!qed_offset_is_unalloc_cluster(cluster) &&
937 !qed_offset_is_zero_cluster(cluster)) {
938 cluster += s->header.cluster_size;
943 /* Called with table_lock held. */
944 static void coroutine_fn qed_aio_complete(QEDAIOCB *acb)
946 BDRVQEDState *s = acb_to_s(acb);
948 /* Free resources */
949 qemu_iovec_destroy(&acb->cur_qiov);
950 qed_unref_l2_cache_entry(acb->request.l2_table);
952 /* Free the buffer we may have allocated for zero writes */
953 if (acb->flags & QED_AIOCB_ZERO) {
954 qemu_vfree(acb->qiov->iov[0].iov_base);
955 acb->qiov->iov[0].iov_base = NULL;
958 /* Start next allocating write request waiting behind this one. Note that
959 * requests enqueue themselves when they first hit an unallocated cluster
960 * but they wait until the entire request is finished before waking up the
961 * next request in the queue. This ensures that we don't cycle through
962 * requests multiple times but rather finish one at a time completely.
964 if (acb == s->allocating_acb) {
965 s->allocating_acb = NULL;
966 if (!qemu_co_queue_empty(&s->allocating_write_reqs)) {
967 qemu_co_queue_next(&s->allocating_write_reqs);
968 } else if (s->header.features & QED_F_NEED_CHECK) {
969 qed_start_need_check_timer(s);
975 * Update L1 table with new L2 table offset and write it out
977 * Called with table_lock held.
979 static int coroutine_fn qed_aio_write_l1_update(QEDAIOCB *acb)
981 BDRVQEDState *s = acb_to_s(acb);
982 CachedL2Table *l2_table = acb->request.l2_table;
983 uint64_t l2_offset = l2_table->offset;
984 int index, ret;
986 index = qed_l1_index(s, acb->cur_pos);
987 s->l1_table->offsets[index] = l2_table->offset;
989 ret = qed_write_l1_table(s, index, 1);
991 /* Commit the current L2 table to the cache */
992 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
994 /* This is guaranteed to succeed because we just committed the entry to the
995 * cache.
997 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
998 assert(acb->request.l2_table != NULL);
1000 return ret;
1005 * Update L2 table with new cluster offsets and write them out
1007 * Called with table_lock held.
1009 static int coroutine_fn qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset)
1011 BDRVQEDState *s = acb_to_s(acb);
1012 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
1013 int index, ret;
1015 if (need_alloc) {
1016 qed_unref_l2_cache_entry(acb->request.l2_table);
1017 acb->request.l2_table = qed_new_l2_table(s);
1020 index = qed_l2_index(s, acb->cur_pos);
1021 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
1022 offset);
1024 if (need_alloc) {
1025 /* Write out the whole new L2 table */
1026 ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true);
1027 if (ret) {
1028 return ret;
1030 return qed_aio_write_l1_update(acb);
1031 } else {
1032 /* Write out only the updated part of the L2 table */
1033 ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters,
1034 false);
1035 if (ret) {
1036 return ret;
1039 return 0;
1043 * Write data to the image file
1045 * Called with table_lock *not* held.
1047 static int coroutine_fn qed_aio_write_main(QEDAIOCB *acb)
1049 BDRVQEDState *s = acb_to_s(acb);
1050 uint64_t offset = acb->cur_cluster +
1051 qed_offset_into_cluster(s, acb->cur_pos);
1053 trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size);
1055 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
1056 return bdrv_co_pwritev(s->bs->file, offset, acb->cur_qiov.size,
1057 &acb->cur_qiov, 0);
1061 * Populate untouched regions of new data cluster
1063 * Called with table_lock held.
1065 static int coroutine_fn qed_aio_write_cow(QEDAIOCB *acb)
1067 BDRVQEDState *s = acb_to_s(acb);
1068 uint64_t start, len, offset;
1069 int ret;
1071 qemu_co_mutex_unlock(&s->table_lock);
1073 /* Populate front untouched region of new data cluster */
1074 start = qed_start_of_cluster(s, acb->cur_pos);
1075 len = qed_offset_into_cluster(s, acb->cur_pos);
1077 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1078 ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster);
1079 if (ret < 0) {
1080 goto out;
1083 /* Populate back untouched region of new data cluster */
1084 start = acb->cur_pos + acb->cur_qiov.size;
1085 len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1086 offset = acb->cur_cluster +
1087 qed_offset_into_cluster(s, acb->cur_pos) +
1088 acb->cur_qiov.size;
1090 trace_qed_aio_write_postfill(s, acb, start, len, offset);
1091 ret = qed_copy_from_backing_file(s, start, len, offset);
1092 if (ret < 0) {
1093 goto out;
1096 ret = qed_aio_write_main(acb);
1097 if (ret < 0) {
1098 goto out;
1101 if (s->bs->backing) {
1103 * Flush new data clusters before updating the L2 table
1105 * This flush is necessary when a backing file is in use. A crash
1106 * during an allocating write could result in empty clusters in the
1107 * image. If the write only touched a subregion of the cluster,
1108 * then backing image sectors have been lost in the untouched
1109 * region. The solution is to flush after writing a new data
1110 * cluster and before updating the L2 table.
1112 ret = bdrv_co_flush(s->bs->file->bs);
1115 out:
1116 qemu_co_mutex_lock(&s->table_lock);
1117 return ret;
1121 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1123 static bool qed_should_set_need_check(BDRVQEDState *s)
1125 /* The flush before L2 update path ensures consistency */
1126 if (s->bs->backing) {
1127 return false;
1130 return !(s->header.features & QED_F_NEED_CHECK);
1134 * Write new data cluster
1136 * @acb: Write request
1137 * @len: Length in bytes
1139 * This path is taken when writing to previously unallocated clusters.
1141 * Called with table_lock held.
1143 static int coroutine_fn qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1145 BDRVQEDState *s = acb_to_s(acb);
1146 int ret;
1148 /* Cancel timer when the first allocating request comes in */
1149 if (s->allocating_acb == NULL) {
1150 qed_cancel_need_check_timer(s);
1153 /* Freeze this request if another allocating write is in progress */
1154 if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) {
1155 if (s->allocating_acb != NULL) {
1156 qemu_co_queue_wait(&s->allocating_write_reqs, &s->table_lock);
1157 assert(s->allocating_acb == NULL);
1159 s->allocating_acb = acb;
1160 return -EAGAIN; /* start over with looking up table entries */
1163 acb->cur_nclusters = qed_bytes_to_clusters(s,
1164 qed_offset_into_cluster(s, acb->cur_pos) + len);
1165 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1167 if (acb->flags & QED_AIOCB_ZERO) {
1168 /* Skip ahead if the clusters are already zero */
1169 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
1170 return 0;
1172 acb->cur_cluster = 1;
1173 } else {
1174 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1177 if (qed_should_set_need_check(s)) {
1178 s->header.features |= QED_F_NEED_CHECK;
1179 ret = qed_write_header(s);
1180 if (ret < 0) {
1181 return ret;
1185 if (!(acb->flags & QED_AIOCB_ZERO)) {
1186 ret = qed_aio_write_cow(acb);
1187 if (ret < 0) {
1188 return ret;
1192 return qed_aio_write_l2_update(acb, acb->cur_cluster);
1196 * Write data cluster in place
1198 * @acb: Write request
1199 * @offset: Cluster offset in bytes
1200 * @len: Length in bytes
1202 * This path is taken when writing to already allocated clusters.
1204 * Called with table_lock held.
1206 static int coroutine_fn qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset,
1207 size_t len)
1209 BDRVQEDState *s = acb_to_s(acb);
1210 int r;
1212 qemu_co_mutex_unlock(&s->table_lock);
1214 /* Allocate buffer for zero writes */
1215 if (acb->flags & QED_AIOCB_ZERO) {
1216 struct iovec *iov = acb->qiov->iov;
1218 if (!iov->iov_base) {
1219 iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len);
1220 if (iov->iov_base == NULL) {
1221 r = -ENOMEM;
1222 goto out;
1224 memset(iov->iov_base, 0, iov->iov_len);
1228 /* Calculate the I/O vector */
1229 acb->cur_cluster = offset;
1230 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1232 /* Do the actual write. */
1233 r = qed_aio_write_main(acb);
1234 out:
1235 qemu_co_mutex_lock(&s->table_lock);
1236 return r;
1240 * Write data cluster
1242 * @opaque: Write request
1243 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
1244 * @offset: Cluster offset in bytes
1245 * @len: Length in bytes
1247 * Called with table_lock held.
1249 static int coroutine_fn qed_aio_write_data(void *opaque, int ret,
1250 uint64_t offset, size_t len)
1252 QEDAIOCB *acb = opaque;
1254 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1256 acb->find_cluster_ret = ret;
1258 switch (ret) {
1259 case QED_CLUSTER_FOUND:
1260 return qed_aio_write_inplace(acb, offset, len);
1262 case QED_CLUSTER_L2:
1263 case QED_CLUSTER_L1:
1264 case QED_CLUSTER_ZERO:
1265 return qed_aio_write_alloc(acb, len);
1267 default:
1268 g_assert_not_reached();
1273 * Read data cluster
1275 * @opaque: Read request
1276 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
1277 * @offset: Cluster offset in bytes
1278 * @len: Length in bytes
1280 * Called with table_lock held.
1282 static int coroutine_fn qed_aio_read_data(void *opaque, int ret,
1283 uint64_t offset, size_t len)
1285 QEDAIOCB *acb = opaque;
1286 BDRVQEDState *s = acb_to_s(acb);
1287 BlockDriverState *bs = acb->bs;
1288 int r;
1290 qemu_co_mutex_unlock(&s->table_lock);
1292 /* Adjust offset into cluster */
1293 offset += qed_offset_into_cluster(s, acb->cur_pos);
1295 trace_qed_aio_read_data(s, acb, ret, offset, len);
1297 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1299 /* Handle zero cluster and backing file reads, otherwise read
1300 * data cluster directly.
1302 if (ret == QED_CLUSTER_ZERO) {
1303 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
1304 r = 0;
1305 } else if (ret != QED_CLUSTER_FOUND) {
1306 r = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov);
1307 } else {
1308 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1309 r = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size,
1310 &acb->cur_qiov, 0);
1313 qemu_co_mutex_lock(&s->table_lock);
1314 return r;
1318 * Begin next I/O or complete the request
1320 static int coroutine_fn qed_aio_next_io(QEDAIOCB *acb)
1322 BDRVQEDState *s = acb_to_s(acb);
1323 uint64_t offset;
1324 size_t len;
1325 int ret;
1327 qemu_co_mutex_lock(&s->table_lock);
1328 while (1) {
1329 trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size);
1331 acb->qiov_offset += acb->cur_qiov.size;
1332 acb->cur_pos += acb->cur_qiov.size;
1333 qemu_iovec_reset(&acb->cur_qiov);
1335 /* Complete request */
1336 if (acb->cur_pos >= acb->end_pos) {
1337 ret = 0;
1338 break;
1341 /* Find next cluster and start I/O */
1342 len = acb->end_pos - acb->cur_pos;
1343 ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset);
1344 if (ret < 0) {
1345 break;
1348 if (acb->flags & QED_AIOCB_WRITE) {
1349 ret = qed_aio_write_data(acb, ret, offset, len);
1350 } else {
1351 ret = qed_aio_read_data(acb, ret, offset, len);
1354 if (ret < 0 && ret != -EAGAIN) {
1355 break;
1359 trace_qed_aio_complete(s, acb, ret);
1360 qed_aio_complete(acb);
1361 qemu_co_mutex_unlock(&s->table_lock);
1362 return ret;
1365 static int coroutine_fn qed_co_request(BlockDriverState *bs, int64_t sector_num,
1366 QEMUIOVector *qiov, int nb_sectors,
1367 int flags)
1369 QEDAIOCB acb = {
1370 .bs = bs,
1371 .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE,
1372 .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE,
1373 .qiov = qiov,
1374 .flags = flags,
1376 qemu_iovec_init(&acb.cur_qiov, qiov->niov);
1378 trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags);
1380 /* Start request */
1381 return qed_aio_next_io(&acb);
1384 static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs,
1385 int64_t sector_num, int nb_sectors,
1386 QEMUIOVector *qiov)
1388 return qed_co_request(bs, sector_num, qiov, nb_sectors, 0);
1391 static int coroutine_fn bdrv_qed_co_writev(BlockDriverState *bs,
1392 int64_t sector_num, int nb_sectors,
1393 QEMUIOVector *qiov, int flags)
1395 assert(!flags);
1396 return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRITE);
1399 static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs,
1400 int64_t offset,
1401 int64_t bytes,
1402 BdrvRequestFlags flags)
1404 BDRVQEDState *s = bs->opaque;
1407 * Zero writes start without an I/O buffer. If a buffer becomes necessary
1408 * then it will be allocated during request processing.
1410 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes);
1413 * QED is not prepared for 63bit write-zero requests, so rely on
1414 * max_pwrite_zeroes.
1416 assert(bytes <= INT_MAX);
1418 /* Fall back if the request is not aligned */
1419 if (qed_offset_into_cluster(s, offset) ||
1420 qed_offset_into_cluster(s, bytes)) {
1421 return -ENOTSUP;
1424 return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov,
1425 bytes >> BDRV_SECTOR_BITS,
1426 QED_AIOCB_WRITE | QED_AIOCB_ZERO);
1429 static int coroutine_fn bdrv_qed_co_truncate(BlockDriverState *bs,
1430 int64_t offset,
1431 bool exact,
1432 PreallocMode prealloc,
1433 BdrvRequestFlags flags,
1434 Error **errp)
1436 BDRVQEDState *s = bs->opaque;
1437 uint64_t old_image_size;
1438 int ret;
1440 if (prealloc != PREALLOC_MODE_OFF) {
1441 error_setg(errp, "Unsupported preallocation mode '%s'",
1442 PreallocMode_str(prealloc));
1443 return -ENOTSUP;
1446 if (!qed_is_image_size_valid(offset, s->header.cluster_size,
1447 s->header.table_size)) {
1448 error_setg(errp, "Invalid image size specified");
1449 return -EINVAL;
1452 if ((uint64_t)offset < s->header.image_size) {
1453 error_setg(errp, "Shrinking images is currently not supported");
1454 return -ENOTSUP;
1457 old_image_size = s->header.image_size;
1458 s->header.image_size = offset;
1459 ret = qed_write_header_sync(s);
1460 if (ret < 0) {
1461 s->header.image_size = old_image_size;
1462 error_setg_errno(errp, -ret, "Failed to update the image size");
1464 return ret;
1467 static int64_t bdrv_qed_getlength(BlockDriverState *bs)
1469 BDRVQEDState *s = bs->opaque;
1470 return s->header.image_size;
1473 static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1475 BDRVQEDState *s = bs->opaque;
1477 memset(bdi, 0, sizeof(*bdi));
1478 bdi->cluster_size = s->header.cluster_size;
1479 bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
1480 return 0;
1483 static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1484 const char *backing_file,
1485 const char *backing_fmt)
1487 BDRVQEDState *s = bs->opaque;
1488 QEDHeader new_header, le_header;
1489 void *buffer;
1490 size_t buffer_len, backing_file_len;
1491 int ret;
1493 /* Refuse to set backing filename if unknown compat feature bits are
1494 * active. If the image uses an unknown compat feature then we may not
1495 * know the layout of data following the header structure and cannot safely
1496 * add a new string.
1498 if (backing_file && (s->header.compat_features &
1499 ~QED_COMPAT_FEATURE_MASK)) {
1500 return -ENOTSUP;
1503 memcpy(&new_header, &s->header, sizeof(new_header));
1505 new_header.features &= ~(QED_F_BACKING_FILE |
1506 QED_F_BACKING_FORMAT_NO_PROBE);
1508 /* Adjust feature flags */
1509 if (backing_file) {
1510 new_header.features |= QED_F_BACKING_FILE;
1512 if (qed_fmt_is_raw(backing_fmt)) {
1513 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1517 /* Calculate new header size */
1518 backing_file_len = 0;
1520 if (backing_file) {
1521 backing_file_len = strlen(backing_file);
1524 buffer_len = sizeof(new_header);
1525 new_header.backing_filename_offset = buffer_len;
1526 new_header.backing_filename_size = backing_file_len;
1527 buffer_len += backing_file_len;
1529 /* Make sure we can rewrite header without failing */
1530 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1531 return -ENOSPC;
1534 /* Prepare new header */
1535 buffer = g_malloc(buffer_len);
1537 qed_header_cpu_to_le(&new_header, &le_header);
1538 memcpy(buffer, &le_header, sizeof(le_header));
1539 buffer_len = sizeof(le_header);
1541 if (backing_file) {
1542 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1543 buffer_len += backing_file_len;
1546 /* Write new header */
1547 ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len);
1548 g_free(buffer);
1549 if (ret == 0) {
1550 memcpy(&s->header, &new_header, sizeof(new_header));
1552 return ret;
1555 static void coroutine_fn bdrv_qed_co_invalidate_cache(BlockDriverState *bs,
1556 Error **errp)
1558 BDRVQEDState *s = bs->opaque;
1559 int ret;
1561 bdrv_qed_close(bs);
1563 bdrv_qed_init_state(bs);
1564 qemu_co_mutex_lock(&s->table_lock);
1565 ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, errp);
1566 qemu_co_mutex_unlock(&s->table_lock);
1567 if (ret < 0) {
1568 error_prepend(errp, "Could not reopen qed layer: ");
1572 static int coroutine_fn bdrv_qed_co_check(BlockDriverState *bs,
1573 BdrvCheckResult *result,
1574 BdrvCheckMode fix)
1576 BDRVQEDState *s = bs->opaque;
1577 int ret;
1579 qemu_co_mutex_lock(&s->table_lock);
1580 ret = qed_check(s, result, !!fix);
1581 qemu_co_mutex_unlock(&s->table_lock);
1583 return ret;
1586 static QemuOptsList qed_create_opts = {
1587 .name = "qed-create-opts",
1588 .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
1589 .desc = {
1591 .name = BLOCK_OPT_SIZE,
1592 .type = QEMU_OPT_SIZE,
1593 .help = "Virtual disk size"
1596 .name = BLOCK_OPT_BACKING_FILE,
1597 .type = QEMU_OPT_STRING,
1598 .help = "File name of a base image"
1601 .name = BLOCK_OPT_BACKING_FMT,
1602 .type = QEMU_OPT_STRING,
1603 .help = "Image format of the base image"
1606 .name = BLOCK_OPT_CLUSTER_SIZE,
1607 .type = QEMU_OPT_SIZE,
1608 .help = "Cluster size (in bytes)",
1609 .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE)
1612 .name = BLOCK_OPT_TABLE_SIZE,
1613 .type = QEMU_OPT_SIZE,
1614 .help = "L1/L2 table size (in clusters)"
1616 { /* end of list */ }
1620 static BlockDriver bdrv_qed = {
1621 .format_name = "qed",
1622 .instance_size = sizeof(BDRVQEDState),
1623 .create_opts = &qed_create_opts,
1624 .is_format = true,
1625 .supports_backing = true,
1627 .bdrv_probe = bdrv_qed_probe,
1628 .bdrv_open = bdrv_qed_open,
1629 .bdrv_close = bdrv_qed_close,
1630 .bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
1631 .bdrv_child_perm = bdrv_default_perms,
1632 .bdrv_co_create = bdrv_qed_co_create,
1633 .bdrv_co_create_opts = bdrv_qed_co_create_opts,
1634 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1635 .bdrv_co_block_status = bdrv_qed_co_block_status,
1636 .bdrv_co_readv = bdrv_qed_co_readv,
1637 .bdrv_co_writev = bdrv_qed_co_writev,
1638 .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes,
1639 .bdrv_co_truncate = bdrv_qed_co_truncate,
1640 .bdrv_getlength = bdrv_qed_getlength,
1641 .bdrv_get_info = bdrv_qed_get_info,
1642 .bdrv_refresh_limits = bdrv_qed_refresh_limits,
1643 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
1644 .bdrv_co_invalidate_cache = bdrv_qed_co_invalidate_cache,
1645 .bdrv_co_check = bdrv_qed_co_check,
1646 .bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
1647 .bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
1648 .bdrv_co_drain_begin = bdrv_qed_co_drain_begin,
1651 static void bdrv_qed_init(void)
1653 bdrv_register(&bdrv_qed);
1656 block_init(bdrv_qed_init);