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.
18 static void qed_aio_cancel(BlockDriverAIOCB
*blockacb
)
20 QEDAIOCB
*acb
= (QEDAIOCB
*)blockacb
;
21 bool finished
= false;
23 /* Wait for the request to finish */
24 acb
->finished
= &finished
;
30 static AIOPool qed_aio_pool
= {
31 .aiocb_size
= sizeof(QEDAIOCB
),
32 .cancel
= qed_aio_cancel
,
35 static int bdrv_qed_probe(const uint8_t *buf
, int buf_size
,
38 const QEDHeader
*header
= (const QEDHeader
*)buf
;
40 if (buf_size
< sizeof(*header
)) {
43 if (le32_to_cpu(header
->magic
) != QED_MAGIC
) {
50 * Check whether an image format is raw
52 * @fmt: Backing file format, may be NULL
54 static bool qed_fmt_is_raw(const char *fmt
)
56 return fmt
&& strcmp(fmt
, "raw") == 0;
59 static void qed_header_le_to_cpu(const QEDHeader
*le
, QEDHeader
*cpu
)
61 cpu
->magic
= le32_to_cpu(le
->magic
);
62 cpu
->cluster_size
= le32_to_cpu(le
->cluster_size
);
63 cpu
->table_size
= le32_to_cpu(le
->table_size
);
64 cpu
->header_size
= le32_to_cpu(le
->header_size
);
65 cpu
->features
= le64_to_cpu(le
->features
);
66 cpu
->compat_features
= le64_to_cpu(le
->compat_features
);
67 cpu
->autoclear_features
= le64_to_cpu(le
->autoclear_features
);
68 cpu
->l1_table_offset
= le64_to_cpu(le
->l1_table_offset
);
69 cpu
->image_size
= le64_to_cpu(le
->image_size
);
70 cpu
->backing_filename_offset
= le32_to_cpu(le
->backing_filename_offset
);
71 cpu
->backing_filename_size
= le32_to_cpu(le
->backing_filename_size
);
74 static void qed_header_cpu_to_le(const QEDHeader
*cpu
, QEDHeader
*le
)
76 le
->magic
= cpu_to_le32(cpu
->magic
);
77 le
->cluster_size
= cpu_to_le32(cpu
->cluster_size
);
78 le
->table_size
= cpu_to_le32(cpu
->table_size
);
79 le
->header_size
= cpu_to_le32(cpu
->header_size
);
80 le
->features
= cpu_to_le64(cpu
->features
);
81 le
->compat_features
= cpu_to_le64(cpu
->compat_features
);
82 le
->autoclear_features
= cpu_to_le64(cpu
->autoclear_features
);
83 le
->l1_table_offset
= cpu_to_le64(cpu
->l1_table_offset
);
84 le
->image_size
= cpu_to_le64(cpu
->image_size
);
85 le
->backing_filename_offset
= cpu_to_le32(cpu
->backing_filename_offset
);
86 le
->backing_filename_size
= cpu_to_le32(cpu
->backing_filename_size
);
89 static int qed_write_header_sync(BDRVQEDState
*s
)
94 qed_header_cpu_to_le(&s
->header
, &le
);
95 ret
= bdrv_pwrite(s
->bs
->file
, 0, &le
, sizeof(le
));
96 if (ret
!= sizeof(le
)) {
102 static uint64_t qed_max_image_size(uint32_t cluster_size
, uint32_t table_size
)
104 uint64_t table_entries
;
107 table_entries
= (table_size
* cluster_size
) / sizeof(uint64_t);
108 l2_size
= table_entries
* cluster_size
;
110 return l2_size
* table_entries
;
113 static bool qed_is_cluster_size_valid(uint32_t cluster_size
)
115 if (cluster_size
< QED_MIN_CLUSTER_SIZE
||
116 cluster_size
> QED_MAX_CLUSTER_SIZE
) {
119 if (cluster_size
& (cluster_size
- 1)) {
120 return false; /* not power of 2 */
125 static bool qed_is_table_size_valid(uint32_t table_size
)
127 if (table_size
< QED_MIN_TABLE_SIZE
||
128 table_size
> QED_MAX_TABLE_SIZE
) {
131 if (table_size
& (table_size
- 1)) {
132 return false; /* not power of 2 */
137 static bool qed_is_image_size_valid(uint64_t image_size
, uint32_t cluster_size
,
140 if (image_size
% BDRV_SECTOR_SIZE
!= 0) {
141 return false; /* not multiple of sector size */
143 if (image_size
> qed_max_image_size(cluster_size
, table_size
)) {
144 return false; /* image is too large */
150 * Read a string of known length from the image file
153 * @offset: File offset to start of string, in bytes
154 * @n: String length in bytes
155 * @buf: Destination buffer
156 * @buflen: Destination buffer length in bytes
157 * @ret: 0 on success, -errno on failure
159 * The string is NUL-terminated.
161 static int qed_read_string(BlockDriverState
*file
, uint64_t offset
, size_t n
,
162 char *buf
, size_t buflen
)
168 ret
= bdrv_pread(file
, offset
, buf
, n
);
177 * Allocate new clusters
180 * @n: Number of contiguous clusters to allocate
181 * @ret: Offset of first allocated cluster
183 * This function only produces the offset where the new clusters should be
184 * written. It updates BDRVQEDState but does not make any changes to the image
187 static uint64_t qed_alloc_clusters(BDRVQEDState
*s
, unsigned int n
)
189 uint64_t offset
= s
->file_size
;
190 s
->file_size
+= n
* s
->header
.cluster_size
;
194 QEDTable
*qed_alloc_table(BDRVQEDState
*s
)
196 /* Honor O_DIRECT memory alignment requirements */
197 return qemu_blockalign(s
->bs
,
198 s
->header
.cluster_size
* s
->header
.table_size
);
202 * Allocate a new zeroed L2 table
204 static CachedL2Table
*qed_new_l2_table(BDRVQEDState
*s
)
206 CachedL2Table
*l2_table
= qed_alloc_l2_cache_entry(&s
->l2_cache
);
208 l2_table
->table
= qed_alloc_table(s
);
209 l2_table
->offset
= qed_alloc_clusters(s
, s
->header
.table_size
);
211 memset(l2_table
->table
->offsets
, 0,
212 s
->header
.cluster_size
* s
->header
.table_size
);
216 static void qed_aio_next_io(void *opaque
, int ret
);
218 static int bdrv_qed_open(BlockDriverState
*bs
, int flags
)
220 BDRVQEDState
*s
= bs
->opaque
;
226 QSIMPLEQ_INIT(&s
->allocating_write_reqs
);
228 ret
= bdrv_pread(bs
->file
, 0, &le_header
, sizeof(le_header
));
232 ret
= 0; /* ret should always be 0 or -errno */
233 qed_header_le_to_cpu(&le_header
, &s
->header
);
235 if (s
->header
.magic
!= QED_MAGIC
) {
238 if (s
->header
.features
& ~QED_FEATURE_MASK
) {
239 return -ENOTSUP
; /* image uses unsupported feature bits */
241 if (!qed_is_cluster_size_valid(s
->header
.cluster_size
)) {
245 /* Round down file size to the last cluster */
246 file_size
= bdrv_getlength(bs
->file
);
250 s
->file_size
= qed_start_of_cluster(s
, file_size
);
252 if (!qed_is_table_size_valid(s
->header
.table_size
)) {
255 if (!qed_is_image_size_valid(s
->header
.image_size
,
256 s
->header
.cluster_size
,
257 s
->header
.table_size
)) {
260 if (!qed_check_table_offset(s
, s
->header
.l1_table_offset
)) {
264 s
->table_nelems
= (s
->header
.cluster_size
* s
->header
.table_size
) /
266 s
->l2_shift
= ffs(s
->header
.cluster_size
) - 1;
267 s
->l2_mask
= s
->table_nelems
- 1;
268 s
->l1_shift
= s
->l2_shift
+ ffs(s
->table_nelems
) - 1;
270 if ((s
->header
.features
& QED_F_BACKING_FILE
)) {
271 if ((uint64_t)s
->header
.backing_filename_offset
+
272 s
->header
.backing_filename_size
>
273 s
->header
.cluster_size
* s
->header
.header_size
) {
277 ret
= qed_read_string(bs
->file
, s
->header
.backing_filename_offset
,
278 s
->header
.backing_filename_size
, bs
->backing_file
,
279 sizeof(bs
->backing_file
));
284 if (s
->header
.features
& QED_F_BACKING_FORMAT_NO_PROBE
) {
285 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), "raw");
289 /* Reset unknown autoclear feature bits. This is a backwards
290 * compatibility mechanism that allows images to be opened by older
291 * programs, which "knock out" unknown feature bits. When an image is
292 * opened by a newer program again it can detect that the autoclear
293 * feature is no longer valid.
295 if ((s
->header
.autoclear_features
& ~QED_AUTOCLEAR_FEATURE_MASK
) != 0 &&
296 !bdrv_is_read_only(bs
->file
)) {
297 s
->header
.autoclear_features
&= QED_AUTOCLEAR_FEATURE_MASK
;
299 ret
= qed_write_header_sync(s
);
304 /* From here on only known autoclear feature bits are valid */
305 bdrv_flush(bs
->file
);
308 s
->l1_table
= qed_alloc_table(s
);
309 qed_init_l2_cache(&s
->l2_cache
);
311 ret
= qed_read_l1_table_sync(s
);
313 qed_free_l2_cache(&s
->l2_cache
);
314 qemu_vfree(s
->l1_table
);
319 static void bdrv_qed_close(BlockDriverState
*bs
)
321 BDRVQEDState
*s
= bs
->opaque
;
323 qed_free_l2_cache(&s
->l2_cache
);
324 qemu_vfree(s
->l1_table
);
327 static int bdrv_qed_flush(BlockDriverState
*bs
)
329 return bdrv_flush(bs
->file
);
332 static int qed_create(const char *filename
, uint32_t cluster_size
,
333 uint64_t image_size
, uint32_t table_size
,
334 const char *backing_file
, const char *backing_fmt
)
338 .cluster_size
= cluster_size
,
339 .table_size
= table_size
,
342 .compat_features
= 0,
343 .l1_table_offset
= cluster_size
,
344 .image_size
= image_size
,
347 uint8_t *l1_table
= NULL
;
348 size_t l1_size
= header
.cluster_size
* header
.table_size
;
350 BlockDriverState
*bs
= NULL
;
352 ret
= bdrv_create_file(filename
, NULL
);
357 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDWR
| BDRV_O_CACHE_WB
);
363 header
.features
|= QED_F_BACKING_FILE
;
364 header
.backing_filename_offset
= sizeof(le_header
);
365 header
.backing_filename_size
= strlen(backing_file
);
367 if (qed_fmt_is_raw(backing_fmt
)) {
368 header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
372 qed_header_cpu_to_le(&header
, &le_header
);
373 ret
= bdrv_pwrite(bs
, 0, &le_header
, sizeof(le_header
));
377 ret
= bdrv_pwrite(bs
, sizeof(le_header
), backing_file
,
378 header
.backing_filename_size
);
383 l1_table
= qemu_mallocz(l1_size
);
384 ret
= bdrv_pwrite(bs
, header
.l1_table_offset
, l1_table
, l1_size
);
389 ret
= 0; /* success */
396 static int bdrv_qed_create(const char *filename
, QEMUOptionParameter
*options
)
398 uint64_t image_size
= 0;
399 uint32_t cluster_size
= QED_DEFAULT_CLUSTER_SIZE
;
400 uint32_t table_size
= QED_DEFAULT_TABLE_SIZE
;
401 const char *backing_file
= NULL
;
402 const char *backing_fmt
= NULL
;
404 while (options
&& options
->name
) {
405 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
406 image_size
= options
->value
.n
;
407 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
408 backing_file
= options
->value
.s
;
409 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
410 backing_fmt
= options
->value
.s
;
411 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
412 if (options
->value
.n
) {
413 cluster_size
= options
->value
.n
;
415 } else if (!strcmp(options
->name
, BLOCK_OPT_TABLE_SIZE
)) {
416 if (options
->value
.n
) {
417 table_size
= options
->value
.n
;
423 if (!qed_is_cluster_size_valid(cluster_size
)) {
424 fprintf(stderr
, "QED cluster size must be within range [%u, %u] and power of 2\n",
425 QED_MIN_CLUSTER_SIZE
, QED_MAX_CLUSTER_SIZE
);
428 if (!qed_is_table_size_valid(table_size
)) {
429 fprintf(stderr
, "QED table size must be within range [%u, %u] and power of 2\n",
430 QED_MIN_TABLE_SIZE
, QED_MAX_TABLE_SIZE
);
433 if (!qed_is_image_size_valid(image_size
, cluster_size
, table_size
)) {
434 fprintf(stderr
, "QED image size must be a non-zero multiple of "
435 "cluster size and less than %" PRIu64
" bytes\n",
436 qed_max_image_size(cluster_size
, table_size
));
440 return qed_create(filename
, cluster_size
, image_size
, table_size
,
441 backing_file
, backing_fmt
);
449 static void qed_is_allocated_cb(void *opaque
, int ret
, uint64_t offset
, size_t len
)
451 QEDIsAllocatedCB
*cb
= opaque
;
452 *cb
->pnum
= len
/ BDRV_SECTOR_SIZE
;
453 cb
->is_allocated
= ret
== QED_CLUSTER_FOUND
;
456 static int bdrv_qed_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
457 int nb_sectors
, int *pnum
)
459 BDRVQEDState
*s
= bs
->opaque
;
460 uint64_t pos
= (uint64_t)sector_num
* BDRV_SECTOR_SIZE
;
461 size_t len
= (size_t)nb_sectors
* BDRV_SECTOR_SIZE
;
462 QEDIsAllocatedCB cb
= {
466 QEDRequest request
= { .l2_table
= NULL
};
468 async_context_push();
470 qed_find_cluster(s
, &request
, pos
, len
, qed_is_allocated_cb
, &cb
);
472 while (cb
.is_allocated
== -1) {
478 qed_unref_l2_cache_entry(request
.l2_table
);
480 return cb
.is_allocated
;
483 static int bdrv_qed_make_empty(BlockDriverState
*bs
)
488 static BDRVQEDState
*acb_to_s(QEDAIOCB
*acb
)
490 return acb
->common
.bs
->opaque
;
494 * Read from the backing file or zero-fill if no backing file
497 * @pos: Byte position in device
498 * @qiov: Destination I/O vector
499 * @cb: Completion function
500 * @opaque: User data for completion function
502 * This function reads qiov->size bytes starting at pos from the backing file.
503 * If there is no backing file then zeroes are read.
505 static void qed_read_backing_file(BDRVQEDState
*s
, uint64_t pos
,
507 BlockDriverCompletionFunc
*cb
, void *opaque
)
509 BlockDriverAIOCB
*aiocb
;
510 uint64_t backing_length
= 0;
513 /* If there is a backing file, get its length. Treat the absence of a
514 * backing file like a zero length backing file.
516 if (s
->bs
->backing_hd
) {
517 int64_t l
= bdrv_getlength(s
->bs
->backing_hd
);
525 /* Zero all sectors if reading beyond the end of the backing file */
526 if (pos
>= backing_length
||
527 pos
+ qiov
->size
> backing_length
) {
528 qemu_iovec_memset(qiov
, 0, qiov
->size
);
531 /* Complete now if there are no backing file sectors to read */
532 if (pos
>= backing_length
) {
537 /* If the read straddles the end of the backing file, shorten it */
538 size
= MIN((uint64_t)backing_length
- pos
, qiov
->size
);
540 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_READ_BACKING
);
541 aiocb
= bdrv_aio_readv(s
->bs
->backing_hd
, pos
/ BDRV_SECTOR_SIZE
,
542 qiov
, size
/ BDRV_SECTOR_SIZE
, cb
, opaque
);
554 } CopyFromBackingFileCB
;
556 static void qed_copy_from_backing_file_cb(void *opaque
, int ret
)
558 CopyFromBackingFileCB
*copy_cb
= opaque
;
559 qemu_vfree(copy_cb
->iov
.iov_base
);
560 gencb_complete(©_cb
->gencb
, ret
);
563 static void qed_copy_from_backing_file_write(void *opaque
, int ret
)
565 CopyFromBackingFileCB
*copy_cb
= opaque
;
566 BDRVQEDState
*s
= copy_cb
->s
;
567 BlockDriverAIOCB
*aiocb
;
570 qed_copy_from_backing_file_cb(copy_cb
, ret
);
574 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_COW_WRITE
);
575 aiocb
= bdrv_aio_writev(s
->bs
->file
, copy_cb
->offset
/ BDRV_SECTOR_SIZE
,
577 copy_cb
->qiov
.size
/ BDRV_SECTOR_SIZE
,
578 qed_copy_from_backing_file_cb
, copy_cb
);
580 qed_copy_from_backing_file_cb(copy_cb
, -EIO
);
585 * Copy data from backing file into the image
588 * @pos: Byte position in device
589 * @len: Number of bytes
590 * @offset: Byte offset in image file
591 * @cb: Completion function
592 * @opaque: User data for completion function
594 static void qed_copy_from_backing_file(BDRVQEDState
*s
, uint64_t pos
,
595 uint64_t len
, uint64_t offset
,
596 BlockDriverCompletionFunc
*cb
,
599 CopyFromBackingFileCB
*copy_cb
;
601 /* Skip copy entirely if there is no work to do */
607 copy_cb
= gencb_alloc(sizeof(*copy_cb
), cb
, opaque
);
609 copy_cb
->offset
= offset
;
610 copy_cb
->iov
.iov_base
= qemu_blockalign(s
->bs
, len
);
611 copy_cb
->iov
.iov_len
= len
;
612 qemu_iovec_init_external(©_cb
->qiov
, ©_cb
->iov
, 1);
614 qed_read_backing_file(s
, pos
, ©_cb
->qiov
,
615 qed_copy_from_backing_file_write
, copy_cb
);
619 * Link one or more contiguous clusters into a table
623 * @index: First cluster index
624 * @n: Number of contiguous clusters
625 * @cluster: First cluster byte offset in image file
627 static void qed_update_l2_table(BDRVQEDState
*s
, QEDTable
*table
, int index
,
628 unsigned int n
, uint64_t cluster
)
631 for (i
= index
; i
< index
+ n
; i
++) {
632 table
->offsets
[i
] = cluster
;
633 cluster
+= s
->header
.cluster_size
;
637 static void qed_aio_complete_bh(void *opaque
)
639 QEDAIOCB
*acb
= opaque
;
640 BlockDriverCompletionFunc
*cb
= acb
->common
.cb
;
641 void *user_opaque
= acb
->common
.opaque
;
642 int ret
= acb
->bh_ret
;
643 bool *finished
= acb
->finished
;
645 qemu_bh_delete(acb
->bh
);
646 qemu_aio_release(acb
);
648 /* Invoke callback */
649 cb(user_opaque
, ret
);
651 /* Signal cancel completion */
657 static void qed_aio_complete(QEDAIOCB
*acb
, int ret
)
659 BDRVQEDState
*s
= acb_to_s(acb
);
661 trace_qed_aio_complete(s
, acb
, ret
);
664 qemu_iovec_destroy(&acb
->cur_qiov
);
665 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
667 /* Arrange for a bh to invoke the completion function */
669 acb
->bh
= qemu_bh_new(qed_aio_complete_bh
, acb
);
670 qemu_bh_schedule(acb
->bh
);
672 /* Start next allocating write request waiting behind this one. Note that
673 * requests enqueue themselves when they first hit an unallocated cluster
674 * but they wait until the entire request is finished before waking up the
675 * next request in the queue. This ensures that we don't cycle through
676 * requests multiple times but rather finish one at a time completely.
678 if (acb
== QSIMPLEQ_FIRST(&s
->allocating_write_reqs
)) {
679 QSIMPLEQ_REMOVE_HEAD(&s
->allocating_write_reqs
, next
);
680 acb
= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
);
682 qed_aio_next_io(acb
, 0);
688 * Commit the current L2 table to the cache
690 static void qed_commit_l2_update(void *opaque
, int ret
)
692 QEDAIOCB
*acb
= opaque
;
693 BDRVQEDState
*s
= acb_to_s(acb
);
694 CachedL2Table
*l2_table
= acb
->request
.l2_table
;
696 qed_commit_l2_cache_entry(&s
->l2_cache
, l2_table
);
698 /* This is guaranteed to succeed because we just committed the entry to the
701 acb
->request
.l2_table
= qed_find_l2_cache_entry(&s
->l2_cache
,
703 assert(acb
->request
.l2_table
!= NULL
);
705 qed_aio_next_io(opaque
, ret
);
709 * Update L1 table with new L2 table offset and write it out
711 static void qed_aio_write_l1_update(void *opaque
, int ret
)
713 QEDAIOCB
*acb
= opaque
;
714 BDRVQEDState
*s
= acb_to_s(acb
);
718 qed_aio_complete(acb
, ret
);
722 index
= qed_l1_index(s
, acb
->cur_pos
);
723 s
->l1_table
->offsets
[index
] = acb
->request
.l2_table
->offset
;
725 qed_write_l1_table(s
, index
, 1, qed_commit_l2_update
, acb
);
729 * Update L2 table with new cluster offsets and write them out
731 static void qed_aio_write_l2_update(void *opaque
, int ret
)
733 QEDAIOCB
*acb
= opaque
;
734 BDRVQEDState
*s
= acb_to_s(acb
);
735 bool need_alloc
= acb
->find_cluster_ret
== QED_CLUSTER_L1
;
743 qed_unref_l2_cache_entry(acb
->request
.l2_table
);
744 acb
->request
.l2_table
= qed_new_l2_table(s
);
747 index
= qed_l2_index(s
, acb
->cur_pos
);
748 qed_update_l2_table(s
, acb
->request
.l2_table
->table
, index
, acb
->cur_nclusters
,
752 /* Write out the whole new L2 table */
753 qed_write_l2_table(s
, &acb
->request
, 0, s
->table_nelems
, true,
754 qed_aio_write_l1_update
, acb
);
756 /* Write out only the updated part of the L2 table */
757 qed_write_l2_table(s
, &acb
->request
, index
, acb
->cur_nclusters
, false,
758 qed_aio_next_io
, acb
);
763 qed_aio_complete(acb
, ret
);
767 * Flush new data clusters before updating the L2 table
769 * This flush is necessary when a backing file is in use. A crash during an
770 * allocating write could result in empty clusters in the image. If the write
771 * only touched a subregion of the cluster, then backing image sectors have
772 * been lost in the untouched region. The solution is to flush after writing a
773 * new data cluster and before updating the L2 table.
775 static void qed_aio_write_flush_before_l2_update(void *opaque
, int ret
)
777 QEDAIOCB
*acb
= opaque
;
778 BDRVQEDState
*s
= acb_to_s(acb
);
780 if (!bdrv_aio_flush(s
->bs
->file
, qed_aio_write_l2_update
, opaque
)) {
781 qed_aio_complete(acb
, -EIO
);
786 * Write data to the image file
788 static void qed_aio_write_main(void *opaque
, int ret
)
790 QEDAIOCB
*acb
= opaque
;
791 BDRVQEDState
*s
= acb_to_s(acb
);
792 uint64_t offset
= acb
->cur_cluster
+
793 qed_offset_into_cluster(s
, acb
->cur_pos
);
794 BlockDriverCompletionFunc
*next_fn
;
795 BlockDriverAIOCB
*file_acb
;
797 trace_qed_aio_write_main(s
, acb
, ret
, offset
, acb
->cur_qiov
.size
);
800 qed_aio_complete(acb
, ret
);
804 if (acb
->find_cluster_ret
== QED_CLUSTER_FOUND
) {
805 next_fn
= qed_aio_next_io
;
807 if (s
->bs
->backing_hd
) {
808 next_fn
= qed_aio_write_flush_before_l2_update
;
810 next_fn
= qed_aio_write_l2_update
;
814 BLKDBG_EVENT(s
->bs
->file
, BLKDBG_WRITE_AIO
);
815 file_acb
= bdrv_aio_writev(s
->bs
->file
, offset
/ BDRV_SECTOR_SIZE
,
817 acb
->cur_qiov
.size
/ BDRV_SECTOR_SIZE
,
820 qed_aio_complete(acb
, -EIO
);
825 * Populate back untouched region of new data cluster
827 static void qed_aio_write_postfill(void *opaque
, int ret
)
829 QEDAIOCB
*acb
= opaque
;
830 BDRVQEDState
*s
= acb_to_s(acb
);
831 uint64_t start
= acb
->cur_pos
+ acb
->cur_qiov
.size
;
833 qed_start_of_cluster(s
, start
+ s
->header
.cluster_size
- 1) - start
;
834 uint64_t offset
= acb
->cur_cluster
+
835 qed_offset_into_cluster(s
, acb
->cur_pos
) +
839 qed_aio_complete(acb
, ret
);
843 trace_qed_aio_write_postfill(s
, acb
, start
, len
, offset
);
844 qed_copy_from_backing_file(s
, start
, len
, offset
,
845 qed_aio_write_main
, acb
);
849 * Populate front untouched region of new data cluster
851 static void qed_aio_write_prefill(void *opaque
, int ret
)
853 QEDAIOCB
*acb
= opaque
;
854 BDRVQEDState
*s
= acb_to_s(acb
);
855 uint64_t start
= qed_start_of_cluster(s
, acb
->cur_pos
);
856 uint64_t len
= qed_offset_into_cluster(s
, acb
->cur_pos
);
858 trace_qed_aio_write_prefill(s
, acb
, start
, len
, acb
->cur_cluster
);
859 qed_copy_from_backing_file(s
, start
, len
, acb
->cur_cluster
,
860 qed_aio_write_postfill
, acb
);
864 * Write new data cluster
866 * @acb: Write request
867 * @len: Length in bytes
869 * This path is taken when writing to previously unallocated clusters.
871 static void qed_aio_write_alloc(QEDAIOCB
*acb
, size_t len
)
873 BDRVQEDState
*s
= acb_to_s(acb
);
875 /* Freeze this request if another allocating write is in progress */
876 if (acb
!= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
)) {
877 QSIMPLEQ_INSERT_TAIL(&s
->allocating_write_reqs
, acb
, next
);
879 if (acb
!= QSIMPLEQ_FIRST(&s
->allocating_write_reqs
)) {
880 return; /* wait for existing request to finish */
883 acb
->cur_nclusters
= qed_bytes_to_clusters(s
,
884 qed_offset_into_cluster(s
, acb
->cur_pos
) + len
);
885 acb
->cur_cluster
= qed_alloc_clusters(s
, acb
->cur_nclusters
);
886 qemu_iovec_copy(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
888 /* Write new cluster */
889 qed_aio_write_prefill(acb
, 0);
893 * Write data cluster in place
895 * @acb: Write request
896 * @offset: Cluster offset in bytes
897 * @len: Length in bytes
899 * This path is taken when writing to already allocated clusters.
901 static void qed_aio_write_inplace(QEDAIOCB
*acb
, uint64_t offset
, size_t len
)
903 /* Calculate the I/O vector */
904 acb
->cur_cluster
= offset
;
905 qemu_iovec_copy(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
907 /* Do the actual write */
908 qed_aio_write_main(acb
, 0);
914 * @opaque: Write request
915 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
917 * @offset: Cluster offset in bytes
918 * @len: Length in bytes
920 * Callback from qed_find_cluster().
922 static void qed_aio_write_data(void *opaque
, int ret
,
923 uint64_t offset
, size_t len
)
925 QEDAIOCB
*acb
= opaque
;
927 trace_qed_aio_write_data(acb_to_s(acb
), acb
, ret
, offset
, len
);
929 acb
->find_cluster_ret
= ret
;
932 case QED_CLUSTER_FOUND
:
933 qed_aio_write_inplace(acb
, offset
, len
);
938 qed_aio_write_alloc(acb
, len
);
942 qed_aio_complete(acb
, ret
);
950 * @opaque: Read request
951 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
953 * @offset: Cluster offset in bytes
954 * @len: Length in bytes
956 * Callback from qed_find_cluster().
958 static void qed_aio_read_data(void *opaque
, int ret
,
959 uint64_t offset
, size_t len
)
961 QEDAIOCB
*acb
= opaque
;
962 BDRVQEDState
*s
= acb_to_s(acb
);
963 BlockDriverState
*bs
= acb
->common
.bs
;
964 BlockDriverAIOCB
*file_acb
;
966 /* Adjust offset into cluster */
967 offset
+= qed_offset_into_cluster(s
, acb
->cur_pos
);
969 trace_qed_aio_read_data(s
, acb
, ret
, offset
, len
);
975 qemu_iovec_copy(&acb
->cur_qiov
, acb
->qiov
, acb
->qiov_offset
, len
);
977 /* Handle backing file and unallocated sparse hole reads */
978 if (ret
!= QED_CLUSTER_FOUND
) {
979 qed_read_backing_file(s
, acb
->cur_pos
, &acb
->cur_qiov
,
980 qed_aio_next_io
, acb
);
984 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
985 file_acb
= bdrv_aio_readv(bs
->file
, offset
/ BDRV_SECTOR_SIZE
,
987 acb
->cur_qiov
.size
/ BDRV_SECTOR_SIZE
,
988 qed_aio_next_io
, acb
);
996 qed_aio_complete(acb
, ret
);
1000 * Begin next I/O or complete the request
1002 static void qed_aio_next_io(void *opaque
, int ret
)
1004 QEDAIOCB
*acb
= opaque
;
1005 BDRVQEDState
*s
= acb_to_s(acb
);
1006 QEDFindClusterFunc
*io_fn
=
1007 acb
->is_write
? qed_aio_write_data
: qed_aio_read_data
;
1009 trace_qed_aio_next_io(s
, acb
, ret
, acb
->cur_pos
+ acb
->cur_qiov
.size
);
1011 /* Handle I/O error */
1013 qed_aio_complete(acb
, ret
);
1017 acb
->qiov_offset
+= acb
->cur_qiov
.size
;
1018 acb
->cur_pos
+= acb
->cur_qiov
.size
;
1019 qemu_iovec_reset(&acb
->cur_qiov
);
1021 /* Complete request */
1022 if (acb
->cur_pos
>= acb
->end_pos
) {
1023 qed_aio_complete(acb
, 0);
1027 /* Find next cluster and start I/O */
1028 qed_find_cluster(s
, &acb
->request
,
1029 acb
->cur_pos
, acb
->end_pos
- acb
->cur_pos
,
1033 static BlockDriverAIOCB
*qed_aio_setup(BlockDriverState
*bs
,
1035 QEMUIOVector
*qiov
, int nb_sectors
,
1036 BlockDriverCompletionFunc
*cb
,
1037 void *opaque
, bool is_write
)
1039 QEDAIOCB
*acb
= qemu_aio_get(&qed_aio_pool
, bs
, cb
, opaque
);
1041 trace_qed_aio_setup(bs
->opaque
, acb
, sector_num
, nb_sectors
,
1044 acb
->is_write
= is_write
;
1045 acb
->finished
= NULL
;
1047 acb
->qiov_offset
= 0;
1048 acb
->cur_pos
= (uint64_t)sector_num
* BDRV_SECTOR_SIZE
;
1049 acb
->end_pos
= acb
->cur_pos
+ nb_sectors
* BDRV_SECTOR_SIZE
;
1050 acb
->request
.l2_table
= NULL
;
1051 qemu_iovec_init(&acb
->cur_qiov
, qiov
->niov
);
1054 qed_aio_next_io(acb
, 0);
1055 return &acb
->common
;
1058 static BlockDriverAIOCB
*bdrv_qed_aio_readv(BlockDriverState
*bs
,
1060 QEMUIOVector
*qiov
, int nb_sectors
,
1061 BlockDriverCompletionFunc
*cb
,
1064 return qed_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, false);
1067 static BlockDriverAIOCB
*bdrv_qed_aio_writev(BlockDriverState
*bs
,
1069 QEMUIOVector
*qiov
, int nb_sectors
,
1070 BlockDriverCompletionFunc
*cb
,
1073 return qed_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, true);
1076 static BlockDriverAIOCB
*bdrv_qed_aio_flush(BlockDriverState
*bs
,
1077 BlockDriverCompletionFunc
*cb
,
1080 return bdrv_aio_flush(bs
->file
, cb
, opaque
);
1083 static int bdrv_qed_truncate(BlockDriverState
*bs
, int64_t offset
)
1088 static int64_t bdrv_qed_getlength(BlockDriverState
*bs
)
1090 BDRVQEDState
*s
= bs
->opaque
;
1091 return s
->header
.image_size
;
1094 static int bdrv_qed_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1096 BDRVQEDState
*s
= bs
->opaque
;
1098 memset(bdi
, 0, sizeof(*bdi
));
1099 bdi
->cluster_size
= s
->header
.cluster_size
;
1103 static int bdrv_qed_change_backing_file(BlockDriverState
*bs
,
1104 const char *backing_file
,
1105 const char *backing_fmt
)
1107 BDRVQEDState
*s
= bs
->opaque
;
1108 QEDHeader new_header
, le_header
;
1110 size_t buffer_len
, backing_file_len
;
1113 /* Refuse to set backing filename if unknown compat feature bits are
1114 * active. If the image uses an unknown compat feature then we may not
1115 * know the layout of data following the header structure and cannot safely
1118 if (backing_file
&& (s
->header
.compat_features
&
1119 ~QED_COMPAT_FEATURE_MASK
)) {
1123 memcpy(&new_header
, &s
->header
, sizeof(new_header
));
1125 new_header
.features
&= ~(QED_F_BACKING_FILE
|
1126 QED_F_BACKING_FORMAT_NO_PROBE
);
1128 /* Adjust feature flags */
1130 new_header
.features
|= QED_F_BACKING_FILE
;
1132 if (qed_fmt_is_raw(backing_fmt
)) {
1133 new_header
.features
|= QED_F_BACKING_FORMAT_NO_PROBE
;
1137 /* Calculate new header size */
1138 backing_file_len
= 0;
1141 backing_file_len
= strlen(backing_file
);
1144 buffer_len
= sizeof(new_header
);
1145 new_header
.backing_filename_offset
= buffer_len
;
1146 new_header
.backing_filename_size
= backing_file_len
;
1147 buffer_len
+= backing_file_len
;
1149 /* Make sure we can rewrite header without failing */
1150 if (buffer_len
> new_header
.header_size
* new_header
.cluster_size
) {
1154 /* Prepare new header */
1155 buffer
= qemu_malloc(buffer_len
);
1157 qed_header_cpu_to_le(&new_header
, &le_header
);
1158 memcpy(buffer
, &le_header
, sizeof(le_header
));
1159 buffer_len
= sizeof(le_header
);
1161 memcpy(buffer
+ buffer_len
, backing_file
, backing_file_len
);
1162 buffer_len
+= backing_file_len
;
1164 /* Write new header */
1165 ret
= bdrv_pwrite_sync(bs
->file
, 0, buffer
, buffer_len
);
1168 memcpy(&s
->header
, &new_header
, sizeof(new_header
));
1173 static int bdrv_qed_check(BlockDriverState
*bs
, BdrvCheckResult
*result
)
1178 static QEMUOptionParameter qed_create_options
[] = {
1180 .name
= BLOCK_OPT_SIZE
,
1182 .help
= "Virtual disk size (in bytes)"
1184 .name
= BLOCK_OPT_BACKING_FILE
,
1186 .help
= "File name of a base image"
1188 .name
= BLOCK_OPT_BACKING_FMT
,
1190 .help
= "Image format of the base image"
1192 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1194 .help
= "Cluster size (in bytes)"
1196 .name
= BLOCK_OPT_TABLE_SIZE
,
1198 .help
= "L1/L2 table size (in clusters)"
1200 { /* end of list */ }
1203 static BlockDriver bdrv_qed
= {
1204 .format_name
= "qed",
1205 .instance_size
= sizeof(BDRVQEDState
),
1206 .create_options
= qed_create_options
,
1208 .bdrv_probe
= bdrv_qed_probe
,
1209 .bdrv_open
= bdrv_qed_open
,
1210 .bdrv_close
= bdrv_qed_close
,
1211 .bdrv_create
= bdrv_qed_create
,
1212 .bdrv_flush
= bdrv_qed_flush
,
1213 .bdrv_is_allocated
= bdrv_qed_is_allocated
,
1214 .bdrv_make_empty
= bdrv_qed_make_empty
,
1215 .bdrv_aio_readv
= bdrv_qed_aio_readv
,
1216 .bdrv_aio_writev
= bdrv_qed_aio_writev
,
1217 .bdrv_aio_flush
= bdrv_qed_aio_flush
,
1218 .bdrv_truncate
= bdrv_qed_truncate
,
1219 .bdrv_getlength
= bdrv_qed_getlength
,
1220 .bdrv_get_info
= bdrv_qed_get_info
,
1221 .bdrv_change_backing_file
= bdrv_qed_change_backing_file
,
1222 .bdrv_check
= bdrv_qed_check
,
1225 static void bdrv_qed_init(void)
1227 bdrv_register(&bdrv_qed
);
1230 block_init(bdrv_qed_init
);