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 "qapi/error.h"
17 #include "qemu/timer.h"
18 #include "qemu/bswap.h"
21 #include "qapi/qmp/qerror.h"
22 #include "migration/migration.h"
23 #include "sysemu/block-backend.h"
25 static const AIOCBInfo qed_aiocb_info
= {
26 .aiocb_size
= sizeof(QEDAIOCB
),
29 static int bdrv_qed_probe(const uint8_t *buf
, int buf_size
,
32 const QEDHeader
*header
= (const QEDHeader
*)buf
;
34 if (buf_size
< sizeof(*header
)) {
37 if (le32_to_cpu(header
->magic
) != QED_MAGIC
) {
44 * Check whether an image format is raw
46 * @fmt: Backing file format, may be NULL
48 static bool qed_fmt_is_raw(const char *fmt
)
50 return fmt
&& strcmp(fmt
, "raw") == 0;
53 static void qed_header_le_to_cpu(const QEDHeader
*le
, QEDHeader
*cpu
)
55 cpu
->magic
= le32_to_cpu(le
->magic
);
56 cpu
->cluster_size
= le32_to_cpu(le
->cluster_size
);
57 cpu
->table_size
= le32_to_cpu(le
->table_size
);
58 cpu
->header_size
= le32_to_cpu(le
->header_size
);
59 cpu
->features
= le64_to_cpu(le
->features
);
60 cpu
->compat_features
= le64_to_cpu(le
->compat_features
);
61 cpu
->autoclear_features
= le64_to_cpu(le
->autoclear_features
);
62 cpu
->l1_table_offset
= le64_to_cpu(le
->l1_table_offset
);
63 cpu
->image_size
= le64_to_cpu(le
->image_size
);
64 cpu
->backing_filename_offset
= le32_to_cpu(le
->backing_filename_offset
);
65 cpu
->backing_filename_size
= le32_to_cpu(le
->backing_filename_size
);
68 static void qed_header_cpu_to_le(const QEDHeader
*cpu
, QEDHeader
*le
)
70 le
->magic
= cpu_to_le32(cpu
->magic
);
71 le
->cluster_size
= cpu_to_le32(cpu
->cluster_size
);
72 le
->table_size
= cpu_to_le32(cpu
->table_size
);
73 le
->header_size
= cpu_to_le32(cpu
->header_size
);
74 le
->features
= cpu_to_le64(cpu
->features
);
75 le
->compat_features
= cpu_to_le64(cpu
->compat_features
);
76 le
->autoclear_features
= cpu_to_le64(cpu
->autoclear_features
);
77 le
->l1_table_offset
= cpu_to_le64(cpu
->l1_table_offset
);
78 le
->image_size
= cpu_to_le64(cpu
->image_size
);
79 le
->backing_filename_offset
= cpu_to_le32(cpu
->backing_filename_offset
);
80 le
->backing_filename_size
= cpu_to_le32(cpu
->backing_filename_size
);
83 int qed_write_header_sync(BDRVQEDState
*s
)
88 qed_header_cpu_to_le(&s
->header
, &le
);
89 ret
= bdrv_pwrite(s
->bs
->file
, 0, &le
, sizeof(le
));
90 if (ret
!= sizeof(le
)) {
105 static void qed_write_header_cb(void *opaque
, int ret
)
107 QEDWriteHeaderCB
*write_header_cb
= opaque
;
109 qemu_vfree(write_header_cb
->buf
);
110 gencb_complete(write_header_cb
, ret
);
113 static void qed_write_header_read_cb(void *opaque
, int ret
)
115 QEDWriteHeaderCB
*write_header_cb
= opaque
;
116 BDRVQEDState
*s
= write_header_cb
->s
;
119 qed_write_header_cb(write_header_cb
, ret
);
124 qed_header_cpu_to_le(&s
->header
, (QEDHeader
*)write_header_cb
->buf
);
126 bdrv_aio_writev(s
->bs
->file
, 0, &write_header_cb
->qiov
,
127 write_header_cb
->nsectors
, qed_write_header_cb
,
132 * Update header in-place (does not rewrite backing filename or other strings)
134 * This function only updates known header fields in-place and does not affect
135 * extra data after the QED header.
137 static void qed_write_header(BDRVQEDState
*s
, BlockCompletionFunc cb
,
140 /* We must write full sectors for O_DIRECT but cannot necessarily generate
141 * the data following the header if an unrecognized compat feature is
142 * active. Therefore, first read the sectors containing the header, update
143 * them, and write back.
146 int nsectors
= DIV_ROUND_UP(sizeof(QEDHeader
), BDRV_SECTOR_SIZE
);
147 size_t len
= nsectors
* BDRV_SECTOR_SIZE
;
148 QEDWriteHeaderCB
*write_header_cb
= gencb_alloc(sizeof(*write_header_cb
),
151 write_header_cb
->s
= s
;
152 write_header_cb
->nsectors
= nsectors
;
153 write_header_cb
->buf
= qemu_blockalign(s
->bs
, len
);
154 write_header_cb
->iov
.iov_base
= write_header_cb
->buf
;
155 write_header_cb
->iov
.iov_len
= len
;
156 qemu_iovec_init_external(&write_header_cb
->qiov
, &write_header_cb
->iov
, 1);
158 bdrv_aio_readv(s
->bs
->file
, 0, &write_header_cb
->qiov
, nsectors
,
159 qed_write_header_read_cb
, write_header_cb
);
162 static uint64_t qed_max_image_size(uint32_t cluster_size
, uint32_t table_size
)
164 uint64_t table_entries
;
167 table_entries
= (table_size
* cluster_size
) / sizeof(uint64_t);
168 l2_size
= table_entries
* cluster_size
;
170 return l2_size
* table_entries
;
173 static bool qed_is_cluster_size_valid(uint32_t cluster_size
)
175 if (cluster_size
< QED_MIN_CLUSTER_SIZE
||
176 cluster_size
> QED_MAX_CLUSTER_SIZE
) {
179 if (cluster_size
& (cluster_size
- 1)) {
180 return false; /* not power of 2 */
185 static bool qed_is_table_size_valid(uint32_t table_size
)
187 if (table_size
< QED_MIN_TABLE_SIZE
||
188 table_size
> QED_MAX_TABLE_SIZE
) {
191 if (table_size
& (table_size
- 1)) {
192 return false; /* not power of 2 */
197 static bool qed_is_image_size_valid(uint64_t image_size
, uint32_t cluster_size
,
200 if (image_size
% BDRV_SECTOR_SIZE
!= 0) {
201 return false; /* not multiple of sector size */
203 if (image_size
> qed_max_image_size(cluster_size
, table_size
)) {
204 return false; /* image is too large */
210 * Read a string of known length from the image file
213 * @offset: File offset to start of string, in bytes
214 * @n: String length in bytes
215 * @buf: Destination buffer
216 * @buflen: Destination buffer length in bytes
217 * @ret: 0 on success, -errno on failure
219 * The string is NUL-terminated.
221 static int qed_read_string(BdrvChild
*file
, uint64_t offset
, size_t n
,
222 char *buf
, size_t buflen
)
228 ret
= bdrv_pread(file
, offset
, buf
, n
);
237 * Allocate new clusters
240 * @n: Number of contiguous clusters to allocate
241 * @ret: Offset of first allocated cluster
243 * This function only produces the offset where the new clusters should be
244 * written. It updates BDRVQEDState but does not make any changes to the image
247 static uint64_t qed_alloc_clusters(BDRVQEDState
*s
, unsigned int n
)
249 uint64_t offset
= s
->file_size
;
250 s
->file_size
+= n
* s
->header
.cluster_size
;
254 QEDTable
*qed_alloc_table(BDRVQEDState
*s
)
256 /* Honor O_DIRECT memory alignment requirements */
257 return qemu_blockalign(s
->bs
,
258 s
->header
.cluster_size
* s
->header
.table_size
);
262 * Allocate a new zeroed L2 table
264 static CachedL2Table
*qed_new_l2_table(BDRVQEDState
*s
)
266 CachedL2Table
*l2_table
= qed_alloc_l2_cache_entry(&s
->l2_cache
);
268 l2_table
->table
= qed_alloc_table(s
);
269 l2_table
->offset
= qed_alloc_clusters(s
, s
->header
.table_size
);
271 memset(l2_table
->table
->offsets
, 0,
272 s
->header
.cluster_size
* s
->header
.table_size
);
276 static void qed_aio_next_io(QEDAIOCB
*acb
, int ret
);
278 static void qed_aio_start_io(QEDAIOCB
*acb
)
280 qed_aio_next_io(acb
, 0);
283 static void qed_aio_next_io_cb(void *opaque
, int ret
)
285 QEDAIOCB
*acb
= opaque
;
287 qed_aio_next_io(acb
, ret
);
290 static void qed_plug_allocating_write_reqs(BDRVQEDState
*s
)
292 assert(!s
->allocating_write_reqs_plugged
);
294 s
->allocating_write_reqs_plugged
= true;
297 static void qed_unplug_allocating_write_reqs(BDRVQEDState
*s
)
301 assert(s
->allocating_write_reqs_plugged
);
303 s
->allocating_write_reqs_plugged
= false;
305 acb
= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
);
307 qed_aio_start_io(acb
);
311 static void qed_finish_clear_need_check(void *opaque
, int ret
)
316 static void qed_flush_after_clear_need_check(void *opaque
, int ret
)
318 BDRVQEDState
*s
= opaque
;
320 bdrv_aio_flush(s
->bs
, qed_finish_clear_need_check
, s
);
322 /* No need to wait until flush completes */
323 qed_unplug_allocating_write_reqs(s
);
326 static void qed_clear_need_check(void *opaque
, int ret
)
328 BDRVQEDState
*s
= opaque
;
331 qed_unplug_allocating_write_reqs(s
);
335 s
->header
.features
&= ~QED_F_NEED_CHECK
;
336 qed_write_header(s
, qed_flush_after_clear_need_check
, s
);
339 static void qed_need_check_timer_cb(void *opaque
)
341 BDRVQEDState
*s
= opaque
;
343 /* The timer should only fire when allocating writes have drained */
344 assert(!QSIMPLEQ_FIRST(&s
->allocating_write_reqs
));
346 trace_qed_need_check_timer_cb(s
);
349 qed_plug_allocating_write_reqs(s
);
351 /* Ensure writes are on disk before clearing flag */
352 bdrv_aio_flush(s
->bs
->file
->bs
, qed_clear_need_check
, s
);
356 void qed_acquire(BDRVQEDState
*s
)
358 aio_context_acquire(bdrv_get_aio_context(s
->bs
));
361 void qed_release(BDRVQEDState
*s
)
363 aio_context_release(bdrv_get_aio_context(s
->bs
));
366 static void qed_start_need_check_timer(BDRVQEDState
*s
)
368 trace_qed_start_need_check_timer(s
);
370 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
373 timer_mod(s
->need_check_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
374 NANOSECONDS_PER_SECOND
* QED_NEED_CHECK_TIMEOUT
);
377 /* It's okay to call this multiple times or when no timer is started */
378 static void qed_cancel_need_check_timer(BDRVQEDState
*s
)
380 trace_qed_cancel_need_check_timer(s
);
381 timer_del(s
->need_check_timer
);
384 static void bdrv_qed_detach_aio_context(BlockDriverState
*bs
)
386 BDRVQEDState
*s
= bs
->opaque
;
388 qed_cancel_need_check_timer(s
);
389 timer_free(s
->need_check_timer
);
392 static void bdrv_qed_attach_aio_context(BlockDriverState
*bs
,
393 AioContext
*new_context
)
395 BDRVQEDState
*s
= bs
->opaque
;
397 s
->need_check_timer
= aio_timer_new(new_context
,
398 QEMU_CLOCK_VIRTUAL
, SCALE_NS
,
399 qed_need_check_timer_cb
, s
);
400 if (s
->header
.features
& QED_F_NEED_CHECK
) {
401 qed_start_need_check_timer(s
);
405 static void bdrv_qed_drain(BlockDriverState
*bs
)
407 BDRVQEDState
*s
= bs
->opaque
;
409 /* Fire the timer immediately in order to start doing I/O as soon as the
412 if (s
->need_check_timer
&& timer_pending(s
->need_check_timer
)) {
413 qed_cancel_need_check_timer(s
);
414 qed_need_check_timer_cb(s
);
418 static int bdrv_qed_do_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
421 BDRVQEDState
*s
= bs
->opaque
;
427 QSIMPLEQ_INIT(&s
->allocating_write_reqs
);
429 ret
= bdrv_pread(bs
->file
, 0, &le_header
, sizeof(le_header
));
433 qed_header_le_to_cpu(&le_header
, &s
->header
);
435 if (s
->header
.magic
!= QED_MAGIC
) {
436 error_setg(errp
, "Image not in QED format");
439 if (s
->header
.features
& ~QED_FEATURE_MASK
) {
440 /* image uses unsupported feature bits */
441 error_setg(errp
, "Unsupported QED features: %" PRIx64
,
442 s
->header
.features
& ~QED_FEATURE_MASK
);
445 if (!qed_is_cluster_size_valid(s
->header
.cluster_size
)) {
449 /* Round down file size to the last cluster */
450 file_size
= bdrv_getlength(bs
->file
->bs
);
454 s
->file_size
= qed_start_of_cluster(s
, file_size
);
456 if (!qed_is_table_size_valid(s
->header
.table_size
)) {
459 if (!qed_is_image_size_valid(s
->header
.image_size
,
460 s
->header
.cluster_size
,
461 s
->header
.table_size
)) {
464 if (!qed_check_table_offset(s
, s
->header
.l1_table_offset
)) {
468 s
->table_nelems
= (s
->header
.cluster_size
* s
->header
.table_size
) /
470 s
->l2_shift
= ctz32(s
->header
.cluster_size
);
471 s
->l2_mask
= s
->table_nelems
- 1;
472 s
->l1_shift
= s
->l2_shift
+ ctz32(s
->table_nelems
);
474 /* Header size calculation must not overflow uint32_t */
475 if (s
->header
.header_size
> UINT32_MAX
/ s
->header
.cluster_size
) {
479 if ((s
->header
.features
& QED_F_BACKING_FILE
)) {
480 if ((uint64_t)s
->header
.backing_filename_offset
+
481 s
->header
.backing_filename_size
>
482 s
->header
.cluster_size
* s
->header
.header_size
) {
486 ret
= qed_read_string(bs
->file
, s
->header
.backing_filename_offset
,
487 s
->header
.backing_filename_size
, bs
->backing_file
,
488 sizeof(bs
->backing_file
));
493 if (s
->header
.features
& QED_F_BACKING_FORMAT_NO_PROBE
) {
494 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), "raw");
498 /* Reset unknown autoclear feature bits. This is a backwards
499 * compatibility mechanism that allows images to be opened by older
500 * programs, which "knock out" unknown feature bits. When an image is
501 * opened by a newer program again it can detect that the autoclear
502 * feature is no longer valid.
504 if ((s
->header
.autoclear_features
& ~QED_AUTOCLEAR_FEATURE_MASK
) != 0 &&
505 !bdrv_is_read_only(bs
->file
->bs
) && !(flags
& BDRV_O_INACTIVE
)) {
506 s
->header
.autoclear_features
&= QED_AUTOCLEAR_FEATURE_MASK
;
508 ret
= qed_write_header_sync(s
);
513 /* From here on only known autoclear feature bits are valid */
514 bdrv_flush(bs
->file
->bs
);
517 s
->l1_table
= qed_alloc_table(s
);
518 qed_init_l2_cache(&s
->l2_cache
);
520 ret
= qed_read_l1_table_sync(s
);
525 /* If image was not closed cleanly, check consistency */
526 if (!(flags
& BDRV_O_CHECK
) && (s
->header
.features
& QED_F_NEED_CHECK
)) {
527 /* Read-only images cannot be fixed. There is no risk of corruption
528 * since write operations are not possible. Therefore, allow
529 * potentially inconsistent images to be opened read-only. This can
530 * aid data recovery from an otherwise inconsistent image.
532 if (!bdrv_is_read_only(bs
->file
->bs
) &&
533 !(flags
& BDRV_O_INACTIVE
)) {
534 BdrvCheckResult result
= {0};
536 ret
= qed_check(s
, &result
, true);
543 bdrv_qed_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
547 qed_free_l2_cache(&s
->l2_cache
);
548 qemu_vfree(s
->l1_table
);
553 static int bdrv_qed_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
556 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
562 return bdrv_qed_do_open(bs
, options
, flags
, errp
);
565 static void bdrv_qed_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
567 BDRVQEDState
*s
= bs
->opaque
;
569 bs
->bl
.pwrite_zeroes_alignment
= s
->header
.cluster_size
;
572 /* We have nothing to do for QED reopen, stubs just return
574 static int bdrv_qed_reopen_prepare(BDRVReopenState
*state
,
575 BlockReopenQueue
*queue
, Error
**errp
)
580 static void bdrv_qed_close(BlockDriverState
*bs
)
582 BDRVQEDState
*s
= bs
->opaque
;
584 bdrv_qed_detach_aio_context(bs
);
586 /* Ensure writes reach stable storage */
587 bdrv_flush(bs
->file
->bs
);
589 /* Clean shutdown, no check required on next open */
590 if (s
->header
.features
& QED_F_NEED_CHECK
) {
591 s
->header
.features
&= ~QED_F_NEED_CHECK
;
592 qed_write_header_sync(s
);
595 qed_free_l2_cache(&s
->l2_cache
);
596 qemu_vfree(s
->l1_table
);
599 static int qed_create(const char *filename
, uint32_t cluster_size
,
600 uint64_t image_size
, uint32_t table_size
,
601 const char *backing_file
, const char *backing_fmt
,
602 QemuOpts
*opts
, Error
**errp
)
606 .cluster_size
= cluster_size
,
607 .table_size
= table_size
,
610 .compat_features
= 0,
611 .l1_table_offset
= cluster_size
,
612 .image_size
= image_size
,
615 uint8_t *l1_table
= NULL
;
616 size_t l1_size
= header
.cluster_size
* header
.table_size
;
617 Error
*local_err
= NULL
;
621 ret
= bdrv_create_file(filename
, opts
, &local_err
);
623 error_propagate(errp
, local_err
);
627 blk
= blk_new_open(filename
, NULL
, NULL
,
628 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
631 error_propagate(errp
, local_err
);
635 blk_set_allow_write_beyond_eof(blk
, true);
637 /* File must start empty and grow, check truncate is supported */
638 ret
= blk_truncate(blk
, 0, errp
);
644 header
.features
|= QED_F_BACKING_FILE
;
645 header
.backing_filename_offset
= sizeof(le_header
);
646 header
.backing_filename_size
= strlen(backing_file
);
648 if (qed_fmt_is_raw(backing_fmt
)) {
649 header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
653 qed_header_cpu_to_le(&header
, &le_header
);
654 ret
= blk_pwrite(blk
, 0, &le_header
, sizeof(le_header
), 0);
658 ret
= blk_pwrite(blk
, sizeof(le_header
), backing_file
,
659 header
.backing_filename_size
, 0);
664 l1_table
= g_malloc0(l1_size
);
665 ret
= blk_pwrite(blk
, header
.l1_table_offset
, l1_table
, l1_size
, 0);
670 ret
= 0; /* success */
677 static int bdrv_qed_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
679 uint64_t image_size
= 0;
680 uint32_t cluster_size
= QED_DEFAULT_CLUSTER_SIZE
;
681 uint32_t table_size
= QED_DEFAULT_TABLE_SIZE
;
682 char *backing_file
= NULL
;
683 char *backing_fmt
= NULL
;
686 image_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
688 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
689 backing_fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FMT
);
690 cluster_size
= qemu_opt_get_size_del(opts
,
691 BLOCK_OPT_CLUSTER_SIZE
,
692 QED_DEFAULT_CLUSTER_SIZE
);
693 table_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_TABLE_SIZE
,
694 QED_DEFAULT_TABLE_SIZE
);
696 if (!qed_is_cluster_size_valid(cluster_size
)) {
697 error_setg(errp
, "QED cluster size must be within range [%u, %u] "
699 QED_MIN_CLUSTER_SIZE
, QED_MAX_CLUSTER_SIZE
);
703 if (!qed_is_table_size_valid(table_size
)) {
704 error_setg(errp
, "QED table size must be within range [%u, %u] "
706 QED_MIN_TABLE_SIZE
, QED_MAX_TABLE_SIZE
);
710 if (!qed_is_image_size_valid(image_size
, cluster_size
, table_size
)) {
711 error_setg(errp
, "QED image size must be a non-zero multiple of "
712 "cluster size and less than %" PRIu64
" bytes",
713 qed_max_image_size(cluster_size
, table_size
));
718 ret
= qed_create(filename
, cluster_size
, image_size
, table_size
,
719 backing_file
, backing_fmt
, opts
, errp
);
722 g_free(backing_file
);
728 BlockDriverState
*bs
;
733 BlockDriverState
**file
;
736 static void qed_is_allocated_cb(void *opaque
, int ret
, uint64_t offset
, size_t len
)
738 QEDIsAllocatedCB
*cb
= opaque
;
739 BDRVQEDState
*s
= cb
->bs
->opaque
;
740 *cb
->pnum
= len
/ BDRV_SECTOR_SIZE
;
742 case QED_CLUSTER_FOUND
:
743 offset
|= qed_offset_into_cluster(s
, cb
->pos
);
744 cb
->status
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| offset
;
745 *cb
->file
= cb
->bs
->file
->bs
;
747 case QED_CLUSTER_ZERO
:
748 cb
->status
= BDRV_BLOCK_ZERO
;
765 static int64_t coroutine_fn
bdrv_qed_co_get_block_status(BlockDriverState
*bs
,
767 int nb_sectors
, int *pnum
,
768 BlockDriverState
**file
)
770 BDRVQEDState
*s
= bs
->opaque
;
771 size_t len
= (size_t)nb_sectors
* BDRV_SECTOR_SIZE
;
772 QEDIsAllocatedCB cb
= {
774 .pos
= (uint64_t)sector_num
* BDRV_SECTOR_SIZE
,
775 .status
= BDRV_BLOCK_OFFSET_MASK
,
779 QEDRequest request
= { .l2_table
= NULL
};
781 qed_find_cluster(s
, &request
, cb
.pos
, len
, qed_is_allocated_cb
, &cb
);
783 /* Now sleep if the callback wasn't invoked immediately */
784 while (cb
.status
== BDRV_BLOCK_OFFSET_MASK
) {
785 cb
.co
= qemu_coroutine_self();
786 qemu_coroutine_yield();
789 qed_unref_l2_cache_entry(request
.l2_table
);
794 static BDRVQEDState
*acb_to_s(QEDAIOCB
*acb
)
796 return acb
->common
.bs
->opaque
;
800 * Read from the backing file or zero-fill if no backing file
803 * @pos: Byte position in device
804 * @qiov: Destination I/O vector
805 * @backing_qiov: Possibly shortened copy of qiov, to be allocated here
806 * @cb: Completion function
807 * @opaque: User data for completion function
809 * This function reads qiov->size bytes starting at pos from the backing file.
810 * If there is no backing file then zeroes are read.
812 static void qed_read_backing_file(BDRVQEDState
*s
, uint64_t pos
,
814 QEMUIOVector
**backing_qiov
,
815 BlockCompletionFunc
*cb
, void *opaque
)
817 uint64_t backing_length
= 0;
820 /* If there is a backing file, get its length. Treat the absence of a
821 * backing file like a zero length backing file.
823 if (s
->bs
->backing
) {
824 int64_t l
= bdrv_getlength(s
->bs
->backing
->bs
);
832 /* Zero all sectors if reading beyond the end of the backing file */
833 if (pos
>= backing_length
||
834 pos
+ qiov
->size
> backing_length
) {
835 qemu_iovec_memset(qiov
, 0, 0, qiov
->size
);
838 /* Complete now if there are no backing file sectors to read */
839 if (pos
>= backing_length
) {
844 /* If the read straddles the end of the backing file, shorten it */
845 size
= MIN((uint64_t)backing_length
- pos
, qiov
->size
);
847 assert(*backing_qiov
== NULL
);
848 *backing_qiov
= g_new(QEMUIOVector
, 1);
849 qemu_iovec_init(*backing_qiov
, qiov
->niov
);
850 qemu_iovec_concat(*backing_qiov
, qiov
, 0, size
);
852 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_READ_BACKING_AIO
);
853 bdrv_aio_readv(s
->bs
->backing
, pos
/ BDRV_SECTOR_SIZE
,
854 *backing_qiov
, size
/ BDRV_SECTOR_SIZE
, cb
, opaque
);
861 QEMUIOVector
*backing_qiov
;
864 } CopyFromBackingFileCB
;
866 static void qed_copy_from_backing_file_cb(void *opaque
, int ret
)
868 CopyFromBackingFileCB
*copy_cb
= opaque
;
869 qemu_vfree(copy_cb
->iov
.iov_base
);
870 gencb_complete(©_cb
->gencb
, ret
);
873 static void qed_copy_from_backing_file_write(void *opaque
, int ret
)
875 CopyFromBackingFileCB
*copy_cb
= opaque
;
876 BDRVQEDState
*s
= copy_cb
->s
;
878 if (copy_cb
->backing_qiov
) {
879 qemu_iovec_destroy(copy_cb
->backing_qiov
);
880 g_free(copy_cb
->backing_qiov
);
881 copy_cb
->backing_qiov
= NULL
;
885 qed_copy_from_backing_file_cb(copy_cb
, ret
);
889 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_COW_WRITE
);
890 bdrv_aio_writev(s
->bs
->file
, copy_cb
->offset
/ BDRV_SECTOR_SIZE
,
891 ©_cb
->qiov
, copy_cb
->qiov
.size
/ BDRV_SECTOR_SIZE
,
892 qed_copy_from_backing_file_cb
, copy_cb
);
896 * Copy data from backing file into the image
899 * @pos: Byte position in device
900 * @len: Number of bytes
901 * @offset: Byte offset in image file
902 * @cb: Completion function
903 * @opaque: User data for completion function
905 static void qed_copy_from_backing_file(BDRVQEDState
*s
, uint64_t pos
,
906 uint64_t len
, uint64_t offset
,
907 BlockCompletionFunc
*cb
,
910 CopyFromBackingFileCB
*copy_cb
;
912 /* Skip copy entirely if there is no work to do */
918 copy_cb
= gencb_alloc(sizeof(*copy_cb
), cb
, opaque
);
920 copy_cb
->offset
= offset
;
921 copy_cb
->backing_qiov
= NULL
;
922 copy_cb
->iov
.iov_base
= qemu_blockalign(s
->bs
, len
);
923 copy_cb
->iov
.iov_len
= len
;
924 qemu_iovec_init_external(©_cb
->qiov
, ©_cb
->iov
, 1);
926 qed_read_backing_file(s
, pos
, ©_cb
->qiov
, ©_cb
->backing_qiov
,
927 qed_copy_from_backing_file_write
, copy_cb
);
931 * Link one or more contiguous clusters into a table
935 * @index: First cluster index
936 * @n: Number of contiguous clusters
937 * @cluster: First cluster offset
939 * The cluster offset may be an allocated byte offset in the image file, the
940 * zero cluster marker, or the unallocated cluster marker.
942 static void qed_update_l2_table(BDRVQEDState
*s
, QEDTable
*table
, int index
,
943 unsigned int n
, uint64_t cluster
)
946 for (i
= index
; i
< index
+ n
; i
++) {
947 table
->offsets
[i
] = cluster
;
948 if (!qed_offset_is_unalloc_cluster(cluster
) &&
949 !qed_offset_is_zero_cluster(cluster
)) {
950 cluster
+= s
->header
.cluster_size
;
955 static void qed_aio_complete_bh(void *opaque
)
957 QEDAIOCB
*acb
= opaque
;
958 BDRVQEDState
*s
= acb_to_s(acb
);
959 BlockCompletionFunc
*cb
= acb
->common
.cb
;
960 void *user_opaque
= acb
->common
.opaque
;
961 int ret
= acb
->bh_ret
;
965 /* Invoke callback */
967 cb(user_opaque
, ret
);
971 static void qed_aio_complete(QEDAIOCB
*acb
, int ret
)
973 BDRVQEDState
*s
= acb_to_s(acb
);
975 trace_qed_aio_complete(s
, acb
, ret
);
978 qemu_iovec_destroy(&acb
->cur_qiov
);
979 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
981 /* Free the buffer we may have allocated for zero writes */
982 if (acb
->flags
& QED_AIOCB_ZERO
) {
983 qemu_vfree(acb
->qiov
->iov
[0].iov_base
);
984 acb
->qiov
->iov
[0].iov_base
= NULL
;
987 /* Arrange for a bh to invoke the completion function */
989 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb
->common
.bs
),
990 qed_aio_complete_bh
, acb
);
992 /* Start next allocating write request waiting behind this one. Note that
993 * requests enqueue themselves when they first hit an unallocated cluster
994 * but they wait until the entire request is finished before waking up the
995 * next request in the queue. This ensures that we don't cycle through
996 * requests multiple times but rather finish one at a time completely.
998 if (acb
== QSIMPLEQ_FIRST(&s
->allocating_write_reqs
)) {
999 QSIMPLEQ_REMOVE_HEAD(&s
->allocating_write_reqs
, next
);
1000 acb
= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
);
1002 qed_aio_start_io(acb
);
1003 } else if (s
->header
.features
& QED_F_NEED_CHECK
) {
1004 qed_start_need_check_timer(s
);
1010 * Commit the current L2 table to the cache
1012 static void qed_commit_l2_update(void *opaque
, int ret
)
1014 QEDAIOCB
*acb
= opaque
;
1015 BDRVQEDState
*s
= acb_to_s(acb
);
1016 CachedL2Table
*l2_table
= acb
->request
.l2_table
;
1017 uint64_t l2_offset
= l2_table
->offset
;
1019 qed_commit_l2_cache_entry(&s
->l2_cache
, l2_table
);
1021 /* This is guaranteed to succeed because we just committed the entry to the
1024 acb
->request
.l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, l2_offset
);
1025 assert(acb
->request
.l2_table
!= NULL
);
1027 qed_aio_next_io(acb
, ret
);
1031 * Update L1 table with new L2 table offset and write it out
1033 static void qed_aio_write_l1_update(void *opaque
, int ret
)
1035 QEDAIOCB
*acb
= opaque
;
1036 BDRVQEDState
*s
= acb_to_s(acb
);
1040 qed_aio_complete(acb
, ret
);
1044 index
= qed_l1_index(s
, acb
->cur_pos
);
1045 s
->l1_table
->offsets
[index
] = acb
->request
.l2_table
->offset
;
1047 qed_write_l1_table(s
, index
, 1, qed_commit_l2_update
, acb
);
1051 * Update L2 table with new cluster offsets and write them out
1053 static void qed_aio_write_l2_update(QEDAIOCB
*acb
, int ret
, uint64_t offset
)
1055 BDRVQEDState
*s
= acb_to_s(acb
);
1056 bool need_alloc
= acb
->find_cluster_ret
== QED_CLUSTER_L1
;
1064 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
1065 acb
->request
.l2_table
= qed_new_l2_table(s
);
1068 index
= qed_l2_index(s
, acb
->cur_pos
);
1069 qed_update_l2_table(s
, acb
->request
.l2_table
->table
, index
, acb
->cur_nclusters
,
1073 /* Write out the whole new L2 table */
1074 qed_write_l2_table(s
, &acb
->request
, 0, s
->table_nelems
, true,
1075 qed_aio_write_l1_update
, acb
);
1077 /* Write out only the updated part of the L2 table */
1078 qed_write_l2_table(s
, &acb
->request
, index
, acb
->cur_nclusters
, false,
1079 qed_aio_next_io_cb
, acb
);
1084 qed_aio_complete(acb
, ret
);
1087 static void qed_aio_write_l2_update_cb(void *opaque
, int ret
)
1089 QEDAIOCB
*acb
= opaque
;
1090 qed_aio_write_l2_update(acb
, ret
, acb
->cur_cluster
);
1094 * Flush new data clusters before updating the L2 table
1096 * This flush is necessary when a backing file is in use. A crash during an
1097 * allocating write could result in empty clusters in the image. If the write
1098 * only touched a subregion of the cluster, then backing image sectors have
1099 * been lost in the untouched region. The solution is to flush after writing a
1100 * new data cluster and before updating the L2 table.
1102 static void qed_aio_write_flush_before_l2_update(void *opaque
, int ret
)
1104 QEDAIOCB
*acb
= opaque
;
1105 BDRVQEDState
*s
= acb_to_s(acb
);
1107 if (!bdrv_aio_flush(s
->bs
->file
->bs
, qed_aio_write_l2_update_cb
, opaque
)) {
1108 qed_aio_complete(acb
, -EIO
);
1113 * Write data to the image file
1115 static void qed_aio_write_main(void *opaque
, int ret
)
1117 QEDAIOCB
*acb
= opaque
;
1118 BDRVQEDState
*s
= acb_to_s(acb
);
1119 uint64_t offset
= acb
->cur_cluster
+
1120 qed_offset_into_cluster(s
, acb
->cur_pos
);
1121 BlockCompletionFunc
*next_fn
;
1123 trace_qed_aio_write_main(s
, acb
, ret
, offset
, acb
->cur_qiov
.size
);
1126 qed_aio_complete(acb
, ret
);
1130 if (acb
->find_cluster_ret
== QED_CLUSTER_FOUND
) {
1131 next_fn
= qed_aio_next_io_cb
;
1133 if (s
->bs
->backing
) {
1134 next_fn
= qed_aio_write_flush_before_l2_update
;
1136 next_fn
= qed_aio_write_l2_update_cb
;
1140 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_WRITE_AIO
);
1141 bdrv_aio_writev(s
->bs
->file
, offset
/ BDRV_SECTOR_SIZE
,
1142 &acb
->cur_qiov
, acb
->cur_qiov
.size
/ BDRV_SECTOR_SIZE
,
1147 * Populate back untouched region of new data cluster
1149 static void qed_aio_write_postfill(void *opaque
, int ret
)
1151 QEDAIOCB
*acb
= opaque
;
1152 BDRVQEDState
*s
= acb_to_s(acb
);
1153 uint64_t start
= acb
->cur_pos
+ acb
->cur_qiov
.size
;
1155 qed_start_of_cluster(s
, start
+ s
->header
.cluster_size
- 1) - start
;
1156 uint64_t offset
= acb
->cur_cluster
+
1157 qed_offset_into_cluster(s
, acb
->cur_pos
) +
1161 qed_aio_complete(acb
, ret
);
1165 trace_qed_aio_write_postfill(s
, acb
, start
, len
, offset
);
1166 qed_copy_from_backing_file(s
, start
, len
, offset
,
1167 qed_aio_write_main
, acb
);
1171 * Populate front untouched region of new data cluster
1173 static void qed_aio_write_prefill(void *opaque
, int ret
)
1175 QEDAIOCB
*acb
= opaque
;
1176 BDRVQEDState
*s
= acb_to_s(acb
);
1177 uint64_t start
= qed_start_of_cluster(s
, acb
->cur_pos
);
1178 uint64_t len
= qed_offset_into_cluster(s
, acb
->cur_pos
);
1180 trace_qed_aio_write_prefill(s
, acb
, start
, len
, acb
->cur_cluster
);
1181 qed_copy_from_backing_file(s
, start
, len
, acb
->cur_cluster
,
1182 qed_aio_write_postfill
, acb
);
1186 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1188 static bool qed_should_set_need_check(BDRVQEDState
*s
)
1190 /* The flush before L2 update path ensures consistency */
1191 if (s
->bs
->backing
) {
1195 return !(s
->header
.features
& QED_F_NEED_CHECK
);
1198 static void qed_aio_write_zero_cluster(void *opaque
, int ret
)
1200 QEDAIOCB
*acb
= opaque
;
1203 qed_aio_complete(acb
, ret
);
1207 qed_aio_write_l2_update(acb
, 0, 1);
1211 * Write new data cluster
1213 * @acb: Write request
1214 * @len: Length in bytes
1216 * This path is taken when writing to previously unallocated clusters.
1218 static void qed_aio_write_alloc(QEDAIOCB
*acb
, size_t len
)
1220 BDRVQEDState
*s
= acb_to_s(acb
);
1221 BlockCompletionFunc
*cb
;
1223 /* Cancel timer when the first allocating request comes in */
1224 if (QSIMPLEQ_EMPTY(&s
->allocating_write_reqs
)) {
1225 qed_cancel_need_check_timer(s
);
1228 /* Freeze this request if another allocating write is in progress */
1229 if (acb
!= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
)) {
1230 QSIMPLEQ_INSERT_TAIL(&s
->allocating_write_reqs
, acb
, next
);
1232 if (acb
!= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
) ||
1233 s
->allocating_write_reqs_plugged
) {
1234 return; /* wait for existing request to finish */
1237 acb
->cur_nclusters
= qed_bytes_to_clusters(s
,
1238 qed_offset_into_cluster(s
, acb
->cur_pos
) + len
);
1239 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1241 if (acb
->flags
& QED_AIOCB_ZERO
) {
1242 /* Skip ahead if the clusters are already zero */
1243 if (acb
->find_cluster_ret
== QED_CLUSTER_ZERO
) {
1244 qed_aio_start_io(acb
);
1248 cb
= qed_aio_write_zero_cluster
;
1250 cb
= qed_aio_write_prefill
;
1251 acb
->cur_cluster
= qed_alloc_clusters(s
, acb
->cur_nclusters
);
1254 if (qed_should_set_need_check(s
)) {
1255 s
->header
.features
|= QED_F_NEED_CHECK
;
1256 qed_write_header(s
, cb
, acb
);
1263 * Write data cluster in place
1265 * @acb: Write request
1266 * @offset: Cluster offset in bytes
1267 * @len: Length in bytes
1269 * This path is taken when writing to already allocated clusters.
1271 static void qed_aio_write_inplace(QEDAIOCB
*acb
, uint64_t offset
, size_t len
)
1273 /* Allocate buffer for zero writes */
1274 if (acb
->flags
& QED_AIOCB_ZERO
) {
1275 struct iovec
*iov
= acb
->qiov
->iov
;
1277 if (!iov
->iov_base
) {
1278 iov
->iov_base
= qemu_try_blockalign(acb
->common
.bs
, iov
->iov_len
);
1279 if (iov
->iov_base
== NULL
) {
1280 qed_aio_complete(acb
, -ENOMEM
);
1283 memset(iov
->iov_base
, 0, iov
->iov_len
);
1287 /* Calculate the I/O vector */
1288 acb
->cur_cluster
= offset
;
1289 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1291 /* Do the actual write */
1292 qed_aio_write_main(acb
, 0);
1296 * Write data cluster
1298 * @opaque: Write request
1299 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1301 * @offset: Cluster offset in bytes
1302 * @len: Length in bytes
1304 * Callback from qed_find_cluster().
1306 static void qed_aio_write_data(void *opaque
, int ret
,
1307 uint64_t offset
, size_t len
)
1309 QEDAIOCB
*acb
= opaque
;
1311 trace_qed_aio_write_data(acb_to_s(acb
), acb
, ret
, offset
, len
);
1313 acb
->find_cluster_ret
= ret
;
1316 case QED_CLUSTER_FOUND
:
1317 qed_aio_write_inplace(acb
, offset
, len
);
1320 case QED_CLUSTER_L2
:
1321 case QED_CLUSTER_L1
:
1322 case QED_CLUSTER_ZERO
:
1323 qed_aio_write_alloc(acb
, len
);
1327 qed_aio_complete(acb
, ret
);
1335 * @opaque: Read request
1336 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1338 * @offset: Cluster offset in bytes
1339 * @len: Length in bytes
1341 * Callback from qed_find_cluster().
1343 static void qed_aio_read_data(void *opaque
, int ret
,
1344 uint64_t offset
, size_t len
)
1346 QEDAIOCB
*acb
= opaque
;
1347 BDRVQEDState
*s
= acb_to_s(acb
);
1348 BlockDriverState
*bs
= acb
->common
.bs
;
1350 /* Adjust offset into cluster */
1351 offset
+= qed_offset_into_cluster(s
, acb
->cur_pos
);
1353 trace_qed_aio_read_data(s
, acb
, ret
, offset
, len
);
1359 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1361 /* Handle zero cluster and backing file reads */
1362 if (ret
== QED_CLUSTER_ZERO
) {
1363 qemu_iovec_memset(&acb
->cur_qiov
, 0, 0, acb
->cur_qiov
.size
);
1364 qed_aio_start_io(acb
);
1366 } else if (ret
!= QED_CLUSTER_FOUND
) {
1367 qed_read_backing_file(s
, acb
->cur_pos
, &acb
->cur_qiov
,
1368 &acb
->backing_qiov
, qed_aio_next_io_cb
, acb
);
1372 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
1373 bdrv_aio_readv(bs
->file
, offset
/ BDRV_SECTOR_SIZE
,
1374 &acb
->cur_qiov
, acb
->cur_qiov
.size
/ BDRV_SECTOR_SIZE
,
1375 qed_aio_next_io_cb
, acb
);
1379 qed_aio_complete(acb
, ret
);
1383 * Begin next I/O or complete the request
1385 static void qed_aio_next_io(QEDAIOCB
*acb
, int ret
)
1387 BDRVQEDState
*s
= acb_to_s(acb
);
1388 QEDFindClusterFunc
*io_fn
= (acb
->flags
& QED_AIOCB_WRITE
) ?
1389 qed_aio_write_data
: qed_aio_read_data
;
1391 trace_qed_aio_next_io(s
, acb
, ret
, acb
->cur_pos
+ acb
->cur_qiov
.size
);
1393 if (acb
->backing_qiov
) {
1394 qemu_iovec_destroy(acb
->backing_qiov
);
1395 g_free(acb
->backing_qiov
);
1396 acb
->backing_qiov
= NULL
;
1399 /* Handle I/O error */
1401 qed_aio_complete(acb
, ret
);
1405 acb
->qiov_offset
+= acb
->cur_qiov
.size
;
1406 acb
->cur_pos
+= acb
->cur_qiov
.size
;
1407 qemu_iovec_reset(&acb
->cur_qiov
);
1409 /* Complete request */
1410 if (acb
->cur_pos
>= acb
->end_pos
) {
1411 qed_aio_complete(acb
, 0);
1415 /* Find next cluster and start I/O */
1416 qed_find_cluster(s
, &acb
->request
,
1417 acb
->cur_pos
, acb
->end_pos
- acb
->cur_pos
,
1421 static BlockAIOCB
*qed_aio_setup(BlockDriverState
*bs
,
1423 QEMUIOVector
*qiov
, int nb_sectors
,
1424 BlockCompletionFunc
*cb
,
1425 void *opaque
, int flags
)
1427 QEDAIOCB
*acb
= qemu_aio_get(&qed_aiocb_info
, bs
, cb
, opaque
);
1429 trace_qed_aio_setup(bs
->opaque
, acb
, sector_num
, nb_sectors
,
1434 acb
->qiov_offset
= 0;
1435 acb
->cur_pos
= (uint64_t)sector_num
* BDRV_SECTOR_SIZE
;
1436 acb
->end_pos
= acb
->cur_pos
+ nb_sectors
* BDRV_SECTOR_SIZE
;
1437 acb
->backing_qiov
= NULL
;
1438 acb
->request
.l2_table
= NULL
;
1439 qemu_iovec_init(&acb
->cur_qiov
, qiov
->niov
);
1442 qed_aio_start_io(acb
);
1443 return &acb
->common
;
1446 static BlockAIOCB
*bdrv_qed_aio_readv(BlockDriverState
*bs
,
1448 QEMUIOVector
*qiov
, int nb_sectors
,
1449 BlockCompletionFunc
*cb
,
1452 return qed_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1455 static BlockAIOCB
*bdrv_qed_aio_writev(BlockDriverState
*bs
,
1457 QEMUIOVector
*qiov
, int nb_sectors
,
1458 BlockCompletionFunc
*cb
,
1461 return qed_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
,
1462 opaque
, QED_AIOCB_WRITE
);
1471 static void coroutine_fn
qed_co_pwrite_zeroes_cb(void *opaque
, int ret
)
1473 QEDWriteZeroesCB
*cb
= opaque
;
1478 aio_co_wake(cb
->co
);
1482 static int coroutine_fn
bdrv_qed_co_pwrite_zeroes(BlockDriverState
*bs
,
1485 BdrvRequestFlags flags
)
1487 BlockAIOCB
*blockacb
;
1488 BDRVQEDState
*s
= bs
->opaque
;
1489 QEDWriteZeroesCB cb
= { .done
= false };
1493 /* Fall back if the request is not aligned */
1494 if (qed_offset_into_cluster(s
, offset
) ||
1495 qed_offset_into_cluster(s
, count
)) {
1499 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1500 * then it will be allocated during request processing.
1502 iov
.iov_base
= NULL
;
1503 iov
.iov_len
= count
;
1505 qemu_iovec_init_external(&qiov
, &iov
, 1);
1506 blockacb
= qed_aio_setup(bs
, offset
>> BDRV_SECTOR_BITS
, &qiov
,
1507 count
>> BDRV_SECTOR_BITS
,
1508 qed_co_pwrite_zeroes_cb
, &cb
,
1509 QED_AIOCB_WRITE
| QED_AIOCB_ZERO
);
1514 cb
.co
= qemu_coroutine_self();
1515 qemu_coroutine_yield();
1521 static int bdrv_qed_truncate(BlockDriverState
*bs
, int64_t offset
, Error
**errp
)
1523 BDRVQEDState
*s
= bs
->opaque
;
1524 uint64_t old_image_size
;
1527 if (!qed_is_image_size_valid(offset
, s
->header
.cluster_size
,
1528 s
->header
.table_size
)) {
1529 error_setg(errp
, "Invalid image size specified");
1533 if ((uint64_t)offset
< s
->header
.image_size
) {
1534 error_setg(errp
, "Shrinking images is currently not supported");
1538 old_image_size
= s
->header
.image_size
;
1539 s
->header
.image_size
= offset
;
1540 ret
= qed_write_header_sync(s
);
1542 s
->header
.image_size
= old_image_size
;
1543 error_setg_errno(errp
, -ret
, "Failed to update the image size");
1548 static int64_t bdrv_qed_getlength(BlockDriverState
*bs
)
1550 BDRVQEDState
*s
= bs
->opaque
;
1551 return s
->header
.image_size
;
1554 static int bdrv_qed_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1556 BDRVQEDState
*s
= bs
->opaque
;
1558 memset(bdi
, 0, sizeof(*bdi
));
1559 bdi
->cluster_size
= s
->header
.cluster_size
;
1560 bdi
->is_dirty
= s
->header
.features
& QED_F_NEED_CHECK
;
1561 bdi
->unallocated_blocks_are_zero
= true;
1562 bdi
->can_write_zeroes_with_unmap
= true;
1566 static int bdrv_qed_change_backing_file(BlockDriverState
*bs
,
1567 const char *backing_file
,
1568 const char *backing_fmt
)
1570 BDRVQEDState
*s
= bs
->opaque
;
1571 QEDHeader new_header
, le_header
;
1573 size_t buffer_len
, backing_file_len
;
1576 /* Refuse to set backing filename if unknown compat feature bits are
1577 * active. If the image uses an unknown compat feature then we may not
1578 * know the layout of data following the header structure and cannot safely
1581 if (backing_file
&& (s
->header
.compat_features
&
1582 ~QED_COMPAT_FEATURE_MASK
)) {
1586 memcpy(&new_header
, &s
->header
, sizeof(new_header
));
1588 new_header
.features
&= ~(QED_F_BACKING_FILE
|
1589 QED_F_BACKING_FORMAT_NO_PROBE
);
1591 /* Adjust feature flags */
1593 new_header
.features
|= QED_F_BACKING_FILE
;
1595 if (qed_fmt_is_raw(backing_fmt
)) {
1596 new_header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
1600 /* Calculate new header size */
1601 backing_file_len
= 0;
1604 backing_file_len
= strlen(backing_file
);
1607 buffer_len
= sizeof(new_header
);
1608 new_header
.backing_filename_offset
= buffer_len
;
1609 new_header
.backing_filename_size
= backing_file_len
;
1610 buffer_len
+= backing_file_len
;
1612 /* Make sure we can rewrite header without failing */
1613 if (buffer_len
> new_header
.header_size
* new_header
.cluster_size
) {
1617 /* Prepare new header */
1618 buffer
= g_malloc(buffer_len
);
1620 qed_header_cpu_to_le(&new_header
, &le_header
);
1621 memcpy(buffer
, &le_header
, sizeof(le_header
));
1622 buffer_len
= sizeof(le_header
);
1625 memcpy(buffer
+ buffer_len
, backing_file
, backing_file_len
);
1626 buffer_len
+= backing_file_len
;
1629 /* Write new header */
1630 ret
= bdrv_pwrite_sync(bs
->file
, 0, buffer
, buffer_len
);
1633 memcpy(&s
->header
, &new_header
, sizeof(new_header
));
1638 static void bdrv_qed_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
1640 BDRVQEDState
*s
= bs
->opaque
;
1641 Error
*local_err
= NULL
;
1646 memset(s
, 0, sizeof(BDRVQEDState
));
1647 ret
= bdrv_qed_do_open(bs
, NULL
, bs
->open_flags
, &local_err
);
1649 error_propagate(errp
, local_err
);
1650 error_prepend(errp
, "Could not reopen qed layer: ");
1652 } else if (ret
< 0) {
1653 error_setg_errno(errp
, -ret
, "Could not reopen qed layer");
1658 static int bdrv_qed_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
1661 BDRVQEDState
*s
= bs
->opaque
;
1663 return qed_check(s
, result
, !!fix
);
1666 static QemuOptsList qed_create_opts
= {
1667 .name
= "qed-create-opts",
1668 .head
= QTAILQ_HEAD_INITIALIZER(qed_create_opts
.head
),
1671 .name
= BLOCK_OPT_SIZE
,
1672 .type
= QEMU_OPT_SIZE
,
1673 .help
= "Virtual disk size"
1676 .name
= BLOCK_OPT_BACKING_FILE
,
1677 .type
= QEMU_OPT_STRING
,
1678 .help
= "File name of a base image"
1681 .name
= BLOCK_OPT_BACKING_FMT
,
1682 .type
= QEMU_OPT_STRING
,
1683 .help
= "Image format of the base image"
1686 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1687 .type
= QEMU_OPT_SIZE
,
1688 .help
= "Cluster size (in bytes)",
1689 .def_value_str
= stringify(QED_DEFAULT_CLUSTER_SIZE
)
1692 .name
= BLOCK_OPT_TABLE_SIZE
,
1693 .type
= QEMU_OPT_SIZE
,
1694 .help
= "L1/L2 table size (in clusters)"
1696 { /* end of list */ }
1700 static BlockDriver bdrv_qed
= {
1701 .format_name
= "qed",
1702 .instance_size
= sizeof(BDRVQEDState
),
1703 .create_opts
= &qed_create_opts
,
1704 .supports_backing
= true,
1706 .bdrv_probe
= bdrv_qed_probe
,
1707 .bdrv_open
= bdrv_qed_open
,
1708 .bdrv_close
= bdrv_qed_close
,
1709 .bdrv_reopen_prepare
= bdrv_qed_reopen_prepare
,
1710 .bdrv_child_perm
= bdrv_format_default_perms
,
1711 .bdrv_create
= bdrv_qed_create
,
1712 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1713 .bdrv_co_get_block_status
= bdrv_qed_co_get_block_status
,
1714 .bdrv_aio_readv
= bdrv_qed_aio_readv
,
1715 .bdrv_aio_writev
= bdrv_qed_aio_writev
,
1716 .bdrv_co_pwrite_zeroes
= bdrv_qed_co_pwrite_zeroes
,
1717 .bdrv_truncate
= bdrv_qed_truncate
,
1718 .bdrv_getlength
= bdrv_qed_getlength
,
1719 .bdrv_get_info
= bdrv_qed_get_info
,
1720 .bdrv_refresh_limits
= bdrv_qed_refresh_limits
,
1721 .bdrv_change_backing_file
= bdrv_qed_change_backing_file
,
1722 .bdrv_invalidate_cache
= bdrv_qed_invalidate_cache
,
1723 .bdrv_check
= bdrv_qed_check
,
1724 .bdrv_detach_aio_context
= bdrv_qed_detach_aio_context
,
1725 .bdrv_attach_aio_context
= bdrv_qed_attach_aio_context
,
1726 .bdrv_drain
= bdrv_qed_drain
,
1729 static void bdrv_qed_init(void)
1731 bdrv_register(&bdrv_qed
);
1734 block_init(bdrv_qed_init
);