block: Mark bdrv_co_pwrite_zeroes() and callers GRAPH_RDLOCK
[qemu/ar7.git] / block / qed.c
blobbdcb6de6dfb00d0c42f04b253016786c4657175b
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 "qemu/memalign.h"
24 #include "trace.h"
25 #include "qed.h"
26 #include "sysemu/block-backend.h"
27 #include "qapi/qmp/qdict.h"
28 #include "qapi/qobject-input-visitor.h"
29 #include "qapi/qapi-visit-block-core.h"
31 static QemuOptsList qed_create_opts;
33 static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
34 const char *filename)
36 const QEDHeader *header = (const QEDHeader *)buf;
38 if (buf_size < sizeof(*header)) {
39 return 0;
41 if (le32_to_cpu(header->magic) != QED_MAGIC) {
42 return 0;
44 return 100;
47 /**
48 * Check whether an image format is raw
50 * @fmt: Backing file format, may be NULL
52 static bool qed_fmt_is_raw(const char *fmt)
54 return fmt && strcmp(fmt, "raw") == 0;
57 static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
59 cpu->magic = le32_to_cpu(le->magic);
60 cpu->cluster_size = le32_to_cpu(le->cluster_size);
61 cpu->table_size = le32_to_cpu(le->table_size);
62 cpu->header_size = le32_to_cpu(le->header_size);
63 cpu->features = le64_to_cpu(le->features);
64 cpu->compat_features = le64_to_cpu(le->compat_features);
65 cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
66 cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
67 cpu->image_size = le64_to_cpu(le->image_size);
68 cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
69 cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
72 static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
74 le->magic = cpu_to_le32(cpu->magic);
75 le->cluster_size = cpu_to_le32(cpu->cluster_size);
76 le->table_size = cpu_to_le32(cpu->table_size);
77 le->header_size = cpu_to_le32(cpu->header_size);
78 le->features = cpu_to_le64(cpu->features);
79 le->compat_features = cpu_to_le64(cpu->compat_features);
80 le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
81 le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
82 le->image_size = cpu_to_le64(cpu->image_size);
83 le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
84 le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
87 int qed_write_header_sync(BDRVQEDState *s)
89 QEDHeader le;
91 qed_header_cpu_to_le(&s->header, &le);
92 return bdrv_pwrite(s->bs->file, 0, sizeof(le), &le, 0);
95 /**
96 * Update header in-place (does not rewrite backing filename or other strings)
98 * This function only updates known header fields in-place and does not affect
99 * extra data after the QED header.
101 * No new allocating reqs can start while this function runs.
103 static int coroutine_fn qed_write_header(BDRVQEDState *s)
105 /* We must write full sectors for O_DIRECT but cannot necessarily generate
106 * the data following the header if an unrecognized compat feature is
107 * active. Therefore, first read the sectors containing the header, update
108 * them, and write back.
111 int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE);
112 size_t len = nsectors * BDRV_SECTOR_SIZE;
113 uint8_t *buf;
114 int ret;
116 assert(s->allocating_acb || s->allocating_write_reqs_plugged);
118 buf = qemu_blockalign(s->bs, len);
120 ret = bdrv_co_pread(s->bs->file, 0, len, buf, 0);
121 if (ret < 0) {
122 goto out;
125 /* Update header */
126 qed_header_cpu_to_le(&s->header, (QEDHeader *) buf);
128 ret = bdrv_co_pwrite(s->bs->file, 0, len, buf, 0);
129 if (ret < 0) {
130 goto out;
133 ret = 0;
134 out:
135 qemu_vfree(buf);
136 return ret;
139 static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
141 uint64_t table_entries;
142 uint64_t l2_size;
144 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
145 l2_size = table_entries * cluster_size;
147 return l2_size * table_entries;
150 static bool qed_is_cluster_size_valid(uint32_t cluster_size)
152 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
153 cluster_size > QED_MAX_CLUSTER_SIZE) {
154 return false;
156 if (cluster_size & (cluster_size - 1)) {
157 return false; /* not power of 2 */
159 return true;
162 static bool qed_is_table_size_valid(uint32_t table_size)
164 if (table_size < QED_MIN_TABLE_SIZE ||
165 table_size > QED_MAX_TABLE_SIZE) {
166 return false;
168 if (table_size & (table_size - 1)) {
169 return false; /* not power of 2 */
171 return true;
174 static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
175 uint32_t table_size)
177 if (image_size % BDRV_SECTOR_SIZE != 0) {
178 return false; /* not multiple of sector size */
180 if (image_size > qed_max_image_size(cluster_size, table_size)) {
181 return false; /* image is too large */
183 return true;
187 * Read a string of known length from the image file
189 * @file: Image file
190 * @offset: File offset to start of string, in bytes
191 * @n: String length in bytes
192 * @buf: Destination buffer
193 * @buflen: Destination buffer length in bytes
194 * @ret: 0 on success, -errno on failure
196 * The string is NUL-terminated.
198 static int qed_read_string(BdrvChild *file, uint64_t offset, size_t n,
199 char *buf, size_t buflen)
201 int ret;
202 if (n >= buflen) {
203 return -EINVAL;
205 ret = bdrv_pread(file, offset, n, buf, 0);
206 if (ret < 0) {
207 return ret;
209 buf[n] = '\0';
210 return 0;
214 * Allocate new clusters
216 * @s: QED state
217 * @n: Number of contiguous clusters to allocate
218 * @ret: Offset of first allocated cluster
220 * This function only produces the offset where the new clusters should be
221 * written. It updates BDRVQEDState but does not make any changes to the image
222 * file.
224 * Called with table_lock held.
226 static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
228 uint64_t offset = s->file_size;
229 s->file_size += n * s->header.cluster_size;
230 return offset;
233 QEDTable *qed_alloc_table(BDRVQEDState *s)
235 /* Honor O_DIRECT memory alignment requirements */
236 return qemu_blockalign(s->bs,
237 s->header.cluster_size * s->header.table_size);
241 * Allocate a new zeroed L2 table
243 * Called with table_lock held.
245 static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
247 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
249 l2_table->table = qed_alloc_table(s);
250 l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
252 memset(l2_table->table->offsets, 0,
253 s->header.cluster_size * s->header.table_size);
254 return l2_table;
257 static bool coroutine_fn qed_plug_allocating_write_reqs(BDRVQEDState *s)
259 qemu_co_mutex_lock(&s->table_lock);
261 /* No reentrancy is allowed. */
262 assert(!s->allocating_write_reqs_plugged);
263 if (s->allocating_acb != NULL) {
264 /* Another allocating write came concurrently. This cannot happen
265 * from bdrv_qed_drain_begin, but it can happen when the timer runs.
267 qemu_co_mutex_unlock(&s->table_lock);
268 return false;
271 s->allocating_write_reqs_plugged = true;
272 qemu_co_mutex_unlock(&s->table_lock);
273 return true;
276 static void coroutine_fn qed_unplug_allocating_write_reqs(BDRVQEDState *s)
278 qemu_co_mutex_lock(&s->table_lock);
279 assert(s->allocating_write_reqs_plugged);
280 s->allocating_write_reqs_plugged = false;
281 qemu_co_queue_next(&s->allocating_write_reqs);
282 qemu_co_mutex_unlock(&s->table_lock);
285 static void coroutine_fn GRAPH_RDLOCK qed_need_check_timer(BDRVQEDState *s)
287 int ret;
289 trace_qed_need_check_timer_cb(s);
290 assert_bdrv_graph_readable();
292 if (!qed_plug_allocating_write_reqs(s)) {
293 return;
296 /* Ensure writes are on disk before clearing flag */
297 ret = bdrv_co_flush(s->bs->file->bs);
298 if (ret < 0) {
299 qed_unplug_allocating_write_reqs(s);
300 return;
303 s->header.features &= ~QED_F_NEED_CHECK;
304 ret = qed_write_header(s);
305 (void) ret;
307 qed_unplug_allocating_write_reqs(s);
309 ret = bdrv_co_flush(s->bs);
310 (void) ret;
313 static void coroutine_fn qed_need_check_timer_entry(void *opaque)
315 BDRVQEDState *s = opaque;
316 GRAPH_RDLOCK_GUARD();
318 qed_need_check_timer(opaque);
319 bdrv_dec_in_flight(s->bs);
322 static void qed_need_check_timer_cb(void *opaque)
324 BDRVQEDState *s = opaque;
325 Coroutine *co = qemu_coroutine_create(qed_need_check_timer_entry, opaque);
327 bdrv_inc_in_flight(s->bs);
328 qemu_coroutine_enter(co);
331 static void qed_start_need_check_timer(BDRVQEDState *s)
333 trace_qed_start_need_check_timer(s);
335 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
336 * migration.
338 timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
339 NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT);
342 /* It's okay to call this multiple times or when no timer is started */
343 static void qed_cancel_need_check_timer(BDRVQEDState *s)
345 trace_qed_cancel_need_check_timer(s);
346 timer_del(s->need_check_timer);
349 static void bdrv_qed_detach_aio_context(BlockDriverState *bs)
351 BDRVQEDState *s = bs->opaque;
353 qed_cancel_need_check_timer(s);
354 timer_free(s->need_check_timer);
357 static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
358 AioContext *new_context)
360 BDRVQEDState *s = bs->opaque;
362 s->need_check_timer = aio_timer_new(new_context,
363 QEMU_CLOCK_VIRTUAL, SCALE_NS,
364 qed_need_check_timer_cb, s);
365 if (s->header.features & QED_F_NEED_CHECK) {
366 qed_start_need_check_timer(s);
370 static void bdrv_qed_drain_begin(BlockDriverState *bs)
372 BDRVQEDState *s = bs->opaque;
374 /* Fire the timer immediately in order to start doing I/O as soon as the
375 * header is flushed.
377 if (s->need_check_timer && timer_pending(s->need_check_timer)) {
378 Coroutine *co;
380 qed_cancel_need_check_timer(s);
381 co = qemu_coroutine_create(qed_need_check_timer_entry, s);
382 bdrv_inc_in_flight(bs);
383 aio_co_enter(bdrv_get_aio_context(bs), co);
387 static void bdrv_qed_init_state(BlockDriverState *bs)
389 BDRVQEDState *s = bs->opaque;
391 memset(s, 0, sizeof(BDRVQEDState));
392 s->bs = bs;
393 qemu_co_mutex_init(&s->table_lock);
394 qemu_co_queue_init(&s->allocating_write_reqs);
397 /* Called with table_lock held. */
398 static int coroutine_fn GRAPH_RDLOCK
399 bdrv_qed_do_open(BlockDriverState *bs, QDict *options, int flags, Error **errp)
401 BDRVQEDState *s = bs->opaque;
402 QEDHeader le_header;
403 int64_t file_size;
404 int ret;
406 ret = bdrv_co_pread(bs->file, 0, sizeof(le_header), &le_header, 0);
407 if (ret < 0) {
408 error_setg(errp, "Failed to read QED header");
409 return ret;
411 qed_header_le_to_cpu(&le_header, &s->header);
413 if (s->header.magic != QED_MAGIC) {
414 error_setg(errp, "Image not in QED format");
415 return -EINVAL;
417 if (s->header.features & ~QED_FEATURE_MASK) {
418 /* image uses unsupported feature bits */
419 error_setg(errp, "Unsupported QED features: %" PRIx64,
420 s->header.features & ~QED_FEATURE_MASK);
421 return -ENOTSUP;
423 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
424 error_setg(errp, "QED cluster size is invalid");
425 return -EINVAL;
428 /* Round down file size to the last cluster */
429 file_size = bdrv_co_getlength(bs->file->bs);
430 if (file_size < 0) {
431 error_setg(errp, "Failed to get file length");
432 return file_size;
434 s->file_size = qed_start_of_cluster(s, file_size);
436 if (!qed_is_table_size_valid(s->header.table_size)) {
437 error_setg(errp, "QED table size is invalid");
438 return -EINVAL;
440 if (!qed_is_image_size_valid(s->header.image_size,
441 s->header.cluster_size,
442 s->header.table_size)) {
443 error_setg(errp, "QED image size is invalid");
444 return -EINVAL;
446 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
447 error_setg(errp, "QED table offset is invalid");
448 return -EINVAL;
451 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
452 sizeof(uint64_t);
453 s->l2_shift = ctz32(s->header.cluster_size);
454 s->l2_mask = s->table_nelems - 1;
455 s->l1_shift = s->l2_shift + ctz32(s->table_nelems);
457 /* Header size calculation must not overflow uint32_t */
458 if (s->header.header_size > UINT32_MAX / s->header.cluster_size) {
459 error_setg(errp, "QED header size is too large");
460 return -EINVAL;
463 if ((s->header.features & QED_F_BACKING_FILE)) {
464 g_autofree char *backing_file_str = NULL;
466 if ((uint64_t)s->header.backing_filename_offset +
467 s->header.backing_filename_size >
468 s->header.cluster_size * s->header.header_size) {
469 error_setg(errp, "QED backing filename offset is invalid");
470 return -EINVAL;
473 backing_file_str = g_malloc(sizeof(bs->backing_file));
474 ret = qed_read_string(bs->file, s->header.backing_filename_offset,
475 s->header.backing_filename_size,
476 backing_file_str, sizeof(bs->backing_file));
477 if (ret < 0) {
478 error_setg(errp, "Failed to read backing filename");
479 return ret;
482 if (!g_str_equal(backing_file_str, bs->backing_file)) {
483 pstrcpy(bs->backing_file, sizeof(bs->backing_file),
484 backing_file_str);
485 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
486 backing_file_str);
489 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
490 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
494 /* Reset unknown autoclear feature bits. This is a backwards
495 * compatibility mechanism that allows images to be opened by older
496 * programs, which "knock out" unknown feature bits. When an image is
497 * opened by a newer program again it can detect that the autoclear
498 * feature is no longer valid.
500 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
501 !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) {
502 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
504 ret = qed_write_header_sync(s);
505 if (ret) {
506 error_setg(errp, "Failed to update header");
507 return ret;
510 /* From here on only known autoclear feature bits are valid */
511 bdrv_co_flush(bs->file->bs);
514 s->l1_table = qed_alloc_table(s);
515 qed_init_l2_cache(&s->l2_cache);
517 ret = qed_read_l1_table_sync(s);
518 if (ret) {
519 error_setg(errp, "Failed to read L1 table");
520 goto out;
523 /* If image was not closed cleanly, check consistency */
524 if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
525 /* Read-only images cannot be fixed. There is no risk of corruption
526 * since write operations are not possible. Therefore, allow
527 * potentially inconsistent images to be opened read-only. This can
528 * aid data recovery from an otherwise inconsistent image.
530 if (!bdrv_is_read_only(bs->file->bs) &&
531 !(flags & BDRV_O_INACTIVE)) {
532 BdrvCheckResult result = {0};
534 ret = qed_check(s, &result, true);
535 if (ret) {
536 error_setg(errp, "Image corrupted");
537 goto out;
542 bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
544 out:
545 if (ret) {
546 qed_free_l2_cache(&s->l2_cache);
547 qemu_vfree(s->l1_table);
549 return ret;
552 typedef struct QEDOpenCo {
553 BlockDriverState *bs;
554 QDict *options;
555 int flags;
556 Error **errp;
557 int ret;
558 } QEDOpenCo;
560 static void coroutine_fn GRAPH_RDLOCK bdrv_qed_open_entry(void *opaque)
562 QEDOpenCo *qoc = opaque;
563 BDRVQEDState *s = qoc->bs->opaque;
565 qemu_co_mutex_lock(&s->table_lock);
566 qoc->ret = bdrv_qed_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp);
567 qemu_co_mutex_unlock(&s->table_lock);
570 static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
571 Error **errp)
573 QEDOpenCo qoc = {
574 .bs = bs,
575 .options = options,
576 .flags = flags,
577 .errp = errp,
578 .ret = -EINPROGRESS
580 int ret;
582 assume_graph_lock(); /* FIXME */
584 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
585 if (ret < 0) {
586 return ret;
589 bdrv_qed_init_state(bs);
590 if (qemu_in_coroutine()) {
591 bdrv_qed_open_entry(&qoc);
592 } else {
593 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
594 qemu_coroutine_enter(qemu_coroutine_create(bdrv_qed_open_entry, &qoc));
595 BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
597 BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
598 return qoc.ret;
601 static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
603 BDRVQEDState *s = bs->opaque;
605 bs->bl.pwrite_zeroes_alignment = s->header.cluster_size;
606 bs->bl.max_pwrite_zeroes = QEMU_ALIGN_DOWN(INT_MAX, s->header.cluster_size);
609 /* We have nothing to do for QED reopen, stubs just return
610 * success */
611 static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
612 BlockReopenQueue *queue, Error **errp)
614 return 0;
617 static void bdrv_qed_close(BlockDriverState *bs)
619 BDRVQEDState *s = bs->opaque;
621 bdrv_qed_detach_aio_context(bs);
623 /* Ensure writes reach stable storage */
624 bdrv_flush(bs->file->bs);
626 /* Clean shutdown, no check required on next open */
627 if (s->header.features & QED_F_NEED_CHECK) {
628 s->header.features &= ~QED_F_NEED_CHECK;
629 qed_write_header_sync(s);
632 qed_free_l2_cache(&s->l2_cache);
633 qemu_vfree(s->l1_table);
636 static int coroutine_fn bdrv_qed_co_create(BlockdevCreateOptions *opts,
637 Error **errp)
639 BlockdevCreateOptionsQed *qed_opts;
640 BlockBackend *blk = NULL;
641 BlockDriverState *bs = NULL;
643 QEDHeader header;
644 QEDHeader le_header;
645 uint8_t *l1_table = NULL;
646 size_t l1_size;
647 int ret = 0;
649 assert(opts->driver == BLOCKDEV_DRIVER_QED);
650 qed_opts = &opts->u.qed;
652 /* Validate options and set default values */
653 if (!qed_opts->has_cluster_size) {
654 qed_opts->cluster_size = QED_DEFAULT_CLUSTER_SIZE;
656 if (!qed_opts->has_table_size) {
657 qed_opts->table_size = QED_DEFAULT_TABLE_SIZE;
660 if (!qed_is_cluster_size_valid(qed_opts->cluster_size)) {
661 error_setg(errp, "QED cluster size must be within range [%u, %u] "
662 "and power of 2",
663 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
664 return -EINVAL;
666 if (!qed_is_table_size_valid(qed_opts->table_size)) {
667 error_setg(errp, "QED table size must be within range [%u, %u] "
668 "and power of 2",
669 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
670 return -EINVAL;
672 if (!qed_is_image_size_valid(qed_opts->size, qed_opts->cluster_size,
673 qed_opts->table_size))
675 error_setg(errp, "QED image size must be a non-zero multiple of "
676 "cluster size and less than %" PRIu64 " bytes",
677 qed_max_image_size(qed_opts->cluster_size,
678 qed_opts->table_size));
679 return -EINVAL;
682 /* Create BlockBackend to write to the image */
683 bs = bdrv_co_open_blockdev_ref(qed_opts->file, errp);
684 if (bs == NULL) {
685 return -EIO;
688 blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
689 errp);
690 if (!blk) {
691 ret = -EPERM;
692 goto out;
694 blk_set_allow_write_beyond_eof(blk, true);
696 /* Prepare image format */
697 header = (QEDHeader) {
698 .magic = QED_MAGIC,
699 .cluster_size = qed_opts->cluster_size,
700 .table_size = qed_opts->table_size,
701 .header_size = 1,
702 .features = 0,
703 .compat_features = 0,
704 .l1_table_offset = qed_opts->cluster_size,
705 .image_size = qed_opts->size,
708 l1_size = header.cluster_size * header.table_size;
711 * The QED format associates file length with allocation status,
712 * so a new file (which is empty) must have a length of 0.
714 ret = blk_co_truncate(blk, 0, true, PREALLOC_MODE_OFF, 0, errp);
715 if (ret < 0) {
716 goto out;
719 if (qed_opts->backing_file) {
720 header.features |= QED_F_BACKING_FILE;
721 header.backing_filename_offset = sizeof(le_header);
722 header.backing_filename_size = strlen(qed_opts->backing_file);
724 if (qed_opts->has_backing_fmt) {
725 const char *backing_fmt = BlockdevDriver_str(qed_opts->backing_fmt);
726 if (qed_fmt_is_raw(backing_fmt)) {
727 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
732 qed_header_cpu_to_le(&header, &le_header);
733 ret = blk_co_pwrite(blk, 0, sizeof(le_header), &le_header, 0);
734 if (ret < 0) {
735 goto out;
737 ret = blk_co_pwrite(blk, sizeof(le_header), header.backing_filename_size,
738 qed_opts->backing_file, 0);
739 if (ret < 0) {
740 goto out;
743 l1_table = g_malloc0(l1_size);
744 ret = blk_co_pwrite(blk, header.l1_table_offset, l1_size, l1_table, 0);
745 if (ret < 0) {
746 goto out;
749 ret = 0; /* success */
750 out:
751 g_free(l1_table);
752 blk_unref(blk);
753 bdrv_unref(bs);
754 return ret;
757 static int coroutine_fn bdrv_qed_co_create_opts(BlockDriver *drv,
758 const char *filename,
759 QemuOpts *opts,
760 Error **errp)
762 BlockdevCreateOptions *create_options = NULL;
763 QDict *qdict;
764 Visitor *v;
765 BlockDriverState *bs = NULL;
766 int ret;
768 static const QDictRenames opt_renames[] = {
769 { BLOCK_OPT_BACKING_FILE, "backing-file" },
770 { BLOCK_OPT_BACKING_FMT, "backing-fmt" },
771 { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" },
772 { BLOCK_OPT_TABLE_SIZE, "table-size" },
773 { NULL, NULL },
776 /* Parse options and convert legacy syntax */
777 qdict = qemu_opts_to_qdict_filtered(opts, NULL, &qed_create_opts, true);
779 if (!qdict_rename_keys(qdict, opt_renames, errp)) {
780 ret = -EINVAL;
781 goto fail;
784 /* Create and open the file (protocol layer) */
785 ret = bdrv_co_create_file(filename, opts, errp);
786 if (ret < 0) {
787 goto fail;
790 bs = bdrv_co_open(filename, NULL, NULL,
791 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
792 if (bs == NULL) {
793 ret = -EIO;
794 goto fail;
797 /* Now get the QAPI type BlockdevCreateOptions */
798 qdict_put_str(qdict, "driver", "qed");
799 qdict_put_str(qdict, "file", bs->node_name);
801 v = qobject_input_visitor_new_flat_confused(qdict, errp);
802 if (!v) {
803 ret = -EINVAL;
804 goto fail;
807 visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
808 visit_free(v);
809 if (!create_options) {
810 ret = -EINVAL;
811 goto fail;
814 /* Silently round up size */
815 assert(create_options->driver == BLOCKDEV_DRIVER_QED);
816 create_options->u.qed.size =
817 ROUND_UP(create_options->u.qed.size, BDRV_SECTOR_SIZE);
819 /* Create the qed image (format layer) */
820 ret = bdrv_qed_co_create(create_options, errp);
822 fail:
823 qobject_unref(qdict);
824 bdrv_unref(bs);
825 qapi_free_BlockdevCreateOptions(create_options);
826 return ret;
829 static int coroutine_fn bdrv_qed_co_block_status(BlockDriverState *bs,
830 bool want_zero,
831 int64_t pos, int64_t bytes,
832 int64_t *pnum, int64_t *map,
833 BlockDriverState **file)
835 BDRVQEDState *s = bs->opaque;
836 size_t len = MIN(bytes, SIZE_MAX);
837 int status;
838 QEDRequest request = { .l2_table = NULL };
839 uint64_t offset;
840 int ret;
842 qemu_co_mutex_lock(&s->table_lock);
843 ret = qed_find_cluster(s, &request, pos, &len, &offset);
845 *pnum = len;
846 switch (ret) {
847 case QED_CLUSTER_FOUND:
848 *map = offset | qed_offset_into_cluster(s, pos);
849 status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
850 *file = bs->file->bs;
851 break;
852 case QED_CLUSTER_ZERO:
853 status = BDRV_BLOCK_ZERO;
854 break;
855 case QED_CLUSTER_L2:
856 case QED_CLUSTER_L1:
857 status = 0;
858 break;
859 default:
860 assert(ret < 0);
861 status = ret;
862 break;
865 qed_unref_l2_cache_entry(request.l2_table);
866 qemu_co_mutex_unlock(&s->table_lock);
868 return status;
871 static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
873 return acb->bs->opaque;
877 * Read from the backing file or zero-fill if no backing file
879 * @s: QED state
880 * @pos: Byte position in device
881 * @qiov: Destination I/O vector
883 * This function reads qiov->size bytes starting at pos from the backing file.
884 * If there is no backing file then zeroes are read.
886 static int coroutine_fn qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
887 QEMUIOVector *qiov)
889 if (s->bs->backing) {
890 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
891 return bdrv_co_preadv(s->bs->backing, pos, qiov->size, qiov, 0);
893 qemu_iovec_memset(qiov, 0, 0, qiov->size);
894 return 0;
898 * Copy data from backing file into the image
900 * @s: QED state
901 * @pos: Byte position in device
902 * @len: Number of bytes
903 * @offset: Byte offset in image file
905 static int coroutine_fn qed_copy_from_backing_file(BDRVQEDState *s,
906 uint64_t pos, uint64_t len,
907 uint64_t offset)
909 QEMUIOVector qiov;
910 int ret;
912 /* Skip copy entirely if there is no work to do */
913 if (len == 0) {
914 return 0;
917 qemu_iovec_init_buf(&qiov, qemu_blockalign(s->bs, len), len);
919 ret = qed_read_backing_file(s, pos, &qiov);
921 if (ret) {
922 goto out;
925 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
926 ret = bdrv_co_pwritev(s->bs->file, offset, qiov.size, &qiov, 0);
927 if (ret < 0) {
928 goto out;
930 ret = 0;
931 out:
932 qemu_vfree(qemu_iovec_buf(&qiov));
933 return ret;
937 * Link one or more contiguous clusters into a table
939 * @s: QED state
940 * @table: L2 table
941 * @index: First cluster index
942 * @n: Number of contiguous clusters
943 * @cluster: First cluster offset
945 * The cluster offset may be an allocated byte offset in the image file, the
946 * zero cluster marker, or the unallocated cluster marker.
948 * Called with table_lock held.
950 static void coroutine_fn qed_update_l2_table(BDRVQEDState *s, QEDTable *table,
951 int index, unsigned int n,
952 uint64_t cluster)
954 int i;
955 for (i = index; i < index + n; i++) {
956 table->offsets[i] = cluster;
957 if (!qed_offset_is_unalloc_cluster(cluster) &&
958 !qed_offset_is_zero_cluster(cluster)) {
959 cluster += s->header.cluster_size;
964 /* Called with table_lock held. */
965 static void coroutine_fn qed_aio_complete(QEDAIOCB *acb)
967 BDRVQEDState *s = acb_to_s(acb);
969 /* Free resources */
970 qemu_iovec_destroy(&acb->cur_qiov);
971 qed_unref_l2_cache_entry(acb->request.l2_table);
973 /* Free the buffer we may have allocated for zero writes */
974 if (acb->flags & QED_AIOCB_ZERO) {
975 qemu_vfree(acb->qiov->iov[0].iov_base);
976 acb->qiov->iov[0].iov_base = NULL;
979 /* Start next allocating write request waiting behind this one. Note that
980 * requests enqueue themselves when they first hit an unallocated cluster
981 * but they wait until the entire request is finished before waking up the
982 * next request in the queue. This ensures that we don't cycle through
983 * requests multiple times but rather finish one at a time completely.
985 if (acb == s->allocating_acb) {
986 s->allocating_acb = NULL;
987 if (!qemu_co_queue_empty(&s->allocating_write_reqs)) {
988 qemu_co_queue_next(&s->allocating_write_reqs);
989 } else if (s->header.features & QED_F_NEED_CHECK) {
990 qed_start_need_check_timer(s);
996 * Update L1 table with new L2 table offset and write it out
998 * Called with table_lock held.
1000 static int coroutine_fn GRAPH_RDLOCK qed_aio_write_l1_update(QEDAIOCB *acb)
1002 BDRVQEDState *s = acb_to_s(acb);
1003 CachedL2Table *l2_table = acb->request.l2_table;
1004 uint64_t l2_offset = l2_table->offset;
1005 int index, ret;
1007 index = qed_l1_index(s, acb->cur_pos);
1008 s->l1_table->offsets[index] = l2_table->offset;
1010 ret = qed_write_l1_table(s, index, 1);
1012 /* Commit the current L2 table to the cache */
1013 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
1015 /* This is guaranteed to succeed because we just committed the entry to the
1016 * cache.
1018 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
1019 assert(acb->request.l2_table != NULL);
1021 return ret;
1026 * Update L2 table with new cluster offsets and write them out
1028 * Called with table_lock held.
1030 static int coroutine_fn GRAPH_RDLOCK
1031 qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset)
1033 BDRVQEDState *s = acb_to_s(acb);
1034 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
1035 int index, ret;
1037 if (need_alloc) {
1038 qed_unref_l2_cache_entry(acb->request.l2_table);
1039 acb->request.l2_table = qed_new_l2_table(s);
1042 index = qed_l2_index(s, acb->cur_pos);
1043 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
1044 offset);
1046 if (need_alloc) {
1047 /* Write out the whole new L2 table */
1048 ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true);
1049 if (ret) {
1050 return ret;
1052 return qed_aio_write_l1_update(acb);
1053 } else {
1054 /* Write out only the updated part of the L2 table */
1055 ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters,
1056 false);
1057 if (ret) {
1058 return ret;
1061 return 0;
1065 * Write data to the image file
1067 * Called with table_lock *not* held.
1069 static int coroutine_fn qed_aio_write_main(QEDAIOCB *acb)
1071 BDRVQEDState *s = acb_to_s(acb);
1072 uint64_t offset = acb->cur_cluster +
1073 qed_offset_into_cluster(s, acb->cur_pos);
1075 trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size);
1077 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
1078 return bdrv_co_pwritev(s->bs->file, offset, acb->cur_qiov.size,
1079 &acb->cur_qiov, 0);
1083 * Populate untouched regions of new data cluster
1085 * Called with table_lock held.
1087 static int coroutine_fn GRAPH_RDLOCK qed_aio_write_cow(QEDAIOCB *acb)
1089 BDRVQEDState *s = acb_to_s(acb);
1090 uint64_t start, len, offset;
1091 int ret;
1093 qemu_co_mutex_unlock(&s->table_lock);
1095 /* Populate front untouched region of new data cluster */
1096 start = qed_start_of_cluster(s, acb->cur_pos);
1097 len = qed_offset_into_cluster(s, acb->cur_pos);
1099 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1100 ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster);
1101 if (ret < 0) {
1102 goto out;
1105 /* Populate back untouched region of new data cluster */
1106 start = acb->cur_pos + acb->cur_qiov.size;
1107 len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1108 offset = acb->cur_cluster +
1109 qed_offset_into_cluster(s, acb->cur_pos) +
1110 acb->cur_qiov.size;
1112 trace_qed_aio_write_postfill(s, acb, start, len, offset);
1113 ret = qed_copy_from_backing_file(s, start, len, offset);
1114 if (ret < 0) {
1115 goto out;
1118 ret = qed_aio_write_main(acb);
1119 if (ret < 0) {
1120 goto out;
1123 if (s->bs->backing) {
1125 * Flush new data clusters before updating the L2 table
1127 * This flush is necessary when a backing file is in use. A crash
1128 * during an allocating write could result in empty clusters in the
1129 * image. If the write only touched a subregion of the cluster,
1130 * then backing image sectors have been lost in the untouched
1131 * region. The solution is to flush after writing a new data
1132 * cluster and before updating the L2 table.
1134 ret = bdrv_co_flush(s->bs->file->bs);
1137 out:
1138 qemu_co_mutex_lock(&s->table_lock);
1139 return ret;
1143 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1145 static bool qed_should_set_need_check(BDRVQEDState *s)
1147 /* The flush before L2 update path ensures consistency */
1148 if (s->bs->backing) {
1149 return false;
1152 return !(s->header.features & QED_F_NEED_CHECK);
1156 * Write new data cluster
1158 * @acb: Write request
1159 * @len: Length in bytes
1161 * This path is taken when writing to previously unallocated clusters.
1163 * Called with table_lock held.
1165 static int coroutine_fn GRAPH_RDLOCK
1166 qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1168 BDRVQEDState *s = acb_to_s(acb);
1169 int ret;
1171 /* Cancel timer when the first allocating request comes in */
1172 if (s->allocating_acb == NULL) {
1173 qed_cancel_need_check_timer(s);
1176 /* Freeze this request if another allocating write is in progress */
1177 if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) {
1178 if (s->allocating_acb != NULL) {
1179 qemu_co_queue_wait(&s->allocating_write_reqs, &s->table_lock);
1180 assert(s->allocating_acb == NULL);
1182 s->allocating_acb = acb;
1183 return -EAGAIN; /* start over with looking up table entries */
1186 acb->cur_nclusters = qed_bytes_to_clusters(s,
1187 qed_offset_into_cluster(s, acb->cur_pos) + len);
1188 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1190 if (acb->flags & QED_AIOCB_ZERO) {
1191 /* Skip ahead if the clusters are already zero */
1192 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
1193 return 0;
1195 acb->cur_cluster = 1;
1196 } else {
1197 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1200 if (qed_should_set_need_check(s)) {
1201 s->header.features |= QED_F_NEED_CHECK;
1202 ret = qed_write_header(s);
1203 if (ret < 0) {
1204 return ret;
1208 if (!(acb->flags & QED_AIOCB_ZERO)) {
1209 ret = qed_aio_write_cow(acb);
1210 if (ret < 0) {
1211 return ret;
1215 return qed_aio_write_l2_update(acb, acb->cur_cluster);
1219 * Write data cluster in place
1221 * @acb: Write request
1222 * @offset: Cluster offset in bytes
1223 * @len: Length in bytes
1225 * This path is taken when writing to already allocated clusters.
1227 * Called with table_lock held.
1229 static int coroutine_fn qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset,
1230 size_t len)
1232 BDRVQEDState *s = acb_to_s(acb);
1233 int r;
1235 qemu_co_mutex_unlock(&s->table_lock);
1237 /* Allocate buffer for zero writes */
1238 if (acb->flags & QED_AIOCB_ZERO) {
1239 struct iovec *iov = acb->qiov->iov;
1241 if (!iov->iov_base) {
1242 iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len);
1243 if (iov->iov_base == NULL) {
1244 r = -ENOMEM;
1245 goto out;
1247 memset(iov->iov_base, 0, iov->iov_len);
1251 /* Calculate the I/O vector */
1252 acb->cur_cluster = offset;
1253 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1255 /* Do the actual write. */
1256 r = qed_aio_write_main(acb);
1257 out:
1258 qemu_co_mutex_lock(&s->table_lock);
1259 return r;
1263 * Write data cluster
1265 * @opaque: Write request
1266 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
1267 * @offset: Cluster offset in bytes
1268 * @len: Length in bytes
1270 * Called with table_lock held.
1272 static int coroutine_fn GRAPH_RDLOCK
1273 qed_aio_write_data(void *opaque, int ret, uint64_t offset, size_t len)
1275 QEDAIOCB *acb = opaque;
1277 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1279 acb->find_cluster_ret = ret;
1281 switch (ret) {
1282 case QED_CLUSTER_FOUND:
1283 return qed_aio_write_inplace(acb, offset, len);
1285 case QED_CLUSTER_L2:
1286 case QED_CLUSTER_L1:
1287 case QED_CLUSTER_ZERO:
1288 return qed_aio_write_alloc(acb, len);
1290 default:
1291 g_assert_not_reached();
1296 * Read data cluster
1298 * @opaque: Read request
1299 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
1300 * @offset: Cluster offset in bytes
1301 * @len: Length in bytes
1303 * Called with table_lock held.
1305 static int coroutine_fn qed_aio_read_data(void *opaque, int ret,
1306 uint64_t offset, size_t len)
1308 QEDAIOCB *acb = opaque;
1309 BDRVQEDState *s = acb_to_s(acb);
1310 BlockDriverState *bs = acb->bs;
1311 int r;
1313 qemu_co_mutex_unlock(&s->table_lock);
1315 /* Adjust offset into cluster */
1316 offset += qed_offset_into_cluster(s, acb->cur_pos);
1318 trace_qed_aio_read_data(s, acb, ret, offset, len);
1320 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1322 /* Handle zero cluster and backing file reads, otherwise read
1323 * data cluster directly.
1325 if (ret == QED_CLUSTER_ZERO) {
1326 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
1327 r = 0;
1328 } else if (ret != QED_CLUSTER_FOUND) {
1329 r = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov);
1330 } else {
1331 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1332 r = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size,
1333 &acb->cur_qiov, 0);
1336 qemu_co_mutex_lock(&s->table_lock);
1337 return r;
1341 * Begin next I/O or complete the request
1343 static int coroutine_fn GRAPH_RDLOCK qed_aio_next_io(QEDAIOCB *acb)
1345 BDRVQEDState *s = acb_to_s(acb);
1346 uint64_t offset;
1347 size_t len;
1348 int ret;
1350 qemu_co_mutex_lock(&s->table_lock);
1351 while (1) {
1352 trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size);
1354 acb->qiov_offset += acb->cur_qiov.size;
1355 acb->cur_pos += acb->cur_qiov.size;
1356 qemu_iovec_reset(&acb->cur_qiov);
1358 /* Complete request */
1359 if (acb->cur_pos >= acb->end_pos) {
1360 ret = 0;
1361 break;
1364 /* Find next cluster and start I/O */
1365 len = acb->end_pos - acb->cur_pos;
1366 ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset);
1367 if (ret < 0) {
1368 break;
1371 if (acb->flags & QED_AIOCB_WRITE) {
1372 ret = qed_aio_write_data(acb, ret, offset, len);
1373 } else {
1374 ret = qed_aio_read_data(acb, ret, offset, len);
1377 if (ret < 0 && ret != -EAGAIN) {
1378 break;
1382 trace_qed_aio_complete(s, acb, ret);
1383 qed_aio_complete(acb);
1384 qemu_co_mutex_unlock(&s->table_lock);
1385 return ret;
1388 static int coroutine_fn GRAPH_RDLOCK
1389 qed_co_request(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov,
1390 int nb_sectors, int flags)
1392 QEDAIOCB acb = {
1393 .bs = bs,
1394 .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE,
1395 .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE,
1396 .qiov = qiov,
1397 .flags = flags,
1399 qemu_iovec_init(&acb.cur_qiov, qiov->niov);
1401 trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags);
1403 /* Start request */
1404 return qed_aio_next_io(&acb);
1407 static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs,
1408 int64_t sector_num, int nb_sectors,
1409 QEMUIOVector *qiov)
1411 assume_graph_lock(); /* FIXME */
1412 return qed_co_request(bs, sector_num, qiov, nb_sectors, 0);
1415 static int coroutine_fn bdrv_qed_co_writev(BlockDriverState *bs,
1416 int64_t sector_num, int nb_sectors,
1417 QEMUIOVector *qiov, int flags)
1419 assume_graph_lock(); /* FIXME */
1420 return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRITE);
1423 static int coroutine_fn GRAPH_RDLOCK
1424 bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
1425 BdrvRequestFlags flags)
1427 BDRVQEDState *s = bs->opaque;
1430 * Zero writes start without an I/O buffer. If a buffer becomes necessary
1431 * then it will be allocated during request processing.
1433 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes);
1436 * QED is not prepared for 63bit write-zero requests, so rely on
1437 * max_pwrite_zeroes.
1439 assert(bytes <= INT_MAX);
1441 /* Fall back if the request is not aligned */
1442 if (qed_offset_into_cluster(s, offset) ||
1443 qed_offset_into_cluster(s, bytes)) {
1444 return -ENOTSUP;
1447 return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov,
1448 bytes >> BDRV_SECTOR_BITS,
1449 QED_AIOCB_WRITE | QED_AIOCB_ZERO);
1452 static int coroutine_fn bdrv_qed_co_truncate(BlockDriverState *bs,
1453 int64_t offset,
1454 bool exact,
1455 PreallocMode prealloc,
1456 BdrvRequestFlags flags,
1457 Error **errp)
1459 BDRVQEDState *s = bs->opaque;
1460 uint64_t old_image_size;
1461 int ret;
1463 if (prealloc != PREALLOC_MODE_OFF) {
1464 error_setg(errp, "Unsupported preallocation mode '%s'",
1465 PreallocMode_str(prealloc));
1466 return -ENOTSUP;
1469 if (!qed_is_image_size_valid(offset, s->header.cluster_size,
1470 s->header.table_size)) {
1471 error_setg(errp, "Invalid image size specified");
1472 return -EINVAL;
1475 if ((uint64_t)offset < s->header.image_size) {
1476 error_setg(errp, "Shrinking images is currently not supported");
1477 return -ENOTSUP;
1480 old_image_size = s->header.image_size;
1481 s->header.image_size = offset;
1482 ret = qed_write_header_sync(s);
1483 if (ret < 0) {
1484 s->header.image_size = old_image_size;
1485 error_setg_errno(errp, -ret, "Failed to update the image size");
1487 return ret;
1490 static int64_t coroutine_fn bdrv_qed_co_getlength(BlockDriverState *bs)
1492 BDRVQEDState *s = bs->opaque;
1493 return s->header.image_size;
1496 static int coroutine_fn
1497 bdrv_qed_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1499 BDRVQEDState *s = bs->opaque;
1501 memset(bdi, 0, sizeof(*bdi));
1502 bdi->cluster_size = s->header.cluster_size;
1503 bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
1504 return 0;
1507 static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1508 const char *backing_file,
1509 const char *backing_fmt)
1511 BDRVQEDState *s = bs->opaque;
1512 QEDHeader new_header, le_header;
1513 void *buffer;
1514 size_t buffer_len, backing_file_len;
1515 int ret;
1517 /* Refuse to set backing filename if unknown compat feature bits are
1518 * active. If the image uses an unknown compat feature then we may not
1519 * know the layout of data following the header structure and cannot safely
1520 * add a new string.
1522 if (backing_file && (s->header.compat_features &
1523 ~QED_COMPAT_FEATURE_MASK)) {
1524 return -ENOTSUP;
1527 memcpy(&new_header, &s->header, sizeof(new_header));
1529 new_header.features &= ~(QED_F_BACKING_FILE |
1530 QED_F_BACKING_FORMAT_NO_PROBE);
1532 /* Adjust feature flags */
1533 if (backing_file) {
1534 new_header.features |= QED_F_BACKING_FILE;
1536 if (qed_fmt_is_raw(backing_fmt)) {
1537 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1541 /* Calculate new header size */
1542 backing_file_len = 0;
1544 if (backing_file) {
1545 backing_file_len = strlen(backing_file);
1548 buffer_len = sizeof(new_header);
1549 new_header.backing_filename_offset = buffer_len;
1550 new_header.backing_filename_size = backing_file_len;
1551 buffer_len += backing_file_len;
1553 /* Make sure we can rewrite header without failing */
1554 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1555 return -ENOSPC;
1558 /* Prepare new header */
1559 buffer = g_malloc(buffer_len);
1561 qed_header_cpu_to_le(&new_header, &le_header);
1562 memcpy(buffer, &le_header, sizeof(le_header));
1563 buffer_len = sizeof(le_header);
1565 if (backing_file) {
1566 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1567 buffer_len += backing_file_len;
1570 /* Write new header */
1571 ret = bdrv_pwrite_sync(bs->file, 0, buffer_len, buffer, 0);
1572 g_free(buffer);
1573 if (ret == 0) {
1574 memcpy(&s->header, &new_header, sizeof(new_header));
1576 return ret;
1579 static void coroutine_fn GRAPH_RDLOCK
1580 bdrv_qed_co_invalidate_cache(BlockDriverState *bs, Error **errp)
1582 BDRVQEDState *s = bs->opaque;
1583 int ret;
1585 bdrv_qed_close(bs);
1587 bdrv_qed_init_state(bs);
1588 qemu_co_mutex_lock(&s->table_lock);
1589 ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, errp);
1590 qemu_co_mutex_unlock(&s->table_lock);
1591 if (ret < 0) {
1592 error_prepend(errp, "Could not reopen qed layer: ");
1596 static int coroutine_fn GRAPH_RDLOCK
1597 bdrv_qed_co_check(BlockDriverState *bs, BdrvCheckResult *result,
1598 BdrvCheckMode fix)
1600 BDRVQEDState *s = bs->opaque;
1601 int ret;
1603 qemu_co_mutex_lock(&s->table_lock);
1604 ret = qed_check(s, result, !!fix);
1605 qemu_co_mutex_unlock(&s->table_lock);
1607 return ret;
1610 static QemuOptsList qed_create_opts = {
1611 .name = "qed-create-opts",
1612 .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
1613 .desc = {
1615 .name = BLOCK_OPT_SIZE,
1616 .type = QEMU_OPT_SIZE,
1617 .help = "Virtual disk size"
1620 .name = BLOCK_OPT_BACKING_FILE,
1621 .type = QEMU_OPT_STRING,
1622 .help = "File name of a base image"
1625 .name = BLOCK_OPT_BACKING_FMT,
1626 .type = QEMU_OPT_STRING,
1627 .help = "Image format of the base image"
1630 .name = BLOCK_OPT_CLUSTER_SIZE,
1631 .type = QEMU_OPT_SIZE,
1632 .help = "Cluster size (in bytes)",
1633 .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE)
1636 .name = BLOCK_OPT_TABLE_SIZE,
1637 .type = QEMU_OPT_SIZE,
1638 .help = "L1/L2 table size (in clusters)"
1640 { /* end of list */ }
1644 static BlockDriver bdrv_qed = {
1645 .format_name = "qed",
1646 .instance_size = sizeof(BDRVQEDState),
1647 .create_opts = &qed_create_opts,
1648 .is_format = true,
1649 .supports_backing = true,
1651 .bdrv_probe = bdrv_qed_probe,
1652 .bdrv_open = bdrv_qed_open,
1653 .bdrv_close = bdrv_qed_close,
1654 .bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
1655 .bdrv_child_perm = bdrv_default_perms,
1656 .bdrv_co_create = bdrv_qed_co_create,
1657 .bdrv_co_create_opts = bdrv_qed_co_create_opts,
1658 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1659 .bdrv_co_block_status = bdrv_qed_co_block_status,
1660 .bdrv_co_readv = bdrv_qed_co_readv,
1661 .bdrv_co_writev = bdrv_qed_co_writev,
1662 .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes,
1663 .bdrv_co_truncate = bdrv_qed_co_truncate,
1664 .bdrv_co_getlength = bdrv_qed_co_getlength,
1665 .bdrv_co_get_info = bdrv_qed_co_get_info,
1666 .bdrv_refresh_limits = bdrv_qed_refresh_limits,
1667 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
1668 .bdrv_co_invalidate_cache = bdrv_qed_co_invalidate_cache,
1669 .bdrv_co_check = bdrv_qed_co_check,
1670 .bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
1671 .bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
1672 .bdrv_drain_begin = bdrv_qed_drain_begin,
1675 static void bdrv_qed_init(void)
1677 bdrv_register(&bdrv_qed);
1680 block_init(bdrv_qed_init);