xref: /openbmc/qemu/block/qed.c (revision 0d09c7970063ecdabdc2841ffa32e1cf3e032336)
175411d23SStefan Hajnoczi /*
275411d23SStefan Hajnoczi  * QEMU Enhanced Disk Format
375411d23SStefan Hajnoczi  *
475411d23SStefan Hajnoczi  * Copyright IBM, Corp. 2010
575411d23SStefan Hajnoczi  *
675411d23SStefan Hajnoczi  * Authors:
775411d23SStefan Hajnoczi  *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
875411d23SStefan Hajnoczi  *  Anthony Liguori   <aliguori@us.ibm.com>
975411d23SStefan Hajnoczi  *
1075411d23SStefan Hajnoczi  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
1175411d23SStefan Hajnoczi  * See the COPYING.LIB file in the top-level directory.
1275411d23SStefan Hajnoczi  *
1375411d23SStefan Hajnoczi  */
1475411d23SStefan Hajnoczi 
15eabba580SStefan Hajnoczi #include "trace.h"
1675411d23SStefan Hajnoczi #include "qed.h"
1775411d23SStefan Hajnoczi 
18eabba580SStefan Hajnoczi static void qed_aio_cancel(BlockDriverAIOCB *blockacb)
19eabba580SStefan Hajnoczi {
20eabba580SStefan Hajnoczi     QEDAIOCB *acb = (QEDAIOCB *)blockacb;
21eabba580SStefan Hajnoczi     bool finished = false;
22eabba580SStefan Hajnoczi 
23eabba580SStefan Hajnoczi     /* Wait for the request to finish */
24eabba580SStefan Hajnoczi     acb->finished = &finished;
25eabba580SStefan Hajnoczi     while (!finished) {
26eabba580SStefan Hajnoczi         qemu_aio_wait();
27eabba580SStefan Hajnoczi     }
28eabba580SStefan Hajnoczi }
29eabba580SStefan Hajnoczi 
30eabba580SStefan Hajnoczi static AIOPool qed_aio_pool = {
31eabba580SStefan Hajnoczi     .aiocb_size         = sizeof(QEDAIOCB),
32eabba580SStefan Hajnoczi     .cancel             = qed_aio_cancel,
33eabba580SStefan Hajnoczi };
34eabba580SStefan Hajnoczi 
3575411d23SStefan Hajnoczi static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
3675411d23SStefan Hajnoczi                           const char *filename)
3775411d23SStefan Hajnoczi {
3875411d23SStefan Hajnoczi     const QEDHeader *header = (const QEDHeader *)buf;
3975411d23SStefan Hajnoczi 
4075411d23SStefan Hajnoczi     if (buf_size < sizeof(*header)) {
4175411d23SStefan Hajnoczi         return 0;
4275411d23SStefan Hajnoczi     }
4375411d23SStefan Hajnoczi     if (le32_to_cpu(header->magic) != QED_MAGIC) {
4475411d23SStefan Hajnoczi         return 0;
4575411d23SStefan Hajnoczi     }
4675411d23SStefan Hajnoczi     return 100;
4775411d23SStefan Hajnoczi }
4875411d23SStefan Hajnoczi 
4975411d23SStefan Hajnoczi /**
5075411d23SStefan Hajnoczi  * Check whether an image format is raw
5175411d23SStefan Hajnoczi  *
5275411d23SStefan Hajnoczi  * @fmt:    Backing file format, may be NULL
5375411d23SStefan Hajnoczi  */
5475411d23SStefan Hajnoczi static bool qed_fmt_is_raw(const char *fmt)
5575411d23SStefan Hajnoczi {
5675411d23SStefan Hajnoczi     return fmt && strcmp(fmt, "raw") == 0;
5775411d23SStefan Hajnoczi }
5875411d23SStefan Hajnoczi 
5975411d23SStefan Hajnoczi static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
6075411d23SStefan Hajnoczi {
6175411d23SStefan Hajnoczi     cpu->magic = le32_to_cpu(le->magic);
6275411d23SStefan Hajnoczi     cpu->cluster_size = le32_to_cpu(le->cluster_size);
6375411d23SStefan Hajnoczi     cpu->table_size = le32_to_cpu(le->table_size);
6475411d23SStefan Hajnoczi     cpu->header_size = le32_to_cpu(le->header_size);
6575411d23SStefan Hajnoczi     cpu->features = le64_to_cpu(le->features);
6675411d23SStefan Hajnoczi     cpu->compat_features = le64_to_cpu(le->compat_features);
6775411d23SStefan Hajnoczi     cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
6875411d23SStefan Hajnoczi     cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
6975411d23SStefan Hajnoczi     cpu->image_size = le64_to_cpu(le->image_size);
7075411d23SStefan Hajnoczi     cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
7175411d23SStefan Hajnoczi     cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
7275411d23SStefan Hajnoczi }
7375411d23SStefan Hajnoczi 
7475411d23SStefan Hajnoczi static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
7575411d23SStefan Hajnoczi {
7675411d23SStefan Hajnoczi     le->magic = cpu_to_le32(cpu->magic);
7775411d23SStefan Hajnoczi     le->cluster_size = cpu_to_le32(cpu->cluster_size);
7875411d23SStefan Hajnoczi     le->table_size = cpu_to_le32(cpu->table_size);
7975411d23SStefan Hajnoczi     le->header_size = cpu_to_le32(cpu->header_size);
8075411d23SStefan Hajnoczi     le->features = cpu_to_le64(cpu->features);
8175411d23SStefan Hajnoczi     le->compat_features = cpu_to_le64(cpu->compat_features);
8275411d23SStefan Hajnoczi     le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
8375411d23SStefan Hajnoczi     le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
8475411d23SStefan Hajnoczi     le->image_size = cpu_to_le64(cpu->image_size);
8575411d23SStefan Hajnoczi     le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
8675411d23SStefan Hajnoczi     le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
8775411d23SStefan Hajnoczi }
8875411d23SStefan Hajnoczi 
8975411d23SStefan Hajnoczi static int qed_write_header_sync(BDRVQEDState *s)
9075411d23SStefan Hajnoczi {
9175411d23SStefan Hajnoczi     QEDHeader le;
9275411d23SStefan Hajnoczi     int ret;
9375411d23SStefan Hajnoczi 
9475411d23SStefan Hajnoczi     qed_header_cpu_to_le(&s->header, &le);
9575411d23SStefan Hajnoczi     ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le));
9675411d23SStefan Hajnoczi     if (ret != sizeof(le)) {
9775411d23SStefan Hajnoczi         return ret;
9875411d23SStefan Hajnoczi     }
9975411d23SStefan Hajnoczi     return 0;
10075411d23SStefan Hajnoczi }
10175411d23SStefan Hajnoczi 
10201979a98SStefan Hajnoczi typedef struct {
10301979a98SStefan Hajnoczi     GenericCB gencb;
10401979a98SStefan Hajnoczi     BDRVQEDState *s;
10501979a98SStefan Hajnoczi     struct iovec iov;
10601979a98SStefan Hajnoczi     QEMUIOVector qiov;
10701979a98SStefan Hajnoczi     int nsectors;
10801979a98SStefan Hajnoczi     uint8_t *buf;
10901979a98SStefan Hajnoczi } QEDWriteHeaderCB;
11001979a98SStefan Hajnoczi 
11101979a98SStefan Hajnoczi static void qed_write_header_cb(void *opaque, int ret)
11201979a98SStefan Hajnoczi {
11301979a98SStefan Hajnoczi     QEDWriteHeaderCB *write_header_cb = opaque;
11401979a98SStefan Hajnoczi 
11501979a98SStefan Hajnoczi     qemu_vfree(write_header_cb->buf);
11601979a98SStefan Hajnoczi     gencb_complete(write_header_cb, ret);
11701979a98SStefan Hajnoczi }
11801979a98SStefan Hajnoczi 
11901979a98SStefan Hajnoczi static void qed_write_header_read_cb(void *opaque, int ret)
12001979a98SStefan Hajnoczi {
12101979a98SStefan Hajnoczi     QEDWriteHeaderCB *write_header_cb = opaque;
12201979a98SStefan Hajnoczi     BDRVQEDState *s = write_header_cb->s;
12301979a98SStefan Hajnoczi     BlockDriverAIOCB *acb;
12401979a98SStefan Hajnoczi 
12501979a98SStefan Hajnoczi     if (ret) {
12601979a98SStefan Hajnoczi         qed_write_header_cb(write_header_cb, ret);
12701979a98SStefan Hajnoczi         return;
12801979a98SStefan Hajnoczi     }
12901979a98SStefan Hajnoczi 
13001979a98SStefan Hajnoczi     /* Update header */
13101979a98SStefan Hajnoczi     qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf);
13201979a98SStefan Hajnoczi 
13301979a98SStefan Hajnoczi     acb = bdrv_aio_writev(s->bs->file, 0, &write_header_cb->qiov,
13401979a98SStefan Hajnoczi                           write_header_cb->nsectors, qed_write_header_cb,
13501979a98SStefan Hajnoczi                           write_header_cb);
13601979a98SStefan Hajnoczi     if (!acb) {
13701979a98SStefan Hajnoczi         qed_write_header_cb(write_header_cb, -EIO);
13801979a98SStefan Hajnoczi     }
13901979a98SStefan Hajnoczi }
14001979a98SStefan Hajnoczi 
14101979a98SStefan Hajnoczi /**
14201979a98SStefan Hajnoczi  * Update header in-place (does not rewrite backing filename or other strings)
14301979a98SStefan Hajnoczi  *
14401979a98SStefan Hajnoczi  * This function only updates known header fields in-place and does not affect
14501979a98SStefan Hajnoczi  * extra data after the QED header.
14601979a98SStefan Hajnoczi  */
14701979a98SStefan Hajnoczi static void qed_write_header(BDRVQEDState *s, BlockDriverCompletionFunc cb,
14801979a98SStefan Hajnoczi                              void *opaque)
14901979a98SStefan Hajnoczi {
15001979a98SStefan Hajnoczi     /* We must write full sectors for O_DIRECT but cannot necessarily generate
15101979a98SStefan Hajnoczi      * the data following the header if an unrecognized compat feature is
15201979a98SStefan Hajnoczi      * active.  Therefore, first read the sectors containing the header, update
15301979a98SStefan Hajnoczi      * them, and write back.
15401979a98SStefan Hajnoczi      */
15501979a98SStefan Hajnoczi 
15601979a98SStefan Hajnoczi     BlockDriverAIOCB *acb;
15701979a98SStefan Hajnoczi     int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) /
15801979a98SStefan Hajnoczi                    BDRV_SECTOR_SIZE;
15901979a98SStefan Hajnoczi     size_t len = nsectors * BDRV_SECTOR_SIZE;
16001979a98SStefan Hajnoczi     QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb),
16101979a98SStefan Hajnoczi                                                     cb, opaque);
16201979a98SStefan Hajnoczi 
16301979a98SStefan Hajnoczi     write_header_cb->s = s;
16401979a98SStefan Hajnoczi     write_header_cb->nsectors = nsectors;
16501979a98SStefan Hajnoczi     write_header_cb->buf = qemu_blockalign(s->bs, len);
16601979a98SStefan Hajnoczi     write_header_cb->iov.iov_base = write_header_cb->buf;
16701979a98SStefan Hajnoczi     write_header_cb->iov.iov_len = len;
16801979a98SStefan Hajnoczi     qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1);
16901979a98SStefan Hajnoczi 
17001979a98SStefan Hajnoczi     acb = bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors,
17101979a98SStefan Hajnoczi                          qed_write_header_read_cb, write_header_cb);
17201979a98SStefan Hajnoczi     if (!acb) {
17301979a98SStefan Hajnoczi         qed_write_header_cb(write_header_cb, -EIO);
17401979a98SStefan Hajnoczi     }
17501979a98SStefan Hajnoczi }
17601979a98SStefan Hajnoczi 
17775411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
17875411d23SStefan Hajnoczi {
17975411d23SStefan Hajnoczi     uint64_t table_entries;
18075411d23SStefan Hajnoczi     uint64_t l2_size;
18175411d23SStefan Hajnoczi 
18275411d23SStefan Hajnoczi     table_entries = (table_size * cluster_size) / sizeof(uint64_t);
18375411d23SStefan Hajnoczi     l2_size = table_entries * cluster_size;
18475411d23SStefan Hajnoczi 
18575411d23SStefan Hajnoczi     return l2_size * table_entries;
18675411d23SStefan Hajnoczi }
18775411d23SStefan Hajnoczi 
18875411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size)
18975411d23SStefan Hajnoczi {
19075411d23SStefan Hajnoczi     if (cluster_size < QED_MIN_CLUSTER_SIZE ||
19175411d23SStefan Hajnoczi         cluster_size > QED_MAX_CLUSTER_SIZE) {
19275411d23SStefan Hajnoczi         return false;
19375411d23SStefan Hajnoczi     }
19475411d23SStefan Hajnoczi     if (cluster_size & (cluster_size - 1)) {
19575411d23SStefan Hajnoczi         return false; /* not power of 2 */
19675411d23SStefan Hajnoczi     }
19775411d23SStefan Hajnoczi     return true;
19875411d23SStefan Hajnoczi }
19975411d23SStefan Hajnoczi 
20075411d23SStefan Hajnoczi static bool qed_is_table_size_valid(uint32_t table_size)
20175411d23SStefan Hajnoczi {
20275411d23SStefan Hajnoczi     if (table_size < QED_MIN_TABLE_SIZE ||
20375411d23SStefan Hajnoczi         table_size > QED_MAX_TABLE_SIZE) {
20475411d23SStefan Hajnoczi         return false;
20575411d23SStefan Hajnoczi     }
20675411d23SStefan Hajnoczi     if (table_size & (table_size - 1)) {
20775411d23SStefan Hajnoczi         return false; /* not power of 2 */
20875411d23SStefan Hajnoczi     }
20975411d23SStefan Hajnoczi     return true;
21075411d23SStefan Hajnoczi }
21175411d23SStefan Hajnoczi 
21275411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
21375411d23SStefan Hajnoczi                                     uint32_t table_size)
21475411d23SStefan Hajnoczi {
21575411d23SStefan Hajnoczi     if (image_size % BDRV_SECTOR_SIZE != 0) {
21675411d23SStefan Hajnoczi         return false; /* not multiple of sector size */
21775411d23SStefan Hajnoczi     }
21875411d23SStefan Hajnoczi     if (image_size > qed_max_image_size(cluster_size, table_size)) {
21975411d23SStefan Hajnoczi         return false; /* image is too large */
22075411d23SStefan Hajnoczi     }
22175411d23SStefan Hajnoczi     return true;
22275411d23SStefan Hajnoczi }
22375411d23SStefan Hajnoczi 
22475411d23SStefan Hajnoczi /**
22575411d23SStefan Hajnoczi  * Read a string of known length from the image file
22675411d23SStefan Hajnoczi  *
22775411d23SStefan Hajnoczi  * @file:       Image file
22875411d23SStefan Hajnoczi  * @offset:     File offset to start of string, in bytes
22975411d23SStefan Hajnoczi  * @n:          String length in bytes
23075411d23SStefan Hajnoczi  * @buf:        Destination buffer
23175411d23SStefan Hajnoczi  * @buflen:     Destination buffer length in bytes
23275411d23SStefan Hajnoczi  * @ret:        0 on success, -errno on failure
23375411d23SStefan Hajnoczi  *
23475411d23SStefan Hajnoczi  * The string is NUL-terminated.
23575411d23SStefan Hajnoczi  */
23675411d23SStefan Hajnoczi static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n,
23775411d23SStefan Hajnoczi                            char *buf, size_t buflen)
23875411d23SStefan Hajnoczi {
23975411d23SStefan Hajnoczi     int ret;
24075411d23SStefan Hajnoczi     if (n >= buflen) {
24175411d23SStefan Hajnoczi         return -EINVAL;
24275411d23SStefan Hajnoczi     }
24375411d23SStefan Hajnoczi     ret = bdrv_pread(file, offset, buf, n);
24475411d23SStefan Hajnoczi     if (ret < 0) {
24575411d23SStefan Hajnoczi         return ret;
24675411d23SStefan Hajnoczi     }
24775411d23SStefan Hajnoczi     buf[n] = '\0';
24875411d23SStefan Hajnoczi     return 0;
24975411d23SStefan Hajnoczi }
25075411d23SStefan Hajnoczi 
251eabba580SStefan Hajnoczi /**
252eabba580SStefan Hajnoczi  * Allocate new clusters
253eabba580SStefan Hajnoczi  *
254eabba580SStefan Hajnoczi  * @s:          QED state
255eabba580SStefan Hajnoczi  * @n:          Number of contiguous clusters to allocate
256eabba580SStefan Hajnoczi  * @ret:        Offset of first allocated cluster
257eabba580SStefan Hajnoczi  *
258eabba580SStefan Hajnoczi  * This function only produces the offset where the new clusters should be
259eabba580SStefan Hajnoczi  * written.  It updates BDRVQEDState but does not make any changes to the image
260eabba580SStefan Hajnoczi  * file.
261eabba580SStefan Hajnoczi  */
262eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
263eabba580SStefan Hajnoczi {
264eabba580SStefan Hajnoczi     uint64_t offset = s->file_size;
265eabba580SStefan Hajnoczi     s->file_size += n * s->header.cluster_size;
266eabba580SStefan Hajnoczi     return offset;
267eabba580SStefan Hajnoczi }
268eabba580SStefan Hajnoczi 
269298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s)
270298800caSStefan Hajnoczi {
271298800caSStefan Hajnoczi     /* Honor O_DIRECT memory alignment requirements */
272298800caSStefan Hajnoczi     return qemu_blockalign(s->bs,
273298800caSStefan Hajnoczi                            s->header.cluster_size * s->header.table_size);
274298800caSStefan Hajnoczi }
275298800caSStefan Hajnoczi 
276eabba580SStefan Hajnoczi /**
277eabba580SStefan Hajnoczi  * Allocate a new zeroed L2 table
278eabba580SStefan Hajnoczi  */
279eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
280eabba580SStefan Hajnoczi {
281eabba580SStefan Hajnoczi     CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
282eabba580SStefan Hajnoczi 
283eabba580SStefan Hajnoczi     l2_table->table = qed_alloc_table(s);
284eabba580SStefan Hajnoczi     l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
285eabba580SStefan Hajnoczi 
286eabba580SStefan Hajnoczi     memset(l2_table->table->offsets, 0,
287eabba580SStefan Hajnoczi            s->header.cluster_size * s->header.table_size);
288eabba580SStefan Hajnoczi     return l2_table;
289eabba580SStefan Hajnoczi }
290eabba580SStefan Hajnoczi 
291eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret);
292eabba580SStefan Hajnoczi 
29375411d23SStefan Hajnoczi static int bdrv_qed_open(BlockDriverState *bs, int flags)
29475411d23SStefan Hajnoczi {
29575411d23SStefan Hajnoczi     BDRVQEDState *s = bs->opaque;
29675411d23SStefan Hajnoczi     QEDHeader le_header;
29775411d23SStefan Hajnoczi     int64_t file_size;
29875411d23SStefan Hajnoczi     int ret;
29975411d23SStefan Hajnoczi 
30075411d23SStefan Hajnoczi     s->bs = bs;
301eabba580SStefan Hajnoczi     QSIMPLEQ_INIT(&s->allocating_write_reqs);
30275411d23SStefan Hajnoczi 
30375411d23SStefan Hajnoczi     ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header));
30475411d23SStefan Hajnoczi     if (ret < 0) {
30575411d23SStefan Hajnoczi         return ret;
30675411d23SStefan Hajnoczi     }
30775411d23SStefan Hajnoczi     ret = 0; /* ret should always be 0 or -errno */
30875411d23SStefan Hajnoczi     qed_header_le_to_cpu(&le_header, &s->header);
30975411d23SStefan Hajnoczi 
31075411d23SStefan Hajnoczi     if (s->header.magic != QED_MAGIC) {
31175411d23SStefan Hajnoczi         return -EINVAL;
31275411d23SStefan Hajnoczi     }
31375411d23SStefan Hajnoczi     if (s->header.features & ~QED_FEATURE_MASK) {
31475411d23SStefan Hajnoczi         return -ENOTSUP; /* image uses unsupported feature bits */
31575411d23SStefan Hajnoczi     }
31675411d23SStefan Hajnoczi     if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
31775411d23SStefan Hajnoczi         return -EINVAL;
31875411d23SStefan Hajnoczi     }
31975411d23SStefan Hajnoczi 
32075411d23SStefan Hajnoczi     /* Round down file size to the last cluster */
32175411d23SStefan Hajnoczi     file_size = bdrv_getlength(bs->file);
32275411d23SStefan Hajnoczi     if (file_size < 0) {
32375411d23SStefan Hajnoczi         return file_size;
32475411d23SStefan Hajnoczi     }
32575411d23SStefan Hajnoczi     s->file_size = qed_start_of_cluster(s, file_size);
32675411d23SStefan Hajnoczi 
32775411d23SStefan Hajnoczi     if (!qed_is_table_size_valid(s->header.table_size)) {
32875411d23SStefan Hajnoczi         return -EINVAL;
32975411d23SStefan Hajnoczi     }
33075411d23SStefan Hajnoczi     if (!qed_is_image_size_valid(s->header.image_size,
33175411d23SStefan Hajnoczi                                  s->header.cluster_size,
33275411d23SStefan Hajnoczi                                  s->header.table_size)) {
33375411d23SStefan Hajnoczi         return -EINVAL;
33475411d23SStefan Hajnoczi     }
33575411d23SStefan Hajnoczi     if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
33675411d23SStefan Hajnoczi         return -EINVAL;
33775411d23SStefan Hajnoczi     }
33875411d23SStefan Hajnoczi 
33975411d23SStefan Hajnoczi     s->table_nelems = (s->header.cluster_size * s->header.table_size) /
34075411d23SStefan Hajnoczi                       sizeof(uint64_t);
34175411d23SStefan Hajnoczi     s->l2_shift = ffs(s->header.cluster_size) - 1;
34275411d23SStefan Hajnoczi     s->l2_mask = s->table_nelems - 1;
34375411d23SStefan Hajnoczi     s->l1_shift = s->l2_shift + ffs(s->table_nelems) - 1;
34475411d23SStefan Hajnoczi 
34575411d23SStefan Hajnoczi     if ((s->header.features & QED_F_BACKING_FILE)) {
34675411d23SStefan Hajnoczi         if ((uint64_t)s->header.backing_filename_offset +
34775411d23SStefan Hajnoczi             s->header.backing_filename_size >
34875411d23SStefan Hajnoczi             s->header.cluster_size * s->header.header_size) {
34975411d23SStefan Hajnoczi             return -EINVAL;
35075411d23SStefan Hajnoczi         }
35175411d23SStefan Hajnoczi 
35275411d23SStefan Hajnoczi         ret = qed_read_string(bs->file, s->header.backing_filename_offset,
35375411d23SStefan Hajnoczi                               s->header.backing_filename_size, bs->backing_file,
35475411d23SStefan Hajnoczi                               sizeof(bs->backing_file));
35575411d23SStefan Hajnoczi         if (ret < 0) {
35675411d23SStefan Hajnoczi             return ret;
35775411d23SStefan Hajnoczi         }
35875411d23SStefan Hajnoczi 
35975411d23SStefan Hajnoczi         if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
36075411d23SStefan Hajnoczi             pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
36175411d23SStefan Hajnoczi         }
36275411d23SStefan Hajnoczi     }
36375411d23SStefan Hajnoczi 
36475411d23SStefan Hajnoczi     /* Reset unknown autoclear feature bits.  This is a backwards
36575411d23SStefan Hajnoczi      * compatibility mechanism that allows images to be opened by older
36675411d23SStefan Hajnoczi      * programs, which "knock out" unknown feature bits.  When an image is
36775411d23SStefan Hajnoczi      * opened by a newer program again it can detect that the autoclear
36875411d23SStefan Hajnoczi      * feature is no longer valid.
36975411d23SStefan Hajnoczi      */
37075411d23SStefan Hajnoczi     if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
37175411d23SStefan Hajnoczi         !bdrv_is_read_only(bs->file)) {
37275411d23SStefan Hajnoczi         s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
37375411d23SStefan Hajnoczi 
37475411d23SStefan Hajnoczi         ret = qed_write_header_sync(s);
37575411d23SStefan Hajnoczi         if (ret) {
37675411d23SStefan Hajnoczi             return ret;
37775411d23SStefan Hajnoczi         }
37875411d23SStefan Hajnoczi 
37975411d23SStefan Hajnoczi         /* From here on only known autoclear feature bits are valid */
38075411d23SStefan Hajnoczi         bdrv_flush(bs->file);
38175411d23SStefan Hajnoczi     }
38275411d23SStefan Hajnoczi 
383298800caSStefan Hajnoczi     s->l1_table = qed_alloc_table(s);
384298800caSStefan Hajnoczi     qed_init_l2_cache(&s->l2_cache);
385298800caSStefan Hajnoczi 
386298800caSStefan Hajnoczi     ret = qed_read_l1_table_sync(s);
387298800caSStefan Hajnoczi     if (ret) {
38801979a98SStefan Hajnoczi         goto out;
38901979a98SStefan Hajnoczi     }
39001979a98SStefan Hajnoczi 
39101979a98SStefan Hajnoczi     /* If image was not closed cleanly, check consistency */
39201979a98SStefan Hajnoczi     if (s->header.features & QED_F_NEED_CHECK) {
39301979a98SStefan Hajnoczi         /* Read-only images cannot be fixed.  There is no risk of corruption
39401979a98SStefan Hajnoczi          * since write operations are not possible.  Therefore, allow
39501979a98SStefan Hajnoczi          * potentially inconsistent images to be opened read-only.  This can
39601979a98SStefan Hajnoczi          * aid data recovery from an otherwise inconsistent image.
39701979a98SStefan Hajnoczi          */
39801979a98SStefan Hajnoczi         if (!bdrv_is_read_only(bs->file)) {
39901979a98SStefan Hajnoczi             BdrvCheckResult result = {0};
40001979a98SStefan Hajnoczi 
40101979a98SStefan Hajnoczi             ret = qed_check(s, &result, true);
40201979a98SStefan Hajnoczi             if (!ret && !result.corruptions && !result.check_errors) {
40301979a98SStefan Hajnoczi                 /* Ensure fixes reach storage before clearing check bit */
40401979a98SStefan Hajnoczi                 bdrv_flush(s->bs);
40501979a98SStefan Hajnoczi 
40601979a98SStefan Hajnoczi                 s->header.features &= ~QED_F_NEED_CHECK;
40701979a98SStefan Hajnoczi                 qed_write_header_sync(s);
40801979a98SStefan Hajnoczi             }
40901979a98SStefan Hajnoczi         }
41001979a98SStefan Hajnoczi     }
41101979a98SStefan Hajnoczi 
41201979a98SStefan Hajnoczi out:
41301979a98SStefan Hajnoczi     if (ret) {
414298800caSStefan Hajnoczi         qed_free_l2_cache(&s->l2_cache);
415298800caSStefan Hajnoczi         qemu_vfree(s->l1_table);
416298800caSStefan Hajnoczi     }
41775411d23SStefan Hajnoczi     return ret;
41875411d23SStefan Hajnoczi }
41975411d23SStefan Hajnoczi 
42075411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs)
42175411d23SStefan Hajnoczi {
422298800caSStefan Hajnoczi     BDRVQEDState *s = bs->opaque;
423298800caSStefan Hajnoczi 
42401979a98SStefan Hajnoczi     /* Ensure writes reach stable storage */
42501979a98SStefan Hajnoczi     bdrv_flush(bs->file);
42601979a98SStefan Hajnoczi 
42701979a98SStefan Hajnoczi     /* Clean shutdown, no check required on next open */
42801979a98SStefan Hajnoczi     if (s->header.features & QED_F_NEED_CHECK) {
42901979a98SStefan Hajnoczi         s->header.features &= ~QED_F_NEED_CHECK;
43001979a98SStefan Hajnoczi         qed_write_header_sync(s);
43101979a98SStefan Hajnoczi     }
43201979a98SStefan Hajnoczi 
433298800caSStefan Hajnoczi     qed_free_l2_cache(&s->l2_cache);
434298800caSStefan Hajnoczi     qemu_vfree(s->l1_table);
43575411d23SStefan Hajnoczi }
43675411d23SStefan Hajnoczi 
43775411d23SStefan Hajnoczi static int bdrv_qed_flush(BlockDriverState *bs)
43875411d23SStefan Hajnoczi {
43975411d23SStefan Hajnoczi     return bdrv_flush(bs->file);
44075411d23SStefan Hajnoczi }
44175411d23SStefan Hajnoczi 
44275411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size,
44375411d23SStefan Hajnoczi                       uint64_t image_size, uint32_t table_size,
44475411d23SStefan Hajnoczi                       const char *backing_file, const char *backing_fmt)
44575411d23SStefan Hajnoczi {
44675411d23SStefan Hajnoczi     QEDHeader header = {
44775411d23SStefan Hajnoczi         .magic = QED_MAGIC,
44875411d23SStefan Hajnoczi         .cluster_size = cluster_size,
44975411d23SStefan Hajnoczi         .table_size = table_size,
45075411d23SStefan Hajnoczi         .header_size = 1,
45175411d23SStefan Hajnoczi         .features = 0,
45275411d23SStefan Hajnoczi         .compat_features = 0,
45375411d23SStefan Hajnoczi         .l1_table_offset = cluster_size,
45475411d23SStefan Hajnoczi         .image_size = image_size,
45575411d23SStefan Hajnoczi     };
45675411d23SStefan Hajnoczi     QEDHeader le_header;
45775411d23SStefan Hajnoczi     uint8_t *l1_table = NULL;
45875411d23SStefan Hajnoczi     size_t l1_size = header.cluster_size * header.table_size;
45975411d23SStefan Hajnoczi     int ret = 0;
46075411d23SStefan Hajnoczi     BlockDriverState *bs = NULL;
46175411d23SStefan Hajnoczi 
46275411d23SStefan Hajnoczi     ret = bdrv_create_file(filename, NULL);
46375411d23SStefan Hajnoczi     if (ret < 0) {
46475411d23SStefan Hajnoczi         return ret;
46575411d23SStefan Hajnoczi     }
46675411d23SStefan Hajnoczi 
46775411d23SStefan Hajnoczi     ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR | BDRV_O_CACHE_WB);
46875411d23SStefan Hajnoczi     if (ret < 0) {
46975411d23SStefan Hajnoczi         return ret;
47075411d23SStefan Hajnoczi     }
47175411d23SStefan Hajnoczi 
472c743849bSStefan Hajnoczi     /* File must start empty and grow, check truncate is supported */
473c743849bSStefan Hajnoczi     ret = bdrv_truncate(bs, 0);
474c743849bSStefan Hajnoczi     if (ret < 0) {
475c743849bSStefan Hajnoczi         goto out;
476c743849bSStefan Hajnoczi     }
477c743849bSStefan Hajnoczi 
47875411d23SStefan Hajnoczi     if (backing_file) {
47975411d23SStefan Hajnoczi         header.features |= QED_F_BACKING_FILE;
48075411d23SStefan Hajnoczi         header.backing_filename_offset = sizeof(le_header);
48175411d23SStefan Hajnoczi         header.backing_filename_size = strlen(backing_file);
48275411d23SStefan Hajnoczi 
48375411d23SStefan Hajnoczi         if (qed_fmt_is_raw(backing_fmt)) {
48475411d23SStefan Hajnoczi             header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
48575411d23SStefan Hajnoczi         }
48675411d23SStefan Hajnoczi     }
48775411d23SStefan Hajnoczi 
48875411d23SStefan Hajnoczi     qed_header_cpu_to_le(&header, &le_header);
48975411d23SStefan Hajnoczi     ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header));
49075411d23SStefan Hajnoczi     if (ret < 0) {
49175411d23SStefan Hajnoczi         goto out;
49275411d23SStefan Hajnoczi     }
49375411d23SStefan Hajnoczi     ret = bdrv_pwrite(bs, sizeof(le_header), backing_file,
49475411d23SStefan Hajnoczi                       header.backing_filename_size);
49575411d23SStefan Hajnoczi     if (ret < 0) {
49675411d23SStefan Hajnoczi         goto out;
49775411d23SStefan Hajnoczi     }
49875411d23SStefan Hajnoczi 
49975411d23SStefan Hajnoczi     l1_table = qemu_mallocz(l1_size);
50075411d23SStefan Hajnoczi     ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size);
50175411d23SStefan Hajnoczi     if (ret < 0) {
50275411d23SStefan Hajnoczi         goto out;
50375411d23SStefan Hajnoczi     }
50475411d23SStefan Hajnoczi 
50575411d23SStefan Hajnoczi     ret = 0; /* success */
50675411d23SStefan Hajnoczi out:
50775411d23SStefan Hajnoczi     qemu_free(l1_table);
50875411d23SStefan Hajnoczi     bdrv_delete(bs);
50975411d23SStefan Hajnoczi     return ret;
51075411d23SStefan Hajnoczi }
51175411d23SStefan Hajnoczi 
51275411d23SStefan Hajnoczi static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options)
51375411d23SStefan Hajnoczi {
51475411d23SStefan Hajnoczi     uint64_t image_size = 0;
51575411d23SStefan Hajnoczi     uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
51675411d23SStefan Hajnoczi     uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
51775411d23SStefan Hajnoczi     const char *backing_file = NULL;
51875411d23SStefan Hajnoczi     const char *backing_fmt = NULL;
51975411d23SStefan Hajnoczi 
52075411d23SStefan Hajnoczi     while (options && options->name) {
52175411d23SStefan Hajnoczi         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
52275411d23SStefan Hajnoczi             image_size = options->value.n;
52375411d23SStefan Hajnoczi         } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
52475411d23SStefan Hajnoczi             backing_file = options->value.s;
52575411d23SStefan Hajnoczi         } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
52675411d23SStefan Hajnoczi             backing_fmt = options->value.s;
52775411d23SStefan Hajnoczi         } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
52875411d23SStefan Hajnoczi             if (options->value.n) {
52975411d23SStefan Hajnoczi                 cluster_size = options->value.n;
53075411d23SStefan Hajnoczi             }
53175411d23SStefan Hajnoczi         } else if (!strcmp(options->name, BLOCK_OPT_TABLE_SIZE)) {
53275411d23SStefan Hajnoczi             if (options->value.n) {
53375411d23SStefan Hajnoczi                 table_size = options->value.n;
53475411d23SStefan Hajnoczi             }
53575411d23SStefan Hajnoczi         }
53675411d23SStefan Hajnoczi         options++;
53775411d23SStefan Hajnoczi     }
53875411d23SStefan Hajnoczi 
53975411d23SStefan Hajnoczi     if (!qed_is_cluster_size_valid(cluster_size)) {
54075411d23SStefan Hajnoczi         fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n",
54175411d23SStefan Hajnoczi                 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
54275411d23SStefan Hajnoczi         return -EINVAL;
54375411d23SStefan Hajnoczi     }
54475411d23SStefan Hajnoczi     if (!qed_is_table_size_valid(table_size)) {
54575411d23SStefan Hajnoczi         fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n",
54675411d23SStefan Hajnoczi                 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
54775411d23SStefan Hajnoczi         return -EINVAL;
54875411d23SStefan Hajnoczi     }
54975411d23SStefan Hajnoczi     if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
55075411d23SStefan Hajnoczi         fprintf(stderr, "QED image size must be a non-zero multiple of "
55175411d23SStefan Hajnoczi                         "cluster size and less than %" PRIu64 " bytes\n",
55275411d23SStefan Hajnoczi                 qed_max_image_size(cluster_size, table_size));
55375411d23SStefan Hajnoczi         return -EINVAL;
55475411d23SStefan Hajnoczi     }
55575411d23SStefan Hajnoczi 
55675411d23SStefan Hajnoczi     return qed_create(filename, cluster_size, image_size, table_size,
55775411d23SStefan Hajnoczi                       backing_file, backing_fmt);
55875411d23SStefan Hajnoczi }
55975411d23SStefan Hajnoczi 
560298800caSStefan Hajnoczi typedef struct {
561298800caSStefan Hajnoczi     int is_allocated;
562298800caSStefan Hajnoczi     int *pnum;
563298800caSStefan Hajnoczi } QEDIsAllocatedCB;
564298800caSStefan Hajnoczi 
565298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
566298800caSStefan Hajnoczi {
567298800caSStefan Hajnoczi     QEDIsAllocatedCB *cb = opaque;
568298800caSStefan Hajnoczi     *cb->pnum = len / BDRV_SECTOR_SIZE;
569298800caSStefan Hajnoczi     cb->is_allocated = ret == QED_CLUSTER_FOUND;
570298800caSStefan Hajnoczi }
571298800caSStefan Hajnoczi 
57275411d23SStefan Hajnoczi static int bdrv_qed_is_allocated(BlockDriverState *bs, int64_t sector_num,
57375411d23SStefan Hajnoczi                                   int nb_sectors, int *pnum)
57475411d23SStefan Hajnoczi {
575298800caSStefan Hajnoczi     BDRVQEDState *s = bs->opaque;
576298800caSStefan Hajnoczi     uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
577298800caSStefan Hajnoczi     size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
578298800caSStefan Hajnoczi     QEDIsAllocatedCB cb = {
579298800caSStefan Hajnoczi         .is_allocated = -1,
580298800caSStefan Hajnoczi         .pnum = pnum,
581298800caSStefan Hajnoczi     };
582298800caSStefan Hajnoczi     QEDRequest request = { .l2_table = NULL };
583298800caSStefan Hajnoczi 
584298800caSStefan Hajnoczi     async_context_push();
585298800caSStefan Hajnoczi 
586298800caSStefan Hajnoczi     qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb);
587298800caSStefan Hajnoczi 
588298800caSStefan Hajnoczi     while (cb.is_allocated == -1) {
589298800caSStefan Hajnoczi         qemu_aio_wait();
590298800caSStefan Hajnoczi     }
591298800caSStefan Hajnoczi 
592298800caSStefan Hajnoczi     async_context_pop();
593298800caSStefan Hajnoczi 
594298800caSStefan Hajnoczi     qed_unref_l2_cache_entry(request.l2_table);
595298800caSStefan Hajnoczi 
596298800caSStefan Hajnoczi     return cb.is_allocated;
59775411d23SStefan Hajnoczi }
59875411d23SStefan Hajnoczi 
59975411d23SStefan Hajnoczi static int bdrv_qed_make_empty(BlockDriverState *bs)
60075411d23SStefan Hajnoczi {
60175411d23SStefan Hajnoczi     return -ENOTSUP;
60275411d23SStefan Hajnoczi }
60375411d23SStefan Hajnoczi 
604eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
605eabba580SStefan Hajnoczi {
606eabba580SStefan Hajnoczi     return acb->common.bs->opaque;
607eabba580SStefan Hajnoczi }
608eabba580SStefan Hajnoczi 
609eabba580SStefan Hajnoczi /**
610eabba580SStefan Hajnoczi  * Read from the backing file or zero-fill if no backing file
611eabba580SStefan Hajnoczi  *
612eabba580SStefan Hajnoczi  * @s:          QED state
613eabba580SStefan Hajnoczi  * @pos:        Byte position in device
614eabba580SStefan Hajnoczi  * @qiov:       Destination I/O vector
615eabba580SStefan Hajnoczi  * @cb:         Completion function
616eabba580SStefan Hajnoczi  * @opaque:     User data for completion function
617eabba580SStefan Hajnoczi  *
618eabba580SStefan Hajnoczi  * This function reads qiov->size bytes starting at pos from the backing file.
619eabba580SStefan Hajnoczi  * If there is no backing file then zeroes are read.
620eabba580SStefan Hajnoczi  */
621eabba580SStefan Hajnoczi static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
622eabba580SStefan Hajnoczi                                   QEMUIOVector *qiov,
623eabba580SStefan Hajnoczi                                   BlockDriverCompletionFunc *cb, void *opaque)
624eabba580SStefan Hajnoczi {
625eabba580SStefan Hajnoczi     BlockDriverAIOCB *aiocb;
626eabba580SStefan Hajnoczi     uint64_t backing_length = 0;
627eabba580SStefan Hajnoczi     size_t size;
628eabba580SStefan Hajnoczi 
629eabba580SStefan Hajnoczi     /* If there is a backing file, get its length.  Treat the absence of a
630eabba580SStefan Hajnoczi      * backing file like a zero length backing file.
631eabba580SStefan Hajnoczi      */
632eabba580SStefan Hajnoczi     if (s->bs->backing_hd) {
633eabba580SStefan Hajnoczi         int64_t l = bdrv_getlength(s->bs->backing_hd);
634eabba580SStefan Hajnoczi         if (l < 0) {
635eabba580SStefan Hajnoczi             cb(opaque, l);
636eabba580SStefan Hajnoczi             return;
637eabba580SStefan Hajnoczi         }
638eabba580SStefan Hajnoczi         backing_length = l;
639eabba580SStefan Hajnoczi     }
640eabba580SStefan Hajnoczi 
641eabba580SStefan Hajnoczi     /* Zero all sectors if reading beyond the end of the backing file */
642eabba580SStefan Hajnoczi     if (pos >= backing_length ||
643eabba580SStefan Hajnoczi         pos + qiov->size > backing_length) {
644eabba580SStefan Hajnoczi         qemu_iovec_memset(qiov, 0, qiov->size);
645eabba580SStefan Hajnoczi     }
646eabba580SStefan Hajnoczi 
647eabba580SStefan Hajnoczi     /* Complete now if there are no backing file sectors to read */
648eabba580SStefan Hajnoczi     if (pos >= backing_length) {
649eabba580SStefan Hajnoczi         cb(opaque, 0);
650eabba580SStefan Hajnoczi         return;
651eabba580SStefan Hajnoczi     }
652eabba580SStefan Hajnoczi 
653eabba580SStefan Hajnoczi     /* If the read straddles the end of the backing file, shorten it */
654eabba580SStefan Hajnoczi     size = MIN((uint64_t)backing_length - pos, qiov->size);
655eabba580SStefan Hajnoczi 
656eabba580SStefan Hajnoczi     BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING);
657eabba580SStefan Hajnoczi     aiocb = bdrv_aio_readv(s->bs->backing_hd, pos / BDRV_SECTOR_SIZE,
658eabba580SStefan Hajnoczi                            qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
659eabba580SStefan Hajnoczi     if (!aiocb) {
660eabba580SStefan Hajnoczi         cb(opaque, -EIO);
661eabba580SStefan Hajnoczi     }
662eabba580SStefan Hajnoczi }
663eabba580SStefan Hajnoczi 
664eabba580SStefan Hajnoczi typedef struct {
665eabba580SStefan Hajnoczi     GenericCB gencb;
666eabba580SStefan Hajnoczi     BDRVQEDState *s;
667eabba580SStefan Hajnoczi     QEMUIOVector qiov;
668eabba580SStefan Hajnoczi     struct iovec iov;
669eabba580SStefan Hajnoczi     uint64_t offset;
670eabba580SStefan Hajnoczi } CopyFromBackingFileCB;
671eabba580SStefan Hajnoczi 
672eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_cb(void *opaque, int ret)
673eabba580SStefan Hajnoczi {
674eabba580SStefan Hajnoczi     CopyFromBackingFileCB *copy_cb = opaque;
675eabba580SStefan Hajnoczi     qemu_vfree(copy_cb->iov.iov_base);
676eabba580SStefan Hajnoczi     gencb_complete(&copy_cb->gencb, ret);
677eabba580SStefan Hajnoczi }
678eabba580SStefan Hajnoczi 
679eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_write(void *opaque, int ret)
680eabba580SStefan Hajnoczi {
681eabba580SStefan Hajnoczi     CopyFromBackingFileCB *copy_cb = opaque;
682eabba580SStefan Hajnoczi     BDRVQEDState *s = copy_cb->s;
683eabba580SStefan Hajnoczi     BlockDriverAIOCB *aiocb;
684eabba580SStefan Hajnoczi 
685eabba580SStefan Hajnoczi     if (ret) {
686eabba580SStefan Hajnoczi         qed_copy_from_backing_file_cb(copy_cb, ret);
687eabba580SStefan Hajnoczi         return;
688eabba580SStefan Hajnoczi     }
689eabba580SStefan Hajnoczi 
690eabba580SStefan Hajnoczi     BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
691eabba580SStefan Hajnoczi     aiocb = bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE,
692eabba580SStefan Hajnoczi                             &copy_cb->qiov,
693eabba580SStefan Hajnoczi                             copy_cb->qiov.size / BDRV_SECTOR_SIZE,
694eabba580SStefan Hajnoczi                             qed_copy_from_backing_file_cb, copy_cb);
695eabba580SStefan Hajnoczi     if (!aiocb) {
696eabba580SStefan Hajnoczi         qed_copy_from_backing_file_cb(copy_cb, -EIO);
697eabba580SStefan Hajnoczi     }
698eabba580SStefan Hajnoczi }
699eabba580SStefan Hajnoczi 
700eabba580SStefan Hajnoczi /**
701eabba580SStefan Hajnoczi  * Copy data from backing file into the image
702eabba580SStefan Hajnoczi  *
703eabba580SStefan Hajnoczi  * @s:          QED state
704eabba580SStefan Hajnoczi  * @pos:        Byte position in device
705eabba580SStefan Hajnoczi  * @len:        Number of bytes
706eabba580SStefan Hajnoczi  * @offset:     Byte offset in image file
707eabba580SStefan Hajnoczi  * @cb:         Completion function
708eabba580SStefan Hajnoczi  * @opaque:     User data for completion function
709eabba580SStefan Hajnoczi  */
710eabba580SStefan Hajnoczi static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
711eabba580SStefan Hajnoczi                                        uint64_t len, uint64_t offset,
712eabba580SStefan Hajnoczi                                        BlockDriverCompletionFunc *cb,
713eabba580SStefan Hajnoczi                                        void *opaque)
714eabba580SStefan Hajnoczi {
715eabba580SStefan Hajnoczi     CopyFromBackingFileCB *copy_cb;
716eabba580SStefan Hajnoczi 
717eabba580SStefan Hajnoczi     /* Skip copy entirely if there is no work to do */
718eabba580SStefan Hajnoczi     if (len == 0) {
719eabba580SStefan Hajnoczi         cb(opaque, 0);
720eabba580SStefan Hajnoczi         return;
721eabba580SStefan Hajnoczi     }
722eabba580SStefan Hajnoczi 
723eabba580SStefan Hajnoczi     copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
724eabba580SStefan Hajnoczi     copy_cb->s = s;
725eabba580SStefan Hajnoczi     copy_cb->offset = offset;
726eabba580SStefan Hajnoczi     copy_cb->iov.iov_base = qemu_blockalign(s->bs, len);
727eabba580SStefan Hajnoczi     copy_cb->iov.iov_len = len;
728eabba580SStefan Hajnoczi     qemu_iovec_init_external(&copy_cb->qiov, &copy_cb->iov, 1);
729eabba580SStefan Hajnoczi 
730eabba580SStefan Hajnoczi     qed_read_backing_file(s, pos, &copy_cb->qiov,
731eabba580SStefan Hajnoczi                           qed_copy_from_backing_file_write, copy_cb);
732eabba580SStefan Hajnoczi }
733eabba580SStefan Hajnoczi 
734eabba580SStefan Hajnoczi /**
735eabba580SStefan Hajnoczi  * Link one or more contiguous clusters into a table
736eabba580SStefan Hajnoczi  *
737eabba580SStefan Hajnoczi  * @s:              QED state
738eabba580SStefan Hajnoczi  * @table:          L2 table
739eabba580SStefan Hajnoczi  * @index:          First cluster index
740eabba580SStefan Hajnoczi  * @n:              Number of contiguous clusters
741eabba580SStefan Hajnoczi  * @cluster:        First cluster byte offset in image file
742eabba580SStefan Hajnoczi  */
743eabba580SStefan Hajnoczi static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
744eabba580SStefan Hajnoczi                                 unsigned int n, uint64_t cluster)
745eabba580SStefan Hajnoczi {
746eabba580SStefan Hajnoczi     int i;
747eabba580SStefan Hajnoczi     for (i = index; i < index + n; i++) {
748eabba580SStefan Hajnoczi         table->offsets[i] = cluster;
749eabba580SStefan Hajnoczi         cluster += s->header.cluster_size;
750eabba580SStefan Hajnoczi     }
751eabba580SStefan Hajnoczi }
752eabba580SStefan Hajnoczi 
753eabba580SStefan Hajnoczi static void qed_aio_complete_bh(void *opaque)
754eabba580SStefan Hajnoczi {
755eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
756eabba580SStefan Hajnoczi     BlockDriverCompletionFunc *cb = acb->common.cb;
757eabba580SStefan Hajnoczi     void *user_opaque = acb->common.opaque;
758eabba580SStefan Hajnoczi     int ret = acb->bh_ret;
759eabba580SStefan Hajnoczi     bool *finished = acb->finished;
760eabba580SStefan Hajnoczi 
761eabba580SStefan Hajnoczi     qemu_bh_delete(acb->bh);
762eabba580SStefan Hajnoczi     qemu_aio_release(acb);
763eabba580SStefan Hajnoczi 
764eabba580SStefan Hajnoczi     /* Invoke callback */
765eabba580SStefan Hajnoczi     cb(user_opaque, ret);
766eabba580SStefan Hajnoczi 
767eabba580SStefan Hajnoczi     /* Signal cancel completion */
768eabba580SStefan Hajnoczi     if (finished) {
769eabba580SStefan Hajnoczi         *finished = true;
770eabba580SStefan Hajnoczi     }
771eabba580SStefan Hajnoczi }
772eabba580SStefan Hajnoczi 
773eabba580SStefan Hajnoczi static void qed_aio_complete(QEDAIOCB *acb, int ret)
774eabba580SStefan Hajnoczi {
775eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
776eabba580SStefan Hajnoczi 
777eabba580SStefan Hajnoczi     trace_qed_aio_complete(s, acb, ret);
778eabba580SStefan Hajnoczi 
779eabba580SStefan Hajnoczi     /* Free resources */
780eabba580SStefan Hajnoczi     qemu_iovec_destroy(&acb->cur_qiov);
781eabba580SStefan Hajnoczi     qed_unref_l2_cache_entry(acb->request.l2_table);
782eabba580SStefan Hajnoczi 
783eabba580SStefan Hajnoczi     /* Arrange for a bh to invoke the completion function */
784eabba580SStefan Hajnoczi     acb->bh_ret = ret;
785eabba580SStefan Hajnoczi     acb->bh = qemu_bh_new(qed_aio_complete_bh, acb);
786eabba580SStefan Hajnoczi     qemu_bh_schedule(acb->bh);
787eabba580SStefan Hajnoczi 
788eabba580SStefan Hajnoczi     /* Start next allocating write request waiting behind this one.  Note that
789eabba580SStefan Hajnoczi      * requests enqueue themselves when they first hit an unallocated cluster
790eabba580SStefan Hajnoczi      * but they wait until the entire request is finished before waking up the
791eabba580SStefan Hajnoczi      * next request in the queue.  This ensures that we don't cycle through
792eabba580SStefan Hajnoczi      * requests multiple times but rather finish one at a time completely.
793eabba580SStefan Hajnoczi      */
794eabba580SStefan Hajnoczi     if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
795eabba580SStefan Hajnoczi         QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
796eabba580SStefan Hajnoczi         acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
797eabba580SStefan Hajnoczi         if (acb) {
798eabba580SStefan Hajnoczi             qed_aio_next_io(acb, 0);
799eabba580SStefan Hajnoczi         }
800eabba580SStefan Hajnoczi     }
801eabba580SStefan Hajnoczi }
802eabba580SStefan Hajnoczi 
803eabba580SStefan Hajnoczi /**
804eabba580SStefan Hajnoczi  * Commit the current L2 table to the cache
805eabba580SStefan Hajnoczi  */
806eabba580SStefan Hajnoczi static void qed_commit_l2_update(void *opaque, int ret)
807eabba580SStefan Hajnoczi {
808eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
809eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
810eabba580SStefan Hajnoczi     CachedL2Table *l2_table = acb->request.l2_table;
811eabba580SStefan Hajnoczi 
812eabba580SStefan Hajnoczi     qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
813eabba580SStefan Hajnoczi 
814eabba580SStefan Hajnoczi     /* This is guaranteed to succeed because we just committed the entry to the
815eabba580SStefan Hajnoczi      * cache.
816eabba580SStefan Hajnoczi      */
817eabba580SStefan Hajnoczi     acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache,
818eabba580SStefan Hajnoczi                                                     l2_table->offset);
819eabba580SStefan Hajnoczi     assert(acb->request.l2_table != NULL);
820eabba580SStefan Hajnoczi 
821eabba580SStefan Hajnoczi     qed_aio_next_io(opaque, ret);
822eabba580SStefan Hajnoczi }
823eabba580SStefan Hajnoczi 
824eabba580SStefan Hajnoczi /**
825eabba580SStefan Hajnoczi  * Update L1 table with new L2 table offset and write it out
826eabba580SStefan Hajnoczi  */
827eabba580SStefan Hajnoczi static void qed_aio_write_l1_update(void *opaque, int ret)
828eabba580SStefan Hajnoczi {
829eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
830eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
831eabba580SStefan Hajnoczi     int index;
832eabba580SStefan Hajnoczi 
833eabba580SStefan Hajnoczi     if (ret) {
834eabba580SStefan Hajnoczi         qed_aio_complete(acb, ret);
835eabba580SStefan Hajnoczi         return;
836eabba580SStefan Hajnoczi     }
837eabba580SStefan Hajnoczi 
838eabba580SStefan Hajnoczi     index = qed_l1_index(s, acb->cur_pos);
839eabba580SStefan Hajnoczi     s->l1_table->offsets[index] = acb->request.l2_table->offset;
840eabba580SStefan Hajnoczi 
841eabba580SStefan Hajnoczi     qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
842eabba580SStefan Hajnoczi }
843eabba580SStefan Hajnoczi 
844eabba580SStefan Hajnoczi /**
845eabba580SStefan Hajnoczi  * Update L2 table with new cluster offsets and write them out
846eabba580SStefan Hajnoczi  */
847eabba580SStefan Hajnoczi static void qed_aio_write_l2_update(void *opaque, int ret)
848eabba580SStefan Hajnoczi {
849eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
850eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
851eabba580SStefan Hajnoczi     bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
852eabba580SStefan Hajnoczi     int index;
853eabba580SStefan Hajnoczi 
854eabba580SStefan Hajnoczi     if (ret) {
855eabba580SStefan Hajnoczi         goto err;
856eabba580SStefan Hajnoczi     }
857eabba580SStefan Hajnoczi 
858eabba580SStefan Hajnoczi     if (need_alloc) {
859eabba580SStefan Hajnoczi         qed_unref_l2_cache_entry(acb->request.l2_table);
860eabba580SStefan Hajnoczi         acb->request.l2_table = qed_new_l2_table(s);
861eabba580SStefan Hajnoczi     }
862eabba580SStefan Hajnoczi 
863eabba580SStefan Hajnoczi     index = qed_l2_index(s, acb->cur_pos);
864eabba580SStefan Hajnoczi     qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
865eabba580SStefan Hajnoczi                          acb->cur_cluster);
866eabba580SStefan Hajnoczi 
867eabba580SStefan Hajnoczi     if (need_alloc) {
868eabba580SStefan Hajnoczi         /* Write out the whole new L2 table */
869eabba580SStefan Hajnoczi         qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true,
870eabba580SStefan Hajnoczi                             qed_aio_write_l1_update, acb);
871eabba580SStefan Hajnoczi     } else {
872eabba580SStefan Hajnoczi         /* Write out only the updated part of the L2 table */
873eabba580SStefan Hajnoczi         qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false,
874eabba580SStefan Hajnoczi                             qed_aio_next_io, acb);
875eabba580SStefan Hajnoczi     }
876eabba580SStefan Hajnoczi     return;
877eabba580SStefan Hajnoczi 
878eabba580SStefan Hajnoczi err:
879eabba580SStefan Hajnoczi     qed_aio_complete(acb, ret);
880eabba580SStefan Hajnoczi }
881eabba580SStefan Hajnoczi 
882eabba580SStefan Hajnoczi /**
883eabba580SStefan Hajnoczi  * Flush new data clusters before updating the L2 table
884eabba580SStefan Hajnoczi  *
885eabba580SStefan Hajnoczi  * This flush is necessary when a backing file is in use.  A crash during an
886eabba580SStefan Hajnoczi  * allocating write could result in empty clusters in the image.  If the write
887eabba580SStefan Hajnoczi  * only touched a subregion of the cluster, then backing image sectors have
888eabba580SStefan Hajnoczi  * been lost in the untouched region.  The solution is to flush after writing a
889eabba580SStefan Hajnoczi  * new data cluster and before updating the L2 table.
890eabba580SStefan Hajnoczi  */
891eabba580SStefan Hajnoczi static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
892eabba580SStefan Hajnoczi {
893eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
894eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
895eabba580SStefan Hajnoczi 
896eabba580SStefan Hajnoczi     if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update, opaque)) {
897eabba580SStefan Hajnoczi         qed_aio_complete(acb, -EIO);
898eabba580SStefan Hajnoczi     }
899eabba580SStefan Hajnoczi }
900eabba580SStefan Hajnoczi 
901eabba580SStefan Hajnoczi /**
902eabba580SStefan Hajnoczi  * Write data to the image file
903eabba580SStefan Hajnoczi  */
904eabba580SStefan Hajnoczi static void qed_aio_write_main(void *opaque, int ret)
905eabba580SStefan Hajnoczi {
906eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
907eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
908eabba580SStefan Hajnoczi     uint64_t offset = acb->cur_cluster +
909eabba580SStefan Hajnoczi                       qed_offset_into_cluster(s, acb->cur_pos);
910eabba580SStefan Hajnoczi     BlockDriverCompletionFunc *next_fn;
911eabba580SStefan Hajnoczi     BlockDriverAIOCB *file_acb;
912eabba580SStefan Hajnoczi 
913eabba580SStefan Hajnoczi     trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
914eabba580SStefan Hajnoczi 
915eabba580SStefan Hajnoczi     if (ret) {
916eabba580SStefan Hajnoczi         qed_aio_complete(acb, ret);
917eabba580SStefan Hajnoczi         return;
918eabba580SStefan Hajnoczi     }
919eabba580SStefan Hajnoczi 
920eabba580SStefan Hajnoczi     if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
921eabba580SStefan Hajnoczi         next_fn = qed_aio_next_io;
922eabba580SStefan Hajnoczi     } else {
923eabba580SStefan Hajnoczi         if (s->bs->backing_hd) {
924eabba580SStefan Hajnoczi             next_fn = qed_aio_write_flush_before_l2_update;
925eabba580SStefan Hajnoczi         } else {
926eabba580SStefan Hajnoczi             next_fn = qed_aio_write_l2_update;
927eabba580SStefan Hajnoczi         }
928eabba580SStefan Hajnoczi     }
929eabba580SStefan Hajnoczi 
930eabba580SStefan Hajnoczi     BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
931eabba580SStefan Hajnoczi     file_acb = bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE,
932eabba580SStefan Hajnoczi                                &acb->cur_qiov,
933eabba580SStefan Hajnoczi                                acb->cur_qiov.size / BDRV_SECTOR_SIZE,
934eabba580SStefan Hajnoczi                                next_fn, acb);
935eabba580SStefan Hajnoczi     if (!file_acb) {
936eabba580SStefan Hajnoczi         qed_aio_complete(acb, -EIO);
937eabba580SStefan Hajnoczi     }
938eabba580SStefan Hajnoczi }
939eabba580SStefan Hajnoczi 
940eabba580SStefan Hajnoczi /**
941eabba580SStefan Hajnoczi  * Populate back untouched region of new data cluster
942eabba580SStefan Hajnoczi  */
943eabba580SStefan Hajnoczi static void qed_aio_write_postfill(void *opaque, int ret)
944eabba580SStefan Hajnoczi {
945eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
946eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
947eabba580SStefan Hajnoczi     uint64_t start = acb->cur_pos + acb->cur_qiov.size;
948eabba580SStefan Hajnoczi     uint64_t len =
949eabba580SStefan Hajnoczi         qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
950eabba580SStefan Hajnoczi     uint64_t offset = acb->cur_cluster +
951eabba580SStefan Hajnoczi                       qed_offset_into_cluster(s, acb->cur_pos) +
952eabba580SStefan Hajnoczi                       acb->cur_qiov.size;
953eabba580SStefan Hajnoczi 
954eabba580SStefan Hajnoczi     if (ret) {
955eabba580SStefan Hajnoczi         qed_aio_complete(acb, ret);
956eabba580SStefan Hajnoczi         return;
957eabba580SStefan Hajnoczi     }
958eabba580SStefan Hajnoczi 
959eabba580SStefan Hajnoczi     trace_qed_aio_write_postfill(s, acb, start, len, offset);
960eabba580SStefan Hajnoczi     qed_copy_from_backing_file(s, start, len, offset,
961eabba580SStefan Hajnoczi                                 qed_aio_write_main, acb);
962eabba580SStefan Hajnoczi }
963eabba580SStefan Hajnoczi 
964eabba580SStefan Hajnoczi /**
965eabba580SStefan Hajnoczi  * Populate front untouched region of new data cluster
966eabba580SStefan Hajnoczi  */
967eabba580SStefan Hajnoczi static void qed_aio_write_prefill(void *opaque, int ret)
968eabba580SStefan Hajnoczi {
969eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
970eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
971eabba580SStefan Hajnoczi     uint64_t start = qed_start_of_cluster(s, acb->cur_pos);
972eabba580SStefan Hajnoczi     uint64_t len = qed_offset_into_cluster(s, acb->cur_pos);
973eabba580SStefan Hajnoczi 
974eabba580SStefan Hajnoczi     trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
975eabba580SStefan Hajnoczi     qed_copy_from_backing_file(s, start, len, acb->cur_cluster,
976eabba580SStefan Hajnoczi                                 qed_aio_write_postfill, acb);
977eabba580SStefan Hajnoczi }
978eabba580SStefan Hajnoczi 
979eabba580SStefan Hajnoczi /**
980*0d09c797SStefan Hajnoczi  * Check if the QED_F_NEED_CHECK bit should be set during allocating write
981*0d09c797SStefan Hajnoczi  */
982*0d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s)
983*0d09c797SStefan Hajnoczi {
984*0d09c797SStefan Hajnoczi     /* The flush before L2 update path ensures consistency */
985*0d09c797SStefan Hajnoczi     if (s->bs->backing_hd) {
986*0d09c797SStefan Hajnoczi         return false;
987*0d09c797SStefan Hajnoczi     }
988*0d09c797SStefan Hajnoczi 
989*0d09c797SStefan Hajnoczi     return !(s->header.features & QED_F_NEED_CHECK);
990*0d09c797SStefan Hajnoczi }
991*0d09c797SStefan Hajnoczi 
992*0d09c797SStefan Hajnoczi /**
993eabba580SStefan Hajnoczi  * Write new data cluster
994eabba580SStefan Hajnoczi  *
995eabba580SStefan Hajnoczi  * @acb:        Write request
996eabba580SStefan Hajnoczi  * @len:        Length in bytes
997eabba580SStefan Hajnoczi  *
998eabba580SStefan Hajnoczi  * This path is taken when writing to previously unallocated clusters.
999eabba580SStefan Hajnoczi  */
1000eabba580SStefan Hajnoczi static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1001eabba580SStefan Hajnoczi {
1002eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
1003eabba580SStefan Hajnoczi 
1004eabba580SStefan Hajnoczi     /* Freeze this request if another allocating write is in progress */
1005eabba580SStefan Hajnoczi     if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
1006eabba580SStefan Hajnoczi         QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
1007eabba580SStefan Hajnoczi     }
1008eabba580SStefan Hajnoczi     if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
1009eabba580SStefan Hajnoczi         return; /* wait for existing request to finish */
1010eabba580SStefan Hajnoczi     }
1011eabba580SStefan Hajnoczi 
1012eabba580SStefan Hajnoczi     acb->cur_nclusters = qed_bytes_to_clusters(s,
1013eabba580SStefan Hajnoczi             qed_offset_into_cluster(s, acb->cur_pos) + len);
1014eabba580SStefan Hajnoczi     acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1015eabba580SStefan Hajnoczi     qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1016eabba580SStefan Hajnoczi 
1017*0d09c797SStefan Hajnoczi     if (qed_should_set_need_check(s)) {
101801979a98SStefan Hajnoczi         s->header.features |= QED_F_NEED_CHECK;
101901979a98SStefan Hajnoczi         qed_write_header(s, qed_aio_write_prefill, acb);
1020*0d09c797SStefan Hajnoczi     } else {
1021*0d09c797SStefan Hajnoczi         qed_aio_write_prefill(acb, 0);
1022*0d09c797SStefan Hajnoczi     }
1023eabba580SStefan Hajnoczi }
1024eabba580SStefan Hajnoczi 
1025eabba580SStefan Hajnoczi /**
1026eabba580SStefan Hajnoczi  * Write data cluster in place
1027eabba580SStefan Hajnoczi  *
1028eabba580SStefan Hajnoczi  * @acb:        Write request
1029eabba580SStefan Hajnoczi  * @offset:     Cluster offset in bytes
1030eabba580SStefan Hajnoczi  * @len:        Length in bytes
1031eabba580SStefan Hajnoczi  *
1032eabba580SStefan Hajnoczi  * This path is taken when writing to already allocated clusters.
1033eabba580SStefan Hajnoczi  */
1034eabba580SStefan Hajnoczi static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1035eabba580SStefan Hajnoczi {
1036eabba580SStefan Hajnoczi     /* Calculate the I/O vector */
1037eabba580SStefan Hajnoczi     acb->cur_cluster = offset;
1038eabba580SStefan Hajnoczi     qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1039eabba580SStefan Hajnoczi 
1040eabba580SStefan Hajnoczi     /* Do the actual write */
1041eabba580SStefan Hajnoczi     qed_aio_write_main(acb, 0);
1042eabba580SStefan Hajnoczi }
1043eabba580SStefan Hajnoczi 
1044eabba580SStefan Hajnoczi /**
1045eabba580SStefan Hajnoczi  * Write data cluster
1046eabba580SStefan Hajnoczi  *
1047eabba580SStefan Hajnoczi  * @opaque:     Write request
1048eabba580SStefan Hajnoczi  * @ret:        QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1049eabba580SStefan Hajnoczi  *              or -errno
1050eabba580SStefan Hajnoczi  * @offset:     Cluster offset in bytes
1051eabba580SStefan Hajnoczi  * @len:        Length in bytes
1052eabba580SStefan Hajnoczi  *
1053eabba580SStefan Hajnoczi  * Callback from qed_find_cluster().
1054eabba580SStefan Hajnoczi  */
1055eabba580SStefan Hajnoczi static void qed_aio_write_data(void *opaque, int ret,
1056eabba580SStefan Hajnoczi                                uint64_t offset, size_t len)
1057eabba580SStefan Hajnoczi {
1058eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
1059eabba580SStefan Hajnoczi 
1060eabba580SStefan Hajnoczi     trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1061eabba580SStefan Hajnoczi 
1062eabba580SStefan Hajnoczi     acb->find_cluster_ret = ret;
1063eabba580SStefan Hajnoczi 
1064eabba580SStefan Hajnoczi     switch (ret) {
1065eabba580SStefan Hajnoczi     case QED_CLUSTER_FOUND:
1066eabba580SStefan Hajnoczi         qed_aio_write_inplace(acb, offset, len);
1067eabba580SStefan Hajnoczi         break;
1068eabba580SStefan Hajnoczi 
1069eabba580SStefan Hajnoczi     case QED_CLUSTER_L2:
1070eabba580SStefan Hajnoczi     case QED_CLUSTER_L1:
1071eabba580SStefan Hajnoczi         qed_aio_write_alloc(acb, len);
1072eabba580SStefan Hajnoczi         break;
1073eabba580SStefan Hajnoczi 
1074eabba580SStefan Hajnoczi     default:
1075eabba580SStefan Hajnoczi         qed_aio_complete(acb, ret);
1076eabba580SStefan Hajnoczi         break;
1077eabba580SStefan Hajnoczi     }
1078eabba580SStefan Hajnoczi }
1079eabba580SStefan Hajnoczi 
1080eabba580SStefan Hajnoczi /**
1081eabba580SStefan Hajnoczi  * Read data cluster
1082eabba580SStefan Hajnoczi  *
1083eabba580SStefan Hajnoczi  * @opaque:     Read request
1084eabba580SStefan Hajnoczi  * @ret:        QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
1085eabba580SStefan Hajnoczi  *              or -errno
1086eabba580SStefan Hajnoczi  * @offset:     Cluster offset in bytes
1087eabba580SStefan Hajnoczi  * @len:        Length in bytes
1088eabba580SStefan Hajnoczi  *
1089eabba580SStefan Hajnoczi  * Callback from qed_find_cluster().
1090eabba580SStefan Hajnoczi  */
1091eabba580SStefan Hajnoczi static void qed_aio_read_data(void *opaque, int ret,
1092eabba580SStefan Hajnoczi                               uint64_t offset, size_t len)
1093eabba580SStefan Hajnoczi {
1094eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
1095eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
1096eabba580SStefan Hajnoczi     BlockDriverState *bs = acb->common.bs;
1097eabba580SStefan Hajnoczi     BlockDriverAIOCB *file_acb;
1098eabba580SStefan Hajnoczi 
1099eabba580SStefan Hajnoczi     /* Adjust offset into cluster */
1100eabba580SStefan Hajnoczi     offset += qed_offset_into_cluster(s, acb->cur_pos);
1101eabba580SStefan Hajnoczi 
1102eabba580SStefan Hajnoczi     trace_qed_aio_read_data(s, acb, ret, offset, len);
1103eabba580SStefan Hajnoczi 
1104eabba580SStefan Hajnoczi     if (ret < 0) {
1105eabba580SStefan Hajnoczi         goto err;
1106eabba580SStefan Hajnoczi     }
1107eabba580SStefan Hajnoczi 
1108eabba580SStefan Hajnoczi     qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1109eabba580SStefan Hajnoczi 
1110eabba580SStefan Hajnoczi     /* Handle backing file and unallocated sparse hole reads */
1111eabba580SStefan Hajnoczi     if (ret != QED_CLUSTER_FOUND) {
1112eabba580SStefan Hajnoczi         qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
1113eabba580SStefan Hajnoczi                               qed_aio_next_io, acb);
1114eabba580SStefan Hajnoczi         return;
1115eabba580SStefan Hajnoczi     }
1116eabba580SStefan Hajnoczi 
1117eabba580SStefan Hajnoczi     BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1118eabba580SStefan Hajnoczi     file_acb = bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE,
1119eabba580SStefan Hajnoczi                               &acb->cur_qiov,
1120eabba580SStefan Hajnoczi                               acb->cur_qiov.size / BDRV_SECTOR_SIZE,
1121eabba580SStefan Hajnoczi                               qed_aio_next_io, acb);
1122eabba580SStefan Hajnoczi     if (!file_acb) {
1123eabba580SStefan Hajnoczi         ret = -EIO;
1124eabba580SStefan Hajnoczi         goto err;
1125eabba580SStefan Hajnoczi     }
1126eabba580SStefan Hajnoczi     return;
1127eabba580SStefan Hajnoczi 
1128eabba580SStefan Hajnoczi err:
1129eabba580SStefan Hajnoczi     qed_aio_complete(acb, ret);
1130eabba580SStefan Hajnoczi }
1131eabba580SStefan Hajnoczi 
1132eabba580SStefan Hajnoczi /**
1133eabba580SStefan Hajnoczi  * Begin next I/O or complete the request
1134eabba580SStefan Hajnoczi  */
1135eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret)
1136eabba580SStefan Hajnoczi {
1137eabba580SStefan Hajnoczi     QEDAIOCB *acb = opaque;
1138eabba580SStefan Hajnoczi     BDRVQEDState *s = acb_to_s(acb);
1139eabba580SStefan Hajnoczi     QEDFindClusterFunc *io_fn =
1140eabba580SStefan Hajnoczi         acb->is_write ? qed_aio_write_data : qed_aio_read_data;
1141eabba580SStefan Hajnoczi 
1142eabba580SStefan Hajnoczi     trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
1143eabba580SStefan Hajnoczi 
1144eabba580SStefan Hajnoczi     /* Handle I/O error */
1145eabba580SStefan Hajnoczi     if (ret) {
1146eabba580SStefan Hajnoczi         qed_aio_complete(acb, ret);
1147eabba580SStefan Hajnoczi         return;
1148eabba580SStefan Hajnoczi     }
1149eabba580SStefan Hajnoczi 
1150eabba580SStefan Hajnoczi     acb->qiov_offset += acb->cur_qiov.size;
1151eabba580SStefan Hajnoczi     acb->cur_pos += acb->cur_qiov.size;
1152eabba580SStefan Hajnoczi     qemu_iovec_reset(&acb->cur_qiov);
1153eabba580SStefan Hajnoczi 
1154eabba580SStefan Hajnoczi     /* Complete request */
1155eabba580SStefan Hajnoczi     if (acb->cur_pos >= acb->end_pos) {
1156eabba580SStefan Hajnoczi         qed_aio_complete(acb, 0);
1157eabba580SStefan Hajnoczi         return;
1158eabba580SStefan Hajnoczi     }
1159eabba580SStefan Hajnoczi 
1160eabba580SStefan Hajnoczi     /* Find next cluster and start I/O */
1161eabba580SStefan Hajnoczi     qed_find_cluster(s, &acb->request,
1162eabba580SStefan Hajnoczi                       acb->cur_pos, acb->end_pos - acb->cur_pos,
1163eabba580SStefan Hajnoczi                       io_fn, acb);
1164eabba580SStefan Hajnoczi }
1165eabba580SStefan Hajnoczi 
1166eabba580SStefan Hajnoczi static BlockDriverAIOCB *qed_aio_setup(BlockDriverState *bs,
1167eabba580SStefan Hajnoczi                                        int64_t sector_num,
1168eabba580SStefan Hajnoczi                                        QEMUIOVector *qiov, int nb_sectors,
1169eabba580SStefan Hajnoczi                                        BlockDriverCompletionFunc *cb,
1170eabba580SStefan Hajnoczi                                        void *opaque, bool is_write)
1171eabba580SStefan Hajnoczi {
1172eabba580SStefan Hajnoczi     QEDAIOCB *acb = qemu_aio_get(&qed_aio_pool, bs, cb, opaque);
1173eabba580SStefan Hajnoczi 
1174eabba580SStefan Hajnoczi     trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
1175eabba580SStefan Hajnoczi                          opaque, is_write);
1176eabba580SStefan Hajnoczi 
1177eabba580SStefan Hajnoczi     acb->is_write = is_write;
1178eabba580SStefan Hajnoczi     acb->finished = NULL;
1179eabba580SStefan Hajnoczi     acb->qiov = qiov;
1180eabba580SStefan Hajnoczi     acb->qiov_offset = 0;
1181eabba580SStefan Hajnoczi     acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
1182eabba580SStefan Hajnoczi     acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
1183eabba580SStefan Hajnoczi     acb->request.l2_table = NULL;
1184eabba580SStefan Hajnoczi     qemu_iovec_init(&acb->cur_qiov, qiov->niov);
1185eabba580SStefan Hajnoczi 
1186eabba580SStefan Hajnoczi     /* Start request */
1187eabba580SStefan Hajnoczi     qed_aio_next_io(acb, 0);
1188eabba580SStefan Hajnoczi     return &acb->common;
1189eabba580SStefan Hajnoczi }
1190eabba580SStefan Hajnoczi 
119175411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
119275411d23SStefan Hajnoczi                                             int64_t sector_num,
119375411d23SStefan Hajnoczi                                             QEMUIOVector *qiov, int nb_sectors,
119475411d23SStefan Hajnoczi                                             BlockDriverCompletionFunc *cb,
119575411d23SStefan Hajnoczi                                             void *opaque)
119675411d23SStefan Hajnoczi {
1197eabba580SStefan Hajnoczi     return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, false);
119875411d23SStefan Hajnoczi }
119975411d23SStefan Hajnoczi 
120075411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
120175411d23SStefan Hajnoczi                                              int64_t sector_num,
120275411d23SStefan Hajnoczi                                              QEMUIOVector *qiov, int nb_sectors,
120375411d23SStefan Hajnoczi                                              BlockDriverCompletionFunc *cb,
120475411d23SStefan Hajnoczi                                              void *opaque)
120575411d23SStefan Hajnoczi {
1206eabba580SStefan Hajnoczi     return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, true);
120775411d23SStefan Hajnoczi }
120875411d23SStefan Hajnoczi 
120975411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_flush(BlockDriverState *bs,
121075411d23SStefan Hajnoczi                                             BlockDriverCompletionFunc *cb,
121175411d23SStefan Hajnoczi                                             void *opaque)
121275411d23SStefan Hajnoczi {
121375411d23SStefan Hajnoczi     return bdrv_aio_flush(bs->file, cb, opaque);
121475411d23SStefan Hajnoczi }
121575411d23SStefan Hajnoczi 
121675411d23SStefan Hajnoczi static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset)
121775411d23SStefan Hajnoczi {
121875411d23SStefan Hajnoczi     return -ENOTSUP;
121975411d23SStefan Hajnoczi }
122075411d23SStefan Hajnoczi 
122175411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs)
122275411d23SStefan Hajnoczi {
122375411d23SStefan Hajnoczi     BDRVQEDState *s = bs->opaque;
122475411d23SStefan Hajnoczi     return s->header.image_size;
122575411d23SStefan Hajnoczi }
122675411d23SStefan Hajnoczi 
122775411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
122875411d23SStefan Hajnoczi {
122975411d23SStefan Hajnoczi     BDRVQEDState *s = bs->opaque;
123075411d23SStefan Hajnoczi 
123175411d23SStefan Hajnoczi     memset(bdi, 0, sizeof(*bdi));
123275411d23SStefan Hajnoczi     bdi->cluster_size = s->header.cluster_size;
123375411d23SStefan Hajnoczi     return 0;
123475411d23SStefan Hajnoczi }
123575411d23SStefan Hajnoczi 
123675411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs,
123775411d23SStefan Hajnoczi                                         const char *backing_file,
123875411d23SStefan Hajnoczi                                         const char *backing_fmt)
123975411d23SStefan Hajnoczi {
124075411d23SStefan Hajnoczi     BDRVQEDState *s = bs->opaque;
124175411d23SStefan Hajnoczi     QEDHeader new_header, le_header;
124275411d23SStefan Hajnoczi     void *buffer;
124375411d23SStefan Hajnoczi     size_t buffer_len, backing_file_len;
124475411d23SStefan Hajnoczi     int ret;
124575411d23SStefan Hajnoczi 
124675411d23SStefan Hajnoczi     /* Refuse to set backing filename if unknown compat feature bits are
124775411d23SStefan Hajnoczi      * active.  If the image uses an unknown compat feature then we may not
124875411d23SStefan Hajnoczi      * know the layout of data following the header structure and cannot safely
124975411d23SStefan Hajnoczi      * add a new string.
125075411d23SStefan Hajnoczi      */
125175411d23SStefan Hajnoczi     if (backing_file && (s->header.compat_features &
125275411d23SStefan Hajnoczi                          ~QED_COMPAT_FEATURE_MASK)) {
125375411d23SStefan Hajnoczi         return -ENOTSUP;
125475411d23SStefan Hajnoczi     }
125575411d23SStefan Hajnoczi 
125675411d23SStefan Hajnoczi     memcpy(&new_header, &s->header, sizeof(new_header));
125775411d23SStefan Hajnoczi 
125875411d23SStefan Hajnoczi     new_header.features &= ~(QED_F_BACKING_FILE |
125975411d23SStefan Hajnoczi                              QED_F_BACKING_FORMAT_NO_PROBE);
126075411d23SStefan Hajnoczi 
126175411d23SStefan Hajnoczi     /* Adjust feature flags */
126275411d23SStefan Hajnoczi     if (backing_file) {
126375411d23SStefan Hajnoczi         new_header.features |= QED_F_BACKING_FILE;
126475411d23SStefan Hajnoczi 
126575411d23SStefan Hajnoczi         if (qed_fmt_is_raw(backing_fmt)) {
126675411d23SStefan Hajnoczi             new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
126775411d23SStefan Hajnoczi         }
126875411d23SStefan Hajnoczi     }
126975411d23SStefan Hajnoczi 
127075411d23SStefan Hajnoczi     /* Calculate new header size */
127175411d23SStefan Hajnoczi     backing_file_len = 0;
127275411d23SStefan Hajnoczi 
127375411d23SStefan Hajnoczi     if (backing_file) {
127475411d23SStefan Hajnoczi         backing_file_len = strlen(backing_file);
127575411d23SStefan Hajnoczi     }
127675411d23SStefan Hajnoczi 
127775411d23SStefan Hajnoczi     buffer_len = sizeof(new_header);
127875411d23SStefan Hajnoczi     new_header.backing_filename_offset = buffer_len;
127975411d23SStefan Hajnoczi     new_header.backing_filename_size = backing_file_len;
128075411d23SStefan Hajnoczi     buffer_len += backing_file_len;
128175411d23SStefan Hajnoczi 
128275411d23SStefan Hajnoczi     /* Make sure we can rewrite header without failing */
128375411d23SStefan Hajnoczi     if (buffer_len > new_header.header_size * new_header.cluster_size) {
128475411d23SStefan Hajnoczi         return -ENOSPC;
128575411d23SStefan Hajnoczi     }
128675411d23SStefan Hajnoczi 
128775411d23SStefan Hajnoczi     /* Prepare new header */
128875411d23SStefan Hajnoczi     buffer = qemu_malloc(buffer_len);
128975411d23SStefan Hajnoczi 
129075411d23SStefan Hajnoczi     qed_header_cpu_to_le(&new_header, &le_header);
129175411d23SStefan Hajnoczi     memcpy(buffer, &le_header, sizeof(le_header));
129275411d23SStefan Hajnoczi     buffer_len = sizeof(le_header);
129375411d23SStefan Hajnoczi 
129475411d23SStefan Hajnoczi     memcpy(buffer + buffer_len, backing_file, backing_file_len);
129575411d23SStefan Hajnoczi     buffer_len += backing_file_len;
129675411d23SStefan Hajnoczi 
129775411d23SStefan Hajnoczi     /* Write new header */
129875411d23SStefan Hajnoczi     ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len);
129975411d23SStefan Hajnoczi     qemu_free(buffer);
130075411d23SStefan Hajnoczi     if (ret == 0) {
130175411d23SStefan Hajnoczi         memcpy(&s->header, &new_header, sizeof(new_header));
130275411d23SStefan Hajnoczi     }
130375411d23SStefan Hajnoczi     return ret;
130475411d23SStefan Hajnoczi }
130575411d23SStefan Hajnoczi 
130675411d23SStefan Hajnoczi static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result)
130775411d23SStefan Hajnoczi {
130801979a98SStefan Hajnoczi     BDRVQEDState *s = bs->opaque;
130901979a98SStefan Hajnoczi 
131001979a98SStefan Hajnoczi     return qed_check(s, result, false);
131175411d23SStefan Hajnoczi }
131275411d23SStefan Hajnoczi 
131375411d23SStefan Hajnoczi static QEMUOptionParameter qed_create_options[] = {
131475411d23SStefan Hajnoczi     {
131575411d23SStefan Hajnoczi         .name = BLOCK_OPT_SIZE,
131675411d23SStefan Hajnoczi         .type = OPT_SIZE,
131775411d23SStefan Hajnoczi         .help = "Virtual disk size (in bytes)"
131875411d23SStefan Hajnoczi     }, {
131975411d23SStefan Hajnoczi         .name = BLOCK_OPT_BACKING_FILE,
132075411d23SStefan Hajnoczi         .type = OPT_STRING,
132175411d23SStefan Hajnoczi         .help = "File name of a base image"
132275411d23SStefan Hajnoczi     }, {
132375411d23SStefan Hajnoczi         .name = BLOCK_OPT_BACKING_FMT,
132475411d23SStefan Hajnoczi         .type = OPT_STRING,
132575411d23SStefan Hajnoczi         .help = "Image format of the base image"
132675411d23SStefan Hajnoczi     }, {
132775411d23SStefan Hajnoczi         .name = BLOCK_OPT_CLUSTER_SIZE,
132875411d23SStefan Hajnoczi         .type = OPT_SIZE,
132975411d23SStefan Hajnoczi         .help = "Cluster size (in bytes)"
133075411d23SStefan Hajnoczi     }, {
133175411d23SStefan Hajnoczi         .name = BLOCK_OPT_TABLE_SIZE,
133275411d23SStefan Hajnoczi         .type = OPT_SIZE,
133375411d23SStefan Hajnoczi         .help = "L1/L2 table size (in clusters)"
133475411d23SStefan Hajnoczi     },
133575411d23SStefan Hajnoczi     { /* end of list */ }
133675411d23SStefan Hajnoczi };
133775411d23SStefan Hajnoczi 
133875411d23SStefan Hajnoczi static BlockDriver bdrv_qed = {
133975411d23SStefan Hajnoczi     .format_name              = "qed",
134075411d23SStefan Hajnoczi     .instance_size            = sizeof(BDRVQEDState),
134175411d23SStefan Hajnoczi     .create_options           = qed_create_options,
134275411d23SStefan Hajnoczi 
134375411d23SStefan Hajnoczi     .bdrv_probe               = bdrv_qed_probe,
134475411d23SStefan Hajnoczi     .bdrv_open                = bdrv_qed_open,
134575411d23SStefan Hajnoczi     .bdrv_close               = bdrv_qed_close,
134675411d23SStefan Hajnoczi     .bdrv_create              = bdrv_qed_create,
134775411d23SStefan Hajnoczi     .bdrv_flush               = bdrv_qed_flush,
134875411d23SStefan Hajnoczi     .bdrv_is_allocated        = bdrv_qed_is_allocated,
134975411d23SStefan Hajnoczi     .bdrv_make_empty          = bdrv_qed_make_empty,
135075411d23SStefan Hajnoczi     .bdrv_aio_readv           = bdrv_qed_aio_readv,
135175411d23SStefan Hajnoczi     .bdrv_aio_writev          = bdrv_qed_aio_writev,
135275411d23SStefan Hajnoczi     .bdrv_aio_flush           = bdrv_qed_aio_flush,
135375411d23SStefan Hajnoczi     .bdrv_truncate            = bdrv_qed_truncate,
135475411d23SStefan Hajnoczi     .bdrv_getlength           = bdrv_qed_getlength,
135575411d23SStefan Hajnoczi     .bdrv_get_info            = bdrv_qed_get_info,
135675411d23SStefan Hajnoczi     .bdrv_change_backing_file = bdrv_qed_change_backing_file,
135775411d23SStefan Hajnoczi     .bdrv_check               = bdrv_qed_check,
135875411d23SStefan Hajnoczi };
135975411d23SStefan Hajnoczi 
136075411d23SStefan Hajnoczi static void bdrv_qed_init(void)
136175411d23SStefan Hajnoczi {
136275411d23SStefan Hajnoczi     bdrv_register(&bdrv_qed);
136375411d23SStefan Hajnoczi }
136475411d23SStefan Hajnoczi 
136575411d23SStefan Hajnoczi block_init(bdrv_qed_init);
1366