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 void qed_aio_cancel(BlockDriverAIOCB
*blockacb
)
23 QEDAIOCB
*acb
= (QEDAIOCB
*)blockacb
;
24 bool finished
= false;
26 /* Wait for the request to finish */
27 acb
->finished
= &finished
;
33 static const AIOCBInfo qed_aiocb_info
= {
34 .aiocb_size
= sizeof(QEDAIOCB
),
35 .cancel
= qed_aio_cancel
,
38 static int bdrv_qed_probe(const uint8_t *buf
, int buf_size
,
41 const QEDHeader
*header
= (const QEDHeader
*)buf
;
43 if (buf_size
< sizeof(*header
)) {
46 if (le32_to_cpu(header
->magic
) != QED_MAGIC
) {
53 * Check whether an image format is raw
55 * @fmt: Backing file format, may be NULL
57 static bool qed_fmt_is_raw(const char *fmt
)
59 return fmt
&& strcmp(fmt
, "raw") == 0;
62 static void qed_header_le_to_cpu(const QEDHeader
*le
, QEDHeader
*cpu
)
64 cpu
->magic
= le32_to_cpu(le
->magic
);
65 cpu
->cluster_size
= le32_to_cpu(le
->cluster_size
);
66 cpu
->table_size
= le32_to_cpu(le
->table_size
);
67 cpu
->header_size
= le32_to_cpu(le
->header_size
);
68 cpu
->features
= le64_to_cpu(le
->features
);
69 cpu
->compat_features
= le64_to_cpu(le
->compat_features
);
70 cpu
->autoclear_features
= le64_to_cpu(le
->autoclear_features
);
71 cpu
->l1_table_offset
= le64_to_cpu(le
->l1_table_offset
);
72 cpu
->image_size
= le64_to_cpu(le
->image_size
);
73 cpu
->backing_filename_offset
= le32_to_cpu(le
->backing_filename_offset
);
74 cpu
->backing_filename_size
= le32_to_cpu(le
->backing_filename_size
);
77 static void qed_header_cpu_to_le(const QEDHeader
*cpu
, QEDHeader
*le
)
79 le
->magic
= cpu_to_le32(cpu
->magic
);
80 le
->cluster_size
= cpu_to_le32(cpu
->cluster_size
);
81 le
->table_size
= cpu_to_le32(cpu
->table_size
);
82 le
->header_size
= cpu_to_le32(cpu
->header_size
);
83 le
->features
= cpu_to_le64(cpu
->features
);
84 le
->compat_features
= cpu_to_le64(cpu
->compat_features
);
85 le
->autoclear_features
= cpu_to_le64(cpu
->autoclear_features
);
86 le
->l1_table_offset
= cpu_to_le64(cpu
->l1_table_offset
);
87 le
->image_size
= cpu_to_le64(cpu
->image_size
);
88 le
->backing_filename_offset
= cpu_to_le32(cpu
->backing_filename_offset
);
89 le
->backing_filename_size
= cpu_to_le32(cpu
->backing_filename_size
);
92 int qed_write_header_sync(BDRVQEDState
*s
)
97 qed_header_cpu_to_le(&s
->header
, &le
);
98 ret
= bdrv_pwrite(s
->bs
->file
, 0, &le
, sizeof(le
));
99 if (ret
!= sizeof(le
)) {
114 static void qed_write_header_cb(void *opaque
, int ret
)
116 QEDWriteHeaderCB
*write_header_cb
= opaque
;
118 qemu_vfree(write_header_cb
->buf
);
119 gencb_complete(write_header_cb
, ret
);
122 static void qed_write_header_read_cb(void *opaque
, int ret
)
124 QEDWriteHeaderCB
*write_header_cb
= opaque
;
125 BDRVQEDState
*s
= write_header_cb
->s
;
128 qed_write_header_cb(write_header_cb
, ret
);
133 qed_header_cpu_to_le(&s
->header
, (QEDHeader
*)write_header_cb
->buf
);
135 bdrv_aio_writev(s
->bs
->file
, 0, &write_header_cb
->qiov
,
136 write_header_cb
->nsectors
, qed_write_header_cb
,
141 * Update header in-place (does not rewrite backing filename or other strings)
143 * This function only updates known header fields in-place and does not affect
144 * extra data after the QED header.
146 static void qed_write_header(BDRVQEDState
*s
, BlockDriverCompletionFunc cb
,
149 /* We must write full sectors for O_DIRECT but cannot necessarily generate
150 * the data following the header if an unrecognized compat feature is
151 * active. Therefore, first read the sectors containing the header, update
152 * them, and write back.
155 int nsectors
= (sizeof(QEDHeader
) + BDRV_SECTOR_SIZE
- 1) /
157 size_t len
= nsectors
* BDRV_SECTOR_SIZE
;
158 QEDWriteHeaderCB
*write_header_cb
= gencb_alloc(sizeof(*write_header_cb
),
161 write_header_cb
->s
= s
;
162 write_header_cb
->nsectors
= nsectors
;
163 write_header_cb
->buf
= qemu_blockalign(s
->bs
, len
);
164 write_header_cb
->iov
.iov_base
= write_header_cb
->buf
;
165 write_header_cb
->iov
.iov_len
= len
;
166 qemu_iovec_init_external(&write_header_cb
->qiov
, &write_header_cb
->iov
, 1);
168 bdrv_aio_readv(s
->bs
->file
, 0, &write_header_cb
->qiov
, nsectors
,
169 qed_write_header_read_cb
, write_header_cb
);
172 static uint64_t qed_max_image_size(uint32_t cluster_size
, uint32_t table_size
)
174 uint64_t table_entries
;
177 table_entries
= (table_size
* cluster_size
) / sizeof(uint64_t);
178 l2_size
= table_entries
* cluster_size
;
180 return l2_size
* table_entries
;
183 static bool qed_is_cluster_size_valid(uint32_t cluster_size
)
185 if (cluster_size
< QED_MIN_CLUSTER_SIZE
||
186 cluster_size
> QED_MAX_CLUSTER_SIZE
) {
189 if (cluster_size
& (cluster_size
- 1)) {
190 return false; /* not power of 2 */
195 static bool qed_is_table_size_valid(uint32_t table_size
)
197 if (table_size
< QED_MIN_TABLE_SIZE
||
198 table_size
> QED_MAX_TABLE_SIZE
) {
201 if (table_size
& (table_size
- 1)) {
202 return false; /* not power of 2 */
207 static bool qed_is_image_size_valid(uint64_t image_size
, uint32_t cluster_size
,
210 if (image_size
% BDRV_SECTOR_SIZE
!= 0) {
211 return false; /* not multiple of sector size */
213 if (image_size
> qed_max_image_size(cluster_size
, table_size
)) {
214 return false; /* image is too large */
220 * Read a string of known length from the image file
223 * @offset: File offset to start of string, in bytes
224 * @n: String length in bytes
225 * @buf: Destination buffer
226 * @buflen: Destination buffer length in bytes
227 * @ret: 0 on success, -errno on failure
229 * The string is NUL-terminated.
231 static int qed_read_string(BlockDriverState
*file
, uint64_t offset
, size_t n
,
232 char *buf
, size_t buflen
)
238 ret
= bdrv_pread(file
, offset
, buf
, n
);
247 * Allocate new clusters
250 * @n: Number of contiguous clusters to allocate
251 * @ret: Offset of first allocated cluster
253 * This function only produces the offset where the new clusters should be
254 * written. It updates BDRVQEDState but does not make any changes to the image
257 static uint64_t qed_alloc_clusters(BDRVQEDState
*s
, unsigned int n
)
259 uint64_t offset
= s
->file_size
;
260 s
->file_size
+= n
* s
->header
.cluster_size
;
264 QEDTable
*qed_alloc_table(BDRVQEDState
*s
)
266 /* Honor O_DIRECT memory alignment requirements */
267 return qemu_blockalign(s
->bs
,
268 s
->header
.cluster_size
* s
->header
.table_size
);
272 * Allocate a new zeroed L2 table
274 static CachedL2Table
*qed_new_l2_table(BDRVQEDState
*s
)
276 CachedL2Table
*l2_table
= qed_alloc_l2_cache_entry(&s
->l2_cache
);
278 l2_table
->table
= qed_alloc_table(s
);
279 l2_table
->offset
= qed_alloc_clusters(s
, s
->header
.table_size
);
281 memset(l2_table
->table
->offsets
, 0,
282 s
->header
.cluster_size
* s
->header
.table_size
);
286 static void qed_aio_next_io(void *opaque
, int ret
);
288 static void qed_plug_allocating_write_reqs(BDRVQEDState
*s
)
290 assert(!s
->allocating_write_reqs_plugged
);
292 s
->allocating_write_reqs_plugged
= true;
295 static void qed_unplug_allocating_write_reqs(BDRVQEDState
*s
)
299 assert(s
->allocating_write_reqs_plugged
);
301 s
->allocating_write_reqs_plugged
= false;
303 acb
= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
);
305 qed_aio_next_io(acb
, 0);
309 static void qed_finish_clear_need_check(void *opaque
, int ret
)
314 static void qed_flush_after_clear_need_check(void *opaque
, int ret
)
316 BDRVQEDState
*s
= opaque
;
318 bdrv_aio_flush(s
->bs
, qed_finish_clear_need_check
, s
);
320 /* No need to wait until flush completes */
321 qed_unplug_allocating_write_reqs(s
);
324 static void qed_clear_need_check(void *opaque
, int ret
)
326 BDRVQEDState
*s
= opaque
;
329 qed_unplug_allocating_write_reqs(s
);
333 s
->header
.features
&= ~QED_F_NEED_CHECK
;
334 qed_write_header(s
, qed_flush_after_clear_need_check
, s
);
337 static void qed_need_check_timer_cb(void *opaque
)
339 BDRVQEDState
*s
= opaque
;
341 /* The timer should only fire when allocating writes have drained */
342 assert(!QSIMPLEQ_FIRST(&s
->allocating_write_reqs
));
344 trace_qed_need_check_timer_cb(s
);
346 qed_plug_allocating_write_reqs(s
);
348 /* Ensure writes are on disk before clearing flag */
349 bdrv_aio_flush(s
->bs
, qed_clear_need_check
, s
);
352 static void qed_start_need_check_timer(BDRVQEDState
*s
)
354 trace_qed_start_need_check_timer(s
);
356 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
359 timer_mod(s
->need_check_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
360 get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT
);
363 /* It's okay to call this multiple times or when no timer is started */
364 static void qed_cancel_need_check_timer(BDRVQEDState
*s
)
366 trace_qed_cancel_need_check_timer(s
);
367 timer_del(s
->need_check_timer
);
370 static void bdrv_qed_rebind(BlockDriverState
*bs
)
372 BDRVQEDState
*s
= bs
->opaque
;
376 static int bdrv_qed_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
379 BDRVQEDState
*s
= bs
->opaque
;
385 QSIMPLEQ_INIT(&s
->allocating_write_reqs
);
387 ret
= bdrv_pread(bs
->file
, 0, &le_header
, sizeof(le_header
));
391 qed_header_le_to_cpu(&le_header
, &s
->header
);
393 if (s
->header
.magic
!= QED_MAGIC
) {
396 if (s
->header
.features
& ~QED_FEATURE_MASK
) {
397 /* image uses unsupported feature bits */
399 snprintf(buf
, sizeof(buf
), "%" PRIx64
,
400 s
->header
.features
& ~QED_FEATURE_MASK
);
401 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
402 bs
->device_name
, "QED", buf
);
405 if (!qed_is_cluster_size_valid(s
->header
.cluster_size
)) {
409 /* Round down file size to the last cluster */
410 file_size
= bdrv_getlength(bs
->file
);
414 s
->file_size
= qed_start_of_cluster(s
, file_size
);
416 if (!qed_is_table_size_valid(s
->header
.table_size
)) {
419 if (!qed_is_image_size_valid(s
->header
.image_size
,
420 s
->header
.cluster_size
,
421 s
->header
.table_size
)) {
424 if (!qed_check_table_offset(s
, s
->header
.l1_table_offset
)) {
428 s
->table_nelems
= (s
->header
.cluster_size
* s
->header
.table_size
) /
430 s
->l2_shift
= ffs(s
->header
.cluster_size
) - 1;
431 s
->l2_mask
= s
->table_nelems
- 1;
432 s
->l1_shift
= s
->l2_shift
+ ffs(s
->table_nelems
) - 1;
434 if ((s
->header
.features
& QED_F_BACKING_FILE
)) {
435 if ((uint64_t)s
->header
.backing_filename_offset
+
436 s
->header
.backing_filename_size
>
437 s
->header
.cluster_size
* s
->header
.header_size
) {
441 ret
= qed_read_string(bs
->file
, s
->header
.backing_filename_offset
,
442 s
->header
.backing_filename_size
, bs
->backing_file
,
443 sizeof(bs
->backing_file
));
448 if (s
->header
.features
& QED_F_BACKING_FORMAT_NO_PROBE
) {
449 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), "raw");
453 /* Reset unknown autoclear feature bits. This is a backwards
454 * compatibility mechanism that allows images to be opened by older
455 * programs, which "knock out" unknown feature bits. When an image is
456 * opened by a newer program again it can detect that the autoclear
457 * feature is no longer valid.
459 if ((s
->header
.autoclear_features
& ~QED_AUTOCLEAR_FEATURE_MASK
) != 0 &&
460 !bdrv_is_read_only(bs
->file
) && !(flags
& BDRV_O_INCOMING
)) {
461 s
->header
.autoclear_features
&= QED_AUTOCLEAR_FEATURE_MASK
;
463 ret
= qed_write_header_sync(s
);
468 /* From here on only known autoclear feature bits are valid */
469 bdrv_flush(bs
->file
);
472 s
->l1_table
= qed_alloc_table(s
);
473 qed_init_l2_cache(&s
->l2_cache
);
475 ret
= qed_read_l1_table_sync(s
);
480 /* If image was not closed cleanly, check consistency */
481 if (!(flags
& BDRV_O_CHECK
) && (s
->header
.features
& QED_F_NEED_CHECK
)) {
482 /* Read-only images cannot be fixed. There is no risk of corruption
483 * since write operations are not possible. Therefore, allow
484 * potentially inconsistent images to be opened read-only. This can
485 * aid data recovery from an otherwise inconsistent image.
487 if (!bdrv_is_read_only(bs
->file
) &&
488 !(flags
& BDRV_O_INCOMING
)) {
489 BdrvCheckResult result
= {0};
491 ret
= qed_check(s
, &result
, true);
498 s
->need_check_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
499 qed_need_check_timer_cb
, s
);
503 qed_free_l2_cache(&s
->l2_cache
);
504 qemu_vfree(s
->l1_table
);
509 static int bdrv_qed_refresh_limits(BlockDriverState
*bs
)
511 BDRVQEDState
*s
= bs
->opaque
;
513 bs
->bl
.write_zeroes_alignment
= s
->header
.cluster_size
>> BDRV_SECTOR_BITS
;
518 /* We have nothing to do for QED reopen, stubs just return
520 static int bdrv_qed_reopen_prepare(BDRVReopenState
*state
,
521 BlockReopenQueue
*queue
, Error
**errp
)
526 static void bdrv_qed_close(BlockDriverState
*bs
)
528 BDRVQEDState
*s
= bs
->opaque
;
530 qed_cancel_need_check_timer(s
);
531 timer_free(s
->need_check_timer
);
533 /* Ensure writes reach stable storage */
534 bdrv_flush(bs
->file
);
536 /* Clean shutdown, no check required on next open */
537 if (s
->header
.features
& QED_F_NEED_CHECK
) {
538 s
->header
.features
&= ~QED_F_NEED_CHECK
;
539 qed_write_header_sync(s
);
542 qed_free_l2_cache(&s
->l2_cache
);
543 qemu_vfree(s
->l1_table
);
546 static int qed_create(const char *filename
, uint32_t cluster_size
,
547 uint64_t image_size
, uint32_t table_size
,
548 const char *backing_file
, const char *backing_fmt
)
552 .cluster_size
= cluster_size
,
553 .table_size
= table_size
,
556 .compat_features
= 0,
557 .l1_table_offset
= cluster_size
,
558 .image_size
= image_size
,
561 uint8_t *l1_table
= NULL
;
562 size_t l1_size
= header
.cluster_size
* header
.table_size
;
563 Error
*local_err
= NULL
;
565 BlockDriverState
*bs
= NULL
;
567 ret
= bdrv_create_file(filename
, NULL
, &local_err
);
569 qerror_report_err(local_err
);
570 error_free(local_err
);
574 ret
= bdrv_file_open(&bs
, filename
, NULL
, NULL
,
575 BDRV_O_RDWR
| BDRV_O_CACHE_WB
, &local_err
);
577 qerror_report_err(local_err
);
578 error_free(local_err
);
582 /* File must start empty and grow, check truncate is supported */
583 ret
= bdrv_truncate(bs
, 0);
589 header
.features
|= QED_F_BACKING_FILE
;
590 header
.backing_filename_offset
= sizeof(le_header
);
591 header
.backing_filename_size
= strlen(backing_file
);
593 if (qed_fmt_is_raw(backing_fmt
)) {
594 header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
598 qed_header_cpu_to_le(&header
, &le_header
);
599 ret
= bdrv_pwrite(bs
, 0, &le_header
, sizeof(le_header
));
603 ret
= bdrv_pwrite(bs
, sizeof(le_header
), backing_file
,
604 header
.backing_filename_size
);
609 l1_table
= g_malloc0(l1_size
);
610 ret
= bdrv_pwrite(bs
, header
.l1_table_offset
, l1_table
, l1_size
);
615 ret
= 0; /* success */
622 static int bdrv_qed_create(const char *filename
, QEMUOptionParameter
*options
,
625 uint64_t image_size
= 0;
626 uint32_t cluster_size
= QED_DEFAULT_CLUSTER_SIZE
;
627 uint32_t table_size
= QED_DEFAULT_TABLE_SIZE
;
628 const char *backing_file
= NULL
;
629 const char *backing_fmt
= NULL
;
631 while (options
&& options
->name
) {
632 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
633 image_size
= options
->value
.n
;
634 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
635 backing_file
= options
->value
.s
;
636 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
637 backing_fmt
= options
->value
.s
;
638 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
639 if (options
->value
.n
) {
640 cluster_size
= options
->value
.n
;
642 } else if (!strcmp(options
->name
, BLOCK_OPT_TABLE_SIZE
)) {
643 if (options
->value
.n
) {
644 table_size
= options
->value
.n
;
650 if (!qed_is_cluster_size_valid(cluster_size
)) {
651 fprintf(stderr
, "QED cluster size must be within range [%u, %u] and power of 2\n",
652 QED_MIN_CLUSTER_SIZE
, QED_MAX_CLUSTER_SIZE
);
655 if (!qed_is_table_size_valid(table_size
)) {
656 fprintf(stderr
, "QED table size must be within range [%u, %u] and power of 2\n",
657 QED_MIN_TABLE_SIZE
, QED_MAX_TABLE_SIZE
);
660 if (!qed_is_image_size_valid(image_size
, cluster_size
, table_size
)) {
661 fprintf(stderr
, "QED image size must be a non-zero multiple of "
662 "cluster size and less than %" PRIu64
" bytes\n",
663 qed_max_image_size(cluster_size
, table_size
));
667 return qed_create(filename
, cluster_size
, image_size
, table_size
,
668 backing_file
, backing_fmt
);
672 BlockDriverState
*bs
;
679 static void qed_is_allocated_cb(void *opaque
, int ret
, uint64_t offset
, size_t len
)
681 QEDIsAllocatedCB
*cb
= opaque
;
682 BDRVQEDState
*s
= cb
->bs
->opaque
;
683 *cb
->pnum
= len
/ BDRV_SECTOR_SIZE
;
685 case QED_CLUSTER_FOUND
:
686 offset
|= qed_offset_into_cluster(s
, cb
->pos
);
687 cb
->status
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| offset
;
689 case QED_CLUSTER_ZERO
:
690 cb
->status
= BDRV_BLOCK_ZERO
;
703 qemu_coroutine_enter(cb
->co
, NULL
);
707 static int64_t coroutine_fn
bdrv_qed_co_get_block_status(BlockDriverState
*bs
,
709 int nb_sectors
, int *pnum
)
711 BDRVQEDState
*s
= bs
->opaque
;
712 size_t len
= (size_t)nb_sectors
* BDRV_SECTOR_SIZE
;
713 QEDIsAllocatedCB cb
= {
715 .pos
= (uint64_t)sector_num
* BDRV_SECTOR_SIZE
,
716 .status
= BDRV_BLOCK_OFFSET_MASK
,
719 QEDRequest request
= { .l2_table
= NULL
};
721 qed_find_cluster(s
, &request
, cb
.pos
, len
, qed_is_allocated_cb
, &cb
);
723 /* Now sleep if the callback wasn't invoked immediately */
724 while (cb
.status
== BDRV_BLOCK_OFFSET_MASK
) {
725 cb
.co
= qemu_coroutine_self();
726 qemu_coroutine_yield();
729 qed_unref_l2_cache_entry(request
.l2_table
);
734 static int bdrv_qed_make_empty(BlockDriverState
*bs
)
739 static BDRVQEDState
*acb_to_s(QEDAIOCB
*acb
)
741 return acb
->common
.bs
->opaque
;
745 * Read from the backing file or zero-fill if no backing file
748 * @pos: Byte position in device
749 * @qiov: Destination I/O vector
750 * @cb: Completion function
751 * @opaque: User data for completion function
753 * This function reads qiov->size bytes starting at pos from the backing file.
754 * If there is no backing file then zeroes are read.
756 static void qed_read_backing_file(BDRVQEDState
*s
, uint64_t pos
,
758 BlockDriverCompletionFunc
*cb
, void *opaque
)
760 uint64_t backing_length
= 0;
763 /* If there is a backing file, get its length. Treat the absence of a
764 * backing file like a zero length backing file.
766 if (s
->bs
->backing_hd
) {
767 int64_t l
= bdrv_getlength(s
->bs
->backing_hd
);
775 /* Zero all sectors if reading beyond the end of the backing file */
776 if (pos
>= backing_length
||
777 pos
+ qiov
->size
> backing_length
) {
778 qemu_iovec_memset(qiov
, 0, 0, qiov
->size
);
781 /* Complete now if there are no backing file sectors to read */
782 if (pos
>= backing_length
) {
787 /* If the read straddles the end of the backing file, shorten it */
788 size
= MIN((uint64_t)backing_length
- pos
, qiov
->size
);
790 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_READ_BACKING_AIO
);
791 bdrv_aio_readv(s
->bs
->backing_hd
, pos
/ BDRV_SECTOR_SIZE
,
792 qiov
, size
/ BDRV_SECTOR_SIZE
, cb
, opaque
);
801 } CopyFromBackingFileCB
;
803 static void qed_copy_from_backing_file_cb(void *opaque
, int ret
)
805 CopyFromBackingFileCB
*copy_cb
= opaque
;
806 qemu_vfree(copy_cb
->iov
.iov_base
);
807 gencb_complete(©_cb
->gencb
, ret
);
810 static void qed_copy_from_backing_file_write(void *opaque
, int ret
)
812 CopyFromBackingFileCB
*copy_cb
= opaque
;
813 BDRVQEDState
*s
= copy_cb
->s
;
816 qed_copy_from_backing_file_cb(copy_cb
, ret
);
820 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_COW_WRITE
);
821 bdrv_aio_writev(s
->bs
->file
, copy_cb
->offset
/ BDRV_SECTOR_SIZE
,
822 ©_cb
->qiov
, copy_cb
->qiov
.size
/ BDRV_SECTOR_SIZE
,
823 qed_copy_from_backing_file_cb
, copy_cb
);
827 * Copy data from backing file into the image
830 * @pos: Byte position in device
831 * @len: Number of bytes
832 * @offset: Byte offset in image file
833 * @cb: Completion function
834 * @opaque: User data for completion function
836 static void qed_copy_from_backing_file(BDRVQEDState
*s
, uint64_t pos
,
837 uint64_t len
, uint64_t offset
,
838 BlockDriverCompletionFunc
*cb
,
841 CopyFromBackingFileCB
*copy_cb
;
843 /* Skip copy entirely if there is no work to do */
849 copy_cb
= gencb_alloc(sizeof(*copy_cb
), cb
, opaque
);
851 copy_cb
->offset
= offset
;
852 copy_cb
->iov
.iov_base
= qemu_blockalign(s
->bs
, len
);
853 copy_cb
->iov
.iov_len
= len
;
854 qemu_iovec_init_external(©_cb
->qiov
, ©_cb
->iov
, 1);
856 qed_read_backing_file(s
, pos
, ©_cb
->qiov
,
857 qed_copy_from_backing_file_write
, copy_cb
);
861 * Link one or more contiguous clusters into a table
865 * @index: First cluster index
866 * @n: Number of contiguous clusters
867 * @cluster: First cluster offset
869 * The cluster offset may be an allocated byte offset in the image file, the
870 * zero cluster marker, or the unallocated cluster marker.
872 static void qed_update_l2_table(BDRVQEDState
*s
, QEDTable
*table
, int index
,
873 unsigned int n
, uint64_t cluster
)
876 for (i
= index
; i
< index
+ n
; i
++) {
877 table
->offsets
[i
] = cluster
;
878 if (!qed_offset_is_unalloc_cluster(cluster
) &&
879 !qed_offset_is_zero_cluster(cluster
)) {
880 cluster
+= s
->header
.cluster_size
;
885 static void qed_aio_complete_bh(void *opaque
)
887 QEDAIOCB
*acb
= opaque
;
888 BlockDriverCompletionFunc
*cb
= acb
->common
.cb
;
889 void *user_opaque
= acb
->common
.opaque
;
890 int ret
= acb
->bh_ret
;
891 bool *finished
= acb
->finished
;
893 qemu_bh_delete(acb
->bh
);
894 qemu_aio_release(acb
);
896 /* Invoke callback */
897 cb(user_opaque
, ret
);
899 /* Signal cancel completion */
905 static void qed_aio_complete(QEDAIOCB
*acb
, int ret
)
907 BDRVQEDState
*s
= acb_to_s(acb
);
909 trace_qed_aio_complete(s
, acb
, ret
);
912 qemu_iovec_destroy(&acb
->cur_qiov
);
913 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
915 /* Free the buffer we may have allocated for zero writes */
916 if (acb
->flags
& QED_AIOCB_ZERO
) {
917 qemu_vfree(acb
->qiov
->iov
[0].iov_base
);
918 acb
->qiov
->iov
[0].iov_base
= NULL
;
921 /* Arrange for a bh to invoke the completion function */
923 acb
->bh
= qemu_bh_new(qed_aio_complete_bh
, acb
);
924 qemu_bh_schedule(acb
->bh
);
926 /* Start next allocating write request waiting behind this one. Note that
927 * requests enqueue themselves when they first hit an unallocated cluster
928 * but they wait until the entire request is finished before waking up the
929 * next request in the queue. This ensures that we don't cycle through
930 * requests multiple times but rather finish one at a time completely.
932 if (acb
== QSIMPLEQ_FIRST(&s
->allocating_write_reqs
)) {
933 QSIMPLEQ_REMOVE_HEAD(&s
->allocating_write_reqs
, next
);
934 acb
= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
);
936 qed_aio_next_io(acb
, 0);
937 } else if (s
->header
.features
& QED_F_NEED_CHECK
) {
938 qed_start_need_check_timer(s
);
944 * Commit the current L2 table to the cache
946 static void qed_commit_l2_update(void *opaque
, int ret
)
948 QEDAIOCB
*acb
= opaque
;
949 BDRVQEDState
*s
= acb_to_s(acb
);
950 CachedL2Table
*l2_table
= acb
->request
.l2_table
;
951 uint64_t l2_offset
= l2_table
->offset
;
953 qed_commit_l2_cache_entry(&s
->l2_cache
, l2_table
);
955 /* This is guaranteed to succeed because we just committed the entry to the
958 acb
->request
.l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
, l2_offset
);
959 assert(acb
->request
.l2_table
!= NULL
);
961 qed_aio_next_io(opaque
, ret
);
965 * Update L1 table with new L2 table offset and write it out
967 static void qed_aio_write_l1_update(void *opaque
, int ret
)
969 QEDAIOCB
*acb
= opaque
;
970 BDRVQEDState
*s
= acb_to_s(acb
);
974 qed_aio_complete(acb
, ret
);
978 index
= qed_l1_index(s
, acb
->cur_pos
);
979 s
->l1_table
->offsets
[index
] = acb
->request
.l2_table
->offset
;
981 qed_write_l1_table(s
, index
, 1, qed_commit_l2_update
, acb
);
985 * Update L2 table with new cluster offsets and write them out
987 static void qed_aio_write_l2_update(QEDAIOCB
*acb
, int ret
, uint64_t offset
)
989 BDRVQEDState
*s
= acb_to_s(acb
);
990 bool need_alloc
= acb
->find_cluster_ret
== QED_CLUSTER_L1
;
998 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
999 acb
->request
.l2_table
= qed_new_l2_table(s
);
1002 index
= qed_l2_index(s
, acb
->cur_pos
);
1003 qed_update_l2_table(s
, acb
->request
.l2_table
->table
, index
, acb
->cur_nclusters
,
1007 /* Write out the whole new L2 table */
1008 qed_write_l2_table(s
, &acb
->request
, 0, s
->table_nelems
, true,
1009 qed_aio_write_l1_update
, acb
);
1011 /* Write out only the updated part of the L2 table */
1012 qed_write_l2_table(s
, &acb
->request
, index
, acb
->cur_nclusters
, false,
1013 qed_aio_next_io
, acb
);
1018 qed_aio_complete(acb
, ret
);
1021 static void qed_aio_write_l2_update_cb(void *opaque
, int ret
)
1023 QEDAIOCB
*acb
= opaque
;
1024 qed_aio_write_l2_update(acb
, ret
, acb
->cur_cluster
);
1028 * Flush new data clusters before updating the L2 table
1030 * This flush is necessary when a backing file is in use. A crash during an
1031 * allocating write could result in empty clusters in the image. If the write
1032 * only touched a subregion of the cluster, then backing image sectors have
1033 * been lost in the untouched region. The solution is to flush after writing a
1034 * new data cluster and before updating the L2 table.
1036 static void qed_aio_write_flush_before_l2_update(void *opaque
, int ret
)
1038 QEDAIOCB
*acb
= opaque
;
1039 BDRVQEDState
*s
= acb_to_s(acb
);
1041 if (!bdrv_aio_flush(s
->bs
->file
, qed_aio_write_l2_update_cb
, opaque
)) {
1042 qed_aio_complete(acb
, -EIO
);
1047 * Write data to the image file
1049 static void qed_aio_write_main(void *opaque
, int ret
)
1051 QEDAIOCB
*acb
= opaque
;
1052 BDRVQEDState
*s
= acb_to_s(acb
);
1053 uint64_t offset
= acb
->cur_cluster
+
1054 qed_offset_into_cluster(s
, acb
->cur_pos
);
1055 BlockDriverCompletionFunc
*next_fn
;
1057 trace_qed_aio_write_main(s
, acb
, ret
, offset
, acb
->cur_qiov
.size
);
1060 qed_aio_complete(acb
, ret
);
1064 if (acb
->find_cluster_ret
== QED_CLUSTER_FOUND
) {
1065 next_fn
= qed_aio_next_io
;
1067 if (s
->bs
->backing_hd
) {
1068 next_fn
= qed_aio_write_flush_before_l2_update
;
1070 next_fn
= qed_aio_write_l2_update_cb
;
1074 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_WRITE_AIO
);
1075 bdrv_aio_writev(s
->bs
->file
, offset
/ BDRV_SECTOR_SIZE
,
1076 &acb
->cur_qiov
, acb
->cur_qiov
.size
/ BDRV_SECTOR_SIZE
,
1081 * Populate back untouched region of new data cluster
1083 static void qed_aio_write_postfill(void *opaque
, int ret
)
1085 QEDAIOCB
*acb
= opaque
;
1086 BDRVQEDState
*s
= acb_to_s(acb
);
1087 uint64_t start
= acb
->cur_pos
+ acb
->cur_qiov
.size
;
1089 qed_start_of_cluster(s
, start
+ s
->header
.cluster_size
- 1) - start
;
1090 uint64_t offset
= acb
->cur_cluster
+
1091 qed_offset_into_cluster(s
, acb
->cur_pos
) +
1095 qed_aio_complete(acb
, ret
);
1099 trace_qed_aio_write_postfill(s
, acb
, start
, len
, offset
);
1100 qed_copy_from_backing_file(s
, start
, len
, offset
,
1101 qed_aio_write_main
, acb
);
1105 * Populate front untouched region of new data cluster
1107 static void qed_aio_write_prefill(void *opaque
, int ret
)
1109 QEDAIOCB
*acb
= opaque
;
1110 BDRVQEDState
*s
= acb_to_s(acb
);
1111 uint64_t start
= qed_start_of_cluster(s
, acb
->cur_pos
);
1112 uint64_t len
= qed_offset_into_cluster(s
, acb
->cur_pos
);
1114 trace_qed_aio_write_prefill(s
, acb
, start
, len
, acb
->cur_cluster
);
1115 qed_copy_from_backing_file(s
, start
, len
, acb
->cur_cluster
,
1116 qed_aio_write_postfill
, acb
);
1120 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1122 static bool qed_should_set_need_check(BDRVQEDState
*s
)
1124 /* The flush before L2 update path ensures consistency */
1125 if (s
->bs
->backing_hd
) {
1129 return !(s
->header
.features
& QED_F_NEED_CHECK
);
1132 static void qed_aio_write_zero_cluster(void *opaque
, int ret
)
1134 QEDAIOCB
*acb
= opaque
;
1137 qed_aio_complete(acb
, ret
);
1141 qed_aio_write_l2_update(acb
, 0, 1);
1145 * Write new data cluster
1147 * @acb: Write request
1148 * @len: Length in bytes
1150 * This path is taken when writing to previously unallocated clusters.
1152 static void qed_aio_write_alloc(QEDAIOCB
*acb
, size_t len
)
1154 BDRVQEDState
*s
= acb_to_s(acb
);
1155 BlockDriverCompletionFunc
*cb
;
1157 /* Cancel timer when the first allocating request comes in */
1158 if (QSIMPLEQ_EMPTY(&s
->allocating_write_reqs
)) {
1159 qed_cancel_need_check_timer(s
);
1162 /* Freeze this request if another allocating write is in progress */
1163 if (acb
!= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
)) {
1164 QSIMPLEQ_INSERT_TAIL(&s
->allocating_write_reqs
, acb
, next
);
1166 if (acb
!= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
) ||
1167 s
->allocating_write_reqs_plugged
) {
1168 return; /* wait for existing request to finish */
1171 acb
->cur_nclusters
= qed_bytes_to_clusters(s
,
1172 qed_offset_into_cluster(s
, acb
->cur_pos
) + len
);
1173 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1175 if (acb
->flags
& QED_AIOCB_ZERO
) {
1176 /* Skip ahead if the clusters are already zero */
1177 if (acb
->find_cluster_ret
== QED_CLUSTER_ZERO
) {
1178 qed_aio_next_io(acb
, 0);
1182 cb
= qed_aio_write_zero_cluster
;
1184 cb
= qed_aio_write_prefill
;
1185 acb
->cur_cluster
= qed_alloc_clusters(s
, acb
->cur_nclusters
);
1188 if (qed_should_set_need_check(s
)) {
1189 s
->header
.features
|= QED_F_NEED_CHECK
;
1190 qed_write_header(s
, cb
, acb
);
1197 * Write data cluster in place
1199 * @acb: Write request
1200 * @offset: Cluster offset in bytes
1201 * @len: Length in bytes
1203 * This path is taken when writing to already allocated clusters.
1205 static void qed_aio_write_inplace(QEDAIOCB
*acb
, uint64_t offset
, size_t len
)
1207 /* Allocate buffer for zero writes */
1208 if (acb
->flags
& QED_AIOCB_ZERO
) {
1209 struct iovec
*iov
= acb
->qiov
->iov
;
1211 if (!iov
->iov_base
) {
1212 iov
->iov_base
= qemu_blockalign(acb
->common
.bs
, iov
->iov_len
);
1213 memset(iov
->iov_base
, 0, iov
->iov_len
);
1217 /* Calculate the I/O vector */
1218 acb
->cur_cluster
= offset
;
1219 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1221 /* Do the actual write */
1222 qed_aio_write_main(acb
, 0);
1226 * Write data cluster
1228 * @opaque: Write request
1229 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1231 * @offset: Cluster offset in bytes
1232 * @len: Length in bytes
1234 * Callback from qed_find_cluster().
1236 static void qed_aio_write_data(void *opaque
, int ret
,
1237 uint64_t offset
, size_t len
)
1239 QEDAIOCB
*acb
= opaque
;
1241 trace_qed_aio_write_data(acb_to_s(acb
), acb
, ret
, offset
, len
);
1243 acb
->find_cluster_ret
= ret
;
1246 case QED_CLUSTER_FOUND
:
1247 qed_aio_write_inplace(acb
, offset
, len
);
1250 case QED_CLUSTER_L2
:
1251 case QED_CLUSTER_L1
:
1252 case QED_CLUSTER_ZERO
:
1253 qed_aio_write_alloc(acb
, len
);
1257 qed_aio_complete(acb
, ret
);
1265 * @opaque: Read request
1266 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1268 * @offset: Cluster offset in bytes
1269 * @len: Length in bytes
1271 * Callback from qed_find_cluster().
1273 static void qed_aio_read_data(void *opaque
, int ret
,
1274 uint64_t offset
, size_t len
)
1276 QEDAIOCB
*acb
= opaque
;
1277 BDRVQEDState
*s
= acb_to_s(acb
);
1278 BlockDriverState
*bs
= acb
->common
.bs
;
1280 /* Adjust offset into cluster */
1281 offset
+= qed_offset_into_cluster(s
, acb
->cur_pos
);
1283 trace_qed_aio_read_data(s
, acb
, ret
, offset
, len
);
1289 qemu_iovec_concat(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
1291 /* Handle zero cluster and backing file reads */
1292 if (ret
== QED_CLUSTER_ZERO
) {
1293 qemu_iovec_memset(&acb
->cur_qiov
, 0, 0, acb
->cur_qiov
.size
);
1294 qed_aio_next_io(acb
, 0);
1296 } else if (ret
!= QED_CLUSTER_FOUND
) {
1297 qed_read_backing_file(s
, acb
->cur_pos
, &acb
->cur_qiov
,
1298 qed_aio_next_io
, acb
);
1302 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
1303 bdrv_aio_readv(bs
->file
, offset
/ BDRV_SECTOR_SIZE
,
1304 &acb
->cur_qiov
, acb
->cur_qiov
.size
/ BDRV_SECTOR_SIZE
,
1305 qed_aio_next_io
, acb
);
1309 qed_aio_complete(acb
, ret
);
1313 * Begin next I/O or complete the request
1315 static void qed_aio_next_io(void *opaque
, int ret
)
1317 QEDAIOCB
*acb
= opaque
;
1318 BDRVQEDState
*s
= acb_to_s(acb
);
1319 QEDFindClusterFunc
*io_fn
= (acb
->flags
& QED_AIOCB_WRITE
) ?
1320 qed_aio_write_data
: qed_aio_read_data
;
1322 trace_qed_aio_next_io(s
, acb
, ret
, acb
->cur_pos
+ acb
->cur_qiov
.size
);
1324 /* Handle I/O error */
1326 qed_aio_complete(acb
, ret
);
1330 acb
->qiov_offset
+= acb
->cur_qiov
.size
;
1331 acb
->cur_pos
+= acb
->cur_qiov
.size
;
1332 qemu_iovec_reset(&acb
->cur_qiov
);
1334 /* Complete request */
1335 if (acb
->cur_pos
>= acb
->end_pos
) {
1336 qed_aio_complete(acb
, 0);
1340 /* Find next cluster and start I/O */
1341 qed_find_cluster(s
, &acb
->request
,
1342 acb
->cur_pos
, acb
->end_pos
- acb
->cur_pos
,
1346 static BlockDriverAIOCB
*qed_aio_setup(BlockDriverState
*bs
,
1348 QEMUIOVector
*qiov
, int nb_sectors
,
1349 BlockDriverCompletionFunc
*cb
,
1350 void *opaque
, int flags
)
1352 QEDAIOCB
*acb
= qemu_aio_get(&qed_aiocb_info
, bs
, cb
, opaque
);
1354 trace_qed_aio_setup(bs
->opaque
, acb
, sector_num
, nb_sectors
,
1358 acb
->finished
= NULL
;
1360 acb
->qiov_offset
= 0;
1361 acb
->cur_pos
= (uint64_t)sector_num
* BDRV_SECTOR_SIZE
;
1362 acb
->end_pos
= acb
->cur_pos
+ nb_sectors
* BDRV_SECTOR_SIZE
;
1363 acb
->request
.l2_table
= NULL
;
1364 qemu_iovec_init(&acb
->cur_qiov
, qiov
->niov
);
1367 qed_aio_next_io(acb
, 0);
1368 return &acb
->common
;
1371 static BlockDriverAIOCB
*bdrv_qed_aio_readv(BlockDriverState
*bs
,
1373 QEMUIOVector
*qiov
, int nb_sectors
,
1374 BlockDriverCompletionFunc
*cb
,
1377 return qed_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1380 static BlockDriverAIOCB
*bdrv_qed_aio_writev(BlockDriverState
*bs
,
1382 QEMUIOVector
*qiov
, int nb_sectors
,
1383 BlockDriverCompletionFunc
*cb
,
1386 return qed_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
,
1387 opaque
, QED_AIOCB_WRITE
);
1396 static void coroutine_fn
qed_co_write_zeroes_cb(void *opaque
, int ret
)
1398 QEDWriteZeroesCB
*cb
= opaque
;
1403 qemu_coroutine_enter(cb
->co
, NULL
);
1407 static int coroutine_fn
bdrv_qed_co_write_zeroes(BlockDriverState
*bs
,
1410 BdrvRequestFlags flags
)
1412 BlockDriverAIOCB
*blockacb
;
1413 BDRVQEDState
*s
= bs
->opaque
;
1414 QEDWriteZeroesCB cb
= { .done
= false };
1418 /* Refuse if there are untouched backing file sectors */
1419 if (bs
->backing_hd
) {
1420 if (qed_offset_into_cluster(s
, sector_num
* BDRV_SECTOR_SIZE
) != 0) {
1423 if (qed_offset_into_cluster(s
, nb_sectors
* BDRV_SECTOR_SIZE
) != 0) {
1428 /* Zero writes start without an I/O buffer. If a buffer becomes necessary
1429 * then it will be allocated during request processing.
1431 iov
.iov_base
= NULL
,
1432 iov
.iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
1434 qemu_iovec_init_external(&qiov
, &iov
, 1);
1435 blockacb
= qed_aio_setup(bs
, sector_num
, &qiov
, nb_sectors
,
1436 qed_co_write_zeroes_cb
, &cb
,
1437 QED_AIOCB_WRITE
| QED_AIOCB_ZERO
);
1442 cb
.co
= qemu_coroutine_self();
1443 qemu_coroutine_yield();
1449 static int bdrv_qed_truncate(BlockDriverState
*bs
, int64_t offset
)
1451 BDRVQEDState
*s
= bs
->opaque
;
1452 uint64_t old_image_size
;
1455 if (!qed_is_image_size_valid(offset
, s
->header
.cluster_size
,
1456 s
->header
.table_size
)) {
1460 /* Shrinking is currently not supported */
1461 if ((uint64_t)offset
< s
->header
.image_size
) {
1465 old_image_size
= s
->header
.image_size
;
1466 s
->header
.image_size
= offset
;
1467 ret
= qed_write_header_sync(s
);
1469 s
->header
.image_size
= old_image_size
;
1474 static int64_t bdrv_qed_getlength(BlockDriverState
*bs
)
1476 BDRVQEDState
*s
= bs
->opaque
;
1477 return s
->header
.image_size
;
1480 static int bdrv_qed_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1482 BDRVQEDState
*s
= bs
->opaque
;
1484 memset(bdi
, 0, sizeof(*bdi
));
1485 bdi
->cluster_size
= s
->header
.cluster_size
;
1486 bdi
->is_dirty
= s
->header
.features
& QED_F_NEED_CHECK
;
1487 bdi
->unallocated_blocks_are_zero
= true;
1488 bdi
->can_write_zeroes_with_unmap
= true;
1492 static int bdrv_qed_change_backing_file(BlockDriverState
*bs
,
1493 const char *backing_file
,
1494 const char *backing_fmt
)
1496 BDRVQEDState
*s
= bs
->opaque
;
1497 QEDHeader new_header
, le_header
;
1499 size_t buffer_len
, backing_file_len
;
1502 /* Refuse to set backing filename if unknown compat feature bits are
1503 * active. If the image uses an unknown compat feature then we may not
1504 * know the layout of data following the header structure and cannot safely
1507 if (backing_file
&& (s
->header
.compat_features
&
1508 ~QED_COMPAT_FEATURE_MASK
)) {
1512 memcpy(&new_header
, &s
->header
, sizeof(new_header
));
1514 new_header
.features
&= ~(QED_F_BACKING_FILE
|
1515 QED_F_BACKING_FORMAT_NO_PROBE
);
1517 /* Adjust feature flags */
1519 new_header
.features
|= QED_F_BACKING_FILE
;
1521 if (qed_fmt_is_raw(backing_fmt
)) {
1522 new_header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
1526 /* Calculate new header size */
1527 backing_file_len
= 0;
1530 backing_file_len
= strlen(backing_file
);
1533 buffer_len
= sizeof(new_header
);
1534 new_header
.backing_filename_offset
= buffer_len
;
1535 new_header
.backing_filename_size
= backing_file_len
;
1536 buffer_len
+= backing_file_len
;
1538 /* Make sure we can rewrite header without failing */
1539 if (buffer_len
> new_header
.header_size
* new_header
.cluster_size
) {
1543 /* Prepare new header */
1544 buffer
= g_malloc(buffer_len
);
1546 qed_header_cpu_to_le(&new_header
, &le_header
);
1547 memcpy(buffer
, &le_header
, sizeof(le_header
));
1548 buffer_len
= sizeof(le_header
);
1551 memcpy(buffer
+ buffer_len
, backing_file
, backing_file_len
);
1552 buffer_len
+= backing_file_len
;
1555 /* Write new header */
1556 ret
= bdrv_pwrite_sync(bs
->file
, 0, buffer
, buffer_len
);
1559 memcpy(&s
->header
, &new_header
, sizeof(new_header
));
1564 static void bdrv_qed_invalidate_cache(BlockDriverState
*bs
)
1566 BDRVQEDState
*s
= bs
->opaque
;
1569 memset(s
, 0, sizeof(BDRVQEDState
));
1570 bdrv_qed_open(bs
, NULL
, bs
->open_flags
, NULL
);
1573 static int bdrv_qed_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
1576 BDRVQEDState
*s
= bs
->opaque
;
1578 return qed_check(s
, result
, !!fix
);
1581 static QEMUOptionParameter qed_create_options
[] = {
1583 .name
= BLOCK_OPT_SIZE
,
1585 .help
= "Virtual disk size (in bytes)"
1587 .name
= BLOCK_OPT_BACKING_FILE
,
1589 .help
= "File name of a base image"
1591 .name
= BLOCK_OPT_BACKING_FMT
,
1593 .help
= "Image format of the base image"
1595 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1597 .help
= "Cluster size (in bytes)",
1598 .value
= { .n
= QED_DEFAULT_CLUSTER_SIZE
},
1600 .name
= BLOCK_OPT_TABLE_SIZE
,
1602 .help
= "L1/L2 table size (in clusters)"
1604 { /* end of list */ }
1607 static BlockDriver bdrv_qed
= {
1608 .format_name
= "qed",
1609 .instance_size
= sizeof(BDRVQEDState
),
1610 .create_options
= qed_create_options
,
1612 .bdrv_probe
= bdrv_qed_probe
,
1613 .bdrv_rebind
= bdrv_qed_rebind
,
1614 .bdrv_open
= bdrv_qed_open
,
1615 .bdrv_close
= bdrv_qed_close
,
1616 .bdrv_reopen_prepare
= bdrv_qed_reopen_prepare
,
1617 .bdrv_create
= bdrv_qed_create
,
1618 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1619 .bdrv_co_get_block_status
= bdrv_qed_co_get_block_status
,
1620 .bdrv_make_empty
= bdrv_qed_make_empty
,
1621 .bdrv_aio_readv
= bdrv_qed_aio_readv
,
1622 .bdrv_aio_writev
= bdrv_qed_aio_writev
,
1623 .bdrv_co_write_zeroes
= bdrv_qed_co_write_zeroes
,
1624 .bdrv_truncate
= bdrv_qed_truncate
,
1625 .bdrv_getlength
= bdrv_qed_getlength
,
1626 .bdrv_get_info
= bdrv_qed_get_info
,
1627 .bdrv_refresh_limits
= bdrv_qed_refresh_limits
,
1628 .bdrv_change_backing_file
= bdrv_qed_change_backing_file
,
1629 .bdrv_invalidate_cache
= bdrv_qed_invalidate_cache
,
1630 .bdrv_check
= bdrv_qed_check
,
1633 static void bdrv_qed_init(void)
1635 bdrv_register(&bdrv_qed
);
1638 block_init(bdrv_qed_init
);