2 * QEMU Enhanced Disk Format
4 * Copyright IBM, Corp. 2010
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"
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
,
35 const QEDHeader
*header
= (const QEDHeader
*)buf
;
37 if (buf_size
< sizeof(*header
)) {
40 if (le32_to_cpu(header
->magic
) != QED_MAGIC
) {
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
)
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
)) {
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
;
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);
130 qed_header_cpu_to_le(&s
->header
, (QEDHeader
*) buf
);
132 ret
= bdrv_co_pwrite(s
->bs
->file
, 0, len
, buf
, 0);
143 static uint64_t qed_max_image_size(uint32_t cluster_size
, uint32_t table_size
)
145 uint64_t table_entries
;
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
) {
160 if (cluster_size
& (cluster_size
- 1)) {
161 return false; /* not power of 2 */
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
) {
172 if (table_size
& (table_size
- 1)) {
173 return false; /* not power of 2 */
178 static bool qed_is_image_size_valid(uint64_t image_size
, uint32_t cluster_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 */
191 * Read a string of known length from the 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
)
209 ret
= bdrv_pread(file
, offset
, buf
, n
);
218 * Allocate new clusters
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
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
;
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
);
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
);
275 s
->allocating_write_reqs_plugged
= true;
276 qemu_co_mutex_unlock(&s
->table_lock
);
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
;
294 trace_qed_need_check_timer_cb(s
);
296 if (!qed_plug_allocating_write_reqs(s
)) {
300 /* Ensure writes are on disk before clearing flag */
301 ret
= bdrv_co_flush(s
->bs
->file
->bs
);
303 qed_unplug_allocating_write_reqs(s
);
307 s
->header
.features
&= ~QED_F_NEED_CHECK
;
308 ret
= qed_write_header(s
);
311 qed_unplug_allocating_write_reqs(s
);
313 ret
= bdrv_co_flush(s
->bs
);
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
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
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
));
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
;
394 ret
= bdrv_pread(bs
->file
, 0, &le_header
, sizeof(le_header
));
398 qed_header_le_to_cpu(&le_header
, &s
->header
);
400 if (s
->header
.magic
!= QED_MAGIC
) {
401 error_setg(errp
, "Image not in QED format");
404 if (s
->header
.features
& ~QED_FEATURE_MASK
) {
405 /* image uses unsupported feature bits */
406 error_setg(errp
, "Unsupported QED features: %" PRIx64
,
407 s
->header
.features
& ~QED_FEATURE_MASK
);
410 if (!qed_is_cluster_size_valid(s
->header
.cluster_size
)) {
414 /* Round down file size to the last cluster */
415 file_size
= bdrv_getlength(bs
->file
->bs
);
419 s
->file_size
= qed_start_of_cluster(s
, file_size
);
421 if (!qed_is_table_size_valid(s
->header
.table_size
)) {
424 if (!qed_is_image_size_valid(s
->header
.image_size
,
425 s
->header
.cluster_size
,
426 s
->header
.table_size
)) {
429 if (!qed_check_table_offset(s
, s
->header
.l1_table_offset
)) {
433 s
->table_nelems
= (s
->header
.cluster_size
* s
->header
.table_size
) /
435 s
->l2_shift
= ctz32(s
->header
.cluster_size
);
436 s
->l2_mask
= s
->table_nelems
- 1;
437 s
->l1_shift
= s
->l2_shift
+ ctz32(s
->table_nelems
);
439 /* Header size calculation must not overflow uint32_t */
440 if (s
->header
.header_size
> UINT32_MAX
/ s
->header
.cluster_size
) {
444 if ((s
->header
.features
& QED_F_BACKING_FILE
)) {
445 if ((uint64_t)s
->header
.backing_filename_offset
+
446 s
->header
.backing_filename_size
>
447 s
->header
.cluster_size
* s
->header
.header_size
) {
451 ret
= qed_read_string(bs
->file
, s
->header
.backing_filename_offset
,
452 s
->header
.backing_filename_size
,
453 bs
->auto_backing_file
,
454 sizeof(bs
->auto_backing_file
));
458 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
459 bs
->auto_backing_file
);
461 if (s
->header
.features
& QED_F_BACKING_FORMAT_NO_PROBE
) {
462 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), "raw");
466 /* Reset unknown autoclear feature bits. This is a backwards
467 * compatibility mechanism that allows images to be opened by older
468 * programs, which "knock out" unknown feature bits. When an image is
469 * opened by a newer program again it can detect that the autoclear
470 * feature is no longer valid.
472 if ((s
->header
.autoclear_features
& ~QED_AUTOCLEAR_FEATURE_MASK
) != 0 &&
473 !bdrv_is_read_only(bs
->file
->bs
) && !(flags
& BDRV_O_INACTIVE
)) {
474 s
->header
.autoclear_features
&= QED_AUTOCLEAR_FEATURE_MASK
;
476 ret
= qed_write_header_sync(s
);
481 /* From here on only known autoclear feature bits are valid */
482 bdrv_flush(bs
->file
->bs
);
485 s
->l1_table
= qed_alloc_table(s
);
486 qed_init_l2_cache(&s
->l2_cache
);
488 ret
= qed_read_l1_table_sync(s
);
493 /* If image was not closed cleanly, check consistency */
494 if (!(flags
& BDRV_O_CHECK
) && (s
->header
.features
& QED_F_NEED_CHECK
)) {
495 /* Read-only images cannot be fixed. There is no risk of corruption
496 * since write operations are not possible. Therefore, allow
497 * potentially inconsistent images to be opened read-only. This can
498 * aid data recovery from an otherwise inconsistent image.
500 if (!bdrv_is_read_only(bs
->file
->bs
) &&
501 !(flags
& BDRV_O_INACTIVE
)) {
502 BdrvCheckResult result
= {0};
504 ret
= qed_check(s
, &result
, true);
511 bdrv_qed_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
515 qed_free_l2_cache(&s
->l2_cache
);
516 qemu_vfree(s
->l1_table
);
521 typedef struct QEDOpenCo
{
522 BlockDriverState
*bs
;
529 static void coroutine_fn
bdrv_qed_open_entry(void *opaque
)
531 QEDOpenCo
*qoc
= opaque
;
532 BDRVQEDState
*s
= qoc
->bs
->opaque
;
534 qemu_co_mutex_lock(&s
->table_lock
);
535 qoc
->ret
= bdrv_qed_do_open(qoc
->bs
, qoc
->options
, qoc
->flags
, qoc
->errp
);
536 qemu_co_mutex_unlock(&s
->table_lock
);
539 static int bdrv_qed_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
550 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
556 bdrv_qed_init_state(bs
);
557 if (qemu_in_coroutine()) {
558 bdrv_qed_open_entry(&qoc
);
560 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
561 qemu_coroutine_enter(qemu_coroutine_create(bdrv_qed_open_entry
, &qoc
));
562 BDRV_POLL_WHILE(bs
, qoc
.ret
== -EINPROGRESS
);
564 BDRV_POLL_WHILE(bs
, qoc
.ret
== -EINPROGRESS
);
568 static void bdrv_qed_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
570 BDRVQEDState
*s
= bs
->opaque
;
572 bs
->bl
.pwrite_zeroes_alignment
= s
->header
.cluster_size
;
575 /* We have nothing to do for QED reopen, stubs just return
577 static int bdrv_qed_reopen_prepare(BDRVReopenState
*state
,
578 BlockReopenQueue
*queue
, Error
**errp
)
583 static void bdrv_qed_close(BlockDriverState
*bs
)
585 BDRVQEDState
*s
= bs
->opaque
;
587 bdrv_qed_detach_aio_context(bs
);
589 /* Ensure writes reach stable storage */
590 bdrv_flush(bs
->file
->bs
);
592 /* Clean shutdown, no check required on next open */
593 if (s
->header
.features
& QED_F_NEED_CHECK
) {
594 s
->header
.features
&= ~QED_F_NEED_CHECK
;
595 qed_write_header_sync(s
);
598 qed_free_l2_cache(&s
->l2_cache
);
599 qemu_vfree(s
->l1_table
);
602 static int coroutine_fn
bdrv_qed_co_create(BlockdevCreateOptions
*opts
,
605 BlockdevCreateOptionsQed
*qed_opts
;
606 BlockBackend
*blk
= NULL
;
607 BlockDriverState
*bs
= NULL
;
611 uint8_t *l1_table
= NULL
;
615 assert(opts
->driver
== BLOCKDEV_DRIVER_QED
);
616 qed_opts
= &opts
->u
.qed
;
618 /* Validate options and set default values */
619 if (!qed_opts
->has_cluster_size
) {
620 qed_opts
->cluster_size
= QED_DEFAULT_CLUSTER_SIZE
;
622 if (!qed_opts
->has_table_size
) {
623 qed_opts
->table_size
= QED_DEFAULT_TABLE_SIZE
;
626 if (!qed_is_cluster_size_valid(qed_opts
->cluster_size
)) {
627 error_setg(errp
, "QED cluster size must be within range [%u, %u] "
629 QED_MIN_CLUSTER_SIZE
, QED_MAX_CLUSTER_SIZE
);
632 if (!qed_is_table_size_valid(qed_opts
->table_size
)) {
633 error_setg(errp
, "QED table size must be within range [%u, %u] "
635 QED_MIN_TABLE_SIZE
, QED_MAX_TABLE_SIZE
);
638 if (!qed_is_image_size_valid(qed_opts
->size
, qed_opts
->cluster_size
,
639 qed_opts
->table_size
))
641 error_setg(errp
, "QED image size must be a non-zero multiple of "
642 "cluster size and less than %" PRIu64
" bytes",
643 qed_max_image_size(qed_opts
->cluster_size
,
644 qed_opts
->table_size
));
648 /* Create BlockBackend to write to the image */
649 bs
= bdrv_open_blockdev_ref(qed_opts
->file
, errp
);
654 blk
= blk_new(bdrv_get_aio_context(bs
),
655 BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
656 ret
= blk_insert_bs(blk
, bs
, errp
);
660 blk_set_allow_write_beyond_eof(blk
, true);
662 /* Prepare image format */
663 header
= (QEDHeader
) {
665 .cluster_size
= qed_opts
->cluster_size
,
666 .table_size
= qed_opts
->table_size
,
669 .compat_features
= 0,
670 .l1_table_offset
= qed_opts
->cluster_size
,
671 .image_size
= qed_opts
->size
,
674 l1_size
= header
.cluster_size
* header
.table_size
;
677 * The QED format associates file length with allocation status,
678 * so a new file (which is empty) must have a length of 0.
680 ret
= blk_truncate(blk
, 0, true, PREALLOC_MODE_OFF
, errp
);
685 if (qed_opts
->has_backing_file
) {
686 header
.features
|= QED_F_BACKING_FILE
;
687 header
.backing_filename_offset
= sizeof(le_header
);
688 header
.backing_filename_size
= strlen(qed_opts
->backing_file
);
690 if (qed_opts
->has_backing_fmt
) {
691 const char *backing_fmt
= BlockdevDriver_str(qed_opts
->backing_fmt
);
692 if (qed_fmt_is_raw(backing_fmt
)) {
693 header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
698 qed_header_cpu_to_le(&header
, &le_header
);
699 ret
= blk_pwrite(blk
, 0, &le_header
, sizeof(le_header
), 0);
703 ret
= blk_pwrite(blk
, sizeof(le_header
), qed_opts
->backing_file
,
704 header
.backing_filename_size
, 0);
709 l1_table
= g_malloc0(l1_size
);
710 ret
= blk_pwrite(blk
, header
.l1_table_offset
, l1_table
, l1_size
, 0);
715 ret
= 0; /* success */
723 static int coroutine_fn
bdrv_qed_co_create_opts(BlockDriver
*drv
,
724 const char *filename
,
728 BlockdevCreateOptions
*create_options
= NULL
;
731 BlockDriverState
*bs
= NULL
;
732 Error
*local_err
= NULL
;
735 static const QDictRenames opt_renames
[] = {
736 { BLOCK_OPT_BACKING_FILE
, "backing-file" },
737 { BLOCK_OPT_BACKING_FMT
, "backing-fmt" },
738 { BLOCK_OPT_CLUSTER_SIZE
, "cluster-size" },
739 { BLOCK_OPT_TABLE_SIZE
, "table-size" },
743 /* Parse options and convert legacy syntax */
744 qdict
= qemu_opts_to_qdict_filtered(opts
, NULL
, &qed_create_opts
, true);
746 if (!qdict_rename_keys(qdict
, opt_renames
, errp
)) {
751 /* Create and open the file (protocol layer) */
752 ret
= bdrv_create_file(filename
, opts
, &local_err
);
754 error_propagate(errp
, local_err
);
758 bs
= bdrv_open(filename
, NULL
, NULL
,
759 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
765 /* Now get the QAPI type BlockdevCreateOptions */
766 qdict_put_str(qdict
, "driver", "qed");
767 qdict_put_str(qdict
, "file", bs
->node_name
);
769 v
= qobject_input_visitor_new_flat_confused(qdict
, errp
);
775 visit_type_BlockdevCreateOptions(v
, NULL
, &create_options
, &local_err
);
779 error_propagate(errp
, local_err
);
784 /* Silently round up size */
785 assert(create_options
->driver
== BLOCKDEV_DRIVER_QED
);
786 create_options
->u
.qed
.size
=
787 ROUND_UP(create_options
->u
.qed
.size
, BDRV_SECTOR_SIZE
);
789 /* Create the qed image (format layer) */
790 ret
= bdrv_qed_co_create(create_options
, errp
);
793 qobject_unref(qdict
);
795 qapi_free_BlockdevCreateOptions(create_options
);
799 static int coroutine_fn
bdrv_qed_co_block_status(BlockDriverState
*bs
,
801 int64_t pos
, int64_t bytes
,
802 int64_t *pnum
, int64_t *map
,
803 BlockDriverState
**file
)
805 BDRVQEDState
*s
= bs
->opaque
;
806 size_t len
= MIN(bytes
, SIZE_MAX
);
808 QEDRequest request
= { .l2_table
= NULL
};
812 qemu_co_mutex_lock(&s
->table_lock
);
813 ret
= qed_find_cluster(s
, &request
, pos
, &len
, &offset
);
817 case QED_CLUSTER_FOUND
:
818 *map
= offset
| qed_offset_into_cluster(s
, pos
);
819 status
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
820 *file
= bs
->file
->bs
;
822 case QED_CLUSTER_ZERO
:
823 status
= BDRV_BLOCK_ZERO
;
835 qed_unref_l2_cache_entry(request
.l2_table
);
836 qemu_co_mutex_unlock(&s
->table_lock
);
841 static BDRVQEDState
*acb_to_s(QEDAIOCB
*acb
)
843 return acb
->bs
->opaque
;
847 * Read from the backing file or zero-fill if no backing file
850 * @pos: Byte position in device
851 * @qiov: Destination I/O vector
852 * @backing_qiov: Possibly shortened copy of qiov, to be allocated here
853 * @cb: Completion function
854 * @opaque: User data for completion function
856 * This function reads qiov->size bytes starting at pos from the backing file.
857 * If there is no backing file then zeroes are read.
859 static int coroutine_fn
qed_read_backing_file(BDRVQEDState
*s
, uint64_t pos
,
861 QEMUIOVector
**backing_qiov
)
863 uint64_t backing_length
= 0;
867 /* If there is a backing file, get its length. Treat the absence of a
868 * backing file like a zero length backing file.
870 if (s
->bs
->backing
) {
871 int64_t l
= bdrv_getlength(s
->bs
->backing
->bs
);
878 /* Zero all sectors if reading beyond the end of the backing file */
879 if (pos
>= backing_length
||
880 pos
+ qiov
->size
> backing_length
) {
881 qemu_iovec_memset(qiov
, 0, 0, qiov
->size
);
884 /* Complete now if there are no backing file sectors to read */
885 if (pos
>= backing_length
) {
889 /* If the read straddles the end of the backing file, shorten it */
890 size
= MIN((uint64_t)backing_length
- pos
, qiov
->size
);
892 assert(*backing_qiov
== NULL
);
893 *backing_qiov
= g_new(QEMUIOVector
, 1);
894 qemu_iovec_init(*backing_qiov
, qiov
->niov
);
895 qemu_iovec_concat(*backing_qiov
, qiov
, 0, size
);
897 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_READ_BACKING_AIO
);
898 ret
= bdrv_co_preadv(s
->bs
->backing
, pos
, size
, *backing_qiov
, 0);
906 * Copy data from backing file into the image
909 * @pos: Byte position in device
910 * @len: Number of bytes
911 * @offset: Byte offset in image file
913 static int coroutine_fn
qed_copy_from_backing_file(BDRVQEDState
*s
,
914 uint64_t pos
, uint64_t len
,
918 QEMUIOVector
*backing_qiov
= NULL
;
921 /* Skip copy entirely if there is no work to do */
926 qemu_iovec_init_buf(&qiov
, qemu_blockalign(s
->bs
, len
), len
);
928 ret
= qed_read_backing_file(s
, pos
, &qiov
, &backing_qiov
);
931 qemu_iovec_destroy(backing_qiov
);
932 g_free(backing_qiov
);
940 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_COW_WRITE
);
941 ret
= bdrv_co_pwritev(s
->bs
->file
, offset
, qiov
.size
, &qiov
, 0);
947 qemu_vfree(qemu_iovec_buf(&qiov
));
952 * Link one or more contiguous clusters into a table
956 * @index: First cluster index
957 * @n: Number of contiguous clusters
958 * @cluster: First cluster offset
960 * The cluster offset may be an allocated byte offset in the image file, the
961 * zero cluster marker, or the unallocated cluster marker.
963 * Called with table_lock held.
965 static void coroutine_fn
qed_update_l2_table(BDRVQEDState
*s
, QEDTable
*table
,
966 int index
, unsigned int n
,
970 for (i
= index
; i
< index
+ n
; i
++) {
971 table
->offsets
[i
] = cluster
;
972 if (!qed_offset_is_unalloc_cluster(cluster
) &&
973 !qed_offset_is_zero_cluster(cluster
)) {
974 cluster
+= s
->header
.cluster_size
;
979 /* Called with table_lock held. */
980 static void coroutine_fn
qed_aio_complete(QEDAIOCB
*acb
)
982 BDRVQEDState
*s
= acb_to_s(acb
);
985 qemu_iovec_destroy(&acb
->cur_qiov
);
986 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
988 /* Free the buffer we may have allocated for zero writes */
989 if (acb
->flags
& QED_AIOCB_ZERO
) {
990 qemu_vfree(acb
->qiov
->iov
[0].iov_base
);
991 acb
->qiov
->iov
[0].iov_base
= NULL
;
994 /* Start next allocating write request waiting behind this one. Note that
995 * requests enqueue themselves when they first hit an unallocated cluster
996 * but they wait until the entire request is finished before waking up the
997 * next request in the queue. This ensures that we don't cycle through
998 * requests multiple times but rather finish one at a time completely.
1000 if (acb
== s
->allocating_acb
) {
1001 s
->allocating_acb
= NULL
;
1002 if (!qemu_co_queue_empty(&s
->allocating_write_reqs
)) {
1003 qemu_co_queue_next(&s
->allocating_write_reqs
);
1004 } else if (s
->header
.features
& QED_F_NEED_CHECK
) {
1005 qed_start_need_check_timer(s
);
1011 * Update L1 table with new L2 table offset and write it out
1013 * Called with table_lock held.
1015 static int coroutine_fn
qed_aio_write_l1_update(QEDAIOCB
*acb
)
1017 BDRVQEDState
*s
= acb_to_s(acb
);
1018 CachedL2Table
*l2_table
= acb
->request
.l2_table
;
1019 uint64_t l2_offset
= l2_table
->offset
;
1022 index
= qed_l1_index(s
, acb
->cur_pos
);
1023 s
->l1_table
->offsets
[index
] = l2_table
->offset
;
1025 ret
= qed_write_l1_table(s
, index
, 1);
1027 /* Commit the current L2 table to the cache */
1028 qed_commit_l2_cache_entry(&s
->l2_cache
, l2_table
);
1030 /* This is guaranteed to succeed because we just committed the entry to the
1033 acb
->request
.l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, l2_offset
);
1034 assert(acb
->request
.l2_table
!= NULL
);
1041 * Update L2 table with new cluster offsets and write them out
1043 * Called with table_lock held.
1045 static int coroutine_fn
qed_aio_write_l2_update(QEDAIOCB
*acb
, uint64_t offset
)
1047 BDRVQEDState
*s
= acb_to_s(acb
);
1048 bool need_alloc
= acb
->find_cluster_ret
== QED_CLUSTER_L1
;
1052 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
1053 acb
->request
.l2_table
= qed_new_l2_table(s
);
1056 index
= qed_l2_index(s
, acb
->cur_pos
);
1057 qed_update_l2_table(s
, acb
->request
.l2_table
->table
, index
, acb
->cur_nclusters
,
1061 /* Write out the whole new L2 table */
1062 ret
= qed_write_l2_table(s
, &acb
->request
, 0, s
->table_nelems
, true);
1066 return qed_aio_write_l1_update(acb
);
1068 /* Write out only the updated part of the L2 table */
1069 ret
= qed_write_l2_table(s
, &acb
->request
, index
, acb
->cur_nclusters
,
1079 * Write data to the image file
1081 * Called with table_lock *not* held.
1083 static int coroutine_fn
qed_aio_write_main(QEDAIOCB
*acb
)
1085 BDRVQEDState
*s
= acb_to_s(acb
);
1086 uint64_t offset
= acb
->cur_cluster
+
1087 qed_offset_into_cluster(s
, acb
->cur_pos
);
1089 trace_qed_aio_write_main(s
, acb
, 0, offset
, acb
->cur_qiov
.size
);
1091 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_WRITE_AIO
);
1092 return bdrv_co_pwritev(s
->bs
->file
, offset
, acb
->cur_qiov
.size
,
1097 * Populate untouched regions of new data cluster
1099 * Called with table_lock held.
1101 static int coroutine_fn
qed_aio_write_cow(QEDAIOCB
*acb
)
1103 BDRVQEDState
*s
= acb_to_s(acb
);
1104 uint64_t start
, len
, offset
;
1107 qemu_co_mutex_unlock(&s
->table_lock
);
1109 /* Populate front untouched region of new data cluster */
1110 start
= qed_start_of_cluster(s
, acb
->cur_pos
);
1111 len
= qed_offset_into_cluster(s
, acb
->cur_pos
);
1113 trace_qed_aio_write_prefill(s
, acb
, start
, len
, acb
->cur_cluster
);
1114 ret
= qed_copy_from_backing_file(s
, start
, len
, acb
->cur_cluster
);
1119 /* Populate back untouched region of new data cluster */
1120 start
= acb
->cur_pos
+ acb
->cur_qiov
.size
;
1121 len
= qed_start_of_cluster(s
, start
+ s
->header
.cluster_size
- 1) - start
;
1122 offset
= acb
->cur_cluster
+
1123 qed_offset_into_cluster(s
, acb
->cur_pos
) +
1126 trace_qed_aio_write_postfill(s
, acb
, start
, len
, offset
);
1127 ret
= qed_copy_from_backing_file(s
, start
, len
, offset
);
1132 ret
= qed_aio_write_main(acb
);
1137 if (s
->bs
->backing
) {
1139 * Flush new data clusters before updating the L2 table
1141 * This flush is necessary when a backing file is in use. A crash
1142 * during an allocating write could result in empty clusters in the
1143 * image. If the write only touched a subregion of the cluster,
1144 * then backing image sectors have been lost in the untouched
1145 * region. The solution is to flush after writing a new data
1146 * cluster and before updating the L2 table.
1148 ret
= bdrv_co_flush(s
->bs
->file
->bs
);
1152 qemu_co_mutex_lock(&s
->table_lock
);
1157 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1159 static bool qed_should_set_need_check(BDRVQEDState
*s
)
1161 /* The flush before L2 update path ensures consistency */
1162 if (s
->bs
->backing
) {
1166 return !(s
->header
.features
& QED_F_NEED_CHECK
);
1170 * Write new data cluster
1172 * @acb: Write request
1173 * @len: Length in bytes
1175 * This path is taken when writing to previously unallocated clusters.
1177 * Called with table_lock held.
1179 static int coroutine_fn
qed_aio_write_alloc(QEDAIOCB
*acb
, size_t len
)
1181 BDRVQEDState
*s
= acb_to_s(acb
);
1184 /* Cancel timer when the first allocating request comes in */
1185 if (s
->allocating_acb
== NULL
) {
1186 qed_cancel_need_check_timer(s
);
1189 /* Freeze this request if another allocating write is in progress */
1190 if (s
->allocating_acb
!= acb
|| s
->allocating_write_reqs_plugged
) {
1191 if (s
->allocating_acb
!= NULL
) {
1192 qemu_co_queue_wait(&s
->allocating_write_reqs
, &s
->table_lock
);
1193 assert(s
->allocating_acb
== NULL
);
1195 s
->allocating_acb
= acb
;
1196 return -EAGAIN
; /* start over with looking up table entries */
1199 acb
->cur_nclusters
= qed_bytes_to_clusters(s
,
1200 qed_offset_into_cluster(s
, acb
->cur_pos
) + len
);
1201 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1203 if (acb
->flags
& QED_AIOCB_ZERO
) {
1204 /* Skip ahead if the clusters are already zero */
1205 if (acb
->find_cluster_ret
== QED_CLUSTER_ZERO
) {
1208 acb
->cur_cluster
= 1;
1210 acb
->cur_cluster
= qed_alloc_clusters(s
, acb
->cur_nclusters
);
1213 if (qed_should_set_need_check(s
)) {
1214 s
->header
.features
|= QED_F_NEED_CHECK
;
1215 ret
= qed_write_header(s
);
1221 if (!(acb
->flags
& QED_AIOCB_ZERO
)) {
1222 ret
= qed_aio_write_cow(acb
);
1228 return qed_aio_write_l2_update(acb
, acb
->cur_cluster
);
1232 * Write data cluster in place
1234 * @acb: Write request
1235 * @offset: Cluster offset in bytes
1236 * @len: Length in bytes
1238 * This path is taken when writing to already allocated clusters.
1240 * Called with table_lock held.
1242 static int coroutine_fn
qed_aio_write_inplace(QEDAIOCB
*acb
, uint64_t offset
,
1245 BDRVQEDState
*s
= acb_to_s(acb
);
1248 qemu_co_mutex_unlock(&s
->table_lock
);
1250 /* Allocate buffer for zero writes */
1251 if (acb
->flags
& QED_AIOCB_ZERO
) {
1252 struct iovec
*iov
= acb
->qiov
->iov
;
1254 if (!iov
->iov_base
) {
1255 iov
->iov_base
= qemu_try_blockalign(acb
->bs
, iov
->iov_len
);
1256 if (iov
->iov_base
== NULL
) {
1260 memset(iov
->iov_base
, 0, iov
->iov_len
);
1264 /* Calculate the I/O vector */
1265 acb
->cur_cluster
= offset
;
1266 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1268 /* Do the actual write. */
1269 r
= qed_aio_write_main(acb
);
1271 qemu_co_mutex_lock(&s
->table_lock
);
1276 * Write data cluster
1278 * @opaque: Write request
1279 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
1280 * @offset: Cluster offset in bytes
1281 * @len: Length in bytes
1283 * Called with table_lock held.
1285 static int coroutine_fn
qed_aio_write_data(void *opaque
, int ret
,
1286 uint64_t offset
, size_t len
)
1288 QEDAIOCB
*acb
= opaque
;
1290 trace_qed_aio_write_data(acb_to_s(acb
), acb
, ret
, offset
, len
);
1292 acb
->find_cluster_ret
= ret
;
1295 case QED_CLUSTER_FOUND
:
1296 return qed_aio_write_inplace(acb
, offset
, len
);
1298 case QED_CLUSTER_L2
:
1299 case QED_CLUSTER_L1
:
1300 case QED_CLUSTER_ZERO
:
1301 return qed_aio_write_alloc(acb
, len
);
1304 g_assert_not_reached();
1311 * @opaque: Read request
1312 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
1313 * @offset: Cluster offset in bytes
1314 * @len: Length in bytes
1316 * Called with table_lock held.
1318 static int coroutine_fn
qed_aio_read_data(void *opaque
, int ret
,
1319 uint64_t offset
, size_t len
)
1321 QEDAIOCB
*acb
= opaque
;
1322 BDRVQEDState
*s
= acb_to_s(acb
);
1323 BlockDriverState
*bs
= acb
->bs
;
1326 qemu_co_mutex_unlock(&s
->table_lock
);
1328 /* Adjust offset into cluster */
1329 offset
+= qed_offset_into_cluster(s
, acb
->cur_pos
);
1331 trace_qed_aio_read_data(s
, acb
, ret
, offset
, len
);
1333 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1335 /* Handle zero cluster and backing file reads, otherwise read
1336 * data cluster directly.
1338 if (ret
== QED_CLUSTER_ZERO
) {
1339 qemu_iovec_memset(&acb
->cur_qiov
, 0, 0, acb
->cur_qiov
.size
);
1341 } else if (ret
!= QED_CLUSTER_FOUND
) {
1342 r
= qed_read_backing_file(s
, acb
->cur_pos
, &acb
->cur_qiov
,
1343 &acb
->backing_qiov
);
1345 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
1346 r
= bdrv_co_preadv(bs
->file
, offset
, acb
->cur_qiov
.size
,
1350 qemu_co_mutex_lock(&s
->table_lock
);
1355 * Begin next I/O or complete the request
1357 static int coroutine_fn
qed_aio_next_io(QEDAIOCB
*acb
)
1359 BDRVQEDState
*s
= acb_to_s(acb
);
1364 qemu_co_mutex_lock(&s
->table_lock
);
1366 trace_qed_aio_next_io(s
, acb
, 0, acb
->cur_pos
+ acb
->cur_qiov
.size
);
1368 if (acb
->backing_qiov
) {
1369 qemu_iovec_destroy(acb
->backing_qiov
);
1370 g_free(acb
->backing_qiov
);
1371 acb
->backing_qiov
= NULL
;
1374 acb
->qiov_offset
+= acb
->cur_qiov
.size
;
1375 acb
->cur_pos
+= acb
->cur_qiov
.size
;
1376 qemu_iovec_reset(&acb
->cur_qiov
);
1378 /* Complete request */
1379 if (acb
->cur_pos
>= acb
->end_pos
) {
1384 /* Find next cluster and start I/O */
1385 len
= acb
->end_pos
- acb
->cur_pos
;
1386 ret
= qed_find_cluster(s
, &acb
->request
, acb
->cur_pos
, &len
, &offset
);
1391 if (acb
->flags
& QED_AIOCB_WRITE
) {
1392 ret
= qed_aio_write_data(acb
, ret
, offset
, len
);
1394 ret
= qed_aio_read_data(acb
, ret
, offset
, len
);
1397 if (ret
< 0 && ret
!= -EAGAIN
) {
1402 trace_qed_aio_complete(s
, acb
, ret
);
1403 qed_aio_complete(acb
);
1404 qemu_co_mutex_unlock(&s
->table_lock
);
1408 static int coroutine_fn
qed_co_request(BlockDriverState
*bs
, int64_t sector_num
,
1409 QEMUIOVector
*qiov
, int nb_sectors
,
1414 .cur_pos
= (uint64_t) sector_num
* BDRV_SECTOR_SIZE
,
1415 .end_pos
= (sector_num
+ nb_sectors
) * BDRV_SECTOR_SIZE
,
1419 qemu_iovec_init(&acb
.cur_qiov
, qiov
->niov
);
1421 trace_qed_aio_setup(bs
->opaque
, &acb
, sector_num
, nb_sectors
, NULL
, flags
);
1424 return qed_aio_next_io(&acb
);
1427 static int coroutine_fn
bdrv_qed_co_readv(BlockDriverState
*bs
,
1428 int64_t sector_num
, int nb_sectors
,
1431 return qed_co_request(bs
, sector_num
, qiov
, nb_sectors
, 0);
1434 static int coroutine_fn
bdrv_qed_co_writev(BlockDriverState
*bs
,
1435 int64_t sector_num
, int nb_sectors
,
1436 QEMUIOVector
*qiov
, int flags
)
1439 return qed_co_request(bs
, sector_num
, qiov
, nb_sectors
, QED_AIOCB_WRITE
);
1442 static int coroutine_fn
bdrv_qed_co_pwrite_zeroes(BlockDriverState
*bs
,
1445 BdrvRequestFlags flags
)
1447 BDRVQEDState
*s
= bs
->opaque
;
1450 * Zero writes start without an I/O buffer. If a buffer becomes necessary
1451 * then it will be allocated during request processing.
1453 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, bytes
);
1455 /* Fall back if the request is not aligned */
1456 if (qed_offset_into_cluster(s
, offset
) ||
1457 qed_offset_into_cluster(s
, bytes
)) {
1461 return qed_co_request(bs
, offset
>> BDRV_SECTOR_BITS
, &qiov
,
1462 bytes
>> BDRV_SECTOR_BITS
,
1463 QED_AIOCB_WRITE
| QED_AIOCB_ZERO
);
1466 static int coroutine_fn
bdrv_qed_co_truncate(BlockDriverState
*bs
,
1469 PreallocMode prealloc
,
1472 BDRVQEDState
*s
= bs
->opaque
;
1473 uint64_t old_image_size
;
1476 if (prealloc
!= PREALLOC_MODE_OFF
) {
1477 error_setg(errp
, "Unsupported preallocation mode '%s'",
1478 PreallocMode_str(prealloc
));
1482 if (!qed_is_image_size_valid(offset
, s
->header
.cluster_size
,
1483 s
->header
.table_size
)) {
1484 error_setg(errp
, "Invalid image size specified");
1488 if ((uint64_t)offset
< s
->header
.image_size
) {
1489 error_setg(errp
, "Shrinking images is currently not supported");
1493 old_image_size
= s
->header
.image_size
;
1494 s
->header
.image_size
= offset
;
1495 ret
= qed_write_header_sync(s
);
1497 s
->header
.image_size
= old_image_size
;
1498 error_setg_errno(errp
, -ret
, "Failed to update the image size");
1503 static int64_t bdrv_qed_getlength(BlockDriverState
*bs
)
1505 BDRVQEDState
*s
= bs
->opaque
;
1506 return s
->header
.image_size
;
1509 static int bdrv_qed_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1511 BDRVQEDState
*s
= bs
->opaque
;
1513 memset(bdi
, 0, sizeof(*bdi
));
1514 bdi
->cluster_size
= s
->header
.cluster_size
;
1515 bdi
->is_dirty
= s
->header
.features
& QED_F_NEED_CHECK
;
1516 bdi
->unallocated_blocks_are_zero
= true;
1520 static int bdrv_qed_change_backing_file(BlockDriverState
*bs
,
1521 const char *backing_file
,
1522 const char *backing_fmt
)
1524 BDRVQEDState
*s
= bs
->opaque
;
1525 QEDHeader new_header
, le_header
;
1527 size_t buffer_len
, backing_file_len
;
1530 /* Refuse to set backing filename if unknown compat feature bits are
1531 * active. If the image uses an unknown compat feature then we may not
1532 * know the layout of data following the header structure and cannot safely
1535 if (backing_file
&& (s
->header
.compat_features
&
1536 ~QED_COMPAT_FEATURE_MASK
)) {
1540 memcpy(&new_header
, &s
->header
, sizeof(new_header
));
1542 new_header
.features
&= ~(QED_F_BACKING_FILE
|
1543 QED_F_BACKING_FORMAT_NO_PROBE
);
1545 /* Adjust feature flags */
1547 new_header
.features
|= QED_F_BACKING_FILE
;
1549 if (qed_fmt_is_raw(backing_fmt
)) {
1550 new_header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
1554 /* Calculate new header size */
1555 backing_file_len
= 0;
1558 backing_file_len
= strlen(backing_file
);
1561 buffer_len
= sizeof(new_header
);
1562 new_header
.backing_filename_offset
= buffer_len
;
1563 new_header
.backing_filename_size
= backing_file_len
;
1564 buffer_len
+= backing_file_len
;
1566 /* Make sure we can rewrite header without failing */
1567 if (buffer_len
> new_header
.header_size
* new_header
.cluster_size
) {
1571 /* Prepare new header */
1572 buffer
= g_malloc(buffer_len
);
1574 qed_header_cpu_to_le(&new_header
, &le_header
);
1575 memcpy(buffer
, &le_header
, sizeof(le_header
));
1576 buffer_len
= sizeof(le_header
);
1579 memcpy(buffer
+ buffer_len
, backing_file
, backing_file_len
);
1580 buffer_len
+= backing_file_len
;
1583 /* Write new header */
1584 ret
= bdrv_pwrite_sync(bs
->file
, 0, buffer
, buffer_len
);
1587 memcpy(&s
->header
, &new_header
, sizeof(new_header
));
1592 static void coroutine_fn
bdrv_qed_co_invalidate_cache(BlockDriverState
*bs
,
1595 BDRVQEDState
*s
= bs
->opaque
;
1596 Error
*local_err
= NULL
;
1601 bdrv_qed_init_state(bs
);
1602 qemu_co_mutex_lock(&s
->table_lock
);
1603 ret
= bdrv_qed_do_open(bs
, NULL
, bs
->open_flags
, &local_err
);
1604 qemu_co_mutex_unlock(&s
->table_lock
);
1606 error_propagate_prepend(errp
, local_err
,
1607 "Could not reopen qed layer: ");
1609 } else if (ret
< 0) {
1610 error_setg_errno(errp
, -ret
, "Could not reopen qed layer");
1615 static int coroutine_fn
bdrv_qed_co_check(BlockDriverState
*bs
,
1616 BdrvCheckResult
*result
,
1619 BDRVQEDState
*s
= bs
->opaque
;
1622 qemu_co_mutex_lock(&s
->table_lock
);
1623 ret
= qed_check(s
, result
, !!fix
);
1624 qemu_co_mutex_unlock(&s
->table_lock
);
1629 static QemuOptsList qed_create_opts
= {
1630 .name
= "qed-create-opts",
1631 .head
= QTAILQ_HEAD_INITIALIZER(qed_create_opts
.head
),
1634 .name
= BLOCK_OPT_SIZE
,
1635 .type
= QEMU_OPT_SIZE
,
1636 .help
= "Virtual disk size"
1639 .name
= BLOCK_OPT_BACKING_FILE
,
1640 .type
= QEMU_OPT_STRING
,
1641 .help
= "File name of a base image"
1644 .name
= BLOCK_OPT_BACKING_FMT
,
1645 .type
= QEMU_OPT_STRING
,
1646 .help
= "Image format of the base image"
1649 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1650 .type
= QEMU_OPT_SIZE
,
1651 .help
= "Cluster size (in bytes)",
1652 .def_value_str
= stringify(QED_DEFAULT_CLUSTER_SIZE
)
1655 .name
= BLOCK_OPT_TABLE_SIZE
,
1656 .type
= QEMU_OPT_SIZE
,
1657 .help
= "L1/L2 table size (in clusters)"
1659 { /* end of list */ }
1663 static BlockDriver bdrv_qed
= {
1664 .format_name
= "qed",
1665 .instance_size
= sizeof(BDRVQEDState
),
1666 .create_opts
= &qed_create_opts
,
1667 .supports_backing
= true,
1669 .bdrv_probe
= bdrv_qed_probe
,
1670 .bdrv_open
= bdrv_qed_open
,
1671 .bdrv_close
= bdrv_qed_close
,
1672 .bdrv_reopen_prepare
= bdrv_qed_reopen_prepare
,
1673 .bdrv_child_perm
= bdrv_format_default_perms
,
1674 .bdrv_co_create
= bdrv_qed_co_create
,
1675 .bdrv_co_create_opts
= bdrv_qed_co_create_opts
,
1676 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1677 .bdrv_has_zero_init_truncate
= bdrv_has_zero_init_1
,
1678 .bdrv_co_block_status
= bdrv_qed_co_block_status
,
1679 .bdrv_co_readv
= bdrv_qed_co_readv
,
1680 .bdrv_co_writev
= bdrv_qed_co_writev
,
1681 .bdrv_co_pwrite_zeroes
= bdrv_qed_co_pwrite_zeroes
,
1682 .bdrv_co_truncate
= bdrv_qed_co_truncate
,
1683 .bdrv_getlength
= bdrv_qed_getlength
,
1684 .bdrv_get_info
= bdrv_qed_get_info
,
1685 .bdrv_refresh_limits
= bdrv_qed_refresh_limits
,
1686 .bdrv_change_backing_file
= bdrv_qed_change_backing_file
,
1687 .bdrv_co_invalidate_cache
= bdrv_qed_co_invalidate_cache
,
1688 .bdrv_co_check
= bdrv_qed_co_check
,
1689 .bdrv_detach_aio_context
= bdrv_qed_detach_aio_context
,
1690 .bdrv_attach_aio_context
= bdrv_qed_attach_aio_context
,
1691 .bdrv_co_drain_begin
= bdrv_qed_co_drain_begin
,
1694 static void bdrv_qed_init(void)
1696 bdrv_register(&bdrv_qed
);
1699 block_init(bdrv_qed_init
);