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/timer.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
,
28 const QEDHeader
*header
= (const QEDHeader
*)buf
;
30 if (buf_size
< sizeof(*header
)) {
33 if (le32_to_cpu(header
->magic
) != QED_MAGIC
) {
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
)
84 qed_header_cpu_to_le(&s
->header
, &le
);
85 ret
= bdrv_pwrite(s
->bs
->file
, 0, &le
, sizeof(le
));
86 if (ret
!= sizeof(le
)) {
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
;
115 qed_write_header_cb(write_header_cb
, ret
);
120 qed_header_cpu_to_le(&s
->header
, (QEDHeader
*)write_header_cb
->buf
);
122 bdrv_aio_writev(s
->bs
->file
, 0, &write_header_cb
->qiov
,
123 write_header_cb
->nsectors
, qed_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
,
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) /
144 size_t len
= nsectors
* BDRV_SECTOR_SIZE
;
145 QEDWriteHeaderCB
*write_header_cb
= gencb_alloc(sizeof(*write_header_cb
),
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
, 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
;
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
) {
176 if (cluster_size
& (cluster_size
- 1)) {
177 return false; /* not power of 2 */
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
) {
188 if (table_size
& (table_size
- 1)) {
189 return false; /* not power of 2 */
194 static bool qed_is_image_size_valid(uint64_t image_size
, uint32_t cluster_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 */
207 * Read a string of known length from the 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
)
225 ret
= bdrv_pread(file
, offset
, buf
, n
);
234 * Allocate new clusters
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
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
;
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
);
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
)
286 assert(s
->allocating_write_reqs_plugged
);
288 s
->allocating_write_reqs_plugged
= false;
290 acb
= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
);
292 qed_aio_next_io(acb
, 0);
296 static void qed_finish_clear_need_check(void *opaque
, int ret
)
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
;
316 qed_unplug_allocating_write_reqs(s
);
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
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_rebind(BlockDriverState
*bs
)
359 BDRVQEDState
*s
= bs
->opaque
;
363 static void bdrv_qed_detach_aio_context(BlockDriverState
*bs
)
365 BDRVQEDState
*s
= bs
->opaque
;
367 qed_cancel_need_check_timer(s
);
368 timer_free(s
->need_check_timer
);
371 static void bdrv_qed_attach_aio_context(BlockDriverState
*bs
,
372 AioContext
*new_context
)
374 BDRVQEDState
*s
= bs
->opaque
;
376 s
->need_check_timer
= aio_timer_new(new_context
,
377 QEMU_CLOCK_VIRTUAL
, SCALE_NS
,
378 qed_need_check_timer_cb
, s
);
379 if (s
->header
.features
& QED_F_NEED_CHECK
) {
380 qed_start_need_check_timer(s
);
384 static int bdrv_qed_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
387 BDRVQEDState
*s
= bs
->opaque
;
393 QSIMPLEQ_INIT(&s
->allocating_write_reqs
);
395 ret
= bdrv_pread(bs
->file
, 0, &le_header
, sizeof(le_header
));
399 qed_header_le_to_cpu(&le_header
, &s
->header
);
401 if (s
->header
.magic
!= QED_MAGIC
) {
402 error_setg(errp
, "Image not in QED format");
405 if (s
->header
.features
& ~QED_FEATURE_MASK
) {
406 /* image uses unsupported feature bits */
408 snprintf(buf
, sizeof(buf
), "%" PRIx64
,
409 s
->header
.features
& ~QED_FEATURE_MASK
);
410 error_set(errp
, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
411 bdrv_get_device_or_node_name(bs
), "QED", buf
);
414 if (!qed_is_cluster_size_valid(s
->header
.cluster_size
)) {
418 /* Round down file size to the last cluster */
419 file_size
= bdrv_getlength(bs
->file
);
423 s
->file_size
= qed_start_of_cluster(s
, file_size
);
425 if (!qed_is_table_size_valid(s
->header
.table_size
)) {
428 if (!qed_is_image_size_valid(s
->header
.image_size
,
429 s
->header
.cluster_size
,
430 s
->header
.table_size
)) {
433 if (!qed_check_table_offset(s
, s
->header
.l1_table_offset
)) {
437 s
->table_nelems
= (s
->header
.cluster_size
* s
->header
.table_size
) /
439 s
->l2_shift
= ctz32(s
->header
.cluster_size
);
440 s
->l2_mask
= s
->table_nelems
- 1;
441 s
->l1_shift
= s
->l2_shift
+ ctz32(s
->table_nelems
);
443 /* Header size calculation must not overflow uint32_t */
444 if (s
->header
.header_size
> UINT32_MAX
/ s
->header
.cluster_size
) {
448 if ((s
->header
.features
& QED_F_BACKING_FILE
)) {
449 if ((uint64_t)s
->header
.backing_filename_offset
+
450 s
->header
.backing_filename_size
>
451 s
->header
.cluster_size
* s
->header
.header_size
) {
455 ret
= qed_read_string(bs
->file
, s
->header
.backing_filename_offset
,
456 s
->header
.backing_filename_size
, bs
->backing_file
,
457 sizeof(bs
->backing_file
));
462 if (s
->header
.features
& QED_F_BACKING_FORMAT_NO_PROBE
) {
463 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), "raw");
467 /* Reset unknown autoclear feature bits. This is a backwards
468 * compatibility mechanism that allows images to be opened by older
469 * programs, which "knock out" unknown feature bits. When an image is
470 * opened by a newer program again it can detect that the autoclear
471 * feature is no longer valid.
473 if ((s
->header
.autoclear_features
& ~QED_AUTOCLEAR_FEATURE_MASK
) != 0 &&
474 !bdrv_is_read_only(bs
->file
) && !(flags
& BDRV_O_INCOMING
)) {
475 s
->header
.autoclear_features
&= QED_AUTOCLEAR_FEATURE_MASK
;
477 ret
= qed_write_header_sync(s
);
482 /* From here on only known autoclear feature bits are valid */
483 bdrv_flush(bs
->file
);
486 s
->l1_table
= qed_alloc_table(s
);
487 qed_init_l2_cache(&s
->l2_cache
);
489 ret
= qed_read_l1_table_sync(s
);
494 /* If image was not closed cleanly, check consistency */
495 if (!(flags
& BDRV_O_CHECK
) && (s
->header
.features
& QED_F_NEED_CHECK
)) {
496 /* Read-only images cannot be fixed. There is no risk of corruption
497 * since write operations are not possible. Therefore, allow
498 * potentially inconsistent images to be opened read-only. This can
499 * aid data recovery from an otherwise inconsistent image.
501 if (!bdrv_is_read_only(bs
->file
) &&
502 !(flags
& BDRV_O_INCOMING
)) {
503 BdrvCheckResult result
= {0};
505 ret
= qed_check(s
, &result
, true);
512 bdrv_qed_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
516 qed_free_l2_cache(&s
->l2_cache
);
517 qemu_vfree(s
->l1_table
);
522 static void bdrv_qed_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
524 BDRVQEDState
*s
= bs
->opaque
;
526 bs
->bl
.write_zeroes_alignment
= s
->header
.cluster_size
>> BDRV_SECTOR_BITS
;
529 /* We have nothing to do for QED reopen, stubs just return
531 static int bdrv_qed_reopen_prepare(BDRVReopenState
*state
,
532 BlockReopenQueue
*queue
, Error
**errp
)
537 static void bdrv_qed_close(BlockDriverState
*bs
)
539 BDRVQEDState
*s
= bs
->opaque
;
541 bdrv_qed_detach_aio_context(bs
);
543 /* Ensure writes reach stable storage */
544 bdrv_flush(bs
->file
);
546 /* Clean shutdown, no check required on next open */
547 if (s
->header
.features
& QED_F_NEED_CHECK
) {
548 s
->header
.features
&= ~QED_F_NEED_CHECK
;
549 qed_write_header_sync(s
);
552 qed_free_l2_cache(&s
->l2_cache
);
553 qemu_vfree(s
->l1_table
);
556 static int qed_create(const char *filename
, uint32_t cluster_size
,
557 uint64_t image_size
, uint32_t table_size
,
558 const char *backing_file
, const char *backing_fmt
,
559 QemuOpts
*opts
, Error
**errp
)
563 .cluster_size
= cluster_size
,
564 .table_size
= table_size
,
567 .compat_features
= 0,
568 .l1_table_offset
= cluster_size
,
569 .image_size
= image_size
,
572 uint8_t *l1_table
= NULL
;
573 size_t l1_size
= header
.cluster_size
* header
.table_size
;
574 Error
*local_err
= NULL
;
576 BlockDriverState
*bs
;
578 ret
= bdrv_create_file(filename
, opts
, &local_err
);
580 error_propagate(errp
, local_err
);
585 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
,
586 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_PROTOCOL
, NULL
,
589 error_propagate(errp
, local_err
);
593 /* File must start empty and grow, check truncate is supported */
594 ret
= bdrv_truncate(bs
, 0);
600 header
.features
|= QED_F_BACKING_FILE
;
601 header
.backing_filename_offset
= sizeof(le_header
);
602 header
.backing_filename_size
= strlen(backing_file
);
604 if (qed_fmt_is_raw(backing_fmt
)) {
605 header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
609 qed_header_cpu_to_le(&header
, &le_header
);
610 ret
= bdrv_pwrite(bs
, 0, &le_header
, sizeof(le_header
));
614 ret
= bdrv_pwrite(bs
, sizeof(le_header
), backing_file
,
615 header
.backing_filename_size
);
620 l1_table
= g_malloc0(l1_size
);
621 ret
= bdrv_pwrite(bs
, header
.l1_table_offset
, l1_table
, l1_size
);
626 ret
= 0; /* success */
633 static int bdrv_qed_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
635 uint64_t image_size
= 0;
636 uint32_t cluster_size
= QED_DEFAULT_CLUSTER_SIZE
;
637 uint32_t table_size
= QED_DEFAULT_TABLE_SIZE
;
638 char *backing_file
= NULL
;
639 char *backing_fmt
= NULL
;
642 image_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
644 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
645 backing_fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FMT
);
646 cluster_size
= qemu_opt_get_size_del(opts
,
647 BLOCK_OPT_CLUSTER_SIZE
,
648 QED_DEFAULT_CLUSTER_SIZE
);
649 table_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_TABLE_SIZE
,
650 QED_DEFAULT_TABLE_SIZE
);
652 if (!qed_is_cluster_size_valid(cluster_size
)) {
653 error_setg(errp
, "QED cluster size must be within range [%u, %u] "
655 QED_MIN_CLUSTER_SIZE
, QED_MAX_CLUSTER_SIZE
);
659 if (!qed_is_table_size_valid(table_size
)) {
660 error_setg(errp
, "QED table size must be within range [%u, %u] "
662 QED_MIN_TABLE_SIZE
, QED_MAX_TABLE_SIZE
);
666 if (!qed_is_image_size_valid(image_size
, cluster_size
, table_size
)) {
667 error_setg(errp
, "QED image size must be a non-zero multiple of "
668 "cluster size and less than %" PRIu64
" bytes",
669 qed_max_image_size(cluster_size
, table_size
));
674 ret
= qed_create(filename
, cluster_size
, image_size
, table_size
,
675 backing_file
, backing_fmt
, opts
, errp
);
678 g_free(backing_file
);
684 BlockDriverState
*bs
;
691 static void qed_is_allocated_cb(void *opaque
, int ret
, uint64_t offset
, size_t len
)
693 QEDIsAllocatedCB
*cb
= opaque
;
694 BDRVQEDState
*s
= cb
->bs
->opaque
;
695 *cb
->pnum
= len
/ BDRV_SECTOR_SIZE
;
697 case QED_CLUSTER_FOUND
:
698 offset
|= qed_offset_into_cluster(s
, cb
->pos
);
699 cb
->status
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| offset
;
701 case QED_CLUSTER_ZERO
:
702 cb
->status
= BDRV_BLOCK_ZERO
;
715 qemu_coroutine_enter(cb
->co
, NULL
);
719 static int64_t coroutine_fn
bdrv_qed_co_get_block_status(BlockDriverState
*bs
,
721 int nb_sectors
, int *pnum
)
723 BDRVQEDState
*s
= bs
->opaque
;
724 size_t len
= (size_t)nb_sectors
* BDRV_SECTOR_SIZE
;
725 QEDIsAllocatedCB cb
= {
727 .pos
= (uint64_t)sector_num
* BDRV_SECTOR_SIZE
,
728 .status
= BDRV_BLOCK_OFFSET_MASK
,
731 QEDRequest request
= { .l2_table
= NULL
};
733 qed_find_cluster(s
, &request
, cb
.pos
, len
, qed_is_allocated_cb
, &cb
);
735 /* Now sleep if the callback wasn't invoked immediately */
736 while (cb
.status
== BDRV_BLOCK_OFFSET_MASK
) {
737 cb
.co
= qemu_coroutine_self();
738 qemu_coroutine_yield();
741 qed_unref_l2_cache_entry(request
.l2_table
);
746 static BDRVQEDState
*acb_to_s(QEDAIOCB
*acb
)
748 return acb
->common
.bs
->opaque
;
752 * Read from the backing file or zero-fill if no backing file
755 * @pos: Byte position in device
756 * @qiov: Destination I/O vector
757 * @backing_qiov: Possibly shortened copy of qiov, to be allocated here
758 * @cb: Completion function
759 * @opaque: User data for completion function
761 * This function reads qiov->size bytes starting at pos from the backing file.
762 * If there is no backing file then zeroes are read.
764 static void qed_read_backing_file(BDRVQEDState
*s
, uint64_t pos
,
766 QEMUIOVector
**backing_qiov
,
767 BlockCompletionFunc
*cb
, void *opaque
)
769 uint64_t backing_length
= 0;
772 /* If there is a backing file, get its length. Treat the absence of a
773 * backing file like a zero length backing file.
775 if (s
->bs
->backing_hd
) {
776 int64_t l
= bdrv_getlength(s
->bs
->backing_hd
);
784 /* Zero all sectors if reading beyond the end of the backing file */
785 if (pos
>= backing_length
||
786 pos
+ qiov
->size
> backing_length
) {
787 qemu_iovec_memset(qiov
, 0, 0, qiov
->size
);
790 /* Complete now if there are no backing file sectors to read */
791 if (pos
>= backing_length
) {
796 /* If the read straddles the end of the backing file, shorten it */
797 size
= MIN((uint64_t)backing_length
- pos
, qiov
->size
);
799 assert(*backing_qiov
== NULL
);
800 *backing_qiov
= g_new(QEMUIOVector
, 1);
801 qemu_iovec_init(*backing_qiov
, qiov
->niov
);
802 qemu_iovec_concat(*backing_qiov
, qiov
, 0, size
);
804 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_READ_BACKING_AIO
);
805 bdrv_aio_readv(s
->bs
->backing_hd
, pos
/ BDRV_SECTOR_SIZE
,
806 *backing_qiov
, size
/ BDRV_SECTOR_SIZE
, cb
, opaque
);
813 QEMUIOVector
*backing_qiov
;
816 } CopyFromBackingFileCB
;
818 static void qed_copy_from_backing_file_cb(void *opaque
, int ret
)
820 CopyFromBackingFileCB
*copy_cb
= opaque
;
821 qemu_vfree(copy_cb
->iov
.iov_base
);
822 gencb_complete(©_cb
->gencb
, ret
);
825 static void qed_copy_from_backing_file_write(void *opaque
, int ret
)
827 CopyFromBackingFileCB
*copy_cb
= opaque
;
828 BDRVQEDState
*s
= copy_cb
->s
;
830 if (copy_cb
->backing_qiov
) {
831 qemu_iovec_destroy(copy_cb
->backing_qiov
);
832 g_free(copy_cb
->backing_qiov
);
833 copy_cb
->backing_qiov
= NULL
;
837 qed_copy_from_backing_file_cb(copy_cb
, ret
);
841 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_COW_WRITE
);
842 bdrv_aio_writev(s
->bs
->file
, copy_cb
->offset
/ BDRV_SECTOR_SIZE
,
843 ©_cb
->qiov
, copy_cb
->qiov
.size
/ BDRV_SECTOR_SIZE
,
844 qed_copy_from_backing_file_cb
, copy_cb
);
848 * Copy data from backing file into the image
851 * @pos: Byte position in device
852 * @len: Number of bytes
853 * @offset: Byte offset in image file
854 * @cb: Completion function
855 * @opaque: User data for completion function
857 static void qed_copy_from_backing_file(BDRVQEDState
*s
, uint64_t pos
,
858 uint64_t len
, uint64_t offset
,
859 BlockCompletionFunc
*cb
,
862 CopyFromBackingFileCB
*copy_cb
;
864 /* Skip copy entirely if there is no work to do */
870 copy_cb
= gencb_alloc(sizeof(*copy_cb
), cb
, opaque
);
872 copy_cb
->offset
= offset
;
873 copy_cb
->backing_qiov
= NULL
;
874 copy_cb
->iov
.iov_base
= qemu_blockalign(s
->bs
, len
);
875 copy_cb
->iov
.iov_len
= len
;
876 qemu_iovec_init_external(©_cb
->qiov
, ©_cb
->iov
, 1);
878 qed_read_backing_file(s
, pos
, ©_cb
->qiov
, ©_cb
->backing_qiov
,
879 qed_copy_from_backing_file_write
, copy_cb
);
883 * Link one or more contiguous clusters into a table
887 * @index: First cluster index
888 * @n: Number of contiguous clusters
889 * @cluster: First cluster offset
891 * The cluster offset may be an allocated byte offset in the image file, the
892 * zero cluster marker, or the unallocated cluster marker.
894 static void qed_update_l2_table(BDRVQEDState
*s
, QEDTable
*table
, int index
,
895 unsigned int n
, uint64_t cluster
)
898 for (i
= index
; i
< index
+ n
; i
++) {
899 table
->offsets
[i
] = cluster
;
900 if (!qed_offset_is_unalloc_cluster(cluster
) &&
901 !qed_offset_is_zero_cluster(cluster
)) {
902 cluster
+= s
->header
.cluster_size
;
907 static void qed_aio_complete_bh(void *opaque
)
909 QEDAIOCB
*acb
= opaque
;
910 BlockCompletionFunc
*cb
= acb
->common
.cb
;
911 void *user_opaque
= acb
->common
.opaque
;
912 int ret
= acb
->bh_ret
;
914 qemu_bh_delete(acb
->bh
);
917 /* Invoke callback */
918 cb(user_opaque
, ret
);
921 static void qed_aio_complete(QEDAIOCB
*acb
, int ret
)
923 BDRVQEDState
*s
= acb_to_s(acb
);
925 trace_qed_aio_complete(s
, acb
, ret
);
928 qemu_iovec_destroy(&acb
->cur_qiov
);
929 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
931 /* Free the buffer we may have allocated for zero writes */
932 if (acb
->flags
& QED_AIOCB_ZERO
) {
933 qemu_vfree(acb
->qiov
->iov
[0].iov_base
);
934 acb
->qiov
->iov
[0].iov_base
= NULL
;
937 /* Arrange for a bh to invoke the completion function */
939 acb
->bh
= aio_bh_new(bdrv_get_aio_context(acb
->common
.bs
),
940 qed_aio_complete_bh
, acb
);
941 qemu_bh_schedule(acb
->bh
);
943 /* Start next allocating write request waiting behind this one. Note that
944 * requests enqueue themselves when they first hit an unallocated cluster
945 * but they wait until the entire request is finished before waking up the
946 * next request in the queue. This ensures that we don't cycle through
947 * requests multiple times but rather finish one at a time completely.
949 if (acb
== QSIMPLEQ_FIRST(&s
->allocating_write_reqs
)) {
950 QSIMPLEQ_REMOVE_HEAD(&s
->allocating_write_reqs
, next
);
951 acb
= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
);
953 qed_aio_next_io(acb
, 0);
954 } else if (s
->header
.features
& QED_F_NEED_CHECK
) {
955 qed_start_need_check_timer(s
);
961 * Commit the current L2 table to the cache
963 static void qed_commit_l2_update(void *opaque
, int ret
)
965 QEDAIOCB
*acb
= opaque
;
966 BDRVQEDState
*s
= acb_to_s(acb
);
967 CachedL2Table
*l2_table
= acb
->request
.l2_table
;
968 uint64_t l2_offset
= l2_table
->offset
;
970 qed_commit_l2_cache_entry(&s
->l2_cache
, l2_table
);
972 /* This is guaranteed to succeed because we just committed the entry to the
975 acb
->request
.l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, l2_offset
);
976 assert(acb
->request
.l2_table
!= NULL
);
978 qed_aio_next_io(opaque
, ret
);
982 * Update L1 table with new L2 table offset and write it out
984 static void qed_aio_write_l1_update(void *opaque
, int ret
)
986 QEDAIOCB
*acb
= opaque
;
987 BDRVQEDState
*s
= acb_to_s(acb
);
991 qed_aio_complete(acb
, ret
);
995 index
= qed_l1_index(s
, acb
->cur_pos
);
996 s
->l1_table
->offsets
[index
] = acb
->request
.l2_table
->offset
;
998 qed_write_l1_table(s
, index
, 1, qed_commit_l2_update
, acb
);
1002 * Update L2 table with new cluster offsets and write them out
1004 static void qed_aio_write_l2_update(QEDAIOCB
*acb
, int ret
, uint64_t offset
)
1006 BDRVQEDState
*s
= acb_to_s(acb
);
1007 bool need_alloc
= acb
->find_cluster_ret
== QED_CLUSTER_L1
;
1015 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
1016 acb
->request
.l2_table
= qed_new_l2_table(s
);
1019 index
= qed_l2_index(s
, acb
->cur_pos
);
1020 qed_update_l2_table(s
, acb
->request
.l2_table
->table
, index
, acb
->cur_nclusters
,
1024 /* Write out the whole new L2 table */
1025 qed_write_l2_table(s
, &acb
->request
, 0, s
->table_nelems
, true,
1026 qed_aio_write_l1_update
, acb
);
1028 /* Write out only the updated part of the L2 table */
1029 qed_write_l2_table(s
, &acb
->request
, index
, acb
->cur_nclusters
, false,
1030 qed_aio_next_io
, acb
);
1035 qed_aio_complete(acb
, ret
);
1038 static void qed_aio_write_l2_update_cb(void *opaque
, int ret
)
1040 QEDAIOCB
*acb
= opaque
;
1041 qed_aio_write_l2_update(acb
, ret
, acb
->cur_cluster
);
1045 * Flush new data clusters before updating the L2 table
1047 * This flush is necessary when a backing file is in use. A crash during an
1048 * allocating write could result in empty clusters in the image. If the write
1049 * only touched a subregion of the cluster, then backing image sectors have
1050 * been lost in the untouched region. The solution is to flush after writing a
1051 * new data cluster and before updating the L2 table.
1053 static void qed_aio_write_flush_before_l2_update(void *opaque
, int ret
)
1055 QEDAIOCB
*acb
= opaque
;
1056 BDRVQEDState
*s
= acb_to_s(acb
);
1058 if (!bdrv_aio_flush(s
->bs
->file
, qed_aio_write_l2_update_cb
, opaque
)) {
1059 qed_aio_complete(acb
, -EIO
);
1064 * Write data to the image file
1066 static void qed_aio_write_main(void *opaque
, int ret
)
1068 QEDAIOCB
*acb
= opaque
;
1069 BDRVQEDState
*s
= acb_to_s(acb
);
1070 uint64_t offset
= acb
->cur_cluster
+
1071 qed_offset_into_cluster(s
, acb
->cur_pos
);
1072 BlockCompletionFunc
*next_fn
;
1074 trace_qed_aio_write_main(s
, acb
, ret
, offset
, acb
->cur_qiov
.size
);
1077 qed_aio_complete(acb
, ret
);
1081 if (acb
->find_cluster_ret
== QED_CLUSTER_FOUND
) {
1082 next_fn
= qed_aio_next_io
;
1084 if (s
->bs
->backing_hd
) {
1085 next_fn
= qed_aio_write_flush_before_l2_update
;
1087 next_fn
= qed_aio_write_l2_update_cb
;
1091 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_WRITE_AIO
);
1092 bdrv_aio_writev(s
->bs
->file
, offset
/ BDRV_SECTOR_SIZE
,
1093 &acb
->cur_qiov
, acb
->cur_qiov
.size
/ BDRV_SECTOR_SIZE
,
1098 * Populate back untouched region of new data cluster
1100 static void qed_aio_write_postfill(void *opaque
, int ret
)
1102 QEDAIOCB
*acb
= opaque
;
1103 BDRVQEDState
*s
= acb_to_s(acb
);
1104 uint64_t start
= acb
->cur_pos
+ acb
->cur_qiov
.size
;
1106 qed_start_of_cluster(s
, start
+ s
->header
.cluster_size
- 1) - start
;
1107 uint64_t offset
= acb
->cur_cluster
+
1108 qed_offset_into_cluster(s
, acb
->cur_pos
) +
1112 qed_aio_complete(acb
, ret
);
1116 trace_qed_aio_write_postfill(s
, acb
, start
, len
, offset
);
1117 qed_copy_from_backing_file(s
, start
, len
, offset
,
1118 qed_aio_write_main
, acb
);
1122 * Populate front untouched region of new data cluster
1124 static void qed_aio_write_prefill(void *opaque
, int ret
)
1126 QEDAIOCB
*acb
= opaque
;
1127 BDRVQEDState
*s
= acb_to_s(acb
);
1128 uint64_t start
= qed_start_of_cluster(s
, acb
->cur_pos
);
1129 uint64_t len
= qed_offset_into_cluster(s
, acb
->cur_pos
);
1131 trace_qed_aio_write_prefill(s
, acb
, start
, len
, acb
->cur_cluster
);
1132 qed_copy_from_backing_file(s
, start
, len
, acb
->cur_cluster
,
1133 qed_aio_write_postfill
, acb
);
1137 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1139 static bool qed_should_set_need_check(BDRVQEDState
*s
)
1141 /* The flush before L2 update path ensures consistency */
1142 if (s
->bs
->backing_hd
) {
1146 return !(s
->header
.features
& QED_F_NEED_CHECK
);
1149 static void qed_aio_write_zero_cluster(void *opaque
, int ret
)
1151 QEDAIOCB
*acb
= opaque
;
1154 qed_aio_complete(acb
, ret
);
1158 qed_aio_write_l2_update(acb
, 0, 1);
1162 * Write new data cluster
1164 * @acb: Write request
1165 * @len: Length in bytes
1167 * This path is taken when writing to previously unallocated clusters.
1169 static void qed_aio_write_alloc(QEDAIOCB
*acb
, size_t len
)
1171 BDRVQEDState
*s
= acb_to_s(acb
);
1172 BlockCompletionFunc
*cb
;
1174 /* Cancel timer when the first allocating request comes in */
1175 if (QSIMPLEQ_EMPTY(&s
->allocating_write_reqs
)) {
1176 qed_cancel_need_check_timer(s
);
1179 /* Freeze this request if another allocating write is in progress */
1180 if (acb
!= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
)) {
1181 QSIMPLEQ_INSERT_TAIL(&s
->allocating_write_reqs
, acb
, next
);
1183 if (acb
!= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
) ||
1184 s
->allocating_write_reqs_plugged
) {
1185 return; /* wait for existing request to finish */
1188 acb
->cur_nclusters
= qed_bytes_to_clusters(s
,
1189 qed_offset_into_cluster(s
, acb
->cur_pos
) + len
);
1190 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1192 if (acb
->flags
& QED_AIOCB_ZERO
) {
1193 /* Skip ahead if the clusters are already zero */
1194 if (acb
->find_cluster_ret
== QED_CLUSTER_ZERO
) {
1195 qed_aio_next_io(acb
, 0);
1199 cb
= qed_aio_write_zero_cluster
;
1201 cb
= qed_aio_write_prefill
;
1202 acb
->cur_cluster
= qed_alloc_clusters(s
, acb
->cur_nclusters
);
1205 if (qed_should_set_need_check(s
)) {
1206 s
->header
.features
|= QED_F_NEED_CHECK
;
1207 qed_write_header(s
, cb
, acb
);
1214 * Write data cluster in place
1216 * @acb: Write request
1217 * @offset: Cluster offset in bytes
1218 * @len: Length in bytes
1220 * This path is taken when writing to already allocated clusters.
1222 static void qed_aio_write_inplace(QEDAIOCB
*acb
, uint64_t offset
, size_t len
)
1224 /* Allocate buffer for zero writes */
1225 if (acb
->flags
& QED_AIOCB_ZERO
) {
1226 struct iovec
*iov
= acb
->qiov
->iov
;
1228 if (!iov
->iov_base
) {
1229 iov
->iov_base
= qemu_try_blockalign(acb
->common
.bs
, iov
->iov_len
);
1230 if (iov
->iov_base
== NULL
) {
1231 qed_aio_complete(acb
, -ENOMEM
);
1234 memset(iov
->iov_base
, 0, iov
->iov_len
);
1238 /* Calculate the I/O vector */
1239 acb
->cur_cluster
= offset
;
1240 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1242 /* Do the actual write */
1243 qed_aio_write_main(acb
, 0);
1247 * Write data cluster
1249 * @opaque: Write request
1250 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1252 * @offset: Cluster offset in bytes
1253 * @len: Length in bytes
1255 * Callback from qed_find_cluster().
1257 static void qed_aio_write_data(void *opaque
, int ret
,
1258 uint64_t offset
, size_t len
)
1260 QEDAIOCB
*acb
= opaque
;
1262 trace_qed_aio_write_data(acb_to_s(acb
), acb
, ret
, offset
, len
);
1264 acb
->find_cluster_ret
= ret
;
1267 case QED_CLUSTER_FOUND
:
1268 qed_aio_write_inplace(acb
, offset
, len
);
1271 case QED_CLUSTER_L2
:
1272 case QED_CLUSTER_L1
:
1273 case QED_CLUSTER_ZERO
:
1274 qed_aio_write_alloc(acb
, len
);
1278 qed_aio_complete(acb
, ret
);
1286 * @opaque: Read request
1287 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1289 * @offset: Cluster offset in bytes
1290 * @len: Length in bytes
1292 * Callback from qed_find_cluster().
1294 static void qed_aio_read_data(void *opaque
, int ret
,
1295 uint64_t offset
, size_t len
)
1297 QEDAIOCB
*acb
= opaque
;
1298 BDRVQEDState
*s
= acb_to_s(acb
);
1299 BlockDriverState
*bs
= acb
->common
.bs
;
1301 /* Adjust offset into cluster */
1302 offset
+= qed_offset_into_cluster(s
, acb
->cur_pos
);
1304 trace_qed_aio_read_data(s
, acb
, ret
, offset
, len
);
1310 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1312 /* Handle zero cluster and backing file reads */
1313 if (ret
== QED_CLUSTER_ZERO
) {
1314 qemu_iovec_memset(&acb
->cur_qiov
, 0, 0, acb
->cur_qiov
.size
);
1315 qed_aio_next_io(acb
, 0);
1317 } else if (ret
!= QED_CLUSTER_FOUND
) {
1318 qed_read_backing_file(s
, acb
->cur_pos
, &acb
->cur_qiov
,
1319 &acb
->backing_qiov
, qed_aio_next_io
, acb
);
1323 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
1324 bdrv_aio_readv(bs
->file
, offset
/ BDRV_SECTOR_SIZE
,
1325 &acb
->cur_qiov
, acb
->cur_qiov
.size
/ BDRV_SECTOR_SIZE
,
1326 qed_aio_next_io
, acb
);
1330 qed_aio_complete(acb
, ret
);
1334 * Begin next I/O or complete the request
1336 static void qed_aio_next_io(void *opaque
, int ret
)
1338 QEDAIOCB
*acb
= opaque
;
1339 BDRVQEDState
*s
= acb_to_s(acb
);
1340 QEDFindClusterFunc
*io_fn
= (acb
->flags
& QED_AIOCB_WRITE
) ?
1341 qed_aio_write_data
: qed_aio_read_data
;
1343 trace_qed_aio_next_io(s
, acb
, ret
, acb
->cur_pos
+ acb
->cur_qiov
.size
);
1345 if (acb
->backing_qiov
) {
1346 qemu_iovec_destroy(acb
->backing_qiov
);
1347 g_free(acb
->backing_qiov
);
1348 acb
->backing_qiov
= NULL
;
1351 /* Handle I/O error */
1353 qed_aio_complete(acb
, ret
);
1357 acb
->qiov_offset
+= acb
->cur_qiov
.size
;
1358 acb
->cur_pos
+= acb
->cur_qiov
.size
;
1359 qemu_iovec_reset(&acb
->cur_qiov
);
1361 /* Complete request */
1362 if (acb
->cur_pos
>= acb
->end_pos
) {
1363 qed_aio_complete(acb
, 0);
1367 /* Find next cluster and start I/O */
1368 qed_find_cluster(s
, &acb
->request
,
1369 acb
->cur_pos
, acb
->end_pos
- acb
->cur_pos
,
1373 static BlockAIOCB
*qed_aio_setup(BlockDriverState
*bs
,
1375 QEMUIOVector
*qiov
, int nb_sectors
,
1376 BlockCompletionFunc
*cb
,
1377 void *opaque
, int flags
)
1379 QEDAIOCB
*acb
= qemu_aio_get(&qed_aiocb_info
, bs
, cb
, opaque
);
1381 trace_qed_aio_setup(bs
->opaque
, acb
, sector_num
, nb_sectors
,
1386 acb
->qiov_offset
= 0;
1387 acb
->cur_pos
= (uint64_t)sector_num
* BDRV_SECTOR_SIZE
;
1388 acb
->end_pos
= acb
->cur_pos
+ nb_sectors
* BDRV_SECTOR_SIZE
;
1389 acb
->backing_qiov
= NULL
;
1390 acb
->request
.l2_table
= NULL
;
1391 qemu_iovec_init(&acb
->cur_qiov
, qiov
->niov
);
1394 qed_aio_next_io(acb
, 0);
1395 return &acb
->common
;
1398 static BlockAIOCB
*bdrv_qed_aio_readv(BlockDriverState
*bs
,
1400 QEMUIOVector
*qiov
, int nb_sectors
,
1401 BlockCompletionFunc
*cb
,
1404 return qed_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1407 static BlockAIOCB
*bdrv_qed_aio_writev(BlockDriverState
*bs
,
1409 QEMUIOVector
*qiov
, int nb_sectors
,
1410 BlockCompletionFunc
*cb
,
1413 return qed_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
,
1414 opaque
, QED_AIOCB_WRITE
);
1423 static void coroutine_fn
qed_co_write_zeroes_cb(void *opaque
, int ret
)
1425 QEDWriteZeroesCB
*cb
= opaque
;
1430 qemu_coroutine_enter(cb
->co
, NULL
);
1434 static int coroutine_fn
bdrv_qed_co_write_zeroes(BlockDriverState
*bs
,
1437 BdrvRequestFlags flags
)
1439 BlockAIOCB
*blockacb
;
1440 BDRVQEDState
*s
= bs
->opaque
;
1441 QEDWriteZeroesCB cb
= { .done
= false };
1445 /* Refuse if there are untouched backing file sectors */
1446 if (bs
->backing_hd
) {
1447 if (qed_offset_into_cluster(s
, sector_num
* BDRV_SECTOR_SIZE
) != 0) {
1450 if (qed_offset_into_cluster(s
, nb_sectors
* BDRV_SECTOR_SIZE
) != 0) {
1455 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1456 * then it will be allocated during request processing.
1458 iov
.iov_base
= NULL
,
1459 iov
.iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
1461 qemu_iovec_init_external(&qiov
, &iov
, 1);
1462 blockacb
= qed_aio_setup(bs
, sector_num
, &qiov
, nb_sectors
,
1463 qed_co_write_zeroes_cb
, &cb
,
1464 QED_AIOCB_WRITE
| QED_AIOCB_ZERO
);
1469 cb
.co
= qemu_coroutine_self();
1470 qemu_coroutine_yield();
1476 static int bdrv_qed_truncate(BlockDriverState
*bs
, int64_t offset
)
1478 BDRVQEDState
*s
= bs
->opaque
;
1479 uint64_t old_image_size
;
1482 if (!qed_is_image_size_valid(offset
, s
->header
.cluster_size
,
1483 s
->header
.table_size
)) {
1487 /* Shrinking is currently not supported */
1488 if ((uint64_t)offset
< s
->header
.image_size
) {
1492 old_image_size
= s
->header
.image_size
;
1493 s
->header
.image_size
= offset
;
1494 ret
= qed_write_header_sync(s
);
1496 s
->header
.image_size
= old_image_size
;
1501 static int64_t bdrv_qed_getlength(BlockDriverState
*bs
)
1503 BDRVQEDState
*s
= bs
->opaque
;
1504 return s
->header
.image_size
;
1507 static int bdrv_qed_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1509 BDRVQEDState
*s
= bs
->opaque
;
1511 memset(bdi
, 0, sizeof(*bdi
));
1512 bdi
->cluster_size
= s
->header
.cluster_size
;
1513 bdi
->is_dirty
= s
->header
.features
& QED_F_NEED_CHECK
;
1514 bdi
->unallocated_blocks_are_zero
= true;
1515 bdi
->can_write_zeroes_with_unmap
= true;
1519 static int bdrv_qed_change_backing_file(BlockDriverState
*bs
,
1520 const char *backing_file
,
1521 const char *backing_fmt
)
1523 BDRVQEDState
*s
= bs
->opaque
;
1524 QEDHeader new_header
, le_header
;
1526 size_t buffer_len
, backing_file_len
;
1529 /* Refuse to set backing filename if unknown compat feature bits are
1530 * active. If the image uses an unknown compat feature then we may not
1531 * know the layout of data following the header structure and cannot safely
1534 if (backing_file
&& (s
->header
.compat_features
&
1535 ~QED_COMPAT_FEATURE_MASK
)) {
1539 memcpy(&new_header
, &s
->header
, sizeof(new_header
));
1541 new_header
.features
&= ~(QED_F_BACKING_FILE
|
1542 QED_F_BACKING_FORMAT_NO_PROBE
);
1544 /* Adjust feature flags */
1546 new_header
.features
|= QED_F_BACKING_FILE
;
1548 if (qed_fmt_is_raw(backing_fmt
)) {
1549 new_header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
1553 /* Calculate new header size */
1554 backing_file_len
= 0;
1557 backing_file_len
= strlen(backing_file
);
1560 buffer_len
= sizeof(new_header
);
1561 new_header
.backing_filename_offset
= buffer_len
;
1562 new_header
.backing_filename_size
= backing_file_len
;
1563 buffer_len
+= backing_file_len
;
1565 /* Make sure we can rewrite header without failing */
1566 if (buffer_len
> new_header
.header_size
* new_header
.cluster_size
) {
1570 /* Prepare new header */
1571 buffer
= g_malloc(buffer_len
);
1573 qed_header_cpu_to_le(&new_header
, &le_header
);
1574 memcpy(buffer
, &le_header
, sizeof(le_header
));
1575 buffer_len
= sizeof(le_header
);
1578 memcpy(buffer
+ buffer_len
, backing_file
, backing_file_len
);
1579 buffer_len
+= backing_file_len
;
1582 /* Write new header */
1583 ret
= bdrv_pwrite_sync(bs
->file
, 0, buffer
, buffer_len
);
1586 memcpy(&s
->header
, &new_header
, sizeof(new_header
));
1591 static void bdrv_qed_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
1593 BDRVQEDState
*s
= bs
->opaque
;
1594 Error
*local_err
= NULL
;
1599 bdrv_invalidate_cache(bs
->file
, &local_err
);
1601 error_propagate(errp
, local_err
);
1605 memset(s
, 0, sizeof(BDRVQEDState
));
1606 ret
= bdrv_qed_open(bs
, NULL
, bs
->open_flags
, &local_err
);
1608 error_setg(errp
, "Could not reopen qed layer: %s",
1609 error_get_pretty(local_err
));
1610 error_free(local_err
);
1612 } else if (ret
< 0) {
1613 error_setg_errno(errp
, -ret
, "Could not reopen qed layer");
1618 static int bdrv_qed_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
1621 BDRVQEDState
*s
= bs
->opaque
;
1623 return qed_check(s
, result
, !!fix
);
1626 static QemuOptsList qed_create_opts
= {
1627 .name
= "qed-create-opts",
1628 .head
= QTAILQ_HEAD_INITIALIZER(qed_create_opts
.head
),
1631 .name
= BLOCK_OPT_SIZE
,
1632 .type
= QEMU_OPT_SIZE
,
1633 .help
= "Virtual disk size"
1636 .name
= BLOCK_OPT_BACKING_FILE
,
1637 .type
= QEMU_OPT_STRING
,
1638 .help
= "File name of a base image"
1641 .name
= BLOCK_OPT_BACKING_FMT
,
1642 .type
= QEMU_OPT_STRING
,
1643 .help
= "Image format of the base image"
1646 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1647 .type
= QEMU_OPT_SIZE
,
1648 .help
= "Cluster size (in bytes)",
1649 .def_value_str
= stringify(QED_DEFAULT_CLUSTER_SIZE
)
1652 .name
= BLOCK_OPT_TABLE_SIZE
,
1653 .type
= QEMU_OPT_SIZE
,
1654 .help
= "L1/L2 table size (in clusters)"
1656 { /* end of list */ }
1660 static BlockDriver bdrv_qed
= {
1661 .format_name
= "qed",
1662 .instance_size
= sizeof(BDRVQEDState
),
1663 .create_opts
= &qed_create_opts
,
1664 .supports_backing
= true,
1666 .bdrv_probe
= bdrv_qed_probe
,
1667 .bdrv_rebind
= bdrv_qed_rebind
,
1668 .bdrv_open
= bdrv_qed_open
,
1669 .bdrv_close
= bdrv_qed_close
,
1670 .bdrv_reopen_prepare
= bdrv_qed_reopen_prepare
,
1671 .bdrv_create
= bdrv_qed_create
,
1672 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1673 .bdrv_co_get_block_status
= bdrv_qed_co_get_block_status
,
1674 .bdrv_aio_readv
= bdrv_qed_aio_readv
,
1675 .bdrv_aio_writev
= bdrv_qed_aio_writev
,
1676 .bdrv_co_write_zeroes
= bdrv_qed_co_write_zeroes
,
1677 .bdrv_truncate
= bdrv_qed_truncate
,
1678 .bdrv_getlength
= bdrv_qed_getlength
,
1679 .bdrv_get_info
= bdrv_qed_get_info
,
1680 .bdrv_refresh_limits
= bdrv_qed_refresh_limits
,
1681 .bdrv_change_backing_file
= bdrv_qed_change_backing_file
,
1682 .bdrv_invalidate_cache
= bdrv_qed_invalidate_cache
,
1683 .bdrv_check
= bdrv_qed_check
,
1684 .bdrv_detach_aio_context
= bdrv_qed_detach_aio_context
,
1685 .bdrv_attach_aio_context
= bdrv_qed_attach_aio_context
,
1688 static void bdrv_qed_init(void)
1690 bdrv_register(&bdrv_qed
);
1693 block_init(bdrv_qed_init
);