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 151de7afc9SPaolo Bonzini #include "qemu/timer.h" 16eabba580SStefan Hajnoczi #include "trace.h" 1775411d23SStefan Hajnoczi #include "qed.h" 187b1b5d19SPaolo Bonzini #include "qapi/qmp/qerror.h" 19caf71f86SPaolo Bonzini #include "migration/migration.h" 2075411d23SStefan Hajnoczi 21d7331bedSStefan Hajnoczi static const AIOCBInfo qed_aiocb_info = { 22eabba580SStefan Hajnoczi .aiocb_size = sizeof(QEDAIOCB), 23eabba580SStefan Hajnoczi }; 24eabba580SStefan Hajnoczi 2575411d23SStefan Hajnoczi static int bdrv_qed_probe(const uint8_t *buf, int buf_size, 2675411d23SStefan Hajnoczi const char *filename) 2775411d23SStefan Hajnoczi { 2875411d23SStefan Hajnoczi const QEDHeader *header = (const QEDHeader *)buf; 2975411d23SStefan Hajnoczi 3075411d23SStefan Hajnoczi if (buf_size < sizeof(*header)) { 3175411d23SStefan Hajnoczi return 0; 3275411d23SStefan Hajnoczi } 3375411d23SStefan Hajnoczi if (le32_to_cpu(header->magic) != QED_MAGIC) { 3475411d23SStefan Hajnoczi return 0; 3575411d23SStefan Hajnoczi } 3675411d23SStefan Hajnoczi return 100; 3775411d23SStefan Hajnoczi } 3875411d23SStefan Hajnoczi 3975411d23SStefan Hajnoczi /** 4075411d23SStefan Hajnoczi * Check whether an image format is raw 4175411d23SStefan Hajnoczi * 4275411d23SStefan Hajnoczi * @fmt: Backing file format, may be NULL 4375411d23SStefan Hajnoczi */ 4475411d23SStefan Hajnoczi static bool qed_fmt_is_raw(const char *fmt) 4575411d23SStefan Hajnoczi { 4675411d23SStefan Hajnoczi return fmt && strcmp(fmt, "raw") == 0; 4775411d23SStefan Hajnoczi } 4875411d23SStefan Hajnoczi 4975411d23SStefan Hajnoczi static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) 5075411d23SStefan Hajnoczi { 5175411d23SStefan Hajnoczi cpu->magic = le32_to_cpu(le->magic); 5275411d23SStefan Hajnoczi cpu->cluster_size = le32_to_cpu(le->cluster_size); 5375411d23SStefan Hajnoczi cpu->table_size = le32_to_cpu(le->table_size); 5475411d23SStefan Hajnoczi cpu->header_size = le32_to_cpu(le->header_size); 5575411d23SStefan Hajnoczi cpu->features = le64_to_cpu(le->features); 5675411d23SStefan Hajnoczi cpu->compat_features = le64_to_cpu(le->compat_features); 5775411d23SStefan Hajnoczi cpu->autoclear_features = le64_to_cpu(le->autoclear_features); 5875411d23SStefan Hajnoczi cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); 5975411d23SStefan Hajnoczi cpu->image_size = le64_to_cpu(le->image_size); 6075411d23SStefan Hajnoczi cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); 6175411d23SStefan Hajnoczi cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); 6275411d23SStefan Hajnoczi } 6375411d23SStefan Hajnoczi 6475411d23SStefan Hajnoczi static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) 6575411d23SStefan Hajnoczi { 6675411d23SStefan Hajnoczi le->magic = cpu_to_le32(cpu->magic); 6775411d23SStefan Hajnoczi le->cluster_size = cpu_to_le32(cpu->cluster_size); 6875411d23SStefan Hajnoczi le->table_size = cpu_to_le32(cpu->table_size); 6975411d23SStefan Hajnoczi le->header_size = cpu_to_le32(cpu->header_size); 7075411d23SStefan Hajnoczi le->features = cpu_to_le64(cpu->features); 7175411d23SStefan Hajnoczi le->compat_features = cpu_to_le64(cpu->compat_features); 7275411d23SStefan Hajnoczi le->autoclear_features = cpu_to_le64(cpu->autoclear_features); 7375411d23SStefan Hajnoczi le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); 7475411d23SStefan Hajnoczi le->image_size = cpu_to_le64(cpu->image_size); 7575411d23SStefan Hajnoczi le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); 7675411d23SStefan Hajnoczi le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); 7775411d23SStefan Hajnoczi } 7875411d23SStefan Hajnoczi 79b10170acSStefan Hajnoczi int qed_write_header_sync(BDRVQEDState *s) 8075411d23SStefan Hajnoczi { 8175411d23SStefan Hajnoczi QEDHeader le; 8275411d23SStefan Hajnoczi int ret; 8375411d23SStefan Hajnoczi 8475411d23SStefan Hajnoczi qed_header_cpu_to_le(&s->header, &le); 859a4f4c31SKevin Wolf ret = bdrv_pwrite(s->bs->file->bs, 0, &le, sizeof(le)); 8675411d23SStefan Hajnoczi if (ret != sizeof(le)) { 8775411d23SStefan Hajnoczi return ret; 8875411d23SStefan Hajnoczi } 8975411d23SStefan Hajnoczi return 0; 9075411d23SStefan Hajnoczi } 9175411d23SStefan Hajnoczi 9201979a98SStefan Hajnoczi typedef struct { 9301979a98SStefan Hajnoczi GenericCB gencb; 9401979a98SStefan Hajnoczi BDRVQEDState *s; 9501979a98SStefan Hajnoczi struct iovec iov; 9601979a98SStefan Hajnoczi QEMUIOVector qiov; 9701979a98SStefan Hajnoczi int nsectors; 9801979a98SStefan Hajnoczi uint8_t *buf; 9901979a98SStefan Hajnoczi } QEDWriteHeaderCB; 10001979a98SStefan Hajnoczi 10101979a98SStefan Hajnoczi static void qed_write_header_cb(void *opaque, int ret) 10201979a98SStefan Hajnoczi { 10301979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = opaque; 10401979a98SStefan Hajnoczi 10501979a98SStefan Hajnoczi qemu_vfree(write_header_cb->buf); 10601979a98SStefan Hajnoczi gencb_complete(write_header_cb, ret); 10701979a98SStefan Hajnoczi } 10801979a98SStefan Hajnoczi 10901979a98SStefan Hajnoczi static void qed_write_header_read_cb(void *opaque, int ret) 11001979a98SStefan Hajnoczi { 11101979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = opaque; 11201979a98SStefan Hajnoczi BDRVQEDState *s = write_header_cb->s; 11301979a98SStefan Hajnoczi 11401979a98SStefan Hajnoczi if (ret) { 11501979a98SStefan Hajnoczi qed_write_header_cb(write_header_cb, ret); 11601979a98SStefan Hajnoczi return; 11701979a98SStefan Hajnoczi } 11801979a98SStefan Hajnoczi 11901979a98SStefan Hajnoczi /* Update header */ 12001979a98SStefan Hajnoczi qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf); 12101979a98SStefan Hajnoczi 1229a4f4c31SKevin Wolf bdrv_aio_writev(s->bs->file->bs, 0, &write_header_cb->qiov, 12301979a98SStefan Hajnoczi write_header_cb->nsectors, qed_write_header_cb, 12401979a98SStefan Hajnoczi write_header_cb); 12501979a98SStefan Hajnoczi } 12601979a98SStefan Hajnoczi 12701979a98SStefan Hajnoczi /** 12801979a98SStefan Hajnoczi * Update header in-place (does not rewrite backing filename or other strings) 12901979a98SStefan Hajnoczi * 13001979a98SStefan Hajnoczi * This function only updates known header fields in-place and does not affect 13101979a98SStefan Hajnoczi * extra data after the QED header. 13201979a98SStefan Hajnoczi */ 133097310b5SMarkus Armbruster static void qed_write_header(BDRVQEDState *s, BlockCompletionFunc cb, 13401979a98SStefan Hajnoczi void *opaque) 13501979a98SStefan Hajnoczi { 13601979a98SStefan Hajnoczi /* We must write full sectors for O_DIRECT but cannot necessarily generate 13701979a98SStefan Hajnoczi * the data following the header if an unrecognized compat feature is 13801979a98SStefan Hajnoczi * active. Therefore, first read the sectors containing the header, update 13901979a98SStefan Hajnoczi * them, and write back. 14001979a98SStefan Hajnoczi */ 14101979a98SStefan Hajnoczi 14201979a98SStefan Hajnoczi int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) / 14301979a98SStefan Hajnoczi BDRV_SECTOR_SIZE; 14401979a98SStefan Hajnoczi size_t len = nsectors * BDRV_SECTOR_SIZE; 14501979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb), 14601979a98SStefan Hajnoczi cb, opaque); 14701979a98SStefan Hajnoczi 14801979a98SStefan Hajnoczi write_header_cb->s = s; 14901979a98SStefan Hajnoczi write_header_cb->nsectors = nsectors; 15001979a98SStefan Hajnoczi write_header_cb->buf = qemu_blockalign(s->bs, len); 15101979a98SStefan Hajnoczi write_header_cb->iov.iov_base = write_header_cb->buf; 15201979a98SStefan Hajnoczi write_header_cb->iov.iov_len = len; 15301979a98SStefan Hajnoczi qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1); 15401979a98SStefan Hajnoczi 1559a4f4c31SKevin Wolf bdrv_aio_readv(s->bs->file->bs, 0, &write_header_cb->qiov, nsectors, 15601979a98SStefan Hajnoczi qed_write_header_read_cb, write_header_cb); 15701979a98SStefan Hajnoczi } 15801979a98SStefan Hajnoczi 15975411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 16075411d23SStefan Hajnoczi { 16175411d23SStefan Hajnoczi uint64_t table_entries; 16275411d23SStefan Hajnoczi uint64_t l2_size; 16375411d23SStefan Hajnoczi 16475411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t); 16575411d23SStefan Hajnoczi l2_size = table_entries * cluster_size; 16675411d23SStefan Hajnoczi 16775411d23SStefan Hajnoczi return l2_size * table_entries; 16875411d23SStefan Hajnoczi } 16975411d23SStefan Hajnoczi 17075411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size) 17175411d23SStefan Hajnoczi { 17275411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE || 17375411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) { 17475411d23SStefan Hajnoczi return false; 17575411d23SStefan Hajnoczi } 17675411d23SStefan Hajnoczi if (cluster_size & (cluster_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_table_size_valid(uint32_t table_size) 18375411d23SStefan Hajnoczi { 18475411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE || 18575411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) { 18675411d23SStefan Hajnoczi return false; 18775411d23SStefan Hajnoczi } 18875411d23SStefan Hajnoczi if (table_size & (table_size - 1)) { 18975411d23SStefan Hajnoczi return false; /* not power of 2 */ 19075411d23SStefan Hajnoczi } 19175411d23SStefan Hajnoczi return true; 19275411d23SStefan Hajnoczi } 19375411d23SStefan Hajnoczi 19475411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 19575411d23SStefan Hajnoczi uint32_t table_size) 19675411d23SStefan Hajnoczi { 19775411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) { 19875411d23SStefan Hajnoczi return false; /* not multiple of sector size */ 19975411d23SStefan Hajnoczi } 20075411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) { 20175411d23SStefan Hajnoczi return false; /* image is too large */ 20275411d23SStefan Hajnoczi } 20375411d23SStefan Hajnoczi return true; 20475411d23SStefan Hajnoczi } 20575411d23SStefan Hajnoczi 20675411d23SStefan Hajnoczi /** 20775411d23SStefan Hajnoczi * Read a string of known length from the image file 20875411d23SStefan Hajnoczi * 20975411d23SStefan Hajnoczi * @file: Image file 21075411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes 21175411d23SStefan Hajnoczi * @n: String length in bytes 21275411d23SStefan Hajnoczi * @buf: Destination buffer 21375411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes 21475411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure 21575411d23SStefan Hajnoczi * 21675411d23SStefan Hajnoczi * The string is NUL-terminated. 21775411d23SStefan Hajnoczi */ 21875411d23SStefan Hajnoczi static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n, 21975411d23SStefan Hajnoczi char *buf, size_t buflen) 22075411d23SStefan Hajnoczi { 22175411d23SStefan Hajnoczi int ret; 22275411d23SStefan Hajnoczi if (n >= buflen) { 22375411d23SStefan Hajnoczi return -EINVAL; 22475411d23SStefan Hajnoczi } 22575411d23SStefan Hajnoczi ret = bdrv_pread(file, offset, buf, n); 22675411d23SStefan Hajnoczi if (ret < 0) { 22775411d23SStefan Hajnoczi return ret; 22875411d23SStefan Hajnoczi } 22975411d23SStefan Hajnoczi buf[n] = '\0'; 23075411d23SStefan Hajnoczi return 0; 23175411d23SStefan Hajnoczi } 23275411d23SStefan Hajnoczi 233eabba580SStefan Hajnoczi /** 234eabba580SStefan Hajnoczi * Allocate new clusters 235eabba580SStefan Hajnoczi * 236eabba580SStefan Hajnoczi * @s: QED state 237eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate 238eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster 239eabba580SStefan Hajnoczi * 240eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be 241eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image 242eabba580SStefan Hajnoczi * file. 243eabba580SStefan Hajnoczi */ 244eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 245eabba580SStefan Hajnoczi { 246eabba580SStefan Hajnoczi uint64_t offset = s->file_size; 247eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size; 248eabba580SStefan Hajnoczi return offset; 249eabba580SStefan Hajnoczi } 250eabba580SStefan Hajnoczi 251298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s) 252298800caSStefan Hajnoczi { 253298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */ 254298800caSStefan Hajnoczi return qemu_blockalign(s->bs, 255298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size); 256298800caSStefan Hajnoczi } 257298800caSStefan Hajnoczi 258eabba580SStefan Hajnoczi /** 259eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table 260eabba580SStefan Hajnoczi */ 261eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 262eabba580SStefan Hajnoczi { 263eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 264eabba580SStefan Hajnoczi 265eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s); 266eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 267eabba580SStefan Hajnoczi 268eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0, 269eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size); 270eabba580SStefan Hajnoczi return l2_table; 271eabba580SStefan Hajnoczi } 272eabba580SStefan Hajnoczi 273eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret); 274eabba580SStefan Hajnoczi 2756f321e93SStefan Hajnoczi static void qed_plug_allocating_write_reqs(BDRVQEDState *s) 2766f321e93SStefan Hajnoczi { 2776f321e93SStefan Hajnoczi assert(!s->allocating_write_reqs_plugged); 2786f321e93SStefan Hajnoczi 2796f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = true; 2806f321e93SStefan Hajnoczi } 2816f321e93SStefan Hajnoczi 2826f321e93SStefan Hajnoczi static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) 2836f321e93SStefan Hajnoczi { 2846f321e93SStefan Hajnoczi QEDAIOCB *acb; 2856f321e93SStefan Hajnoczi 2866f321e93SStefan Hajnoczi assert(s->allocating_write_reqs_plugged); 2876f321e93SStefan Hajnoczi 2886f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = false; 2896f321e93SStefan Hajnoczi 2906f321e93SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 2916f321e93SStefan Hajnoczi if (acb) { 2926f321e93SStefan Hajnoczi qed_aio_next_io(acb, 0); 2936f321e93SStefan Hajnoczi } 2946f321e93SStefan Hajnoczi } 2956f321e93SStefan Hajnoczi 2966f321e93SStefan Hajnoczi static void qed_finish_clear_need_check(void *opaque, int ret) 2976f321e93SStefan Hajnoczi { 2986f321e93SStefan Hajnoczi /* Do nothing */ 2996f321e93SStefan Hajnoczi } 3006f321e93SStefan Hajnoczi 3016f321e93SStefan Hajnoczi static void qed_flush_after_clear_need_check(void *opaque, int ret) 3026f321e93SStefan Hajnoczi { 3036f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3046f321e93SStefan Hajnoczi 3056f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s); 3066f321e93SStefan Hajnoczi 3076f321e93SStefan Hajnoczi /* No need to wait until flush completes */ 3086f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3096f321e93SStefan Hajnoczi } 3106f321e93SStefan Hajnoczi 3116f321e93SStefan Hajnoczi static void qed_clear_need_check(void *opaque, int ret) 3126f321e93SStefan Hajnoczi { 3136f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3146f321e93SStefan Hajnoczi 3156f321e93SStefan Hajnoczi if (ret) { 3166f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3176f321e93SStefan Hajnoczi return; 3186f321e93SStefan Hajnoczi } 3196f321e93SStefan Hajnoczi 3206f321e93SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 3216f321e93SStefan Hajnoczi qed_write_header(s, qed_flush_after_clear_need_check, s); 3226f321e93SStefan Hajnoczi } 3236f321e93SStefan Hajnoczi 3246f321e93SStefan Hajnoczi static void qed_need_check_timer_cb(void *opaque) 3256f321e93SStefan Hajnoczi { 3266f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3276f321e93SStefan Hajnoczi 3286f321e93SStefan Hajnoczi /* The timer should only fire when allocating writes have drained */ 3296f321e93SStefan Hajnoczi assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs)); 3306f321e93SStefan Hajnoczi 3316f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s); 3326f321e93SStefan Hajnoczi 3336f321e93SStefan Hajnoczi qed_plug_allocating_write_reqs(s); 3346f321e93SStefan Hajnoczi 3356f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */ 3366f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_clear_need_check, s); 3376f321e93SStefan Hajnoczi } 3386f321e93SStefan Hajnoczi 3396f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s) 3406f321e93SStefan Hajnoczi { 3416f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s); 3426f321e93SStefan Hajnoczi 343bc72ad67SAlex Bligh /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for 3446f321e93SStefan Hajnoczi * migration. 3456f321e93SStefan Hajnoczi */ 346bc72ad67SAlex Bligh timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 3476f321e93SStefan Hajnoczi get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT); 3486f321e93SStefan Hajnoczi } 3496f321e93SStefan Hajnoczi 3506f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */ 3516f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s) 3526f321e93SStefan Hajnoczi { 3536f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s); 354bc72ad67SAlex Bligh timer_del(s->need_check_timer); 3556f321e93SStefan Hajnoczi } 3566f321e93SStefan Hajnoczi 357a8c868c3SStefan Hajnoczi static void bdrv_qed_detach_aio_context(BlockDriverState *bs) 358a8c868c3SStefan Hajnoczi { 359a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 360a8c868c3SStefan Hajnoczi 361a8c868c3SStefan Hajnoczi qed_cancel_need_check_timer(s); 362a8c868c3SStefan Hajnoczi timer_free(s->need_check_timer); 363a8c868c3SStefan Hajnoczi } 364a8c868c3SStefan Hajnoczi 365a8c868c3SStefan Hajnoczi static void bdrv_qed_attach_aio_context(BlockDriverState *bs, 366a8c868c3SStefan Hajnoczi AioContext *new_context) 367a8c868c3SStefan Hajnoczi { 368a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 369a8c868c3SStefan Hajnoczi 370a8c868c3SStefan Hajnoczi s->need_check_timer = aio_timer_new(new_context, 371a8c868c3SStefan Hajnoczi QEMU_CLOCK_VIRTUAL, SCALE_NS, 372a8c868c3SStefan Hajnoczi qed_need_check_timer_cb, s); 373a8c868c3SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 374a8c868c3SStefan Hajnoczi qed_start_need_check_timer(s); 375a8c868c3SStefan Hajnoczi } 376a8c868c3SStefan Hajnoczi } 377a8c868c3SStefan Hajnoczi 378*df9a681dSFam Zheng static void bdrv_qed_drain(BlockDriverState *bs) 379*df9a681dSFam Zheng { 380*df9a681dSFam Zheng BDRVQEDState *s = bs->opaque; 381*df9a681dSFam Zheng 382*df9a681dSFam Zheng /* Cancel timer and start doing I/O that were meant to happen as if it 383*df9a681dSFam Zheng * fired, that way we get bdrv_drain() taking care of the ongoing requests 384*df9a681dSFam Zheng * correctly. */ 385*df9a681dSFam Zheng qed_cancel_need_check_timer(s); 386*df9a681dSFam Zheng qed_plug_allocating_write_reqs(s); 387*df9a681dSFam Zheng bdrv_aio_flush(s->bs, qed_clear_need_check, s); 388*df9a681dSFam Zheng } 389*df9a681dSFam Zheng 390015a1036SMax Reitz static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags, 391015a1036SMax Reitz 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 39875411d23SStefan Hajnoczi s->bs = bs; 399eabba580SStefan Hajnoczi QSIMPLEQ_INIT(&s->allocating_write_reqs); 40075411d23SStefan Hajnoczi 4019a4f4c31SKevin Wolf ret = bdrv_pread(bs->file->bs, 0, &le_header, sizeof(le_header)); 40275411d23SStefan Hajnoczi if (ret < 0) { 40375411d23SStefan Hajnoczi return ret; 40475411d23SStefan Hajnoczi } 40575411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header); 40675411d23SStefan Hajnoczi 40775411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) { 40876abe407SPaolo Bonzini error_setg(errp, "Image not in QED format"); 40976abe407SPaolo Bonzini return -EINVAL; 41075411d23SStefan Hajnoczi } 41175411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) { 41210b758e8SKevin Wolf /* image uses unsupported feature bits */ 41310b758e8SKevin Wolf char buf[64]; 41410b758e8SKevin Wolf snprintf(buf, sizeof(buf), "%" PRIx64, 41510b758e8SKevin Wolf s->header.features & ~QED_FEATURE_MASK); 416c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, 41781e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs), "QED", buf); 41810b758e8SKevin Wolf return -ENOTSUP; 41975411d23SStefan Hajnoczi } 42075411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 42175411d23SStefan Hajnoczi return -EINVAL; 42275411d23SStefan Hajnoczi } 42375411d23SStefan Hajnoczi 42475411d23SStefan Hajnoczi /* Round down file size to the last cluster */ 4259a4f4c31SKevin Wolf file_size = bdrv_getlength(bs->file->bs); 42675411d23SStefan Hajnoczi if (file_size < 0) { 42775411d23SStefan Hajnoczi return file_size; 42875411d23SStefan Hajnoczi } 42975411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size); 43075411d23SStefan Hajnoczi 43175411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) { 43275411d23SStefan Hajnoczi return -EINVAL; 43375411d23SStefan Hajnoczi } 43475411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size, 43575411d23SStefan Hajnoczi s->header.cluster_size, 43675411d23SStefan Hajnoczi s->header.table_size)) { 43775411d23SStefan Hajnoczi return -EINVAL; 43875411d23SStefan Hajnoczi } 43975411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 44075411d23SStefan Hajnoczi return -EINVAL; 44175411d23SStefan Hajnoczi } 44275411d23SStefan Hajnoczi 44375411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) / 44475411d23SStefan Hajnoczi sizeof(uint64_t); 445786a4ea8SStefan Hajnoczi s->l2_shift = ctz32(s->header.cluster_size); 44675411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1; 447786a4ea8SStefan Hajnoczi s->l1_shift = s->l2_shift + ctz32(s->table_nelems); 44875411d23SStefan Hajnoczi 4490adfa1edSStefan Hajnoczi /* Header size calculation must not overflow uint32_t */ 4500adfa1edSStefan Hajnoczi if (s->header.header_size > UINT32_MAX / s->header.cluster_size) { 4510adfa1edSStefan Hajnoczi return -EINVAL; 4520adfa1edSStefan Hajnoczi } 4530adfa1edSStefan Hajnoczi 45475411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) { 45575411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset + 45675411d23SStefan Hajnoczi s->header.backing_filename_size > 45775411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) { 45875411d23SStefan Hajnoczi return -EINVAL; 45975411d23SStefan Hajnoczi } 46075411d23SStefan Hajnoczi 4619a4f4c31SKevin Wolf ret = qed_read_string(bs->file->bs, s->header.backing_filename_offset, 46275411d23SStefan Hajnoczi s->header.backing_filename_size, bs->backing_file, 46375411d23SStefan Hajnoczi sizeof(bs->backing_file)); 46475411d23SStefan Hajnoczi if (ret < 0) { 46575411d23SStefan Hajnoczi return ret; 46675411d23SStefan Hajnoczi } 46775411d23SStefan Hajnoczi 46875411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 46975411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 47075411d23SStefan Hajnoczi } 47175411d23SStefan Hajnoczi } 47275411d23SStefan Hajnoczi 47375411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards 47475411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older 47575411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is 47675411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear 47775411d23SStefan Hajnoczi * feature is no longer valid. 47875411d23SStefan Hajnoczi */ 47975411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 4809a4f4c31SKevin Wolf !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INCOMING)) { 48175411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 48275411d23SStefan Hajnoczi 48375411d23SStefan Hajnoczi ret = qed_write_header_sync(s); 48475411d23SStefan Hajnoczi if (ret) { 48575411d23SStefan Hajnoczi return ret; 48675411d23SStefan Hajnoczi } 48775411d23SStefan Hajnoczi 48875411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */ 4899a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 49075411d23SStefan Hajnoczi } 49175411d23SStefan Hajnoczi 492298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s); 493298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache); 494298800caSStefan Hajnoczi 495298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s); 496298800caSStefan Hajnoczi if (ret) { 49701979a98SStefan Hajnoczi goto out; 49801979a98SStefan Hajnoczi } 49901979a98SStefan Hajnoczi 50001979a98SStefan Hajnoczi /* If image was not closed cleanly, check consistency */ 501058f8f16SStefan Hajnoczi if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) { 50201979a98SStefan Hajnoczi /* Read-only images cannot be fixed. There is no risk of corruption 50301979a98SStefan Hajnoczi * since write operations are not possible. Therefore, allow 50401979a98SStefan Hajnoczi * potentially inconsistent images to be opened read-only. This can 50501979a98SStefan Hajnoczi * aid data recovery from an otherwise inconsistent image. 50601979a98SStefan Hajnoczi */ 5079a4f4c31SKevin Wolf if (!bdrv_is_read_only(bs->file->bs) && 5082d1f3c23SBenoît Canet !(flags & BDRV_O_INCOMING)) { 50901979a98SStefan Hajnoczi BdrvCheckResult result = {0}; 51001979a98SStefan Hajnoczi 51101979a98SStefan Hajnoczi ret = qed_check(s, &result, true); 5126f321e93SStefan Hajnoczi if (ret) { 5136f321e93SStefan Hajnoczi goto out; 5146f321e93SStefan Hajnoczi } 51501979a98SStefan Hajnoczi } 51601979a98SStefan Hajnoczi } 51701979a98SStefan Hajnoczi 518a8c868c3SStefan Hajnoczi bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs)); 5196f321e93SStefan Hajnoczi 52001979a98SStefan Hajnoczi out: 52101979a98SStefan Hajnoczi if (ret) { 522298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 523298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 524298800caSStefan Hajnoczi } 52575411d23SStefan Hajnoczi return ret; 52675411d23SStefan Hajnoczi } 52775411d23SStefan Hajnoczi 5283baca891SKevin Wolf static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp) 529d34682cdSKevin Wolf { 530d34682cdSKevin Wolf BDRVQEDState *s = bs->opaque; 531d34682cdSKevin Wolf 532d34682cdSKevin Wolf bs->bl.write_zeroes_alignment = s->header.cluster_size >> BDRV_SECTOR_BITS; 533d34682cdSKevin Wolf } 534d34682cdSKevin Wolf 535f9cb20f1SJeff Cody /* We have nothing to do for QED reopen, stubs just return 536f9cb20f1SJeff Cody * success */ 537f9cb20f1SJeff Cody static int bdrv_qed_reopen_prepare(BDRVReopenState *state, 538f9cb20f1SJeff Cody BlockReopenQueue *queue, Error **errp) 539f9cb20f1SJeff Cody { 540f9cb20f1SJeff Cody return 0; 541f9cb20f1SJeff Cody } 542f9cb20f1SJeff Cody 54375411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 54475411d23SStefan Hajnoczi { 545298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 546298800caSStefan Hajnoczi 547a8c868c3SStefan Hajnoczi bdrv_qed_detach_aio_context(bs); 5486f321e93SStefan Hajnoczi 54901979a98SStefan Hajnoczi /* Ensure writes reach stable storage */ 5509a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 55101979a98SStefan Hajnoczi 55201979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */ 55301979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 55401979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 55501979a98SStefan Hajnoczi qed_write_header_sync(s); 55601979a98SStefan Hajnoczi } 55701979a98SStefan Hajnoczi 558298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 559298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 56075411d23SStefan Hajnoczi } 56175411d23SStefan Hajnoczi 56275411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size, 56375411d23SStefan Hajnoczi uint64_t image_size, uint32_t table_size, 5640fea6b79SPaolo Bonzini const char *backing_file, const char *backing_fmt, 5654ab15590SChunyan Liu QemuOpts *opts, Error **errp) 56675411d23SStefan Hajnoczi { 56775411d23SStefan Hajnoczi QEDHeader header = { 56875411d23SStefan Hajnoczi .magic = QED_MAGIC, 56975411d23SStefan Hajnoczi .cluster_size = cluster_size, 57075411d23SStefan Hajnoczi .table_size = table_size, 57175411d23SStefan Hajnoczi .header_size = 1, 57275411d23SStefan Hajnoczi .features = 0, 57375411d23SStefan Hajnoczi .compat_features = 0, 57475411d23SStefan Hajnoczi .l1_table_offset = cluster_size, 57575411d23SStefan Hajnoczi .image_size = image_size, 57675411d23SStefan Hajnoczi }; 57775411d23SStefan Hajnoczi QEDHeader le_header; 57875411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 57975411d23SStefan Hajnoczi size_t l1_size = header.cluster_size * header.table_size; 58034b5d2c6SMax Reitz Error *local_err = NULL; 58175411d23SStefan Hajnoczi int ret = 0; 5822e40134bSMax Reitz BlockDriverState *bs; 58375411d23SStefan Hajnoczi 5844ab15590SChunyan Liu ret = bdrv_create_file(filename, opts, &local_err); 58575411d23SStefan Hajnoczi if (ret < 0) { 5860fea6b79SPaolo Bonzini error_propagate(errp, local_err); 58775411d23SStefan Hajnoczi return ret; 58875411d23SStefan Hajnoczi } 58975411d23SStefan Hajnoczi 5902e40134bSMax Reitz bs = NULL; 5912e40134bSMax Reitz ret = bdrv_open(&bs, filename, NULL, NULL, 5926ebf9aa2SMax Reitz BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL, 5932e40134bSMax Reitz &local_err); 59475411d23SStefan Hajnoczi if (ret < 0) { 5950fea6b79SPaolo Bonzini error_propagate(errp, local_err); 59675411d23SStefan Hajnoczi return ret; 59775411d23SStefan Hajnoczi } 59875411d23SStefan Hajnoczi 599c743849bSStefan Hajnoczi /* File must start empty and grow, check truncate is supported */ 600c743849bSStefan Hajnoczi ret = bdrv_truncate(bs, 0); 601c743849bSStefan Hajnoczi if (ret < 0) { 602c743849bSStefan Hajnoczi goto out; 603c743849bSStefan Hajnoczi } 604c743849bSStefan Hajnoczi 60575411d23SStefan Hajnoczi if (backing_file) { 60675411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 60775411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 60875411d23SStefan Hajnoczi header.backing_filename_size = strlen(backing_file); 60975411d23SStefan Hajnoczi 61075411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 61175411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 61275411d23SStefan Hajnoczi } 61375411d23SStefan Hajnoczi } 61475411d23SStefan Hajnoczi 61575411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 61675411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header)); 61775411d23SStefan Hajnoczi if (ret < 0) { 61875411d23SStefan Hajnoczi goto out; 61975411d23SStefan Hajnoczi } 62075411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, sizeof(le_header), backing_file, 62175411d23SStefan Hajnoczi header.backing_filename_size); 62275411d23SStefan Hajnoczi if (ret < 0) { 62375411d23SStefan Hajnoczi goto out; 62475411d23SStefan Hajnoczi } 62575411d23SStefan Hajnoczi 6267267c094SAnthony Liguori l1_table = g_malloc0(l1_size); 62775411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size); 62875411d23SStefan Hajnoczi if (ret < 0) { 62975411d23SStefan Hajnoczi goto out; 63075411d23SStefan Hajnoczi } 63175411d23SStefan Hajnoczi 63275411d23SStefan Hajnoczi ret = 0; /* success */ 63375411d23SStefan Hajnoczi out: 6347267c094SAnthony Liguori g_free(l1_table); 6354f6fd349SFam Zheng bdrv_unref(bs); 63675411d23SStefan Hajnoczi return ret; 63775411d23SStefan Hajnoczi } 63875411d23SStefan Hajnoczi 6397ab74849SChunyan Liu static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp) 64075411d23SStefan Hajnoczi { 64175411d23SStefan Hajnoczi uint64_t image_size = 0; 64275411d23SStefan Hajnoczi uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 64375411d23SStefan Hajnoczi uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 6447ab74849SChunyan Liu char *backing_file = NULL; 6457ab74849SChunyan Liu char *backing_fmt = NULL; 6467ab74849SChunyan Liu int ret; 64775411d23SStefan Hajnoczi 648c2eb918eSHu Tao image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 649c2eb918eSHu Tao BDRV_SECTOR_SIZE); 6507ab74849SChunyan Liu backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); 6517ab74849SChunyan Liu backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); 6527ab74849SChunyan Liu cluster_size = qemu_opt_get_size_del(opts, 6537ab74849SChunyan Liu BLOCK_OPT_CLUSTER_SIZE, 6547ab74849SChunyan Liu QED_DEFAULT_CLUSTER_SIZE); 6557ab74849SChunyan Liu table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE, 6567ab74849SChunyan Liu QED_DEFAULT_TABLE_SIZE); 65775411d23SStefan Hajnoczi 65875411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(cluster_size)) { 6595ff679b4SAakriti Gupta error_setg(errp, "QED cluster size must be within range [%u, %u] " 6605ff679b4SAakriti Gupta "and power of 2", 66175411d23SStefan Hajnoczi QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 6627ab74849SChunyan Liu ret = -EINVAL; 6637ab74849SChunyan Liu goto finish; 66475411d23SStefan Hajnoczi } 66575411d23SStefan Hajnoczi if (!qed_is_table_size_valid(table_size)) { 6665ff679b4SAakriti Gupta error_setg(errp, "QED table size must be within range [%u, %u] " 6675ff679b4SAakriti Gupta "and power of 2", 66875411d23SStefan Hajnoczi QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 6697ab74849SChunyan Liu ret = -EINVAL; 6707ab74849SChunyan Liu goto finish; 67175411d23SStefan Hajnoczi } 67275411d23SStefan Hajnoczi if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 6735ff679b4SAakriti Gupta error_setg(errp, "QED image size must be a non-zero multiple of " 6745ff679b4SAakriti Gupta "cluster size and less than %" PRIu64 " bytes", 67575411d23SStefan Hajnoczi qed_max_image_size(cluster_size, table_size)); 6767ab74849SChunyan Liu ret = -EINVAL; 6777ab74849SChunyan Liu goto finish; 67875411d23SStefan Hajnoczi } 67975411d23SStefan Hajnoczi 6807ab74849SChunyan Liu ret = qed_create(filename, cluster_size, image_size, table_size, 6814ab15590SChunyan Liu backing_file, backing_fmt, opts, errp); 6827ab74849SChunyan Liu 6837ab74849SChunyan Liu finish: 6847ab74849SChunyan Liu g_free(backing_file); 6857ab74849SChunyan Liu g_free(backing_fmt); 6867ab74849SChunyan Liu return ret; 68775411d23SStefan Hajnoczi } 68875411d23SStefan Hajnoczi 689298800caSStefan Hajnoczi typedef struct { 6904bc74be9SPaolo Bonzini BlockDriverState *bs; 691b7d5a5b8SStefan Hajnoczi Coroutine *co; 6924bc74be9SPaolo Bonzini uint64_t pos; 6934bc74be9SPaolo Bonzini int64_t status; 694298800caSStefan Hajnoczi int *pnum; 695298800caSStefan Hajnoczi } QEDIsAllocatedCB; 696298800caSStefan Hajnoczi 697298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 698298800caSStefan Hajnoczi { 699298800caSStefan Hajnoczi QEDIsAllocatedCB *cb = opaque; 7004bc74be9SPaolo Bonzini BDRVQEDState *s = cb->bs->opaque; 701298800caSStefan Hajnoczi *cb->pnum = len / BDRV_SECTOR_SIZE; 7024bc74be9SPaolo Bonzini switch (ret) { 7034bc74be9SPaolo Bonzini case QED_CLUSTER_FOUND: 7044bc74be9SPaolo Bonzini offset |= qed_offset_into_cluster(s, cb->pos); 7054bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; 7064bc74be9SPaolo Bonzini break; 7074bc74be9SPaolo Bonzini case QED_CLUSTER_ZERO: 7084bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_ZERO; 7094bc74be9SPaolo Bonzini break; 7104bc74be9SPaolo Bonzini case QED_CLUSTER_L2: 7114bc74be9SPaolo Bonzini case QED_CLUSTER_L1: 7124bc74be9SPaolo Bonzini cb->status = 0; 7134bc74be9SPaolo Bonzini break; 7144bc74be9SPaolo Bonzini default: 7154bc74be9SPaolo Bonzini assert(ret < 0); 7164bc74be9SPaolo Bonzini cb->status = ret; 7174bc74be9SPaolo Bonzini break; 7184bc74be9SPaolo Bonzini } 7194bc74be9SPaolo Bonzini 720b7d5a5b8SStefan Hajnoczi if (cb->co) { 721b7d5a5b8SStefan Hajnoczi qemu_coroutine_enter(cb->co, NULL); 722b7d5a5b8SStefan Hajnoczi } 723298800caSStefan Hajnoczi } 724298800caSStefan Hajnoczi 725b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs, 726b7d5a5b8SStefan Hajnoczi int64_t sector_num, 72775411d23SStefan Hajnoczi int nb_sectors, int *pnum) 72875411d23SStefan Hajnoczi { 729298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 730298800caSStefan Hajnoczi size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 731298800caSStefan Hajnoczi QEDIsAllocatedCB cb = { 7324bc74be9SPaolo Bonzini .bs = bs, 7334bc74be9SPaolo Bonzini .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE, 7344bc74be9SPaolo Bonzini .status = BDRV_BLOCK_OFFSET_MASK, 735298800caSStefan Hajnoczi .pnum = pnum, 736298800caSStefan Hajnoczi }; 737298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 738298800caSStefan Hajnoczi 7394bc74be9SPaolo Bonzini qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb); 740298800caSStefan Hajnoczi 741b7d5a5b8SStefan Hajnoczi /* Now sleep if the callback wasn't invoked immediately */ 7424bc74be9SPaolo Bonzini while (cb.status == BDRV_BLOCK_OFFSET_MASK) { 743b7d5a5b8SStefan Hajnoczi cb.co = qemu_coroutine_self(); 744b7d5a5b8SStefan Hajnoczi qemu_coroutine_yield(); 745298800caSStefan Hajnoczi } 746298800caSStefan Hajnoczi 747298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 748298800caSStefan Hajnoczi 7494bc74be9SPaolo Bonzini return cb.status; 75075411d23SStefan Hajnoczi } 75175411d23SStefan Hajnoczi 752eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 753eabba580SStefan Hajnoczi { 754eabba580SStefan Hajnoczi return acb->common.bs->opaque; 755eabba580SStefan Hajnoczi } 756eabba580SStefan Hajnoczi 757eabba580SStefan Hajnoczi /** 758eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 759eabba580SStefan Hajnoczi * 760eabba580SStefan Hajnoczi * @s: QED state 761eabba580SStefan Hajnoczi * @pos: Byte position in device 762eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 763f06ee3d4SKevin Wolf * @backing_qiov: Possibly shortened copy of qiov, to be allocated here 764eabba580SStefan Hajnoczi * @cb: Completion function 765eabba580SStefan Hajnoczi * @opaque: User data for completion function 766eabba580SStefan Hajnoczi * 767eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 768eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 769eabba580SStefan Hajnoczi */ 770eabba580SStefan Hajnoczi static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 771eabba580SStefan Hajnoczi QEMUIOVector *qiov, 772f06ee3d4SKevin Wolf QEMUIOVector **backing_qiov, 773097310b5SMarkus Armbruster BlockCompletionFunc *cb, void *opaque) 774eabba580SStefan Hajnoczi { 775eabba580SStefan Hajnoczi uint64_t backing_length = 0; 776eabba580SStefan Hajnoczi size_t size; 777eabba580SStefan Hajnoczi 778eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 779eabba580SStefan Hajnoczi * backing file like a zero length backing file. 780eabba580SStefan Hajnoczi */ 781760e0063SKevin Wolf if (s->bs->backing) { 782760e0063SKevin Wolf int64_t l = bdrv_getlength(s->bs->backing->bs); 783eabba580SStefan Hajnoczi if (l < 0) { 784eabba580SStefan Hajnoczi cb(opaque, l); 785eabba580SStefan Hajnoczi return; 786eabba580SStefan Hajnoczi } 787eabba580SStefan Hajnoczi backing_length = l; 788eabba580SStefan Hajnoczi } 789eabba580SStefan Hajnoczi 790eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 791eabba580SStefan Hajnoczi if (pos >= backing_length || 792eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 7933d9b4925SMichael Tokarev qemu_iovec_memset(qiov, 0, 0, qiov->size); 794eabba580SStefan Hajnoczi } 795eabba580SStefan Hajnoczi 796eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 797eabba580SStefan Hajnoczi if (pos >= backing_length) { 798eabba580SStefan Hajnoczi cb(opaque, 0); 799eabba580SStefan Hajnoczi return; 800eabba580SStefan Hajnoczi } 801eabba580SStefan Hajnoczi 802eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 803eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 804eabba580SStefan Hajnoczi 805f06ee3d4SKevin Wolf assert(*backing_qiov == NULL); 806f06ee3d4SKevin Wolf *backing_qiov = g_new(QEMUIOVector, 1); 807f06ee3d4SKevin Wolf qemu_iovec_init(*backing_qiov, qiov->niov); 808f06ee3d4SKevin Wolf qemu_iovec_concat(*backing_qiov, qiov, 0, size); 809f06ee3d4SKevin Wolf 810820100fdSPaolo Bonzini BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); 811760e0063SKevin Wolf bdrv_aio_readv(s->bs->backing->bs, pos / BDRV_SECTOR_SIZE, 812f06ee3d4SKevin Wolf *backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque); 813eabba580SStefan Hajnoczi } 814eabba580SStefan Hajnoczi 815eabba580SStefan Hajnoczi typedef struct { 816eabba580SStefan Hajnoczi GenericCB gencb; 817eabba580SStefan Hajnoczi BDRVQEDState *s; 818eabba580SStefan Hajnoczi QEMUIOVector qiov; 819f06ee3d4SKevin Wolf QEMUIOVector *backing_qiov; 820eabba580SStefan Hajnoczi struct iovec iov; 821eabba580SStefan Hajnoczi uint64_t offset; 822eabba580SStefan Hajnoczi } CopyFromBackingFileCB; 823eabba580SStefan Hajnoczi 824eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_cb(void *opaque, int ret) 825eabba580SStefan Hajnoczi { 826eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 827eabba580SStefan Hajnoczi qemu_vfree(copy_cb->iov.iov_base); 828eabba580SStefan Hajnoczi gencb_complete(©_cb->gencb, ret); 829eabba580SStefan Hajnoczi } 830eabba580SStefan Hajnoczi 831eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_write(void *opaque, int ret) 832eabba580SStefan Hajnoczi { 833eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 834eabba580SStefan Hajnoczi BDRVQEDState *s = copy_cb->s; 835eabba580SStefan Hajnoczi 836f06ee3d4SKevin Wolf if (copy_cb->backing_qiov) { 837f06ee3d4SKevin Wolf qemu_iovec_destroy(copy_cb->backing_qiov); 838f06ee3d4SKevin Wolf g_free(copy_cb->backing_qiov); 839f06ee3d4SKevin Wolf copy_cb->backing_qiov = NULL; 840f06ee3d4SKevin Wolf } 841f06ee3d4SKevin Wolf 842eabba580SStefan Hajnoczi if (ret) { 843eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb(copy_cb, ret); 844eabba580SStefan Hajnoczi return; 845eabba580SStefan Hajnoczi } 846eabba580SStefan Hajnoczi 847eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 8489a4f4c31SKevin Wolf bdrv_aio_writev(s->bs->file->bs, copy_cb->offset / BDRV_SECTOR_SIZE, 849ad54ae80SPaolo Bonzini ©_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE, 850eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb, copy_cb); 851eabba580SStefan Hajnoczi } 852eabba580SStefan Hajnoczi 853eabba580SStefan Hajnoczi /** 854eabba580SStefan Hajnoczi * Copy data from backing file into the image 855eabba580SStefan Hajnoczi * 856eabba580SStefan Hajnoczi * @s: QED state 857eabba580SStefan Hajnoczi * @pos: Byte position in device 858eabba580SStefan Hajnoczi * @len: Number of bytes 859eabba580SStefan Hajnoczi * @offset: Byte offset in image file 860eabba580SStefan Hajnoczi * @cb: Completion function 861eabba580SStefan Hajnoczi * @opaque: User data for completion function 862eabba580SStefan Hajnoczi */ 863eabba580SStefan Hajnoczi static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, 864eabba580SStefan Hajnoczi uint64_t len, uint64_t offset, 865097310b5SMarkus Armbruster BlockCompletionFunc *cb, 866eabba580SStefan Hajnoczi void *opaque) 867eabba580SStefan Hajnoczi { 868eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb; 869eabba580SStefan Hajnoczi 870eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 871eabba580SStefan Hajnoczi if (len == 0) { 872eabba580SStefan Hajnoczi cb(opaque, 0); 873eabba580SStefan Hajnoczi return; 874eabba580SStefan Hajnoczi } 875eabba580SStefan Hajnoczi 876eabba580SStefan Hajnoczi copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque); 877eabba580SStefan Hajnoczi copy_cb->s = s; 878eabba580SStefan Hajnoczi copy_cb->offset = offset; 879f06ee3d4SKevin Wolf copy_cb->backing_qiov = NULL; 880eabba580SStefan Hajnoczi copy_cb->iov.iov_base = qemu_blockalign(s->bs, len); 881eabba580SStefan Hajnoczi copy_cb->iov.iov_len = len; 882eabba580SStefan Hajnoczi qemu_iovec_init_external(©_cb->qiov, ©_cb->iov, 1); 883eabba580SStefan Hajnoczi 884f06ee3d4SKevin Wolf qed_read_backing_file(s, pos, ©_cb->qiov, ©_cb->backing_qiov, 885eabba580SStefan Hajnoczi qed_copy_from_backing_file_write, copy_cb); 886eabba580SStefan Hajnoczi } 887eabba580SStefan Hajnoczi 888eabba580SStefan Hajnoczi /** 889eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 890eabba580SStefan Hajnoczi * 891eabba580SStefan Hajnoczi * @s: QED state 892eabba580SStefan Hajnoczi * @table: L2 table 893eabba580SStefan Hajnoczi * @index: First cluster index 894eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 89521df65b6SAnthony Liguori * @cluster: First cluster offset 89621df65b6SAnthony Liguori * 89721df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the 89821df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker. 899eabba580SStefan Hajnoczi */ 900eabba580SStefan Hajnoczi static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index, 901eabba580SStefan Hajnoczi unsigned int n, uint64_t cluster) 902eabba580SStefan Hajnoczi { 903eabba580SStefan Hajnoczi int i; 904eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 905eabba580SStefan Hajnoczi table->offsets[i] = cluster; 90621df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) && 90721df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) { 908eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 909eabba580SStefan Hajnoczi } 910eabba580SStefan Hajnoczi } 91121df65b6SAnthony Liguori } 912eabba580SStefan Hajnoczi 913eabba580SStefan Hajnoczi static void qed_aio_complete_bh(void *opaque) 914eabba580SStefan Hajnoczi { 915eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 916097310b5SMarkus Armbruster BlockCompletionFunc *cb = acb->common.cb; 917eabba580SStefan Hajnoczi void *user_opaque = acb->common.opaque; 918eabba580SStefan Hajnoczi int ret = acb->bh_ret; 919eabba580SStefan Hajnoczi 920eabba580SStefan Hajnoczi qemu_bh_delete(acb->bh); 9218007429aSFam Zheng qemu_aio_unref(acb); 922eabba580SStefan Hajnoczi 923eabba580SStefan Hajnoczi /* Invoke callback */ 924eabba580SStefan Hajnoczi cb(user_opaque, ret); 925eabba580SStefan Hajnoczi } 926eabba580SStefan Hajnoczi 927eabba580SStefan Hajnoczi static void qed_aio_complete(QEDAIOCB *acb, int ret) 928eabba580SStefan Hajnoczi { 929eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 930eabba580SStefan Hajnoczi 931eabba580SStefan Hajnoczi trace_qed_aio_complete(s, acb, ret); 932eabba580SStefan Hajnoczi 933eabba580SStefan Hajnoczi /* Free resources */ 934eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov); 935eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 936eabba580SStefan Hajnoczi 9370e71be19SStefan Hajnoczi /* Free the buffer we may have allocated for zero writes */ 9380e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 9390e71be19SStefan Hajnoczi qemu_vfree(acb->qiov->iov[0].iov_base); 9400e71be19SStefan Hajnoczi acb->qiov->iov[0].iov_base = NULL; 9410e71be19SStefan Hajnoczi } 9420e71be19SStefan Hajnoczi 943eabba580SStefan Hajnoczi /* Arrange for a bh to invoke the completion function */ 944eabba580SStefan Hajnoczi acb->bh_ret = ret; 945a8c868c3SStefan Hajnoczi acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs), 946a8c868c3SStefan Hajnoczi qed_aio_complete_bh, acb); 947eabba580SStefan Hajnoczi qemu_bh_schedule(acb->bh); 948eabba580SStefan Hajnoczi 949eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 950eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 951eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 952eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 953eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 954eabba580SStefan Hajnoczi */ 955eabba580SStefan Hajnoczi if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 956eabba580SStefan Hajnoczi QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next); 957eabba580SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 958eabba580SStefan Hajnoczi if (acb) { 959eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 9606f321e93SStefan Hajnoczi } else if (s->header.features & QED_F_NEED_CHECK) { 9616f321e93SStefan Hajnoczi qed_start_need_check_timer(s); 962eabba580SStefan Hajnoczi } 963eabba580SStefan Hajnoczi } 964eabba580SStefan Hajnoczi } 965eabba580SStefan Hajnoczi 966eabba580SStefan Hajnoczi /** 967eabba580SStefan Hajnoczi * Commit the current L2 table to the cache 968eabba580SStefan Hajnoczi */ 969eabba580SStefan Hajnoczi static void qed_commit_l2_update(void *opaque, int ret) 970eabba580SStefan Hajnoczi { 971eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 972eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 973eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 974e4fc8781SStefan Hajnoczi uint64_t l2_offset = l2_table->offset; 975eabba580SStefan Hajnoczi 976eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 977eabba580SStefan Hajnoczi 978eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 979eabba580SStefan Hajnoczi * cache. 980eabba580SStefan Hajnoczi */ 981e4fc8781SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 982eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 983eabba580SStefan Hajnoczi 984eabba580SStefan Hajnoczi qed_aio_next_io(opaque, ret); 985eabba580SStefan Hajnoczi } 986eabba580SStefan Hajnoczi 987eabba580SStefan Hajnoczi /** 988eabba580SStefan Hajnoczi * Update L1 table with new L2 table offset and write it out 989eabba580SStefan Hajnoczi */ 990eabba580SStefan Hajnoczi static void qed_aio_write_l1_update(void *opaque, int ret) 991eabba580SStefan Hajnoczi { 992eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 993eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 994eabba580SStefan Hajnoczi int index; 995eabba580SStefan Hajnoczi 996eabba580SStefan Hajnoczi if (ret) { 997eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 998eabba580SStefan Hajnoczi return; 999eabba580SStefan Hajnoczi } 1000eabba580SStefan Hajnoczi 1001eabba580SStefan Hajnoczi index = qed_l1_index(s, acb->cur_pos); 1002eabba580SStefan Hajnoczi s->l1_table->offsets[index] = acb->request.l2_table->offset; 1003eabba580SStefan Hajnoczi 1004eabba580SStefan Hajnoczi qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb); 1005eabba580SStefan Hajnoczi } 1006eabba580SStefan Hajnoczi 1007eabba580SStefan Hajnoczi /** 1008eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 1009eabba580SStefan Hajnoczi */ 10100e71be19SStefan Hajnoczi static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset) 1011eabba580SStefan Hajnoczi { 1012eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1013eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 1014eabba580SStefan Hajnoczi int index; 1015eabba580SStefan Hajnoczi 1016eabba580SStefan Hajnoczi if (ret) { 1017eabba580SStefan Hajnoczi goto err; 1018eabba580SStefan Hajnoczi } 1019eabba580SStefan Hajnoczi 1020eabba580SStefan Hajnoczi if (need_alloc) { 1021eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 1022eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 1023eabba580SStefan Hajnoczi } 1024eabba580SStefan Hajnoczi 1025eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 1026eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 10270e71be19SStefan Hajnoczi offset); 1028eabba580SStefan Hajnoczi 1029eabba580SStefan Hajnoczi if (need_alloc) { 1030eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 1031eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true, 1032eabba580SStefan Hajnoczi qed_aio_write_l1_update, acb); 1033eabba580SStefan Hajnoczi } else { 1034eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 1035eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false, 1036eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1037eabba580SStefan Hajnoczi } 1038eabba580SStefan Hajnoczi return; 1039eabba580SStefan Hajnoczi 1040eabba580SStefan Hajnoczi err: 1041eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1042eabba580SStefan Hajnoczi } 1043eabba580SStefan Hajnoczi 10440e71be19SStefan Hajnoczi static void qed_aio_write_l2_update_cb(void *opaque, int ret) 10450e71be19SStefan Hajnoczi { 10460e71be19SStefan Hajnoczi QEDAIOCB *acb = opaque; 10470e71be19SStefan Hajnoczi qed_aio_write_l2_update(acb, ret, acb->cur_cluster); 10480e71be19SStefan Hajnoczi } 10490e71be19SStefan Hajnoczi 1050eabba580SStefan Hajnoczi /** 1051eabba580SStefan Hajnoczi * Flush new data clusters before updating the L2 table 1052eabba580SStefan Hajnoczi * 1053eabba580SStefan Hajnoczi * This flush is necessary when a backing file is in use. A crash during an 1054eabba580SStefan Hajnoczi * allocating write could result in empty clusters in the image. If the write 1055eabba580SStefan Hajnoczi * only touched a subregion of the cluster, then backing image sectors have 1056eabba580SStefan Hajnoczi * been lost in the untouched region. The solution is to flush after writing a 1057eabba580SStefan Hajnoczi * new data cluster and before updating the L2 table. 1058eabba580SStefan Hajnoczi */ 1059eabba580SStefan Hajnoczi static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) 1060eabba580SStefan Hajnoczi { 1061eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1062eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1063eabba580SStefan Hajnoczi 10649a4f4c31SKevin Wolf if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) { 1065eabba580SStefan Hajnoczi qed_aio_complete(acb, -EIO); 1066eabba580SStefan Hajnoczi } 1067eabba580SStefan Hajnoczi } 1068eabba580SStefan Hajnoczi 1069eabba580SStefan Hajnoczi /** 1070eabba580SStefan Hajnoczi * Write data to the image file 1071eabba580SStefan Hajnoczi */ 1072eabba580SStefan Hajnoczi static void qed_aio_write_main(void *opaque, int ret) 1073eabba580SStefan Hajnoczi { 1074eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1075eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1076eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1077eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 1078097310b5SMarkus Armbruster BlockCompletionFunc *next_fn; 1079eabba580SStefan Hajnoczi 1080eabba580SStefan Hajnoczi trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size); 1081eabba580SStefan Hajnoczi 1082eabba580SStefan Hajnoczi if (ret) { 1083eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1084eabba580SStefan Hajnoczi return; 1085eabba580SStefan Hajnoczi } 1086eabba580SStefan Hajnoczi 1087eabba580SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_FOUND) { 1088eabba580SStefan Hajnoczi next_fn = qed_aio_next_io; 1089eabba580SStefan Hajnoczi } else { 1090760e0063SKevin Wolf if (s->bs->backing) { 1091eabba580SStefan Hajnoczi next_fn = qed_aio_write_flush_before_l2_update; 1092eabba580SStefan Hajnoczi } else { 10930e71be19SStefan Hajnoczi next_fn = qed_aio_write_l2_update_cb; 1094eabba580SStefan Hajnoczi } 1095eabba580SStefan Hajnoczi } 1096eabba580SStefan Hajnoczi 1097eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 10989a4f4c31SKevin Wolf bdrv_aio_writev(s->bs->file->bs, offset / BDRV_SECTOR_SIZE, 1099ad54ae80SPaolo Bonzini &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1100eabba580SStefan Hajnoczi next_fn, acb); 1101eabba580SStefan Hajnoczi } 1102eabba580SStefan Hajnoczi 1103eabba580SStefan Hajnoczi /** 1104eabba580SStefan Hajnoczi * Populate back untouched region of new data cluster 1105eabba580SStefan Hajnoczi */ 1106eabba580SStefan Hajnoczi static void qed_aio_write_postfill(void *opaque, int ret) 1107eabba580SStefan Hajnoczi { 1108eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1109eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1110eabba580SStefan Hajnoczi uint64_t start = acb->cur_pos + acb->cur_qiov.size; 1111eabba580SStefan Hajnoczi uint64_t len = 1112eabba580SStefan Hajnoczi qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1113eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1114eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + 1115eabba580SStefan Hajnoczi acb->cur_qiov.size; 1116eabba580SStefan Hajnoczi 1117eabba580SStefan Hajnoczi if (ret) { 1118eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1119eabba580SStefan Hajnoczi return; 1120eabba580SStefan Hajnoczi } 1121eabba580SStefan Hajnoczi 1122eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 1123eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, offset, 1124eabba580SStefan Hajnoczi qed_aio_write_main, acb); 1125eabba580SStefan Hajnoczi } 1126eabba580SStefan Hajnoczi 1127eabba580SStefan Hajnoczi /** 1128eabba580SStefan Hajnoczi * Populate front untouched region of new data cluster 1129eabba580SStefan Hajnoczi */ 1130eabba580SStefan Hajnoczi static void qed_aio_write_prefill(void *opaque, int ret) 1131eabba580SStefan Hajnoczi { 1132eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1133eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1134eabba580SStefan Hajnoczi uint64_t start = qed_start_of_cluster(s, acb->cur_pos); 1135eabba580SStefan Hajnoczi uint64_t len = qed_offset_into_cluster(s, acb->cur_pos); 1136eabba580SStefan Hajnoczi 1137eabba580SStefan Hajnoczi trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1138eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, acb->cur_cluster, 1139eabba580SStefan Hajnoczi qed_aio_write_postfill, acb); 1140eabba580SStefan Hajnoczi } 1141eabba580SStefan Hajnoczi 1142eabba580SStefan Hajnoczi /** 11430d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write 11440d09c797SStefan Hajnoczi */ 11450d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s) 11460d09c797SStefan Hajnoczi { 11470d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */ 1148760e0063SKevin Wolf if (s->bs->backing) { 11490d09c797SStefan Hajnoczi return false; 11500d09c797SStefan Hajnoczi } 11510d09c797SStefan Hajnoczi 11520d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK); 11530d09c797SStefan Hajnoczi } 11540d09c797SStefan Hajnoczi 11550e71be19SStefan Hajnoczi static void qed_aio_write_zero_cluster(void *opaque, int ret) 11560e71be19SStefan Hajnoczi { 11570e71be19SStefan Hajnoczi QEDAIOCB *acb = opaque; 11580e71be19SStefan Hajnoczi 11590e71be19SStefan Hajnoczi if (ret) { 11600e71be19SStefan Hajnoczi qed_aio_complete(acb, ret); 11610e71be19SStefan Hajnoczi return; 11620e71be19SStefan Hajnoczi } 11630e71be19SStefan Hajnoczi 11640e71be19SStefan Hajnoczi qed_aio_write_l2_update(acb, 0, 1); 11650e71be19SStefan Hajnoczi } 11660e71be19SStefan Hajnoczi 11670d09c797SStefan Hajnoczi /** 1168eabba580SStefan Hajnoczi * Write new data cluster 1169eabba580SStefan Hajnoczi * 1170eabba580SStefan Hajnoczi * @acb: Write request 1171eabba580SStefan Hajnoczi * @len: Length in bytes 1172eabba580SStefan Hajnoczi * 1173eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters. 1174eabba580SStefan Hajnoczi */ 1175eabba580SStefan Hajnoczi static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1176eabba580SStefan Hajnoczi { 1177eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1178097310b5SMarkus Armbruster BlockCompletionFunc *cb; 1179eabba580SStefan Hajnoczi 11806f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */ 11816f321e93SStefan Hajnoczi if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) { 11826f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 11836f321e93SStefan Hajnoczi } 11846f321e93SStefan Hajnoczi 1185eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 1186eabba580SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 1187eabba580SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next); 1188eabba580SStefan Hajnoczi } 11896f321e93SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) || 11906f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged) { 1191eabba580SStefan Hajnoczi return; /* wait for existing request to finish */ 1192eabba580SStefan Hajnoczi } 1193eabba580SStefan Hajnoczi 1194eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 1195eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 11961b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1197eabba580SStefan Hajnoczi 11980e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 11990e71be19SStefan Hajnoczi /* Skip ahead if the clusters are already zero */ 12000e71be19SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { 12010e71be19SStefan Hajnoczi qed_aio_next_io(acb, 0); 12020e71be19SStefan Hajnoczi return; 12030e71be19SStefan Hajnoczi } 12040e71be19SStefan Hajnoczi 12050e71be19SStefan Hajnoczi cb = qed_aio_write_zero_cluster; 12060e71be19SStefan Hajnoczi } else { 12070e71be19SStefan Hajnoczi cb = qed_aio_write_prefill; 12080e71be19SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 12090e71be19SStefan Hajnoczi } 12100e71be19SStefan Hajnoczi 12110d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) { 121201979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK; 12130e71be19SStefan Hajnoczi qed_write_header(s, cb, acb); 12140d09c797SStefan Hajnoczi } else { 12150e71be19SStefan Hajnoczi cb(acb, 0); 12160d09c797SStefan Hajnoczi } 1217eabba580SStefan Hajnoczi } 1218eabba580SStefan Hajnoczi 1219eabba580SStefan Hajnoczi /** 1220eabba580SStefan Hajnoczi * Write data cluster in place 1221eabba580SStefan Hajnoczi * 1222eabba580SStefan Hajnoczi * @acb: Write request 1223eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1224eabba580SStefan Hajnoczi * @len: Length in bytes 1225eabba580SStefan Hajnoczi * 1226eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 1227eabba580SStefan Hajnoczi */ 1228eabba580SStefan Hajnoczi static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) 1229eabba580SStefan Hajnoczi { 12300e71be19SStefan Hajnoczi /* Allocate buffer for zero writes */ 12310e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 12320e71be19SStefan Hajnoczi struct iovec *iov = acb->qiov->iov; 12330e71be19SStefan Hajnoczi 12340e71be19SStefan Hajnoczi if (!iov->iov_base) { 12354f4896dbSKevin Wolf iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len); 12364f4896dbSKevin Wolf if (iov->iov_base == NULL) { 12374f4896dbSKevin Wolf qed_aio_complete(acb, -ENOMEM); 12384f4896dbSKevin Wolf return; 12394f4896dbSKevin Wolf } 12400e71be19SStefan Hajnoczi memset(iov->iov_base, 0, iov->iov_len); 12410e71be19SStefan Hajnoczi } 12420e71be19SStefan Hajnoczi } 12430e71be19SStefan Hajnoczi 1244eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 1245eabba580SStefan Hajnoczi acb->cur_cluster = offset; 12461b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1247eabba580SStefan Hajnoczi 1248eabba580SStefan Hajnoczi /* Do the actual write */ 1249eabba580SStefan Hajnoczi qed_aio_write_main(acb, 0); 1250eabba580SStefan Hajnoczi } 1251eabba580SStefan Hajnoczi 1252eabba580SStefan Hajnoczi /** 1253eabba580SStefan Hajnoczi * Write data cluster 1254eabba580SStefan Hajnoczi * 1255eabba580SStefan Hajnoczi * @opaque: Write request 1256eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1257eabba580SStefan Hajnoczi * or -errno 1258eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1259eabba580SStefan Hajnoczi * @len: Length in bytes 1260eabba580SStefan Hajnoczi * 1261eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1262eabba580SStefan Hajnoczi */ 1263eabba580SStefan Hajnoczi static void qed_aio_write_data(void *opaque, int ret, 1264eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1265eabba580SStefan Hajnoczi { 1266eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1267eabba580SStefan Hajnoczi 1268eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1269eabba580SStefan Hajnoczi 1270eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 1271eabba580SStefan Hajnoczi 1272eabba580SStefan Hajnoczi switch (ret) { 1273eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 1274eabba580SStefan Hajnoczi qed_aio_write_inplace(acb, offset, len); 1275eabba580SStefan Hajnoczi break; 1276eabba580SStefan Hajnoczi 1277eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 1278eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 127921df65b6SAnthony Liguori case QED_CLUSTER_ZERO: 1280eabba580SStefan Hajnoczi qed_aio_write_alloc(acb, len); 1281eabba580SStefan Hajnoczi break; 1282eabba580SStefan Hajnoczi 1283eabba580SStefan Hajnoczi default: 1284eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1285eabba580SStefan Hajnoczi break; 1286eabba580SStefan Hajnoczi } 1287eabba580SStefan Hajnoczi } 1288eabba580SStefan Hajnoczi 1289eabba580SStefan Hajnoczi /** 1290eabba580SStefan Hajnoczi * Read data cluster 1291eabba580SStefan Hajnoczi * 1292eabba580SStefan Hajnoczi * @opaque: Read request 1293eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1294eabba580SStefan Hajnoczi * or -errno 1295eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1296eabba580SStefan Hajnoczi * @len: Length in bytes 1297eabba580SStefan Hajnoczi * 1298eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1299eabba580SStefan Hajnoczi */ 1300eabba580SStefan Hajnoczi static void qed_aio_read_data(void *opaque, int ret, 1301eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1302eabba580SStefan Hajnoczi { 1303eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1304eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1305eabba580SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 1306eabba580SStefan Hajnoczi 1307eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 1308eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 1309eabba580SStefan Hajnoczi 1310eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 1311eabba580SStefan Hajnoczi 1312eabba580SStefan Hajnoczi if (ret < 0) { 1313eabba580SStefan Hajnoczi goto err; 1314eabba580SStefan Hajnoczi } 1315eabba580SStefan Hajnoczi 13161b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1317eabba580SStefan Hajnoczi 131821df65b6SAnthony Liguori /* Handle zero cluster and backing file reads */ 131921df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) { 13203d9b4925SMichael Tokarev qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); 132121df65b6SAnthony Liguori qed_aio_next_io(acb, 0); 132221df65b6SAnthony Liguori return; 132321df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) { 1324eabba580SStefan Hajnoczi qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1325f06ee3d4SKevin Wolf &acb->backing_qiov, qed_aio_next_io, acb); 1326eabba580SStefan Hajnoczi return; 1327eabba580SStefan Hajnoczi } 1328eabba580SStefan Hajnoczi 1329eabba580SStefan Hajnoczi BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 13309a4f4c31SKevin Wolf bdrv_aio_readv(bs->file->bs, offset / BDRV_SECTOR_SIZE, 1331ad54ae80SPaolo Bonzini &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1332eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1333eabba580SStefan Hajnoczi return; 1334eabba580SStefan Hajnoczi 1335eabba580SStefan Hajnoczi err: 1336eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1337eabba580SStefan Hajnoczi } 1338eabba580SStefan Hajnoczi 1339eabba580SStefan Hajnoczi /** 1340eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1341eabba580SStefan Hajnoczi */ 1342eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret) 1343eabba580SStefan Hajnoczi { 1344eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1345eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 13466e4f59bdSStefan Hajnoczi QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ? 13476e4f59bdSStefan Hajnoczi qed_aio_write_data : qed_aio_read_data; 1348eabba580SStefan Hajnoczi 1349eabba580SStefan Hajnoczi trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size); 1350eabba580SStefan Hajnoczi 1351f06ee3d4SKevin Wolf if (acb->backing_qiov) { 1352f06ee3d4SKevin Wolf qemu_iovec_destroy(acb->backing_qiov); 1353f06ee3d4SKevin Wolf g_free(acb->backing_qiov); 1354f06ee3d4SKevin Wolf acb->backing_qiov = NULL; 1355f06ee3d4SKevin Wolf } 1356f06ee3d4SKevin Wolf 1357eabba580SStefan Hajnoczi /* Handle I/O error */ 1358eabba580SStefan Hajnoczi if (ret) { 1359eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1360eabba580SStefan Hajnoczi return; 1361eabba580SStefan Hajnoczi } 1362eabba580SStefan Hajnoczi 1363eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1364eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1365eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1366eabba580SStefan Hajnoczi 1367eabba580SStefan Hajnoczi /* Complete request */ 1368eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 1369eabba580SStefan Hajnoczi qed_aio_complete(acb, 0); 1370eabba580SStefan Hajnoczi return; 1371eabba580SStefan Hajnoczi } 1372eabba580SStefan Hajnoczi 1373eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 1374eabba580SStefan Hajnoczi qed_find_cluster(s, &acb->request, 1375eabba580SStefan Hajnoczi acb->cur_pos, acb->end_pos - acb->cur_pos, 1376eabba580SStefan Hajnoczi io_fn, acb); 1377eabba580SStefan Hajnoczi } 1378eabba580SStefan Hajnoczi 13797c84b1b8SMarkus Armbruster static BlockAIOCB *qed_aio_setup(BlockDriverState *bs, 1380eabba580SStefan Hajnoczi int64_t sector_num, 1381eabba580SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1382097310b5SMarkus Armbruster BlockCompletionFunc *cb, 13836e4f59bdSStefan Hajnoczi void *opaque, int flags) 1384eabba580SStefan Hajnoczi { 1385d7331bedSStefan Hajnoczi QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque); 1386eabba580SStefan Hajnoczi 1387eabba580SStefan Hajnoczi trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, 13886e4f59bdSStefan Hajnoczi opaque, flags); 1389eabba580SStefan Hajnoczi 13906e4f59bdSStefan Hajnoczi acb->flags = flags; 1391eabba580SStefan Hajnoczi acb->qiov = qiov; 1392eabba580SStefan Hajnoczi acb->qiov_offset = 0; 1393eabba580SStefan Hajnoczi acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 1394eabba580SStefan Hajnoczi acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE; 1395f06ee3d4SKevin Wolf acb->backing_qiov = NULL; 1396eabba580SStefan Hajnoczi acb->request.l2_table = NULL; 1397eabba580SStefan Hajnoczi qemu_iovec_init(&acb->cur_qiov, qiov->niov); 1398eabba580SStefan Hajnoczi 1399eabba580SStefan Hajnoczi /* Start request */ 1400eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 1401eabba580SStefan Hajnoczi return &acb->common; 1402eabba580SStefan Hajnoczi } 1403eabba580SStefan Hajnoczi 14047c84b1b8SMarkus Armbruster static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs, 140575411d23SStefan Hajnoczi int64_t sector_num, 140675411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1407097310b5SMarkus Armbruster BlockCompletionFunc *cb, 140875411d23SStefan Hajnoczi void *opaque) 140975411d23SStefan Hajnoczi { 14106e4f59bdSStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 141175411d23SStefan Hajnoczi } 141275411d23SStefan Hajnoczi 14137c84b1b8SMarkus Armbruster static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs, 141475411d23SStefan Hajnoczi int64_t sector_num, 141575411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1416097310b5SMarkus Armbruster BlockCompletionFunc *cb, 141775411d23SStefan Hajnoczi void *opaque) 141875411d23SStefan Hajnoczi { 14196e4f59bdSStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, 14206e4f59bdSStefan Hajnoczi opaque, QED_AIOCB_WRITE); 142175411d23SStefan Hajnoczi } 142275411d23SStefan Hajnoczi 14230e71be19SStefan Hajnoczi typedef struct { 14240e71be19SStefan Hajnoczi Coroutine *co; 14250e71be19SStefan Hajnoczi int ret; 14260e71be19SStefan Hajnoczi bool done; 14270e71be19SStefan Hajnoczi } QEDWriteZeroesCB; 14280e71be19SStefan Hajnoczi 14290e71be19SStefan Hajnoczi static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret) 14300e71be19SStefan Hajnoczi { 14310e71be19SStefan Hajnoczi QEDWriteZeroesCB *cb = opaque; 14320e71be19SStefan Hajnoczi 14330e71be19SStefan Hajnoczi cb->done = true; 14340e71be19SStefan Hajnoczi cb->ret = ret; 14350e71be19SStefan Hajnoczi if (cb->co) { 14360e71be19SStefan Hajnoczi qemu_coroutine_enter(cb->co, NULL); 14370e71be19SStefan Hajnoczi } 14380e71be19SStefan Hajnoczi } 14390e71be19SStefan Hajnoczi 14400e71be19SStefan Hajnoczi static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs, 14410e71be19SStefan Hajnoczi int64_t sector_num, 1442aa7bfbffSPeter Lieven int nb_sectors, 1443aa7bfbffSPeter Lieven BdrvRequestFlags flags) 14440e71be19SStefan Hajnoczi { 14457c84b1b8SMarkus Armbruster BlockAIOCB *blockacb; 1446ef72f76eSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 14470e71be19SStefan Hajnoczi QEDWriteZeroesCB cb = { .done = false }; 14480e71be19SStefan Hajnoczi QEMUIOVector qiov; 14490e71be19SStefan Hajnoczi struct iovec iov; 14500e71be19SStefan Hajnoczi 1451ef72f76eSStefan Hajnoczi /* Refuse if there are untouched backing file sectors */ 1452760e0063SKevin Wolf if (bs->backing) { 1453ef72f76eSStefan Hajnoczi if (qed_offset_into_cluster(s, sector_num * BDRV_SECTOR_SIZE) != 0) { 1454ef72f76eSStefan Hajnoczi return -ENOTSUP; 1455ef72f76eSStefan Hajnoczi } 1456ef72f76eSStefan Hajnoczi if (qed_offset_into_cluster(s, nb_sectors * BDRV_SECTOR_SIZE) != 0) { 1457ef72f76eSStefan Hajnoczi return -ENOTSUP; 1458ef72f76eSStefan Hajnoczi } 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 */ 14640e71be19SStefan Hajnoczi iov.iov_base = NULL, 14650e71be19SStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE, 14660e71be19SStefan Hajnoczi 14670e71be19SStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 14680e71be19SStefan Hajnoczi blockacb = qed_aio_setup(bs, sector_num, &qiov, nb_sectors, 14690e71be19SStefan Hajnoczi qed_co_write_zeroes_cb, &cb, 14700e71be19SStefan Hajnoczi QED_AIOCB_WRITE | QED_AIOCB_ZERO); 14710e71be19SStefan Hajnoczi if (!blockacb) { 14720e71be19SStefan Hajnoczi return -EIO; 14730e71be19SStefan Hajnoczi } 14740e71be19SStefan Hajnoczi if (!cb.done) { 14750e71be19SStefan Hajnoczi cb.co = qemu_coroutine_self(); 14760e71be19SStefan Hajnoczi qemu_coroutine_yield(); 14770e71be19SStefan Hajnoczi } 14780e71be19SStefan Hajnoczi assert(cb.done); 14790e71be19SStefan Hajnoczi return cb.ret; 14800e71be19SStefan Hajnoczi } 14810e71be19SStefan Hajnoczi 148275411d23SStefan Hajnoczi static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset) 148375411d23SStefan Hajnoczi { 148477a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 148577a5a000SStefan Hajnoczi uint64_t old_image_size; 148677a5a000SStefan Hajnoczi int ret; 148777a5a000SStefan Hajnoczi 148877a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size, 148977a5a000SStefan Hajnoczi s->header.table_size)) { 149077a5a000SStefan Hajnoczi return -EINVAL; 149177a5a000SStefan Hajnoczi } 149277a5a000SStefan Hajnoczi 149377a5a000SStefan Hajnoczi /* Shrinking is currently not supported */ 149477a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) { 149575411d23SStefan Hajnoczi return -ENOTSUP; 149675411d23SStefan Hajnoczi } 149775411d23SStefan Hajnoczi 149877a5a000SStefan Hajnoczi old_image_size = s->header.image_size; 149977a5a000SStefan Hajnoczi s->header.image_size = offset; 150077a5a000SStefan Hajnoczi ret = qed_write_header_sync(s); 150177a5a000SStefan Hajnoczi if (ret < 0) { 150277a5a000SStefan Hajnoczi s->header.image_size = old_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; 152195de6d70SPaolo Bonzini bdi->can_write_zeroes_with_unmap = true; 152275411d23SStefan Hajnoczi return 0; 152375411d23SStefan Hajnoczi } 152475411d23SStefan Hajnoczi 152575411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 152675411d23SStefan Hajnoczi const char *backing_file, 152775411d23SStefan Hajnoczi const char *backing_fmt) 152875411d23SStefan Hajnoczi { 152975411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 153075411d23SStefan Hajnoczi QEDHeader new_header, le_header; 153175411d23SStefan Hajnoczi void *buffer; 153275411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 153375411d23SStefan Hajnoczi int ret; 153475411d23SStefan Hajnoczi 153575411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 153675411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 153775411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 153875411d23SStefan Hajnoczi * add a new string. 153975411d23SStefan Hajnoczi */ 154075411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 154175411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 154275411d23SStefan Hajnoczi return -ENOTSUP; 154375411d23SStefan Hajnoczi } 154475411d23SStefan Hajnoczi 154575411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 154675411d23SStefan Hajnoczi 154775411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 154875411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 154975411d23SStefan Hajnoczi 155075411d23SStefan Hajnoczi /* Adjust feature flags */ 155175411d23SStefan Hajnoczi if (backing_file) { 155275411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 155375411d23SStefan Hajnoczi 155475411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 155575411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 155675411d23SStefan Hajnoczi } 155775411d23SStefan Hajnoczi } 155875411d23SStefan Hajnoczi 155975411d23SStefan Hajnoczi /* Calculate new header size */ 156075411d23SStefan Hajnoczi backing_file_len = 0; 156175411d23SStefan Hajnoczi 156275411d23SStefan Hajnoczi if (backing_file) { 156375411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 156475411d23SStefan Hajnoczi } 156575411d23SStefan Hajnoczi 156675411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 156775411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 156875411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 156975411d23SStefan Hajnoczi buffer_len += backing_file_len; 157075411d23SStefan Hajnoczi 157175411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 157275411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 157375411d23SStefan Hajnoczi return -ENOSPC; 157475411d23SStefan Hajnoczi } 157575411d23SStefan Hajnoczi 157675411d23SStefan Hajnoczi /* Prepare new header */ 15777267c094SAnthony Liguori buffer = g_malloc(buffer_len); 157875411d23SStefan Hajnoczi 157975411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 158075411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 158175411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 158275411d23SStefan Hajnoczi 1583feba23b1SPavel Borzenkov if (backing_file) { 158475411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 158575411d23SStefan Hajnoczi buffer_len += backing_file_len; 1586feba23b1SPavel Borzenkov } 158775411d23SStefan Hajnoczi 158875411d23SStefan Hajnoczi /* Write new header */ 15899a4f4c31SKevin Wolf ret = bdrv_pwrite_sync(bs->file->bs, 0, buffer, buffer_len); 15907267c094SAnthony Liguori g_free(buffer); 159175411d23SStefan Hajnoczi if (ret == 0) { 159275411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 159375411d23SStefan Hajnoczi } 159475411d23SStefan Hajnoczi return ret; 159575411d23SStefan Hajnoczi } 159675411d23SStefan Hajnoczi 15975a8a30dbSKevin Wolf static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp) 1598c82954e5SBenoît Canet { 1599c82954e5SBenoît Canet 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 16059a4f4c31SKevin Wolf bdrv_invalidate_cache(bs->file->bs, &local_err); 16065a8a30dbSKevin Wolf if (local_err) { 16075a8a30dbSKevin Wolf error_propagate(errp, local_err); 16085a8a30dbSKevin Wolf return; 16095a8a30dbSKevin Wolf } 16103456a8d1SKevin Wolf 1611c82954e5SBenoît Canet memset(s, 0, sizeof(BDRVQEDState)); 16125a8a30dbSKevin Wolf ret = bdrv_qed_open(bs, NULL, bs->open_flags, &local_err); 16135a8a30dbSKevin Wolf if (local_err) { 16145a8a30dbSKevin Wolf error_setg(errp, "Could not reopen qed layer: %s", 16155a8a30dbSKevin Wolf error_get_pretty(local_err)); 16165a8a30dbSKevin Wolf error_free(local_err); 16175a8a30dbSKevin Wolf return; 16185a8a30dbSKevin Wolf } else if (ret < 0) { 16195a8a30dbSKevin Wolf error_setg_errno(errp, -ret, "Could not reopen qed layer"); 16205a8a30dbSKevin Wolf return; 16215a8a30dbSKevin Wolf } 1622c82954e5SBenoît Canet } 1623c82954e5SBenoît Canet 16244534ff54SKevin Wolf static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result, 16254534ff54SKevin Wolf BdrvCheckMode fix) 162675411d23SStefan Hajnoczi { 162701979a98SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 162801979a98SStefan Hajnoczi 16294534ff54SKevin Wolf return qed_check(s, result, !!fix); 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, 1676c282e1fdSChunyan Liu .bdrv_create = bdrv_qed_create, 16773ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 1678b6b8a333SPaolo Bonzini .bdrv_co_get_block_status = bdrv_qed_co_get_block_status, 167975411d23SStefan Hajnoczi .bdrv_aio_readv = bdrv_qed_aio_readv, 168075411d23SStefan Hajnoczi .bdrv_aio_writev = bdrv_qed_aio_writev, 16810e71be19SStefan Hajnoczi .bdrv_co_write_zeroes = bdrv_qed_co_write_zeroes, 168275411d23SStefan Hajnoczi .bdrv_truncate = bdrv_qed_truncate, 168375411d23SStefan Hajnoczi .bdrv_getlength = bdrv_qed_getlength, 168475411d23SStefan Hajnoczi .bdrv_get_info = bdrv_qed_get_info, 1685d34682cdSKevin Wolf .bdrv_refresh_limits = bdrv_qed_refresh_limits, 168675411d23SStefan Hajnoczi .bdrv_change_backing_file = bdrv_qed_change_backing_file, 1687c82954e5SBenoît Canet .bdrv_invalidate_cache = bdrv_qed_invalidate_cache, 168875411d23SStefan Hajnoczi .bdrv_check = bdrv_qed_check, 1689a8c868c3SStefan Hajnoczi .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, 1690a8c868c3SStefan Hajnoczi .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, 1691*df9a681dSFam Zheng .bdrv_drain = bdrv_qed_drain, 169275411d23SStefan Hajnoczi }; 169375411d23SStefan Hajnoczi 169475411d23SStefan Hajnoczi static void bdrv_qed_init(void) 169575411d23SStefan Hajnoczi { 169675411d23SStefan Hajnoczi bdrv_register(&bdrv_qed); 169775411d23SStefan Hajnoczi } 169875411d23SStefan Hajnoczi 169975411d23SStefan Hajnoczi block_init(bdrv_qed_init); 1700