virtio-serial-bus: bump up control vq size to 32
[qemu.git] / block / qed.c
blob085c4f22109a41a3cb19a277041803e21c7a64ac
1 /*
2 * QEMU Enhanced Disk Format
4 * Copyright IBM, Corp. 2010
6 * Authors:
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 "trace.h"
16 #include "qed.h"
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;
25 while (!finished) {
26 qemu_aio_wait();
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,
36 const char *filename)
38 const QEDHeader *header = (const QEDHeader *)buf;
40 if (buf_size < sizeof(*header)) {
41 return 0;
43 if (le32_to_cpu(header->magic) != QED_MAGIC) {
44 return 0;
46 return 100;
49 /**
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)
91 QEDHeader le;
92 int ret;
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)) {
97 return ret;
99 return 0;
102 typedef struct {
103 GenericCB gencb;
104 BDRVQEDState *s;
105 struct iovec iov;
106 QEMUIOVector qiov;
107 int nsectors;
108 uint8_t *buf;
109 } QEDWriteHeaderCB;
111 static void qed_write_header_cb(void *opaque, int ret)
113 QEDWriteHeaderCB *write_header_cb = opaque;
115 qemu_vfree(write_header_cb->buf);
116 gencb_complete(write_header_cb, ret);
119 static void qed_write_header_read_cb(void *opaque, int ret)
121 QEDWriteHeaderCB *write_header_cb = opaque;
122 BDRVQEDState *s = write_header_cb->s;
123 BlockDriverAIOCB *acb;
125 if (ret) {
126 qed_write_header_cb(write_header_cb, ret);
127 return;
130 /* Update header */
131 qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf);
133 acb = bdrv_aio_writev(s->bs->file, 0, &write_header_cb->qiov,
134 write_header_cb->nsectors, qed_write_header_cb,
135 write_header_cb);
136 if (!acb) {
137 qed_write_header_cb(write_header_cb, -EIO);
142 * Update header in-place (does not rewrite backing filename or other strings)
144 * This function only updates known header fields in-place and does not affect
145 * extra data after the QED header.
147 static void qed_write_header(BDRVQEDState *s, BlockDriverCompletionFunc cb,
148 void *opaque)
150 /* We must write full sectors for O_DIRECT but cannot necessarily generate
151 * the data following the header if an unrecognized compat feature is
152 * active. Therefore, first read the sectors containing the header, update
153 * them, and write back.
156 BlockDriverAIOCB *acb;
157 int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) /
158 BDRV_SECTOR_SIZE;
159 size_t len = nsectors * BDRV_SECTOR_SIZE;
160 QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb),
161 cb, opaque);
163 write_header_cb->s = s;
164 write_header_cb->nsectors = nsectors;
165 write_header_cb->buf = qemu_blockalign(s->bs, len);
166 write_header_cb->iov.iov_base = write_header_cb->buf;
167 write_header_cb->iov.iov_len = len;
168 qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1);
170 acb = bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors,
171 qed_write_header_read_cb, write_header_cb);
172 if (!acb) {
173 qed_write_header_cb(write_header_cb, -EIO);
177 static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
179 uint64_t table_entries;
180 uint64_t l2_size;
182 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
183 l2_size = table_entries * cluster_size;
185 return l2_size * table_entries;
188 static bool qed_is_cluster_size_valid(uint32_t cluster_size)
190 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
191 cluster_size > QED_MAX_CLUSTER_SIZE) {
192 return false;
194 if (cluster_size & (cluster_size - 1)) {
195 return false; /* not power of 2 */
197 return true;
200 static bool qed_is_table_size_valid(uint32_t table_size)
202 if (table_size < QED_MIN_TABLE_SIZE ||
203 table_size > QED_MAX_TABLE_SIZE) {
204 return false;
206 if (table_size & (table_size - 1)) {
207 return false; /* not power of 2 */
209 return true;
212 static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
213 uint32_t table_size)
215 if (image_size % BDRV_SECTOR_SIZE != 0) {
216 return false; /* not multiple of sector size */
218 if (image_size > qed_max_image_size(cluster_size, table_size)) {
219 return false; /* image is too large */
221 return true;
225 * Read a string of known length from the image file
227 * @file: Image file
228 * @offset: File offset to start of string, in bytes
229 * @n: String length in bytes
230 * @buf: Destination buffer
231 * @buflen: Destination buffer length in bytes
232 * @ret: 0 on success, -errno on failure
234 * The string is NUL-terminated.
236 static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n,
237 char *buf, size_t buflen)
239 int ret;
240 if (n >= buflen) {
241 return -EINVAL;
243 ret = bdrv_pread(file, offset, buf, n);
244 if (ret < 0) {
245 return ret;
247 buf[n] = '\0';
248 return 0;
252 * Allocate new clusters
254 * @s: QED state
255 * @n: Number of contiguous clusters to allocate
256 * @ret: Offset of first allocated cluster
258 * This function only produces the offset where the new clusters should be
259 * written. It updates BDRVQEDState but does not make any changes to the image
260 * file.
262 static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
264 uint64_t offset = s->file_size;
265 s->file_size += n * s->header.cluster_size;
266 return offset;
269 QEDTable *qed_alloc_table(BDRVQEDState *s)
271 /* Honor O_DIRECT memory alignment requirements */
272 return qemu_blockalign(s->bs,
273 s->header.cluster_size * s->header.table_size);
277 * Allocate a new zeroed L2 table
279 static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
281 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
283 l2_table->table = qed_alloc_table(s);
284 l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
286 memset(l2_table->table->offsets, 0,
287 s->header.cluster_size * s->header.table_size);
288 return l2_table;
291 static void qed_aio_next_io(void *opaque, int ret);
293 static int bdrv_qed_open(BlockDriverState *bs, int flags)
295 BDRVQEDState *s = bs->opaque;
296 QEDHeader le_header;
297 int64_t file_size;
298 int ret;
300 s->bs = bs;
301 QSIMPLEQ_INIT(&s->allocating_write_reqs);
303 ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header));
304 if (ret < 0) {
305 return ret;
307 ret = 0; /* ret should always be 0 or -errno */
308 qed_header_le_to_cpu(&le_header, &s->header);
310 if (s->header.magic != QED_MAGIC) {
311 return -EINVAL;
313 if (s->header.features & ~QED_FEATURE_MASK) {
314 return -ENOTSUP; /* image uses unsupported feature bits */
316 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
317 return -EINVAL;
320 /* Round down file size to the last cluster */
321 file_size = bdrv_getlength(bs->file);
322 if (file_size < 0) {
323 return file_size;
325 s->file_size = qed_start_of_cluster(s, file_size);
327 if (!qed_is_table_size_valid(s->header.table_size)) {
328 return -EINVAL;
330 if (!qed_is_image_size_valid(s->header.image_size,
331 s->header.cluster_size,
332 s->header.table_size)) {
333 return -EINVAL;
335 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
336 return -EINVAL;
339 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
340 sizeof(uint64_t);
341 s->l2_shift = ffs(s->header.cluster_size) - 1;
342 s->l2_mask = s->table_nelems - 1;
343 s->l1_shift = s->l2_shift + ffs(s->table_nelems) - 1;
345 if ((s->header.features & QED_F_BACKING_FILE)) {
346 if ((uint64_t)s->header.backing_filename_offset +
347 s->header.backing_filename_size >
348 s->header.cluster_size * s->header.header_size) {
349 return -EINVAL;
352 ret = qed_read_string(bs->file, s->header.backing_filename_offset,
353 s->header.backing_filename_size, bs->backing_file,
354 sizeof(bs->backing_file));
355 if (ret < 0) {
356 return ret;
359 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
360 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
364 /* Reset unknown autoclear feature bits. This is a backwards
365 * compatibility mechanism that allows images to be opened by older
366 * programs, which "knock out" unknown feature bits. When an image is
367 * opened by a newer program again it can detect that the autoclear
368 * feature is no longer valid.
370 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
371 !bdrv_is_read_only(bs->file)) {
372 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
374 ret = qed_write_header_sync(s);
375 if (ret) {
376 return ret;
379 /* From here on only known autoclear feature bits are valid */
380 bdrv_flush(bs->file);
383 s->l1_table = qed_alloc_table(s);
384 qed_init_l2_cache(&s->l2_cache);
386 ret = qed_read_l1_table_sync(s);
387 if (ret) {
388 goto out;
391 /* If image was not closed cleanly, check consistency */
392 if (s->header.features & QED_F_NEED_CHECK) {
393 /* Read-only images cannot be fixed. There is no risk of corruption
394 * since write operations are not possible. Therefore, allow
395 * potentially inconsistent images to be opened read-only. This can
396 * aid data recovery from an otherwise inconsistent image.
398 if (!bdrv_is_read_only(bs->file)) {
399 BdrvCheckResult result = {0};
401 ret = qed_check(s, &result, true);
402 if (!ret && !result.corruptions && !result.check_errors) {
403 /* Ensure fixes reach storage before clearing check bit */
404 bdrv_flush(s->bs);
406 s->header.features &= ~QED_F_NEED_CHECK;
407 qed_write_header_sync(s);
412 out:
413 if (ret) {
414 qed_free_l2_cache(&s->l2_cache);
415 qemu_vfree(s->l1_table);
417 return ret;
420 static void bdrv_qed_close(BlockDriverState *bs)
422 BDRVQEDState *s = bs->opaque;
424 /* Ensure writes reach stable storage */
425 bdrv_flush(bs->file);
427 /* Clean shutdown, no check required on next open */
428 if (s->header.features & QED_F_NEED_CHECK) {
429 s->header.features &= ~QED_F_NEED_CHECK;
430 qed_write_header_sync(s);
433 qed_free_l2_cache(&s->l2_cache);
434 qemu_vfree(s->l1_table);
437 static int bdrv_qed_flush(BlockDriverState *bs)
439 return bdrv_flush(bs->file);
442 static int qed_create(const char *filename, uint32_t cluster_size,
443 uint64_t image_size, uint32_t table_size,
444 const char *backing_file, const char *backing_fmt)
446 QEDHeader header = {
447 .magic = QED_MAGIC,
448 .cluster_size = cluster_size,
449 .table_size = table_size,
450 .header_size = 1,
451 .features = 0,
452 .compat_features = 0,
453 .l1_table_offset = cluster_size,
454 .image_size = image_size,
456 QEDHeader le_header;
457 uint8_t *l1_table = NULL;
458 size_t l1_size = header.cluster_size * header.table_size;
459 int ret = 0;
460 BlockDriverState *bs = NULL;
462 ret = bdrv_create_file(filename, NULL);
463 if (ret < 0) {
464 return ret;
467 ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR | BDRV_O_CACHE_WB);
468 if (ret < 0) {
469 return ret;
472 if (backing_file) {
473 header.features |= QED_F_BACKING_FILE;
474 header.backing_filename_offset = sizeof(le_header);
475 header.backing_filename_size = strlen(backing_file);
477 if (qed_fmt_is_raw(backing_fmt)) {
478 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
482 qed_header_cpu_to_le(&header, &le_header);
483 ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header));
484 if (ret < 0) {
485 goto out;
487 ret = bdrv_pwrite(bs, sizeof(le_header), backing_file,
488 header.backing_filename_size);
489 if (ret < 0) {
490 goto out;
493 l1_table = qemu_mallocz(l1_size);
494 ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size);
495 if (ret < 0) {
496 goto out;
499 ret = 0; /* success */
500 out:
501 qemu_free(l1_table);
502 bdrv_delete(bs);
503 return ret;
506 static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options)
508 uint64_t image_size = 0;
509 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
510 uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
511 const char *backing_file = NULL;
512 const char *backing_fmt = NULL;
514 while (options && options->name) {
515 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
516 image_size = options->value.n;
517 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
518 backing_file = options->value.s;
519 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
520 backing_fmt = options->value.s;
521 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
522 if (options->value.n) {
523 cluster_size = options->value.n;
525 } else if (!strcmp(options->name, BLOCK_OPT_TABLE_SIZE)) {
526 if (options->value.n) {
527 table_size = options->value.n;
530 options++;
533 if (!qed_is_cluster_size_valid(cluster_size)) {
534 fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n",
535 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
536 return -EINVAL;
538 if (!qed_is_table_size_valid(table_size)) {
539 fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n",
540 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
541 return -EINVAL;
543 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
544 fprintf(stderr, "QED image size must be a non-zero multiple of "
545 "cluster size and less than %" PRIu64 " bytes\n",
546 qed_max_image_size(cluster_size, table_size));
547 return -EINVAL;
550 return qed_create(filename, cluster_size, image_size, table_size,
551 backing_file, backing_fmt);
554 typedef struct {
555 int is_allocated;
556 int *pnum;
557 } QEDIsAllocatedCB;
559 static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
561 QEDIsAllocatedCB *cb = opaque;
562 *cb->pnum = len / BDRV_SECTOR_SIZE;
563 cb->is_allocated = ret == QED_CLUSTER_FOUND;
566 static int bdrv_qed_is_allocated(BlockDriverState *bs, int64_t sector_num,
567 int nb_sectors, int *pnum)
569 BDRVQEDState *s = bs->opaque;
570 uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
571 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
572 QEDIsAllocatedCB cb = {
573 .is_allocated = -1,
574 .pnum = pnum,
576 QEDRequest request = { .l2_table = NULL };
578 async_context_push();
580 qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb);
582 while (cb.is_allocated == -1) {
583 qemu_aio_wait();
586 async_context_pop();
588 qed_unref_l2_cache_entry(request.l2_table);
590 return cb.is_allocated;
593 static int bdrv_qed_make_empty(BlockDriverState *bs)
595 return -ENOTSUP;
598 static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
600 return acb->common.bs->opaque;
604 * Read from the backing file or zero-fill if no backing file
606 * @s: QED state
607 * @pos: Byte position in device
608 * @qiov: Destination I/O vector
609 * @cb: Completion function
610 * @opaque: User data for completion function
612 * This function reads qiov->size bytes starting at pos from the backing file.
613 * If there is no backing file then zeroes are read.
615 static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
616 QEMUIOVector *qiov,
617 BlockDriverCompletionFunc *cb, void *opaque)
619 BlockDriverAIOCB *aiocb;
620 uint64_t backing_length = 0;
621 size_t size;
623 /* If there is a backing file, get its length. Treat the absence of a
624 * backing file like a zero length backing file.
626 if (s->bs->backing_hd) {
627 int64_t l = bdrv_getlength(s->bs->backing_hd);
628 if (l < 0) {
629 cb(opaque, l);
630 return;
632 backing_length = l;
635 /* Zero all sectors if reading beyond the end of the backing file */
636 if (pos >= backing_length ||
637 pos + qiov->size > backing_length) {
638 qemu_iovec_memset(qiov, 0, qiov->size);
641 /* Complete now if there are no backing file sectors to read */
642 if (pos >= backing_length) {
643 cb(opaque, 0);
644 return;
647 /* If the read straddles the end of the backing file, shorten it */
648 size = MIN((uint64_t)backing_length - pos, qiov->size);
650 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING);
651 aiocb = bdrv_aio_readv(s->bs->backing_hd, pos / BDRV_SECTOR_SIZE,
652 qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
653 if (!aiocb) {
654 cb(opaque, -EIO);
658 typedef struct {
659 GenericCB gencb;
660 BDRVQEDState *s;
661 QEMUIOVector qiov;
662 struct iovec iov;
663 uint64_t offset;
664 } CopyFromBackingFileCB;
666 static void qed_copy_from_backing_file_cb(void *opaque, int ret)
668 CopyFromBackingFileCB *copy_cb = opaque;
669 qemu_vfree(copy_cb->iov.iov_base);
670 gencb_complete(&copy_cb->gencb, ret);
673 static void qed_copy_from_backing_file_write(void *opaque, int ret)
675 CopyFromBackingFileCB *copy_cb = opaque;
676 BDRVQEDState *s = copy_cb->s;
677 BlockDriverAIOCB *aiocb;
679 if (ret) {
680 qed_copy_from_backing_file_cb(copy_cb, ret);
681 return;
684 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
685 aiocb = bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE,
686 &copy_cb->qiov,
687 copy_cb->qiov.size / BDRV_SECTOR_SIZE,
688 qed_copy_from_backing_file_cb, copy_cb);
689 if (!aiocb) {
690 qed_copy_from_backing_file_cb(copy_cb, -EIO);
695 * Copy data from backing file into the image
697 * @s: QED state
698 * @pos: Byte position in device
699 * @len: Number of bytes
700 * @offset: Byte offset in image file
701 * @cb: Completion function
702 * @opaque: User data for completion function
704 static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
705 uint64_t len, uint64_t offset,
706 BlockDriverCompletionFunc *cb,
707 void *opaque)
709 CopyFromBackingFileCB *copy_cb;
711 /* Skip copy entirely if there is no work to do */
712 if (len == 0) {
713 cb(opaque, 0);
714 return;
717 copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
718 copy_cb->s = s;
719 copy_cb->offset = offset;
720 copy_cb->iov.iov_base = qemu_blockalign(s->bs, len);
721 copy_cb->iov.iov_len = len;
722 qemu_iovec_init_external(&copy_cb->qiov, &copy_cb->iov, 1);
724 qed_read_backing_file(s, pos, &copy_cb->qiov,
725 qed_copy_from_backing_file_write, copy_cb);
729 * Link one or more contiguous clusters into a table
731 * @s: QED state
732 * @table: L2 table
733 * @index: First cluster index
734 * @n: Number of contiguous clusters
735 * @cluster: First cluster byte offset in image file
737 static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
738 unsigned int n, uint64_t cluster)
740 int i;
741 for (i = index; i < index + n; i++) {
742 table->offsets[i] = cluster;
743 cluster += s->header.cluster_size;
747 static void qed_aio_complete_bh(void *opaque)
749 QEDAIOCB *acb = opaque;
750 BlockDriverCompletionFunc *cb = acb->common.cb;
751 void *user_opaque = acb->common.opaque;
752 int ret = acb->bh_ret;
753 bool *finished = acb->finished;
755 qemu_bh_delete(acb->bh);
756 qemu_aio_release(acb);
758 /* Invoke callback */
759 cb(user_opaque, ret);
761 /* Signal cancel completion */
762 if (finished) {
763 *finished = true;
767 static void qed_aio_complete(QEDAIOCB *acb, int ret)
769 BDRVQEDState *s = acb_to_s(acb);
771 trace_qed_aio_complete(s, acb, ret);
773 /* Free resources */
774 qemu_iovec_destroy(&acb->cur_qiov);
775 qed_unref_l2_cache_entry(acb->request.l2_table);
777 /* Arrange for a bh to invoke the completion function */
778 acb->bh_ret = ret;
779 acb->bh = qemu_bh_new(qed_aio_complete_bh, acb);
780 qemu_bh_schedule(acb->bh);
782 /* Start next allocating write request waiting behind this one. Note that
783 * requests enqueue themselves when they first hit an unallocated cluster
784 * but they wait until the entire request is finished before waking up the
785 * next request in the queue. This ensures that we don't cycle through
786 * requests multiple times but rather finish one at a time completely.
788 if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
789 QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
790 acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
791 if (acb) {
792 qed_aio_next_io(acb, 0);
798 * Commit the current L2 table to the cache
800 static void qed_commit_l2_update(void *opaque, int ret)
802 QEDAIOCB *acb = opaque;
803 BDRVQEDState *s = acb_to_s(acb);
804 CachedL2Table *l2_table = acb->request.l2_table;
806 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
808 /* This is guaranteed to succeed because we just committed the entry to the
809 * cache.
811 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache,
812 l2_table->offset);
813 assert(acb->request.l2_table != NULL);
815 qed_aio_next_io(opaque, ret);
819 * Update L1 table with new L2 table offset and write it out
821 static void qed_aio_write_l1_update(void *opaque, int ret)
823 QEDAIOCB *acb = opaque;
824 BDRVQEDState *s = acb_to_s(acb);
825 int index;
827 if (ret) {
828 qed_aio_complete(acb, ret);
829 return;
832 index = qed_l1_index(s, acb->cur_pos);
833 s->l1_table->offsets[index] = acb->request.l2_table->offset;
835 qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
839 * Update L2 table with new cluster offsets and write them out
841 static void qed_aio_write_l2_update(void *opaque, int ret)
843 QEDAIOCB *acb = opaque;
844 BDRVQEDState *s = acb_to_s(acb);
845 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
846 int index;
848 if (ret) {
849 goto err;
852 if (need_alloc) {
853 qed_unref_l2_cache_entry(acb->request.l2_table);
854 acb->request.l2_table = qed_new_l2_table(s);
857 index = qed_l2_index(s, acb->cur_pos);
858 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
859 acb->cur_cluster);
861 if (need_alloc) {
862 /* Write out the whole new L2 table */
863 qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true,
864 qed_aio_write_l1_update, acb);
865 } else {
866 /* Write out only the updated part of the L2 table */
867 qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false,
868 qed_aio_next_io, acb);
870 return;
872 err:
873 qed_aio_complete(acb, ret);
877 * Flush new data clusters before updating the L2 table
879 * This flush is necessary when a backing file is in use. A crash during an
880 * allocating write could result in empty clusters in the image. If the write
881 * only touched a subregion of the cluster, then backing image sectors have
882 * been lost in the untouched region. The solution is to flush after writing a
883 * new data cluster and before updating the L2 table.
885 static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
887 QEDAIOCB *acb = opaque;
888 BDRVQEDState *s = acb_to_s(acb);
890 if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update, opaque)) {
891 qed_aio_complete(acb, -EIO);
896 * Write data to the image file
898 static void qed_aio_write_main(void *opaque, int ret)
900 QEDAIOCB *acb = opaque;
901 BDRVQEDState *s = acb_to_s(acb);
902 uint64_t offset = acb->cur_cluster +
903 qed_offset_into_cluster(s, acb->cur_pos);
904 BlockDriverCompletionFunc *next_fn;
905 BlockDriverAIOCB *file_acb;
907 trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
909 if (ret) {
910 qed_aio_complete(acb, ret);
911 return;
914 if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
915 next_fn = qed_aio_next_io;
916 } else {
917 if (s->bs->backing_hd) {
918 next_fn = qed_aio_write_flush_before_l2_update;
919 } else {
920 next_fn = qed_aio_write_l2_update;
924 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
925 file_acb = bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE,
926 &acb->cur_qiov,
927 acb->cur_qiov.size / BDRV_SECTOR_SIZE,
928 next_fn, acb);
929 if (!file_acb) {
930 qed_aio_complete(acb, -EIO);
935 * Populate back untouched region of new data cluster
937 static void qed_aio_write_postfill(void *opaque, int ret)
939 QEDAIOCB *acb = opaque;
940 BDRVQEDState *s = acb_to_s(acb);
941 uint64_t start = acb->cur_pos + acb->cur_qiov.size;
942 uint64_t len =
943 qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
944 uint64_t offset = acb->cur_cluster +
945 qed_offset_into_cluster(s, acb->cur_pos) +
946 acb->cur_qiov.size;
948 if (ret) {
949 qed_aio_complete(acb, ret);
950 return;
953 trace_qed_aio_write_postfill(s, acb, start, len, offset);
954 qed_copy_from_backing_file(s, start, len, offset,
955 qed_aio_write_main, acb);
959 * Populate front untouched region of new data cluster
961 static void qed_aio_write_prefill(void *opaque, int ret)
963 QEDAIOCB *acb = opaque;
964 BDRVQEDState *s = acb_to_s(acb);
965 uint64_t start = qed_start_of_cluster(s, acb->cur_pos);
966 uint64_t len = qed_offset_into_cluster(s, acb->cur_pos);
968 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
969 qed_copy_from_backing_file(s, start, len, acb->cur_cluster,
970 qed_aio_write_postfill, acb);
974 * Write new data cluster
976 * @acb: Write request
977 * @len: Length in bytes
979 * This path is taken when writing to previously unallocated clusters.
981 static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
983 BDRVQEDState *s = acb_to_s(acb);
985 /* Freeze this request if another allocating write is in progress */
986 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
987 QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
989 if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
990 return; /* wait for existing request to finish */
993 acb->cur_nclusters = qed_bytes_to_clusters(s,
994 qed_offset_into_cluster(s, acb->cur_pos) + len);
995 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
996 qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
998 /* Write new cluster if the image is already marked dirty */
999 if (s->header.features & QED_F_NEED_CHECK) {
1000 qed_aio_write_prefill(acb, 0);
1001 return;
1004 /* Mark the image dirty before writing the new cluster */
1005 s->header.features |= QED_F_NEED_CHECK;
1006 qed_write_header(s, qed_aio_write_prefill, acb);
1010 * Write data cluster in place
1012 * @acb: Write request
1013 * @offset: Cluster offset in bytes
1014 * @len: Length in bytes
1016 * This path is taken when writing to already allocated clusters.
1018 static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1020 /* Calculate the I/O vector */
1021 acb->cur_cluster = offset;
1022 qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1024 /* Do the actual write */
1025 qed_aio_write_main(acb, 0);
1029 * Write data cluster
1031 * @opaque: Write request
1032 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1033 * or -errno
1034 * @offset: Cluster offset in bytes
1035 * @len: Length in bytes
1037 * Callback from qed_find_cluster().
1039 static void qed_aio_write_data(void *opaque, int ret,
1040 uint64_t offset, size_t len)
1042 QEDAIOCB *acb = opaque;
1044 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1046 acb->find_cluster_ret = ret;
1048 switch (ret) {
1049 case QED_CLUSTER_FOUND:
1050 qed_aio_write_inplace(acb, offset, len);
1051 break;
1053 case QED_CLUSTER_L2:
1054 case QED_CLUSTER_L1:
1055 qed_aio_write_alloc(acb, len);
1056 break;
1058 default:
1059 qed_aio_complete(acb, ret);
1060 break;
1065 * Read data cluster
1067 * @opaque: Read request
1068 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1069 * or -errno
1070 * @offset: Cluster offset in bytes
1071 * @len: Length in bytes
1073 * Callback from qed_find_cluster().
1075 static void qed_aio_read_data(void *opaque, int ret,
1076 uint64_t offset, size_t len)
1078 QEDAIOCB *acb = opaque;
1079 BDRVQEDState *s = acb_to_s(acb);
1080 BlockDriverState *bs = acb->common.bs;
1081 BlockDriverAIOCB *file_acb;
1083 /* Adjust offset into cluster */
1084 offset += qed_offset_into_cluster(s, acb->cur_pos);
1086 trace_qed_aio_read_data(s, acb, ret, offset, len);
1088 if (ret < 0) {
1089 goto err;
1092 qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1094 /* Handle backing file and unallocated sparse hole reads */
1095 if (ret != QED_CLUSTER_FOUND) {
1096 qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
1097 qed_aio_next_io, acb);
1098 return;
1101 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1102 file_acb = bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE,
1103 &acb->cur_qiov,
1104 acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1105 qed_aio_next_io, acb);
1106 if (!file_acb) {
1107 ret = -EIO;
1108 goto err;
1110 return;
1112 err:
1113 qed_aio_complete(acb, ret);
1117 * Begin next I/O or complete the request
1119 static void qed_aio_next_io(void *opaque, int ret)
1121 QEDAIOCB *acb = opaque;
1122 BDRVQEDState *s = acb_to_s(acb);
1123 QEDFindClusterFunc *io_fn =
1124 acb->is_write ? qed_aio_write_data : qed_aio_read_data;
1126 trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
1128 /* Handle I/O error */
1129 if (ret) {
1130 qed_aio_complete(acb, ret);
1131 return;
1134 acb->qiov_offset += acb->cur_qiov.size;
1135 acb->cur_pos += acb->cur_qiov.size;
1136 qemu_iovec_reset(&acb->cur_qiov);
1138 /* Complete request */
1139 if (acb->cur_pos >= acb->end_pos) {
1140 qed_aio_complete(acb, 0);
1141 return;
1144 /* Find next cluster and start I/O */
1145 qed_find_cluster(s, &acb->request,
1146 acb->cur_pos, acb->end_pos - acb->cur_pos,
1147 io_fn, acb);
1150 static BlockDriverAIOCB *qed_aio_setup(BlockDriverState *bs,
1151 int64_t sector_num,
1152 QEMUIOVector *qiov, int nb_sectors,
1153 BlockDriverCompletionFunc *cb,
1154 void *opaque, bool is_write)
1156 QEDAIOCB *acb = qemu_aio_get(&qed_aio_pool, bs, cb, opaque);
1158 trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
1159 opaque, is_write);
1161 acb->is_write = is_write;
1162 acb->finished = NULL;
1163 acb->qiov = qiov;
1164 acb->qiov_offset = 0;
1165 acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
1166 acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
1167 acb->request.l2_table = NULL;
1168 qemu_iovec_init(&acb->cur_qiov, qiov->niov);
1170 /* Start request */
1171 qed_aio_next_io(acb, 0);
1172 return &acb->common;
1175 static BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
1176 int64_t sector_num,
1177 QEMUIOVector *qiov, int nb_sectors,
1178 BlockDriverCompletionFunc *cb,
1179 void *opaque)
1181 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, false);
1184 static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
1185 int64_t sector_num,
1186 QEMUIOVector *qiov, int nb_sectors,
1187 BlockDriverCompletionFunc *cb,
1188 void *opaque)
1190 return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, true);
1193 static BlockDriverAIOCB *bdrv_qed_aio_flush(BlockDriverState *bs,
1194 BlockDriverCompletionFunc *cb,
1195 void *opaque)
1197 return bdrv_aio_flush(bs->file, cb, opaque);
1200 static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset)
1202 return -ENOTSUP;
1205 static int64_t bdrv_qed_getlength(BlockDriverState *bs)
1207 BDRVQEDState *s = bs->opaque;
1208 return s->header.image_size;
1211 static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1213 BDRVQEDState *s = bs->opaque;
1215 memset(bdi, 0, sizeof(*bdi));
1216 bdi->cluster_size = s->header.cluster_size;
1217 return 0;
1220 static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1221 const char *backing_file,
1222 const char *backing_fmt)
1224 BDRVQEDState *s = bs->opaque;
1225 QEDHeader new_header, le_header;
1226 void *buffer;
1227 size_t buffer_len, backing_file_len;
1228 int ret;
1230 /* Refuse to set backing filename if unknown compat feature bits are
1231 * active. If the image uses an unknown compat feature then we may not
1232 * know the layout of data following the header structure and cannot safely
1233 * add a new string.
1235 if (backing_file && (s->header.compat_features &
1236 ~QED_COMPAT_FEATURE_MASK)) {
1237 return -ENOTSUP;
1240 memcpy(&new_header, &s->header, sizeof(new_header));
1242 new_header.features &= ~(QED_F_BACKING_FILE |
1243 QED_F_BACKING_FORMAT_NO_PROBE);
1245 /* Adjust feature flags */
1246 if (backing_file) {
1247 new_header.features |= QED_F_BACKING_FILE;
1249 if (qed_fmt_is_raw(backing_fmt)) {
1250 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1254 /* Calculate new header size */
1255 backing_file_len = 0;
1257 if (backing_file) {
1258 backing_file_len = strlen(backing_file);
1261 buffer_len = sizeof(new_header);
1262 new_header.backing_filename_offset = buffer_len;
1263 new_header.backing_filename_size = backing_file_len;
1264 buffer_len += backing_file_len;
1266 /* Make sure we can rewrite header without failing */
1267 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1268 return -ENOSPC;
1271 /* Prepare new header */
1272 buffer = qemu_malloc(buffer_len);
1274 qed_header_cpu_to_le(&new_header, &le_header);
1275 memcpy(buffer, &le_header, sizeof(le_header));
1276 buffer_len = sizeof(le_header);
1278 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1279 buffer_len += backing_file_len;
1281 /* Write new header */
1282 ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len);
1283 qemu_free(buffer);
1284 if (ret == 0) {
1285 memcpy(&s->header, &new_header, sizeof(new_header));
1287 return ret;
1290 static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result)
1292 BDRVQEDState *s = bs->opaque;
1294 return qed_check(s, result, false);
1297 static QEMUOptionParameter qed_create_options[] = {
1299 .name = BLOCK_OPT_SIZE,
1300 .type = OPT_SIZE,
1301 .help = "Virtual disk size (in bytes)"
1302 }, {
1303 .name = BLOCK_OPT_BACKING_FILE,
1304 .type = OPT_STRING,
1305 .help = "File name of a base image"
1306 }, {
1307 .name = BLOCK_OPT_BACKING_FMT,
1308 .type = OPT_STRING,
1309 .help = "Image format of the base image"
1310 }, {
1311 .name = BLOCK_OPT_CLUSTER_SIZE,
1312 .type = OPT_SIZE,
1313 .help = "Cluster size (in bytes)"
1314 }, {
1315 .name = BLOCK_OPT_TABLE_SIZE,
1316 .type = OPT_SIZE,
1317 .help = "L1/L2 table size (in clusters)"
1319 { /* end of list */ }
1322 static BlockDriver bdrv_qed = {
1323 .format_name = "qed",
1324 .instance_size = sizeof(BDRVQEDState),
1325 .create_options = qed_create_options,
1327 .bdrv_probe = bdrv_qed_probe,
1328 .bdrv_open = bdrv_qed_open,
1329 .bdrv_close = bdrv_qed_close,
1330 .bdrv_create = bdrv_qed_create,
1331 .bdrv_flush = bdrv_qed_flush,
1332 .bdrv_is_allocated = bdrv_qed_is_allocated,
1333 .bdrv_make_empty = bdrv_qed_make_empty,
1334 .bdrv_aio_readv = bdrv_qed_aio_readv,
1335 .bdrv_aio_writev = bdrv_qed_aio_writev,
1336 .bdrv_aio_flush = bdrv_qed_aio_flush,
1337 .bdrv_truncate = bdrv_qed_truncate,
1338 .bdrv_getlength = bdrv_qed_getlength,
1339 .bdrv_get_info = bdrv_qed_get_info,
1340 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
1341 .bdrv_check = bdrv_qed_check,
1344 static void bdrv_qed_init(void)
1346 bdrv_register(&bdrv_qed);
1349 block_init(bdrv_qed_init);