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 "sysemu/block-backend.h"
24 static int bdrv_qed_probe(const uint8_t *buf
, int buf_size
,
27 const QEDHeader
*header
= (const QEDHeader
*)buf
;
29 if (buf_size
< sizeof(*header
)) {
32 if (le32_to_cpu(header
->magic
) != QED_MAGIC
) {
39 * Check whether an image format is raw
41 * @fmt: Backing file format, may be NULL
43 static bool qed_fmt_is_raw(const char *fmt
)
45 return fmt
&& strcmp(fmt
, "raw") == 0;
48 static void qed_header_le_to_cpu(const QEDHeader
*le
, QEDHeader
*cpu
)
50 cpu
->magic
= le32_to_cpu(le
->magic
);
51 cpu
->cluster_size
= le32_to_cpu(le
->cluster_size
);
52 cpu
->table_size
= le32_to_cpu(le
->table_size
);
53 cpu
->header_size
= le32_to_cpu(le
->header_size
);
54 cpu
->features
= le64_to_cpu(le
->features
);
55 cpu
->compat_features
= le64_to_cpu(le
->compat_features
);
56 cpu
->autoclear_features
= le64_to_cpu(le
->autoclear_features
);
57 cpu
->l1_table_offset
= le64_to_cpu(le
->l1_table_offset
);
58 cpu
->image_size
= le64_to_cpu(le
->image_size
);
59 cpu
->backing_filename_offset
= le32_to_cpu(le
->backing_filename_offset
);
60 cpu
->backing_filename_size
= le32_to_cpu(le
->backing_filename_size
);
63 static void qed_header_cpu_to_le(const QEDHeader
*cpu
, QEDHeader
*le
)
65 le
->magic
= cpu_to_le32(cpu
->magic
);
66 le
->cluster_size
= cpu_to_le32(cpu
->cluster_size
);
67 le
->table_size
= cpu_to_le32(cpu
->table_size
);
68 le
->header_size
= cpu_to_le32(cpu
->header_size
);
69 le
->features
= cpu_to_le64(cpu
->features
);
70 le
->compat_features
= cpu_to_le64(cpu
->compat_features
);
71 le
->autoclear_features
= cpu_to_le64(cpu
->autoclear_features
);
72 le
->l1_table_offset
= cpu_to_le64(cpu
->l1_table_offset
);
73 le
->image_size
= cpu_to_le64(cpu
->image_size
);
74 le
->backing_filename_offset
= cpu_to_le32(cpu
->backing_filename_offset
);
75 le
->backing_filename_size
= cpu_to_le32(cpu
->backing_filename_size
);
78 int qed_write_header_sync(BDRVQEDState
*s
)
83 qed_header_cpu_to_le(&s
->header
, &le
);
84 ret
= bdrv_pwrite(s
->bs
->file
, 0, &le
, sizeof(le
));
85 if (ret
!= sizeof(le
)) {
92 * Update header in-place (does not rewrite backing filename or other strings)
94 * This function only updates known header fields in-place and does not affect
95 * extra data after the QED header.
97 static int coroutine_fn
qed_write_header(BDRVQEDState
*s
)
99 /* We must write full sectors for O_DIRECT but cannot necessarily generate
100 * the data following the header if an unrecognized compat feature is
101 * active. Therefore, first read the sectors containing the header, update
102 * them, and write back.
105 int nsectors
= DIV_ROUND_UP(sizeof(QEDHeader
), BDRV_SECTOR_SIZE
);
106 size_t len
= nsectors
* BDRV_SECTOR_SIZE
;
112 buf
= qemu_blockalign(s
->bs
, len
);
113 iov
= (struct iovec
) {
117 qemu_iovec_init_external(&qiov
, &iov
, 1);
119 ret
= bdrv_co_preadv(s
->bs
->file
, 0, qiov
.size
, &qiov
, 0);
125 qed_header_cpu_to_le(&s
->header
, (QEDHeader
*) buf
);
127 ret
= bdrv_co_pwritev(s
->bs
->file
, 0, qiov
.size
, &qiov
, 0);
138 static uint64_t qed_max_image_size(uint32_t cluster_size
, uint32_t table_size
)
140 uint64_t table_entries
;
143 table_entries
= (table_size
* cluster_size
) / sizeof(uint64_t);
144 l2_size
= table_entries
* cluster_size
;
146 return l2_size
* table_entries
;
149 static bool qed_is_cluster_size_valid(uint32_t cluster_size
)
151 if (cluster_size
< QED_MIN_CLUSTER_SIZE
||
152 cluster_size
> QED_MAX_CLUSTER_SIZE
) {
155 if (cluster_size
& (cluster_size
- 1)) {
156 return false; /* not power of 2 */
161 static bool qed_is_table_size_valid(uint32_t table_size
)
163 if (table_size
< QED_MIN_TABLE_SIZE
||
164 table_size
> QED_MAX_TABLE_SIZE
) {
167 if (table_size
& (table_size
- 1)) {
168 return false; /* not power of 2 */
173 static bool qed_is_image_size_valid(uint64_t image_size
, uint32_t cluster_size
,
176 if (image_size
% BDRV_SECTOR_SIZE
!= 0) {
177 return false; /* not multiple of sector size */
179 if (image_size
> qed_max_image_size(cluster_size
, table_size
)) {
180 return false; /* image is too large */
186 * Read a string of known length from the image file
189 * @offset: File offset to start of string, in bytes
190 * @n: String length in bytes
191 * @buf: Destination buffer
192 * @buflen: Destination buffer length in bytes
193 * @ret: 0 on success, -errno on failure
195 * The string is NUL-terminated.
197 static int qed_read_string(BdrvChild
*file
, uint64_t offset
, size_t n
,
198 char *buf
, size_t buflen
)
204 ret
= bdrv_pread(file
, offset
, buf
, n
);
213 * Allocate new clusters
216 * @n: Number of contiguous clusters to allocate
217 * @ret: Offset of first allocated cluster
219 * This function only produces the offset where the new clusters should be
220 * written. It updates BDRVQEDState but does not make any changes to the image
223 static uint64_t qed_alloc_clusters(BDRVQEDState
*s
, unsigned int n
)
225 uint64_t offset
= s
->file_size
;
226 s
->file_size
+= n
* s
->header
.cluster_size
;
230 QEDTable
*qed_alloc_table(BDRVQEDState
*s
)
232 /* Honor O_DIRECT memory alignment requirements */
233 return qemu_blockalign(s
->bs
,
234 s
->header
.cluster_size
* s
->header
.table_size
);
238 * Allocate a new zeroed L2 table
240 static CachedL2Table
*qed_new_l2_table(BDRVQEDState
*s
)
242 CachedL2Table
*l2_table
= qed_alloc_l2_cache_entry(&s
->l2_cache
);
244 l2_table
->table
= qed_alloc_table(s
);
245 l2_table
->offset
= qed_alloc_clusters(s
, s
->header
.table_size
);
247 memset(l2_table
->table
->offsets
, 0,
248 s
->header
.cluster_size
* s
->header
.table_size
);
252 static void qed_plug_allocating_write_reqs(BDRVQEDState
*s
)
254 assert(!s
->allocating_write_reqs_plugged
);
256 s
->allocating_write_reqs_plugged
= true;
259 static void qed_unplug_allocating_write_reqs(BDRVQEDState
*s
)
261 assert(s
->allocating_write_reqs_plugged
);
263 s
->allocating_write_reqs_plugged
= false;
264 qemu_co_enter_next(&s
->allocating_write_reqs
);
267 static void coroutine_fn
qed_need_check_timer_entry(void *opaque
)
269 BDRVQEDState
*s
= opaque
;
272 /* The timer should only fire when allocating writes have drained */
273 assert(!s
->allocating_acb
);
275 trace_qed_need_check_timer_cb(s
);
278 qed_plug_allocating_write_reqs(s
);
280 /* Ensure writes are on disk before clearing flag */
281 ret
= bdrv_co_flush(s
->bs
->file
->bs
);
284 qed_unplug_allocating_write_reqs(s
);
288 s
->header
.features
&= ~QED_F_NEED_CHECK
;
289 ret
= qed_write_header(s
);
292 qed_unplug_allocating_write_reqs(s
);
294 ret
= bdrv_co_flush(s
->bs
);
298 static void qed_need_check_timer_cb(void *opaque
)
300 Coroutine
*co
= qemu_coroutine_create(qed_need_check_timer_entry
, opaque
);
301 qemu_coroutine_enter(co
);
304 void qed_acquire(BDRVQEDState
*s
)
306 aio_context_acquire(bdrv_get_aio_context(s
->bs
));
309 void qed_release(BDRVQEDState
*s
)
311 aio_context_release(bdrv_get_aio_context(s
->bs
));
314 static void qed_start_need_check_timer(BDRVQEDState
*s
)
316 trace_qed_start_need_check_timer(s
);
318 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
321 timer_mod(s
->need_check_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
322 NANOSECONDS_PER_SECOND
* QED_NEED_CHECK_TIMEOUT
);
325 /* It's okay to call this multiple times or when no timer is started */
326 static void qed_cancel_need_check_timer(BDRVQEDState
*s
)
328 trace_qed_cancel_need_check_timer(s
);
329 timer_del(s
->need_check_timer
);
332 static void bdrv_qed_detach_aio_context(BlockDriverState
*bs
)
334 BDRVQEDState
*s
= bs
->opaque
;
336 qed_cancel_need_check_timer(s
);
337 timer_free(s
->need_check_timer
);
340 static void bdrv_qed_attach_aio_context(BlockDriverState
*bs
,
341 AioContext
*new_context
)
343 BDRVQEDState
*s
= bs
->opaque
;
345 s
->need_check_timer
= aio_timer_new(new_context
,
346 QEMU_CLOCK_VIRTUAL
, SCALE_NS
,
347 qed_need_check_timer_cb
, s
);
348 if (s
->header
.features
& QED_F_NEED_CHECK
) {
349 qed_start_need_check_timer(s
);
353 static void bdrv_qed_drain(BlockDriverState
*bs
)
355 BDRVQEDState
*s
= bs
->opaque
;
357 /* Fire the timer immediately in order to start doing I/O as soon as the
360 if (s
->need_check_timer
&& timer_pending(s
->need_check_timer
)) {
361 qed_cancel_need_check_timer(s
);
362 qed_need_check_timer_cb(s
);
366 static int bdrv_qed_do_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
369 BDRVQEDState
*s
= bs
->opaque
;
375 qemu_co_queue_init(&s
->allocating_write_reqs
);
377 ret
= bdrv_pread(bs
->file
, 0, &le_header
, sizeof(le_header
));
381 qed_header_le_to_cpu(&le_header
, &s
->header
);
383 if (s
->header
.magic
!= QED_MAGIC
) {
384 error_setg(errp
, "Image not in QED format");
387 if (s
->header
.features
& ~QED_FEATURE_MASK
) {
388 /* image uses unsupported feature bits */
389 error_setg(errp
, "Unsupported QED features: %" PRIx64
,
390 s
->header
.features
& ~QED_FEATURE_MASK
);
393 if (!qed_is_cluster_size_valid(s
->header
.cluster_size
)) {
397 /* Round down file size to the last cluster */
398 file_size
= bdrv_getlength(bs
->file
->bs
);
402 s
->file_size
= qed_start_of_cluster(s
, file_size
);
404 if (!qed_is_table_size_valid(s
->header
.table_size
)) {
407 if (!qed_is_image_size_valid(s
->header
.image_size
,
408 s
->header
.cluster_size
,
409 s
->header
.table_size
)) {
412 if (!qed_check_table_offset(s
, s
->header
.l1_table_offset
)) {
416 s
->table_nelems
= (s
->header
.cluster_size
* s
->header
.table_size
) /
418 s
->l2_shift
= ctz32(s
->header
.cluster_size
);
419 s
->l2_mask
= s
->table_nelems
- 1;
420 s
->l1_shift
= s
->l2_shift
+ ctz32(s
->table_nelems
);
422 /* Header size calculation must not overflow uint32_t */
423 if (s
->header
.header_size
> UINT32_MAX
/ s
->header
.cluster_size
) {
427 if ((s
->header
.features
& QED_F_BACKING_FILE
)) {
428 if ((uint64_t)s
->header
.backing_filename_offset
+
429 s
->header
.backing_filename_size
>
430 s
->header
.cluster_size
* s
->header
.header_size
) {
434 ret
= qed_read_string(bs
->file
, s
->header
.backing_filename_offset
,
435 s
->header
.backing_filename_size
, bs
->backing_file
,
436 sizeof(bs
->backing_file
));
441 if (s
->header
.features
& QED_F_BACKING_FORMAT_NO_PROBE
) {
442 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), "raw");
446 /* Reset unknown autoclear feature bits. This is a backwards
447 * compatibility mechanism that allows images to be opened by older
448 * programs, which "knock out" unknown feature bits. When an image is
449 * opened by a newer program again it can detect that the autoclear
450 * feature is no longer valid.
452 if ((s
->header
.autoclear_features
& ~QED_AUTOCLEAR_FEATURE_MASK
) != 0 &&
453 !bdrv_is_read_only(bs
->file
->bs
) && !(flags
& BDRV_O_INACTIVE
)) {
454 s
->header
.autoclear_features
&= QED_AUTOCLEAR_FEATURE_MASK
;
456 ret
= qed_write_header_sync(s
);
461 /* From here on only known autoclear feature bits are valid */
462 bdrv_flush(bs
->file
->bs
);
465 s
->l1_table
= qed_alloc_table(s
);
466 qed_init_l2_cache(&s
->l2_cache
);
468 ret
= qed_read_l1_table_sync(s
);
473 /* If image was not closed cleanly, check consistency */
474 if (!(flags
& BDRV_O_CHECK
) && (s
->header
.features
& QED_F_NEED_CHECK
)) {
475 /* Read-only images cannot be fixed. There is no risk of corruption
476 * since write operations are not possible. Therefore, allow
477 * potentially inconsistent images to be opened read-only. This can
478 * aid data recovery from an otherwise inconsistent image.
480 if (!bdrv_is_read_only(bs
->file
->bs
) &&
481 !(flags
& BDRV_O_INACTIVE
)) {
482 BdrvCheckResult result
= {0};
484 ret
= qed_check(s
, &result
, true);
491 bdrv_qed_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
495 qed_free_l2_cache(&s
->l2_cache
);
496 qemu_vfree(s
->l1_table
);
501 static int bdrv_qed_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
504 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
510 return bdrv_qed_do_open(bs
, options
, flags
, errp
);
513 static void bdrv_qed_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
515 BDRVQEDState
*s
= bs
->opaque
;
517 bs
->bl
.pwrite_zeroes_alignment
= s
->header
.cluster_size
;
520 /* We have nothing to do for QED reopen, stubs just return
522 static int bdrv_qed_reopen_prepare(BDRVReopenState
*state
,
523 BlockReopenQueue
*queue
, Error
**errp
)
528 static void bdrv_qed_close(BlockDriverState
*bs
)
530 BDRVQEDState
*s
= bs
->opaque
;
532 bdrv_qed_detach_aio_context(bs
);
534 /* Ensure writes reach stable storage */
535 bdrv_flush(bs
->file
->bs
);
537 /* Clean shutdown, no check required on next open */
538 if (s
->header
.features
& QED_F_NEED_CHECK
) {
539 s
->header
.features
&= ~QED_F_NEED_CHECK
;
540 qed_write_header_sync(s
);
543 qed_free_l2_cache(&s
->l2_cache
);
544 qemu_vfree(s
->l1_table
);
547 static int qed_create(const char *filename
, uint32_t cluster_size
,
548 uint64_t image_size
, uint32_t table_size
,
549 const char *backing_file
, const char *backing_fmt
,
550 QemuOpts
*opts
, Error
**errp
)
554 .cluster_size
= cluster_size
,
555 .table_size
= table_size
,
558 .compat_features
= 0,
559 .l1_table_offset
= cluster_size
,
560 .image_size
= image_size
,
563 uint8_t *l1_table
= NULL
;
564 size_t l1_size
= header
.cluster_size
* header
.table_size
;
565 Error
*local_err
= NULL
;
569 ret
= bdrv_create_file(filename
, opts
, &local_err
);
571 error_propagate(errp
, local_err
);
575 blk
= blk_new_open(filename
, NULL
, NULL
,
576 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
579 error_propagate(errp
, local_err
);
583 blk_set_allow_write_beyond_eof(blk
, true);
585 /* File must start empty and grow, check truncate is supported */
586 ret
= blk_truncate(blk
, 0, errp
);
592 header
.features
|= QED_F_BACKING_FILE
;
593 header
.backing_filename_offset
= sizeof(le_header
);
594 header
.backing_filename_size
= strlen(backing_file
);
596 if (qed_fmt_is_raw(backing_fmt
)) {
597 header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
601 qed_header_cpu_to_le(&header
, &le_header
);
602 ret
= blk_pwrite(blk
, 0, &le_header
, sizeof(le_header
), 0);
606 ret
= blk_pwrite(blk
, sizeof(le_header
), backing_file
,
607 header
.backing_filename_size
, 0);
612 l1_table
= g_malloc0(l1_size
);
613 ret
= blk_pwrite(blk
, header
.l1_table_offset
, l1_table
, l1_size
, 0);
618 ret
= 0; /* success */
625 static int bdrv_qed_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
627 uint64_t image_size
= 0;
628 uint32_t cluster_size
= QED_DEFAULT_CLUSTER_SIZE
;
629 uint32_t table_size
= QED_DEFAULT_TABLE_SIZE
;
630 char *backing_file
= NULL
;
631 char *backing_fmt
= NULL
;
634 image_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
636 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
637 backing_fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FMT
);
638 cluster_size
= qemu_opt_get_size_del(opts
,
639 BLOCK_OPT_CLUSTER_SIZE
,
640 QED_DEFAULT_CLUSTER_SIZE
);
641 table_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_TABLE_SIZE
,
642 QED_DEFAULT_TABLE_SIZE
);
644 if (!qed_is_cluster_size_valid(cluster_size
)) {
645 error_setg(errp
, "QED cluster size must be within range [%u, %u] "
647 QED_MIN_CLUSTER_SIZE
, QED_MAX_CLUSTER_SIZE
);
651 if (!qed_is_table_size_valid(table_size
)) {
652 error_setg(errp
, "QED table size must be within range [%u, %u] "
654 QED_MIN_TABLE_SIZE
, QED_MAX_TABLE_SIZE
);
658 if (!qed_is_image_size_valid(image_size
, cluster_size
, table_size
)) {
659 error_setg(errp
, "QED image size must be a non-zero multiple of "
660 "cluster size and less than %" PRIu64
" bytes",
661 qed_max_image_size(cluster_size
, table_size
));
666 ret
= qed_create(filename
, cluster_size
, image_size
, table_size
,
667 backing_file
, backing_fmt
, opts
, errp
);
670 g_free(backing_file
);
676 BlockDriverState
*bs
;
681 BlockDriverState
**file
;
684 static void qed_is_allocated_cb(void *opaque
, int ret
, uint64_t offset
, size_t len
)
686 QEDIsAllocatedCB
*cb
= opaque
;
687 BDRVQEDState
*s
= cb
->bs
->opaque
;
688 *cb
->pnum
= len
/ BDRV_SECTOR_SIZE
;
690 case QED_CLUSTER_FOUND
:
691 offset
|= qed_offset_into_cluster(s
, cb
->pos
);
692 cb
->status
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| offset
;
693 *cb
->file
= cb
->bs
->file
->bs
;
695 case QED_CLUSTER_ZERO
:
696 cb
->status
= BDRV_BLOCK_ZERO
;
713 static int64_t coroutine_fn
bdrv_qed_co_get_block_status(BlockDriverState
*bs
,
715 int nb_sectors
, int *pnum
,
716 BlockDriverState
**file
)
718 BDRVQEDState
*s
= bs
->opaque
;
719 size_t len
= (size_t)nb_sectors
* BDRV_SECTOR_SIZE
;
720 QEDIsAllocatedCB cb
= {
722 .pos
= (uint64_t)sector_num
* BDRV_SECTOR_SIZE
,
723 .status
= BDRV_BLOCK_OFFSET_MASK
,
727 QEDRequest request
= { .l2_table
= NULL
};
731 ret
= qed_find_cluster(s
, &request
, cb
.pos
, &len
, &offset
);
732 qed_is_allocated_cb(&cb
, ret
, offset
, len
);
734 /* The callback was invoked immediately */
735 assert(cb
.status
!= BDRV_BLOCK_OFFSET_MASK
);
737 qed_unref_l2_cache_entry(request
.l2_table
);
742 static BDRVQEDState
*acb_to_s(QEDAIOCB
*acb
)
744 return acb
->bs
->opaque
;
748 * Read from the backing file or zero-fill if no backing file
751 * @pos: Byte position in device
752 * @qiov: Destination I/O vector
753 * @backing_qiov: Possibly shortened copy of qiov, to be allocated here
754 * @cb: Completion function
755 * @opaque: User data for completion function
757 * This function reads qiov->size bytes starting at pos from the backing file.
758 * If there is no backing file then zeroes are read.
760 static int coroutine_fn
qed_read_backing_file(BDRVQEDState
*s
, uint64_t pos
,
762 QEMUIOVector
**backing_qiov
)
764 uint64_t backing_length
= 0;
768 /* If there is a backing file, get its length. Treat the absence of a
769 * backing file like a zero length backing file.
771 if (s
->bs
->backing
) {
772 int64_t l
= bdrv_getlength(s
->bs
->backing
->bs
);
779 /* Zero all sectors if reading beyond the end of the backing file */
780 if (pos
>= backing_length
||
781 pos
+ qiov
->size
> backing_length
) {
782 qemu_iovec_memset(qiov
, 0, 0, qiov
->size
);
785 /* Complete now if there are no backing file sectors to read */
786 if (pos
>= backing_length
) {
790 /* If the read straddles the end of the backing file, shorten it */
791 size
= MIN((uint64_t)backing_length
- pos
, qiov
->size
);
793 assert(*backing_qiov
== NULL
);
794 *backing_qiov
= g_new(QEMUIOVector
, 1);
795 qemu_iovec_init(*backing_qiov
, qiov
->niov
);
796 qemu_iovec_concat(*backing_qiov
, qiov
, 0, size
);
798 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_READ_BACKING_AIO
);
799 ret
= bdrv_co_preadv(s
->bs
->backing
, pos
, size
, *backing_qiov
, 0);
807 * Copy data from backing file into the image
810 * @pos: Byte position in device
811 * @len: Number of bytes
812 * @offset: Byte offset in image file
814 static int coroutine_fn
qed_copy_from_backing_file(BDRVQEDState
*s
,
815 uint64_t pos
, uint64_t len
,
819 QEMUIOVector
*backing_qiov
= NULL
;
823 /* Skip copy entirely if there is no work to do */
828 iov
= (struct iovec
) {
829 .iov_base
= qemu_blockalign(s
->bs
, len
),
832 qemu_iovec_init_external(&qiov
, &iov
, 1);
834 ret
= qed_read_backing_file(s
, pos
, &qiov
, &backing_qiov
);
837 qemu_iovec_destroy(backing_qiov
);
838 g_free(backing_qiov
);
846 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_COW_WRITE
);
847 ret
= bdrv_co_pwritev(s
->bs
->file
, offset
, qiov
.size
, &qiov
, 0);
853 qemu_vfree(iov
.iov_base
);
858 * Link one or more contiguous clusters into a table
862 * @index: First cluster index
863 * @n: Number of contiguous clusters
864 * @cluster: First cluster offset
866 * The cluster offset may be an allocated byte offset in the image file, the
867 * zero cluster marker, or the unallocated cluster marker.
869 static void coroutine_fn
qed_update_l2_table(BDRVQEDState
*s
, QEDTable
*table
,
870 int index
, unsigned int n
,
874 for (i
= index
; i
< index
+ n
; i
++) {
875 table
->offsets
[i
] = cluster
;
876 if (!qed_offset_is_unalloc_cluster(cluster
) &&
877 !qed_offset_is_zero_cluster(cluster
)) {
878 cluster
+= s
->header
.cluster_size
;
883 static void coroutine_fn
qed_aio_complete(QEDAIOCB
*acb
)
885 BDRVQEDState
*s
= acb_to_s(acb
);
888 qemu_iovec_destroy(&acb
->cur_qiov
);
889 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
891 /* Free the buffer we may have allocated for zero writes */
892 if (acb
->flags
& QED_AIOCB_ZERO
) {
893 qemu_vfree(acb
->qiov
->iov
[0].iov_base
);
894 acb
->qiov
->iov
[0].iov_base
= NULL
;
897 /* Start next allocating write request waiting behind this one. Note that
898 * requests enqueue themselves when they first hit an unallocated cluster
899 * but they wait until the entire request is finished before waking up the
900 * next request in the queue. This ensures that we don't cycle through
901 * requests multiple times but rather finish one at a time completely.
903 if (acb
== s
->allocating_acb
) {
904 s
->allocating_acb
= NULL
;
905 if (!qemu_co_queue_empty(&s
->allocating_write_reqs
)) {
906 qemu_co_enter_next(&s
->allocating_write_reqs
);
907 } else if (s
->header
.features
& QED_F_NEED_CHECK
) {
908 qed_start_need_check_timer(s
);
914 * Update L1 table with new L2 table offset and write it out
916 static int coroutine_fn
qed_aio_write_l1_update(QEDAIOCB
*acb
)
918 BDRVQEDState
*s
= acb_to_s(acb
);
919 CachedL2Table
*l2_table
= acb
->request
.l2_table
;
920 uint64_t l2_offset
= l2_table
->offset
;
923 index
= qed_l1_index(s
, acb
->cur_pos
);
924 s
->l1_table
->offsets
[index
] = l2_table
->offset
;
926 ret
= qed_write_l1_table(s
, index
, 1);
928 /* Commit the current L2 table to the cache */
929 qed_commit_l2_cache_entry(&s
->l2_cache
, l2_table
);
931 /* This is guaranteed to succeed because we just committed the entry to the
934 acb
->request
.l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, l2_offset
);
935 assert(acb
->request
.l2_table
!= NULL
);
942 * Update L2 table with new cluster offsets and write them out
944 static int coroutine_fn
qed_aio_write_l2_update(QEDAIOCB
*acb
, uint64_t offset
)
946 BDRVQEDState
*s
= acb_to_s(acb
);
947 bool need_alloc
= acb
->find_cluster_ret
== QED_CLUSTER_L1
;
951 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
952 acb
->request
.l2_table
= qed_new_l2_table(s
);
955 index
= qed_l2_index(s
, acb
->cur_pos
);
956 qed_update_l2_table(s
, acb
->request
.l2_table
->table
, index
, acb
->cur_nclusters
,
960 /* Write out the whole new L2 table */
961 ret
= qed_write_l2_table(s
, &acb
->request
, 0, s
->table_nelems
, true);
965 return qed_aio_write_l1_update(acb
);
967 /* Write out only the updated part of the L2 table */
968 ret
= qed_write_l2_table(s
, &acb
->request
, index
, acb
->cur_nclusters
,
978 * Write data to the image file
980 static int coroutine_fn
qed_aio_write_main(QEDAIOCB
*acb
)
982 BDRVQEDState
*s
= acb_to_s(acb
);
983 uint64_t offset
= acb
->cur_cluster
+
984 qed_offset_into_cluster(s
, acb
->cur_pos
);
987 trace_qed_aio_write_main(s
, acb
, 0, offset
, acb
->cur_qiov
.size
);
989 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_WRITE_AIO
);
990 ret
= bdrv_co_pwritev(s
->bs
->file
, offset
, acb
->cur_qiov
.size
,
996 if (acb
->find_cluster_ret
!= QED_CLUSTER_FOUND
) {
997 if (s
->bs
->backing
) {
999 * Flush new data clusters before updating the L2 table
1001 * This flush is necessary when a backing file is in use. A crash
1002 * during an allocating write could result in empty clusters in the
1003 * image. If the write only touched a subregion of the cluster,
1004 * then backing image sectors have been lost in the untouched
1005 * region. The solution is to flush after writing a new data
1006 * cluster and before updating the L2 table.
1008 ret
= bdrv_co_flush(s
->bs
->file
->bs
);
1013 ret
= qed_aio_write_l2_update(acb
, acb
->cur_cluster
);
1022 * Populate untouched regions of new data cluster
1024 static int coroutine_fn
qed_aio_write_cow(QEDAIOCB
*acb
)
1026 BDRVQEDState
*s
= acb_to_s(acb
);
1027 uint64_t start
, len
, offset
;
1030 /* Populate front untouched region of new data cluster */
1031 start
= qed_start_of_cluster(s
, acb
->cur_pos
);
1032 len
= qed_offset_into_cluster(s
, acb
->cur_pos
);
1034 trace_qed_aio_write_prefill(s
, acb
, start
, len
, acb
->cur_cluster
);
1035 ret
= qed_copy_from_backing_file(s
, start
, len
, acb
->cur_cluster
);
1040 /* Populate back untouched region of new data cluster */
1041 start
= acb
->cur_pos
+ acb
->cur_qiov
.size
;
1042 len
= qed_start_of_cluster(s
, start
+ s
->header
.cluster_size
- 1) - start
;
1043 offset
= acb
->cur_cluster
+
1044 qed_offset_into_cluster(s
, acb
->cur_pos
) +
1047 trace_qed_aio_write_postfill(s
, acb
, start
, len
, offset
);
1048 ret
= qed_copy_from_backing_file(s
, start
, len
, offset
);
1053 return qed_aio_write_main(acb
);
1057 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1059 static bool qed_should_set_need_check(BDRVQEDState
*s
)
1061 /* The flush before L2 update path ensures consistency */
1062 if (s
->bs
->backing
) {
1066 return !(s
->header
.features
& QED_F_NEED_CHECK
);
1070 * Write new data cluster
1072 * @acb: Write request
1073 * @len: Length in bytes
1075 * This path is taken when writing to previously unallocated clusters.
1077 static int coroutine_fn
qed_aio_write_alloc(QEDAIOCB
*acb
, size_t len
)
1079 BDRVQEDState
*s
= acb_to_s(acb
);
1082 /* Cancel timer when the first allocating request comes in */
1083 if (s
->allocating_acb
== NULL
) {
1084 qed_cancel_need_check_timer(s
);
1087 /* Freeze this request if another allocating write is in progress */
1088 if (s
->allocating_acb
!= acb
|| s
->allocating_write_reqs_plugged
) {
1089 if (s
->allocating_acb
!= NULL
) {
1090 qemu_co_queue_wait(&s
->allocating_write_reqs
, NULL
);
1091 assert(s
->allocating_acb
== NULL
);
1093 s
->allocating_acb
= acb
;
1094 return -EAGAIN
; /* start over with looking up table entries */
1097 acb
->cur_nclusters
= qed_bytes_to_clusters(s
,
1098 qed_offset_into_cluster(s
, acb
->cur_pos
) + len
);
1099 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1101 if (acb
->flags
& QED_AIOCB_ZERO
) {
1102 /* Skip ahead if the clusters are already zero */
1103 if (acb
->find_cluster_ret
== QED_CLUSTER_ZERO
) {
1107 acb
->cur_cluster
= qed_alloc_clusters(s
, acb
->cur_nclusters
);
1110 if (qed_should_set_need_check(s
)) {
1111 s
->header
.features
|= QED_F_NEED_CHECK
;
1112 ret
= qed_write_header(s
);
1118 if (acb
->flags
& QED_AIOCB_ZERO
) {
1119 ret
= qed_aio_write_l2_update(acb
, 1);
1121 ret
= qed_aio_write_cow(acb
);
1130 * Write data cluster in place
1132 * @acb: Write request
1133 * @offset: Cluster offset in bytes
1134 * @len: Length in bytes
1136 * This path is taken when writing to already allocated clusters.
1138 static int coroutine_fn
qed_aio_write_inplace(QEDAIOCB
*acb
, uint64_t offset
,
1141 /* Allocate buffer for zero writes */
1142 if (acb
->flags
& QED_AIOCB_ZERO
) {
1143 struct iovec
*iov
= acb
->qiov
->iov
;
1145 if (!iov
->iov_base
) {
1146 iov
->iov_base
= qemu_try_blockalign(acb
->bs
, iov
->iov_len
);
1147 if (iov
->iov_base
== NULL
) {
1150 memset(iov
->iov_base
, 0, iov
->iov_len
);
1154 /* Calculate the I/O vector */
1155 acb
->cur_cluster
= offset
;
1156 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1158 /* Do the actual write */
1159 return qed_aio_write_main(acb
);
1163 * Write data cluster
1165 * @opaque: Write request
1166 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
1167 * @offset: Cluster offset in bytes
1168 * @len: Length in bytes
1170 static int coroutine_fn
qed_aio_write_data(void *opaque
, int ret
,
1171 uint64_t offset
, size_t len
)
1173 QEDAIOCB
*acb
= opaque
;
1175 trace_qed_aio_write_data(acb_to_s(acb
), acb
, ret
, offset
, len
);
1177 acb
->find_cluster_ret
= ret
;
1180 case QED_CLUSTER_FOUND
:
1181 return qed_aio_write_inplace(acb
, offset
, len
);
1183 case QED_CLUSTER_L2
:
1184 case QED_CLUSTER_L1
:
1185 case QED_CLUSTER_ZERO
:
1186 return qed_aio_write_alloc(acb
, len
);
1189 g_assert_not_reached();
1196 * @opaque: Read request
1197 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
1198 * @offset: Cluster offset in bytes
1199 * @len: Length in bytes
1201 static int coroutine_fn
qed_aio_read_data(void *opaque
, int ret
,
1202 uint64_t offset
, size_t len
)
1204 QEDAIOCB
*acb
= opaque
;
1205 BDRVQEDState
*s
= acb_to_s(acb
);
1206 BlockDriverState
*bs
= acb
->bs
;
1208 /* Adjust offset into cluster */
1209 offset
+= qed_offset_into_cluster(s
, acb
->cur_pos
);
1211 trace_qed_aio_read_data(s
, acb
, ret
, offset
, len
);
1213 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1215 /* Handle zero cluster and backing file reads */
1216 if (ret
== QED_CLUSTER_ZERO
) {
1217 qemu_iovec_memset(&acb
->cur_qiov
, 0, 0, acb
->cur_qiov
.size
);
1219 } else if (ret
!= QED_CLUSTER_FOUND
) {
1220 return qed_read_backing_file(s
, acb
->cur_pos
, &acb
->cur_qiov
,
1221 &acb
->backing_qiov
);
1224 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
1225 ret
= bdrv_co_preadv(bs
->file
, offset
, acb
->cur_qiov
.size
,
1234 * Begin next I/O or complete the request
1236 static int coroutine_fn
qed_aio_next_io(QEDAIOCB
*acb
)
1238 BDRVQEDState
*s
= acb_to_s(acb
);
1244 trace_qed_aio_next_io(s
, acb
, 0, acb
->cur_pos
+ acb
->cur_qiov
.size
);
1246 if (acb
->backing_qiov
) {
1247 qemu_iovec_destroy(acb
->backing_qiov
);
1248 g_free(acb
->backing_qiov
);
1249 acb
->backing_qiov
= NULL
;
1252 acb
->qiov_offset
+= acb
->cur_qiov
.size
;
1253 acb
->cur_pos
+= acb
->cur_qiov
.size
;
1254 qemu_iovec_reset(&acb
->cur_qiov
);
1256 /* Complete request */
1257 if (acb
->cur_pos
>= acb
->end_pos
) {
1262 /* Find next cluster and start I/O */
1263 len
= acb
->end_pos
- acb
->cur_pos
;
1264 ret
= qed_find_cluster(s
, &acb
->request
, acb
->cur_pos
, &len
, &offset
);
1269 if (acb
->flags
& QED_AIOCB_WRITE
) {
1270 ret
= qed_aio_write_data(acb
, ret
, offset
, len
);
1272 ret
= qed_aio_read_data(acb
, ret
, offset
, len
);
1275 if (ret
< 0 && ret
!= -EAGAIN
) {
1280 trace_qed_aio_complete(s
, acb
, ret
);
1281 qed_aio_complete(acb
);
1285 static int coroutine_fn
qed_co_request(BlockDriverState
*bs
, int64_t sector_num
,
1286 QEMUIOVector
*qiov
, int nb_sectors
,
1291 .cur_pos
= (uint64_t) sector_num
* BDRV_SECTOR_SIZE
,
1292 .end_pos
= (sector_num
+ nb_sectors
) * BDRV_SECTOR_SIZE
,
1296 qemu_iovec_init(&acb
.cur_qiov
, qiov
->niov
);
1298 trace_qed_aio_setup(bs
->opaque
, &acb
, sector_num
, nb_sectors
, NULL
, flags
);
1301 return qed_aio_next_io(&acb
);
1304 static int coroutine_fn
bdrv_qed_co_readv(BlockDriverState
*bs
,
1305 int64_t sector_num
, int nb_sectors
,
1308 return qed_co_request(bs
, sector_num
, qiov
, nb_sectors
, 0);
1311 static int coroutine_fn
bdrv_qed_co_writev(BlockDriverState
*bs
,
1312 int64_t sector_num
, int nb_sectors
,
1315 return qed_co_request(bs
, sector_num
, qiov
, nb_sectors
, QED_AIOCB_WRITE
);
1318 static int coroutine_fn
bdrv_qed_co_pwrite_zeroes(BlockDriverState
*bs
,
1321 BdrvRequestFlags flags
)
1323 BDRVQEDState
*s
= bs
->opaque
;
1327 /* Fall back if the request is not aligned */
1328 if (qed_offset_into_cluster(s
, offset
) ||
1329 qed_offset_into_cluster(s
, bytes
)) {
1333 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1334 * then it will be allocated during request processing.
1336 iov
.iov_base
= NULL
;
1337 iov
.iov_len
= bytes
;
1339 qemu_iovec_init_external(&qiov
, &iov
, 1);
1340 return qed_co_request(bs
, offset
>> BDRV_SECTOR_BITS
, &qiov
,
1341 bytes
>> BDRV_SECTOR_BITS
,
1342 QED_AIOCB_WRITE
| QED_AIOCB_ZERO
);
1345 static int bdrv_qed_truncate(BlockDriverState
*bs
, int64_t offset
, Error
**errp
)
1347 BDRVQEDState
*s
= bs
->opaque
;
1348 uint64_t old_image_size
;
1351 if (!qed_is_image_size_valid(offset
, s
->header
.cluster_size
,
1352 s
->header
.table_size
)) {
1353 error_setg(errp
, "Invalid image size specified");
1357 if ((uint64_t)offset
< s
->header
.image_size
) {
1358 error_setg(errp
, "Shrinking images is currently not supported");
1362 old_image_size
= s
->header
.image_size
;
1363 s
->header
.image_size
= offset
;
1364 ret
= qed_write_header_sync(s
);
1366 s
->header
.image_size
= old_image_size
;
1367 error_setg_errno(errp
, -ret
, "Failed to update the image size");
1372 static int64_t bdrv_qed_getlength(BlockDriverState
*bs
)
1374 BDRVQEDState
*s
= bs
->opaque
;
1375 return s
->header
.image_size
;
1378 static int bdrv_qed_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1380 BDRVQEDState
*s
= bs
->opaque
;
1382 memset(bdi
, 0, sizeof(*bdi
));
1383 bdi
->cluster_size
= s
->header
.cluster_size
;
1384 bdi
->is_dirty
= s
->header
.features
& QED_F_NEED_CHECK
;
1385 bdi
->unallocated_blocks_are_zero
= true;
1386 bdi
->can_write_zeroes_with_unmap
= true;
1390 static int bdrv_qed_change_backing_file(BlockDriverState
*bs
,
1391 const char *backing_file
,
1392 const char *backing_fmt
)
1394 BDRVQEDState
*s
= bs
->opaque
;
1395 QEDHeader new_header
, le_header
;
1397 size_t buffer_len
, backing_file_len
;
1400 /* Refuse to set backing filename if unknown compat feature bits are
1401 * active. If the image uses an unknown compat feature then we may not
1402 * know the layout of data following the header structure and cannot safely
1405 if (backing_file
&& (s
->header
.compat_features
&
1406 ~QED_COMPAT_FEATURE_MASK
)) {
1410 memcpy(&new_header
, &s
->header
, sizeof(new_header
));
1412 new_header
.features
&= ~(QED_F_BACKING_FILE
|
1413 QED_F_BACKING_FORMAT_NO_PROBE
);
1415 /* Adjust feature flags */
1417 new_header
.features
|= QED_F_BACKING_FILE
;
1419 if (qed_fmt_is_raw(backing_fmt
)) {
1420 new_header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
1424 /* Calculate new header size */
1425 backing_file_len
= 0;
1428 backing_file_len
= strlen(backing_file
);
1431 buffer_len
= sizeof(new_header
);
1432 new_header
.backing_filename_offset
= buffer_len
;
1433 new_header
.backing_filename_size
= backing_file_len
;
1434 buffer_len
+= backing_file_len
;
1436 /* Make sure we can rewrite header without failing */
1437 if (buffer_len
> new_header
.header_size
* new_header
.cluster_size
) {
1441 /* Prepare new header */
1442 buffer
= g_malloc(buffer_len
);
1444 qed_header_cpu_to_le(&new_header
, &le_header
);
1445 memcpy(buffer
, &le_header
, sizeof(le_header
));
1446 buffer_len
= sizeof(le_header
);
1449 memcpy(buffer
+ buffer_len
, backing_file
, backing_file_len
);
1450 buffer_len
+= backing_file_len
;
1453 /* Write new header */
1454 ret
= bdrv_pwrite_sync(bs
->file
, 0, buffer
, buffer_len
);
1457 memcpy(&s
->header
, &new_header
, sizeof(new_header
));
1462 static void bdrv_qed_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
1464 BDRVQEDState
*s
= bs
->opaque
;
1465 Error
*local_err
= NULL
;
1470 memset(s
, 0, sizeof(BDRVQEDState
));
1471 ret
= bdrv_qed_do_open(bs
, NULL
, bs
->open_flags
, &local_err
);
1473 error_propagate(errp
, local_err
);
1474 error_prepend(errp
, "Could not reopen qed layer: ");
1476 } else if (ret
< 0) {
1477 error_setg_errno(errp
, -ret
, "Could not reopen qed layer");
1482 static int bdrv_qed_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
1485 BDRVQEDState
*s
= bs
->opaque
;
1487 return qed_check(s
, result
, !!fix
);
1490 static QemuOptsList qed_create_opts
= {
1491 .name
= "qed-create-opts",
1492 .head
= QTAILQ_HEAD_INITIALIZER(qed_create_opts
.head
),
1495 .name
= BLOCK_OPT_SIZE
,
1496 .type
= QEMU_OPT_SIZE
,
1497 .help
= "Virtual disk size"
1500 .name
= BLOCK_OPT_BACKING_FILE
,
1501 .type
= QEMU_OPT_STRING
,
1502 .help
= "File name of a base image"
1505 .name
= BLOCK_OPT_BACKING_FMT
,
1506 .type
= QEMU_OPT_STRING
,
1507 .help
= "Image format of the base image"
1510 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1511 .type
= QEMU_OPT_SIZE
,
1512 .help
= "Cluster size (in bytes)",
1513 .def_value_str
= stringify(QED_DEFAULT_CLUSTER_SIZE
)
1516 .name
= BLOCK_OPT_TABLE_SIZE
,
1517 .type
= QEMU_OPT_SIZE
,
1518 .help
= "L1/L2 table size (in clusters)"
1520 { /* end of list */ }
1524 static BlockDriver bdrv_qed
= {
1525 .format_name
= "qed",
1526 .instance_size
= sizeof(BDRVQEDState
),
1527 .create_opts
= &qed_create_opts
,
1528 .supports_backing
= true,
1530 .bdrv_probe
= bdrv_qed_probe
,
1531 .bdrv_open
= bdrv_qed_open
,
1532 .bdrv_close
= bdrv_qed_close
,
1533 .bdrv_reopen_prepare
= bdrv_qed_reopen_prepare
,
1534 .bdrv_child_perm
= bdrv_format_default_perms
,
1535 .bdrv_create
= bdrv_qed_create
,
1536 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1537 .bdrv_co_get_block_status
= bdrv_qed_co_get_block_status
,
1538 .bdrv_co_readv
= bdrv_qed_co_readv
,
1539 .bdrv_co_writev
= bdrv_qed_co_writev
,
1540 .bdrv_co_pwrite_zeroes
= bdrv_qed_co_pwrite_zeroes
,
1541 .bdrv_truncate
= bdrv_qed_truncate
,
1542 .bdrv_getlength
= bdrv_qed_getlength
,
1543 .bdrv_get_info
= bdrv_qed_get_info
,
1544 .bdrv_refresh_limits
= bdrv_qed_refresh_limits
,
1545 .bdrv_change_backing_file
= bdrv_qed_change_backing_file
,
1546 .bdrv_invalidate_cache
= bdrv_qed_invalidate_cache
,
1547 .bdrv_check
= bdrv_qed_check
,
1548 .bdrv_detach_aio_context
= bdrv_qed_detach_aio_context
,
1549 .bdrv_attach_aio_context
= bdrv_qed_attach_aio_context
,
1550 .bdrv_drain
= bdrv_qed_drain
,
1553 static void bdrv_qed_init(void)
1555 bdrv_register(&bdrv_qed
);
1558 block_init(bdrv_qed_init
);