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" 19eabba580SStefan Hajnoczi #include "trace.h" 2075411d23SStefan Hajnoczi #include "qed.h" 217b1b5d19SPaolo Bonzini #include "qapi/qmp/qerror.h" 22caf71f86SPaolo Bonzini #include "migration/migration.h" 238a56fdadSKevin Wolf #include "sysemu/block-backend.h" 2475411d23SStefan Hajnoczi 25d7331bedSStefan Hajnoczi static const AIOCBInfo qed_aiocb_info = { 26eabba580SStefan Hajnoczi .aiocb_size = sizeof(QEDAIOCB), 27eabba580SStefan Hajnoczi }; 28eabba580SStefan 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); 899a4f4c31SKevin Wolf ret = bdrv_pwrite(s->bs->file->bs, 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 typedef struct { 9701979a98SStefan Hajnoczi GenericCB gencb; 9801979a98SStefan Hajnoczi BDRVQEDState *s; 9901979a98SStefan Hajnoczi struct iovec iov; 10001979a98SStefan Hajnoczi QEMUIOVector qiov; 10101979a98SStefan Hajnoczi int nsectors; 10201979a98SStefan Hajnoczi uint8_t *buf; 10301979a98SStefan Hajnoczi } QEDWriteHeaderCB; 10401979a98SStefan Hajnoczi 10501979a98SStefan Hajnoczi static void qed_write_header_cb(void *opaque, int ret) 10601979a98SStefan Hajnoczi { 10701979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = opaque; 10801979a98SStefan Hajnoczi 10901979a98SStefan Hajnoczi qemu_vfree(write_header_cb->buf); 11001979a98SStefan Hajnoczi gencb_complete(write_header_cb, ret); 11101979a98SStefan Hajnoczi } 11201979a98SStefan Hajnoczi 11301979a98SStefan Hajnoczi static void qed_write_header_read_cb(void *opaque, int ret) 11401979a98SStefan Hajnoczi { 11501979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = opaque; 11601979a98SStefan Hajnoczi BDRVQEDState *s = write_header_cb->s; 11701979a98SStefan Hajnoczi 11801979a98SStefan Hajnoczi if (ret) { 11901979a98SStefan Hajnoczi qed_write_header_cb(write_header_cb, ret); 12001979a98SStefan Hajnoczi return; 12101979a98SStefan Hajnoczi } 12201979a98SStefan Hajnoczi 12301979a98SStefan Hajnoczi /* Update header */ 12401979a98SStefan Hajnoczi qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf); 12501979a98SStefan Hajnoczi 1269a4f4c31SKevin Wolf bdrv_aio_writev(s->bs->file->bs, 0, &write_header_cb->qiov, 12701979a98SStefan Hajnoczi write_header_cb->nsectors, qed_write_header_cb, 12801979a98SStefan Hajnoczi write_header_cb); 12901979a98SStefan Hajnoczi } 13001979a98SStefan Hajnoczi 13101979a98SStefan Hajnoczi /** 13201979a98SStefan Hajnoczi * Update header in-place (does not rewrite backing filename or other strings) 13301979a98SStefan Hajnoczi * 13401979a98SStefan Hajnoczi * This function only updates known header fields in-place and does not affect 13501979a98SStefan Hajnoczi * extra data after the QED header. 13601979a98SStefan Hajnoczi */ 137097310b5SMarkus Armbruster static void qed_write_header(BDRVQEDState *s, BlockCompletionFunc cb, 13801979a98SStefan Hajnoczi void *opaque) 13901979a98SStefan Hajnoczi { 14001979a98SStefan Hajnoczi /* We must write full sectors for O_DIRECT but cannot necessarily generate 14101979a98SStefan Hajnoczi * the data following the header if an unrecognized compat feature is 14201979a98SStefan Hajnoczi * active. Therefore, first read the sectors containing the header, update 14301979a98SStefan Hajnoczi * them, and write back. 14401979a98SStefan Hajnoczi */ 14501979a98SStefan Hajnoczi 146c41a73ffSLaurent Vivier int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE); 14701979a98SStefan Hajnoczi size_t len = nsectors * BDRV_SECTOR_SIZE; 14801979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb), 14901979a98SStefan Hajnoczi cb, opaque); 15001979a98SStefan Hajnoczi 15101979a98SStefan Hajnoczi write_header_cb->s = s; 15201979a98SStefan Hajnoczi write_header_cb->nsectors = nsectors; 15301979a98SStefan Hajnoczi write_header_cb->buf = qemu_blockalign(s->bs, len); 15401979a98SStefan Hajnoczi write_header_cb->iov.iov_base = write_header_cb->buf; 15501979a98SStefan Hajnoczi write_header_cb->iov.iov_len = len; 15601979a98SStefan Hajnoczi qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1); 15701979a98SStefan Hajnoczi 1589a4f4c31SKevin Wolf bdrv_aio_readv(s->bs->file->bs, 0, &write_header_cb->qiov, nsectors, 15901979a98SStefan Hajnoczi qed_write_header_read_cb, write_header_cb); 16001979a98SStefan Hajnoczi } 16101979a98SStefan Hajnoczi 16275411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 16375411d23SStefan Hajnoczi { 16475411d23SStefan Hajnoczi uint64_t table_entries; 16575411d23SStefan Hajnoczi uint64_t l2_size; 16675411d23SStefan Hajnoczi 16775411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t); 16875411d23SStefan Hajnoczi l2_size = table_entries * cluster_size; 16975411d23SStefan Hajnoczi 17075411d23SStefan Hajnoczi return l2_size * table_entries; 17175411d23SStefan Hajnoczi } 17275411d23SStefan Hajnoczi 17375411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size) 17475411d23SStefan Hajnoczi { 17575411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE || 17675411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) { 17775411d23SStefan Hajnoczi return false; 17875411d23SStefan Hajnoczi } 17975411d23SStefan Hajnoczi if (cluster_size & (cluster_size - 1)) { 18075411d23SStefan Hajnoczi return false; /* not power of 2 */ 18175411d23SStefan Hajnoczi } 18275411d23SStefan Hajnoczi return true; 18375411d23SStefan Hajnoczi } 18475411d23SStefan Hajnoczi 18575411d23SStefan Hajnoczi static bool qed_is_table_size_valid(uint32_t table_size) 18675411d23SStefan Hajnoczi { 18775411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE || 18875411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) { 18975411d23SStefan Hajnoczi return false; 19075411d23SStefan Hajnoczi } 19175411d23SStefan Hajnoczi if (table_size & (table_size - 1)) { 19275411d23SStefan Hajnoczi return false; /* not power of 2 */ 19375411d23SStefan Hajnoczi } 19475411d23SStefan Hajnoczi return true; 19575411d23SStefan Hajnoczi } 19675411d23SStefan Hajnoczi 19775411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 19875411d23SStefan Hajnoczi uint32_t table_size) 19975411d23SStefan Hajnoczi { 20075411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) { 20175411d23SStefan Hajnoczi return false; /* not multiple of sector size */ 20275411d23SStefan Hajnoczi } 20375411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) { 20475411d23SStefan Hajnoczi return false; /* image is too large */ 20575411d23SStefan Hajnoczi } 20675411d23SStefan Hajnoczi return true; 20775411d23SStefan Hajnoczi } 20875411d23SStefan Hajnoczi 20975411d23SStefan Hajnoczi /** 21075411d23SStefan Hajnoczi * Read a string of known length from the image file 21175411d23SStefan Hajnoczi * 21275411d23SStefan Hajnoczi * @file: Image file 21375411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes 21475411d23SStefan Hajnoczi * @n: String length in bytes 21575411d23SStefan Hajnoczi * @buf: Destination buffer 21675411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes 21775411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure 21875411d23SStefan Hajnoczi * 21975411d23SStefan Hajnoczi * The string is NUL-terminated. 22075411d23SStefan Hajnoczi */ 22175411d23SStefan Hajnoczi static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n, 22275411d23SStefan Hajnoczi char *buf, size_t buflen) 22375411d23SStefan Hajnoczi { 22475411d23SStefan Hajnoczi int ret; 22575411d23SStefan Hajnoczi if (n >= buflen) { 22675411d23SStefan Hajnoczi return -EINVAL; 22775411d23SStefan Hajnoczi } 22875411d23SStefan Hajnoczi ret = bdrv_pread(file, offset, buf, n); 22975411d23SStefan Hajnoczi if (ret < 0) { 23075411d23SStefan Hajnoczi return ret; 23175411d23SStefan Hajnoczi } 23275411d23SStefan Hajnoczi buf[n] = '\0'; 23375411d23SStefan Hajnoczi return 0; 23475411d23SStefan Hajnoczi } 23575411d23SStefan Hajnoczi 236eabba580SStefan Hajnoczi /** 237eabba580SStefan Hajnoczi * Allocate new clusters 238eabba580SStefan Hajnoczi * 239eabba580SStefan Hajnoczi * @s: QED state 240eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate 241eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster 242eabba580SStefan Hajnoczi * 243eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be 244eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image 245eabba580SStefan Hajnoczi * file. 246eabba580SStefan Hajnoczi */ 247eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 248eabba580SStefan Hajnoczi { 249eabba580SStefan Hajnoczi uint64_t offset = s->file_size; 250eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size; 251eabba580SStefan Hajnoczi return offset; 252eabba580SStefan Hajnoczi } 253eabba580SStefan Hajnoczi 254298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s) 255298800caSStefan Hajnoczi { 256298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */ 257298800caSStefan Hajnoczi return qemu_blockalign(s->bs, 258298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size); 259298800caSStefan Hajnoczi } 260298800caSStefan Hajnoczi 261eabba580SStefan Hajnoczi /** 262eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table 263eabba580SStefan Hajnoczi */ 264eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 265eabba580SStefan Hajnoczi { 266eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 267eabba580SStefan Hajnoczi 268eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s); 269eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 270eabba580SStefan Hajnoczi 271eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0, 272eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size); 273eabba580SStefan Hajnoczi return l2_table; 274eabba580SStefan Hajnoczi } 275eabba580SStefan Hajnoczi 276eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret); 277eabba580SStefan Hajnoczi 2786f321e93SStefan Hajnoczi static void qed_plug_allocating_write_reqs(BDRVQEDState *s) 2796f321e93SStefan Hajnoczi { 2806f321e93SStefan Hajnoczi assert(!s->allocating_write_reqs_plugged); 2816f321e93SStefan Hajnoczi 2826f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = true; 2836f321e93SStefan Hajnoczi } 2846f321e93SStefan Hajnoczi 2856f321e93SStefan Hajnoczi static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) 2866f321e93SStefan Hajnoczi { 2876f321e93SStefan Hajnoczi QEDAIOCB *acb; 2886f321e93SStefan Hajnoczi 2896f321e93SStefan Hajnoczi assert(s->allocating_write_reqs_plugged); 2906f321e93SStefan Hajnoczi 2916f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = false; 2926f321e93SStefan Hajnoczi 2936f321e93SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 2946f321e93SStefan Hajnoczi if (acb) { 2956f321e93SStefan Hajnoczi qed_aio_next_io(acb, 0); 2966f321e93SStefan Hajnoczi } 2976f321e93SStefan Hajnoczi } 2986f321e93SStefan Hajnoczi 2996f321e93SStefan Hajnoczi static void qed_finish_clear_need_check(void *opaque, int ret) 3006f321e93SStefan Hajnoczi { 3016f321e93SStefan Hajnoczi /* Do nothing */ 3026f321e93SStefan Hajnoczi } 3036f321e93SStefan Hajnoczi 3046f321e93SStefan Hajnoczi static void qed_flush_after_clear_need_check(void *opaque, int ret) 3056f321e93SStefan Hajnoczi { 3066f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3076f321e93SStefan Hajnoczi 3086f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s); 3096f321e93SStefan Hajnoczi 3106f321e93SStefan Hajnoczi /* No need to wait until flush completes */ 3116f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3126f321e93SStefan Hajnoczi } 3136f321e93SStefan Hajnoczi 3146f321e93SStefan Hajnoczi static void qed_clear_need_check(void *opaque, int ret) 3156f321e93SStefan Hajnoczi { 3166f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3176f321e93SStefan Hajnoczi 3186f321e93SStefan Hajnoczi if (ret) { 3196f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3206f321e93SStefan Hajnoczi return; 3216f321e93SStefan Hajnoczi } 3226f321e93SStefan Hajnoczi 3236f321e93SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 3246f321e93SStefan Hajnoczi qed_write_header(s, qed_flush_after_clear_need_check, s); 3256f321e93SStefan Hajnoczi } 3266f321e93SStefan Hajnoczi 3276f321e93SStefan Hajnoczi static void qed_need_check_timer_cb(void *opaque) 3286f321e93SStefan Hajnoczi { 3296f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3306f321e93SStefan Hajnoczi 3316f321e93SStefan Hajnoczi /* The timer should only fire when allocating writes have drained */ 3326f321e93SStefan Hajnoczi assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs)); 3336f321e93SStefan Hajnoczi 3346f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s); 3356f321e93SStefan Hajnoczi 3366f321e93SStefan Hajnoczi qed_plug_allocating_write_reqs(s); 3376f321e93SStefan Hajnoczi 3386f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */ 3396f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_clear_need_check, s); 3406f321e93SStefan Hajnoczi } 3416f321e93SStefan Hajnoczi 3426f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s) 3436f321e93SStefan Hajnoczi { 3446f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s); 3456f321e93SStefan Hajnoczi 346bc72ad67SAlex Bligh /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for 3476f321e93SStefan Hajnoczi * migration. 3486f321e93SStefan Hajnoczi */ 349bc72ad67SAlex Bligh timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 35073bcb24dSRutuja Shah NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT); 3516f321e93SStefan Hajnoczi } 3526f321e93SStefan Hajnoczi 3536f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */ 3546f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s) 3556f321e93SStefan Hajnoczi { 3566f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s); 357bc72ad67SAlex Bligh timer_del(s->need_check_timer); 3586f321e93SStefan Hajnoczi } 3596f321e93SStefan Hajnoczi 360a8c868c3SStefan Hajnoczi static void bdrv_qed_detach_aio_context(BlockDriverState *bs) 361a8c868c3SStefan Hajnoczi { 362a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 363a8c868c3SStefan Hajnoczi 364a8c868c3SStefan Hajnoczi qed_cancel_need_check_timer(s); 365a8c868c3SStefan Hajnoczi timer_free(s->need_check_timer); 366a8c868c3SStefan Hajnoczi } 367a8c868c3SStefan Hajnoczi 368a8c868c3SStefan Hajnoczi static void bdrv_qed_attach_aio_context(BlockDriverState *bs, 369a8c868c3SStefan Hajnoczi AioContext *new_context) 370a8c868c3SStefan Hajnoczi { 371a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 372a8c868c3SStefan Hajnoczi 373a8c868c3SStefan Hajnoczi s->need_check_timer = aio_timer_new(new_context, 374a8c868c3SStefan Hajnoczi QEMU_CLOCK_VIRTUAL, SCALE_NS, 375a8c868c3SStefan Hajnoczi qed_need_check_timer_cb, s); 376a8c868c3SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 377a8c868c3SStefan Hajnoczi qed_start_need_check_timer(s); 378a8c868c3SStefan Hajnoczi } 379a8c868c3SStefan Hajnoczi } 380a8c868c3SStefan Hajnoczi 381015a1036SMax Reitz static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags, 382015a1036SMax Reitz Error **errp) 38375411d23SStefan Hajnoczi { 38475411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 38575411d23SStefan Hajnoczi QEDHeader le_header; 38675411d23SStefan Hajnoczi int64_t file_size; 38775411d23SStefan Hajnoczi int ret; 38875411d23SStefan Hajnoczi 38975411d23SStefan Hajnoczi s->bs = bs; 390eabba580SStefan Hajnoczi QSIMPLEQ_INIT(&s->allocating_write_reqs); 39175411d23SStefan Hajnoczi 3929a4f4c31SKevin Wolf ret = bdrv_pread(bs->file->bs, 0, &le_header, sizeof(le_header)); 39375411d23SStefan Hajnoczi if (ret < 0) { 39475411d23SStefan Hajnoczi return ret; 39575411d23SStefan Hajnoczi } 39675411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header); 39775411d23SStefan Hajnoczi 39875411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) { 39976abe407SPaolo Bonzini error_setg(errp, "Image not in QED format"); 40076abe407SPaolo Bonzini return -EINVAL; 40175411d23SStefan Hajnoczi } 40275411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) { 40310b758e8SKevin Wolf /* image uses unsupported feature bits */ 404a55448b3SMax Reitz error_setg(errp, "Unsupported QED features: %" PRIx64, 40510b758e8SKevin Wolf s->header.features & ~QED_FEATURE_MASK); 40610b758e8SKevin Wolf return -ENOTSUP; 40775411d23SStefan Hajnoczi } 40875411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 40975411d23SStefan Hajnoczi return -EINVAL; 41075411d23SStefan Hajnoczi } 41175411d23SStefan Hajnoczi 41275411d23SStefan Hajnoczi /* Round down file size to the last cluster */ 4139a4f4c31SKevin Wolf file_size = bdrv_getlength(bs->file->bs); 41475411d23SStefan Hajnoczi if (file_size < 0) { 41575411d23SStefan Hajnoczi return file_size; 41675411d23SStefan Hajnoczi } 41775411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size); 41875411d23SStefan Hajnoczi 41975411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) { 42075411d23SStefan Hajnoczi return -EINVAL; 42175411d23SStefan Hajnoczi } 42275411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size, 42375411d23SStefan Hajnoczi s->header.cluster_size, 42475411d23SStefan Hajnoczi s->header.table_size)) { 42575411d23SStefan Hajnoczi return -EINVAL; 42675411d23SStefan Hajnoczi } 42775411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 42875411d23SStefan Hajnoczi return -EINVAL; 42975411d23SStefan Hajnoczi } 43075411d23SStefan Hajnoczi 43175411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) / 43275411d23SStefan Hajnoczi sizeof(uint64_t); 433786a4ea8SStefan Hajnoczi s->l2_shift = ctz32(s->header.cluster_size); 43475411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1; 435786a4ea8SStefan Hajnoczi s->l1_shift = s->l2_shift + ctz32(s->table_nelems); 43675411d23SStefan Hajnoczi 4370adfa1edSStefan Hajnoczi /* Header size calculation must not overflow uint32_t */ 4380adfa1edSStefan Hajnoczi if (s->header.header_size > UINT32_MAX / s->header.cluster_size) { 4390adfa1edSStefan Hajnoczi return -EINVAL; 4400adfa1edSStefan Hajnoczi } 4410adfa1edSStefan Hajnoczi 44275411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) { 44375411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset + 44475411d23SStefan Hajnoczi s->header.backing_filename_size > 44575411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) { 44675411d23SStefan Hajnoczi return -EINVAL; 44775411d23SStefan Hajnoczi } 44875411d23SStefan Hajnoczi 4499a4f4c31SKevin Wolf ret = qed_read_string(bs->file->bs, s->header.backing_filename_offset, 45075411d23SStefan Hajnoczi s->header.backing_filename_size, bs->backing_file, 45175411d23SStefan Hajnoczi sizeof(bs->backing_file)); 45275411d23SStefan Hajnoczi if (ret < 0) { 45375411d23SStefan Hajnoczi return ret; 45475411d23SStefan Hajnoczi } 45575411d23SStefan Hajnoczi 45675411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 45775411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 45875411d23SStefan Hajnoczi } 45975411d23SStefan Hajnoczi } 46075411d23SStefan Hajnoczi 46175411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards 46275411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older 46375411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is 46475411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear 46575411d23SStefan Hajnoczi * feature is no longer valid. 46675411d23SStefan Hajnoczi */ 46775411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 46804c01a5cSKevin Wolf !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) { 46975411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 47075411d23SStefan Hajnoczi 47175411d23SStefan Hajnoczi ret = qed_write_header_sync(s); 47275411d23SStefan Hajnoczi if (ret) { 47375411d23SStefan Hajnoczi return ret; 47475411d23SStefan Hajnoczi } 47575411d23SStefan Hajnoczi 47675411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */ 4779a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 47875411d23SStefan Hajnoczi } 47975411d23SStefan Hajnoczi 480298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s); 481298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache); 482298800caSStefan Hajnoczi 483298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s); 484298800caSStefan Hajnoczi if (ret) { 48501979a98SStefan Hajnoczi goto out; 48601979a98SStefan Hajnoczi } 48701979a98SStefan Hajnoczi 48801979a98SStefan Hajnoczi /* If image was not closed cleanly, check consistency */ 489058f8f16SStefan Hajnoczi if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) { 49001979a98SStefan Hajnoczi /* Read-only images cannot be fixed. There is no risk of corruption 49101979a98SStefan Hajnoczi * since write operations are not possible. Therefore, allow 49201979a98SStefan Hajnoczi * potentially inconsistent images to be opened read-only. This can 49301979a98SStefan Hajnoczi * aid data recovery from an otherwise inconsistent image. 49401979a98SStefan Hajnoczi */ 4959a4f4c31SKevin Wolf if (!bdrv_is_read_only(bs->file->bs) && 49604c01a5cSKevin Wolf !(flags & BDRV_O_INACTIVE)) { 49701979a98SStefan Hajnoczi BdrvCheckResult result = {0}; 49801979a98SStefan Hajnoczi 49901979a98SStefan Hajnoczi ret = qed_check(s, &result, true); 5006f321e93SStefan Hajnoczi if (ret) { 5016f321e93SStefan Hajnoczi goto out; 5026f321e93SStefan Hajnoczi } 50301979a98SStefan Hajnoczi } 50401979a98SStefan Hajnoczi } 50501979a98SStefan Hajnoczi 506a8c868c3SStefan Hajnoczi bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs)); 5076f321e93SStefan Hajnoczi 50801979a98SStefan Hajnoczi out: 50901979a98SStefan Hajnoczi if (ret) { 510298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 511298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 512298800caSStefan Hajnoczi } 51375411d23SStefan Hajnoczi return ret; 51475411d23SStefan Hajnoczi } 51575411d23SStefan Hajnoczi 5163baca891SKevin Wolf static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp) 517d34682cdSKevin Wolf { 518d34682cdSKevin Wolf BDRVQEDState *s = bs->opaque; 519d34682cdSKevin Wolf 520cf081fcaSEric Blake bs->bl.pwrite_zeroes_alignment = s->header.cluster_size; 521d34682cdSKevin Wolf } 522d34682cdSKevin Wolf 523f9cb20f1SJeff Cody /* We have nothing to do for QED reopen, stubs just return 524f9cb20f1SJeff Cody * success */ 525f9cb20f1SJeff Cody static int bdrv_qed_reopen_prepare(BDRVReopenState *state, 526f9cb20f1SJeff Cody BlockReopenQueue *queue, Error **errp) 527f9cb20f1SJeff Cody { 528f9cb20f1SJeff Cody return 0; 529f9cb20f1SJeff Cody } 530f9cb20f1SJeff Cody 53175411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 53275411d23SStefan Hajnoczi { 533298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 534298800caSStefan Hajnoczi 535a8c868c3SStefan Hajnoczi bdrv_qed_detach_aio_context(bs); 5366f321e93SStefan Hajnoczi 53701979a98SStefan Hajnoczi /* Ensure writes reach stable storage */ 5389a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 53901979a98SStefan Hajnoczi 54001979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */ 54101979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 54201979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 54301979a98SStefan Hajnoczi qed_write_header_sync(s); 54401979a98SStefan Hajnoczi } 54501979a98SStefan Hajnoczi 546298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 547298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 54875411d23SStefan Hajnoczi } 54975411d23SStefan Hajnoczi 55075411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size, 55175411d23SStefan Hajnoczi uint64_t image_size, uint32_t table_size, 5520fea6b79SPaolo Bonzini const char *backing_file, const char *backing_fmt, 5534ab15590SChunyan Liu QemuOpts *opts, Error **errp) 55475411d23SStefan Hajnoczi { 55575411d23SStefan Hajnoczi QEDHeader header = { 55675411d23SStefan Hajnoczi .magic = QED_MAGIC, 55775411d23SStefan Hajnoczi .cluster_size = cluster_size, 55875411d23SStefan Hajnoczi .table_size = table_size, 55975411d23SStefan Hajnoczi .header_size = 1, 56075411d23SStefan Hajnoczi .features = 0, 56175411d23SStefan Hajnoczi .compat_features = 0, 56275411d23SStefan Hajnoczi .l1_table_offset = cluster_size, 56375411d23SStefan Hajnoczi .image_size = image_size, 56475411d23SStefan Hajnoczi }; 56575411d23SStefan Hajnoczi QEDHeader le_header; 56675411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 56775411d23SStefan Hajnoczi size_t l1_size = header.cluster_size * header.table_size; 56834b5d2c6SMax Reitz Error *local_err = NULL; 56975411d23SStefan Hajnoczi int ret = 0; 5708a56fdadSKevin Wolf BlockBackend *blk; 57175411d23SStefan Hajnoczi 5724ab15590SChunyan Liu ret = bdrv_create_file(filename, opts, &local_err); 57375411d23SStefan Hajnoczi if (ret < 0) { 5740fea6b79SPaolo Bonzini error_propagate(errp, local_err); 57575411d23SStefan Hajnoczi return ret; 57675411d23SStefan Hajnoczi } 57775411d23SStefan Hajnoczi 578efaa7c4eSMax Reitz blk = blk_new_open(filename, NULL, NULL, 57972e775c7SKevin Wolf BDRV_O_RDWR | BDRV_O_PROTOCOL, &local_err); 5808a56fdadSKevin Wolf if (blk == NULL) { 5810fea6b79SPaolo Bonzini error_propagate(errp, local_err); 5828a56fdadSKevin Wolf return -EIO; 58375411d23SStefan Hajnoczi } 58475411d23SStefan Hajnoczi 5858a56fdadSKevin Wolf blk_set_allow_write_beyond_eof(blk, true); 5868a56fdadSKevin Wolf 587c743849bSStefan Hajnoczi /* File must start empty and grow, check truncate is supported */ 5888a56fdadSKevin Wolf ret = blk_truncate(blk, 0); 589c743849bSStefan Hajnoczi if (ret < 0) { 590c743849bSStefan Hajnoczi goto out; 591c743849bSStefan Hajnoczi } 592c743849bSStefan Hajnoczi 59375411d23SStefan Hajnoczi if (backing_file) { 59475411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 59575411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 59675411d23SStefan Hajnoczi header.backing_filename_size = strlen(backing_file); 59775411d23SStefan Hajnoczi 59875411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 59975411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 60075411d23SStefan Hajnoczi } 60175411d23SStefan Hajnoczi } 60275411d23SStefan Hajnoczi 60375411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 6048341f00dSEric Blake ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0); 60575411d23SStefan Hajnoczi if (ret < 0) { 60675411d23SStefan Hajnoczi goto out; 60775411d23SStefan Hajnoczi } 6088a56fdadSKevin Wolf ret = blk_pwrite(blk, sizeof(le_header), backing_file, 6098341f00dSEric Blake header.backing_filename_size, 0); 61075411d23SStefan Hajnoczi if (ret < 0) { 61175411d23SStefan Hajnoczi goto out; 61275411d23SStefan Hajnoczi } 61375411d23SStefan Hajnoczi 6147267c094SAnthony Liguori l1_table = g_malloc0(l1_size); 6158341f00dSEric Blake ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0); 61675411d23SStefan Hajnoczi if (ret < 0) { 61775411d23SStefan Hajnoczi goto out; 61875411d23SStefan Hajnoczi } 61975411d23SStefan Hajnoczi 62075411d23SStefan Hajnoczi ret = 0; /* success */ 62175411d23SStefan Hajnoczi out: 6227267c094SAnthony Liguori g_free(l1_table); 6238a56fdadSKevin Wolf blk_unref(blk); 62475411d23SStefan Hajnoczi return ret; 62575411d23SStefan Hajnoczi } 62675411d23SStefan Hajnoczi 6277ab74849SChunyan Liu static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp) 62875411d23SStefan Hajnoczi { 62975411d23SStefan Hajnoczi uint64_t image_size = 0; 63075411d23SStefan Hajnoczi uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 63175411d23SStefan Hajnoczi uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 6327ab74849SChunyan Liu char *backing_file = NULL; 6337ab74849SChunyan Liu char *backing_fmt = NULL; 6347ab74849SChunyan Liu int ret; 63575411d23SStefan Hajnoczi 636c2eb918eSHu Tao image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 637c2eb918eSHu Tao BDRV_SECTOR_SIZE); 6387ab74849SChunyan Liu backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); 6397ab74849SChunyan Liu backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); 6407ab74849SChunyan Liu cluster_size = qemu_opt_get_size_del(opts, 6417ab74849SChunyan Liu BLOCK_OPT_CLUSTER_SIZE, 6427ab74849SChunyan Liu QED_DEFAULT_CLUSTER_SIZE); 6437ab74849SChunyan Liu table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE, 6447ab74849SChunyan Liu QED_DEFAULT_TABLE_SIZE); 64575411d23SStefan Hajnoczi 64675411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(cluster_size)) { 6475ff679b4SAakriti Gupta error_setg(errp, "QED cluster size must be within range [%u, %u] " 6485ff679b4SAakriti Gupta "and power of 2", 64975411d23SStefan Hajnoczi QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 6507ab74849SChunyan Liu ret = -EINVAL; 6517ab74849SChunyan Liu goto finish; 65275411d23SStefan Hajnoczi } 65375411d23SStefan Hajnoczi if (!qed_is_table_size_valid(table_size)) { 6545ff679b4SAakriti Gupta error_setg(errp, "QED table size must be within range [%u, %u] " 6555ff679b4SAakriti Gupta "and power of 2", 65675411d23SStefan Hajnoczi QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 6577ab74849SChunyan Liu ret = -EINVAL; 6587ab74849SChunyan Liu goto finish; 65975411d23SStefan Hajnoczi } 66075411d23SStefan Hajnoczi if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 6615ff679b4SAakriti Gupta error_setg(errp, "QED image size must be a non-zero multiple of " 6625ff679b4SAakriti Gupta "cluster size and less than %" PRIu64 " bytes", 66375411d23SStefan Hajnoczi qed_max_image_size(cluster_size, table_size)); 6647ab74849SChunyan Liu ret = -EINVAL; 6657ab74849SChunyan Liu goto finish; 66675411d23SStefan Hajnoczi } 66775411d23SStefan Hajnoczi 6687ab74849SChunyan Liu ret = qed_create(filename, cluster_size, image_size, table_size, 6694ab15590SChunyan Liu backing_file, backing_fmt, opts, errp); 6707ab74849SChunyan Liu 6717ab74849SChunyan Liu finish: 6727ab74849SChunyan Liu g_free(backing_file); 6737ab74849SChunyan Liu g_free(backing_fmt); 6747ab74849SChunyan Liu return ret; 67575411d23SStefan Hajnoczi } 67675411d23SStefan Hajnoczi 677298800caSStefan Hajnoczi typedef struct { 6784bc74be9SPaolo Bonzini BlockDriverState *bs; 679b7d5a5b8SStefan Hajnoczi Coroutine *co; 6804bc74be9SPaolo Bonzini uint64_t pos; 6814bc74be9SPaolo Bonzini int64_t status; 682298800caSStefan Hajnoczi int *pnum; 68353f1dfd1SFam Zheng BlockDriverState **file; 684298800caSStefan Hajnoczi } QEDIsAllocatedCB; 685298800caSStefan Hajnoczi 686298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 687298800caSStefan Hajnoczi { 688298800caSStefan Hajnoczi QEDIsAllocatedCB *cb = opaque; 6894bc74be9SPaolo Bonzini BDRVQEDState *s = cb->bs->opaque; 690298800caSStefan Hajnoczi *cb->pnum = len / BDRV_SECTOR_SIZE; 6914bc74be9SPaolo Bonzini switch (ret) { 6924bc74be9SPaolo Bonzini case QED_CLUSTER_FOUND: 6934bc74be9SPaolo Bonzini offset |= qed_offset_into_cluster(s, cb->pos); 6944bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; 69553f1dfd1SFam Zheng *cb->file = cb->bs->file->bs; 6964bc74be9SPaolo Bonzini break; 6974bc74be9SPaolo Bonzini case QED_CLUSTER_ZERO: 6984bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_ZERO; 6994bc74be9SPaolo Bonzini break; 7004bc74be9SPaolo Bonzini case QED_CLUSTER_L2: 7014bc74be9SPaolo Bonzini case QED_CLUSTER_L1: 7024bc74be9SPaolo Bonzini cb->status = 0; 7034bc74be9SPaolo Bonzini break; 7044bc74be9SPaolo Bonzini default: 7054bc74be9SPaolo Bonzini assert(ret < 0); 7064bc74be9SPaolo Bonzini cb->status = ret; 7074bc74be9SPaolo Bonzini break; 7084bc74be9SPaolo Bonzini } 7094bc74be9SPaolo Bonzini 710b7d5a5b8SStefan Hajnoczi if (cb->co) { 711b7d5a5b8SStefan Hajnoczi qemu_coroutine_enter(cb->co, NULL); 712b7d5a5b8SStefan Hajnoczi } 713298800caSStefan Hajnoczi } 714298800caSStefan Hajnoczi 715b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs, 716b7d5a5b8SStefan Hajnoczi int64_t sector_num, 71767a0fd2aSFam Zheng int nb_sectors, int *pnum, 71867a0fd2aSFam Zheng BlockDriverState **file) 71975411d23SStefan Hajnoczi { 720298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 721298800caSStefan Hajnoczi size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 722298800caSStefan Hajnoczi QEDIsAllocatedCB cb = { 7234bc74be9SPaolo Bonzini .bs = bs, 7244bc74be9SPaolo Bonzini .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE, 7254bc74be9SPaolo Bonzini .status = BDRV_BLOCK_OFFSET_MASK, 726298800caSStefan Hajnoczi .pnum = pnum, 72753f1dfd1SFam Zheng .file = file, 728298800caSStefan Hajnoczi }; 729298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 730298800caSStefan Hajnoczi 7314bc74be9SPaolo Bonzini qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb); 732298800caSStefan Hajnoczi 733b7d5a5b8SStefan Hajnoczi /* Now sleep if the callback wasn't invoked immediately */ 7344bc74be9SPaolo Bonzini while (cb.status == BDRV_BLOCK_OFFSET_MASK) { 735b7d5a5b8SStefan Hajnoczi cb.co = qemu_coroutine_self(); 736b7d5a5b8SStefan Hajnoczi qemu_coroutine_yield(); 737298800caSStefan Hajnoczi } 738298800caSStefan Hajnoczi 739298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 740298800caSStefan Hajnoczi 7414bc74be9SPaolo Bonzini return cb.status; 74275411d23SStefan Hajnoczi } 74375411d23SStefan Hajnoczi 744eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 745eabba580SStefan Hajnoczi { 746eabba580SStefan Hajnoczi return acb->common.bs->opaque; 747eabba580SStefan Hajnoczi } 748eabba580SStefan Hajnoczi 749eabba580SStefan Hajnoczi /** 750eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 751eabba580SStefan Hajnoczi * 752eabba580SStefan Hajnoczi * @s: QED state 753eabba580SStefan Hajnoczi * @pos: Byte position in device 754eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 755f06ee3d4SKevin Wolf * @backing_qiov: Possibly shortened copy of qiov, to be allocated here 756eabba580SStefan Hajnoczi * @cb: Completion function 757eabba580SStefan Hajnoczi * @opaque: User data for completion function 758eabba580SStefan Hajnoczi * 759eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 760eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 761eabba580SStefan Hajnoczi */ 762eabba580SStefan Hajnoczi static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 763eabba580SStefan Hajnoczi QEMUIOVector *qiov, 764f06ee3d4SKevin Wolf QEMUIOVector **backing_qiov, 765097310b5SMarkus Armbruster BlockCompletionFunc *cb, void *opaque) 766eabba580SStefan Hajnoczi { 767eabba580SStefan Hajnoczi uint64_t backing_length = 0; 768eabba580SStefan Hajnoczi size_t size; 769eabba580SStefan Hajnoczi 770eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 771eabba580SStefan Hajnoczi * backing file like a zero length backing file. 772eabba580SStefan Hajnoczi */ 773760e0063SKevin Wolf if (s->bs->backing) { 774760e0063SKevin Wolf int64_t l = bdrv_getlength(s->bs->backing->bs); 775eabba580SStefan Hajnoczi if (l < 0) { 776eabba580SStefan Hajnoczi cb(opaque, l); 777eabba580SStefan Hajnoczi return; 778eabba580SStefan Hajnoczi } 779eabba580SStefan Hajnoczi backing_length = l; 780eabba580SStefan Hajnoczi } 781eabba580SStefan Hajnoczi 782eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 783eabba580SStefan Hajnoczi if (pos >= backing_length || 784eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 7853d9b4925SMichael Tokarev qemu_iovec_memset(qiov, 0, 0, qiov->size); 786eabba580SStefan Hajnoczi } 787eabba580SStefan Hajnoczi 788eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 789eabba580SStefan Hajnoczi if (pos >= backing_length) { 790eabba580SStefan Hajnoczi cb(opaque, 0); 791eabba580SStefan Hajnoczi return; 792eabba580SStefan Hajnoczi } 793eabba580SStefan Hajnoczi 794eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 795eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 796eabba580SStefan Hajnoczi 797f06ee3d4SKevin Wolf assert(*backing_qiov == NULL); 798f06ee3d4SKevin Wolf *backing_qiov = g_new(QEMUIOVector, 1); 799f06ee3d4SKevin Wolf qemu_iovec_init(*backing_qiov, qiov->niov); 800f06ee3d4SKevin Wolf qemu_iovec_concat(*backing_qiov, qiov, 0, size); 801f06ee3d4SKevin Wolf 802820100fdSPaolo Bonzini BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); 803760e0063SKevin Wolf bdrv_aio_readv(s->bs->backing->bs, pos / BDRV_SECTOR_SIZE, 804f06ee3d4SKevin Wolf *backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque); 805eabba580SStefan Hajnoczi } 806eabba580SStefan Hajnoczi 807eabba580SStefan Hajnoczi typedef struct { 808eabba580SStefan Hajnoczi GenericCB gencb; 809eabba580SStefan Hajnoczi BDRVQEDState *s; 810eabba580SStefan Hajnoczi QEMUIOVector qiov; 811f06ee3d4SKevin Wolf QEMUIOVector *backing_qiov; 812eabba580SStefan Hajnoczi struct iovec iov; 813eabba580SStefan Hajnoczi uint64_t offset; 814eabba580SStefan Hajnoczi } CopyFromBackingFileCB; 815eabba580SStefan Hajnoczi 816eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_cb(void *opaque, int ret) 817eabba580SStefan Hajnoczi { 818eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 819eabba580SStefan Hajnoczi qemu_vfree(copy_cb->iov.iov_base); 820eabba580SStefan Hajnoczi gencb_complete(©_cb->gencb, ret); 821eabba580SStefan Hajnoczi } 822eabba580SStefan Hajnoczi 823eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_write(void *opaque, int ret) 824eabba580SStefan Hajnoczi { 825eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 826eabba580SStefan Hajnoczi BDRVQEDState *s = copy_cb->s; 827eabba580SStefan Hajnoczi 828f06ee3d4SKevin Wolf if (copy_cb->backing_qiov) { 829f06ee3d4SKevin Wolf qemu_iovec_destroy(copy_cb->backing_qiov); 830f06ee3d4SKevin Wolf g_free(copy_cb->backing_qiov); 831f06ee3d4SKevin Wolf copy_cb->backing_qiov = NULL; 832f06ee3d4SKevin Wolf } 833f06ee3d4SKevin Wolf 834eabba580SStefan Hajnoczi if (ret) { 835eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb(copy_cb, ret); 836eabba580SStefan Hajnoczi return; 837eabba580SStefan Hajnoczi } 838eabba580SStefan Hajnoczi 839eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 8409a4f4c31SKevin Wolf bdrv_aio_writev(s->bs->file->bs, copy_cb->offset / BDRV_SECTOR_SIZE, 841ad54ae80SPaolo Bonzini ©_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE, 842eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb, copy_cb); 843eabba580SStefan Hajnoczi } 844eabba580SStefan Hajnoczi 845eabba580SStefan Hajnoczi /** 846eabba580SStefan Hajnoczi * Copy data from backing file into the image 847eabba580SStefan Hajnoczi * 848eabba580SStefan Hajnoczi * @s: QED state 849eabba580SStefan Hajnoczi * @pos: Byte position in device 850eabba580SStefan Hajnoczi * @len: Number of bytes 851eabba580SStefan Hajnoczi * @offset: Byte offset in image file 852eabba580SStefan Hajnoczi * @cb: Completion function 853eabba580SStefan Hajnoczi * @opaque: User data for completion function 854eabba580SStefan Hajnoczi */ 855eabba580SStefan Hajnoczi static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, 856eabba580SStefan Hajnoczi uint64_t len, uint64_t offset, 857097310b5SMarkus Armbruster BlockCompletionFunc *cb, 858eabba580SStefan Hajnoczi void *opaque) 859eabba580SStefan Hajnoczi { 860eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb; 861eabba580SStefan Hajnoczi 862eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 863eabba580SStefan Hajnoczi if (len == 0) { 864eabba580SStefan Hajnoczi cb(opaque, 0); 865eabba580SStefan Hajnoczi return; 866eabba580SStefan Hajnoczi } 867eabba580SStefan Hajnoczi 868eabba580SStefan Hajnoczi copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque); 869eabba580SStefan Hajnoczi copy_cb->s = s; 870eabba580SStefan Hajnoczi copy_cb->offset = offset; 871f06ee3d4SKevin Wolf copy_cb->backing_qiov = NULL; 872eabba580SStefan Hajnoczi copy_cb->iov.iov_base = qemu_blockalign(s->bs, len); 873eabba580SStefan Hajnoczi copy_cb->iov.iov_len = len; 874eabba580SStefan Hajnoczi qemu_iovec_init_external(©_cb->qiov, ©_cb->iov, 1); 875eabba580SStefan Hajnoczi 876f06ee3d4SKevin Wolf qed_read_backing_file(s, pos, ©_cb->qiov, ©_cb->backing_qiov, 877eabba580SStefan Hajnoczi qed_copy_from_backing_file_write, copy_cb); 878eabba580SStefan Hajnoczi } 879eabba580SStefan Hajnoczi 880eabba580SStefan Hajnoczi /** 881eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 882eabba580SStefan Hajnoczi * 883eabba580SStefan Hajnoczi * @s: QED state 884eabba580SStefan Hajnoczi * @table: L2 table 885eabba580SStefan Hajnoczi * @index: First cluster index 886eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 88721df65b6SAnthony Liguori * @cluster: First cluster offset 88821df65b6SAnthony Liguori * 88921df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the 89021df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker. 891eabba580SStefan Hajnoczi */ 892eabba580SStefan Hajnoczi static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index, 893eabba580SStefan Hajnoczi unsigned int n, uint64_t cluster) 894eabba580SStefan Hajnoczi { 895eabba580SStefan Hajnoczi int i; 896eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 897eabba580SStefan Hajnoczi table->offsets[i] = cluster; 89821df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) && 89921df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) { 900eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 901eabba580SStefan Hajnoczi } 902eabba580SStefan Hajnoczi } 90321df65b6SAnthony Liguori } 904eabba580SStefan Hajnoczi 905eabba580SStefan Hajnoczi static void qed_aio_complete_bh(void *opaque) 906eabba580SStefan Hajnoczi { 907eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 908097310b5SMarkus Armbruster BlockCompletionFunc *cb = acb->common.cb; 909eabba580SStefan Hajnoczi void *user_opaque = acb->common.opaque; 910eabba580SStefan Hajnoczi int ret = acb->bh_ret; 911eabba580SStefan Hajnoczi 912eabba580SStefan Hajnoczi qemu_bh_delete(acb->bh); 9138007429aSFam Zheng qemu_aio_unref(acb); 914eabba580SStefan Hajnoczi 915eabba580SStefan Hajnoczi /* Invoke callback */ 916eabba580SStefan Hajnoczi cb(user_opaque, ret); 917eabba580SStefan Hajnoczi } 918eabba580SStefan Hajnoczi 919eabba580SStefan Hajnoczi static void qed_aio_complete(QEDAIOCB *acb, int ret) 920eabba580SStefan Hajnoczi { 921eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 922eabba580SStefan Hajnoczi 923eabba580SStefan Hajnoczi trace_qed_aio_complete(s, acb, ret); 924eabba580SStefan Hajnoczi 925eabba580SStefan Hajnoczi /* Free resources */ 926eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov); 927eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 928eabba580SStefan Hajnoczi 9290e71be19SStefan Hajnoczi /* Free the buffer we may have allocated for zero writes */ 9300e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 9310e71be19SStefan Hajnoczi qemu_vfree(acb->qiov->iov[0].iov_base); 9320e71be19SStefan Hajnoczi acb->qiov->iov[0].iov_base = NULL; 9330e71be19SStefan Hajnoczi } 9340e71be19SStefan Hajnoczi 935eabba580SStefan Hajnoczi /* Arrange for a bh to invoke the completion function */ 936eabba580SStefan Hajnoczi acb->bh_ret = ret; 937a8c868c3SStefan Hajnoczi acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs), 938a8c868c3SStefan Hajnoczi qed_aio_complete_bh, acb); 939eabba580SStefan Hajnoczi qemu_bh_schedule(acb->bh); 940eabba580SStefan Hajnoczi 941eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 942eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 943eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 944eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 945eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 946eabba580SStefan Hajnoczi */ 947eabba580SStefan Hajnoczi if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 948eabba580SStefan Hajnoczi QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next); 949eabba580SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 950eabba580SStefan Hajnoczi if (acb) { 951eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 9526f321e93SStefan Hajnoczi } else if (s->header.features & QED_F_NEED_CHECK) { 9536f321e93SStefan Hajnoczi qed_start_need_check_timer(s); 954eabba580SStefan Hajnoczi } 955eabba580SStefan Hajnoczi } 956eabba580SStefan Hajnoczi } 957eabba580SStefan Hajnoczi 958eabba580SStefan Hajnoczi /** 959eabba580SStefan Hajnoczi * Commit the current L2 table to the cache 960eabba580SStefan Hajnoczi */ 961eabba580SStefan Hajnoczi static void qed_commit_l2_update(void *opaque, int ret) 962eabba580SStefan Hajnoczi { 963eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 964eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 965eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 966e4fc8781SStefan Hajnoczi uint64_t l2_offset = l2_table->offset; 967eabba580SStefan Hajnoczi 968eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 969eabba580SStefan Hajnoczi 970eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 971eabba580SStefan Hajnoczi * cache. 972eabba580SStefan Hajnoczi */ 973e4fc8781SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 974eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 975eabba580SStefan Hajnoczi 976eabba580SStefan Hajnoczi qed_aio_next_io(opaque, ret); 977eabba580SStefan Hajnoczi } 978eabba580SStefan Hajnoczi 979eabba580SStefan Hajnoczi /** 980eabba580SStefan Hajnoczi * Update L1 table with new L2 table offset and write it out 981eabba580SStefan Hajnoczi */ 982eabba580SStefan Hajnoczi static void qed_aio_write_l1_update(void *opaque, int ret) 983eabba580SStefan Hajnoczi { 984eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 985eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 986eabba580SStefan Hajnoczi int index; 987eabba580SStefan Hajnoczi 988eabba580SStefan Hajnoczi if (ret) { 989eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 990eabba580SStefan Hajnoczi return; 991eabba580SStefan Hajnoczi } 992eabba580SStefan Hajnoczi 993eabba580SStefan Hajnoczi index = qed_l1_index(s, acb->cur_pos); 994eabba580SStefan Hajnoczi s->l1_table->offsets[index] = acb->request.l2_table->offset; 995eabba580SStefan Hajnoczi 996eabba580SStefan Hajnoczi qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb); 997eabba580SStefan Hajnoczi } 998eabba580SStefan Hajnoczi 999eabba580SStefan Hajnoczi /** 1000eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 1001eabba580SStefan Hajnoczi */ 10020e71be19SStefan Hajnoczi static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset) 1003eabba580SStefan Hajnoczi { 1004eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1005eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 1006eabba580SStefan Hajnoczi int index; 1007eabba580SStefan Hajnoczi 1008eabba580SStefan Hajnoczi if (ret) { 1009eabba580SStefan Hajnoczi goto err; 1010eabba580SStefan Hajnoczi } 1011eabba580SStefan Hajnoczi 1012eabba580SStefan Hajnoczi if (need_alloc) { 1013eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 1014eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 1015eabba580SStefan Hajnoczi } 1016eabba580SStefan Hajnoczi 1017eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 1018eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 10190e71be19SStefan Hajnoczi offset); 1020eabba580SStefan Hajnoczi 1021eabba580SStefan Hajnoczi if (need_alloc) { 1022eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 1023eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true, 1024eabba580SStefan Hajnoczi qed_aio_write_l1_update, acb); 1025eabba580SStefan Hajnoczi } else { 1026eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 1027eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false, 1028eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1029eabba580SStefan Hajnoczi } 1030eabba580SStefan Hajnoczi return; 1031eabba580SStefan Hajnoczi 1032eabba580SStefan Hajnoczi err: 1033eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1034eabba580SStefan Hajnoczi } 1035eabba580SStefan Hajnoczi 10360e71be19SStefan Hajnoczi static void qed_aio_write_l2_update_cb(void *opaque, int ret) 10370e71be19SStefan Hajnoczi { 10380e71be19SStefan Hajnoczi QEDAIOCB *acb = opaque; 10390e71be19SStefan Hajnoczi qed_aio_write_l2_update(acb, ret, acb->cur_cluster); 10400e71be19SStefan Hajnoczi } 10410e71be19SStefan Hajnoczi 1042eabba580SStefan Hajnoczi /** 1043eabba580SStefan Hajnoczi * Flush new data clusters before updating the L2 table 1044eabba580SStefan Hajnoczi * 1045eabba580SStefan Hajnoczi * This flush is necessary when a backing file is in use. A crash during an 1046eabba580SStefan Hajnoczi * allocating write could result in empty clusters in the image. If the write 1047eabba580SStefan Hajnoczi * only touched a subregion of the cluster, then backing image sectors have 1048eabba580SStefan Hajnoczi * been lost in the untouched region. The solution is to flush after writing a 1049eabba580SStefan Hajnoczi * new data cluster and before updating the L2 table. 1050eabba580SStefan Hajnoczi */ 1051eabba580SStefan Hajnoczi static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) 1052eabba580SStefan Hajnoczi { 1053eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1054eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1055eabba580SStefan Hajnoczi 10569a4f4c31SKevin Wolf if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) { 1057eabba580SStefan Hajnoczi qed_aio_complete(acb, -EIO); 1058eabba580SStefan Hajnoczi } 1059eabba580SStefan Hajnoczi } 1060eabba580SStefan Hajnoczi 1061eabba580SStefan Hajnoczi /** 1062eabba580SStefan Hajnoczi * Write data to the image file 1063eabba580SStefan Hajnoczi */ 1064eabba580SStefan Hajnoczi static void qed_aio_write_main(void *opaque, int ret) 1065eabba580SStefan Hajnoczi { 1066eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1067eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1068eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1069eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 1070097310b5SMarkus Armbruster BlockCompletionFunc *next_fn; 1071eabba580SStefan Hajnoczi 1072eabba580SStefan Hajnoczi trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size); 1073eabba580SStefan Hajnoczi 1074eabba580SStefan Hajnoczi if (ret) { 1075eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1076eabba580SStefan Hajnoczi return; 1077eabba580SStefan Hajnoczi } 1078eabba580SStefan Hajnoczi 1079eabba580SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_FOUND) { 1080eabba580SStefan Hajnoczi next_fn = qed_aio_next_io; 1081eabba580SStefan Hajnoczi } else { 1082760e0063SKevin Wolf if (s->bs->backing) { 1083eabba580SStefan Hajnoczi next_fn = qed_aio_write_flush_before_l2_update; 1084eabba580SStefan Hajnoczi } else { 10850e71be19SStefan Hajnoczi next_fn = qed_aio_write_l2_update_cb; 1086eabba580SStefan Hajnoczi } 1087eabba580SStefan Hajnoczi } 1088eabba580SStefan Hajnoczi 1089eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 10909a4f4c31SKevin Wolf bdrv_aio_writev(s->bs->file->bs, offset / BDRV_SECTOR_SIZE, 1091ad54ae80SPaolo Bonzini &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1092eabba580SStefan Hajnoczi next_fn, acb); 1093eabba580SStefan Hajnoczi } 1094eabba580SStefan Hajnoczi 1095eabba580SStefan Hajnoczi /** 1096eabba580SStefan Hajnoczi * Populate back untouched region of new data cluster 1097eabba580SStefan Hajnoczi */ 1098eabba580SStefan Hajnoczi static void qed_aio_write_postfill(void *opaque, int ret) 1099eabba580SStefan Hajnoczi { 1100eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1101eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1102eabba580SStefan Hajnoczi uint64_t start = acb->cur_pos + acb->cur_qiov.size; 1103eabba580SStefan Hajnoczi uint64_t len = 1104eabba580SStefan Hajnoczi qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1105eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1106eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + 1107eabba580SStefan Hajnoczi acb->cur_qiov.size; 1108eabba580SStefan Hajnoczi 1109eabba580SStefan Hajnoczi if (ret) { 1110eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1111eabba580SStefan Hajnoczi return; 1112eabba580SStefan Hajnoczi } 1113eabba580SStefan Hajnoczi 1114eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 1115eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, offset, 1116eabba580SStefan Hajnoczi qed_aio_write_main, acb); 1117eabba580SStefan Hajnoczi } 1118eabba580SStefan Hajnoczi 1119eabba580SStefan Hajnoczi /** 1120eabba580SStefan Hajnoczi * Populate front untouched region of new data cluster 1121eabba580SStefan Hajnoczi */ 1122eabba580SStefan Hajnoczi static void qed_aio_write_prefill(void *opaque, int ret) 1123eabba580SStefan Hajnoczi { 1124eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1125eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1126eabba580SStefan Hajnoczi uint64_t start = qed_start_of_cluster(s, acb->cur_pos); 1127eabba580SStefan Hajnoczi uint64_t len = qed_offset_into_cluster(s, acb->cur_pos); 1128eabba580SStefan Hajnoczi 1129eabba580SStefan Hajnoczi trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1130eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, acb->cur_cluster, 1131eabba580SStefan Hajnoczi qed_aio_write_postfill, acb); 1132eabba580SStefan Hajnoczi } 1133eabba580SStefan Hajnoczi 1134eabba580SStefan Hajnoczi /** 11350d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write 11360d09c797SStefan Hajnoczi */ 11370d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s) 11380d09c797SStefan Hajnoczi { 11390d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */ 1140760e0063SKevin Wolf if (s->bs->backing) { 11410d09c797SStefan Hajnoczi return false; 11420d09c797SStefan Hajnoczi } 11430d09c797SStefan Hajnoczi 11440d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK); 11450d09c797SStefan Hajnoczi } 11460d09c797SStefan Hajnoczi 11470e71be19SStefan Hajnoczi static void qed_aio_write_zero_cluster(void *opaque, int ret) 11480e71be19SStefan Hajnoczi { 11490e71be19SStefan Hajnoczi QEDAIOCB *acb = opaque; 11500e71be19SStefan Hajnoczi 11510e71be19SStefan Hajnoczi if (ret) { 11520e71be19SStefan Hajnoczi qed_aio_complete(acb, ret); 11530e71be19SStefan Hajnoczi return; 11540e71be19SStefan Hajnoczi } 11550e71be19SStefan Hajnoczi 11560e71be19SStefan Hajnoczi qed_aio_write_l2_update(acb, 0, 1); 11570e71be19SStefan Hajnoczi } 11580e71be19SStefan 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. 1166eabba580SStefan Hajnoczi */ 1167eabba580SStefan Hajnoczi static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1168eabba580SStefan Hajnoczi { 1169eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1170097310b5SMarkus Armbruster BlockCompletionFunc *cb; 1171eabba580SStefan Hajnoczi 11726f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */ 11736f321e93SStefan Hajnoczi if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) { 11746f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 11756f321e93SStefan Hajnoczi } 11766f321e93SStefan Hajnoczi 1177eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 1178eabba580SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 1179eabba580SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next); 1180eabba580SStefan Hajnoczi } 11816f321e93SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) || 11826f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged) { 1183eabba580SStefan Hajnoczi return; /* wait for existing request to finish */ 1184eabba580SStefan Hajnoczi } 1185eabba580SStefan Hajnoczi 1186eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 1187eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 11881b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1189eabba580SStefan Hajnoczi 11900e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 11910e71be19SStefan Hajnoczi /* Skip ahead if the clusters are already zero */ 11920e71be19SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { 11930e71be19SStefan Hajnoczi qed_aio_next_io(acb, 0); 11940e71be19SStefan Hajnoczi return; 11950e71be19SStefan Hajnoczi } 11960e71be19SStefan Hajnoczi 11970e71be19SStefan Hajnoczi cb = qed_aio_write_zero_cluster; 11980e71be19SStefan Hajnoczi } else { 11990e71be19SStefan Hajnoczi cb = qed_aio_write_prefill; 12000e71be19SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 12010e71be19SStefan Hajnoczi } 12020e71be19SStefan Hajnoczi 12030d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) { 120401979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK; 12050e71be19SStefan Hajnoczi qed_write_header(s, cb, acb); 12060d09c797SStefan Hajnoczi } else { 12070e71be19SStefan Hajnoczi cb(acb, 0); 12080d09c797SStefan Hajnoczi } 1209eabba580SStefan Hajnoczi } 1210eabba580SStefan Hajnoczi 1211eabba580SStefan Hajnoczi /** 1212eabba580SStefan Hajnoczi * Write data cluster in place 1213eabba580SStefan Hajnoczi * 1214eabba580SStefan Hajnoczi * @acb: Write request 1215eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1216eabba580SStefan Hajnoczi * @len: Length in bytes 1217eabba580SStefan Hajnoczi * 1218eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 1219eabba580SStefan Hajnoczi */ 1220eabba580SStefan Hajnoczi static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) 1221eabba580SStefan Hajnoczi { 12220e71be19SStefan Hajnoczi /* Allocate buffer for zero writes */ 12230e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 12240e71be19SStefan Hajnoczi struct iovec *iov = acb->qiov->iov; 12250e71be19SStefan Hajnoczi 12260e71be19SStefan Hajnoczi if (!iov->iov_base) { 12274f4896dbSKevin Wolf iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len); 12284f4896dbSKevin Wolf if (iov->iov_base == NULL) { 12294f4896dbSKevin Wolf qed_aio_complete(acb, -ENOMEM); 12304f4896dbSKevin Wolf return; 12314f4896dbSKevin Wolf } 12320e71be19SStefan Hajnoczi memset(iov->iov_base, 0, iov->iov_len); 12330e71be19SStefan Hajnoczi } 12340e71be19SStefan Hajnoczi } 12350e71be19SStefan Hajnoczi 1236eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 1237eabba580SStefan Hajnoczi acb->cur_cluster = offset; 12381b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1239eabba580SStefan Hajnoczi 1240eabba580SStefan Hajnoczi /* Do the actual write */ 1241eabba580SStefan Hajnoczi qed_aio_write_main(acb, 0); 1242eabba580SStefan Hajnoczi } 1243eabba580SStefan Hajnoczi 1244eabba580SStefan Hajnoczi /** 1245eabba580SStefan Hajnoczi * Write data cluster 1246eabba580SStefan Hajnoczi * 1247eabba580SStefan Hajnoczi * @opaque: Write request 1248eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1249eabba580SStefan Hajnoczi * or -errno 1250eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1251eabba580SStefan Hajnoczi * @len: Length in bytes 1252eabba580SStefan Hajnoczi * 1253eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1254eabba580SStefan Hajnoczi */ 1255eabba580SStefan Hajnoczi static void qed_aio_write_data(void *opaque, int ret, 1256eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1257eabba580SStefan Hajnoczi { 1258eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1259eabba580SStefan Hajnoczi 1260eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1261eabba580SStefan Hajnoczi 1262eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 1263eabba580SStefan Hajnoczi 1264eabba580SStefan Hajnoczi switch (ret) { 1265eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 1266eabba580SStefan Hajnoczi qed_aio_write_inplace(acb, offset, len); 1267eabba580SStefan Hajnoczi break; 1268eabba580SStefan Hajnoczi 1269eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 1270eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 127121df65b6SAnthony Liguori case QED_CLUSTER_ZERO: 1272eabba580SStefan Hajnoczi qed_aio_write_alloc(acb, len); 1273eabba580SStefan Hajnoczi break; 1274eabba580SStefan Hajnoczi 1275eabba580SStefan Hajnoczi default: 1276eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1277eabba580SStefan Hajnoczi break; 1278eabba580SStefan Hajnoczi } 1279eabba580SStefan Hajnoczi } 1280eabba580SStefan Hajnoczi 1281eabba580SStefan Hajnoczi /** 1282eabba580SStefan Hajnoczi * Read data cluster 1283eabba580SStefan Hajnoczi * 1284eabba580SStefan Hajnoczi * @opaque: Read request 1285eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1286eabba580SStefan Hajnoczi * or -errno 1287eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1288eabba580SStefan Hajnoczi * @len: Length in bytes 1289eabba580SStefan Hajnoczi * 1290eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1291eabba580SStefan Hajnoczi */ 1292eabba580SStefan Hajnoczi static void qed_aio_read_data(void *opaque, int ret, 1293eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1294eabba580SStefan Hajnoczi { 1295eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1296eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1297eabba580SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 1298eabba580SStefan Hajnoczi 1299eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 1300eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 1301eabba580SStefan Hajnoczi 1302eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 1303eabba580SStefan Hajnoczi 1304eabba580SStefan Hajnoczi if (ret < 0) { 1305eabba580SStefan Hajnoczi goto err; 1306eabba580SStefan Hajnoczi } 1307eabba580SStefan Hajnoczi 13081b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1309eabba580SStefan Hajnoczi 131021df65b6SAnthony Liguori /* Handle zero cluster and backing file reads */ 131121df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) { 13123d9b4925SMichael Tokarev qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); 131321df65b6SAnthony Liguori qed_aio_next_io(acb, 0); 131421df65b6SAnthony Liguori return; 131521df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) { 1316eabba580SStefan Hajnoczi qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1317f06ee3d4SKevin Wolf &acb->backing_qiov, qed_aio_next_io, acb); 1318eabba580SStefan Hajnoczi return; 1319eabba580SStefan Hajnoczi } 1320eabba580SStefan Hajnoczi 1321eabba580SStefan Hajnoczi BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 13229a4f4c31SKevin Wolf bdrv_aio_readv(bs->file->bs, offset / BDRV_SECTOR_SIZE, 1323ad54ae80SPaolo Bonzini &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1324eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1325eabba580SStefan Hajnoczi return; 1326eabba580SStefan Hajnoczi 1327eabba580SStefan Hajnoczi err: 1328eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1329eabba580SStefan Hajnoczi } 1330eabba580SStefan Hajnoczi 1331eabba580SStefan Hajnoczi /** 1332eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1333eabba580SStefan Hajnoczi */ 1334eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret) 1335eabba580SStefan Hajnoczi { 1336eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1337eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 13386e4f59bdSStefan Hajnoczi QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ? 13396e4f59bdSStefan Hajnoczi qed_aio_write_data : qed_aio_read_data; 1340eabba580SStefan Hajnoczi 1341eabba580SStefan Hajnoczi trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size); 1342eabba580SStefan Hajnoczi 1343f06ee3d4SKevin Wolf if (acb->backing_qiov) { 1344f06ee3d4SKevin Wolf qemu_iovec_destroy(acb->backing_qiov); 1345f06ee3d4SKevin Wolf g_free(acb->backing_qiov); 1346f06ee3d4SKevin Wolf acb->backing_qiov = NULL; 1347f06ee3d4SKevin Wolf } 1348f06ee3d4SKevin Wolf 1349eabba580SStefan Hajnoczi /* Handle I/O error */ 1350eabba580SStefan Hajnoczi if (ret) { 1351eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1352eabba580SStefan Hajnoczi return; 1353eabba580SStefan Hajnoczi } 1354eabba580SStefan Hajnoczi 1355eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1356eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1357eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1358eabba580SStefan Hajnoczi 1359eabba580SStefan Hajnoczi /* Complete request */ 1360eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 1361eabba580SStefan Hajnoczi qed_aio_complete(acb, 0); 1362eabba580SStefan Hajnoczi return; 1363eabba580SStefan Hajnoczi } 1364eabba580SStefan Hajnoczi 1365eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 1366eabba580SStefan Hajnoczi qed_find_cluster(s, &acb->request, 1367eabba580SStefan Hajnoczi acb->cur_pos, acb->end_pos - acb->cur_pos, 1368eabba580SStefan Hajnoczi io_fn, acb); 1369eabba580SStefan Hajnoczi } 1370eabba580SStefan Hajnoczi 13717c84b1b8SMarkus Armbruster static BlockAIOCB *qed_aio_setup(BlockDriverState *bs, 1372eabba580SStefan Hajnoczi int64_t sector_num, 1373eabba580SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1374097310b5SMarkus Armbruster BlockCompletionFunc *cb, 13756e4f59bdSStefan Hajnoczi void *opaque, int flags) 1376eabba580SStefan Hajnoczi { 1377d7331bedSStefan Hajnoczi QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque); 1378eabba580SStefan Hajnoczi 1379eabba580SStefan Hajnoczi trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, 13806e4f59bdSStefan Hajnoczi opaque, flags); 1381eabba580SStefan Hajnoczi 13826e4f59bdSStefan Hajnoczi acb->flags = flags; 1383eabba580SStefan Hajnoczi acb->qiov = qiov; 1384eabba580SStefan Hajnoczi acb->qiov_offset = 0; 1385eabba580SStefan Hajnoczi acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 1386eabba580SStefan Hajnoczi acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE; 1387f06ee3d4SKevin Wolf acb->backing_qiov = NULL; 1388eabba580SStefan Hajnoczi acb->request.l2_table = NULL; 1389eabba580SStefan Hajnoczi qemu_iovec_init(&acb->cur_qiov, qiov->niov); 1390eabba580SStefan Hajnoczi 1391eabba580SStefan Hajnoczi /* Start request */ 1392eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 1393eabba580SStefan Hajnoczi return &acb->common; 1394eabba580SStefan Hajnoczi } 1395eabba580SStefan Hajnoczi 13967c84b1b8SMarkus Armbruster static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs, 139775411d23SStefan Hajnoczi int64_t sector_num, 139875411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1399097310b5SMarkus Armbruster BlockCompletionFunc *cb, 140075411d23SStefan Hajnoczi void *opaque) 140175411d23SStefan Hajnoczi { 14026e4f59bdSStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 140375411d23SStefan Hajnoczi } 140475411d23SStefan Hajnoczi 14057c84b1b8SMarkus Armbruster static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs, 140675411d23SStefan Hajnoczi int64_t sector_num, 140775411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1408097310b5SMarkus Armbruster BlockCompletionFunc *cb, 140975411d23SStefan Hajnoczi void *opaque) 141075411d23SStefan Hajnoczi { 14116e4f59bdSStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, 14126e4f59bdSStefan Hajnoczi opaque, QED_AIOCB_WRITE); 141375411d23SStefan Hajnoczi } 141475411d23SStefan Hajnoczi 14150e71be19SStefan Hajnoczi typedef struct { 14160e71be19SStefan Hajnoczi Coroutine *co; 14170e71be19SStefan Hajnoczi int ret; 14180e71be19SStefan Hajnoczi bool done; 14190e71be19SStefan Hajnoczi } QEDWriteZeroesCB; 14200e71be19SStefan Hajnoczi 1421*49a2e483SEric Blake static void coroutine_fn qed_co_pwrite_zeroes_cb(void *opaque, int ret) 14220e71be19SStefan Hajnoczi { 14230e71be19SStefan Hajnoczi QEDWriteZeroesCB *cb = opaque; 14240e71be19SStefan Hajnoczi 14250e71be19SStefan Hajnoczi cb->done = true; 14260e71be19SStefan Hajnoczi cb->ret = ret; 14270e71be19SStefan Hajnoczi if (cb->co) { 14280e71be19SStefan Hajnoczi qemu_coroutine_enter(cb->co, NULL); 14290e71be19SStefan Hajnoczi } 14300e71be19SStefan Hajnoczi } 14310e71be19SStefan Hajnoczi 1432*49a2e483SEric Blake static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, 1433*49a2e483SEric Blake int64_t offset, 1434*49a2e483SEric Blake int count, 1435aa7bfbffSPeter Lieven BdrvRequestFlags flags) 14360e71be19SStefan Hajnoczi { 14377c84b1b8SMarkus Armbruster BlockAIOCB *blockacb; 1438ef72f76eSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 14390e71be19SStefan Hajnoczi QEDWriteZeroesCB cb = { .done = false }; 14400e71be19SStefan Hajnoczi QEMUIOVector qiov; 14410e71be19SStefan Hajnoczi struct iovec iov; 14420e71be19SStefan Hajnoczi 1443*49a2e483SEric Blake /* Fall back if the request is not aligned */ 1444*49a2e483SEric Blake if (qed_offset_into_cluster(s, offset) || 1445*49a2e483SEric Blake qed_offset_into_cluster(s, count)) { 1446ef72f76eSStefan Hajnoczi return -ENOTSUP; 1447ef72f76eSStefan Hajnoczi } 1448ef72f76eSStefan Hajnoczi 14490e71be19SStefan Hajnoczi /* Zero writes start without an I/O buffer. If a buffer becomes necessary 14500e71be19SStefan Hajnoczi * then it will be allocated during request processing. 14510e71be19SStefan Hajnoczi */ 1452*49a2e483SEric Blake iov.iov_base = NULL; 1453*49a2e483SEric Blake iov.iov_len = count; 14540e71be19SStefan Hajnoczi 14550e71be19SStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 1456*49a2e483SEric Blake blockacb = qed_aio_setup(bs, offset >> BDRV_SECTOR_BITS, &qiov, 1457*49a2e483SEric Blake count >> BDRV_SECTOR_BITS, 1458*49a2e483SEric Blake qed_co_pwrite_zeroes_cb, &cb, 14590e71be19SStefan Hajnoczi QED_AIOCB_WRITE | QED_AIOCB_ZERO); 14600e71be19SStefan Hajnoczi if (!blockacb) { 14610e71be19SStefan Hajnoczi return -EIO; 14620e71be19SStefan Hajnoczi } 14630e71be19SStefan Hajnoczi if (!cb.done) { 14640e71be19SStefan Hajnoczi cb.co = qemu_coroutine_self(); 14650e71be19SStefan Hajnoczi qemu_coroutine_yield(); 14660e71be19SStefan Hajnoczi } 14670e71be19SStefan Hajnoczi assert(cb.done); 14680e71be19SStefan Hajnoczi return cb.ret; 14690e71be19SStefan Hajnoczi } 14700e71be19SStefan Hajnoczi 147175411d23SStefan Hajnoczi static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset) 147275411d23SStefan Hajnoczi { 147377a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 147477a5a000SStefan Hajnoczi uint64_t old_image_size; 147577a5a000SStefan Hajnoczi int ret; 147677a5a000SStefan Hajnoczi 147777a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size, 147877a5a000SStefan Hajnoczi s->header.table_size)) { 147977a5a000SStefan Hajnoczi return -EINVAL; 148077a5a000SStefan Hajnoczi } 148177a5a000SStefan Hajnoczi 148277a5a000SStefan Hajnoczi /* Shrinking is currently not supported */ 148377a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) { 148475411d23SStefan Hajnoczi return -ENOTSUP; 148575411d23SStefan Hajnoczi } 148675411d23SStefan Hajnoczi 148777a5a000SStefan Hajnoczi old_image_size = s->header.image_size; 148877a5a000SStefan Hajnoczi s->header.image_size = offset; 148977a5a000SStefan Hajnoczi ret = qed_write_header_sync(s); 149077a5a000SStefan Hajnoczi if (ret < 0) { 149177a5a000SStefan Hajnoczi s->header.image_size = old_image_size; 149277a5a000SStefan Hajnoczi } 149377a5a000SStefan Hajnoczi return ret; 149477a5a000SStefan Hajnoczi } 149577a5a000SStefan Hajnoczi 149675411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs) 149775411d23SStefan Hajnoczi { 149875411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 149975411d23SStefan Hajnoczi return s->header.image_size; 150075411d23SStefan Hajnoczi } 150175411d23SStefan Hajnoczi 150275411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 150375411d23SStefan Hajnoczi { 150475411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 150575411d23SStefan Hajnoczi 150675411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi)); 150775411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size; 1508d68dbee8SDong Xu Wang bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; 150995de6d70SPaolo Bonzini bdi->unallocated_blocks_are_zero = true; 151095de6d70SPaolo Bonzini bdi->can_write_zeroes_with_unmap = true; 151175411d23SStefan Hajnoczi return 0; 151275411d23SStefan Hajnoczi } 151375411d23SStefan Hajnoczi 151475411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 151575411d23SStefan Hajnoczi const char *backing_file, 151675411d23SStefan Hajnoczi const char *backing_fmt) 151775411d23SStefan Hajnoczi { 151875411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 151975411d23SStefan Hajnoczi QEDHeader new_header, le_header; 152075411d23SStefan Hajnoczi void *buffer; 152175411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 152275411d23SStefan Hajnoczi int ret; 152375411d23SStefan Hajnoczi 152475411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 152575411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 152675411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 152775411d23SStefan Hajnoczi * add a new string. 152875411d23SStefan Hajnoczi */ 152975411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 153075411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 153175411d23SStefan Hajnoczi return -ENOTSUP; 153275411d23SStefan Hajnoczi } 153375411d23SStefan Hajnoczi 153475411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 153575411d23SStefan Hajnoczi 153675411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 153775411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 153875411d23SStefan Hajnoczi 153975411d23SStefan Hajnoczi /* Adjust feature flags */ 154075411d23SStefan Hajnoczi if (backing_file) { 154175411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 154275411d23SStefan Hajnoczi 154375411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 154475411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 154575411d23SStefan Hajnoczi } 154675411d23SStefan Hajnoczi } 154775411d23SStefan Hajnoczi 154875411d23SStefan Hajnoczi /* Calculate new header size */ 154975411d23SStefan Hajnoczi backing_file_len = 0; 155075411d23SStefan Hajnoczi 155175411d23SStefan Hajnoczi if (backing_file) { 155275411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 155375411d23SStefan Hajnoczi } 155475411d23SStefan Hajnoczi 155575411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 155675411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 155775411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 155875411d23SStefan Hajnoczi buffer_len += backing_file_len; 155975411d23SStefan Hajnoczi 156075411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 156175411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 156275411d23SStefan Hajnoczi return -ENOSPC; 156375411d23SStefan Hajnoczi } 156475411d23SStefan Hajnoczi 156575411d23SStefan Hajnoczi /* Prepare new header */ 15667267c094SAnthony Liguori buffer = g_malloc(buffer_len); 156775411d23SStefan Hajnoczi 156875411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 156975411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 157075411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 157175411d23SStefan Hajnoczi 1572feba23b1SPavel Borzenkov if (backing_file) { 157375411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 157475411d23SStefan Hajnoczi buffer_len += backing_file_len; 1575feba23b1SPavel Borzenkov } 157675411d23SStefan Hajnoczi 157775411d23SStefan Hajnoczi /* Write new header */ 15789a4f4c31SKevin Wolf ret = bdrv_pwrite_sync(bs->file->bs, 0, buffer, buffer_len); 15797267c094SAnthony Liguori g_free(buffer); 158075411d23SStefan Hajnoczi if (ret == 0) { 158175411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 158275411d23SStefan Hajnoczi } 158375411d23SStefan Hajnoczi return ret; 158475411d23SStefan Hajnoczi } 158575411d23SStefan Hajnoczi 15865a8a30dbSKevin Wolf static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp) 1587c82954e5SBenoît Canet { 1588c82954e5SBenoît Canet BDRVQEDState *s = bs->opaque; 15895a8a30dbSKevin Wolf Error *local_err = NULL; 15905a8a30dbSKevin Wolf int ret; 1591c82954e5SBenoît Canet 1592c82954e5SBenoît Canet bdrv_qed_close(bs); 15933456a8d1SKevin Wolf 1594c82954e5SBenoît Canet memset(s, 0, sizeof(BDRVQEDState)); 15955a8a30dbSKevin Wolf ret = bdrv_qed_open(bs, NULL, bs->open_flags, &local_err); 15965a8a30dbSKevin Wolf if (local_err) { 1597e43bfd9cSMarkus Armbruster error_propagate(errp, local_err); 1598e43bfd9cSMarkus Armbruster error_prepend(errp, "Could not reopen qed layer: "); 15995a8a30dbSKevin Wolf return; 16005a8a30dbSKevin Wolf } else if (ret < 0) { 16015a8a30dbSKevin Wolf error_setg_errno(errp, -ret, "Could not reopen qed layer"); 16025a8a30dbSKevin Wolf return; 16035a8a30dbSKevin Wolf } 1604c82954e5SBenoît Canet } 1605c82954e5SBenoît Canet 16064534ff54SKevin Wolf static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result, 16074534ff54SKevin Wolf BdrvCheckMode fix) 160875411d23SStefan Hajnoczi { 160901979a98SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 161001979a98SStefan Hajnoczi 16114534ff54SKevin Wolf return qed_check(s, result, !!fix); 161275411d23SStefan Hajnoczi } 161375411d23SStefan Hajnoczi 16147ab74849SChunyan Liu static QemuOptsList qed_create_opts = { 16157ab74849SChunyan Liu .name = "qed-create-opts", 16167ab74849SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head), 16177ab74849SChunyan Liu .desc = { 161875411d23SStefan Hajnoczi { 161975411d23SStefan Hajnoczi .name = BLOCK_OPT_SIZE, 16207ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 16217ab74849SChunyan Liu .help = "Virtual disk size" 16227ab74849SChunyan Liu }, 16237ab74849SChunyan Liu { 162475411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FILE, 16257ab74849SChunyan Liu .type = QEMU_OPT_STRING, 162675411d23SStefan Hajnoczi .help = "File name of a base image" 16277ab74849SChunyan Liu }, 16287ab74849SChunyan Liu { 162975411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FMT, 16307ab74849SChunyan Liu .type = QEMU_OPT_STRING, 163175411d23SStefan Hajnoczi .help = "Image format of the base image" 16327ab74849SChunyan Liu }, 16337ab74849SChunyan Liu { 163475411d23SStefan Hajnoczi .name = BLOCK_OPT_CLUSTER_SIZE, 16357ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 163699cce9faSKevin Wolf .help = "Cluster size (in bytes)", 16377ab74849SChunyan Liu .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE) 16387ab74849SChunyan Liu }, 16397ab74849SChunyan Liu { 164075411d23SStefan Hajnoczi .name = BLOCK_OPT_TABLE_SIZE, 16417ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 164275411d23SStefan Hajnoczi .help = "L1/L2 table size (in clusters)" 164375411d23SStefan Hajnoczi }, 164475411d23SStefan Hajnoczi { /* end of list */ } 16457ab74849SChunyan Liu } 164675411d23SStefan Hajnoczi }; 164775411d23SStefan Hajnoczi 164875411d23SStefan Hajnoczi static BlockDriver bdrv_qed = { 164975411d23SStefan Hajnoczi .format_name = "qed", 165075411d23SStefan Hajnoczi .instance_size = sizeof(BDRVQEDState), 16517ab74849SChunyan Liu .create_opts = &qed_create_opts, 16528ee79e70SKevin Wolf .supports_backing = true, 165375411d23SStefan Hajnoczi 165475411d23SStefan Hajnoczi .bdrv_probe = bdrv_qed_probe, 165575411d23SStefan Hajnoczi .bdrv_open = bdrv_qed_open, 165675411d23SStefan Hajnoczi .bdrv_close = bdrv_qed_close, 1657f9cb20f1SJeff Cody .bdrv_reopen_prepare = bdrv_qed_reopen_prepare, 1658c282e1fdSChunyan Liu .bdrv_create = bdrv_qed_create, 16593ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 1660b6b8a333SPaolo Bonzini .bdrv_co_get_block_status = bdrv_qed_co_get_block_status, 166175411d23SStefan Hajnoczi .bdrv_aio_readv = bdrv_qed_aio_readv, 166275411d23SStefan Hajnoczi .bdrv_aio_writev = bdrv_qed_aio_writev, 1663*49a2e483SEric Blake .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes, 166475411d23SStefan Hajnoczi .bdrv_truncate = bdrv_qed_truncate, 166575411d23SStefan Hajnoczi .bdrv_getlength = bdrv_qed_getlength, 166675411d23SStefan Hajnoczi .bdrv_get_info = bdrv_qed_get_info, 1667d34682cdSKevin Wolf .bdrv_refresh_limits = bdrv_qed_refresh_limits, 166875411d23SStefan Hajnoczi .bdrv_change_backing_file = bdrv_qed_change_backing_file, 1669c82954e5SBenoît Canet .bdrv_invalidate_cache = bdrv_qed_invalidate_cache, 167075411d23SStefan Hajnoczi .bdrv_check = bdrv_qed_check, 1671a8c868c3SStefan Hajnoczi .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, 1672a8c868c3SStefan Hajnoczi .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, 167375411d23SStefan Hajnoczi }; 167475411d23SStefan Hajnoczi 167575411d23SStefan Hajnoczi static void bdrv_qed_init(void) 167675411d23SStefan Hajnoczi { 167775411d23SStefan Hajnoczi bdrv_register(&bdrv_qed); 167875411d23SStefan Hajnoczi } 167975411d23SStefan Hajnoczi 168075411d23SStefan Hajnoczi block_init(bdrv_qed_init); 1681