s390x/ipl: clean up qom definitions and turn into TYPE_DEVICE
[qemu.git] / block / qed.c
blob5ea05d49093100657943c57520330f55a8e04ca1
1 /*
2 * QEMU Enhanced Disk Format
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/timer.h"
16 #include "trace.h"
17 #include "qed.h"
18 #include "qapi/qmp/qerror.h"
19 #include "migration/migration.h"
21 static const AIOCBInfo qed_aiocb_info = {
22 .aiocb_size = sizeof(QEDAIOCB),
25 static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
26 const char *filename)
28 const QEDHeader *header = (const QEDHeader *)buf;
30 if (buf_size < sizeof(*header)) {
31 return 0;
33 if (le32_to_cpu(header->magic) != QED_MAGIC) {
34 return 0;
36 return 100;
39 /**
40 * Check whether an image format is raw
42 * @fmt: Backing file format, may be NULL
44 static bool qed_fmt_is_raw(const char *fmt)
46 return fmt && strcmp(fmt, "raw") == 0;
49 static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
51 cpu->magic = le32_to_cpu(le->magic);
52 cpu->cluster_size = le32_to_cpu(le->cluster_size);
53 cpu->table_size = le32_to_cpu(le->table_size);
54 cpu->header_size = le32_to_cpu(le->header_size);
55 cpu->features = le64_to_cpu(le->features);
56 cpu->compat_features = le64_to_cpu(le->compat_features);
57 cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
58 cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
59 cpu->image_size = le64_to_cpu(le->image_size);
60 cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
61 cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
64 static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
66 le->magic = cpu_to_le32(cpu->magic);
67 le->cluster_size = cpu_to_le32(cpu->cluster_size);
68 le->table_size = cpu_to_le32(cpu->table_size);
69 le->header_size = cpu_to_le32(cpu->header_size);
70 le->features = cpu_to_le64(cpu->features);
71 le->compat_features = cpu_to_le64(cpu->compat_features);
72 le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
73 le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
74 le->image_size = cpu_to_le64(cpu->image_size);
75 le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
76 le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
79 int qed_write_header_sync(BDRVQEDState *s)
81 QEDHeader le;
82 int ret;
84 qed_header_cpu_to_le(&s->header, &le);
85 ret = bdrv_pwrite(s->bs->file->bs, 0, &le, sizeof(le));
86 if (ret != sizeof(le)) {
87 return ret;
89 return 0;
92 typedef struct {
93 GenericCB gencb;
94 BDRVQEDState *s;
95 struct iovec iov;
96 QEMUIOVector qiov;
97 int nsectors;
98 uint8_t *buf;
99 } QEDWriteHeaderCB;
101 static void qed_write_header_cb(void *opaque, int ret)
103 QEDWriteHeaderCB *write_header_cb = opaque;
105 qemu_vfree(write_header_cb->buf);
106 gencb_complete(write_header_cb, ret);
109 static void qed_write_header_read_cb(void *opaque, int ret)
111 QEDWriteHeaderCB *write_header_cb = opaque;
112 BDRVQEDState *s = write_header_cb->s;
114 if (ret) {
115 qed_write_header_cb(write_header_cb, ret);
116 return;
119 /* Update header */
120 qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf);
122 bdrv_aio_writev(s->bs->file->bs, 0, &write_header_cb->qiov,
123 write_header_cb->nsectors, qed_write_header_cb,
124 write_header_cb);
128 * Update header in-place (does not rewrite backing filename or other strings)
130 * This function only updates known header fields in-place and does not affect
131 * extra data after the QED header.
133 static void qed_write_header(BDRVQEDState *s, BlockCompletionFunc cb,
134 void *opaque)
136 /* We must write full sectors for O_DIRECT but cannot necessarily generate
137 * the data following the header if an unrecognized compat feature is
138 * active. Therefore, first read the sectors containing the header, update
139 * them, and write back.
142 int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) /
143 BDRV_SECTOR_SIZE;
144 size_t len = nsectors * BDRV_SECTOR_SIZE;
145 QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb),
146 cb, opaque);
148 write_header_cb->s = s;
149 write_header_cb->nsectors = nsectors;
150 write_header_cb->buf = qemu_blockalign(s->bs, len);
151 write_header_cb->iov.iov_base = write_header_cb->buf;
152 write_header_cb->iov.iov_len = len;
153 qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1);
155 bdrv_aio_readv(s->bs->file->bs, 0, &write_header_cb->qiov, nsectors,
156 qed_write_header_read_cb, write_header_cb);
159 static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
161 uint64_t table_entries;
162 uint64_t l2_size;
164 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
165 l2_size = table_entries * cluster_size;
167 return l2_size * table_entries;
170 static bool qed_is_cluster_size_valid(uint32_t cluster_size)
172 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
173 cluster_size > QED_MAX_CLUSTER_SIZE) {
174 return false;
176 if (cluster_size & (cluster_size - 1)) {
177 return false; /* not power of 2 */
179 return true;
182 static bool qed_is_table_size_valid(uint32_t table_size)
184 if (table_size < QED_MIN_TABLE_SIZE ||
185 table_size > QED_MAX_TABLE_SIZE) {
186 return false;
188 if (table_size & (table_size - 1)) {
189 return false; /* not power of 2 */
191 return true;
194 static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
195 uint32_t table_size)
197 if (image_size % BDRV_SECTOR_SIZE != 0) {
198 return false; /* not multiple of sector size */
200 if (image_size > qed_max_image_size(cluster_size, table_size)) {
201 return false; /* image is too large */
203 return true;
207 * Read a string of known length from the image file
209 * @file: Image file
210 * @offset: File offset to start of string, in bytes
211 * @n: String length in bytes
212 * @buf: Destination buffer
213 * @buflen: Destination buffer length in bytes
214 * @ret: 0 on success, -errno on failure
216 * The string is NUL-terminated.
218 static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n,
219 char *buf, size_t buflen)
221 int ret;
222 if (n >= buflen) {
223 return -EINVAL;
225 ret = bdrv_pread(file, offset, buf, n);
226 if (ret < 0) {
227 return ret;
229 buf[n] = '\0';
230 return 0;
234 * Allocate new clusters
236 * @s: QED state
237 * @n: Number of contiguous clusters to allocate
238 * @ret: Offset of first allocated cluster
240 * This function only produces the offset where the new clusters should be
241 * written. It updates BDRVQEDState but does not make any changes to the image
242 * file.
244 static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
246 uint64_t offset = s->file_size;
247 s->file_size += n * s->header.cluster_size;
248 return offset;
251 QEDTable *qed_alloc_table(BDRVQEDState *s)
253 /* Honor O_DIRECT memory alignment requirements */
254 return qemu_blockalign(s->bs,
255 s->header.cluster_size * s->header.table_size);
259 * Allocate a new zeroed L2 table
261 static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
263 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
265 l2_table->table = qed_alloc_table(s);
266 l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
268 memset(l2_table->table->offsets, 0,
269 s->header.cluster_size * s->header.table_size);
270 return l2_table;
273 static void qed_aio_next_io(void *opaque, int ret);
275 static void qed_plug_allocating_write_reqs(BDRVQEDState *s)
277 assert(!s->allocating_write_reqs_plugged);
279 s->allocating_write_reqs_plugged = true;
282 static void qed_unplug_allocating_write_reqs(BDRVQEDState *s)
284 QEDAIOCB *acb;
286 assert(s->allocating_write_reqs_plugged);
288 s->allocating_write_reqs_plugged = false;
290 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
291 if (acb) {
292 qed_aio_next_io(acb, 0);
296 static void qed_finish_clear_need_check(void *opaque, int ret)
298 /* Do nothing */
301 static void qed_flush_after_clear_need_check(void *opaque, int ret)
303 BDRVQEDState *s = opaque;
305 bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s);
307 /* No need to wait until flush completes */
308 qed_unplug_allocating_write_reqs(s);
311 static void qed_clear_need_check(void *opaque, int ret)
313 BDRVQEDState *s = opaque;
315 if (ret) {
316 qed_unplug_allocating_write_reqs(s);
317 return;
320 s->header.features &= ~QED_F_NEED_CHECK;
321 qed_write_header(s, qed_flush_after_clear_need_check, s);
324 static void qed_need_check_timer_cb(void *opaque)
326 BDRVQEDState *s = opaque;
328 /* The timer should only fire when allocating writes have drained */
329 assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs));
331 trace_qed_need_check_timer_cb(s);
333 qed_plug_allocating_write_reqs(s);
335 /* Ensure writes are on disk before clearing flag */
336 bdrv_aio_flush(s->bs, qed_clear_need_check, s);
339 static void qed_start_need_check_timer(BDRVQEDState *s)
341 trace_qed_start_need_check_timer(s);
343 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
344 * migration.
346 timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
347 get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT);
350 /* It's okay to call this multiple times or when no timer is started */
351 static void qed_cancel_need_check_timer(BDRVQEDState *s)
353 trace_qed_cancel_need_check_timer(s);
354 timer_del(s->need_check_timer);
357 static void bdrv_qed_detach_aio_context(BlockDriverState *bs)
359 BDRVQEDState *s = bs->opaque;
361 qed_cancel_need_check_timer(s);
362 timer_free(s->need_check_timer);
365 static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
366 AioContext *new_context)
368 BDRVQEDState *s = bs->opaque;
370 s->need_check_timer = aio_timer_new(new_context,
371 QEMU_CLOCK_VIRTUAL, SCALE_NS,
372 qed_need_check_timer_cb, s);
373 if (s->header.features & QED_F_NEED_CHECK) {
374 qed_start_need_check_timer(s);
378 static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
379 Error **errp)
381 BDRVQEDState *s = bs->opaque;
382 QEDHeader le_header;
383 int64_t file_size;
384 int ret;
386 s->bs = bs;
387 QSIMPLEQ_INIT(&s->allocating_write_reqs);
389 ret = bdrv_pread(bs->file->bs, 0, &le_header, sizeof(le_header));
390 if (ret < 0) {
391 return ret;
393 qed_header_le_to_cpu(&le_header, &s->header);
395 if (s->header.magic != QED_MAGIC) {
396 error_setg(errp, "Image not in QED format");
397 return -EINVAL;
399 if (s->header.features & ~QED_FEATURE_MASK) {
400 /* image uses unsupported feature bits */
401 char buf[64];
402 snprintf(buf, sizeof(buf), "%" PRIx64,
403 s->header.features & ~QED_FEATURE_MASK);
404 error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
405 bdrv_get_device_or_node_name(bs), "QED", buf);
406 return -ENOTSUP;
408 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
409 return -EINVAL;
412 /* Round down file size to the last cluster */
413 file_size = bdrv_getlength(bs->file->bs);
414 if (file_size < 0) {
415 return file_size;
417 s->file_size = qed_start_of_cluster(s, file_size);
419 if (!qed_is_table_size_valid(s->header.table_size)) {
420 return -EINVAL;
422 if (!qed_is_image_size_valid(s->header.image_size,
423 s->header.cluster_size,
424 s->header.table_size)) {
425 return -EINVAL;
427 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
428 return -EINVAL;
431 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
432 sizeof(uint64_t);
433 s->l2_shift = ctz32(s->header.cluster_size);
434 s->l2_mask = s->table_nelems - 1;
435 s->l1_shift = s->l2_shift + ctz32(s->table_nelems);
437 /* Header size calculation must not overflow uint32_t */
438 if (s->header.header_size > UINT32_MAX / s->header.cluster_size) {
439 return -EINVAL;
442 if ((s->header.features & QED_F_BACKING_FILE)) {
443 if ((uint64_t)s->header.backing_filename_offset +
444 s->header.backing_filename_size >
445 s->header.cluster_size * s->header.header_size) {
446 return -EINVAL;
449 ret = qed_read_string(bs->file->bs, s->header.backing_filename_offset,
450 s->header.backing_filename_size, bs->backing_file,
451 sizeof(bs->backing_file));
452 if (ret < 0) {
453 return ret;
456 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
457 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
461 /* Reset unknown autoclear feature bits. This is a backwards
462 * compatibility mechanism that allows images to be opened by older
463 * programs, which "knock out" unknown feature bits. When an image is
464 * opened by a newer program again it can detect that the autoclear
465 * feature is no longer valid.
467 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
468 !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INCOMING)) {
469 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
471 ret = qed_write_header_sync(s);
472 if (ret) {
473 return ret;
476 /* From here on only known autoclear feature bits are valid */
477 bdrv_flush(bs->file->bs);
480 s->l1_table = qed_alloc_table(s);
481 qed_init_l2_cache(&s->l2_cache);
483 ret = qed_read_l1_table_sync(s);
484 if (ret) {
485 goto out;
488 /* If image was not closed cleanly, check consistency */
489 if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
490 /* Read-only images cannot be fixed. There is no risk of corruption
491 * since write operations are not possible. Therefore, allow
492 * potentially inconsistent images to be opened read-only. This can
493 * aid data recovery from an otherwise inconsistent image.
495 if (!bdrv_is_read_only(bs->file->bs) &&
496 !(flags & BDRV_O_INCOMING)) {
497 BdrvCheckResult result = {0};
499 ret = qed_check(s, &result, true);
500 if (ret) {
501 goto out;
506 bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
508 out:
509 if (ret) {
510 qed_free_l2_cache(&s->l2_cache);
511 qemu_vfree(s->l1_table);
513 return ret;
516 static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
518 BDRVQEDState *s = bs->opaque;
520 bs->bl.write_zeroes_alignment = s->header.cluster_size >> BDRV_SECTOR_BITS;
523 /* We have nothing to do for QED reopen, stubs just return
524 * success */
525 static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
526 BlockReopenQueue *queue, Error **errp)
528 return 0;
531 static void bdrv_qed_close(BlockDriverState *bs)
533 BDRVQEDState *s = bs->opaque;
535 bdrv_qed_detach_aio_context(bs);
537 /* Ensure writes reach stable storage */
538 bdrv_flush(bs->file->bs);
540 /* Clean shutdown, no check required on next open */
541 if (s->header.features & QED_F_NEED_CHECK) {
542 s->header.features &= ~QED_F_NEED_CHECK;
543 qed_write_header_sync(s);
546 qed_free_l2_cache(&s->l2_cache);
547 qemu_vfree(s->l1_table);
550 static int qed_create(const char *filename, uint32_t cluster_size,
551 uint64_t image_size, uint32_t table_size,
552 const char *backing_file, const char *backing_fmt,
553 QemuOpts *opts, Error **errp)
555 QEDHeader header = {
556 .magic = QED_MAGIC,
557 .cluster_size = cluster_size,
558 .table_size = table_size,
559 .header_size = 1,
560 .features = 0,
561 .compat_features = 0,
562 .l1_table_offset = cluster_size,
563 .image_size = image_size,
565 QEDHeader le_header;
566 uint8_t *l1_table = NULL;
567 size_t l1_size = header.cluster_size * header.table_size;
568 Error *local_err = NULL;
569 int ret = 0;
570 BlockDriverState *bs;
572 ret = bdrv_create_file(filename, opts, &local_err);
573 if (ret < 0) {
574 error_propagate(errp, local_err);
575 return ret;
578 bs = NULL;
579 ret = bdrv_open(&bs, filename, NULL, NULL,
580 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
581 &local_err);
582 if (ret < 0) {
583 error_propagate(errp, local_err);
584 return ret;
587 /* File must start empty and grow, check truncate is supported */
588 ret = bdrv_truncate(bs, 0);
589 if (ret < 0) {
590 goto out;
593 if (backing_file) {
594 header.features |= QED_F_BACKING_FILE;
595 header.backing_filename_offset = sizeof(le_header);
596 header.backing_filename_size = strlen(backing_file);
598 if (qed_fmt_is_raw(backing_fmt)) {
599 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
603 qed_header_cpu_to_le(&header, &le_header);
604 ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header));
605 if (ret < 0) {
606 goto out;
608 ret = bdrv_pwrite(bs, sizeof(le_header), backing_file,
609 header.backing_filename_size);
610 if (ret < 0) {
611 goto out;
614 l1_table = g_malloc0(l1_size);
615 ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size);
616 if (ret < 0) {
617 goto out;
620 ret = 0; /* success */
621 out:
622 g_free(l1_table);
623 bdrv_unref(bs);
624 return ret;
627 static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp)
629 uint64_t image_size = 0;
630 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
631 uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
632 char *backing_file = NULL;
633 char *backing_fmt = NULL;
634 int ret;
636 image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
637 BDRV_SECTOR_SIZE);
638 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
639 backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
640 cluster_size = qemu_opt_get_size_del(opts,
641 BLOCK_OPT_CLUSTER_SIZE,
642 QED_DEFAULT_CLUSTER_SIZE);
643 table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE,
644 QED_DEFAULT_TABLE_SIZE);
646 if (!qed_is_cluster_size_valid(cluster_size)) {
647 error_setg(errp, "QED cluster size must be within range [%u, %u] "
648 "and power of 2",
649 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
650 ret = -EINVAL;
651 goto finish;
653 if (!qed_is_table_size_valid(table_size)) {
654 error_setg(errp, "QED table size must be within range [%u, %u] "
655 "and power of 2",
656 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
657 ret = -EINVAL;
658 goto finish;
660 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
661 error_setg(errp, "QED image size must be a non-zero multiple of "
662 "cluster size and less than %" PRIu64 " bytes",
663 qed_max_image_size(cluster_size, table_size));
664 ret = -EINVAL;
665 goto finish;
668 ret = qed_create(filename, cluster_size, image_size, table_size,
669 backing_file, backing_fmt, opts, errp);
671 finish:
672 g_free(backing_file);
673 g_free(backing_fmt);
674 return ret;
677 typedef struct {
678 BlockDriverState *bs;
679 Coroutine *co;
680 uint64_t pos;
681 int64_t status;
682 int *pnum;
683 } QEDIsAllocatedCB;
685 static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
687 QEDIsAllocatedCB *cb = opaque;
688 BDRVQEDState *s = cb->bs->opaque;
689 *cb->pnum = len / BDRV_SECTOR_SIZE;
690 switch (ret) {
691 case QED_CLUSTER_FOUND:
692 offset |= qed_offset_into_cluster(s, cb->pos);
693 cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
694 break;
695 case QED_CLUSTER_ZERO:
696 cb->status = BDRV_BLOCK_ZERO;
697 break;
698 case QED_CLUSTER_L2:
699 case QED_CLUSTER_L1:
700 cb->status = 0;
701 break;
702 default:
703 assert(ret < 0);
704 cb->status = ret;
705 break;
708 if (cb->co) {
709 qemu_coroutine_enter(cb->co, NULL);
713 static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs,
714 int64_t sector_num,
715 int nb_sectors, int *pnum)
717 BDRVQEDState *s = bs->opaque;
718 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
719 QEDIsAllocatedCB cb = {
720 .bs = bs,
721 .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE,
722 .status = BDRV_BLOCK_OFFSET_MASK,
723 .pnum = pnum,
725 QEDRequest request = { .l2_table = NULL };
727 qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb);
729 /* Now sleep if the callback wasn't invoked immediately */
730 while (cb.status == BDRV_BLOCK_OFFSET_MASK) {
731 cb.co = qemu_coroutine_self();
732 qemu_coroutine_yield();
735 qed_unref_l2_cache_entry(request.l2_table);
737 return cb.status;
740 static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
742 return acb->common.bs->opaque;
746 * Read from the backing file or zero-fill if no backing file
748 * @s: QED state
749 * @pos: Byte position in device
750 * @qiov: Destination I/O vector
751 * @backing_qiov: Possibly shortened copy of qiov, to be allocated here
752 * @cb: Completion function
753 * @opaque: User data for completion function
755 * This function reads qiov->size bytes starting at pos from the backing file.
756 * If there is no backing file then zeroes are read.
758 static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
759 QEMUIOVector *qiov,
760 QEMUIOVector **backing_qiov,
761 BlockCompletionFunc *cb, void *opaque)
763 uint64_t backing_length = 0;
764 size_t size;
766 /* If there is a backing file, get its length. Treat the absence of a
767 * backing file like a zero length backing file.
769 if (s->bs->backing) {
770 int64_t l = bdrv_getlength(s->bs->backing->bs);
771 if (l < 0) {
772 cb(opaque, l);
773 return;
775 backing_length = l;
778 /* Zero all sectors if reading beyond the end of the backing file */
779 if (pos >= backing_length ||
780 pos + qiov->size > backing_length) {
781 qemu_iovec_memset(qiov, 0, 0, qiov->size);
784 /* Complete now if there are no backing file sectors to read */
785 if (pos >= backing_length) {
786 cb(opaque, 0);
787 return;
790 /* If the read straddles the end of the backing file, shorten it */
791 size = MIN((uint64_t)backing_length - pos, qiov->size);
793 assert(*backing_qiov == NULL);
794 *backing_qiov = g_new(QEMUIOVector, 1);
795 qemu_iovec_init(*backing_qiov, qiov->niov);
796 qemu_iovec_concat(*backing_qiov, qiov, 0, size);
798 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
799 bdrv_aio_readv(s->bs->backing->bs, pos / BDRV_SECTOR_SIZE,
800 *backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
803 typedef struct {
804 GenericCB gencb;
805 BDRVQEDState *s;
806 QEMUIOVector qiov;
807 QEMUIOVector *backing_qiov;
808 struct iovec iov;
809 uint64_t offset;
810 } CopyFromBackingFileCB;
812 static void qed_copy_from_backing_file_cb(void *opaque, int ret)
814 CopyFromBackingFileCB *copy_cb = opaque;
815 qemu_vfree(copy_cb->iov.iov_base);
816 gencb_complete(&copy_cb->gencb, ret);
819 static void qed_copy_from_backing_file_write(void *opaque, int ret)
821 CopyFromBackingFileCB *copy_cb = opaque;
822 BDRVQEDState *s = copy_cb->s;
824 if (copy_cb->backing_qiov) {
825 qemu_iovec_destroy(copy_cb->backing_qiov);
826 g_free(copy_cb->backing_qiov);
827 copy_cb->backing_qiov = NULL;
830 if (ret) {
831 qed_copy_from_backing_file_cb(copy_cb, ret);
832 return;
835 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
836 bdrv_aio_writev(s->bs->file->bs, copy_cb->offset / BDRV_SECTOR_SIZE,
837 &copy_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE,
838 qed_copy_from_backing_file_cb, copy_cb);
842 * Copy data from backing file into the image
844 * @s: QED state
845 * @pos: Byte position in device
846 * @len: Number of bytes
847 * @offset: Byte offset in image file
848 * @cb: Completion function
849 * @opaque: User data for completion function
851 static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
852 uint64_t len, uint64_t offset,
853 BlockCompletionFunc *cb,
854 void *opaque)
856 CopyFromBackingFileCB *copy_cb;
858 /* Skip copy entirely if there is no work to do */
859 if (len == 0) {
860 cb(opaque, 0);
861 return;
864 copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
865 copy_cb->s = s;
866 copy_cb->offset = offset;
867 copy_cb->backing_qiov = NULL;
868 copy_cb->iov.iov_base = qemu_blockalign(s->bs, len);
869 copy_cb->iov.iov_len = len;
870 qemu_iovec_init_external(&copy_cb->qiov, &copy_cb->iov, 1);
872 qed_read_backing_file(s, pos, &copy_cb->qiov, &copy_cb->backing_qiov,
873 qed_copy_from_backing_file_write, copy_cb);
877 * Link one or more contiguous clusters into a table
879 * @s: QED state
880 * @table: L2 table
881 * @index: First cluster index
882 * @n: Number of contiguous clusters
883 * @cluster: First cluster offset
885 * The cluster offset may be an allocated byte offset in the image file, the
886 * zero cluster marker, or the unallocated cluster marker.
888 static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
889 unsigned int n, uint64_t cluster)
891 int i;
892 for (i = index; i < index + n; i++) {
893 table->offsets[i] = cluster;
894 if (!qed_offset_is_unalloc_cluster(cluster) &&
895 !qed_offset_is_zero_cluster(cluster)) {
896 cluster += s->header.cluster_size;
901 static void qed_aio_complete_bh(void *opaque)
903 QEDAIOCB *acb = opaque;
904 BlockCompletionFunc *cb = acb->common.cb;
905 void *user_opaque = acb->common.opaque;
906 int ret = acb->bh_ret;
908 qemu_bh_delete(acb->bh);
909 qemu_aio_unref(acb);
911 /* Invoke callback */
912 cb(user_opaque, ret);
915 static void qed_aio_complete(QEDAIOCB *acb, int ret)
917 BDRVQEDState *s = acb_to_s(acb);
919 trace_qed_aio_complete(s, acb, ret);
921 /* Free resources */
922 qemu_iovec_destroy(&acb->cur_qiov);
923 qed_unref_l2_cache_entry(acb->request.l2_table);
925 /* Free the buffer we may have allocated for zero writes */
926 if (acb->flags & QED_AIOCB_ZERO) {
927 qemu_vfree(acb->qiov->iov[0].iov_base);
928 acb->qiov->iov[0].iov_base = NULL;
931 /* Arrange for a bh to invoke the completion function */
932 acb->bh_ret = ret;
933 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
934 qed_aio_complete_bh, acb);
935 qemu_bh_schedule(acb->bh);
937 /* Start next allocating write request waiting behind this one. Note that
938 * requests enqueue themselves when they first hit an unallocated cluster
939 * but they wait until the entire request is finished before waking up the
940 * next request in the queue. This ensures that we don't cycle through
941 * requests multiple times but rather finish one at a time completely.
943 if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
944 QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
945 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
946 if (acb) {
947 qed_aio_next_io(acb, 0);
948 } else if (s->header.features & QED_F_NEED_CHECK) {
949 qed_start_need_check_timer(s);
955 * Commit the current L2 table to the cache
957 static void qed_commit_l2_update(void *opaque, int ret)
959 QEDAIOCB *acb = opaque;
960 BDRVQEDState *s = acb_to_s(acb);
961 CachedL2Table *l2_table = acb->request.l2_table;
962 uint64_t l2_offset = l2_table->offset;
964 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
966 /* This is guaranteed to succeed because we just committed the entry to the
967 * cache.
969 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
970 assert(acb->request.l2_table != NULL);
972 qed_aio_next_io(opaque, ret);
976 * Update L1 table with new L2 table offset and write it out
978 static void qed_aio_write_l1_update(void *opaque, int ret)
980 QEDAIOCB *acb = opaque;
981 BDRVQEDState *s = acb_to_s(acb);
982 int index;
984 if (ret) {
985 qed_aio_complete(acb, ret);
986 return;
989 index = qed_l1_index(s, acb->cur_pos);
990 s->l1_table->offsets[index] = acb->request.l2_table->offset;
992 qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
996 * Update L2 table with new cluster offsets and write them out
998 static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset)
1000 BDRVQEDState *s = acb_to_s(acb);
1001 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
1002 int index;
1004 if (ret) {
1005 goto err;
1008 if (need_alloc) {
1009 qed_unref_l2_cache_entry(acb->request.l2_table);
1010 acb->request.l2_table = qed_new_l2_table(s);
1013 index = qed_l2_index(s, acb->cur_pos);
1014 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
1015 offset);
1017 if (need_alloc) {
1018 /* Write out the whole new L2 table */
1019 qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true,
1020 qed_aio_write_l1_update, acb);
1021 } else {
1022 /* Write out only the updated part of the L2 table */
1023 qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false,
1024 qed_aio_next_io, acb);
1026 return;
1028 err:
1029 qed_aio_complete(acb, ret);
1032 static void qed_aio_write_l2_update_cb(void *opaque, int ret)
1034 QEDAIOCB *acb = opaque;
1035 qed_aio_write_l2_update(acb, ret, acb->cur_cluster);
1039 * Flush new data clusters before updating the L2 table
1041 * This flush is necessary when a backing file is in use. A crash during an
1042 * allocating write could result in empty clusters in the image. If the write
1043 * only touched a subregion of the cluster, then backing image sectors have
1044 * been lost in the untouched region. The solution is to flush after writing a
1045 * new data cluster and before updating the L2 table.
1047 static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
1049 QEDAIOCB *acb = opaque;
1050 BDRVQEDState *s = acb_to_s(acb);
1052 if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) {
1053 qed_aio_complete(acb, -EIO);
1058 * Write data to the image file
1060 static void qed_aio_write_main(void *opaque, int ret)
1062 QEDAIOCB *acb = opaque;
1063 BDRVQEDState *s = acb_to_s(acb);
1064 uint64_t offset = acb->cur_cluster +
1065 qed_offset_into_cluster(s, acb->cur_pos);
1066 BlockCompletionFunc *next_fn;
1068 trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
1070 if (ret) {
1071 qed_aio_complete(acb, ret);
1072 return;
1075 if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
1076 next_fn = qed_aio_next_io;
1077 } else {
1078 if (s->bs->backing) {
1079 next_fn = qed_aio_write_flush_before_l2_update;
1080 } else {
1081 next_fn = qed_aio_write_l2_update_cb;
1085 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
1086 bdrv_aio_writev(s->bs->file->bs, offset / BDRV_SECTOR_SIZE,
1087 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1088 next_fn, acb);
1092 * Populate back untouched region of new data cluster
1094 static void qed_aio_write_postfill(void *opaque, int ret)
1096 QEDAIOCB *acb = opaque;
1097 BDRVQEDState *s = acb_to_s(acb);
1098 uint64_t start = acb->cur_pos + acb->cur_qiov.size;
1099 uint64_t len =
1100 qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1101 uint64_t offset = acb->cur_cluster +
1102 qed_offset_into_cluster(s, acb->cur_pos) +
1103 acb->cur_qiov.size;
1105 if (ret) {
1106 qed_aio_complete(acb, ret);
1107 return;
1110 trace_qed_aio_write_postfill(s, acb, start, len, offset);
1111 qed_copy_from_backing_file(s, start, len, offset,
1112 qed_aio_write_main, acb);
1116 * Populate front untouched region of new data cluster
1118 static void qed_aio_write_prefill(void *opaque, int ret)
1120 QEDAIOCB *acb = opaque;
1121 BDRVQEDState *s = acb_to_s(acb);
1122 uint64_t start = qed_start_of_cluster(s, acb->cur_pos);
1123 uint64_t len = qed_offset_into_cluster(s, acb->cur_pos);
1125 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1126 qed_copy_from_backing_file(s, start, len, acb->cur_cluster,
1127 qed_aio_write_postfill, acb);
1131 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1133 static bool qed_should_set_need_check(BDRVQEDState *s)
1135 /* The flush before L2 update path ensures consistency */
1136 if (s->bs->backing) {
1137 return false;
1140 return !(s->header.features & QED_F_NEED_CHECK);
1143 static void qed_aio_write_zero_cluster(void *opaque, int ret)
1145 QEDAIOCB *acb = opaque;
1147 if (ret) {
1148 qed_aio_complete(acb, ret);
1149 return;
1152 qed_aio_write_l2_update(acb, 0, 1);
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 static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1165 BDRVQEDState *s = acb_to_s(acb);
1166 BlockCompletionFunc *cb;
1168 /* Cancel timer when the first allocating request comes in */
1169 if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) {
1170 qed_cancel_need_check_timer(s);
1173 /* Freeze this request if another allocating write is in progress */
1174 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
1175 QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
1177 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) ||
1178 s->allocating_write_reqs_plugged) {
1179 return; /* wait for existing request to finish */
1182 acb->cur_nclusters = qed_bytes_to_clusters(s,
1183 qed_offset_into_cluster(s, acb->cur_pos) + len);
1184 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1186 if (acb->flags & QED_AIOCB_ZERO) {
1187 /* Skip ahead if the clusters are already zero */
1188 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
1189 qed_aio_next_io(acb, 0);
1190 return;
1193 cb = qed_aio_write_zero_cluster;
1194 } else {
1195 cb = qed_aio_write_prefill;
1196 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1199 if (qed_should_set_need_check(s)) {
1200 s->header.features |= QED_F_NEED_CHECK;
1201 qed_write_header(s, cb, acb);
1202 } else {
1203 cb(acb, 0);
1208 * Write data cluster in place
1210 * @acb: Write request
1211 * @offset: Cluster offset in bytes
1212 * @len: Length in bytes
1214 * This path is taken when writing to already allocated clusters.
1216 static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1218 /* Allocate buffer for zero writes */
1219 if (acb->flags & QED_AIOCB_ZERO) {
1220 struct iovec *iov = acb->qiov->iov;
1222 if (!iov->iov_base) {
1223 iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len);
1224 if (iov->iov_base == NULL) {
1225 qed_aio_complete(acb, -ENOMEM);
1226 return;
1228 memset(iov->iov_base, 0, iov->iov_len);
1232 /* Calculate the I/O vector */
1233 acb->cur_cluster = offset;
1234 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1236 /* Do the actual write */
1237 qed_aio_write_main(acb, 0);
1241 * Write data cluster
1243 * @opaque: Write request
1244 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1245 * or -errno
1246 * @offset: Cluster offset in bytes
1247 * @len: Length in bytes
1249 * Callback from qed_find_cluster().
1251 static void qed_aio_write_data(void *opaque, int ret,
1252 uint64_t offset, size_t len)
1254 QEDAIOCB *acb = opaque;
1256 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1258 acb->find_cluster_ret = ret;
1260 switch (ret) {
1261 case QED_CLUSTER_FOUND:
1262 qed_aio_write_inplace(acb, offset, len);
1263 break;
1265 case QED_CLUSTER_L2:
1266 case QED_CLUSTER_L1:
1267 case QED_CLUSTER_ZERO:
1268 qed_aio_write_alloc(acb, len);
1269 break;
1271 default:
1272 qed_aio_complete(acb, ret);
1273 break;
1278 * Read data cluster
1280 * @opaque: Read request
1281 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1282 * or -errno
1283 * @offset: Cluster offset in bytes
1284 * @len: Length in bytes
1286 * Callback from qed_find_cluster().
1288 static void qed_aio_read_data(void *opaque, int ret,
1289 uint64_t offset, size_t len)
1291 QEDAIOCB *acb = opaque;
1292 BDRVQEDState *s = acb_to_s(acb);
1293 BlockDriverState *bs = acb->common.bs;
1295 /* Adjust offset into cluster */
1296 offset += qed_offset_into_cluster(s, acb->cur_pos);
1298 trace_qed_aio_read_data(s, acb, ret, offset, len);
1300 if (ret < 0) {
1301 goto err;
1304 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1306 /* Handle zero cluster and backing file reads */
1307 if (ret == QED_CLUSTER_ZERO) {
1308 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
1309 qed_aio_next_io(acb, 0);
1310 return;
1311 } else if (ret != QED_CLUSTER_FOUND) {
1312 qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
1313 &acb->backing_qiov, qed_aio_next_io, acb);
1314 return;
1317 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1318 bdrv_aio_readv(bs->file->bs, offset / BDRV_SECTOR_SIZE,
1319 &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1320 qed_aio_next_io, acb);
1321 return;
1323 err:
1324 qed_aio_complete(acb, ret);
1328 * Begin next I/O or complete the request
1330 static void qed_aio_next_io(void *opaque, int ret)
1332 QEDAIOCB *acb = opaque;
1333 BDRVQEDState *s = acb_to_s(acb);
1334 QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ?
1335 qed_aio_write_data : qed_aio_read_data;
1337 trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
1339 if (acb->backing_qiov) {
1340 qemu_iovec_destroy(acb->backing_qiov);
1341 g_free(acb->backing_qiov);
1342 acb->backing_qiov = NULL;
1345 /* Handle I/O error */
1346 if (ret) {
1347 qed_aio_complete(acb, ret);
1348 return;
1351 acb->qiov_offset += acb->cur_qiov.size;
1352 acb->cur_pos += acb->cur_qiov.size;
1353 qemu_iovec_reset(&acb->cur_qiov);
1355 /* Complete request */
1356 if (acb->cur_pos >= acb->end_pos) {
1357 qed_aio_complete(acb, 0);
1358 return;
1361 /* Find next cluster and start I/O */
1362 qed_find_cluster(s, &acb->request,
1363 acb->cur_pos, acb->end_pos - acb->cur_pos,
1364 io_fn, acb);
1367 static BlockAIOCB *qed_aio_setup(BlockDriverState *bs,
1368 int64_t sector_num,
1369 QEMUIOVector *qiov, int nb_sectors,
1370 BlockCompletionFunc *cb,
1371 void *opaque, int flags)
1373 QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque);
1375 trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
1376 opaque, flags);
1378 acb->flags = flags;
1379 acb->qiov = qiov;
1380 acb->qiov_offset = 0;
1381 acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
1382 acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
1383 acb->backing_qiov = NULL;
1384 acb->request.l2_table = NULL;
1385 qemu_iovec_init(&acb->cur_qiov, qiov->niov);
1387 /* Start request */
1388 qed_aio_next_io(acb, 0);
1389 return &acb->common;
1392 static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
1393 int64_t sector_num,
1394 QEMUIOVector *qiov, int nb_sectors,
1395 BlockCompletionFunc *cb,
1396 void *opaque)
1398 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1401 static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
1402 int64_t sector_num,
1403 QEMUIOVector *qiov, int nb_sectors,
1404 BlockCompletionFunc *cb,
1405 void *opaque)
1407 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb,
1408 opaque, QED_AIOCB_WRITE);
1411 typedef struct {
1412 Coroutine *co;
1413 int ret;
1414 bool done;
1415 } QEDWriteZeroesCB;
1417 static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret)
1419 QEDWriteZeroesCB *cb = opaque;
1421 cb->done = true;
1422 cb->ret = ret;
1423 if (cb->co) {
1424 qemu_coroutine_enter(cb->co, NULL);
1428 static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs,
1429 int64_t sector_num,
1430 int nb_sectors,
1431 BdrvRequestFlags flags)
1433 BlockAIOCB *blockacb;
1434 BDRVQEDState *s = bs->opaque;
1435 QEDWriteZeroesCB cb = { .done = false };
1436 QEMUIOVector qiov;
1437 struct iovec iov;
1439 /* Refuse if there are untouched backing file sectors */
1440 if (bs->backing) {
1441 if (qed_offset_into_cluster(s, sector_num * BDRV_SECTOR_SIZE) != 0) {
1442 return -ENOTSUP;
1444 if (qed_offset_into_cluster(s, nb_sectors * BDRV_SECTOR_SIZE) != 0) {
1445 return -ENOTSUP;
1449 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1450 * then it will be allocated during request processing.
1452 iov.iov_base = NULL,
1453 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1455 qemu_iovec_init_external(&qiov, &iov, 1);
1456 blockacb = qed_aio_setup(bs, sector_num, &qiov, nb_sectors,
1457 qed_co_write_zeroes_cb, &cb,
1458 QED_AIOCB_WRITE | QED_AIOCB_ZERO);
1459 if (!blockacb) {
1460 return -EIO;
1462 if (!cb.done) {
1463 cb.co = qemu_coroutine_self();
1464 qemu_coroutine_yield();
1466 assert(cb.done);
1467 return cb.ret;
1470 static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset)
1472 BDRVQEDState *s = bs->opaque;
1473 uint64_t old_image_size;
1474 int ret;
1476 if (!qed_is_image_size_valid(offset, s->header.cluster_size,
1477 s->header.table_size)) {
1478 return -EINVAL;
1481 /* Shrinking is currently not supported */
1482 if ((uint64_t)offset < s->header.image_size) {
1483 return -ENOTSUP;
1486 old_image_size = s->header.image_size;
1487 s->header.image_size = offset;
1488 ret = qed_write_header_sync(s);
1489 if (ret < 0) {
1490 s->header.image_size = old_image_size;
1492 return ret;
1495 static int64_t bdrv_qed_getlength(BlockDriverState *bs)
1497 BDRVQEDState *s = bs->opaque;
1498 return s->header.image_size;
1501 static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1503 BDRVQEDState *s = bs->opaque;
1505 memset(bdi, 0, sizeof(*bdi));
1506 bdi->cluster_size = s->header.cluster_size;
1507 bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
1508 bdi->unallocated_blocks_are_zero = true;
1509 bdi->can_write_zeroes_with_unmap = true;
1510 return 0;
1513 static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1514 const char *backing_file,
1515 const char *backing_fmt)
1517 BDRVQEDState *s = bs->opaque;
1518 QEDHeader new_header, le_header;
1519 void *buffer;
1520 size_t buffer_len, backing_file_len;
1521 int ret;
1523 /* Refuse to set backing filename if unknown compat feature bits are
1524 * active. If the image uses an unknown compat feature then we may not
1525 * know the layout of data following the header structure and cannot safely
1526 * add a new string.
1528 if (backing_file && (s->header.compat_features &
1529 ~QED_COMPAT_FEATURE_MASK)) {
1530 return -ENOTSUP;
1533 memcpy(&new_header, &s->header, sizeof(new_header));
1535 new_header.features &= ~(QED_F_BACKING_FILE |
1536 QED_F_BACKING_FORMAT_NO_PROBE);
1538 /* Adjust feature flags */
1539 if (backing_file) {
1540 new_header.features |= QED_F_BACKING_FILE;
1542 if (qed_fmt_is_raw(backing_fmt)) {
1543 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1547 /* Calculate new header size */
1548 backing_file_len = 0;
1550 if (backing_file) {
1551 backing_file_len = strlen(backing_file);
1554 buffer_len = sizeof(new_header);
1555 new_header.backing_filename_offset = buffer_len;
1556 new_header.backing_filename_size = backing_file_len;
1557 buffer_len += backing_file_len;
1559 /* Make sure we can rewrite header without failing */
1560 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1561 return -ENOSPC;
1564 /* Prepare new header */
1565 buffer = g_malloc(buffer_len);
1567 qed_header_cpu_to_le(&new_header, &le_header);
1568 memcpy(buffer, &le_header, sizeof(le_header));
1569 buffer_len = sizeof(le_header);
1571 if (backing_file) {
1572 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1573 buffer_len += backing_file_len;
1576 /* Write new header */
1577 ret = bdrv_pwrite_sync(bs->file->bs, 0, buffer, buffer_len);
1578 g_free(buffer);
1579 if (ret == 0) {
1580 memcpy(&s->header, &new_header, sizeof(new_header));
1582 return ret;
1585 static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp)
1587 BDRVQEDState *s = bs->opaque;
1588 Error *local_err = NULL;
1589 int ret;
1591 bdrv_qed_close(bs);
1593 bdrv_invalidate_cache(bs->file->bs, &local_err);
1594 if (local_err) {
1595 error_propagate(errp, local_err);
1596 return;
1599 memset(s, 0, sizeof(BDRVQEDState));
1600 ret = bdrv_qed_open(bs, NULL, bs->open_flags, &local_err);
1601 if (local_err) {
1602 error_setg(errp, "Could not reopen qed layer: %s",
1603 error_get_pretty(local_err));
1604 error_free(local_err);
1605 return;
1606 } else if (ret < 0) {
1607 error_setg_errno(errp, -ret, "Could not reopen qed layer");
1608 return;
1612 static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result,
1613 BdrvCheckMode fix)
1615 BDRVQEDState *s = bs->opaque;
1617 return qed_check(s, result, !!fix);
1620 static QemuOptsList qed_create_opts = {
1621 .name = "qed-create-opts",
1622 .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
1623 .desc = {
1625 .name = BLOCK_OPT_SIZE,
1626 .type = QEMU_OPT_SIZE,
1627 .help = "Virtual disk size"
1630 .name = BLOCK_OPT_BACKING_FILE,
1631 .type = QEMU_OPT_STRING,
1632 .help = "File name of a base image"
1635 .name = BLOCK_OPT_BACKING_FMT,
1636 .type = QEMU_OPT_STRING,
1637 .help = "Image format of the base image"
1640 .name = BLOCK_OPT_CLUSTER_SIZE,
1641 .type = QEMU_OPT_SIZE,
1642 .help = "Cluster size (in bytes)",
1643 .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE)
1646 .name = BLOCK_OPT_TABLE_SIZE,
1647 .type = QEMU_OPT_SIZE,
1648 .help = "L1/L2 table size (in clusters)"
1650 { /* end of list */ }
1654 static BlockDriver bdrv_qed = {
1655 .format_name = "qed",
1656 .instance_size = sizeof(BDRVQEDState),
1657 .create_opts = &qed_create_opts,
1658 .supports_backing = true,
1660 .bdrv_probe = bdrv_qed_probe,
1661 .bdrv_open = bdrv_qed_open,
1662 .bdrv_close = bdrv_qed_close,
1663 .bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
1664 .bdrv_create = bdrv_qed_create,
1665 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1666 .bdrv_co_get_block_status = bdrv_qed_co_get_block_status,
1667 .bdrv_aio_readv = bdrv_qed_aio_readv,
1668 .bdrv_aio_writev = bdrv_qed_aio_writev,
1669 .bdrv_co_write_zeroes = bdrv_qed_co_write_zeroes,
1670 .bdrv_truncate = bdrv_qed_truncate,
1671 .bdrv_getlength = bdrv_qed_getlength,
1672 .bdrv_get_info = bdrv_qed_get_info,
1673 .bdrv_refresh_limits = bdrv_qed_refresh_limits,
1674 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
1675 .bdrv_invalidate_cache = bdrv_qed_invalidate_cache,
1676 .bdrv_check = bdrv_qed_check,
1677 .bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
1678 .bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
1681 static void bdrv_qed_init(void)
1683 bdrv_register(&bdrv_qed);
1686 block_init(bdrv_qed_init);