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
1580c71a24SPeter Maydell #include "qemu/osdep.h"
16609f45eaSMax Reitz #include "block/qdict.h"
17da34e65cSMarkus Armbruster #include "qapi/error.h"
181de7afc9SPaolo Bonzini #include "qemu/timer.h"
1958369e22SPaolo Bonzini #include "qemu/bswap.h"
20db725815SMarkus Armbruster #include "qemu/main-loop.h"
210b8fa32fSMarkus Armbruster #include "qemu/module.h"
22922a01a0SMarkus Armbruster #include "qemu/option.h"
235df022cfSPeter Maydell #include "qemu/memalign.h"
24eabba580SStefan Hajnoczi #include "trace.h"
2575411d23SStefan Hajnoczi #include "qed.h"
268a56fdadSKevin Wolf #include "sysemu/block-backend.h"
27959355a4SKevin Wolf #include "qapi/qmp/qdict.h"
28959355a4SKevin Wolf #include "qapi/qobject-input-visitor.h"
29959355a4SKevin Wolf #include "qapi/qapi-visit-block-core.h"
30959355a4SKevin Wolf
31959355a4SKevin Wolf static QemuOptsList qed_create_opts;
3275411d23SStefan Hajnoczi
bdrv_qed_probe(const uint8_t * buf,int buf_size,const char * filename)3375411d23SStefan Hajnoczi static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
3475411d23SStefan Hajnoczi const char *filename)
3575411d23SStefan Hajnoczi {
3675411d23SStefan Hajnoczi const QEDHeader *header = (const QEDHeader *)buf;
3775411d23SStefan Hajnoczi
3875411d23SStefan Hajnoczi if (buf_size < sizeof(*header)) {
3975411d23SStefan Hajnoczi return 0;
4075411d23SStefan Hajnoczi }
4175411d23SStefan Hajnoczi if (le32_to_cpu(header->magic) != QED_MAGIC) {
4275411d23SStefan Hajnoczi return 0;
4375411d23SStefan Hajnoczi }
4475411d23SStefan Hajnoczi return 100;
4575411d23SStefan Hajnoczi }
4675411d23SStefan Hajnoczi
4775411d23SStefan Hajnoczi /**
4875411d23SStefan Hajnoczi * Check whether an image format is raw
4975411d23SStefan Hajnoczi *
5075411d23SStefan Hajnoczi * @fmt: Backing file format, may be NULL
5175411d23SStefan Hajnoczi */
qed_fmt_is_raw(const char * fmt)5275411d23SStefan Hajnoczi static bool qed_fmt_is_raw(const char *fmt)
5375411d23SStefan Hajnoczi {
5475411d23SStefan Hajnoczi return fmt && strcmp(fmt, "raw") == 0;
5575411d23SStefan Hajnoczi }
5675411d23SStefan Hajnoczi
qed_header_le_to_cpu(const QEDHeader * le,QEDHeader * cpu)5775411d23SStefan Hajnoczi static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
5875411d23SStefan Hajnoczi {
5975411d23SStefan Hajnoczi cpu->magic = le32_to_cpu(le->magic);
6075411d23SStefan Hajnoczi cpu->cluster_size = le32_to_cpu(le->cluster_size);
6175411d23SStefan Hajnoczi cpu->table_size = le32_to_cpu(le->table_size);
6275411d23SStefan Hajnoczi cpu->header_size = le32_to_cpu(le->header_size);
6375411d23SStefan Hajnoczi cpu->features = le64_to_cpu(le->features);
6475411d23SStefan Hajnoczi cpu->compat_features = le64_to_cpu(le->compat_features);
6575411d23SStefan Hajnoczi cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
6675411d23SStefan Hajnoczi cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
6775411d23SStefan Hajnoczi cpu->image_size = le64_to_cpu(le->image_size);
6875411d23SStefan Hajnoczi cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
6975411d23SStefan Hajnoczi cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
7075411d23SStefan Hajnoczi }
7175411d23SStefan Hajnoczi
qed_header_cpu_to_le(const QEDHeader * cpu,QEDHeader * le)7275411d23SStefan Hajnoczi static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
7375411d23SStefan Hajnoczi {
7475411d23SStefan Hajnoczi le->magic = cpu_to_le32(cpu->magic);
7575411d23SStefan Hajnoczi le->cluster_size = cpu_to_le32(cpu->cluster_size);
7675411d23SStefan Hajnoczi le->table_size = cpu_to_le32(cpu->table_size);
7775411d23SStefan Hajnoczi le->header_size = cpu_to_le32(cpu->header_size);
7875411d23SStefan Hajnoczi le->features = cpu_to_le64(cpu->features);
7975411d23SStefan Hajnoczi le->compat_features = cpu_to_le64(cpu->compat_features);
8075411d23SStefan Hajnoczi le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
8175411d23SStefan Hajnoczi le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
8275411d23SStefan Hajnoczi le->image_size = cpu_to_le64(cpu->image_size);
8375411d23SStefan Hajnoczi le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
8475411d23SStefan Hajnoczi le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
8575411d23SStefan Hajnoczi }
8675411d23SStefan Hajnoczi
qed_write_header_sync(BDRVQEDState * s)87b10170acSStefan Hajnoczi int qed_write_header_sync(BDRVQEDState *s)
8875411d23SStefan Hajnoczi {
8975411d23SStefan Hajnoczi QEDHeader le;
9075411d23SStefan Hajnoczi
9175411d23SStefan Hajnoczi qed_header_cpu_to_le(&s->header, &le);
92353a5d84SAlberto Faria return bdrv_pwrite(s->bs->file, 0, sizeof(le), &le, 0);
9375411d23SStefan Hajnoczi }
9475411d23SStefan Hajnoczi
9501979a98SStefan Hajnoczi /**
9601979a98SStefan Hajnoczi * Update header in-place (does not rewrite backing filename or other strings)
9701979a98SStefan Hajnoczi *
9801979a98SStefan Hajnoczi * This function only updates known header fields in-place and does not affect
9901979a98SStefan Hajnoczi * extra data after the QED header.
1001f01e50bSPaolo Bonzini *
1011f01e50bSPaolo Bonzini * No new allocating reqs can start while this function runs.
10201979a98SStefan Hajnoczi */
qed_write_header(BDRVQEDState * s)103b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK qed_write_header(BDRVQEDState *s)
10401979a98SStefan Hajnoczi {
10501979a98SStefan Hajnoczi /* We must write full sectors for O_DIRECT but cannot necessarily generate
10601979a98SStefan Hajnoczi * the data following the header if an unrecognized compat feature is
10701979a98SStefan Hajnoczi * active. Therefore, first read the sectors containing the header, update
10801979a98SStefan Hajnoczi * them, and write back.
10901979a98SStefan Hajnoczi */
11001979a98SStefan Hajnoczi
111c41a73ffSLaurent Vivier int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE);
11201979a98SStefan Hajnoczi size_t len = nsectors * BDRV_SECTOR_SIZE;
1137076309aSKevin Wolf uint8_t *buf;
1147076309aSKevin Wolf int ret;
11501979a98SStefan Hajnoczi
1161f01e50bSPaolo Bonzini assert(s->allocating_acb || s->allocating_write_reqs_plugged);
1171f01e50bSPaolo Bonzini
1187076309aSKevin Wolf buf = qemu_blockalign(s->bs, len);
11901979a98SStefan Hajnoczi
120696e8cb2SVladimir Sementsov-Ogievskiy ret = bdrv_co_pread(s->bs->file, 0, len, buf, 0);
1217076309aSKevin Wolf if (ret < 0) {
1227076309aSKevin Wolf goto out;
1237076309aSKevin Wolf }
1247076309aSKevin Wolf
1257076309aSKevin Wolf /* Update header */
1267076309aSKevin Wolf qed_header_cpu_to_le(&s->header, (QEDHeader *) buf);
1277076309aSKevin Wolf
128696e8cb2SVladimir Sementsov-Ogievskiy ret = bdrv_co_pwrite(s->bs->file, 0, len, buf, 0);
1297076309aSKevin Wolf if (ret < 0) {
1307076309aSKevin Wolf goto out;
1317076309aSKevin Wolf }
1327076309aSKevin Wolf
1337076309aSKevin Wolf ret = 0;
1347076309aSKevin Wolf out:
1357076309aSKevin Wolf qemu_vfree(buf);
136f13d712bSKevin Wolf return ret;
13701979a98SStefan Hajnoczi }
13801979a98SStefan Hajnoczi
qed_max_image_size(uint32_t cluster_size,uint32_t table_size)13975411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
14075411d23SStefan Hajnoczi {
14175411d23SStefan Hajnoczi uint64_t table_entries;
14275411d23SStefan Hajnoczi uint64_t l2_size;
14375411d23SStefan Hajnoczi
14475411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t);
14575411d23SStefan Hajnoczi l2_size = table_entries * cluster_size;
14675411d23SStefan Hajnoczi
14775411d23SStefan Hajnoczi return l2_size * table_entries;
14875411d23SStefan Hajnoczi }
14975411d23SStefan Hajnoczi
qed_is_cluster_size_valid(uint32_t cluster_size)15075411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size)
15175411d23SStefan Hajnoczi {
15275411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE ||
15375411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) {
15475411d23SStefan Hajnoczi return false;
15575411d23SStefan Hajnoczi }
15675411d23SStefan Hajnoczi if (cluster_size & (cluster_size - 1)) {
15775411d23SStefan Hajnoczi return false; /* not power of 2 */
15875411d23SStefan Hajnoczi }
15975411d23SStefan Hajnoczi return true;
16075411d23SStefan Hajnoczi }
16175411d23SStefan Hajnoczi
qed_is_table_size_valid(uint32_t table_size)16275411d23SStefan Hajnoczi static bool qed_is_table_size_valid(uint32_t table_size)
16375411d23SStefan Hajnoczi {
16475411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE ||
16575411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) {
16675411d23SStefan Hajnoczi return false;
16775411d23SStefan Hajnoczi }
16875411d23SStefan Hajnoczi if (table_size & (table_size - 1)) {
16975411d23SStefan Hajnoczi return false; /* not power of 2 */
17075411d23SStefan Hajnoczi }
17175411d23SStefan Hajnoczi return true;
17275411d23SStefan Hajnoczi }
17375411d23SStefan Hajnoczi
qed_is_image_size_valid(uint64_t image_size,uint32_t cluster_size,uint32_t table_size)17475411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
17575411d23SStefan Hajnoczi uint32_t table_size)
17675411d23SStefan Hajnoczi {
17775411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) {
17875411d23SStefan Hajnoczi return false; /* not multiple of sector size */
17975411d23SStefan Hajnoczi }
18075411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) {
18175411d23SStefan Hajnoczi return false; /* image is too large */
18275411d23SStefan Hajnoczi }
18375411d23SStefan Hajnoczi return true;
18475411d23SStefan Hajnoczi }
18575411d23SStefan Hajnoczi
18675411d23SStefan Hajnoczi /**
18775411d23SStefan Hajnoczi * Read a string of known length from the image file
18875411d23SStefan Hajnoczi *
18975411d23SStefan Hajnoczi * @file: Image file
19075411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes
19175411d23SStefan Hajnoczi * @n: String length in bytes
19275411d23SStefan Hajnoczi * @buf: Destination buffer
19375411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes
19475411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure
19575411d23SStefan Hajnoczi *
19675411d23SStefan Hajnoczi * The string is NUL-terminated.
19775411d23SStefan Hajnoczi */
198bba667daSPaolo Bonzini static int coroutine_fn GRAPH_RDLOCK
qed_read_string(BdrvChild * file,uint64_t offset,size_t n,char * buf,size_t buflen)199bba667daSPaolo Bonzini qed_read_string(BdrvChild *file, uint64_t offset,
200bba667daSPaolo Bonzini size_t n, char *buf, size_t buflen)
20175411d23SStefan Hajnoczi {
20275411d23SStefan Hajnoczi int ret;
20375411d23SStefan Hajnoczi if (n >= buflen) {
20475411d23SStefan Hajnoczi return -EINVAL;
20575411d23SStefan Hajnoczi }
206bba667daSPaolo Bonzini ret = bdrv_co_pread(file, offset, n, buf, 0);
20775411d23SStefan Hajnoczi if (ret < 0) {
20875411d23SStefan Hajnoczi return ret;
20975411d23SStefan Hajnoczi }
21075411d23SStefan Hajnoczi buf[n] = '\0';
21175411d23SStefan Hajnoczi return 0;
21275411d23SStefan Hajnoczi }
21375411d23SStefan Hajnoczi
214eabba580SStefan Hajnoczi /**
215eabba580SStefan Hajnoczi * Allocate new clusters
216eabba580SStefan Hajnoczi *
217eabba580SStefan Hajnoczi * @s: QED state
218eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate
219eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster
220eabba580SStefan Hajnoczi *
221eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be
222eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image
223eabba580SStefan Hajnoczi * file.
2241f01e50bSPaolo Bonzini *
2251f01e50bSPaolo Bonzini * Called with table_lock held.
226eabba580SStefan Hajnoczi */
qed_alloc_clusters(BDRVQEDState * s,unsigned int n)227eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
228eabba580SStefan Hajnoczi {
229eabba580SStefan Hajnoczi uint64_t offset = s->file_size;
230eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size;
231eabba580SStefan Hajnoczi return offset;
232eabba580SStefan Hajnoczi }
233eabba580SStefan Hajnoczi
qed_alloc_table(BDRVQEDState * s)234298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s)
235298800caSStefan Hajnoczi {
236298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */
237298800caSStefan Hajnoczi return qemu_blockalign(s->bs,
238298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size);
239298800caSStefan Hajnoczi }
240298800caSStefan Hajnoczi
241eabba580SStefan Hajnoczi /**
242eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table
2431f01e50bSPaolo Bonzini *
2441f01e50bSPaolo Bonzini * Called with table_lock held.
245eabba580SStefan Hajnoczi */
qed_new_l2_table(BDRVQEDState * s)246eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
247eabba580SStefan Hajnoczi {
248eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
249eabba580SStefan Hajnoczi
250eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s);
251eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
252eabba580SStefan Hajnoczi
253eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0,
254eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size);
255eabba580SStefan Hajnoczi return l2_table;
256eabba580SStefan Hajnoczi }
257eabba580SStefan Hajnoczi
qed_plug_allocating_write_reqs(BDRVQEDState * s)258b383ae60SPaolo Bonzini static bool coroutine_fn qed_plug_allocating_write_reqs(BDRVQEDState *s)
2596f321e93SStefan Hajnoczi {
2601f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock);
2611f01e50bSPaolo Bonzini
2621f01e50bSPaolo Bonzini /* No reentrancy is allowed. */
2636f321e93SStefan Hajnoczi assert(!s->allocating_write_reqs_plugged);
2641f01e50bSPaolo Bonzini if (s->allocating_acb != NULL) {
2651f01e50bSPaolo Bonzini /* Another allocating write came concurrently. This cannot happen
2665e8ac217SKevin Wolf * from bdrv_qed_drain_begin, but it can happen when the timer runs.
2671f01e50bSPaolo Bonzini */
2681f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
2691f01e50bSPaolo Bonzini return false;
2701f01e50bSPaolo Bonzini }
2716f321e93SStefan Hajnoczi
2726f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = true;
2731f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
2741f01e50bSPaolo Bonzini return true;
2756f321e93SStefan Hajnoczi }
2766f321e93SStefan Hajnoczi
qed_unplug_allocating_write_reqs(BDRVQEDState * s)277b383ae60SPaolo Bonzini static void coroutine_fn qed_unplug_allocating_write_reqs(BDRVQEDState *s)
2786f321e93SStefan Hajnoczi {
2791f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock);
2806f321e93SStefan Hajnoczi assert(s->allocating_write_reqs_plugged);
2816f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = false;
2821f01e50bSPaolo Bonzini qemu_co_queue_next(&s->allocating_write_reqs);
2831f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
2846f321e93SStefan Hajnoczi }
2856f321e93SStefan Hajnoczi
qed_need_check_timer(BDRVQEDState * s)286c16b8bd4SEmanuele Giuseppe Esposito static void coroutine_fn GRAPH_RDLOCK qed_need_check_timer(BDRVQEDState *s)
2876f321e93SStefan Hajnoczi {
288c0e8f989SKevin Wolf int ret;
2896f321e93SStefan Hajnoczi
2906f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s);
291c16b8bd4SEmanuele Giuseppe Esposito assert_bdrv_graph_readable();
2926f321e93SStefan Hajnoczi
2931f01e50bSPaolo Bonzini if (!qed_plug_allocating_write_reqs(s)) {
2941f01e50bSPaolo Bonzini return;
2951f01e50bSPaolo Bonzini }
2966f321e93SStefan Hajnoczi
2976f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */
298c0e8f989SKevin Wolf ret = bdrv_co_flush(s->bs->file->bs);
299c0e8f989SKevin Wolf if (ret < 0) {
300c0e8f989SKevin Wolf qed_unplug_allocating_write_reqs(s);
301c0e8f989SKevin Wolf return;
302c0e8f989SKevin Wolf }
303c0e8f989SKevin Wolf
304c0e8f989SKevin Wolf s->header.features &= ~QED_F_NEED_CHECK;
305c0e8f989SKevin Wolf ret = qed_write_header(s);
306c0e8f989SKevin Wolf (void) ret;
307c0e8f989SKevin Wolf
308c0e8f989SKevin Wolf qed_unplug_allocating_write_reqs(s);
309c0e8f989SKevin Wolf
310c0e8f989SKevin Wolf ret = bdrv_co_flush(s->bs);
311c0e8f989SKevin Wolf (void) ret;
312c0e8f989SKevin Wolf }
313c0e8f989SKevin Wolf
qed_need_check_timer_entry(void * opaque)3146d47eb0cSKevin Wolf static void coroutine_fn qed_need_check_timer_entry(void *opaque)
3156d47eb0cSKevin Wolf {
3166d47eb0cSKevin Wolf BDRVQEDState *s = opaque;
317c16b8bd4SEmanuele Giuseppe Esposito GRAPH_RDLOCK_GUARD();
3186d47eb0cSKevin Wolf
3196d47eb0cSKevin Wolf qed_need_check_timer(opaque);
3206d47eb0cSKevin Wolf bdrv_dec_in_flight(s->bs);
3216d47eb0cSKevin Wolf }
3226d47eb0cSKevin Wolf
qed_need_check_timer_cb(void * opaque)323c0e8f989SKevin Wolf static void qed_need_check_timer_cb(void *opaque)
324c0e8f989SKevin Wolf {
3256d47eb0cSKevin Wolf BDRVQEDState *s = opaque;
326c0e8f989SKevin Wolf Coroutine *co = qemu_coroutine_create(qed_need_check_timer_entry, opaque);
3276d47eb0cSKevin Wolf
3286d47eb0cSKevin Wolf bdrv_inc_in_flight(s->bs);
329c0e8f989SKevin Wolf qemu_coroutine_enter(co);
3302f47da5fSPaolo Bonzini }
3312f47da5fSPaolo Bonzini
qed_start_need_check_timer(BDRVQEDState * s)3326f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s)
3336f321e93SStefan Hajnoczi {
3346f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s);
3356f321e93SStefan Hajnoczi
336bc72ad67SAlex Bligh /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
3376f321e93SStefan Hajnoczi * migration.
3386f321e93SStefan Hajnoczi */
339bc72ad67SAlex Bligh timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
34073bcb24dSRutuja Shah NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT);
3416f321e93SStefan Hajnoczi }
3426f321e93SStefan Hajnoczi
3436f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */
qed_cancel_need_check_timer(BDRVQEDState * s)3446f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s)
3456f321e93SStefan Hajnoczi {
3466f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s);
347bc72ad67SAlex Bligh timer_del(s->need_check_timer);
3486f321e93SStefan Hajnoczi }
3496f321e93SStefan Hajnoczi
bdrv_qed_detach_aio_context(BlockDriverState * bs)350a8c868c3SStefan Hajnoczi static void bdrv_qed_detach_aio_context(BlockDriverState *bs)
351a8c868c3SStefan Hajnoczi {
352a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque;
353a8c868c3SStefan Hajnoczi
354a8c868c3SStefan Hajnoczi qed_cancel_need_check_timer(s);
355a8c868c3SStefan Hajnoczi timer_free(s->need_check_timer);
356a8c868c3SStefan Hajnoczi }
357a8c868c3SStefan Hajnoczi
bdrv_qed_attach_aio_context(BlockDriverState * bs,AioContext * new_context)358a8c868c3SStefan Hajnoczi static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
359a8c868c3SStefan Hajnoczi AioContext *new_context)
360a8c868c3SStefan Hajnoczi {
361a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque;
362a8c868c3SStefan Hajnoczi
363a8c868c3SStefan Hajnoczi s->need_check_timer = aio_timer_new(new_context,
364a8c868c3SStefan Hajnoczi QEMU_CLOCK_VIRTUAL, SCALE_NS,
365a8c868c3SStefan Hajnoczi qed_need_check_timer_cb, s);
366a8c868c3SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) {
367a8c868c3SStefan Hajnoczi qed_start_need_check_timer(s);
368a8c868c3SStefan Hajnoczi }
369a8c868c3SStefan Hajnoczi }
370a8c868c3SStefan Hajnoczi
bdrv_qed_drain_begin(BlockDriverState * bs)3715e8ac217SKevin Wolf static void bdrv_qed_drain_begin(BlockDriverState *bs)
3726653a73dSFam Zheng {
3736653a73dSFam Zheng BDRVQEDState *s = bs->opaque;
3746653a73dSFam Zheng
3756653a73dSFam Zheng /* Fire the timer immediately in order to start doing I/O as soon as the
3766653a73dSFam Zheng * header is flushed.
3776653a73dSFam Zheng */
3786653a73dSFam Zheng if (s->need_check_timer && timer_pending(s->need_check_timer)) {
3796d47eb0cSKevin Wolf Coroutine *co;
3806d47eb0cSKevin Wolf
3816653a73dSFam Zheng qed_cancel_need_check_timer(s);
3826d47eb0cSKevin Wolf co = qemu_coroutine_create(qed_need_check_timer_entry, s);
3836d47eb0cSKevin Wolf bdrv_inc_in_flight(bs);
3846d47eb0cSKevin Wolf aio_co_enter(bdrv_get_aio_context(bs), co);
3856653a73dSFam Zheng }
3866653a73dSFam Zheng }
3876653a73dSFam Zheng
bdrv_qed_init_state(BlockDriverState * bs)38861c7887eSPaolo Bonzini static void bdrv_qed_init_state(BlockDriverState *bs)
38961c7887eSPaolo Bonzini {
39061c7887eSPaolo Bonzini BDRVQEDState *s = bs->opaque;
39161c7887eSPaolo Bonzini
39261c7887eSPaolo Bonzini memset(s, 0, sizeof(BDRVQEDState));
39361c7887eSPaolo Bonzini s->bs = bs;
3941f01e50bSPaolo Bonzini qemu_co_mutex_init(&s->table_lock);
39561c7887eSPaolo Bonzini qemu_co_queue_init(&s->allocating_write_reqs);
39661c7887eSPaolo Bonzini }
39761c7887eSPaolo Bonzini
3989fb4dfc5SPaolo Bonzini /* Called with table_lock held. */
39988095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
bdrv_qed_do_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)40088095349SEmanuele Giuseppe Esposito bdrv_qed_do_open(BlockDriverState *bs, QDict *options, int flags, Error **errp)
40175411d23SStefan Hajnoczi {
40275411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque;
40375411d23SStefan Hajnoczi QEDHeader le_header;
40475411d23SStefan Hajnoczi int64_t file_size;
40575411d23SStefan Hajnoczi int ret;
40675411d23SStefan Hajnoczi
4073aba34adSAlberto Faria ret = bdrv_co_pread(bs->file, 0, sizeof(le_header), &le_header, 0);
40875411d23SStefan Hajnoczi if (ret < 0) {
40915ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "Failed to read QED header");
41075411d23SStefan Hajnoczi return ret;
41175411d23SStefan Hajnoczi }
41275411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header);
41375411d23SStefan Hajnoczi
41475411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) {
41576abe407SPaolo Bonzini error_setg(errp, "Image not in QED format");
41676abe407SPaolo Bonzini return -EINVAL;
41775411d23SStefan Hajnoczi }
41875411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) {
41910b758e8SKevin Wolf /* image uses unsupported feature bits */
420a55448b3SMax Reitz error_setg(errp, "Unsupported QED features: %" PRIx64,
42110b758e8SKevin Wolf s->header.features & ~QED_FEATURE_MASK);
42210b758e8SKevin Wolf return -ENOTSUP;
42375411d23SStefan Hajnoczi }
42475411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
42515ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "QED cluster size is invalid");
42675411d23SStefan Hajnoczi return -EINVAL;
42775411d23SStefan Hajnoczi }
42875411d23SStefan Hajnoczi
42975411d23SStefan Hajnoczi /* Round down file size to the last cluster */
430bd53086eSEmanuele Giuseppe Esposito file_size = bdrv_co_getlength(bs->file->bs);
43175411d23SStefan Hajnoczi if (file_size < 0) {
43215ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "Failed to get file length");
43375411d23SStefan Hajnoczi return file_size;
43475411d23SStefan Hajnoczi }
43575411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size);
43675411d23SStefan Hajnoczi
43775411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) {
43815ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "QED table size is invalid");
43975411d23SStefan Hajnoczi return -EINVAL;
44075411d23SStefan Hajnoczi }
44175411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size,
44275411d23SStefan Hajnoczi s->header.cluster_size,
44375411d23SStefan Hajnoczi s->header.table_size)) {
44415ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "QED image size is invalid");
44575411d23SStefan Hajnoczi return -EINVAL;
44675411d23SStefan Hajnoczi }
44775411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
44815ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "QED table offset is invalid");
44975411d23SStefan Hajnoczi return -EINVAL;
45075411d23SStefan Hajnoczi }
45175411d23SStefan Hajnoczi
45275411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) /
45375411d23SStefan Hajnoczi sizeof(uint64_t);
454786a4ea8SStefan Hajnoczi s->l2_shift = ctz32(s->header.cluster_size);
45575411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1;
456786a4ea8SStefan Hajnoczi s->l1_shift = s->l2_shift + ctz32(s->table_nelems);
45775411d23SStefan Hajnoczi
4580adfa1edSStefan Hajnoczi /* Header size calculation must not overflow uint32_t */
4590adfa1edSStefan Hajnoczi if (s->header.header_size > UINT32_MAX / s->header.cluster_size) {
46015ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "QED header size is too large");
4610adfa1edSStefan Hajnoczi return -EINVAL;
4620adfa1edSStefan Hajnoczi }
4630adfa1edSStefan Hajnoczi
46475411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) {
465dc70638fSHanna Reitz g_autofree char *backing_file_str = NULL;
466dc70638fSHanna Reitz
46775411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset +
46875411d23SStefan Hajnoczi s->header.backing_filename_size >
46975411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) {
47015ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "QED backing filename offset is invalid");
47175411d23SStefan Hajnoczi return -EINVAL;
47275411d23SStefan Hajnoczi }
47375411d23SStefan Hajnoczi
474dc70638fSHanna Reitz backing_file_str = g_malloc(sizeof(bs->backing_file));
475cf2ab8fcSKevin Wolf ret = qed_read_string(bs->file, s->header.backing_filename_offset,
476998c2019SMax Reitz s->header.backing_filename_size,
477dc70638fSHanna Reitz backing_file_str, sizeof(bs->backing_file));
47875411d23SStefan Hajnoczi if (ret < 0) {
47915ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "Failed to read backing filename");
48075411d23SStefan Hajnoczi return ret;
48175411d23SStefan Hajnoczi }
482dc70638fSHanna Reitz
483dc70638fSHanna Reitz if (!g_str_equal(backing_file_str, bs->backing_file)) {
484998c2019SMax Reitz pstrcpy(bs->backing_file, sizeof(bs->backing_file),
485dc70638fSHanna Reitz backing_file_str);
486dc70638fSHanna Reitz pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
487dc70638fSHanna Reitz backing_file_str);
488dc70638fSHanna Reitz }
48975411d23SStefan Hajnoczi
49075411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
49175411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
49275411d23SStefan Hajnoczi }
49375411d23SStefan Hajnoczi }
49475411d23SStefan Hajnoczi
49575411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards
49675411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older
49775411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is
49875411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear
49975411d23SStefan Hajnoczi * feature is no longer valid.
50075411d23SStefan Hajnoczi */
50175411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
50204c01a5cSKevin Wolf !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) {
50375411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
50475411d23SStefan Hajnoczi
50575411d23SStefan Hajnoczi ret = qed_write_header_sync(s);
50675411d23SStefan Hajnoczi if (ret) {
50715ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "Failed to update header");
50875411d23SStefan Hajnoczi return ret;
50975411d23SStefan Hajnoczi }
51075411d23SStefan Hajnoczi
51175411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */
5123aba34adSAlberto Faria bdrv_co_flush(bs->file->bs);
51375411d23SStefan Hajnoczi }
51475411d23SStefan Hajnoczi
515298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s);
516298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache);
517298800caSStefan Hajnoczi
518298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s);
519298800caSStefan Hajnoczi if (ret) {
52015ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "Failed to read L1 table");
52101979a98SStefan Hajnoczi goto out;
52201979a98SStefan Hajnoczi }
52301979a98SStefan Hajnoczi
52401979a98SStefan Hajnoczi /* If image was not closed cleanly, check consistency */
525058f8f16SStefan Hajnoczi if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
52601979a98SStefan Hajnoczi /* Read-only images cannot be fixed. There is no risk of corruption
52701979a98SStefan Hajnoczi * since write operations are not possible. Therefore, allow
52801979a98SStefan Hajnoczi * potentially inconsistent images to be opened read-only. This can
52901979a98SStefan Hajnoczi * aid data recovery from an otherwise inconsistent image.
53001979a98SStefan Hajnoczi */
5319a4f4c31SKevin Wolf if (!bdrv_is_read_only(bs->file->bs) &&
53204c01a5cSKevin Wolf !(flags & BDRV_O_INACTIVE)) {
53301979a98SStefan Hajnoczi BdrvCheckResult result = {0};
53401979a98SStefan Hajnoczi
53501979a98SStefan Hajnoczi ret = qed_check(s, &result, true);
5366f321e93SStefan Hajnoczi if (ret) {
53715ce94a6SVladimir Sementsov-Ogievskiy error_setg(errp, "Image corrupted");
5386f321e93SStefan Hajnoczi goto out;
5396f321e93SStefan Hajnoczi }
54001979a98SStefan Hajnoczi }
54101979a98SStefan Hajnoczi }
54201979a98SStefan Hajnoczi
543a8c868c3SStefan Hajnoczi bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
5446f321e93SStefan Hajnoczi
54501979a98SStefan Hajnoczi out:
54601979a98SStefan Hajnoczi if (ret) {
547298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache);
548298800caSStefan Hajnoczi qemu_vfree(s->l1_table);
549298800caSStefan Hajnoczi }
55075411d23SStefan Hajnoczi return ret;
55175411d23SStefan Hajnoczi }
55275411d23SStefan Hajnoczi
5539fb4dfc5SPaolo Bonzini typedef struct QEDOpenCo {
5549fb4dfc5SPaolo Bonzini BlockDriverState *bs;
5559fb4dfc5SPaolo Bonzini QDict *options;
5569fb4dfc5SPaolo Bonzini int flags;
5579fb4dfc5SPaolo Bonzini Error **errp;
5589fb4dfc5SPaolo Bonzini int ret;
5599fb4dfc5SPaolo Bonzini } QEDOpenCo;
5609fb4dfc5SPaolo Bonzini
bdrv_qed_open_entry(void * opaque)5611a30b0f5SKevin Wolf static void coroutine_fn bdrv_qed_open_entry(void *opaque)
5629fb4dfc5SPaolo Bonzini {
5639fb4dfc5SPaolo Bonzini QEDOpenCo *qoc = opaque;
5649fb4dfc5SPaolo Bonzini BDRVQEDState *s = qoc->bs->opaque;
5659fb4dfc5SPaolo Bonzini
5661a30b0f5SKevin Wolf GRAPH_RDLOCK_GUARD();
5671a30b0f5SKevin Wolf
5689fb4dfc5SPaolo Bonzini qemu_co_mutex_lock(&s->table_lock);
5699fb4dfc5SPaolo Bonzini qoc->ret = bdrv_qed_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp);
5709fb4dfc5SPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
5719fb4dfc5SPaolo Bonzini }
5729fb4dfc5SPaolo Bonzini
bdrv_qed_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)573e2dbca03SPaolo Bonzini static int coroutine_mixed_fn bdrv_qed_open(BlockDriverState *bs, QDict *options,
574e2dbca03SPaolo Bonzini int flags, Error **errp)
5754e4bf5c4SKevin Wolf {
5769fb4dfc5SPaolo Bonzini QEDOpenCo qoc = {
5779fb4dfc5SPaolo Bonzini .bs = bs,
5789fb4dfc5SPaolo Bonzini .options = options,
5799fb4dfc5SPaolo Bonzini .flags = flags,
5809fb4dfc5SPaolo Bonzini .errp = errp,
5819fb4dfc5SPaolo Bonzini .ret = -EINPROGRESS
5829fb4dfc5SPaolo Bonzini };
58383930780SVladimir Sementsov-Ogievskiy int ret;
5849fb4dfc5SPaolo Bonzini
58583930780SVladimir Sementsov-Ogievskiy ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
58683930780SVladimir Sementsov-Ogievskiy if (ret < 0) {
58783930780SVladimir Sementsov-Ogievskiy return ret;
5884e4bf5c4SKevin Wolf }
5894e4bf5c4SKevin Wolf
59061c7887eSPaolo Bonzini bdrv_qed_init_state(bs);
5911a30b0f5SKevin Wolf assert(!qemu_in_coroutine());
5924720cbeeSKevin Wolf assert(qemu_get_current_aio_context() == qemu_get_aio_context());
5939fb4dfc5SPaolo Bonzini qemu_coroutine_enter(qemu_coroutine_create(bdrv_qed_open_entry, &qoc));
5949fb4dfc5SPaolo Bonzini BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
5951a30b0f5SKevin Wolf
5969fb4dfc5SPaolo Bonzini return qoc.ret;
5974e4bf5c4SKevin Wolf }
5984e4bf5c4SKevin Wolf
bdrv_qed_refresh_limits(BlockDriverState * bs,Error ** errp)5993baca891SKevin Wolf static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
600d34682cdSKevin Wolf {
601d34682cdSKevin Wolf BDRVQEDState *s = bs->opaque;
602d34682cdSKevin Wolf
603cf081fcaSEric Blake bs->bl.pwrite_zeroes_alignment = s->header.cluster_size;
604f34b2bcfSVladimir Sementsov-Ogievskiy bs->bl.max_pwrite_zeroes = QEMU_ALIGN_DOWN(INT_MAX, s->header.cluster_size);
605d34682cdSKevin Wolf }
606d34682cdSKevin Wolf
607f9cb20f1SJeff Cody /* We have nothing to do for QED reopen, stubs just return
608f9cb20f1SJeff Cody * success */
bdrv_qed_reopen_prepare(BDRVReopenState * state,BlockReopenQueue * queue,Error ** errp)609f9cb20f1SJeff Cody static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
610f9cb20f1SJeff Cody BlockReopenQueue *queue, Error **errp)
611f9cb20f1SJeff Cody {
612f9cb20f1SJeff Cody return 0;
613f9cb20f1SJeff Cody }
614f9cb20f1SJeff Cody
bdrv_qed_do_close(BlockDriverState * bs)6151f051dcbSKevin Wolf static void GRAPH_RDLOCK bdrv_qed_do_close(BlockDriverState *bs)
61675411d23SStefan Hajnoczi {
617298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque;
618298800caSStefan Hajnoczi
619a8c868c3SStefan Hajnoczi bdrv_qed_detach_aio_context(bs);
6206f321e93SStefan Hajnoczi
62101979a98SStefan Hajnoczi /* Ensure writes reach stable storage */
6229a4f4c31SKevin Wolf bdrv_flush(bs->file->bs);
62301979a98SStefan Hajnoczi
62401979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */
62501979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) {
62601979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK;
62701979a98SStefan Hajnoczi qed_write_header_sync(s);
62801979a98SStefan Hajnoczi }
62901979a98SStefan Hajnoczi
630298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache);
631298800caSStefan Hajnoczi qemu_vfree(s->l1_table);
63275411d23SStefan Hajnoczi }
63375411d23SStefan Hajnoczi
bdrv_qed_close(BlockDriverState * bs)6341f051dcbSKevin Wolf static void GRAPH_UNLOCKED bdrv_qed_close(BlockDriverState *bs)
6351f051dcbSKevin Wolf {
6361f051dcbSKevin Wolf GLOBAL_STATE_CODE();
6371f051dcbSKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
6381f051dcbSKevin Wolf
6391f051dcbSKevin Wolf bdrv_qed_do_close(bs);
6401f051dcbSKevin Wolf }
6411f051dcbSKevin Wolf
6424db7ba3bSKevin Wolf static int coroutine_fn GRAPH_UNLOCKED
bdrv_qed_co_create(BlockdevCreateOptions * opts,Error ** errp)6434db7ba3bSKevin Wolf bdrv_qed_co_create(BlockdevCreateOptions *opts, Error **errp)
64475411d23SStefan Hajnoczi {
645959355a4SKevin Wolf BlockdevCreateOptionsQed *qed_opts;
646959355a4SKevin Wolf BlockBackend *blk = NULL;
647959355a4SKevin Wolf BlockDriverState *bs = NULL;
648959355a4SKevin Wolf
649959355a4SKevin Wolf QEDHeader header;
65075411d23SStefan Hajnoczi QEDHeader le_header;
65175411d23SStefan Hajnoczi uint8_t *l1_table = NULL;
652959355a4SKevin Wolf size_t l1_size;
65375411d23SStefan Hajnoczi int ret = 0;
65475411d23SStefan Hajnoczi
655959355a4SKevin Wolf assert(opts->driver == BLOCKDEV_DRIVER_QED);
656959355a4SKevin Wolf qed_opts = &opts->u.qed;
657959355a4SKevin Wolf
658959355a4SKevin Wolf /* Validate options and set default values */
659959355a4SKevin Wolf if (!qed_opts->has_cluster_size) {
660959355a4SKevin Wolf qed_opts->cluster_size = QED_DEFAULT_CLUSTER_SIZE;
661959355a4SKevin Wolf }
662959355a4SKevin Wolf if (!qed_opts->has_table_size) {
663959355a4SKevin Wolf qed_opts->table_size = QED_DEFAULT_TABLE_SIZE;
66475411d23SStefan Hajnoczi }
66575411d23SStefan Hajnoczi
666959355a4SKevin Wolf if (!qed_is_cluster_size_valid(qed_opts->cluster_size)) {
667959355a4SKevin Wolf error_setg(errp, "QED cluster size must be within range [%u, %u] "
668959355a4SKevin Wolf "and power of 2",
669959355a4SKevin Wolf QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
670959355a4SKevin Wolf return -EINVAL;
671959355a4SKevin Wolf }
672959355a4SKevin Wolf if (!qed_is_table_size_valid(qed_opts->table_size)) {
673959355a4SKevin Wolf error_setg(errp, "QED table size must be within range [%u, %u] "
674959355a4SKevin Wolf "and power of 2",
675959355a4SKevin Wolf QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
676959355a4SKevin Wolf return -EINVAL;
677959355a4SKevin Wolf }
678959355a4SKevin Wolf if (!qed_is_image_size_valid(qed_opts->size, qed_opts->cluster_size,
679959355a4SKevin Wolf qed_opts->table_size))
680959355a4SKevin Wolf {
681959355a4SKevin Wolf error_setg(errp, "QED image size must be a non-zero multiple of "
682959355a4SKevin Wolf "cluster size and less than %" PRIu64 " bytes",
683959355a4SKevin Wolf qed_max_image_size(qed_opts->cluster_size,
684959355a4SKevin Wolf qed_opts->table_size));
685959355a4SKevin Wolf return -EINVAL;
686959355a4SKevin Wolf }
687959355a4SKevin Wolf
688959355a4SKevin Wolf /* Create BlockBackend to write to the image */
6890b1e95cfSKevin Wolf bs = bdrv_co_open_blockdev_ref(qed_opts->file, errp);
690959355a4SKevin Wolf if (bs == NULL) {
6918a56fdadSKevin Wolf return -EIO;
69275411d23SStefan Hajnoczi }
69375411d23SStefan Hajnoczi
6940b1e95cfSKevin Wolf blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
695a3aeeab5SEric Blake errp);
696a3aeeab5SEric Blake if (!blk) {
697a3aeeab5SEric Blake ret = -EPERM;
698959355a4SKevin Wolf goto out;
699959355a4SKevin Wolf }
7008a56fdadSKevin Wolf blk_set_allow_write_beyond_eof(blk, true);
7018a56fdadSKevin Wolf
702959355a4SKevin Wolf /* Prepare image format */
703959355a4SKevin Wolf header = (QEDHeader) {
704959355a4SKevin Wolf .magic = QED_MAGIC,
705959355a4SKevin Wolf .cluster_size = qed_opts->cluster_size,
706959355a4SKevin Wolf .table_size = qed_opts->table_size,
707959355a4SKevin Wolf .header_size = 1,
708959355a4SKevin Wolf .features = 0,
709959355a4SKevin Wolf .compat_features = 0,
710959355a4SKevin Wolf .l1_table_offset = qed_opts->cluster_size,
711959355a4SKevin Wolf .image_size = qed_opts->size,
712959355a4SKevin Wolf };
713959355a4SKevin Wolf
714959355a4SKevin Wolf l1_size = header.cluster_size * header.table_size;
715959355a4SKevin Wolf
716e8d04f92SMax Reitz /*
717e8d04f92SMax Reitz * The QED format associates file length with allocation status,
718e8d04f92SMax Reitz * so a new file (which is empty) must have a length of 0.
719e8d04f92SMax Reitz */
7203aba34adSAlberto Faria ret = blk_co_truncate(blk, 0, true, PREALLOC_MODE_OFF, 0, errp);
721c743849bSStefan Hajnoczi if (ret < 0) {
722c743849bSStefan Hajnoczi goto out;
723c743849bSStefan Hajnoczi }
724c743849bSStefan Hajnoczi
72554fde4ffSMarkus Armbruster if (qed_opts->backing_file) {
72675411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE;
72775411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header);
728959355a4SKevin Wolf header.backing_filename_size = strlen(qed_opts->backing_file);
72975411d23SStefan Hajnoczi
730959355a4SKevin Wolf if (qed_opts->has_backing_fmt) {
731959355a4SKevin Wolf const char *backing_fmt = BlockdevDriver_str(qed_opts->backing_fmt);
73275411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) {
73375411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
73475411d23SStefan Hajnoczi }
73575411d23SStefan Hajnoczi }
736959355a4SKevin Wolf }
73775411d23SStefan Hajnoczi
73875411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header);
7393aba34adSAlberto Faria ret = blk_co_pwrite(blk, 0, sizeof(le_header), &le_header, 0);
74075411d23SStefan Hajnoczi if (ret < 0) {
74175411d23SStefan Hajnoczi goto out;
74275411d23SStefan Hajnoczi }
7433aba34adSAlberto Faria ret = blk_co_pwrite(blk, sizeof(le_header), header.backing_filename_size,
744a9262f55SAlberto Faria qed_opts->backing_file, 0);
74575411d23SStefan Hajnoczi if (ret < 0) {
74675411d23SStefan Hajnoczi goto out;
74775411d23SStefan Hajnoczi }
74875411d23SStefan Hajnoczi
7497267c094SAnthony Liguori l1_table = g_malloc0(l1_size);
7503aba34adSAlberto Faria ret = blk_co_pwrite(blk, header.l1_table_offset, l1_size, l1_table, 0);
75175411d23SStefan Hajnoczi if (ret < 0) {
75275411d23SStefan Hajnoczi goto out;
75375411d23SStefan Hajnoczi }
75475411d23SStefan Hajnoczi
75575411d23SStefan Hajnoczi ret = 0; /* success */
75675411d23SStefan Hajnoczi out:
7577267c094SAnthony Liguori g_free(l1_table);
758b2ab5f54SKevin Wolf blk_co_unref(blk);
759b2ab5f54SKevin Wolf bdrv_co_unref(bs);
76075411d23SStefan Hajnoczi return ret;
76175411d23SStefan Hajnoczi }
76275411d23SStefan Hajnoczi
7634db7ba3bSKevin Wolf static int coroutine_fn GRAPH_UNLOCKED
bdrv_qed_co_create_opts(BlockDriver * drv,const char * filename,QemuOpts * opts,Error ** errp)7644ec8df01SKevin Wolf bdrv_qed_co_create_opts(BlockDriver *drv, const char *filename,
7654ec8df01SKevin Wolf QemuOpts *opts, Error **errp)
76675411d23SStefan Hajnoczi {
767959355a4SKevin Wolf BlockdevCreateOptions *create_options = NULL;
76892adf9dbSMarkus Armbruster QDict *qdict;
769959355a4SKevin Wolf Visitor *v;
770959355a4SKevin Wolf BlockDriverState *bs = NULL;
7717ab74849SChunyan Liu int ret;
77275411d23SStefan Hajnoczi
773959355a4SKevin Wolf static const QDictRenames opt_renames[] = {
774959355a4SKevin Wolf { BLOCK_OPT_BACKING_FILE, "backing-file" },
775959355a4SKevin Wolf { BLOCK_OPT_BACKING_FMT, "backing-fmt" },
776959355a4SKevin Wolf { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" },
777959355a4SKevin Wolf { BLOCK_OPT_TABLE_SIZE, "table-size" },
778959355a4SKevin Wolf { NULL, NULL },
779959355a4SKevin Wolf };
78075411d23SStefan Hajnoczi
781959355a4SKevin Wolf /* Parse options and convert legacy syntax */
782959355a4SKevin Wolf qdict = qemu_opts_to_qdict_filtered(opts, NULL, &qed_create_opts, true);
783959355a4SKevin Wolf
784959355a4SKevin Wolf if (!qdict_rename_keys(qdict, opt_renames, errp)) {
7857ab74849SChunyan Liu ret = -EINVAL;
786959355a4SKevin Wolf goto fail;
78775411d23SStefan Hajnoczi }
78875411d23SStefan Hajnoczi
789959355a4SKevin Wolf /* Create and open the file (protocol layer) */
7902475a0d0SEmanuele Giuseppe Esposito ret = bdrv_co_create_file(filename, opts, errp);
791959355a4SKevin Wolf if (ret < 0) {
792959355a4SKevin Wolf goto fail;
793959355a4SKevin Wolf }
7947ab74849SChunyan Liu
7950b1e95cfSKevin Wolf bs = bdrv_co_open(filename, NULL, NULL,
796959355a4SKevin Wolf BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
797959355a4SKevin Wolf if (bs == NULL) {
798959355a4SKevin Wolf ret = -EIO;
799959355a4SKevin Wolf goto fail;
800959355a4SKevin Wolf }
801959355a4SKevin Wolf
802959355a4SKevin Wolf /* Now get the QAPI type BlockdevCreateOptions */
803959355a4SKevin Wolf qdict_put_str(qdict, "driver", "qed");
804959355a4SKevin Wolf qdict_put_str(qdict, "file", bs->node_name);
805959355a4SKevin Wolf
806af91062eSMarkus Armbruster v = qobject_input_visitor_new_flat_confused(qdict, errp);
807af91062eSMarkus Armbruster if (!v) {
808959355a4SKevin Wolf ret = -EINVAL;
809959355a4SKevin Wolf goto fail;
810959355a4SKevin Wolf }
811959355a4SKevin Wolf
812b11a093cSMarkus Armbruster visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
813959355a4SKevin Wolf visit_free(v);
814b11a093cSMarkus Armbruster if (!create_options) {
815959355a4SKevin Wolf ret = -EINVAL;
816959355a4SKevin Wolf goto fail;
817959355a4SKevin Wolf }
818959355a4SKevin Wolf
819959355a4SKevin Wolf /* Silently round up size */
820959355a4SKevin Wolf assert(create_options->driver == BLOCKDEV_DRIVER_QED);
821959355a4SKevin Wolf create_options->u.qed.size =
822959355a4SKevin Wolf ROUND_UP(create_options->u.qed.size, BDRV_SECTOR_SIZE);
823959355a4SKevin Wolf
824959355a4SKevin Wolf /* Create the qed image (format layer) */
825959355a4SKevin Wolf ret = bdrv_qed_co_create(create_options, errp);
826959355a4SKevin Wolf
827959355a4SKevin Wolf fail:
828cb3e7f08SMarc-André Lureau qobject_unref(qdict);
829b2ab5f54SKevin Wolf bdrv_co_unref(bs);
830959355a4SKevin Wolf qapi_free_BlockdevCreateOptions(create_options);
8317ab74849SChunyan Liu return ret;
83275411d23SStefan Hajnoczi }
83375411d23SStefan Hajnoczi
834b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
bdrv_qed_co_block_status(BlockDriverState * bs,bool want_zero,int64_t pos,int64_t bytes,int64_t * pnum,int64_t * map,BlockDriverState ** file)835b9b10c35SKevin Wolf bdrv_qed_co_block_status(BlockDriverState *bs, bool want_zero, int64_t pos,
836b9b10c35SKevin Wolf int64_t bytes, int64_t *pnum, int64_t *map,
83767a0fd2aSFam Zheng BlockDriverState **file)
83875411d23SStefan Hajnoczi {
839298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque;
840b8d739fdSEric Blake size_t len = MIN(bytes, SIZE_MAX);
841b8d739fdSEric Blake int status;
842298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL };
8430f21b7a1SKevin Wolf uint64_t offset;
8440f21b7a1SKevin Wolf int ret;
845298800caSStefan Hajnoczi
8461f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock);
847b8d739fdSEric Blake ret = qed_find_cluster(s, &request, pos, &len, &offset);
848298800caSStefan Hajnoczi
849b8d739fdSEric Blake *pnum = len;
850b8d739fdSEric Blake switch (ret) {
851b8d739fdSEric Blake case QED_CLUSTER_FOUND:
852b8d739fdSEric Blake *map = offset | qed_offset_into_cluster(s, pos);
853b8d739fdSEric Blake status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
854b8d739fdSEric Blake *file = bs->file->bs;
855b8d739fdSEric Blake break;
856b8d739fdSEric Blake case QED_CLUSTER_ZERO:
857b8d739fdSEric Blake status = BDRV_BLOCK_ZERO;
858b8d739fdSEric Blake break;
859b8d739fdSEric Blake case QED_CLUSTER_L2:
860b8d739fdSEric Blake case QED_CLUSTER_L1:
861b8d739fdSEric Blake status = 0;
862b8d739fdSEric Blake break;
863b8d739fdSEric Blake default:
864b8d739fdSEric Blake assert(ret < 0);
865b8d739fdSEric Blake status = ret;
866b8d739fdSEric Blake break;
867b8d739fdSEric Blake }
868298800caSStefan Hajnoczi
869298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table);
8701f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
871298800caSStefan Hajnoczi
872b8d739fdSEric Blake return status;
87375411d23SStefan Hajnoczi }
87475411d23SStefan Hajnoczi
acb_to_s(QEDAIOCB * acb)875eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
876eabba580SStefan Hajnoczi {
87748cc565eSKevin Wolf return acb->bs->opaque;
878eabba580SStefan Hajnoczi }
879eabba580SStefan Hajnoczi
880eabba580SStefan Hajnoczi /**
881eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file
882eabba580SStefan Hajnoczi *
883eabba580SStefan Hajnoczi * @s: QED state
884eabba580SStefan Hajnoczi * @pos: Byte position in device
885eabba580SStefan Hajnoczi * @qiov: Destination I/O vector
886eabba580SStefan Hajnoczi *
887eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file.
888eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read.
889eabba580SStefan Hajnoczi */
890b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
qed_read_backing_file(BDRVQEDState * s,uint64_t pos,QEMUIOVector * qiov)891b9b10c35SKevin Wolf qed_read_backing_file(BDRVQEDState *s, uint64_t pos, QEMUIOVector *qiov)
892eabba580SStefan Hajnoczi {
893760e0063SKevin Wolf if (s->bs->backing) {
89417362398SPaolo Bonzini BLKDBG_CO_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
895365fed51SEric Blake return bdrv_co_preadv(s->bs->backing, pos, qiov->size, qiov, 0);
896e85c5281SKevin Wolf }
897365fed51SEric Blake qemu_iovec_memset(qiov, 0, 0, qiov->size);
898e85c5281SKevin Wolf return 0;
899eabba580SStefan Hajnoczi }
900eabba580SStefan Hajnoczi
901eabba580SStefan Hajnoczi /**
902eabba580SStefan Hajnoczi * Copy data from backing file into the image
903eabba580SStefan Hajnoczi *
904eabba580SStefan Hajnoczi * @s: QED state
905eabba580SStefan Hajnoczi * @pos: Byte position in device
906eabba580SStefan Hajnoczi * @len: Number of bytes
907eabba580SStefan Hajnoczi * @offset: Byte offset in image file
908eabba580SStefan Hajnoczi */
909b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
qed_copy_from_backing_file(BDRVQEDState * s,uint64_t pos,uint64_t len,uint64_t offset)910b9b10c35SKevin Wolf qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, uint64_t len,
91187f0d882SKevin Wolf uint64_t offset)
912eabba580SStefan Hajnoczi {
9130f7aa24dSKevin Wolf QEMUIOVector qiov;
914e85c5281SKevin Wolf int ret;
915eabba580SStefan Hajnoczi
916eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */
917eabba580SStefan Hajnoczi if (len == 0) {
918b4ac32f3SKevin Wolf return 0;
919eabba580SStefan Hajnoczi }
920eabba580SStefan Hajnoczi
921342544f9SVladimir Sementsov-Ogievskiy qemu_iovec_init_buf(&qiov, qemu_blockalign(s->bs, len), len);
922eabba580SStefan Hajnoczi
923365fed51SEric Blake ret = qed_read_backing_file(s, pos, &qiov);
9240f7aa24dSKevin Wolf
9250f7aa24dSKevin Wolf if (ret) {
9260f7aa24dSKevin Wolf goto out;
9270f7aa24dSKevin Wolf }
9280f7aa24dSKevin Wolf
92917362398SPaolo Bonzini BLKDBG_CO_EVENT(s->bs->file, BLKDBG_COW_WRITE);
9300f714ec7SKevin Wolf ret = bdrv_co_pwritev(s->bs->file, offset, qiov.size, &qiov, 0);
9310f7aa24dSKevin Wolf if (ret < 0) {
9320f7aa24dSKevin Wolf goto out;
9330f7aa24dSKevin Wolf }
9340f7aa24dSKevin Wolf ret = 0;
9350f7aa24dSKevin Wolf out:
936342544f9SVladimir Sementsov-Ogievskiy qemu_vfree(qemu_iovec_buf(&qiov));
937b4ac32f3SKevin Wolf return ret;
938eabba580SStefan Hajnoczi }
939eabba580SStefan Hajnoczi
940eabba580SStefan Hajnoczi /**
941eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table
942eabba580SStefan Hajnoczi *
943eabba580SStefan Hajnoczi * @s: QED state
944eabba580SStefan Hajnoczi * @table: L2 table
945eabba580SStefan Hajnoczi * @index: First cluster index
946eabba580SStefan Hajnoczi * @n: Number of contiguous clusters
94721df65b6SAnthony Liguori * @cluster: First cluster offset
94821df65b6SAnthony Liguori *
94921df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the
95021df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker.
9511f01e50bSPaolo Bonzini *
9521f01e50bSPaolo Bonzini * Called with table_lock held.
953eabba580SStefan Hajnoczi */
qed_update_l2_table(BDRVQEDState * s,QEDTable * table,int index,unsigned int n,uint64_t cluster)95487f0d882SKevin Wolf static void coroutine_fn qed_update_l2_table(BDRVQEDState *s, QEDTable *table,
95587f0d882SKevin Wolf int index, unsigned int n,
95687f0d882SKevin Wolf uint64_t cluster)
957eabba580SStefan Hajnoczi {
958eabba580SStefan Hajnoczi int i;
959eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) {
960eabba580SStefan Hajnoczi table->offsets[i] = cluster;
96121df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) &&
96221df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) {
963eabba580SStefan Hajnoczi cluster += s->header.cluster_size;
964eabba580SStefan Hajnoczi }
965eabba580SStefan Hajnoczi }
96621df65b6SAnthony Liguori }
967eabba580SStefan Hajnoczi
9681f01e50bSPaolo Bonzini /* Called with table_lock held. */
qed_aio_complete(QEDAIOCB * acb)96987f0d882SKevin Wolf static void coroutine_fn qed_aio_complete(QEDAIOCB *acb)
970eabba580SStefan Hajnoczi {
971eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb);
972eabba580SStefan Hajnoczi
973eabba580SStefan Hajnoczi /* Free resources */
974eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov);
975eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table);
976eabba580SStefan Hajnoczi
9770e71be19SStefan Hajnoczi /* Free the buffer we may have allocated for zero writes */
9780e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) {
9790e71be19SStefan Hajnoczi qemu_vfree(acb->qiov->iov[0].iov_base);
9800e71be19SStefan Hajnoczi acb->qiov->iov[0].iov_base = NULL;
9810e71be19SStefan Hajnoczi }
9820e71be19SStefan Hajnoczi
983eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that
984eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster
985eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the
986eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through
987eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely.
988eabba580SStefan Hajnoczi */
9890806c3b5SKevin Wolf if (acb == s->allocating_acb) {
9900806c3b5SKevin Wolf s->allocating_acb = NULL;
9910806c3b5SKevin Wolf if (!qemu_co_queue_empty(&s->allocating_write_reqs)) {
9921f01e50bSPaolo Bonzini qemu_co_queue_next(&s->allocating_write_reqs);
9936f321e93SStefan Hajnoczi } else if (s->header.features & QED_F_NEED_CHECK) {
9946f321e93SStefan Hajnoczi qed_start_need_check_timer(s);
995eabba580SStefan Hajnoczi }
996eabba580SStefan Hajnoczi }
997eabba580SStefan Hajnoczi }
998eabba580SStefan Hajnoczi
999eabba580SStefan Hajnoczi /**
1000fae25ac7SKevin Wolf * Update L1 table with new L2 table offset and write it out
10011f01e50bSPaolo Bonzini *
10021f01e50bSPaolo Bonzini * Called with table_lock held.
1003eabba580SStefan Hajnoczi */
qed_aio_write_l1_update(QEDAIOCB * acb)100488095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK qed_aio_write_l1_update(QEDAIOCB *acb)
1005eabba580SStefan Hajnoczi {
1006eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb);
1007eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table;
1008e4fc8781SStefan Hajnoczi uint64_t l2_offset = l2_table->offset;
1009fb18de21SKevin Wolf int index, ret;
1010fae25ac7SKevin Wolf
1011fae25ac7SKevin Wolf index = qed_l1_index(s, acb->cur_pos);
1012fae25ac7SKevin Wolf s->l1_table->offsets[index] = l2_table->offset;
1013fae25ac7SKevin Wolf
1014fae25ac7SKevin Wolf ret = qed_write_l1_table(s, index, 1);
1015fae25ac7SKevin Wolf
1016fae25ac7SKevin Wolf /* Commit the current L2 table to the cache */
1017eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
1018eabba580SStefan Hajnoczi
1019eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the
1020eabba580SStefan Hajnoczi * cache.
1021eabba580SStefan Hajnoczi */
1022e4fc8781SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
1023eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL);
1024eabba580SStefan Hajnoczi
1025fb18de21SKevin Wolf return ret;
1026eabba580SStefan Hajnoczi }
1027eabba580SStefan Hajnoczi
1028eabba580SStefan Hajnoczi
1029eabba580SStefan Hajnoczi /**
1030eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out
10311f01e50bSPaolo Bonzini *
10321f01e50bSPaolo Bonzini * Called with table_lock held.
1033eabba580SStefan Hajnoczi */
103488095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
qed_aio_write_l2_update(QEDAIOCB * acb,uint64_t offset)103588095349SEmanuele Giuseppe Esposito qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset)
1036eabba580SStefan Hajnoczi {
1037eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb);
1038eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
103988d2dd72SKevin Wolf int index, ret;
1040eabba580SStefan Hajnoczi
1041eabba580SStefan Hajnoczi if (need_alloc) {
1042eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table);
1043eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s);
1044eabba580SStefan Hajnoczi }
1045eabba580SStefan Hajnoczi
1046eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos);
1047eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
10480e71be19SStefan Hajnoczi offset);
1049eabba580SStefan Hajnoczi
1050eabba580SStefan Hajnoczi if (need_alloc) {
1051eabba580SStefan Hajnoczi /* Write out the whole new L2 table */
1052453e53e2SKevin Wolf ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true);
1053fb18de21SKevin Wolf if (ret) {
105488d2dd72SKevin Wolf return ret;
1055fb18de21SKevin Wolf }
105688d2dd72SKevin Wolf return qed_aio_write_l1_update(acb);
1057eabba580SStefan Hajnoczi } else {
1058eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */
1059453e53e2SKevin Wolf ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters,
1060453e53e2SKevin Wolf false);
106188d2dd72SKevin Wolf if (ret) {
106288d2dd72SKevin Wolf return ret;
1063eabba580SStefan Hajnoczi }
106488d2dd72SKevin Wolf }
106588d2dd72SKevin Wolf return 0;
1066eabba580SStefan Hajnoczi }
1067eabba580SStefan Hajnoczi
1068eabba580SStefan Hajnoczi /**
1069eabba580SStefan Hajnoczi * Write data to the image file
10701f01e50bSPaolo Bonzini *
10711f01e50bSPaolo Bonzini * Called with table_lock *not* held.
1072eabba580SStefan Hajnoczi */
qed_aio_write_main(QEDAIOCB * acb)1073b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK qed_aio_write_main(QEDAIOCB *acb)
1074eabba580SStefan Hajnoczi {
1075eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb);
1076eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster +
1077eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos);
1078eabba580SStefan Hajnoczi
1079eaf0bc56SKevin Wolf trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size);
1080eabba580SStefan Hajnoczi
108117362398SPaolo Bonzini BLKDBG_CO_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
1082e7569c18SPaolo Bonzini return bdrv_co_pwritev(s->bs->file, offset, acb->cur_qiov.size,
10830f714ec7SKevin Wolf &acb->cur_qiov, 0);
1084eabba580SStefan Hajnoczi }
1085eabba580SStefan Hajnoczi
1086eabba580SStefan Hajnoczi /**
1087b4ac32f3SKevin Wolf * Populate untouched regions of new data cluster
10881f01e50bSPaolo Bonzini *
10891f01e50bSPaolo Bonzini * Called with table_lock held.
1090eabba580SStefan Hajnoczi */
qed_aio_write_cow(QEDAIOCB * acb)109188095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK qed_aio_write_cow(QEDAIOCB *acb)
1092eabba580SStefan Hajnoczi {
1093eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb);
1094b4ac32f3SKevin Wolf uint64_t start, len, offset;
1095a101341aSKevin Wolf int ret;
1096eabba580SStefan Hajnoczi
10971f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
10981f01e50bSPaolo Bonzini
1099b4ac32f3SKevin Wolf /* Populate front untouched region of new data cluster */
1100b4ac32f3SKevin Wolf start = qed_start_of_cluster(s, acb->cur_pos);
1101b4ac32f3SKevin Wolf len = qed_offset_into_cluster(s, acb->cur_pos);
1102b4ac32f3SKevin Wolf
1103b4ac32f3SKevin Wolf trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1104b4ac32f3SKevin Wolf ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster);
1105a101341aSKevin Wolf if (ret < 0) {
11061f01e50bSPaolo Bonzini goto out;
1107eabba580SStefan Hajnoczi }
1108eabba580SStefan Hajnoczi
1109b4ac32f3SKevin Wolf /* Populate back untouched region of new data cluster */
1110b4ac32f3SKevin Wolf start = acb->cur_pos + acb->cur_qiov.size;
1111b4ac32f3SKevin Wolf len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1112b4ac32f3SKevin Wolf offset = acb->cur_cluster +
1113b4ac32f3SKevin Wolf qed_offset_into_cluster(s, acb->cur_pos) +
1114b4ac32f3SKevin Wolf acb->cur_qiov.size;
1115b4ac32f3SKevin Wolf
1116eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset);
1117b4ac32f3SKevin Wolf ret = qed_copy_from_backing_file(s, start, len, offset);
1118a101341aSKevin Wolf if (ret < 0) {
11191f01e50bSPaolo Bonzini goto out;
1120eaf0bc56SKevin Wolf }
1121eabba580SStefan Hajnoczi
1122e7569c18SPaolo Bonzini ret = qed_aio_write_main(acb);
1123e7569c18SPaolo Bonzini if (ret < 0) {
11241f01e50bSPaolo Bonzini goto out;
1125e7569c18SPaolo Bonzini }
1126e7569c18SPaolo Bonzini
1127e7569c18SPaolo Bonzini if (s->bs->backing) {
1128e7569c18SPaolo Bonzini /*
1129e7569c18SPaolo Bonzini * Flush new data clusters before updating the L2 table
1130e7569c18SPaolo Bonzini *
1131e7569c18SPaolo Bonzini * This flush is necessary when a backing file is in use. A crash
1132e7569c18SPaolo Bonzini * during an allocating write could result in empty clusters in the
1133e7569c18SPaolo Bonzini * image. If the write only touched a subregion of the cluster,
1134e7569c18SPaolo Bonzini * then backing image sectors have been lost in the untouched
1135e7569c18SPaolo Bonzini * region. The solution is to flush after writing a new data
1136e7569c18SPaolo Bonzini * cluster and before updating the L2 table.
1137e7569c18SPaolo Bonzini */
1138e7569c18SPaolo Bonzini ret = bdrv_co_flush(s->bs->file->bs);
1139e7569c18SPaolo Bonzini }
1140e7569c18SPaolo Bonzini
11411f01e50bSPaolo Bonzini out:
11421f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock);
11431f01e50bSPaolo Bonzini return ret;
1144eabba580SStefan Hajnoczi }
1145eabba580SStefan Hajnoczi
1146eabba580SStefan Hajnoczi /**
11470d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write
11480d09c797SStefan Hajnoczi */
qed_should_set_need_check(BDRVQEDState * s)1149004915a9SKevin Wolf static bool GRAPH_RDLOCK qed_should_set_need_check(BDRVQEDState *s)
11500d09c797SStefan Hajnoczi {
11510d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */
1152760e0063SKevin Wolf if (s->bs->backing) {
11530d09c797SStefan Hajnoczi return false;
11540d09c797SStefan Hajnoczi }
11550d09c797SStefan Hajnoczi
11560d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK);
11570d09c797SStefan Hajnoczi }
11580d09c797SStefan Hajnoczi
11590d09c797SStefan Hajnoczi /**
1160eabba580SStefan Hajnoczi * Write new data cluster
1161eabba580SStefan Hajnoczi *
1162eabba580SStefan Hajnoczi * @acb: Write request
1163eabba580SStefan Hajnoczi * @len: Length in bytes
1164eabba580SStefan Hajnoczi *
1165eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters.
11661f01e50bSPaolo Bonzini *
11671f01e50bSPaolo Bonzini * Called with table_lock held.
1168eabba580SStefan Hajnoczi */
116988095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
qed_aio_write_alloc(QEDAIOCB * acb,size_t len)117088095349SEmanuele Giuseppe Esposito qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
1171eabba580SStefan Hajnoczi {
1172eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb);
1173f13d712bSKevin Wolf int ret;
1174eabba580SStefan Hajnoczi
11756f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */
11760806c3b5SKevin Wolf if (s->allocating_acb == NULL) {
11776f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s);
11786f321e93SStefan Hajnoczi }
11796f321e93SStefan Hajnoczi
1180eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */
11810806c3b5SKevin Wolf if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) {
11820806c3b5SKevin Wolf if (s->allocating_acb != NULL) {
11831f01e50bSPaolo Bonzini qemu_co_queue_wait(&s->allocating_write_reqs, &s->table_lock);
11840806c3b5SKevin Wolf assert(s->allocating_acb == NULL);
1185eabba580SStefan Hajnoczi }
11860806c3b5SKevin Wolf s->allocating_acb = acb;
11870806c3b5SKevin Wolf return -EAGAIN; /* start over with looking up table entries */
1188eabba580SStefan Hajnoczi }
1189eabba580SStefan Hajnoczi
1190eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s,
1191eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len);
11921b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1193eabba580SStefan Hajnoczi
11940e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) {
11950e71be19SStefan Hajnoczi /* Skip ahead if the clusters are already zero */
11960e71be19SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
1197d6daddcdSKevin Wolf return 0;
11980e71be19SStefan Hajnoczi }
1199e7569c18SPaolo Bonzini acb->cur_cluster = 1;
12000e71be19SStefan Hajnoczi } else {
12010e71be19SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
12020e71be19SStefan Hajnoczi }
12030e71be19SStefan Hajnoczi
12040d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) {
120501979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK;
1206f13d712bSKevin Wolf ret = qed_write_header(s);
1207a101341aSKevin Wolf if (ret < 0) {
1208d6daddcdSKevin Wolf return ret;
12090d09c797SStefan Hajnoczi }
1210eabba580SStefan Hajnoczi }
1211eabba580SStefan Hajnoczi
1212e7569c18SPaolo Bonzini if (!(acb->flags & QED_AIOCB_ZERO)) {
1213a101341aSKevin Wolf ret = qed_aio_write_cow(acb);
1214a101341aSKevin Wolf if (ret < 0) {
1215d6daddcdSKevin Wolf return ret;
1216a101341aSKevin Wolf }
1217e7569c18SPaolo Bonzini }
1218e7569c18SPaolo Bonzini
1219e7569c18SPaolo Bonzini return qed_aio_write_l2_update(acb, acb->cur_cluster);
1220a101341aSKevin Wolf }
1221a101341aSKevin Wolf
1222eabba580SStefan Hajnoczi /**
1223eabba580SStefan Hajnoczi * Write data cluster in place
1224eabba580SStefan Hajnoczi *
1225eabba580SStefan Hajnoczi * @acb: Write request
1226eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes
1227eabba580SStefan Hajnoczi * @len: Length in bytes
1228eabba580SStefan Hajnoczi *
1229eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters.
12301f01e50bSPaolo Bonzini *
12311f01e50bSPaolo Bonzini * Called with table_lock held.
1232eabba580SStefan Hajnoczi */
1233b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
qed_aio_write_inplace(QEDAIOCB * acb,uint64_t offset,size_t len)1234b9b10c35SKevin Wolf qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
1235eabba580SStefan Hajnoczi {
12361f01e50bSPaolo Bonzini BDRVQEDState *s = acb_to_s(acb);
12371f01e50bSPaolo Bonzini int r;
12381f01e50bSPaolo Bonzini
12391f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
12401f01e50bSPaolo Bonzini
12410e71be19SStefan Hajnoczi /* Allocate buffer for zero writes */
12420e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) {
12430e71be19SStefan Hajnoczi struct iovec *iov = acb->qiov->iov;
12440e71be19SStefan Hajnoczi
12450e71be19SStefan Hajnoczi if (!iov->iov_base) {
124648cc565eSKevin Wolf iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len);
12474f4896dbSKevin Wolf if (iov->iov_base == NULL) {
12481f01e50bSPaolo Bonzini r = -ENOMEM;
12491f01e50bSPaolo Bonzini goto out;
12504f4896dbSKevin Wolf }
12510e71be19SStefan Hajnoczi memset(iov->iov_base, 0, iov->iov_len);
12520e71be19SStefan Hajnoczi }
12530e71be19SStefan Hajnoczi }
12540e71be19SStefan Hajnoczi
1255eabba580SStefan Hajnoczi /* Calculate the I/O vector */
1256eabba580SStefan Hajnoczi acb->cur_cluster = offset;
12571b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1258eabba580SStefan Hajnoczi
12591f01e50bSPaolo Bonzini /* Do the actual write. */
12601f01e50bSPaolo Bonzini r = qed_aio_write_main(acb);
12611f01e50bSPaolo Bonzini out:
12621f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock);
12631f01e50bSPaolo Bonzini return r;
1264eabba580SStefan Hajnoczi }
1265eabba580SStefan Hajnoczi
1266eabba580SStefan Hajnoczi /**
1267eabba580SStefan Hajnoczi * Write data cluster
1268eabba580SStefan Hajnoczi *
1269eabba580SStefan Hajnoczi * @opaque: Write request
12700596be7eSKevin Wolf * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
1271eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes
1272eabba580SStefan Hajnoczi * @len: Length in bytes
12731f01e50bSPaolo Bonzini *
12741f01e50bSPaolo Bonzini * Called with table_lock held.
1275eabba580SStefan Hajnoczi */
127688095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
qed_aio_write_data(void * opaque,int ret,uint64_t offset,size_t len)127788095349SEmanuele Giuseppe Esposito qed_aio_write_data(void *opaque, int ret, uint64_t offset, size_t len)
1278eabba580SStefan Hajnoczi {
1279eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque;
1280eabba580SStefan Hajnoczi
1281eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1282eabba580SStefan Hajnoczi
1283eabba580SStefan Hajnoczi acb->find_cluster_ret = ret;
1284eabba580SStefan Hajnoczi
1285eabba580SStefan Hajnoczi switch (ret) {
1286eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND:
12870596be7eSKevin Wolf return qed_aio_write_inplace(acb, offset, len);
1288eabba580SStefan Hajnoczi
1289eabba580SStefan Hajnoczi case QED_CLUSTER_L2:
1290eabba580SStefan Hajnoczi case QED_CLUSTER_L1:
129121df65b6SAnthony Liguori case QED_CLUSTER_ZERO:
12920596be7eSKevin Wolf return qed_aio_write_alloc(acb, len);
1293eabba580SStefan Hajnoczi
1294eabba580SStefan Hajnoczi default:
12950596be7eSKevin Wolf g_assert_not_reached();
1296eabba580SStefan Hajnoczi }
1297eabba580SStefan Hajnoczi }
1298eabba580SStefan Hajnoczi
1299eabba580SStefan Hajnoczi /**
1300eabba580SStefan Hajnoczi * Read data cluster
1301eabba580SStefan Hajnoczi *
1302eabba580SStefan Hajnoczi * @opaque: Read request
13030596be7eSKevin Wolf * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
1304eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes
1305eabba580SStefan Hajnoczi * @len: Length in bytes
13061f01e50bSPaolo Bonzini *
13071f01e50bSPaolo Bonzini * Called with table_lock held.
1308eabba580SStefan Hajnoczi */
1309b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
qed_aio_read_data(void * opaque,int ret,uint64_t offset,size_t len)1310b9b10c35SKevin Wolf qed_aio_read_data(void *opaque, int ret, uint64_t offset, size_t len)
1311eabba580SStefan Hajnoczi {
1312eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque;
1313eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb);
131448cc565eSKevin Wolf BlockDriverState *bs = acb->bs;
13151f01e50bSPaolo Bonzini int r;
13161f01e50bSPaolo Bonzini
13171f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
1318eabba580SStefan Hajnoczi
1319eabba580SStefan Hajnoczi /* Adjust offset into cluster */
1320eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos);
1321eabba580SStefan Hajnoczi
1322eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len);
1323eabba580SStefan Hajnoczi
13241b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
1325eabba580SStefan Hajnoczi
13261f01e50bSPaolo Bonzini /* Handle zero cluster and backing file reads, otherwise read
13271f01e50bSPaolo Bonzini * data cluster directly.
13281f01e50bSPaolo Bonzini */
132921df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) {
13303d9b4925SMichael Tokarev qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
13311f01e50bSPaolo Bonzini r = 0;
133221df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) {
1333365fed51SEric Blake r = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov);
13341f01e50bSPaolo Bonzini } else {
133517362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_AIO);
13361f01e50bSPaolo Bonzini r = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size,
13371f01e50bSPaolo Bonzini &acb->cur_qiov, 0);
1338eabba580SStefan Hajnoczi }
1339eabba580SStefan Hajnoczi
13401f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock);
13411f01e50bSPaolo Bonzini return r;
1342eabba580SStefan Hajnoczi }
1343eabba580SStefan Hajnoczi
1344eabba580SStefan Hajnoczi /**
1345eabba580SStefan Hajnoczi * Begin next I/O or complete the request
1346eabba580SStefan Hajnoczi */
qed_aio_next_io(QEDAIOCB * acb)134788095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK qed_aio_next_io(QEDAIOCB *acb)
1348eabba580SStefan Hajnoczi {
1349eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb);
13500f21b7a1SKevin Wolf uint64_t offset;
13510f21b7a1SKevin Wolf size_t len;
1352dddf8db1SKevin Wolf int ret;
1353eabba580SStefan Hajnoczi
13541f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock);
135501859874SKevin Wolf while (1) {
1356dddf8db1SKevin Wolf trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size);
1357eabba580SStefan Hajnoczi
1358eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size;
1359eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size;
1360eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov);
1361eabba580SStefan Hajnoczi
1362eabba580SStefan Hajnoczi /* Complete request */
1363eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) {
136448cc565eSKevin Wolf ret = 0;
136548cc565eSKevin Wolf break;
1366eabba580SStefan Hajnoczi }
1367eabba580SStefan Hajnoczi
1368eabba580SStefan Hajnoczi /* Find next cluster and start I/O */
13690f21b7a1SKevin Wolf len = acb->end_pos - acb->cur_pos;
13700f21b7a1SKevin Wolf ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset);
13710596be7eSKevin Wolf if (ret < 0) {
137248cc565eSKevin Wolf break;
13730596be7eSKevin Wolf }
13740596be7eSKevin Wolf
13750596be7eSKevin Wolf if (acb->flags & QED_AIOCB_WRITE) {
13760596be7eSKevin Wolf ret = qed_aio_write_data(acb, ret, offset, len);
13770596be7eSKevin Wolf } else {
13780596be7eSKevin Wolf ret = qed_aio_read_data(acb, ret, offset, len);
13790596be7eSKevin Wolf }
13800596be7eSKevin Wolf
13810806c3b5SKevin Wolf if (ret < 0 && ret != -EAGAIN) {
138248cc565eSKevin Wolf break;
138301859874SKevin Wolf }
1384eabba580SStefan Hajnoczi }
1385eabba580SStefan Hajnoczi
138648cc565eSKevin Wolf trace_qed_aio_complete(s, acb, ret);
138748cc565eSKevin Wolf qed_aio_complete(acb);
13881f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
138948cc565eSKevin Wolf return ret;
139089f89709SKevin Wolf }
139189f89709SKevin Wolf
139288095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
qed_co_request(BlockDriverState * bs,int64_t sector_num,QEMUIOVector * qiov,int nb_sectors,int flags)139388095349SEmanuele Giuseppe Esposito qed_co_request(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov,
139488095349SEmanuele Giuseppe Esposito int nb_sectors, int flags)
139589f89709SKevin Wolf {
139648cc565eSKevin Wolf QEDAIOCB acb = {
139748cc565eSKevin Wolf .bs = bs,
139848cc565eSKevin Wolf .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE,
139948cc565eSKevin Wolf .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE,
140048cc565eSKevin Wolf .qiov = qiov,
140148cc565eSKevin Wolf .flags = flags,
140289f89709SKevin Wolf };
140348cc565eSKevin Wolf qemu_iovec_init(&acb.cur_qiov, qiov->niov);
140489f89709SKevin Wolf
140548cc565eSKevin Wolf trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags);
1406eabba580SStefan Hajnoczi
1407eabba580SStefan Hajnoczi /* Start request */
140848cc565eSKevin Wolf return qed_aio_next_io(&acb);
140989f89709SKevin Wolf }
141089f89709SKevin Wolf
14117b1fb72eSKevin Wolf static int coroutine_fn GRAPH_RDLOCK
bdrv_qed_co_readv(BlockDriverState * bs,int64_t sector_num,int nb_sectors,QEMUIOVector * qiov)14127b1fb72eSKevin Wolf bdrv_qed_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
141389f89709SKevin Wolf QEMUIOVector *qiov)
141475411d23SStefan Hajnoczi {
141589f89709SKevin Wolf return qed_co_request(bs, sector_num, qiov, nb_sectors, 0);
141675411d23SStefan Hajnoczi }
141775411d23SStefan Hajnoczi
14187b1fb72eSKevin Wolf static int coroutine_fn GRAPH_RDLOCK
bdrv_qed_co_writev(BlockDriverState * bs,int64_t sector_num,int nb_sectors,QEMUIOVector * qiov,int flags)14197b1fb72eSKevin Wolf bdrv_qed_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1420e18a58b4SEric Blake QEMUIOVector *qiov, int flags)
142175411d23SStefan Hajnoczi {
142289f89709SKevin Wolf return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRITE);
14230e71be19SStefan Hajnoczi }
14240e71be19SStefan Hajnoczi
1425abaf8b75SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
bdrv_qed_co_pwrite_zeroes(BlockDriverState * bs,int64_t offset,int64_t bytes,BdrvRequestFlags flags)1426abaf8b75SKevin Wolf bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
1427aa7bfbffSPeter Lieven BdrvRequestFlags flags)
14280e71be19SStefan Hajnoczi {
1429ef72f76eSStefan Hajnoczi BDRVQEDState *s = bs->opaque;
1430342544f9SVladimir Sementsov-Ogievskiy
1431342544f9SVladimir Sementsov-Ogievskiy /*
1432342544f9SVladimir Sementsov-Ogievskiy * Zero writes start without an I/O buffer. If a buffer becomes necessary
1433342544f9SVladimir Sementsov-Ogievskiy * then it will be allocated during request processing.
1434342544f9SVladimir Sementsov-Ogievskiy */
1435342544f9SVladimir Sementsov-Ogievskiy QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes);
14360e71be19SStefan Hajnoczi
1437f34b2bcfSVladimir Sementsov-Ogievskiy /*
1438f34b2bcfSVladimir Sementsov-Ogievskiy * QED is not prepared for 63bit write-zero requests, so rely on
1439f34b2bcfSVladimir Sementsov-Ogievskiy * max_pwrite_zeroes.
1440f34b2bcfSVladimir Sementsov-Ogievskiy */
1441f34b2bcfSVladimir Sementsov-Ogievskiy assert(bytes <= INT_MAX);
1442f34b2bcfSVladimir Sementsov-Ogievskiy
144349a2e483SEric Blake /* Fall back if the request is not aligned */
144449a2e483SEric Blake if (qed_offset_into_cluster(s, offset) ||
1445f5a5ca79SManos Pitsidianakis qed_offset_into_cluster(s, bytes)) {
1446ef72f76eSStefan Hajnoczi return -ENOTSUP;
1447ef72f76eSStefan Hajnoczi }
1448ef72f76eSStefan Hajnoczi
144989f89709SKevin Wolf return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov,
1450f5a5ca79SManos Pitsidianakis bytes >> BDRV_SECTOR_BITS,
14510e71be19SStefan Hajnoczi QED_AIOCB_WRITE | QED_AIOCB_ZERO);
14520e71be19SStefan Hajnoczi }
14530e71be19SStefan Hajnoczi
145479a55866SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
bdrv_qed_co_truncate(BlockDriverState * bs,int64_t offset,bool exact,PreallocMode prealloc,BdrvRequestFlags flags,Error ** errp)145579a55866SKevin Wolf bdrv_qed_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
145679a55866SKevin Wolf PreallocMode prealloc, BdrvRequestFlags flags,
1457061ca8a3SKevin Wolf Error **errp)
145875411d23SStefan Hajnoczi {
145977a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque;
146077a5a000SStefan Hajnoczi uint64_t old_image_size;
146177a5a000SStefan Hajnoczi int ret;
146277a5a000SStefan Hajnoczi
14638243ccb7SMax Reitz if (prealloc != PREALLOC_MODE_OFF) {
14648243ccb7SMax Reitz error_setg(errp, "Unsupported preallocation mode '%s'",
1465977c736fSMarkus Armbruster PreallocMode_str(prealloc));
14668243ccb7SMax Reitz return -ENOTSUP;
14678243ccb7SMax Reitz }
14688243ccb7SMax Reitz
146977a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size,
147077a5a000SStefan Hajnoczi s->header.table_size)) {
1471f59adb32SMax Reitz error_setg(errp, "Invalid image size specified");
147277a5a000SStefan Hajnoczi return -EINVAL;
147377a5a000SStefan Hajnoczi }
147477a5a000SStefan Hajnoczi
147577a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) {
1476f59adb32SMax Reitz error_setg(errp, "Shrinking images is currently not supported");
147775411d23SStefan Hajnoczi return -ENOTSUP;
147875411d23SStefan Hajnoczi }
147975411d23SStefan Hajnoczi
148077a5a000SStefan Hajnoczi old_image_size = s->header.image_size;
148177a5a000SStefan Hajnoczi s->header.image_size = offset;
148277a5a000SStefan Hajnoczi ret = qed_write_header_sync(s);
148377a5a000SStefan Hajnoczi if (ret < 0) {
148477a5a000SStefan Hajnoczi s->header.image_size = old_image_size;
1485f59adb32SMax Reitz error_setg_errno(errp, -ret, "Failed to update the image size");
148677a5a000SStefan Hajnoczi }
148777a5a000SStefan Hajnoczi return ret;
148877a5a000SStefan Hajnoczi }
148977a5a000SStefan Hajnoczi
bdrv_qed_co_getlength(BlockDriverState * bs)1490c86422c5SEmanuele Giuseppe Esposito static int64_t coroutine_fn bdrv_qed_co_getlength(BlockDriverState *bs)
149175411d23SStefan Hajnoczi {
149275411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque;
149375411d23SStefan Hajnoczi return s->header.image_size;
149475411d23SStefan Hajnoczi }
149575411d23SStefan Hajnoczi
14963d47eb0aSEmanuele Giuseppe Esposito static int coroutine_fn
bdrv_qed_co_get_info(BlockDriverState * bs,BlockDriverInfo * bdi)14973d47eb0aSEmanuele Giuseppe Esposito bdrv_qed_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
149875411d23SStefan Hajnoczi {
149975411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque;
150075411d23SStefan Hajnoczi
150175411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi));
150275411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size;
1503d68dbee8SDong Xu Wang bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
150475411d23SStefan Hajnoczi return 0;
150575411d23SStefan Hajnoczi }
150675411d23SStefan Hajnoczi
1507e2dd2737SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
bdrv_qed_co_change_backing_file(BlockDriverState * bs,const char * backing_file,const char * backing_fmt)1508e2dd2737SKevin Wolf bdrv_qed_co_change_backing_file(BlockDriverState *bs, const char *backing_file,
150975411d23SStefan Hajnoczi const char *backing_fmt)
151075411d23SStefan Hajnoczi {
151175411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque;
151275411d23SStefan Hajnoczi QEDHeader new_header, le_header;
151375411d23SStefan Hajnoczi void *buffer;
151475411d23SStefan Hajnoczi size_t buffer_len, backing_file_len;
151575411d23SStefan Hajnoczi int ret;
151675411d23SStefan Hajnoczi
151775411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are
151875411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not
151975411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely
152075411d23SStefan Hajnoczi * add a new string.
152175411d23SStefan Hajnoczi */
152275411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features &
152375411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) {
152475411d23SStefan Hajnoczi return -ENOTSUP;
152575411d23SStefan Hajnoczi }
152675411d23SStefan Hajnoczi
152775411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header));
152875411d23SStefan Hajnoczi
152975411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE |
153075411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE);
153175411d23SStefan Hajnoczi
153275411d23SStefan Hajnoczi /* Adjust feature flags */
153375411d23SStefan Hajnoczi if (backing_file) {
153475411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE;
153575411d23SStefan Hajnoczi
153675411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) {
153775411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
153875411d23SStefan Hajnoczi }
153975411d23SStefan Hajnoczi }
154075411d23SStefan Hajnoczi
154175411d23SStefan Hajnoczi /* Calculate new header size */
154275411d23SStefan Hajnoczi backing_file_len = 0;
154375411d23SStefan Hajnoczi
154475411d23SStefan Hajnoczi if (backing_file) {
154575411d23SStefan Hajnoczi backing_file_len = strlen(backing_file);
154675411d23SStefan Hajnoczi }
154775411d23SStefan Hajnoczi
154875411d23SStefan Hajnoczi buffer_len = sizeof(new_header);
154975411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len;
155075411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len;
155175411d23SStefan Hajnoczi buffer_len += backing_file_len;
155275411d23SStefan Hajnoczi
155375411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */
155475411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) {
155575411d23SStefan Hajnoczi return -ENOSPC;
155675411d23SStefan Hajnoczi }
155775411d23SStefan Hajnoczi
155875411d23SStefan Hajnoczi /* Prepare new header */
15597267c094SAnthony Liguori buffer = g_malloc(buffer_len);
156075411d23SStefan Hajnoczi
156175411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header);
156275411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header));
156375411d23SStefan Hajnoczi buffer_len = sizeof(le_header);
156475411d23SStefan Hajnoczi
1565feba23b1SPavel Borzenkov if (backing_file) {
156675411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len);
156775411d23SStefan Hajnoczi buffer_len += backing_file_len;
1568feba23b1SPavel Borzenkov }
156975411d23SStefan Hajnoczi
157075411d23SStefan Hajnoczi /* Write new header */
1571e2dd2737SKevin Wolf ret = bdrv_co_pwrite_sync(bs->file, 0, buffer_len, buffer, 0);
15727267c094SAnthony Liguori g_free(buffer);
157375411d23SStefan Hajnoczi if (ret == 0) {
157475411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header));
157575411d23SStefan Hajnoczi }
157675411d23SStefan Hajnoczi return ret;
157775411d23SStefan Hajnoczi }
157875411d23SStefan Hajnoczi
157988095349SEmanuele Giuseppe Esposito static void coroutine_fn GRAPH_RDLOCK
bdrv_qed_co_invalidate_cache(BlockDriverState * bs,Error ** errp)158088095349SEmanuele Giuseppe Esposito bdrv_qed_co_invalidate_cache(BlockDriverState *bs, Error **errp)
1581c82954e5SBenoît Canet {
1582*c66eec90SZhao Liu ERRP_GUARD();
15831f01e50bSPaolo Bonzini BDRVQEDState *s = bs->opaque;
15845a8a30dbSKevin Wolf int ret;
1585c82954e5SBenoît Canet
15861f051dcbSKevin Wolf bdrv_qed_do_close(bs);
15873456a8d1SKevin Wolf
158861c7887eSPaolo Bonzini bdrv_qed_init_state(bs);
15891f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock);
159015ce94a6SVladimir Sementsov-Ogievskiy ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, errp);
15911f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
159215ce94a6SVladimir Sementsov-Ogievskiy if (ret < 0) {
159315ce94a6SVladimir Sementsov-Ogievskiy error_prepend(errp, "Could not reopen qed layer: ");
15945a8a30dbSKevin Wolf }
1595c82954e5SBenoît Canet }
1596c82954e5SBenoît Canet
159788095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
bdrv_qed_co_check(BlockDriverState * bs,BdrvCheckResult * result,BdrvCheckMode fix)159888095349SEmanuele Giuseppe Esposito bdrv_qed_co_check(BlockDriverState *bs, BdrvCheckResult *result,
15994534ff54SKevin Wolf BdrvCheckMode fix)
160075411d23SStefan Hajnoczi {
160101979a98SStefan Hajnoczi BDRVQEDState *s = bs->opaque;
16022fd61638SPaolo Bonzini int ret;
160301979a98SStefan Hajnoczi
16042fd61638SPaolo Bonzini qemu_co_mutex_lock(&s->table_lock);
16052fd61638SPaolo Bonzini ret = qed_check(s, result, !!fix);
16062fd61638SPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock);
16072fd61638SPaolo Bonzini
16082fd61638SPaolo Bonzini return ret;
160975411d23SStefan Hajnoczi }
161075411d23SStefan Hajnoczi
16117ab74849SChunyan Liu static QemuOptsList qed_create_opts = {
16127ab74849SChunyan Liu .name = "qed-create-opts",
16137ab74849SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
16147ab74849SChunyan Liu .desc = {
161575411d23SStefan Hajnoczi {
161675411d23SStefan Hajnoczi .name = BLOCK_OPT_SIZE,
16177ab74849SChunyan Liu .type = QEMU_OPT_SIZE,
16187ab74849SChunyan Liu .help = "Virtual disk size"
16197ab74849SChunyan Liu },
16207ab74849SChunyan Liu {
162175411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FILE,
16227ab74849SChunyan Liu .type = QEMU_OPT_STRING,
162375411d23SStefan Hajnoczi .help = "File name of a base image"
16247ab74849SChunyan Liu },
16257ab74849SChunyan Liu {
162675411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FMT,
16277ab74849SChunyan Liu .type = QEMU_OPT_STRING,
162875411d23SStefan Hajnoczi .help = "Image format of the base image"
16297ab74849SChunyan Liu },
16307ab74849SChunyan Liu {
163175411d23SStefan Hajnoczi .name = BLOCK_OPT_CLUSTER_SIZE,
16327ab74849SChunyan Liu .type = QEMU_OPT_SIZE,
163399cce9faSKevin Wolf .help = "Cluster size (in bytes)",
16347ab74849SChunyan Liu .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE)
16357ab74849SChunyan Liu },
16367ab74849SChunyan Liu {
163775411d23SStefan Hajnoczi .name = BLOCK_OPT_TABLE_SIZE,
16387ab74849SChunyan Liu .type = QEMU_OPT_SIZE,
163975411d23SStefan Hajnoczi .help = "L1/L2 table size (in clusters)"
164075411d23SStefan Hajnoczi },
164175411d23SStefan Hajnoczi { /* end of list */ }
16427ab74849SChunyan Liu }
164375411d23SStefan Hajnoczi };
164475411d23SStefan Hajnoczi
164575411d23SStefan Hajnoczi static BlockDriver bdrv_qed = {
164675411d23SStefan Hajnoczi .format_name = "qed",
164775411d23SStefan Hajnoczi .instance_size = sizeof(BDRVQEDState),
16487ab74849SChunyan Liu .create_opts = &qed_create_opts,
1649d67066d8SMax Reitz .is_format = true,
16508ee79e70SKevin Wolf .supports_backing = true,
165175411d23SStefan Hajnoczi
165275411d23SStefan Hajnoczi .bdrv_probe = bdrv_qed_probe,
165375411d23SStefan Hajnoczi .bdrv_open = bdrv_qed_open,
165475411d23SStefan Hajnoczi .bdrv_close = bdrv_qed_close,
1655f9cb20f1SJeff Cody .bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
165669dca43dSMax Reitz .bdrv_child_perm = bdrv_default_perms,
1657959355a4SKevin Wolf .bdrv_co_create = bdrv_qed_co_create,
1658efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = bdrv_qed_co_create_opts,
16593ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1,
1660b8d739fdSEric Blake .bdrv_co_block_status = bdrv_qed_co_block_status,
166189f89709SKevin Wolf .bdrv_co_readv = bdrv_qed_co_readv,
166289f89709SKevin Wolf .bdrv_co_writev = bdrv_qed_co_writev,
166349a2e483SEric Blake .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes,
1664061ca8a3SKevin Wolf .bdrv_co_truncate = bdrv_qed_co_truncate,
1665c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = bdrv_qed_co_getlength,
16663d47eb0aSEmanuele Giuseppe Esposito .bdrv_co_get_info = bdrv_qed_co_get_info,
1667d34682cdSKevin Wolf .bdrv_refresh_limits = bdrv_qed_refresh_limits,
1668e2dd2737SKevin Wolf .bdrv_co_change_backing_file = bdrv_qed_co_change_backing_file,
16692b148f39SPaolo Bonzini .bdrv_co_invalidate_cache = bdrv_qed_co_invalidate_cache,
16702fd61638SPaolo Bonzini .bdrv_co_check = bdrv_qed_co_check,
1671a8c868c3SStefan Hajnoczi .bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
1672a8c868c3SStefan Hajnoczi .bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
16735e8ac217SKevin Wolf .bdrv_drain_begin = bdrv_qed_drain_begin,
167475411d23SStefan Hajnoczi };
167575411d23SStefan Hajnoczi
bdrv_qed_init(void)167675411d23SStefan Hajnoczi static void bdrv_qed_init(void)
167775411d23SStefan Hajnoczi {
167875411d23SStefan Hajnoczi bdrv_register(&bdrv_qed);
167975411d23SStefan Hajnoczi }
168075411d23SStefan Hajnoczi
168175411d23SStefan Hajnoczi block_init(bdrv_qed_init);
1682