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" 16da34e65cSMarkus Armbruster #include "qapi/error.h" 171de7afc9SPaolo Bonzini #include "qemu/timer.h" 1858369e22SPaolo Bonzini #include "qemu/bswap.h" 19922a01a0SMarkus Armbruster #include "qemu/option.h" 20eabba580SStefan Hajnoczi #include "trace.h" 2175411d23SStefan Hajnoczi #include "qed.h" 228a56fdadSKevin Wolf #include "sysemu/block-backend.h" 23959355a4SKevin Wolf #include "qapi/qmp/qdict.h" 24959355a4SKevin Wolf #include "qapi/qobject-input-visitor.h" 25959355a4SKevin Wolf #include "qapi/qapi-visit-block-core.h" 26959355a4SKevin Wolf 27959355a4SKevin Wolf static QemuOptsList qed_create_opts; 2875411d23SStefan Hajnoczi 2975411d23SStefan Hajnoczi static int bdrv_qed_probe(const uint8_t *buf, int buf_size, 3075411d23SStefan Hajnoczi const char *filename) 3175411d23SStefan Hajnoczi { 3275411d23SStefan Hajnoczi const QEDHeader *header = (const QEDHeader *)buf; 3375411d23SStefan Hajnoczi 3475411d23SStefan Hajnoczi if (buf_size < sizeof(*header)) { 3575411d23SStefan Hajnoczi return 0; 3675411d23SStefan Hajnoczi } 3775411d23SStefan Hajnoczi if (le32_to_cpu(header->magic) != QED_MAGIC) { 3875411d23SStefan Hajnoczi return 0; 3975411d23SStefan Hajnoczi } 4075411d23SStefan Hajnoczi return 100; 4175411d23SStefan Hajnoczi } 4275411d23SStefan Hajnoczi 4375411d23SStefan Hajnoczi /** 4475411d23SStefan Hajnoczi * Check whether an image format is raw 4575411d23SStefan Hajnoczi * 4675411d23SStefan Hajnoczi * @fmt: Backing file format, may be NULL 4775411d23SStefan Hajnoczi */ 4875411d23SStefan Hajnoczi static bool qed_fmt_is_raw(const char *fmt) 4975411d23SStefan Hajnoczi { 5075411d23SStefan Hajnoczi return fmt && strcmp(fmt, "raw") == 0; 5175411d23SStefan Hajnoczi } 5275411d23SStefan Hajnoczi 5375411d23SStefan Hajnoczi static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) 5475411d23SStefan Hajnoczi { 5575411d23SStefan Hajnoczi cpu->magic = le32_to_cpu(le->magic); 5675411d23SStefan Hajnoczi cpu->cluster_size = le32_to_cpu(le->cluster_size); 5775411d23SStefan Hajnoczi cpu->table_size = le32_to_cpu(le->table_size); 5875411d23SStefan Hajnoczi cpu->header_size = le32_to_cpu(le->header_size); 5975411d23SStefan Hajnoczi cpu->features = le64_to_cpu(le->features); 6075411d23SStefan Hajnoczi cpu->compat_features = le64_to_cpu(le->compat_features); 6175411d23SStefan Hajnoczi cpu->autoclear_features = le64_to_cpu(le->autoclear_features); 6275411d23SStefan Hajnoczi cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); 6375411d23SStefan Hajnoczi cpu->image_size = le64_to_cpu(le->image_size); 6475411d23SStefan Hajnoczi cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); 6575411d23SStefan Hajnoczi cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); 6675411d23SStefan Hajnoczi } 6775411d23SStefan Hajnoczi 6875411d23SStefan Hajnoczi static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) 6975411d23SStefan Hajnoczi { 7075411d23SStefan Hajnoczi le->magic = cpu_to_le32(cpu->magic); 7175411d23SStefan Hajnoczi le->cluster_size = cpu_to_le32(cpu->cluster_size); 7275411d23SStefan Hajnoczi le->table_size = cpu_to_le32(cpu->table_size); 7375411d23SStefan Hajnoczi le->header_size = cpu_to_le32(cpu->header_size); 7475411d23SStefan Hajnoczi le->features = cpu_to_le64(cpu->features); 7575411d23SStefan Hajnoczi le->compat_features = cpu_to_le64(cpu->compat_features); 7675411d23SStefan Hajnoczi le->autoclear_features = cpu_to_le64(cpu->autoclear_features); 7775411d23SStefan Hajnoczi le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); 7875411d23SStefan Hajnoczi le->image_size = cpu_to_le64(cpu->image_size); 7975411d23SStefan Hajnoczi le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); 8075411d23SStefan Hajnoczi le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); 8175411d23SStefan Hajnoczi } 8275411d23SStefan Hajnoczi 83b10170acSStefan Hajnoczi int qed_write_header_sync(BDRVQEDState *s) 8475411d23SStefan Hajnoczi { 8575411d23SStefan Hajnoczi QEDHeader le; 8675411d23SStefan Hajnoczi int ret; 8775411d23SStefan Hajnoczi 8875411d23SStefan Hajnoczi qed_header_cpu_to_le(&s->header, &le); 89d9ca2ea2SKevin Wolf ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le)); 9075411d23SStefan Hajnoczi if (ret != sizeof(le)) { 9175411d23SStefan Hajnoczi return ret; 9275411d23SStefan Hajnoczi } 9375411d23SStefan Hajnoczi return 0; 9475411d23SStefan Hajnoczi } 9575411d23SStefan Hajnoczi 9601979a98SStefan Hajnoczi /** 9701979a98SStefan Hajnoczi * Update header in-place (does not rewrite backing filename or other strings) 9801979a98SStefan Hajnoczi * 9901979a98SStefan Hajnoczi * This function only updates known header fields in-place and does not affect 10001979a98SStefan Hajnoczi * extra data after the QED header. 1011f01e50bSPaolo Bonzini * 1021f01e50bSPaolo Bonzini * No new allocating reqs can start while this function runs. 10301979a98SStefan Hajnoczi */ 10487f0d882SKevin Wolf static int coroutine_fn qed_write_header(BDRVQEDState *s) 10501979a98SStefan Hajnoczi { 10601979a98SStefan Hajnoczi /* We must write full sectors for O_DIRECT but cannot necessarily generate 10701979a98SStefan Hajnoczi * the data following the header if an unrecognized compat feature is 10801979a98SStefan Hajnoczi * active. Therefore, first read the sectors containing the header, update 10901979a98SStefan Hajnoczi * them, and write back. 11001979a98SStefan Hajnoczi */ 11101979a98SStefan Hajnoczi 112c41a73ffSLaurent Vivier int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE); 11301979a98SStefan Hajnoczi size_t len = nsectors * BDRV_SECTOR_SIZE; 1147076309aSKevin Wolf uint8_t *buf; 1157076309aSKevin Wolf struct iovec iov; 1167076309aSKevin Wolf QEMUIOVector qiov; 1177076309aSKevin Wolf int ret; 11801979a98SStefan Hajnoczi 1191f01e50bSPaolo Bonzini assert(s->allocating_acb || s->allocating_write_reqs_plugged); 1201f01e50bSPaolo Bonzini 1217076309aSKevin Wolf buf = qemu_blockalign(s->bs, len); 1227076309aSKevin Wolf iov = (struct iovec) { 1237076309aSKevin Wolf .iov_base = buf, 1247076309aSKevin Wolf .iov_len = len, 1257076309aSKevin Wolf }; 1267076309aSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 12701979a98SStefan Hajnoczi 1280f714ec7SKevin Wolf ret = bdrv_co_preadv(s->bs->file, 0, qiov.size, &qiov, 0); 1297076309aSKevin Wolf if (ret < 0) { 1307076309aSKevin Wolf goto out; 1317076309aSKevin Wolf } 1327076309aSKevin Wolf 1337076309aSKevin Wolf /* Update header */ 1347076309aSKevin Wolf qed_header_cpu_to_le(&s->header, (QEDHeader *) buf); 1357076309aSKevin Wolf 1360f714ec7SKevin Wolf ret = bdrv_co_pwritev(s->bs->file, 0, qiov.size, &qiov, 0); 1377076309aSKevin Wolf if (ret < 0) { 1387076309aSKevin Wolf goto out; 1397076309aSKevin Wolf } 1407076309aSKevin Wolf 1417076309aSKevin Wolf ret = 0; 1427076309aSKevin Wolf out: 1437076309aSKevin Wolf qemu_vfree(buf); 144f13d712bSKevin Wolf return ret; 14501979a98SStefan Hajnoczi } 14601979a98SStefan Hajnoczi 14775411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 14875411d23SStefan Hajnoczi { 14975411d23SStefan Hajnoczi uint64_t table_entries; 15075411d23SStefan Hajnoczi uint64_t l2_size; 15175411d23SStefan Hajnoczi 15275411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t); 15375411d23SStefan Hajnoczi l2_size = table_entries * cluster_size; 15475411d23SStefan Hajnoczi 15575411d23SStefan Hajnoczi return l2_size * table_entries; 15675411d23SStefan Hajnoczi } 15775411d23SStefan Hajnoczi 15875411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size) 15975411d23SStefan Hajnoczi { 16075411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE || 16175411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) { 16275411d23SStefan Hajnoczi return false; 16375411d23SStefan Hajnoczi } 16475411d23SStefan Hajnoczi if (cluster_size & (cluster_size - 1)) { 16575411d23SStefan Hajnoczi return false; /* not power of 2 */ 16675411d23SStefan Hajnoczi } 16775411d23SStefan Hajnoczi return true; 16875411d23SStefan Hajnoczi } 16975411d23SStefan Hajnoczi 17075411d23SStefan Hajnoczi static bool qed_is_table_size_valid(uint32_t table_size) 17175411d23SStefan Hajnoczi { 17275411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE || 17375411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) { 17475411d23SStefan Hajnoczi return false; 17575411d23SStefan Hajnoczi } 17675411d23SStefan Hajnoczi if (table_size & (table_size - 1)) { 17775411d23SStefan Hajnoczi return false; /* not power of 2 */ 17875411d23SStefan Hajnoczi } 17975411d23SStefan Hajnoczi return true; 18075411d23SStefan Hajnoczi } 18175411d23SStefan Hajnoczi 18275411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 18375411d23SStefan Hajnoczi uint32_t table_size) 18475411d23SStefan Hajnoczi { 18575411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) { 18675411d23SStefan Hajnoczi return false; /* not multiple of sector size */ 18775411d23SStefan Hajnoczi } 18875411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) { 18975411d23SStefan Hajnoczi return false; /* image is too large */ 19075411d23SStefan Hajnoczi } 19175411d23SStefan Hajnoczi return true; 19275411d23SStefan Hajnoczi } 19375411d23SStefan Hajnoczi 19475411d23SStefan Hajnoczi /** 19575411d23SStefan Hajnoczi * Read a string of known length from the image file 19675411d23SStefan Hajnoczi * 19775411d23SStefan Hajnoczi * @file: Image file 19875411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes 19975411d23SStefan Hajnoczi * @n: String length in bytes 20075411d23SStefan Hajnoczi * @buf: Destination buffer 20175411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes 20275411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure 20375411d23SStefan Hajnoczi * 20475411d23SStefan Hajnoczi * The string is NUL-terminated. 20575411d23SStefan Hajnoczi */ 206cf2ab8fcSKevin Wolf static int qed_read_string(BdrvChild *file, uint64_t offset, size_t n, 20775411d23SStefan Hajnoczi char *buf, size_t buflen) 20875411d23SStefan Hajnoczi { 20975411d23SStefan Hajnoczi int ret; 21075411d23SStefan Hajnoczi if (n >= buflen) { 21175411d23SStefan Hajnoczi return -EINVAL; 21275411d23SStefan Hajnoczi } 21375411d23SStefan Hajnoczi ret = bdrv_pread(file, offset, buf, n); 21475411d23SStefan Hajnoczi if (ret < 0) { 21575411d23SStefan Hajnoczi return ret; 21675411d23SStefan Hajnoczi } 21775411d23SStefan Hajnoczi buf[n] = '\0'; 21875411d23SStefan Hajnoczi return 0; 21975411d23SStefan Hajnoczi } 22075411d23SStefan Hajnoczi 221eabba580SStefan Hajnoczi /** 222eabba580SStefan Hajnoczi * Allocate new clusters 223eabba580SStefan Hajnoczi * 224eabba580SStefan Hajnoczi * @s: QED state 225eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate 226eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster 227eabba580SStefan Hajnoczi * 228eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be 229eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image 230eabba580SStefan Hajnoczi * file. 2311f01e50bSPaolo Bonzini * 2321f01e50bSPaolo Bonzini * Called with table_lock held. 233eabba580SStefan Hajnoczi */ 234eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 235eabba580SStefan Hajnoczi { 236eabba580SStefan Hajnoczi uint64_t offset = s->file_size; 237eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size; 238eabba580SStefan Hajnoczi return offset; 239eabba580SStefan Hajnoczi } 240eabba580SStefan Hajnoczi 241298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s) 242298800caSStefan Hajnoczi { 243298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */ 244298800caSStefan Hajnoczi return qemu_blockalign(s->bs, 245298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size); 246298800caSStefan Hajnoczi } 247298800caSStefan Hajnoczi 248eabba580SStefan Hajnoczi /** 249eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table 2501f01e50bSPaolo Bonzini * 2511f01e50bSPaolo Bonzini * Called with table_lock held. 252eabba580SStefan Hajnoczi */ 253eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 254eabba580SStefan Hajnoczi { 255eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 256eabba580SStefan Hajnoczi 257eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s); 258eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 259eabba580SStefan Hajnoczi 260eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0, 261eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size); 262eabba580SStefan Hajnoczi return l2_table; 263eabba580SStefan Hajnoczi } 264eabba580SStefan Hajnoczi 2651f01e50bSPaolo Bonzini static bool qed_plug_allocating_write_reqs(BDRVQEDState *s) 2666f321e93SStefan Hajnoczi { 2671f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 2681f01e50bSPaolo Bonzini 2691f01e50bSPaolo Bonzini /* No reentrancy is allowed. */ 2706f321e93SStefan Hajnoczi assert(!s->allocating_write_reqs_plugged); 2711f01e50bSPaolo Bonzini if (s->allocating_acb != NULL) { 2721f01e50bSPaolo Bonzini /* Another allocating write came concurrently. This cannot happen 273f8ea8dacSManos Pitsidianakis * from bdrv_qed_co_drain_begin, but it can happen when the timer runs. 2741f01e50bSPaolo Bonzini */ 2751f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 2761f01e50bSPaolo Bonzini return false; 2771f01e50bSPaolo Bonzini } 2786f321e93SStefan Hajnoczi 2796f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = true; 2801f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 2811f01e50bSPaolo Bonzini return true; 2826f321e93SStefan Hajnoczi } 2836f321e93SStefan Hajnoczi 2846f321e93SStefan Hajnoczi static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) 2856f321e93SStefan Hajnoczi { 2861f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 2876f321e93SStefan Hajnoczi assert(s->allocating_write_reqs_plugged); 2886f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = false; 2891f01e50bSPaolo Bonzini qemu_co_queue_next(&s->allocating_write_reqs); 2901f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 2916f321e93SStefan Hajnoczi } 2926f321e93SStefan Hajnoczi 29387f0d882SKevin Wolf static void coroutine_fn qed_need_check_timer_entry(void *opaque) 2946f321e93SStefan Hajnoczi { 2956f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 296c0e8f989SKevin Wolf int ret; 2976f321e93SStefan Hajnoczi 2986f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s); 2996f321e93SStefan Hajnoczi 3001f01e50bSPaolo Bonzini if (!qed_plug_allocating_write_reqs(s)) { 3011f01e50bSPaolo Bonzini return; 3021f01e50bSPaolo Bonzini } 3036f321e93SStefan Hajnoczi 3046f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */ 305c0e8f989SKevin Wolf ret = bdrv_co_flush(s->bs->file->bs); 306c0e8f989SKevin Wolf if (ret < 0) { 307c0e8f989SKevin Wolf qed_unplug_allocating_write_reqs(s); 308c0e8f989SKevin Wolf return; 309c0e8f989SKevin Wolf } 310c0e8f989SKevin Wolf 311c0e8f989SKevin Wolf s->header.features &= ~QED_F_NEED_CHECK; 312c0e8f989SKevin Wolf ret = qed_write_header(s); 313c0e8f989SKevin Wolf (void) ret; 314c0e8f989SKevin Wolf 315c0e8f989SKevin Wolf qed_unplug_allocating_write_reqs(s); 316c0e8f989SKevin Wolf 317c0e8f989SKevin Wolf ret = bdrv_co_flush(s->bs); 318c0e8f989SKevin Wolf (void) ret; 319c0e8f989SKevin Wolf } 320c0e8f989SKevin Wolf 321c0e8f989SKevin Wolf static void qed_need_check_timer_cb(void *opaque) 322c0e8f989SKevin Wolf { 323c0e8f989SKevin Wolf Coroutine *co = qemu_coroutine_create(qed_need_check_timer_entry, opaque); 324c0e8f989SKevin Wolf qemu_coroutine_enter(co); 3252f47da5fSPaolo Bonzini } 3262f47da5fSPaolo Bonzini 3276f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s) 3286f321e93SStefan Hajnoczi { 3296f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s); 3306f321e93SStefan Hajnoczi 331bc72ad67SAlex Bligh /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for 3326f321e93SStefan Hajnoczi * migration. 3336f321e93SStefan Hajnoczi */ 334bc72ad67SAlex Bligh timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 33573bcb24dSRutuja Shah NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT); 3366f321e93SStefan Hajnoczi } 3376f321e93SStefan Hajnoczi 3386f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */ 3396f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s) 3406f321e93SStefan Hajnoczi { 3416f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s); 342bc72ad67SAlex Bligh timer_del(s->need_check_timer); 3436f321e93SStefan Hajnoczi } 3446f321e93SStefan Hajnoczi 345a8c868c3SStefan Hajnoczi static void bdrv_qed_detach_aio_context(BlockDriverState *bs) 346a8c868c3SStefan Hajnoczi { 347a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 348a8c868c3SStefan Hajnoczi 349a8c868c3SStefan Hajnoczi qed_cancel_need_check_timer(s); 350a8c868c3SStefan Hajnoczi timer_free(s->need_check_timer); 351a8c868c3SStefan Hajnoczi } 352a8c868c3SStefan Hajnoczi 353a8c868c3SStefan Hajnoczi static void bdrv_qed_attach_aio_context(BlockDriverState *bs, 354a8c868c3SStefan Hajnoczi AioContext *new_context) 355a8c868c3SStefan Hajnoczi { 356a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 357a8c868c3SStefan Hajnoczi 358a8c868c3SStefan Hajnoczi s->need_check_timer = aio_timer_new(new_context, 359a8c868c3SStefan Hajnoczi QEMU_CLOCK_VIRTUAL, SCALE_NS, 360a8c868c3SStefan Hajnoczi qed_need_check_timer_cb, s); 361a8c868c3SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 362a8c868c3SStefan Hajnoczi qed_start_need_check_timer(s); 363a8c868c3SStefan Hajnoczi } 364a8c868c3SStefan Hajnoczi } 365a8c868c3SStefan Hajnoczi 366f8ea8dacSManos Pitsidianakis static void coroutine_fn bdrv_qed_co_drain_begin(BlockDriverState *bs) 3676653a73dSFam Zheng { 3686653a73dSFam Zheng BDRVQEDState *s = bs->opaque; 3696653a73dSFam Zheng 3706653a73dSFam Zheng /* Fire the timer immediately in order to start doing I/O as soon as the 3716653a73dSFam Zheng * header is flushed. 3726653a73dSFam Zheng */ 3736653a73dSFam Zheng if (s->need_check_timer && timer_pending(s->need_check_timer)) { 3746653a73dSFam Zheng qed_cancel_need_check_timer(s); 37561124f03SPaolo Bonzini qed_need_check_timer_entry(s); 3766653a73dSFam Zheng } 3776653a73dSFam Zheng } 3786653a73dSFam Zheng 37961c7887eSPaolo Bonzini static void bdrv_qed_init_state(BlockDriverState *bs) 38061c7887eSPaolo Bonzini { 38161c7887eSPaolo Bonzini BDRVQEDState *s = bs->opaque; 38261c7887eSPaolo Bonzini 38361c7887eSPaolo Bonzini memset(s, 0, sizeof(BDRVQEDState)); 38461c7887eSPaolo Bonzini s->bs = bs; 3851f01e50bSPaolo Bonzini qemu_co_mutex_init(&s->table_lock); 38661c7887eSPaolo Bonzini qemu_co_queue_init(&s->allocating_write_reqs); 38761c7887eSPaolo Bonzini } 38861c7887eSPaolo Bonzini 3899fb4dfc5SPaolo Bonzini /* Called with table_lock held. */ 3909fb4dfc5SPaolo Bonzini static int coroutine_fn bdrv_qed_do_open(BlockDriverState *bs, QDict *options, 3919fb4dfc5SPaolo Bonzini int flags, Error **errp) 39275411d23SStefan Hajnoczi { 39375411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 39475411d23SStefan Hajnoczi QEDHeader le_header; 39575411d23SStefan Hajnoczi int64_t file_size; 39675411d23SStefan Hajnoczi int ret; 39775411d23SStefan Hajnoczi 398cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); 39975411d23SStefan Hajnoczi if (ret < 0) { 40075411d23SStefan Hajnoczi return ret; 40175411d23SStefan Hajnoczi } 40275411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header); 40375411d23SStefan Hajnoczi 40475411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) { 40576abe407SPaolo Bonzini error_setg(errp, "Image not in QED format"); 40676abe407SPaolo Bonzini return -EINVAL; 40775411d23SStefan Hajnoczi } 40875411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) { 40910b758e8SKevin Wolf /* image uses unsupported feature bits */ 410a55448b3SMax Reitz error_setg(errp, "Unsupported QED features: %" PRIx64, 41110b758e8SKevin Wolf s->header.features & ~QED_FEATURE_MASK); 41210b758e8SKevin Wolf return -ENOTSUP; 41375411d23SStefan Hajnoczi } 41475411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 41575411d23SStefan Hajnoczi return -EINVAL; 41675411d23SStefan Hajnoczi } 41775411d23SStefan Hajnoczi 41875411d23SStefan Hajnoczi /* Round down file size to the last cluster */ 4199a4f4c31SKevin Wolf file_size = bdrv_getlength(bs->file->bs); 42075411d23SStefan Hajnoczi if (file_size < 0) { 42175411d23SStefan Hajnoczi return file_size; 42275411d23SStefan Hajnoczi } 42375411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size); 42475411d23SStefan Hajnoczi 42575411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) { 42675411d23SStefan Hajnoczi return -EINVAL; 42775411d23SStefan Hajnoczi } 42875411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size, 42975411d23SStefan Hajnoczi s->header.cluster_size, 43075411d23SStefan Hajnoczi s->header.table_size)) { 43175411d23SStefan Hajnoczi return -EINVAL; 43275411d23SStefan Hajnoczi } 43375411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 43475411d23SStefan Hajnoczi return -EINVAL; 43575411d23SStefan Hajnoczi } 43675411d23SStefan Hajnoczi 43775411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) / 43875411d23SStefan Hajnoczi sizeof(uint64_t); 439786a4ea8SStefan Hajnoczi s->l2_shift = ctz32(s->header.cluster_size); 44075411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1; 441786a4ea8SStefan Hajnoczi s->l1_shift = s->l2_shift + ctz32(s->table_nelems); 44275411d23SStefan Hajnoczi 4430adfa1edSStefan Hajnoczi /* Header size calculation must not overflow uint32_t */ 4440adfa1edSStefan Hajnoczi if (s->header.header_size > UINT32_MAX / s->header.cluster_size) { 4450adfa1edSStefan Hajnoczi return -EINVAL; 4460adfa1edSStefan Hajnoczi } 4470adfa1edSStefan Hajnoczi 44875411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) { 44975411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset + 45075411d23SStefan Hajnoczi s->header.backing_filename_size > 45175411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) { 45275411d23SStefan Hajnoczi return -EINVAL; 45375411d23SStefan Hajnoczi } 45475411d23SStefan Hajnoczi 455cf2ab8fcSKevin Wolf ret = qed_read_string(bs->file, s->header.backing_filename_offset, 45675411d23SStefan Hajnoczi s->header.backing_filename_size, bs->backing_file, 45775411d23SStefan Hajnoczi sizeof(bs->backing_file)); 45875411d23SStefan Hajnoczi if (ret < 0) { 45975411d23SStefan Hajnoczi return ret; 46075411d23SStefan Hajnoczi } 46175411d23SStefan Hajnoczi 46275411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 46375411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 46475411d23SStefan Hajnoczi } 46575411d23SStefan Hajnoczi } 46675411d23SStefan Hajnoczi 46775411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards 46875411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older 46975411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is 47075411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear 47175411d23SStefan Hajnoczi * feature is no longer valid. 47275411d23SStefan Hajnoczi */ 47375411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 47404c01a5cSKevin Wolf !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) { 47575411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 47675411d23SStefan Hajnoczi 47775411d23SStefan Hajnoczi ret = qed_write_header_sync(s); 47875411d23SStefan Hajnoczi if (ret) { 47975411d23SStefan Hajnoczi return ret; 48075411d23SStefan Hajnoczi } 48175411d23SStefan Hajnoczi 48275411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */ 4839a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 48475411d23SStefan Hajnoczi } 48575411d23SStefan Hajnoczi 486298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s); 487298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache); 488298800caSStefan Hajnoczi 489298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s); 490298800caSStefan Hajnoczi if (ret) { 49101979a98SStefan Hajnoczi goto out; 49201979a98SStefan Hajnoczi } 49301979a98SStefan Hajnoczi 49401979a98SStefan Hajnoczi /* If image was not closed cleanly, check consistency */ 495058f8f16SStefan Hajnoczi if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) { 49601979a98SStefan Hajnoczi /* Read-only images cannot be fixed. There is no risk of corruption 49701979a98SStefan Hajnoczi * since write operations are not possible. Therefore, allow 49801979a98SStefan Hajnoczi * potentially inconsistent images to be opened read-only. This can 49901979a98SStefan Hajnoczi * aid data recovery from an otherwise inconsistent image. 50001979a98SStefan Hajnoczi */ 5019a4f4c31SKevin Wolf if (!bdrv_is_read_only(bs->file->bs) && 50204c01a5cSKevin Wolf !(flags & BDRV_O_INACTIVE)) { 50301979a98SStefan Hajnoczi BdrvCheckResult result = {0}; 50401979a98SStefan Hajnoczi 50501979a98SStefan Hajnoczi ret = qed_check(s, &result, true); 5066f321e93SStefan Hajnoczi if (ret) { 5076f321e93SStefan Hajnoczi goto out; 5086f321e93SStefan Hajnoczi } 50901979a98SStefan Hajnoczi } 51001979a98SStefan Hajnoczi } 51101979a98SStefan Hajnoczi 512a8c868c3SStefan Hajnoczi bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs)); 5136f321e93SStefan Hajnoczi 51401979a98SStefan Hajnoczi out: 51501979a98SStefan Hajnoczi if (ret) { 516298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 517298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 518298800caSStefan Hajnoczi } 51975411d23SStefan Hajnoczi return ret; 52075411d23SStefan Hajnoczi } 52175411d23SStefan Hajnoczi 5229fb4dfc5SPaolo Bonzini typedef struct QEDOpenCo { 5239fb4dfc5SPaolo Bonzini BlockDriverState *bs; 5249fb4dfc5SPaolo Bonzini QDict *options; 5259fb4dfc5SPaolo Bonzini int flags; 5269fb4dfc5SPaolo Bonzini Error **errp; 5279fb4dfc5SPaolo Bonzini int ret; 5289fb4dfc5SPaolo Bonzini } QEDOpenCo; 5299fb4dfc5SPaolo Bonzini 5309fb4dfc5SPaolo Bonzini static void coroutine_fn bdrv_qed_open_entry(void *opaque) 5319fb4dfc5SPaolo Bonzini { 5329fb4dfc5SPaolo Bonzini QEDOpenCo *qoc = opaque; 5339fb4dfc5SPaolo Bonzini BDRVQEDState *s = qoc->bs->opaque; 5349fb4dfc5SPaolo Bonzini 5359fb4dfc5SPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 5369fb4dfc5SPaolo Bonzini qoc->ret = bdrv_qed_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp); 5379fb4dfc5SPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 5389fb4dfc5SPaolo Bonzini } 5399fb4dfc5SPaolo Bonzini 5404e4bf5c4SKevin Wolf static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags, 5414e4bf5c4SKevin Wolf Error **errp) 5424e4bf5c4SKevin Wolf { 5439fb4dfc5SPaolo Bonzini QEDOpenCo qoc = { 5449fb4dfc5SPaolo Bonzini .bs = bs, 5459fb4dfc5SPaolo Bonzini .options = options, 5469fb4dfc5SPaolo Bonzini .flags = flags, 5479fb4dfc5SPaolo Bonzini .errp = errp, 5489fb4dfc5SPaolo Bonzini .ret = -EINPROGRESS 5499fb4dfc5SPaolo Bonzini }; 5509fb4dfc5SPaolo Bonzini 5514e4bf5c4SKevin Wolf bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, 5524e4bf5c4SKevin Wolf false, errp); 5534e4bf5c4SKevin Wolf if (!bs->file) { 5544e4bf5c4SKevin Wolf return -EINVAL; 5554e4bf5c4SKevin Wolf } 5564e4bf5c4SKevin Wolf 55761c7887eSPaolo Bonzini bdrv_qed_init_state(bs); 5589fb4dfc5SPaolo Bonzini if (qemu_in_coroutine()) { 5599fb4dfc5SPaolo Bonzini bdrv_qed_open_entry(&qoc); 5609fb4dfc5SPaolo Bonzini } else { 5619fb4dfc5SPaolo Bonzini qemu_coroutine_enter(qemu_coroutine_create(bdrv_qed_open_entry, &qoc)); 5629fb4dfc5SPaolo Bonzini BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS); 5639fb4dfc5SPaolo Bonzini } 5649fb4dfc5SPaolo Bonzini BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS); 5659fb4dfc5SPaolo Bonzini return qoc.ret; 5664e4bf5c4SKevin Wolf } 5674e4bf5c4SKevin Wolf 5683baca891SKevin Wolf static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp) 569d34682cdSKevin Wolf { 570d34682cdSKevin Wolf BDRVQEDState *s = bs->opaque; 571d34682cdSKevin Wolf 572cf081fcaSEric Blake bs->bl.pwrite_zeroes_alignment = s->header.cluster_size; 573d34682cdSKevin Wolf } 574d34682cdSKevin Wolf 575f9cb20f1SJeff Cody /* We have nothing to do for QED reopen, stubs just return 576f9cb20f1SJeff Cody * success */ 577f9cb20f1SJeff Cody static int bdrv_qed_reopen_prepare(BDRVReopenState *state, 578f9cb20f1SJeff Cody BlockReopenQueue *queue, Error **errp) 579f9cb20f1SJeff Cody { 580f9cb20f1SJeff Cody return 0; 581f9cb20f1SJeff Cody } 582f9cb20f1SJeff Cody 58375411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 58475411d23SStefan Hajnoczi { 585298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 586298800caSStefan Hajnoczi 587a8c868c3SStefan Hajnoczi bdrv_qed_detach_aio_context(bs); 5886f321e93SStefan Hajnoczi 58901979a98SStefan Hajnoczi /* Ensure writes reach stable storage */ 5909a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 59101979a98SStefan Hajnoczi 59201979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */ 59301979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 59401979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 59501979a98SStefan Hajnoczi qed_write_header_sync(s); 59601979a98SStefan Hajnoczi } 59701979a98SStefan Hajnoczi 598298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 599298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 60075411d23SStefan Hajnoczi } 60175411d23SStefan Hajnoczi 602959355a4SKevin Wolf static int coroutine_fn bdrv_qed_co_create(BlockdevCreateOptions *opts, 603959355a4SKevin Wolf Error **errp) 60475411d23SStefan Hajnoczi { 605959355a4SKevin Wolf BlockdevCreateOptionsQed *qed_opts; 606959355a4SKevin Wolf BlockBackend *blk = NULL; 607959355a4SKevin Wolf BlockDriverState *bs = NULL; 608959355a4SKevin Wolf 609959355a4SKevin Wolf QEDHeader header; 61075411d23SStefan Hajnoczi QEDHeader le_header; 61175411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 612959355a4SKevin Wolf size_t l1_size; 61375411d23SStefan Hajnoczi int ret = 0; 61475411d23SStefan Hajnoczi 615959355a4SKevin Wolf assert(opts->driver == BLOCKDEV_DRIVER_QED); 616959355a4SKevin Wolf qed_opts = &opts->u.qed; 617959355a4SKevin Wolf 618959355a4SKevin Wolf /* Validate options and set default values */ 619959355a4SKevin Wolf if (!qed_opts->has_cluster_size) { 620959355a4SKevin Wolf qed_opts->cluster_size = QED_DEFAULT_CLUSTER_SIZE; 621959355a4SKevin Wolf } 622959355a4SKevin Wolf if (!qed_opts->has_table_size) { 623959355a4SKevin Wolf qed_opts->table_size = QED_DEFAULT_TABLE_SIZE; 62475411d23SStefan Hajnoczi } 62575411d23SStefan Hajnoczi 626959355a4SKevin Wolf if (!qed_is_cluster_size_valid(qed_opts->cluster_size)) { 627959355a4SKevin Wolf error_setg(errp, "QED cluster size must be within range [%u, %u] " 628959355a4SKevin Wolf "and power of 2", 629959355a4SKevin Wolf QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 630959355a4SKevin Wolf return -EINVAL; 631959355a4SKevin Wolf } 632959355a4SKevin Wolf if (!qed_is_table_size_valid(qed_opts->table_size)) { 633959355a4SKevin Wolf error_setg(errp, "QED table size must be within range [%u, %u] " 634959355a4SKevin Wolf "and power of 2", 635959355a4SKevin Wolf QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 636959355a4SKevin Wolf return -EINVAL; 637959355a4SKevin Wolf } 638959355a4SKevin Wolf if (!qed_is_image_size_valid(qed_opts->size, qed_opts->cluster_size, 639959355a4SKevin Wolf qed_opts->table_size)) 640959355a4SKevin Wolf { 641959355a4SKevin Wolf error_setg(errp, "QED image size must be a non-zero multiple of " 642959355a4SKevin Wolf "cluster size and less than %" PRIu64 " bytes", 643959355a4SKevin Wolf qed_max_image_size(qed_opts->cluster_size, 644959355a4SKevin Wolf qed_opts->table_size)); 645959355a4SKevin Wolf return -EINVAL; 646959355a4SKevin Wolf } 647959355a4SKevin Wolf 648959355a4SKevin Wolf /* Create BlockBackend to write to the image */ 649959355a4SKevin Wolf bs = bdrv_open_blockdev_ref(qed_opts->file, errp); 650959355a4SKevin Wolf if (bs == NULL) { 6518a56fdadSKevin Wolf return -EIO; 65275411d23SStefan Hajnoczi } 65375411d23SStefan Hajnoczi 654959355a4SKevin Wolf blk = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); 655959355a4SKevin Wolf ret = blk_insert_bs(blk, bs, errp); 656959355a4SKevin Wolf if (ret < 0) { 657959355a4SKevin Wolf goto out; 658959355a4SKevin Wolf } 6598a56fdadSKevin Wolf blk_set_allow_write_beyond_eof(blk, true); 6608a56fdadSKevin Wolf 661959355a4SKevin Wolf /* Prepare image format */ 662959355a4SKevin Wolf header = (QEDHeader) { 663959355a4SKevin Wolf .magic = QED_MAGIC, 664959355a4SKevin Wolf .cluster_size = qed_opts->cluster_size, 665959355a4SKevin Wolf .table_size = qed_opts->table_size, 666959355a4SKevin Wolf .header_size = 1, 667959355a4SKevin Wolf .features = 0, 668959355a4SKevin Wolf .compat_features = 0, 669959355a4SKevin Wolf .l1_table_offset = qed_opts->cluster_size, 670959355a4SKevin Wolf .image_size = qed_opts->size, 671959355a4SKevin Wolf }; 672959355a4SKevin Wolf 673959355a4SKevin Wolf l1_size = header.cluster_size * header.table_size; 674959355a4SKevin Wolf 675c743849bSStefan Hajnoczi /* File must start empty and grow, check truncate is supported */ 6763a691c50SMax Reitz ret = blk_truncate(blk, 0, PREALLOC_MODE_OFF, errp); 677c743849bSStefan Hajnoczi if (ret < 0) { 678c743849bSStefan Hajnoczi goto out; 679c743849bSStefan Hajnoczi } 680c743849bSStefan Hajnoczi 681959355a4SKevin Wolf if (qed_opts->has_backing_file) { 68275411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 68375411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 684959355a4SKevin Wolf header.backing_filename_size = strlen(qed_opts->backing_file); 68575411d23SStefan Hajnoczi 686959355a4SKevin Wolf if (qed_opts->has_backing_fmt) { 687959355a4SKevin Wolf const char *backing_fmt = BlockdevDriver_str(qed_opts->backing_fmt); 68875411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 68975411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 69075411d23SStefan Hajnoczi } 69175411d23SStefan Hajnoczi } 692959355a4SKevin Wolf } 69375411d23SStefan Hajnoczi 69475411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 6958341f00dSEric Blake ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0); 69675411d23SStefan Hajnoczi if (ret < 0) { 69775411d23SStefan Hajnoczi goto out; 69875411d23SStefan Hajnoczi } 699959355a4SKevin Wolf ret = blk_pwrite(blk, sizeof(le_header), qed_opts->backing_file, 7008341f00dSEric Blake header.backing_filename_size, 0); 70175411d23SStefan Hajnoczi if (ret < 0) { 70275411d23SStefan Hajnoczi goto out; 70375411d23SStefan Hajnoczi } 70475411d23SStefan Hajnoczi 7057267c094SAnthony Liguori l1_table = g_malloc0(l1_size); 7068341f00dSEric Blake ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0); 70775411d23SStefan Hajnoczi if (ret < 0) { 70875411d23SStefan Hajnoczi goto out; 70975411d23SStefan Hajnoczi } 71075411d23SStefan Hajnoczi 71175411d23SStefan Hajnoczi ret = 0; /* success */ 71275411d23SStefan Hajnoczi out: 7137267c094SAnthony Liguori g_free(l1_table); 7148a56fdadSKevin Wolf blk_unref(blk); 715959355a4SKevin Wolf bdrv_unref(bs); 71675411d23SStefan Hajnoczi return ret; 71775411d23SStefan Hajnoczi } 71875411d23SStefan Hajnoczi 719efc75e2aSStefan Hajnoczi static int coroutine_fn bdrv_qed_co_create_opts(const char *filename, 720efc75e2aSStefan Hajnoczi QemuOpts *opts, 721efc75e2aSStefan Hajnoczi Error **errp) 72275411d23SStefan Hajnoczi { 723959355a4SKevin Wolf BlockdevCreateOptions *create_options = NULL; 724959355a4SKevin Wolf QDict *qdict = NULL; 725959355a4SKevin Wolf QObject *qobj; 726959355a4SKevin Wolf Visitor *v; 727959355a4SKevin Wolf BlockDriverState *bs = NULL; 728959355a4SKevin Wolf Error *local_err = NULL; 7297ab74849SChunyan Liu int ret; 73075411d23SStefan Hajnoczi 731959355a4SKevin Wolf static const QDictRenames opt_renames[] = { 732959355a4SKevin Wolf { BLOCK_OPT_BACKING_FILE, "backing-file" }, 733959355a4SKevin Wolf { BLOCK_OPT_BACKING_FMT, "backing-fmt" }, 734959355a4SKevin Wolf { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" }, 735959355a4SKevin Wolf { BLOCK_OPT_TABLE_SIZE, "table-size" }, 736959355a4SKevin Wolf { NULL, NULL }, 737959355a4SKevin Wolf }; 73875411d23SStefan Hajnoczi 739959355a4SKevin Wolf /* Parse options and convert legacy syntax */ 740959355a4SKevin Wolf qdict = qemu_opts_to_qdict_filtered(opts, NULL, &qed_create_opts, true); 741959355a4SKevin Wolf 742959355a4SKevin Wolf if (!qdict_rename_keys(qdict, opt_renames, errp)) { 7437ab74849SChunyan Liu ret = -EINVAL; 744959355a4SKevin Wolf goto fail; 74575411d23SStefan Hajnoczi } 74675411d23SStefan Hajnoczi 747959355a4SKevin Wolf /* Create and open the file (protocol layer) */ 748959355a4SKevin Wolf ret = bdrv_create_file(filename, opts, &local_err); 749959355a4SKevin Wolf if (ret < 0) { 750959355a4SKevin Wolf error_propagate(errp, local_err); 751959355a4SKevin Wolf goto fail; 752959355a4SKevin Wolf } 7537ab74849SChunyan Liu 754959355a4SKevin Wolf bs = bdrv_open(filename, NULL, NULL, 755959355a4SKevin Wolf BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); 756959355a4SKevin Wolf if (bs == NULL) { 757959355a4SKevin Wolf ret = -EIO; 758959355a4SKevin Wolf goto fail; 759959355a4SKevin Wolf } 760959355a4SKevin Wolf 761959355a4SKevin Wolf /* Now get the QAPI type BlockdevCreateOptions */ 762959355a4SKevin Wolf qdict_put_str(qdict, "driver", "qed"); 763959355a4SKevin Wolf qdict_put_str(qdict, "file", bs->node_name); 764959355a4SKevin Wolf 765959355a4SKevin Wolf qobj = qdict_crumple(qdict, errp); 766cb3e7f08SMarc-André Lureau qobject_unref(qdict); 7677dc847ebSMax Reitz qdict = qobject_to(QDict, qobj); 768959355a4SKevin Wolf if (qdict == NULL) { 769959355a4SKevin Wolf ret = -EINVAL; 770959355a4SKevin Wolf goto fail; 771959355a4SKevin Wolf } 772959355a4SKevin Wolf 773959355a4SKevin Wolf v = qobject_input_visitor_new_keyval(QOBJECT(qdict)); 774959355a4SKevin Wolf visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err); 775959355a4SKevin Wolf visit_free(v); 776959355a4SKevin Wolf 777959355a4SKevin Wolf if (local_err) { 778959355a4SKevin Wolf error_propagate(errp, local_err); 779959355a4SKevin Wolf ret = -EINVAL; 780959355a4SKevin Wolf goto fail; 781959355a4SKevin Wolf } 782959355a4SKevin Wolf 783959355a4SKevin Wolf /* Silently round up size */ 784959355a4SKevin Wolf assert(create_options->driver == BLOCKDEV_DRIVER_QED); 785959355a4SKevin Wolf create_options->u.qed.size = 786959355a4SKevin Wolf ROUND_UP(create_options->u.qed.size, BDRV_SECTOR_SIZE); 787959355a4SKevin Wolf 788959355a4SKevin Wolf /* Create the qed image (format layer) */ 789959355a4SKevin Wolf ret = bdrv_qed_co_create(create_options, errp); 790959355a4SKevin Wolf 791959355a4SKevin Wolf fail: 792cb3e7f08SMarc-André Lureau qobject_unref(qdict); 793959355a4SKevin Wolf bdrv_unref(bs); 794959355a4SKevin Wolf qapi_free_BlockdevCreateOptions(create_options); 7957ab74849SChunyan Liu return ret; 79675411d23SStefan Hajnoczi } 79775411d23SStefan Hajnoczi 798b8d739fdSEric Blake static int coroutine_fn bdrv_qed_co_block_status(BlockDriverState *bs, 799b8d739fdSEric Blake bool want_zero, 800b8d739fdSEric Blake int64_t pos, int64_t bytes, 801b8d739fdSEric Blake int64_t *pnum, int64_t *map, 80267a0fd2aSFam Zheng BlockDriverState **file) 80375411d23SStefan Hajnoczi { 804298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 805b8d739fdSEric Blake size_t len = MIN(bytes, SIZE_MAX); 806b8d739fdSEric Blake int status; 807298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 8080f21b7a1SKevin Wolf uint64_t offset; 8090f21b7a1SKevin Wolf int ret; 810298800caSStefan Hajnoczi 8111f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 812b8d739fdSEric Blake ret = qed_find_cluster(s, &request, pos, &len, &offset); 813298800caSStefan Hajnoczi 814b8d739fdSEric Blake *pnum = len; 815b8d739fdSEric Blake switch (ret) { 816b8d739fdSEric Blake case QED_CLUSTER_FOUND: 817b8d739fdSEric Blake *map = offset | qed_offset_into_cluster(s, pos); 818b8d739fdSEric Blake status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 819b8d739fdSEric Blake *file = bs->file->bs; 820b8d739fdSEric Blake break; 821b8d739fdSEric Blake case QED_CLUSTER_ZERO: 822b8d739fdSEric Blake status = BDRV_BLOCK_ZERO; 823b8d739fdSEric Blake break; 824b8d739fdSEric Blake case QED_CLUSTER_L2: 825b8d739fdSEric Blake case QED_CLUSTER_L1: 826b8d739fdSEric Blake status = 0; 827b8d739fdSEric Blake break; 828b8d739fdSEric Blake default: 829b8d739fdSEric Blake assert(ret < 0); 830b8d739fdSEric Blake status = ret; 831b8d739fdSEric Blake break; 832b8d739fdSEric Blake } 833298800caSStefan Hajnoczi 834298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 8351f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 836298800caSStefan Hajnoczi 837b8d739fdSEric Blake return status; 83875411d23SStefan Hajnoczi } 83975411d23SStefan Hajnoczi 840eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 841eabba580SStefan Hajnoczi { 84248cc565eSKevin Wolf return acb->bs->opaque; 843eabba580SStefan Hajnoczi } 844eabba580SStefan Hajnoczi 845eabba580SStefan Hajnoczi /** 846eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 847eabba580SStefan Hajnoczi * 848eabba580SStefan Hajnoczi * @s: QED state 849eabba580SStefan Hajnoczi * @pos: Byte position in device 850eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 851f06ee3d4SKevin Wolf * @backing_qiov: Possibly shortened copy of qiov, to be allocated here 852eabba580SStefan Hajnoczi * @cb: Completion function 853eabba580SStefan Hajnoczi * @opaque: User data for completion function 854eabba580SStefan Hajnoczi * 855eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 856eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 857eabba580SStefan Hajnoczi */ 85887f0d882SKevin Wolf static int coroutine_fn qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 859eabba580SStefan Hajnoczi QEMUIOVector *qiov, 860e85c5281SKevin Wolf QEMUIOVector **backing_qiov) 861eabba580SStefan Hajnoczi { 862eabba580SStefan Hajnoczi uint64_t backing_length = 0; 863eabba580SStefan Hajnoczi size_t size; 864e85c5281SKevin Wolf int ret; 865eabba580SStefan Hajnoczi 866eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 867eabba580SStefan Hajnoczi * backing file like a zero length backing file. 868eabba580SStefan Hajnoczi */ 869760e0063SKevin Wolf if (s->bs->backing) { 870760e0063SKevin Wolf int64_t l = bdrv_getlength(s->bs->backing->bs); 871eabba580SStefan Hajnoczi if (l < 0) { 872e85c5281SKevin Wolf return l; 873eabba580SStefan Hajnoczi } 874eabba580SStefan Hajnoczi backing_length = l; 875eabba580SStefan Hajnoczi } 876eabba580SStefan Hajnoczi 877eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 878eabba580SStefan Hajnoczi if (pos >= backing_length || 879eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 8803d9b4925SMichael Tokarev qemu_iovec_memset(qiov, 0, 0, qiov->size); 881eabba580SStefan Hajnoczi } 882eabba580SStefan Hajnoczi 883eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 884eabba580SStefan Hajnoczi if (pos >= backing_length) { 885e85c5281SKevin Wolf return 0; 886eabba580SStefan Hajnoczi } 887eabba580SStefan Hajnoczi 888eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 889eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 890eabba580SStefan Hajnoczi 891f06ee3d4SKevin Wolf assert(*backing_qiov == NULL); 892f06ee3d4SKevin Wolf *backing_qiov = g_new(QEMUIOVector, 1); 893f06ee3d4SKevin Wolf qemu_iovec_init(*backing_qiov, qiov->niov); 894f06ee3d4SKevin Wolf qemu_iovec_concat(*backing_qiov, qiov, 0, size); 895f06ee3d4SKevin Wolf 896820100fdSPaolo Bonzini BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); 8970f714ec7SKevin Wolf ret = bdrv_co_preadv(s->bs->backing, pos, size, *backing_qiov, 0); 898e85c5281SKevin Wolf if (ret < 0) { 899e85c5281SKevin Wolf return ret; 900e85c5281SKevin Wolf } 901e85c5281SKevin Wolf return 0; 902eabba580SStefan Hajnoczi } 903eabba580SStefan Hajnoczi 904eabba580SStefan Hajnoczi /** 905eabba580SStefan Hajnoczi * Copy data from backing file into the image 906eabba580SStefan Hajnoczi * 907eabba580SStefan Hajnoczi * @s: QED state 908eabba580SStefan Hajnoczi * @pos: Byte position in device 909eabba580SStefan Hajnoczi * @len: Number of bytes 910eabba580SStefan Hajnoczi * @offset: Byte offset in image file 911eabba580SStefan Hajnoczi */ 91287f0d882SKevin Wolf static int coroutine_fn qed_copy_from_backing_file(BDRVQEDState *s, 91387f0d882SKevin Wolf uint64_t pos, uint64_t len, 91487f0d882SKevin Wolf uint64_t offset) 915eabba580SStefan Hajnoczi { 9160f7aa24dSKevin Wolf QEMUIOVector qiov; 9170f7aa24dSKevin Wolf QEMUIOVector *backing_qiov = NULL; 9180f7aa24dSKevin Wolf struct iovec iov; 919e85c5281SKevin Wolf int ret; 920eabba580SStefan Hajnoczi 921eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 922eabba580SStefan Hajnoczi if (len == 0) { 923b4ac32f3SKevin Wolf return 0; 924eabba580SStefan Hajnoczi } 925eabba580SStefan Hajnoczi 9260f7aa24dSKevin Wolf iov = (struct iovec) { 9270f7aa24dSKevin Wolf .iov_base = qemu_blockalign(s->bs, len), 9280f7aa24dSKevin Wolf .iov_len = len, 9290f7aa24dSKevin Wolf }; 9300f7aa24dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 931eabba580SStefan Hajnoczi 9320f7aa24dSKevin Wolf ret = qed_read_backing_file(s, pos, &qiov, &backing_qiov); 9330f7aa24dSKevin Wolf 9340f7aa24dSKevin Wolf if (backing_qiov) { 9350f7aa24dSKevin Wolf qemu_iovec_destroy(backing_qiov); 9360f7aa24dSKevin Wolf g_free(backing_qiov); 9370f7aa24dSKevin Wolf backing_qiov = NULL; 9380f7aa24dSKevin Wolf } 9390f7aa24dSKevin Wolf 9400f7aa24dSKevin Wolf if (ret) { 9410f7aa24dSKevin Wolf goto out; 9420f7aa24dSKevin Wolf } 9430f7aa24dSKevin Wolf 9440f7aa24dSKevin Wolf BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 9450f714ec7SKevin Wolf ret = bdrv_co_pwritev(s->bs->file, offset, qiov.size, &qiov, 0); 9460f7aa24dSKevin Wolf if (ret < 0) { 9470f7aa24dSKevin Wolf goto out; 9480f7aa24dSKevin Wolf } 9490f7aa24dSKevin Wolf ret = 0; 9500f7aa24dSKevin Wolf out: 9510f7aa24dSKevin Wolf qemu_vfree(iov.iov_base); 952b4ac32f3SKevin Wolf return ret; 953eabba580SStefan Hajnoczi } 954eabba580SStefan Hajnoczi 955eabba580SStefan Hajnoczi /** 956eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 957eabba580SStefan Hajnoczi * 958eabba580SStefan Hajnoczi * @s: QED state 959eabba580SStefan Hajnoczi * @table: L2 table 960eabba580SStefan Hajnoczi * @index: First cluster index 961eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 96221df65b6SAnthony Liguori * @cluster: First cluster offset 96321df65b6SAnthony Liguori * 96421df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the 96521df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker. 9661f01e50bSPaolo Bonzini * 9671f01e50bSPaolo Bonzini * Called with table_lock held. 968eabba580SStefan Hajnoczi */ 96987f0d882SKevin Wolf static void coroutine_fn qed_update_l2_table(BDRVQEDState *s, QEDTable *table, 97087f0d882SKevin Wolf int index, unsigned int n, 97187f0d882SKevin Wolf uint64_t cluster) 972eabba580SStefan Hajnoczi { 973eabba580SStefan Hajnoczi int i; 974eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 975eabba580SStefan Hajnoczi table->offsets[i] = cluster; 97621df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) && 97721df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) { 978eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 979eabba580SStefan Hajnoczi } 980eabba580SStefan Hajnoczi } 98121df65b6SAnthony Liguori } 982eabba580SStefan Hajnoczi 9831f01e50bSPaolo Bonzini /* Called with table_lock held. */ 98487f0d882SKevin Wolf static void coroutine_fn qed_aio_complete(QEDAIOCB *acb) 985eabba580SStefan Hajnoczi { 986eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 987eabba580SStefan Hajnoczi 988eabba580SStefan Hajnoczi /* Free resources */ 989eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov); 990eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 991eabba580SStefan Hajnoczi 9920e71be19SStefan Hajnoczi /* Free the buffer we may have allocated for zero writes */ 9930e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 9940e71be19SStefan Hajnoczi qemu_vfree(acb->qiov->iov[0].iov_base); 9950e71be19SStefan Hajnoczi acb->qiov->iov[0].iov_base = NULL; 9960e71be19SStefan Hajnoczi } 9970e71be19SStefan Hajnoczi 998eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 999eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 1000eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 1001eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 1002eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 1003eabba580SStefan Hajnoczi */ 10040806c3b5SKevin Wolf if (acb == s->allocating_acb) { 10050806c3b5SKevin Wolf s->allocating_acb = NULL; 10060806c3b5SKevin Wolf if (!qemu_co_queue_empty(&s->allocating_write_reqs)) { 10071f01e50bSPaolo Bonzini qemu_co_queue_next(&s->allocating_write_reqs); 10086f321e93SStefan Hajnoczi } else if (s->header.features & QED_F_NEED_CHECK) { 10096f321e93SStefan Hajnoczi qed_start_need_check_timer(s); 1010eabba580SStefan Hajnoczi } 1011eabba580SStefan Hajnoczi } 1012eabba580SStefan Hajnoczi } 1013eabba580SStefan Hajnoczi 1014eabba580SStefan Hajnoczi /** 1015fae25ac7SKevin Wolf * Update L1 table with new L2 table offset and write it out 10161f01e50bSPaolo Bonzini * 10171f01e50bSPaolo Bonzini * Called with table_lock held. 1018eabba580SStefan Hajnoczi */ 101987f0d882SKevin Wolf static int coroutine_fn qed_aio_write_l1_update(QEDAIOCB *acb) 1020eabba580SStefan Hajnoczi { 1021eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1022eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 1023e4fc8781SStefan Hajnoczi uint64_t l2_offset = l2_table->offset; 1024fb18de21SKevin Wolf int index, ret; 1025fae25ac7SKevin Wolf 1026fae25ac7SKevin Wolf index = qed_l1_index(s, acb->cur_pos); 1027fae25ac7SKevin Wolf s->l1_table->offsets[index] = l2_table->offset; 1028fae25ac7SKevin Wolf 1029fae25ac7SKevin Wolf ret = qed_write_l1_table(s, index, 1); 1030fae25ac7SKevin Wolf 1031fae25ac7SKevin Wolf /* Commit the current L2 table to the cache */ 1032eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 1033eabba580SStefan Hajnoczi 1034eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 1035eabba580SStefan Hajnoczi * cache. 1036eabba580SStefan Hajnoczi */ 1037e4fc8781SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 1038eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 1039eabba580SStefan Hajnoczi 1040fb18de21SKevin Wolf return ret; 1041eabba580SStefan Hajnoczi } 1042eabba580SStefan Hajnoczi 1043eabba580SStefan Hajnoczi 1044eabba580SStefan Hajnoczi /** 1045eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 10461f01e50bSPaolo Bonzini * 10471f01e50bSPaolo Bonzini * Called with table_lock held. 1048eabba580SStefan Hajnoczi */ 104987f0d882SKevin Wolf static int coroutine_fn qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset) 1050eabba580SStefan Hajnoczi { 1051eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1052eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 105388d2dd72SKevin Wolf int index, ret; 1054eabba580SStefan Hajnoczi 1055eabba580SStefan Hajnoczi if (need_alloc) { 1056eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 1057eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 1058eabba580SStefan Hajnoczi } 1059eabba580SStefan Hajnoczi 1060eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 1061eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 10620e71be19SStefan Hajnoczi offset); 1063eabba580SStefan Hajnoczi 1064eabba580SStefan Hajnoczi if (need_alloc) { 1065eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 1066453e53e2SKevin Wolf ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true); 1067fb18de21SKevin Wolf if (ret) { 106888d2dd72SKevin Wolf return ret; 1069fb18de21SKevin Wolf } 107088d2dd72SKevin Wolf return qed_aio_write_l1_update(acb); 1071eabba580SStefan Hajnoczi } else { 1072eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 1073453e53e2SKevin Wolf ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, 1074453e53e2SKevin Wolf false); 107588d2dd72SKevin Wolf if (ret) { 107688d2dd72SKevin Wolf return ret; 1077eabba580SStefan Hajnoczi } 107888d2dd72SKevin Wolf } 107988d2dd72SKevin Wolf return 0; 1080eabba580SStefan Hajnoczi } 1081eabba580SStefan Hajnoczi 1082eabba580SStefan Hajnoczi /** 1083eabba580SStefan Hajnoczi * Write data to the image file 10841f01e50bSPaolo Bonzini * 10851f01e50bSPaolo Bonzini * Called with table_lock *not* held. 1086eabba580SStefan Hajnoczi */ 108787f0d882SKevin Wolf static int coroutine_fn qed_aio_write_main(QEDAIOCB *acb) 1088eabba580SStefan Hajnoczi { 1089eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1090eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1091eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 1092eabba580SStefan Hajnoczi 1093eaf0bc56SKevin Wolf trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size); 1094eabba580SStefan Hajnoczi 1095a4d8f1aeSKevin Wolf BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 1096e7569c18SPaolo Bonzini return bdrv_co_pwritev(s->bs->file, offset, acb->cur_qiov.size, 10970f714ec7SKevin Wolf &acb->cur_qiov, 0); 1098eabba580SStefan Hajnoczi } 1099eabba580SStefan Hajnoczi 1100eabba580SStefan Hajnoczi /** 1101b4ac32f3SKevin Wolf * Populate untouched regions of new data cluster 11021f01e50bSPaolo Bonzini * 11031f01e50bSPaolo Bonzini * Called with table_lock held. 1104eabba580SStefan Hajnoczi */ 110587f0d882SKevin Wolf static int coroutine_fn qed_aio_write_cow(QEDAIOCB *acb) 1106eabba580SStefan Hajnoczi { 1107eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1108b4ac32f3SKevin Wolf uint64_t start, len, offset; 1109a101341aSKevin Wolf int ret; 1110eabba580SStefan Hajnoczi 11111f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 11121f01e50bSPaolo Bonzini 1113b4ac32f3SKevin Wolf /* Populate front untouched region of new data cluster */ 1114b4ac32f3SKevin Wolf start = qed_start_of_cluster(s, acb->cur_pos); 1115b4ac32f3SKevin Wolf len = qed_offset_into_cluster(s, acb->cur_pos); 1116b4ac32f3SKevin Wolf 1117b4ac32f3SKevin Wolf trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1118b4ac32f3SKevin Wolf ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster); 1119a101341aSKevin Wolf if (ret < 0) { 11201f01e50bSPaolo Bonzini goto out; 1121eabba580SStefan Hajnoczi } 1122eabba580SStefan Hajnoczi 1123b4ac32f3SKevin Wolf /* Populate back untouched region of new data cluster */ 1124b4ac32f3SKevin Wolf start = acb->cur_pos + acb->cur_qiov.size; 1125b4ac32f3SKevin Wolf len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1126b4ac32f3SKevin Wolf offset = acb->cur_cluster + 1127b4ac32f3SKevin Wolf qed_offset_into_cluster(s, acb->cur_pos) + 1128b4ac32f3SKevin Wolf acb->cur_qiov.size; 1129b4ac32f3SKevin Wolf 1130eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 1131b4ac32f3SKevin Wolf ret = qed_copy_from_backing_file(s, start, len, offset); 1132a101341aSKevin Wolf if (ret < 0) { 11331f01e50bSPaolo Bonzini goto out; 1134eaf0bc56SKevin Wolf } 1135eabba580SStefan Hajnoczi 1136e7569c18SPaolo Bonzini ret = qed_aio_write_main(acb); 1137e7569c18SPaolo Bonzini if (ret < 0) { 11381f01e50bSPaolo Bonzini goto out; 1139e7569c18SPaolo Bonzini } 1140e7569c18SPaolo Bonzini 1141e7569c18SPaolo Bonzini if (s->bs->backing) { 1142e7569c18SPaolo Bonzini /* 1143e7569c18SPaolo Bonzini * Flush new data clusters before updating the L2 table 1144e7569c18SPaolo Bonzini * 1145e7569c18SPaolo Bonzini * This flush is necessary when a backing file is in use. A crash 1146e7569c18SPaolo Bonzini * during an allocating write could result in empty clusters in the 1147e7569c18SPaolo Bonzini * image. If the write only touched a subregion of the cluster, 1148e7569c18SPaolo Bonzini * then backing image sectors have been lost in the untouched 1149e7569c18SPaolo Bonzini * region. The solution is to flush after writing a new data 1150e7569c18SPaolo Bonzini * cluster and before updating the L2 table. 1151e7569c18SPaolo Bonzini */ 1152e7569c18SPaolo Bonzini ret = bdrv_co_flush(s->bs->file->bs); 1153e7569c18SPaolo Bonzini } 1154e7569c18SPaolo Bonzini 11551f01e50bSPaolo Bonzini out: 11561f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 11571f01e50bSPaolo Bonzini return ret; 1158eabba580SStefan Hajnoczi } 1159eabba580SStefan Hajnoczi 1160eabba580SStefan Hajnoczi /** 11610d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write 11620d09c797SStefan Hajnoczi */ 11630d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s) 11640d09c797SStefan Hajnoczi { 11650d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */ 1166760e0063SKevin Wolf if (s->bs->backing) { 11670d09c797SStefan Hajnoczi return false; 11680d09c797SStefan Hajnoczi } 11690d09c797SStefan Hajnoczi 11700d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK); 11710d09c797SStefan Hajnoczi } 11720d09c797SStefan Hajnoczi 11730d09c797SStefan Hajnoczi /** 1174eabba580SStefan Hajnoczi * Write new data cluster 1175eabba580SStefan Hajnoczi * 1176eabba580SStefan Hajnoczi * @acb: Write request 1177eabba580SStefan Hajnoczi * @len: Length in bytes 1178eabba580SStefan Hajnoczi * 1179eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters. 11801f01e50bSPaolo Bonzini * 11811f01e50bSPaolo Bonzini * Called with table_lock held. 1182eabba580SStefan Hajnoczi */ 118387f0d882SKevin Wolf static int coroutine_fn qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1184eabba580SStefan Hajnoczi { 1185eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1186f13d712bSKevin Wolf int ret; 1187eabba580SStefan Hajnoczi 11886f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */ 11890806c3b5SKevin Wolf if (s->allocating_acb == NULL) { 11906f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 11916f321e93SStefan Hajnoczi } 11926f321e93SStefan Hajnoczi 1193eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 11940806c3b5SKevin Wolf if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) { 11950806c3b5SKevin Wolf if (s->allocating_acb != NULL) { 11961f01e50bSPaolo Bonzini qemu_co_queue_wait(&s->allocating_write_reqs, &s->table_lock); 11970806c3b5SKevin Wolf assert(s->allocating_acb == NULL); 1198eabba580SStefan Hajnoczi } 11990806c3b5SKevin Wolf s->allocating_acb = acb; 12000806c3b5SKevin Wolf return -EAGAIN; /* start over with looking up table entries */ 1201eabba580SStefan Hajnoczi } 1202eabba580SStefan Hajnoczi 1203eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 1204eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 12051b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1206eabba580SStefan Hajnoczi 12070e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 12080e71be19SStefan Hajnoczi /* Skip ahead if the clusters are already zero */ 12090e71be19SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { 1210d6daddcdSKevin Wolf return 0; 12110e71be19SStefan Hajnoczi } 1212e7569c18SPaolo Bonzini acb->cur_cluster = 1; 12130e71be19SStefan Hajnoczi } else { 12140e71be19SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 12150e71be19SStefan Hajnoczi } 12160e71be19SStefan Hajnoczi 12170d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) { 121801979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK; 1219f13d712bSKevin Wolf ret = qed_write_header(s); 1220a101341aSKevin Wolf if (ret < 0) { 1221d6daddcdSKevin Wolf return ret; 12220d09c797SStefan Hajnoczi } 1223eabba580SStefan Hajnoczi } 1224eabba580SStefan Hajnoczi 1225e7569c18SPaolo Bonzini if (!(acb->flags & QED_AIOCB_ZERO)) { 1226a101341aSKevin Wolf ret = qed_aio_write_cow(acb); 1227a101341aSKevin Wolf if (ret < 0) { 1228d6daddcdSKevin Wolf return ret; 1229a101341aSKevin Wolf } 1230e7569c18SPaolo Bonzini } 1231e7569c18SPaolo Bonzini 1232e7569c18SPaolo Bonzini return qed_aio_write_l2_update(acb, acb->cur_cluster); 1233a101341aSKevin Wolf } 1234a101341aSKevin Wolf 1235eabba580SStefan Hajnoczi /** 1236eabba580SStefan Hajnoczi * Write data cluster in place 1237eabba580SStefan Hajnoczi * 1238eabba580SStefan Hajnoczi * @acb: Write request 1239eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1240eabba580SStefan Hajnoczi * @len: Length in bytes 1241eabba580SStefan Hajnoczi * 1242eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 12431f01e50bSPaolo Bonzini * 12441f01e50bSPaolo Bonzini * Called with table_lock held. 1245eabba580SStefan Hajnoczi */ 124687f0d882SKevin Wolf static int coroutine_fn qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, 124787f0d882SKevin Wolf size_t len) 1248eabba580SStefan Hajnoczi { 12491f01e50bSPaolo Bonzini BDRVQEDState *s = acb_to_s(acb); 12501f01e50bSPaolo Bonzini int r; 12511f01e50bSPaolo Bonzini 12521f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 12531f01e50bSPaolo Bonzini 12540e71be19SStefan Hajnoczi /* Allocate buffer for zero writes */ 12550e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 12560e71be19SStefan Hajnoczi struct iovec *iov = acb->qiov->iov; 12570e71be19SStefan Hajnoczi 12580e71be19SStefan Hajnoczi if (!iov->iov_base) { 125948cc565eSKevin Wolf iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len); 12604f4896dbSKevin Wolf if (iov->iov_base == NULL) { 12611f01e50bSPaolo Bonzini r = -ENOMEM; 12621f01e50bSPaolo Bonzini goto out; 12634f4896dbSKevin Wolf } 12640e71be19SStefan Hajnoczi memset(iov->iov_base, 0, iov->iov_len); 12650e71be19SStefan Hajnoczi } 12660e71be19SStefan Hajnoczi } 12670e71be19SStefan Hajnoczi 1268eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 1269eabba580SStefan Hajnoczi acb->cur_cluster = offset; 12701b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1271eabba580SStefan Hajnoczi 12721f01e50bSPaolo Bonzini /* Do the actual write. */ 12731f01e50bSPaolo Bonzini r = qed_aio_write_main(acb); 12741f01e50bSPaolo Bonzini out: 12751f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 12761f01e50bSPaolo Bonzini return r; 1277eabba580SStefan Hajnoczi } 1278eabba580SStefan Hajnoczi 1279eabba580SStefan Hajnoczi /** 1280eabba580SStefan Hajnoczi * Write data cluster 1281eabba580SStefan Hajnoczi * 1282eabba580SStefan Hajnoczi * @opaque: Write request 12830596be7eSKevin Wolf * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 1284eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1285eabba580SStefan Hajnoczi * @len: Length in bytes 12861f01e50bSPaolo Bonzini * 12871f01e50bSPaolo Bonzini * Called with table_lock held. 1288eabba580SStefan Hajnoczi */ 128987f0d882SKevin Wolf static int coroutine_fn qed_aio_write_data(void *opaque, int ret, 1290eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1291eabba580SStefan Hajnoczi { 1292eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1293eabba580SStefan Hajnoczi 1294eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1295eabba580SStefan Hajnoczi 1296eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 1297eabba580SStefan Hajnoczi 1298eabba580SStefan Hajnoczi switch (ret) { 1299eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 13000596be7eSKevin Wolf return qed_aio_write_inplace(acb, offset, len); 1301eabba580SStefan Hajnoczi 1302eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 1303eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 130421df65b6SAnthony Liguori case QED_CLUSTER_ZERO: 13050596be7eSKevin Wolf return qed_aio_write_alloc(acb, len); 1306eabba580SStefan Hajnoczi 1307eabba580SStefan Hajnoczi default: 13080596be7eSKevin Wolf g_assert_not_reached(); 1309eabba580SStefan Hajnoczi } 1310eabba580SStefan Hajnoczi } 1311eabba580SStefan Hajnoczi 1312eabba580SStefan Hajnoczi /** 1313eabba580SStefan Hajnoczi * Read data cluster 1314eabba580SStefan Hajnoczi * 1315eabba580SStefan Hajnoczi * @opaque: Read request 13160596be7eSKevin Wolf * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 1317eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1318eabba580SStefan Hajnoczi * @len: Length in bytes 13191f01e50bSPaolo Bonzini * 13201f01e50bSPaolo Bonzini * Called with table_lock held. 1321eabba580SStefan Hajnoczi */ 132287f0d882SKevin Wolf static int coroutine_fn qed_aio_read_data(void *opaque, int ret, 132387f0d882SKevin Wolf uint64_t offset, size_t len) 1324eabba580SStefan Hajnoczi { 1325eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1326eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 132748cc565eSKevin Wolf BlockDriverState *bs = acb->bs; 13281f01e50bSPaolo Bonzini int r; 13291f01e50bSPaolo Bonzini 13301f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 1331eabba580SStefan Hajnoczi 1332eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 1333eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 1334eabba580SStefan Hajnoczi 1335eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 1336eabba580SStefan Hajnoczi 13371b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1338eabba580SStefan Hajnoczi 13391f01e50bSPaolo Bonzini /* Handle zero cluster and backing file reads, otherwise read 13401f01e50bSPaolo Bonzini * data cluster directly. 13411f01e50bSPaolo Bonzini */ 134221df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) { 13433d9b4925SMichael Tokarev qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); 13441f01e50bSPaolo Bonzini r = 0; 134521df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) { 13461f01e50bSPaolo Bonzini r = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1347e85c5281SKevin Wolf &acb->backing_qiov); 13481f01e50bSPaolo Bonzini } else { 13491f01e50bSPaolo Bonzini BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 13501f01e50bSPaolo Bonzini r = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size, 13511f01e50bSPaolo Bonzini &acb->cur_qiov, 0); 1352eabba580SStefan Hajnoczi } 1353eabba580SStefan Hajnoczi 13541f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 13551f01e50bSPaolo Bonzini return r; 1356eabba580SStefan Hajnoczi } 1357eabba580SStefan Hajnoczi 1358eabba580SStefan Hajnoczi /** 1359eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1360eabba580SStefan Hajnoczi */ 136187f0d882SKevin Wolf static int coroutine_fn qed_aio_next_io(QEDAIOCB *acb) 1362eabba580SStefan Hajnoczi { 1363eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 13640f21b7a1SKevin Wolf uint64_t offset; 13650f21b7a1SKevin Wolf size_t len; 1366dddf8db1SKevin Wolf int ret; 1367eabba580SStefan Hajnoczi 13681f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 136901859874SKevin Wolf while (1) { 1370dddf8db1SKevin Wolf trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size); 1371eabba580SStefan Hajnoczi 1372f06ee3d4SKevin Wolf if (acb->backing_qiov) { 1373f06ee3d4SKevin Wolf qemu_iovec_destroy(acb->backing_qiov); 1374f06ee3d4SKevin Wolf g_free(acb->backing_qiov); 1375f06ee3d4SKevin Wolf acb->backing_qiov = NULL; 1376f06ee3d4SKevin Wolf } 1377f06ee3d4SKevin Wolf 1378eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1379eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1380eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1381eabba580SStefan Hajnoczi 1382eabba580SStefan Hajnoczi /* Complete request */ 1383eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 138448cc565eSKevin Wolf ret = 0; 138548cc565eSKevin Wolf break; 1386eabba580SStefan Hajnoczi } 1387eabba580SStefan Hajnoczi 1388eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 13890f21b7a1SKevin Wolf len = acb->end_pos - acb->cur_pos; 13900f21b7a1SKevin Wolf ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset); 13910596be7eSKevin Wolf if (ret < 0) { 139248cc565eSKevin Wolf break; 13930596be7eSKevin Wolf } 13940596be7eSKevin Wolf 13950596be7eSKevin Wolf if (acb->flags & QED_AIOCB_WRITE) { 13960596be7eSKevin Wolf ret = qed_aio_write_data(acb, ret, offset, len); 13970596be7eSKevin Wolf } else { 13980596be7eSKevin Wolf ret = qed_aio_read_data(acb, ret, offset, len); 13990596be7eSKevin Wolf } 14000596be7eSKevin Wolf 14010806c3b5SKevin Wolf if (ret < 0 && ret != -EAGAIN) { 140248cc565eSKevin Wolf break; 140301859874SKevin Wolf } 1404eabba580SStefan Hajnoczi } 1405eabba580SStefan Hajnoczi 140648cc565eSKevin Wolf trace_qed_aio_complete(s, acb, ret); 140748cc565eSKevin Wolf qed_aio_complete(acb); 14081f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 140948cc565eSKevin Wolf return ret; 141089f89709SKevin Wolf } 141189f89709SKevin Wolf 141289f89709SKevin Wolf static int coroutine_fn qed_co_request(BlockDriverState *bs, int64_t sector_num, 141389f89709SKevin Wolf QEMUIOVector *qiov, int nb_sectors, 141489f89709SKevin Wolf int flags) 141589f89709SKevin Wolf { 141648cc565eSKevin Wolf QEDAIOCB acb = { 141748cc565eSKevin Wolf .bs = bs, 141848cc565eSKevin Wolf .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE, 141948cc565eSKevin Wolf .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE, 142048cc565eSKevin Wolf .qiov = qiov, 142148cc565eSKevin Wolf .flags = flags, 142289f89709SKevin Wolf }; 142348cc565eSKevin Wolf qemu_iovec_init(&acb.cur_qiov, qiov->niov); 142489f89709SKevin Wolf 142548cc565eSKevin Wolf trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags); 1426eabba580SStefan Hajnoczi 1427eabba580SStefan Hajnoczi /* Start request */ 142848cc565eSKevin Wolf return qed_aio_next_io(&acb); 142989f89709SKevin Wolf } 143089f89709SKevin Wolf 143189f89709SKevin Wolf static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs, 143289f89709SKevin Wolf int64_t sector_num, int nb_sectors, 143389f89709SKevin Wolf QEMUIOVector *qiov) 143475411d23SStefan Hajnoczi { 143589f89709SKevin Wolf return qed_co_request(bs, sector_num, qiov, nb_sectors, 0); 143675411d23SStefan Hajnoczi } 143775411d23SStefan Hajnoczi 143889f89709SKevin Wolf static int coroutine_fn bdrv_qed_co_writev(BlockDriverState *bs, 143989f89709SKevin Wolf int64_t sector_num, int nb_sectors, 1440*e18a58b4SEric Blake QEMUIOVector *qiov, int flags) 144175411d23SStefan Hajnoczi { 1442*e18a58b4SEric Blake assert(!flags); 144389f89709SKevin Wolf return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRITE); 14440e71be19SStefan Hajnoczi } 14450e71be19SStefan Hajnoczi 144649a2e483SEric Blake static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, 144749a2e483SEric Blake int64_t offset, 1448f5a5ca79SManos Pitsidianakis int bytes, 1449aa7bfbffSPeter Lieven BdrvRequestFlags flags) 14500e71be19SStefan Hajnoczi { 1451ef72f76eSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 14520e71be19SStefan Hajnoczi QEMUIOVector qiov; 14530e71be19SStefan Hajnoczi struct iovec iov; 14540e71be19SStefan Hajnoczi 145549a2e483SEric Blake /* Fall back if the request is not aligned */ 145649a2e483SEric Blake if (qed_offset_into_cluster(s, offset) || 1457f5a5ca79SManos Pitsidianakis qed_offset_into_cluster(s, bytes)) { 1458ef72f76eSStefan Hajnoczi return -ENOTSUP; 1459ef72f76eSStefan Hajnoczi } 1460ef72f76eSStefan Hajnoczi 14610e71be19SStefan Hajnoczi /* Zero writes start without an I/O buffer. If a buffer becomes necessary 14620e71be19SStefan Hajnoczi * then it will be allocated during request processing. 14630e71be19SStefan Hajnoczi */ 146449a2e483SEric Blake iov.iov_base = NULL; 1465f5a5ca79SManos Pitsidianakis iov.iov_len = bytes; 14660e71be19SStefan Hajnoczi 14670e71be19SStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 146889f89709SKevin Wolf return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov, 1469f5a5ca79SManos Pitsidianakis bytes >> BDRV_SECTOR_BITS, 14700e71be19SStefan Hajnoczi QED_AIOCB_WRITE | QED_AIOCB_ZERO); 14710e71be19SStefan Hajnoczi } 14720e71be19SStefan Hajnoczi 14738243ccb7SMax Reitz static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset, 14748243ccb7SMax Reitz PreallocMode prealloc, Error **errp) 147575411d23SStefan Hajnoczi { 147677a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 147777a5a000SStefan Hajnoczi uint64_t old_image_size; 147877a5a000SStefan Hajnoczi int ret; 147977a5a000SStefan Hajnoczi 14808243ccb7SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 14818243ccb7SMax Reitz error_setg(errp, "Unsupported preallocation mode '%s'", 1482977c736fSMarkus Armbruster PreallocMode_str(prealloc)); 14838243ccb7SMax Reitz return -ENOTSUP; 14848243ccb7SMax Reitz } 14858243ccb7SMax Reitz 148677a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size, 148777a5a000SStefan Hajnoczi s->header.table_size)) { 1488f59adb32SMax Reitz error_setg(errp, "Invalid image size specified"); 148977a5a000SStefan Hajnoczi return -EINVAL; 149077a5a000SStefan Hajnoczi } 149177a5a000SStefan Hajnoczi 149277a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) { 1493f59adb32SMax Reitz error_setg(errp, "Shrinking images is currently not supported"); 149475411d23SStefan Hajnoczi return -ENOTSUP; 149575411d23SStefan Hajnoczi } 149675411d23SStefan Hajnoczi 149777a5a000SStefan Hajnoczi old_image_size = s->header.image_size; 149877a5a000SStefan Hajnoczi s->header.image_size = offset; 149977a5a000SStefan Hajnoczi ret = qed_write_header_sync(s); 150077a5a000SStefan Hajnoczi if (ret < 0) { 150177a5a000SStefan Hajnoczi s->header.image_size = old_image_size; 1502f59adb32SMax Reitz error_setg_errno(errp, -ret, "Failed to update the image size"); 150377a5a000SStefan Hajnoczi } 150477a5a000SStefan Hajnoczi return ret; 150577a5a000SStefan Hajnoczi } 150677a5a000SStefan Hajnoczi 150775411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs) 150875411d23SStefan Hajnoczi { 150975411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 151075411d23SStefan Hajnoczi return s->header.image_size; 151175411d23SStefan Hajnoczi } 151275411d23SStefan Hajnoczi 151375411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 151475411d23SStefan Hajnoczi { 151575411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 151675411d23SStefan Hajnoczi 151775411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi)); 151875411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size; 1519d68dbee8SDong Xu Wang bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; 152095de6d70SPaolo Bonzini bdi->unallocated_blocks_are_zero = true; 152175411d23SStefan Hajnoczi return 0; 152275411d23SStefan Hajnoczi } 152375411d23SStefan Hajnoczi 152475411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 152575411d23SStefan Hajnoczi const char *backing_file, 152675411d23SStefan Hajnoczi const char *backing_fmt) 152775411d23SStefan Hajnoczi { 152875411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 152975411d23SStefan Hajnoczi QEDHeader new_header, le_header; 153075411d23SStefan Hajnoczi void *buffer; 153175411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 153275411d23SStefan Hajnoczi int ret; 153375411d23SStefan Hajnoczi 153475411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 153575411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 153675411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 153775411d23SStefan Hajnoczi * add a new string. 153875411d23SStefan Hajnoczi */ 153975411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 154075411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 154175411d23SStefan Hajnoczi return -ENOTSUP; 154275411d23SStefan Hajnoczi } 154375411d23SStefan Hajnoczi 154475411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 154575411d23SStefan Hajnoczi 154675411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 154775411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 154875411d23SStefan Hajnoczi 154975411d23SStefan Hajnoczi /* Adjust feature flags */ 155075411d23SStefan Hajnoczi if (backing_file) { 155175411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 155275411d23SStefan Hajnoczi 155375411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 155475411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 155575411d23SStefan Hajnoczi } 155675411d23SStefan Hajnoczi } 155775411d23SStefan Hajnoczi 155875411d23SStefan Hajnoczi /* Calculate new header size */ 155975411d23SStefan Hajnoczi backing_file_len = 0; 156075411d23SStefan Hajnoczi 156175411d23SStefan Hajnoczi if (backing_file) { 156275411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 156375411d23SStefan Hajnoczi } 156475411d23SStefan Hajnoczi 156575411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 156675411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 156775411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 156875411d23SStefan Hajnoczi buffer_len += backing_file_len; 156975411d23SStefan Hajnoczi 157075411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 157175411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 157275411d23SStefan Hajnoczi return -ENOSPC; 157375411d23SStefan Hajnoczi } 157475411d23SStefan Hajnoczi 157575411d23SStefan Hajnoczi /* Prepare new header */ 15767267c094SAnthony Liguori buffer = g_malloc(buffer_len); 157775411d23SStefan Hajnoczi 157875411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 157975411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 158075411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 158175411d23SStefan Hajnoczi 1582feba23b1SPavel Borzenkov if (backing_file) { 158375411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 158475411d23SStefan Hajnoczi buffer_len += backing_file_len; 1585feba23b1SPavel Borzenkov } 158675411d23SStefan Hajnoczi 158775411d23SStefan Hajnoczi /* Write new header */ 1588d9ca2ea2SKevin Wolf ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); 15897267c094SAnthony Liguori g_free(buffer); 159075411d23SStefan Hajnoczi if (ret == 0) { 159175411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 159275411d23SStefan Hajnoczi } 159375411d23SStefan Hajnoczi return ret; 159475411d23SStefan Hajnoczi } 159575411d23SStefan Hajnoczi 15962b148f39SPaolo Bonzini static void coroutine_fn bdrv_qed_co_invalidate_cache(BlockDriverState *bs, 15972b148f39SPaolo Bonzini Error **errp) 1598c82954e5SBenoît Canet { 15991f01e50bSPaolo Bonzini BDRVQEDState *s = bs->opaque; 16005a8a30dbSKevin Wolf Error *local_err = NULL; 16015a8a30dbSKevin Wolf int ret; 1602c82954e5SBenoît Canet 1603c82954e5SBenoît Canet bdrv_qed_close(bs); 16043456a8d1SKevin Wolf 160561c7887eSPaolo Bonzini bdrv_qed_init_state(bs); 16061f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 16074e4bf5c4SKevin Wolf ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, &local_err); 16081f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 16095a8a30dbSKevin Wolf if (local_err) { 1610e43bfd9cSMarkus Armbruster error_propagate(errp, local_err); 1611e43bfd9cSMarkus Armbruster error_prepend(errp, "Could not reopen qed layer: "); 16125a8a30dbSKevin Wolf return; 16135a8a30dbSKevin Wolf } else if (ret < 0) { 16145a8a30dbSKevin Wolf error_setg_errno(errp, -ret, "Could not reopen qed layer"); 16155a8a30dbSKevin Wolf return; 16165a8a30dbSKevin Wolf } 1617c82954e5SBenoît Canet } 1618c82954e5SBenoît Canet 16192fd61638SPaolo Bonzini static int bdrv_qed_co_check(BlockDriverState *bs, BdrvCheckResult *result, 16204534ff54SKevin Wolf BdrvCheckMode fix) 162175411d23SStefan Hajnoczi { 162201979a98SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 16232fd61638SPaolo Bonzini int ret; 162401979a98SStefan Hajnoczi 16252fd61638SPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 16262fd61638SPaolo Bonzini ret = qed_check(s, result, !!fix); 16272fd61638SPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 16282fd61638SPaolo Bonzini 16292fd61638SPaolo Bonzini return ret; 163075411d23SStefan Hajnoczi } 163175411d23SStefan Hajnoczi 16327ab74849SChunyan Liu static QemuOptsList qed_create_opts = { 16337ab74849SChunyan Liu .name = "qed-create-opts", 16347ab74849SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head), 16357ab74849SChunyan Liu .desc = { 163675411d23SStefan Hajnoczi { 163775411d23SStefan Hajnoczi .name = BLOCK_OPT_SIZE, 16387ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 16397ab74849SChunyan Liu .help = "Virtual disk size" 16407ab74849SChunyan Liu }, 16417ab74849SChunyan Liu { 164275411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FILE, 16437ab74849SChunyan Liu .type = QEMU_OPT_STRING, 164475411d23SStefan Hajnoczi .help = "File name of a base image" 16457ab74849SChunyan Liu }, 16467ab74849SChunyan Liu { 164775411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FMT, 16487ab74849SChunyan Liu .type = QEMU_OPT_STRING, 164975411d23SStefan Hajnoczi .help = "Image format of the base image" 16507ab74849SChunyan Liu }, 16517ab74849SChunyan Liu { 165275411d23SStefan Hajnoczi .name = BLOCK_OPT_CLUSTER_SIZE, 16537ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 165499cce9faSKevin Wolf .help = "Cluster size (in bytes)", 16557ab74849SChunyan Liu .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE) 16567ab74849SChunyan Liu }, 16577ab74849SChunyan Liu { 165875411d23SStefan Hajnoczi .name = BLOCK_OPT_TABLE_SIZE, 16597ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 166075411d23SStefan Hajnoczi .help = "L1/L2 table size (in clusters)" 166175411d23SStefan Hajnoczi }, 166275411d23SStefan Hajnoczi { /* end of list */ } 16637ab74849SChunyan Liu } 166475411d23SStefan Hajnoczi }; 166575411d23SStefan Hajnoczi 166675411d23SStefan Hajnoczi static BlockDriver bdrv_qed = { 166775411d23SStefan Hajnoczi .format_name = "qed", 166875411d23SStefan Hajnoczi .instance_size = sizeof(BDRVQEDState), 16697ab74849SChunyan Liu .create_opts = &qed_create_opts, 16708ee79e70SKevin Wolf .supports_backing = true, 167175411d23SStefan Hajnoczi 167275411d23SStefan Hajnoczi .bdrv_probe = bdrv_qed_probe, 167375411d23SStefan Hajnoczi .bdrv_open = bdrv_qed_open, 167475411d23SStefan Hajnoczi .bdrv_close = bdrv_qed_close, 1675f9cb20f1SJeff Cody .bdrv_reopen_prepare = bdrv_qed_reopen_prepare, 1676862f215fSKevin Wolf .bdrv_child_perm = bdrv_format_default_perms, 1677959355a4SKevin Wolf .bdrv_co_create = bdrv_qed_co_create, 1678efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = bdrv_qed_co_create_opts, 16793ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 1680b8d739fdSEric Blake .bdrv_co_block_status = bdrv_qed_co_block_status, 168189f89709SKevin Wolf .bdrv_co_readv = bdrv_qed_co_readv, 168289f89709SKevin Wolf .bdrv_co_writev = bdrv_qed_co_writev, 168349a2e483SEric Blake .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes, 168475411d23SStefan Hajnoczi .bdrv_truncate = bdrv_qed_truncate, 168575411d23SStefan Hajnoczi .bdrv_getlength = bdrv_qed_getlength, 168675411d23SStefan Hajnoczi .bdrv_get_info = bdrv_qed_get_info, 1687d34682cdSKevin Wolf .bdrv_refresh_limits = bdrv_qed_refresh_limits, 168875411d23SStefan Hajnoczi .bdrv_change_backing_file = bdrv_qed_change_backing_file, 16892b148f39SPaolo Bonzini .bdrv_co_invalidate_cache = bdrv_qed_co_invalidate_cache, 16902fd61638SPaolo Bonzini .bdrv_co_check = bdrv_qed_co_check, 1691a8c868c3SStefan Hajnoczi .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, 1692a8c868c3SStefan Hajnoczi .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, 1693f8ea8dacSManos Pitsidianakis .bdrv_co_drain_begin = bdrv_qed_co_drain_begin, 169475411d23SStefan Hajnoczi }; 169575411d23SStefan Hajnoczi 169675411d23SStefan Hajnoczi static void bdrv_qed_init(void) 169775411d23SStefan Hajnoczi { 169875411d23SStefan Hajnoczi bdrv_register(&bdrv_qed); 169975411d23SStefan Hajnoczi } 170075411d23SStefan Hajnoczi 170175411d23SStefan Hajnoczi block_init(bdrv_qed_init); 1702