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" 161de7afc9SPaolo Bonzini #include "qemu/timer.h" 17eabba580SStefan Hajnoczi #include "trace.h" 1875411d23SStefan Hajnoczi #include "qed.h" 197b1b5d19SPaolo Bonzini #include "qapi/qmp/qerror.h" 20caf71f86SPaolo Bonzini #include "migration/migration.h" 2175411d23SStefan Hajnoczi 22d7331bedSStefan Hajnoczi static const AIOCBInfo qed_aiocb_info = { 23eabba580SStefan Hajnoczi .aiocb_size = sizeof(QEDAIOCB), 24eabba580SStefan Hajnoczi }; 25eabba580SStefan Hajnoczi 2675411d23SStefan Hajnoczi static int bdrv_qed_probe(const uint8_t *buf, int buf_size, 2775411d23SStefan Hajnoczi const char *filename) 2875411d23SStefan Hajnoczi { 2975411d23SStefan Hajnoczi const QEDHeader *header = (const QEDHeader *)buf; 3075411d23SStefan Hajnoczi 3175411d23SStefan Hajnoczi if (buf_size < sizeof(*header)) { 3275411d23SStefan Hajnoczi return 0; 3375411d23SStefan Hajnoczi } 3475411d23SStefan Hajnoczi if (le32_to_cpu(header->magic) != QED_MAGIC) { 3575411d23SStefan Hajnoczi return 0; 3675411d23SStefan Hajnoczi } 3775411d23SStefan Hajnoczi return 100; 3875411d23SStefan Hajnoczi } 3975411d23SStefan Hajnoczi 4075411d23SStefan Hajnoczi /** 4175411d23SStefan Hajnoczi * Check whether an image format is raw 4275411d23SStefan Hajnoczi * 4375411d23SStefan Hajnoczi * @fmt: Backing file format, may be NULL 4475411d23SStefan Hajnoczi */ 4575411d23SStefan Hajnoczi static bool qed_fmt_is_raw(const char *fmt) 4675411d23SStefan Hajnoczi { 4775411d23SStefan Hajnoczi return fmt && strcmp(fmt, "raw") == 0; 4875411d23SStefan Hajnoczi } 4975411d23SStefan Hajnoczi 5075411d23SStefan Hajnoczi static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) 5175411d23SStefan Hajnoczi { 5275411d23SStefan Hajnoczi cpu->magic = le32_to_cpu(le->magic); 5375411d23SStefan Hajnoczi cpu->cluster_size = le32_to_cpu(le->cluster_size); 5475411d23SStefan Hajnoczi cpu->table_size = le32_to_cpu(le->table_size); 5575411d23SStefan Hajnoczi cpu->header_size = le32_to_cpu(le->header_size); 5675411d23SStefan Hajnoczi cpu->features = le64_to_cpu(le->features); 5775411d23SStefan Hajnoczi cpu->compat_features = le64_to_cpu(le->compat_features); 5875411d23SStefan Hajnoczi cpu->autoclear_features = le64_to_cpu(le->autoclear_features); 5975411d23SStefan Hajnoczi cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); 6075411d23SStefan Hajnoczi cpu->image_size = le64_to_cpu(le->image_size); 6175411d23SStefan Hajnoczi cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); 6275411d23SStefan Hajnoczi cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); 6375411d23SStefan Hajnoczi } 6475411d23SStefan Hajnoczi 6575411d23SStefan Hajnoczi static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) 6675411d23SStefan Hajnoczi { 6775411d23SStefan Hajnoczi le->magic = cpu_to_le32(cpu->magic); 6875411d23SStefan Hajnoczi le->cluster_size = cpu_to_le32(cpu->cluster_size); 6975411d23SStefan Hajnoczi le->table_size = cpu_to_le32(cpu->table_size); 7075411d23SStefan Hajnoczi le->header_size = cpu_to_le32(cpu->header_size); 7175411d23SStefan Hajnoczi le->features = cpu_to_le64(cpu->features); 7275411d23SStefan Hajnoczi le->compat_features = cpu_to_le64(cpu->compat_features); 7375411d23SStefan Hajnoczi le->autoclear_features = cpu_to_le64(cpu->autoclear_features); 7475411d23SStefan Hajnoczi le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); 7575411d23SStefan Hajnoczi le->image_size = cpu_to_le64(cpu->image_size); 7675411d23SStefan Hajnoczi le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); 7775411d23SStefan Hajnoczi le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); 7875411d23SStefan Hajnoczi } 7975411d23SStefan Hajnoczi 80b10170acSStefan Hajnoczi int qed_write_header_sync(BDRVQEDState *s) 8175411d23SStefan Hajnoczi { 8275411d23SStefan Hajnoczi QEDHeader le; 8375411d23SStefan Hajnoczi int ret; 8475411d23SStefan Hajnoczi 8575411d23SStefan Hajnoczi qed_header_cpu_to_le(&s->header, &le); 869a4f4c31SKevin Wolf ret = bdrv_pwrite(s->bs->file->bs, 0, &le, sizeof(le)); 8775411d23SStefan Hajnoczi if (ret != sizeof(le)) { 8875411d23SStefan Hajnoczi return ret; 8975411d23SStefan Hajnoczi } 9075411d23SStefan Hajnoczi return 0; 9175411d23SStefan Hajnoczi } 9275411d23SStefan Hajnoczi 9301979a98SStefan Hajnoczi typedef struct { 9401979a98SStefan Hajnoczi GenericCB gencb; 9501979a98SStefan Hajnoczi BDRVQEDState *s; 9601979a98SStefan Hajnoczi struct iovec iov; 9701979a98SStefan Hajnoczi QEMUIOVector qiov; 9801979a98SStefan Hajnoczi int nsectors; 9901979a98SStefan Hajnoczi uint8_t *buf; 10001979a98SStefan Hajnoczi } QEDWriteHeaderCB; 10101979a98SStefan Hajnoczi 10201979a98SStefan Hajnoczi static void qed_write_header_cb(void *opaque, int ret) 10301979a98SStefan Hajnoczi { 10401979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = opaque; 10501979a98SStefan Hajnoczi 10601979a98SStefan Hajnoczi qemu_vfree(write_header_cb->buf); 10701979a98SStefan Hajnoczi gencb_complete(write_header_cb, ret); 10801979a98SStefan Hajnoczi } 10901979a98SStefan Hajnoczi 11001979a98SStefan Hajnoczi static void qed_write_header_read_cb(void *opaque, int ret) 11101979a98SStefan Hajnoczi { 11201979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = opaque; 11301979a98SStefan Hajnoczi BDRVQEDState *s = write_header_cb->s; 11401979a98SStefan Hajnoczi 11501979a98SStefan Hajnoczi if (ret) { 11601979a98SStefan Hajnoczi qed_write_header_cb(write_header_cb, ret); 11701979a98SStefan Hajnoczi return; 11801979a98SStefan Hajnoczi } 11901979a98SStefan Hajnoczi 12001979a98SStefan Hajnoczi /* Update header */ 12101979a98SStefan Hajnoczi qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf); 12201979a98SStefan Hajnoczi 1239a4f4c31SKevin Wolf bdrv_aio_writev(s->bs->file->bs, 0, &write_header_cb->qiov, 12401979a98SStefan Hajnoczi write_header_cb->nsectors, qed_write_header_cb, 12501979a98SStefan Hajnoczi write_header_cb); 12601979a98SStefan Hajnoczi } 12701979a98SStefan Hajnoczi 12801979a98SStefan Hajnoczi /** 12901979a98SStefan Hajnoczi * Update header in-place (does not rewrite backing filename or other strings) 13001979a98SStefan Hajnoczi * 13101979a98SStefan Hajnoczi * This function only updates known header fields in-place and does not affect 13201979a98SStefan Hajnoczi * extra data after the QED header. 13301979a98SStefan Hajnoczi */ 134097310b5SMarkus Armbruster static void qed_write_header(BDRVQEDState *s, BlockCompletionFunc cb, 13501979a98SStefan Hajnoczi void *opaque) 13601979a98SStefan Hajnoczi { 13701979a98SStefan Hajnoczi /* We must write full sectors for O_DIRECT but cannot necessarily generate 13801979a98SStefan Hajnoczi * the data following the header if an unrecognized compat feature is 13901979a98SStefan Hajnoczi * active. Therefore, first read the sectors containing the header, update 14001979a98SStefan Hajnoczi * them, and write back. 14101979a98SStefan Hajnoczi */ 14201979a98SStefan Hajnoczi 14301979a98SStefan Hajnoczi int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) / 14401979a98SStefan Hajnoczi BDRV_SECTOR_SIZE; 14501979a98SStefan Hajnoczi size_t len = nsectors * BDRV_SECTOR_SIZE; 14601979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb), 14701979a98SStefan Hajnoczi cb, opaque); 14801979a98SStefan Hajnoczi 14901979a98SStefan Hajnoczi write_header_cb->s = s; 15001979a98SStefan Hajnoczi write_header_cb->nsectors = nsectors; 15101979a98SStefan Hajnoczi write_header_cb->buf = qemu_blockalign(s->bs, len); 15201979a98SStefan Hajnoczi write_header_cb->iov.iov_base = write_header_cb->buf; 15301979a98SStefan Hajnoczi write_header_cb->iov.iov_len = len; 15401979a98SStefan Hajnoczi qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1); 15501979a98SStefan Hajnoczi 1569a4f4c31SKevin Wolf bdrv_aio_readv(s->bs->file->bs, 0, &write_header_cb->qiov, nsectors, 15701979a98SStefan Hajnoczi qed_write_header_read_cb, write_header_cb); 15801979a98SStefan Hajnoczi } 15901979a98SStefan Hajnoczi 16075411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 16175411d23SStefan Hajnoczi { 16275411d23SStefan Hajnoczi uint64_t table_entries; 16375411d23SStefan Hajnoczi uint64_t l2_size; 16475411d23SStefan Hajnoczi 16575411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t); 16675411d23SStefan Hajnoczi l2_size = table_entries * cluster_size; 16775411d23SStefan Hajnoczi 16875411d23SStefan Hajnoczi return l2_size * table_entries; 16975411d23SStefan Hajnoczi } 17075411d23SStefan Hajnoczi 17175411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size) 17275411d23SStefan Hajnoczi { 17375411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE || 17475411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) { 17575411d23SStefan Hajnoczi return false; 17675411d23SStefan Hajnoczi } 17775411d23SStefan Hajnoczi if (cluster_size & (cluster_size - 1)) { 17875411d23SStefan Hajnoczi return false; /* not power of 2 */ 17975411d23SStefan Hajnoczi } 18075411d23SStefan Hajnoczi return true; 18175411d23SStefan Hajnoczi } 18275411d23SStefan Hajnoczi 18375411d23SStefan Hajnoczi static bool qed_is_table_size_valid(uint32_t table_size) 18475411d23SStefan Hajnoczi { 18575411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE || 18675411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) { 18775411d23SStefan Hajnoczi return false; 18875411d23SStefan Hajnoczi } 18975411d23SStefan Hajnoczi if (table_size & (table_size - 1)) { 19075411d23SStefan Hajnoczi return false; /* not power of 2 */ 19175411d23SStefan Hajnoczi } 19275411d23SStefan Hajnoczi return true; 19375411d23SStefan Hajnoczi } 19475411d23SStefan Hajnoczi 19575411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 19675411d23SStefan Hajnoczi uint32_t table_size) 19775411d23SStefan Hajnoczi { 19875411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) { 19975411d23SStefan Hajnoczi return false; /* not multiple of sector size */ 20075411d23SStefan Hajnoczi } 20175411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) { 20275411d23SStefan Hajnoczi return false; /* image is too large */ 20375411d23SStefan Hajnoczi } 20475411d23SStefan Hajnoczi return true; 20575411d23SStefan Hajnoczi } 20675411d23SStefan Hajnoczi 20775411d23SStefan Hajnoczi /** 20875411d23SStefan Hajnoczi * Read a string of known length from the image file 20975411d23SStefan Hajnoczi * 21075411d23SStefan Hajnoczi * @file: Image file 21175411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes 21275411d23SStefan Hajnoczi * @n: String length in bytes 21375411d23SStefan Hajnoczi * @buf: Destination buffer 21475411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes 21575411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure 21675411d23SStefan Hajnoczi * 21775411d23SStefan Hajnoczi * The string is NUL-terminated. 21875411d23SStefan Hajnoczi */ 21975411d23SStefan Hajnoczi static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n, 22075411d23SStefan Hajnoczi char *buf, size_t buflen) 22175411d23SStefan Hajnoczi { 22275411d23SStefan Hajnoczi int ret; 22375411d23SStefan Hajnoczi if (n >= buflen) { 22475411d23SStefan Hajnoczi return -EINVAL; 22575411d23SStefan Hajnoczi } 22675411d23SStefan Hajnoczi ret = bdrv_pread(file, offset, buf, n); 22775411d23SStefan Hajnoczi if (ret < 0) { 22875411d23SStefan Hajnoczi return ret; 22975411d23SStefan Hajnoczi } 23075411d23SStefan Hajnoczi buf[n] = '\0'; 23175411d23SStefan Hajnoczi return 0; 23275411d23SStefan Hajnoczi } 23375411d23SStefan Hajnoczi 234eabba580SStefan Hajnoczi /** 235eabba580SStefan Hajnoczi * Allocate new clusters 236eabba580SStefan Hajnoczi * 237eabba580SStefan Hajnoczi * @s: QED state 238eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate 239eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster 240eabba580SStefan Hajnoczi * 241eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be 242eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image 243eabba580SStefan Hajnoczi * file. 244eabba580SStefan Hajnoczi */ 245eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 246eabba580SStefan Hajnoczi { 247eabba580SStefan Hajnoczi uint64_t offset = s->file_size; 248eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size; 249eabba580SStefan Hajnoczi return offset; 250eabba580SStefan Hajnoczi } 251eabba580SStefan Hajnoczi 252298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s) 253298800caSStefan Hajnoczi { 254298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */ 255298800caSStefan Hajnoczi return qemu_blockalign(s->bs, 256298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size); 257298800caSStefan Hajnoczi } 258298800caSStefan Hajnoczi 259eabba580SStefan Hajnoczi /** 260eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table 261eabba580SStefan Hajnoczi */ 262eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 263eabba580SStefan Hajnoczi { 264eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 265eabba580SStefan Hajnoczi 266eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s); 267eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 268eabba580SStefan Hajnoczi 269eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0, 270eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size); 271eabba580SStefan Hajnoczi return l2_table; 272eabba580SStefan Hajnoczi } 273eabba580SStefan Hajnoczi 274eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret); 275eabba580SStefan Hajnoczi 2766f321e93SStefan Hajnoczi static void qed_plug_allocating_write_reqs(BDRVQEDState *s) 2776f321e93SStefan Hajnoczi { 2786f321e93SStefan Hajnoczi assert(!s->allocating_write_reqs_plugged); 2796f321e93SStefan Hajnoczi 2806f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = true; 2816f321e93SStefan Hajnoczi } 2826f321e93SStefan Hajnoczi 2836f321e93SStefan Hajnoczi static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) 2846f321e93SStefan Hajnoczi { 2856f321e93SStefan Hajnoczi QEDAIOCB *acb; 2866f321e93SStefan Hajnoczi 2876f321e93SStefan Hajnoczi assert(s->allocating_write_reqs_plugged); 2886f321e93SStefan Hajnoczi 2896f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = false; 2906f321e93SStefan Hajnoczi 2916f321e93SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 2926f321e93SStefan Hajnoczi if (acb) { 2936f321e93SStefan Hajnoczi qed_aio_next_io(acb, 0); 2946f321e93SStefan Hajnoczi } 2956f321e93SStefan Hajnoczi } 2966f321e93SStefan Hajnoczi 2976f321e93SStefan Hajnoczi static void qed_finish_clear_need_check(void *opaque, int ret) 2986f321e93SStefan Hajnoczi { 2996f321e93SStefan Hajnoczi /* Do nothing */ 3006f321e93SStefan Hajnoczi } 3016f321e93SStefan Hajnoczi 3026f321e93SStefan Hajnoczi static void qed_flush_after_clear_need_check(void *opaque, int ret) 3036f321e93SStefan Hajnoczi { 3046f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3056f321e93SStefan Hajnoczi 3066f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s); 3076f321e93SStefan Hajnoczi 3086f321e93SStefan Hajnoczi /* No need to wait until flush completes */ 3096f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3106f321e93SStefan Hajnoczi } 3116f321e93SStefan Hajnoczi 3126f321e93SStefan Hajnoczi static void qed_clear_need_check(void *opaque, int ret) 3136f321e93SStefan Hajnoczi { 3146f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3156f321e93SStefan Hajnoczi 3166f321e93SStefan Hajnoczi if (ret) { 3176f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3186f321e93SStefan Hajnoczi return; 3196f321e93SStefan Hajnoczi } 3206f321e93SStefan Hajnoczi 3216f321e93SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 3226f321e93SStefan Hajnoczi qed_write_header(s, qed_flush_after_clear_need_check, s); 3236f321e93SStefan Hajnoczi } 3246f321e93SStefan Hajnoczi 3256f321e93SStefan Hajnoczi static void qed_need_check_timer_cb(void *opaque) 3266f321e93SStefan Hajnoczi { 3276f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3286f321e93SStefan Hajnoczi 3296f321e93SStefan Hajnoczi /* The timer should only fire when allocating writes have drained */ 3306f321e93SStefan Hajnoczi assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs)); 3316f321e93SStefan Hajnoczi 3326f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s); 3336f321e93SStefan Hajnoczi 3346f321e93SStefan Hajnoczi qed_plug_allocating_write_reqs(s); 3356f321e93SStefan Hajnoczi 3366f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */ 3376f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_clear_need_check, s); 3386f321e93SStefan Hajnoczi } 3396f321e93SStefan Hajnoczi 3406f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s) 3416f321e93SStefan Hajnoczi { 3426f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s); 3436f321e93SStefan Hajnoczi 344bc72ad67SAlex Bligh /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for 3456f321e93SStefan Hajnoczi * migration. 3466f321e93SStefan Hajnoczi */ 347bc72ad67SAlex Bligh timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 3486f321e93SStefan Hajnoczi get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT); 3496f321e93SStefan Hajnoczi } 3506f321e93SStefan Hajnoczi 3516f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */ 3526f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s) 3536f321e93SStefan Hajnoczi { 3546f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s); 355bc72ad67SAlex Bligh timer_del(s->need_check_timer); 3566f321e93SStefan Hajnoczi } 3576f321e93SStefan Hajnoczi 358a8c868c3SStefan Hajnoczi static void bdrv_qed_detach_aio_context(BlockDriverState *bs) 359a8c868c3SStefan Hajnoczi { 360a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 361a8c868c3SStefan Hajnoczi 362a8c868c3SStefan Hajnoczi qed_cancel_need_check_timer(s); 363a8c868c3SStefan Hajnoczi timer_free(s->need_check_timer); 364a8c868c3SStefan Hajnoczi } 365a8c868c3SStefan Hajnoczi 366a8c868c3SStefan Hajnoczi static void bdrv_qed_attach_aio_context(BlockDriverState *bs, 367a8c868c3SStefan Hajnoczi AioContext *new_context) 368a8c868c3SStefan Hajnoczi { 369a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 370a8c868c3SStefan Hajnoczi 371a8c868c3SStefan Hajnoczi s->need_check_timer = aio_timer_new(new_context, 372a8c868c3SStefan Hajnoczi QEMU_CLOCK_VIRTUAL, SCALE_NS, 373a8c868c3SStefan Hajnoczi qed_need_check_timer_cb, s); 374a8c868c3SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 375a8c868c3SStefan Hajnoczi qed_start_need_check_timer(s); 376a8c868c3SStefan Hajnoczi } 377a8c868c3SStefan Hajnoczi } 378a8c868c3SStefan Hajnoczi 379df9a681dSFam Zheng static void bdrv_qed_drain(BlockDriverState *bs) 380df9a681dSFam Zheng { 381df9a681dSFam Zheng BDRVQEDState *s = bs->opaque; 382df9a681dSFam Zheng 383df9a681dSFam Zheng /* Cancel timer and start doing I/O that were meant to happen as if it 384df9a681dSFam Zheng * fired, that way we get bdrv_drain() taking care of the ongoing requests 385df9a681dSFam Zheng * correctly. */ 386df9a681dSFam Zheng qed_cancel_need_check_timer(s); 387df9a681dSFam Zheng qed_plug_allocating_write_reqs(s); 388df9a681dSFam Zheng bdrv_aio_flush(s->bs, qed_clear_need_check, s); 389df9a681dSFam Zheng } 390df9a681dSFam Zheng 391015a1036SMax Reitz static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags, 392015a1036SMax Reitz Error **errp) 39375411d23SStefan Hajnoczi { 39475411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 39575411d23SStefan Hajnoczi QEDHeader le_header; 39675411d23SStefan Hajnoczi int64_t file_size; 39775411d23SStefan Hajnoczi int ret; 39875411d23SStefan Hajnoczi 39975411d23SStefan Hajnoczi s->bs = bs; 400eabba580SStefan Hajnoczi QSIMPLEQ_INIT(&s->allocating_write_reqs); 40175411d23SStefan Hajnoczi 4029a4f4c31SKevin Wolf ret = bdrv_pread(bs->file->bs, 0, &le_header, sizeof(le_header)); 40375411d23SStefan Hajnoczi if (ret < 0) { 40475411d23SStefan Hajnoczi return ret; 40575411d23SStefan Hajnoczi } 40675411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header); 40775411d23SStefan Hajnoczi 40875411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) { 40976abe407SPaolo Bonzini error_setg(errp, "Image not in QED format"); 41076abe407SPaolo Bonzini return -EINVAL; 41175411d23SStefan Hajnoczi } 41275411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) { 41310b758e8SKevin Wolf /* image uses unsupported feature bits */ 41410b758e8SKevin Wolf char buf[64]; 41510b758e8SKevin Wolf snprintf(buf, sizeof(buf), "%" PRIx64, 41610b758e8SKevin Wolf s->header.features & ~QED_FEATURE_MASK); 417c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, 41881e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs), "QED", buf); 41910b758e8SKevin Wolf return -ENOTSUP; 42075411d23SStefan Hajnoczi } 42175411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 42275411d23SStefan Hajnoczi return -EINVAL; 42375411d23SStefan Hajnoczi } 42475411d23SStefan Hajnoczi 42575411d23SStefan Hajnoczi /* Round down file size to the last cluster */ 4269a4f4c31SKevin Wolf file_size = bdrv_getlength(bs->file->bs); 42775411d23SStefan Hajnoczi if (file_size < 0) { 42875411d23SStefan Hajnoczi return file_size; 42975411d23SStefan Hajnoczi } 43075411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size); 43175411d23SStefan Hajnoczi 43275411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) { 43375411d23SStefan Hajnoczi return -EINVAL; 43475411d23SStefan Hajnoczi } 43575411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size, 43675411d23SStefan Hajnoczi s->header.cluster_size, 43775411d23SStefan Hajnoczi s->header.table_size)) { 43875411d23SStefan Hajnoczi return -EINVAL; 43975411d23SStefan Hajnoczi } 44075411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 44175411d23SStefan Hajnoczi return -EINVAL; 44275411d23SStefan Hajnoczi } 44375411d23SStefan Hajnoczi 44475411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) / 44575411d23SStefan Hajnoczi sizeof(uint64_t); 446786a4ea8SStefan Hajnoczi s->l2_shift = ctz32(s->header.cluster_size); 44775411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1; 448786a4ea8SStefan Hajnoczi s->l1_shift = s->l2_shift + ctz32(s->table_nelems); 44975411d23SStefan Hajnoczi 4500adfa1edSStefan Hajnoczi /* Header size calculation must not overflow uint32_t */ 4510adfa1edSStefan Hajnoczi if (s->header.header_size > UINT32_MAX / s->header.cluster_size) { 4520adfa1edSStefan Hajnoczi return -EINVAL; 4530adfa1edSStefan Hajnoczi } 4540adfa1edSStefan Hajnoczi 45575411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) { 45675411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset + 45775411d23SStefan Hajnoczi s->header.backing_filename_size > 45875411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) { 45975411d23SStefan Hajnoczi return -EINVAL; 46075411d23SStefan Hajnoczi } 46175411d23SStefan Hajnoczi 4629a4f4c31SKevin Wolf ret = qed_read_string(bs->file->bs, s->header.backing_filename_offset, 46375411d23SStefan Hajnoczi s->header.backing_filename_size, bs->backing_file, 46475411d23SStefan Hajnoczi sizeof(bs->backing_file)); 46575411d23SStefan Hajnoczi if (ret < 0) { 46675411d23SStefan Hajnoczi return ret; 46775411d23SStefan Hajnoczi } 46875411d23SStefan Hajnoczi 46975411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 47075411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 47175411d23SStefan Hajnoczi } 47275411d23SStefan Hajnoczi } 47375411d23SStefan Hajnoczi 47475411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards 47575411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older 47675411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is 47775411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear 47875411d23SStefan Hajnoczi * feature is no longer valid. 47975411d23SStefan Hajnoczi */ 48075411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 481*04c01a5cSKevin Wolf !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) { 48275411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 48375411d23SStefan Hajnoczi 48475411d23SStefan Hajnoczi ret = qed_write_header_sync(s); 48575411d23SStefan Hajnoczi if (ret) { 48675411d23SStefan Hajnoczi return ret; 48775411d23SStefan Hajnoczi } 48875411d23SStefan Hajnoczi 48975411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */ 4909a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 49175411d23SStefan Hajnoczi } 49275411d23SStefan Hajnoczi 493298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s); 494298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache); 495298800caSStefan Hajnoczi 496298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s); 497298800caSStefan Hajnoczi if (ret) { 49801979a98SStefan Hajnoczi goto out; 49901979a98SStefan Hajnoczi } 50001979a98SStefan Hajnoczi 50101979a98SStefan Hajnoczi /* If image was not closed cleanly, check consistency */ 502058f8f16SStefan Hajnoczi if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) { 50301979a98SStefan Hajnoczi /* Read-only images cannot be fixed. There is no risk of corruption 50401979a98SStefan Hajnoczi * since write operations are not possible. Therefore, allow 50501979a98SStefan Hajnoczi * potentially inconsistent images to be opened read-only. This can 50601979a98SStefan Hajnoczi * aid data recovery from an otherwise inconsistent image. 50701979a98SStefan Hajnoczi */ 5089a4f4c31SKevin Wolf if (!bdrv_is_read_only(bs->file->bs) && 509*04c01a5cSKevin Wolf !(flags & BDRV_O_INACTIVE)) { 51001979a98SStefan Hajnoczi BdrvCheckResult result = {0}; 51101979a98SStefan Hajnoczi 51201979a98SStefan Hajnoczi ret = qed_check(s, &result, true); 5136f321e93SStefan Hajnoczi if (ret) { 5146f321e93SStefan Hajnoczi goto out; 5156f321e93SStefan Hajnoczi } 51601979a98SStefan Hajnoczi } 51701979a98SStefan Hajnoczi } 51801979a98SStefan Hajnoczi 519a8c868c3SStefan Hajnoczi bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs)); 5206f321e93SStefan Hajnoczi 52101979a98SStefan Hajnoczi out: 52201979a98SStefan Hajnoczi if (ret) { 523298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 524298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 525298800caSStefan Hajnoczi } 52675411d23SStefan Hajnoczi return ret; 52775411d23SStefan Hajnoczi } 52875411d23SStefan Hajnoczi 5293baca891SKevin Wolf static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp) 530d34682cdSKevin Wolf { 531d34682cdSKevin Wolf BDRVQEDState *s = bs->opaque; 532d34682cdSKevin Wolf 533d34682cdSKevin Wolf bs->bl.write_zeroes_alignment = s->header.cluster_size >> BDRV_SECTOR_BITS; 534d34682cdSKevin Wolf } 535d34682cdSKevin Wolf 536f9cb20f1SJeff Cody /* We have nothing to do for QED reopen, stubs just return 537f9cb20f1SJeff Cody * success */ 538f9cb20f1SJeff Cody static int bdrv_qed_reopen_prepare(BDRVReopenState *state, 539f9cb20f1SJeff Cody BlockReopenQueue *queue, Error **errp) 540f9cb20f1SJeff Cody { 541f9cb20f1SJeff Cody return 0; 542f9cb20f1SJeff Cody } 543f9cb20f1SJeff Cody 54475411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 54575411d23SStefan Hajnoczi { 546298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 547298800caSStefan Hajnoczi 548a8c868c3SStefan Hajnoczi bdrv_qed_detach_aio_context(bs); 5496f321e93SStefan Hajnoczi 55001979a98SStefan Hajnoczi /* Ensure writes reach stable storage */ 5519a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 55201979a98SStefan Hajnoczi 55301979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */ 55401979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 55501979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 55601979a98SStefan Hajnoczi qed_write_header_sync(s); 55701979a98SStefan Hajnoczi } 55801979a98SStefan Hajnoczi 559298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 560298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 56175411d23SStefan Hajnoczi } 56275411d23SStefan Hajnoczi 56375411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size, 56475411d23SStefan Hajnoczi uint64_t image_size, uint32_t table_size, 5650fea6b79SPaolo Bonzini const char *backing_file, const char *backing_fmt, 5664ab15590SChunyan Liu QemuOpts *opts, Error **errp) 56775411d23SStefan Hajnoczi { 56875411d23SStefan Hajnoczi QEDHeader header = { 56975411d23SStefan Hajnoczi .magic = QED_MAGIC, 57075411d23SStefan Hajnoczi .cluster_size = cluster_size, 57175411d23SStefan Hajnoczi .table_size = table_size, 57275411d23SStefan Hajnoczi .header_size = 1, 57375411d23SStefan Hajnoczi .features = 0, 57475411d23SStefan Hajnoczi .compat_features = 0, 57575411d23SStefan Hajnoczi .l1_table_offset = cluster_size, 57675411d23SStefan Hajnoczi .image_size = image_size, 57775411d23SStefan Hajnoczi }; 57875411d23SStefan Hajnoczi QEDHeader le_header; 57975411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 58075411d23SStefan Hajnoczi size_t l1_size = header.cluster_size * header.table_size; 58134b5d2c6SMax Reitz Error *local_err = NULL; 58275411d23SStefan Hajnoczi int ret = 0; 5832e40134bSMax Reitz BlockDriverState *bs; 58475411d23SStefan Hajnoczi 5854ab15590SChunyan Liu ret = bdrv_create_file(filename, opts, &local_err); 58675411d23SStefan Hajnoczi if (ret < 0) { 5870fea6b79SPaolo Bonzini error_propagate(errp, local_err); 58875411d23SStefan Hajnoczi return ret; 58975411d23SStefan Hajnoczi } 59075411d23SStefan Hajnoczi 5912e40134bSMax Reitz bs = NULL; 5922e40134bSMax Reitz ret = bdrv_open(&bs, filename, NULL, NULL, 5936ebf9aa2SMax Reitz BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL, 5942e40134bSMax Reitz &local_err); 59575411d23SStefan Hajnoczi if (ret < 0) { 5960fea6b79SPaolo Bonzini error_propagate(errp, local_err); 59775411d23SStefan Hajnoczi return ret; 59875411d23SStefan Hajnoczi } 59975411d23SStefan Hajnoczi 600c743849bSStefan Hajnoczi /* File must start empty and grow, check truncate is supported */ 601c743849bSStefan Hajnoczi ret = bdrv_truncate(bs, 0); 602c743849bSStefan Hajnoczi if (ret < 0) { 603c743849bSStefan Hajnoczi goto out; 604c743849bSStefan Hajnoczi } 605c743849bSStefan Hajnoczi 60675411d23SStefan Hajnoczi if (backing_file) { 60775411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 60875411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 60975411d23SStefan Hajnoczi header.backing_filename_size = strlen(backing_file); 61075411d23SStefan Hajnoczi 61175411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 61275411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 61375411d23SStefan Hajnoczi } 61475411d23SStefan Hajnoczi } 61575411d23SStefan Hajnoczi 61675411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 61775411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header)); 61875411d23SStefan Hajnoczi if (ret < 0) { 61975411d23SStefan Hajnoczi goto out; 62075411d23SStefan Hajnoczi } 62175411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, sizeof(le_header), backing_file, 62275411d23SStefan Hajnoczi header.backing_filename_size); 62375411d23SStefan Hajnoczi if (ret < 0) { 62475411d23SStefan Hajnoczi goto out; 62575411d23SStefan Hajnoczi } 62675411d23SStefan Hajnoczi 6277267c094SAnthony Liguori l1_table = g_malloc0(l1_size); 62875411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size); 62975411d23SStefan Hajnoczi if (ret < 0) { 63075411d23SStefan Hajnoczi goto out; 63175411d23SStefan Hajnoczi } 63275411d23SStefan Hajnoczi 63375411d23SStefan Hajnoczi ret = 0; /* success */ 63475411d23SStefan Hajnoczi out: 6357267c094SAnthony Liguori g_free(l1_table); 6364f6fd349SFam Zheng bdrv_unref(bs); 63775411d23SStefan Hajnoczi return ret; 63875411d23SStefan Hajnoczi } 63975411d23SStefan Hajnoczi 6407ab74849SChunyan Liu static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp) 64175411d23SStefan Hajnoczi { 64275411d23SStefan Hajnoczi uint64_t image_size = 0; 64375411d23SStefan Hajnoczi uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 64475411d23SStefan Hajnoczi uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 6457ab74849SChunyan Liu char *backing_file = NULL; 6467ab74849SChunyan Liu char *backing_fmt = NULL; 6477ab74849SChunyan Liu int ret; 64875411d23SStefan Hajnoczi 649c2eb918eSHu Tao image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 650c2eb918eSHu Tao BDRV_SECTOR_SIZE); 6517ab74849SChunyan Liu backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); 6527ab74849SChunyan Liu backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); 6537ab74849SChunyan Liu cluster_size = qemu_opt_get_size_del(opts, 6547ab74849SChunyan Liu BLOCK_OPT_CLUSTER_SIZE, 6557ab74849SChunyan Liu QED_DEFAULT_CLUSTER_SIZE); 6567ab74849SChunyan Liu table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE, 6577ab74849SChunyan Liu QED_DEFAULT_TABLE_SIZE); 65875411d23SStefan Hajnoczi 65975411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(cluster_size)) { 6605ff679b4SAakriti Gupta error_setg(errp, "QED cluster size must be within range [%u, %u] " 6615ff679b4SAakriti Gupta "and power of 2", 66275411d23SStefan Hajnoczi QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 6637ab74849SChunyan Liu ret = -EINVAL; 6647ab74849SChunyan Liu goto finish; 66575411d23SStefan Hajnoczi } 66675411d23SStefan Hajnoczi if (!qed_is_table_size_valid(table_size)) { 6675ff679b4SAakriti Gupta error_setg(errp, "QED table size must be within range [%u, %u] " 6685ff679b4SAakriti Gupta "and power of 2", 66975411d23SStefan Hajnoczi QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 6707ab74849SChunyan Liu ret = -EINVAL; 6717ab74849SChunyan Liu goto finish; 67275411d23SStefan Hajnoczi } 67375411d23SStefan Hajnoczi if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 6745ff679b4SAakriti Gupta error_setg(errp, "QED image size must be a non-zero multiple of " 6755ff679b4SAakriti Gupta "cluster size and less than %" PRIu64 " bytes", 67675411d23SStefan Hajnoczi qed_max_image_size(cluster_size, table_size)); 6777ab74849SChunyan Liu ret = -EINVAL; 6787ab74849SChunyan Liu goto finish; 67975411d23SStefan Hajnoczi } 68075411d23SStefan Hajnoczi 6817ab74849SChunyan Liu ret = qed_create(filename, cluster_size, image_size, table_size, 6824ab15590SChunyan Liu backing_file, backing_fmt, opts, errp); 6837ab74849SChunyan Liu 6847ab74849SChunyan Liu finish: 6857ab74849SChunyan Liu g_free(backing_file); 6867ab74849SChunyan Liu g_free(backing_fmt); 6877ab74849SChunyan Liu return ret; 68875411d23SStefan Hajnoczi } 68975411d23SStefan Hajnoczi 690298800caSStefan Hajnoczi typedef struct { 6914bc74be9SPaolo Bonzini BlockDriverState *bs; 692b7d5a5b8SStefan Hajnoczi Coroutine *co; 6934bc74be9SPaolo Bonzini uint64_t pos; 6944bc74be9SPaolo Bonzini int64_t status; 695298800caSStefan Hajnoczi int *pnum; 696298800caSStefan Hajnoczi } QEDIsAllocatedCB; 697298800caSStefan Hajnoczi 698298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 699298800caSStefan Hajnoczi { 700298800caSStefan Hajnoczi QEDIsAllocatedCB *cb = opaque; 7014bc74be9SPaolo Bonzini BDRVQEDState *s = cb->bs->opaque; 702298800caSStefan Hajnoczi *cb->pnum = len / BDRV_SECTOR_SIZE; 7034bc74be9SPaolo Bonzini switch (ret) { 7044bc74be9SPaolo Bonzini case QED_CLUSTER_FOUND: 7054bc74be9SPaolo Bonzini offset |= qed_offset_into_cluster(s, cb->pos); 7064bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; 7074bc74be9SPaolo Bonzini break; 7084bc74be9SPaolo Bonzini case QED_CLUSTER_ZERO: 7094bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_ZERO; 7104bc74be9SPaolo Bonzini break; 7114bc74be9SPaolo Bonzini case QED_CLUSTER_L2: 7124bc74be9SPaolo Bonzini case QED_CLUSTER_L1: 7134bc74be9SPaolo Bonzini cb->status = 0; 7144bc74be9SPaolo Bonzini break; 7154bc74be9SPaolo Bonzini default: 7164bc74be9SPaolo Bonzini assert(ret < 0); 7174bc74be9SPaolo Bonzini cb->status = ret; 7184bc74be9SPaolo Bonzini break; 7194bc74be9SPaolo Bonzini } 7204bc74be9SPaolo Bonzini 721b7d5a5b8SStefan Hajnoczi if (cb->co) { 722b7d5a5b8SStefan Hajnoczi qemu_coroutine_enter(cb->co, NULL); 723b7d5a5b8SStefan Hajnoczi } 724298800caSStefan Hajnoczi } 725298800caSStefan Hajnoczi 726b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs, 727b7d5a5b8SStefan Hajnoczi int64_t sector_num, 72875411d23SStefan Hajnoczi int nb_sectors, int *pnum) 72975411d23SStefan Hajnoczi { 730298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 731298800caSStefan Hajnoczi size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 732298800caSStefan Hajnoczi QEDIsAllocatedCB cb = { 7334bc74be9SPaolo Bonzini .bs = bs, 7344bc74be9SPaolo Bonzini .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE, 7354bc74be9SPaolo Bonzini .status = BDRV_BLOCK_OFFSET_MASK, 736298800caSStefan Hajnoczi .pnum = pnum, 737298800caSStefan Hajnoczi }; 738298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 739298800caSStefan Hajnoczi 7404bc74be9SPaolo Bonzini qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb); 741298800caSStefan Hajnoczi 742b7d5a5b8SStefan Hajnoczi /* Now sleep if the callback wasn't invoked immediately */ 7434bc74be9SPaolo Bonzini while (cb.status == BDRV_BLOCK_OFFSET_MASK) { 744b7d5a5b8SStefan Hajnoczi cb.co = qemu_coroutine_self(); 745b7d5a5b8SStefan Hajnoczi qemu_coroutine_yield(); 746298800caSStefan Hajnoczi } 747298800caSStefan Hajnoczi 748298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 749298800caSStefan Hajnoczi 7504bc74be9SPaolo Bonzini return cb.status; 75175411d23SStefan Hajnoczi } 75275411d23SStefan Hajnoczi 753eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 754eabba580SStefan Hajnoczi { 755eabba580SStefan Hajnoczi return acb->common.bs->opaque; 756eabba580SStefan Hajnoczi } 757eabba580SStefan Hajnoczi 758eabba580SStefan Hajnoczi /** 759eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 760eabba580SStefan Hajnoczi * 761eabba580SStefan Hajnoczi * @s: QED state 762eabba580SStefan Hajnoczi * @pos: Byte position in device 763eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 764f06ee3d4SKevin Wolf * @backing_qiov: Possibly shortened copy of qiov, to be allocated here 765eabba580SStefan Hajnoczi * @cb: Completion function 766eabba580SStefan Hajnoczi * @opaque: User data for completion function 767eabba580SStefan Hajnoczi * 768eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 769eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 770eabba580SStefan Hajnoczi */ 771eabba580SStefan Hajnoczi static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 772eabba580SStefan Hajnoczi QEMUIOVector *qiov, 773f06ee3d4SKevin Wolf QEMUIOVector **backing_qiov, 774097310b5SMarkus Armbruster BlockCompletionFunc *cb, void *opaque) 775eabba580SStefan Hajnoczi { 776eabba580SStefan Hajnoczi uint64_t backing_length = 0; 777eabba580SStefan Hajnoczi size_t size; 778eabba580SStefan Hajnoczi 779eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 780eabba580SStefan Hajnoczi * backing file like a zero length backing file. 781eabba580SStefan Hajnoczi */ 782760e0063SKevin Wolf if (s->bs->backing) { 783760e0063SKevin Wolf int64_t l = bdrv_getlength(s->bs->backing->bs); 784eabba580SStefan Hajnoczi if (l < 0) { 785eabba580SStefan Hajnoczi cb(opaque, l); 786eabba580SStefan Hajnoczi return; 787eabba580SStefan Hajnoczi } 788eabba580SStefan Hajnoczi backing_length = l; 789eabba580SStefan Hajnoczi } 790eabba580SStefan Hajnoczi 791eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 792eabba580SStefan Hajnoczi if (pos >= backing_length || 793eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 7943d9b4925SMichael Tokarev qemu_iovec_memset(qiov, 0, 0, qiov->size); 795eabba580SStefan Hajnoczi } 796eabba580SStefan Hajnoczi 797eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 798eabba580SStefan Hajnoczi if (pos >= backing_length) { 799eabba580SStefan Hajnoczi cb(opaque, 0); 800eabba580SStefan Hajnoczi return; 801eabba580SStefan Hajnoczi } 802eabba580SStefan Hajnoczi 803eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 804eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 805eabba580SStefan Hajnoczi 806f06ee3d4SKevin Wolf assert(*backing_qiov == NULL); 807f06ee3d4SKevin Wolf *backing_qiov = g_new(QEMUIOVector, 1); 808f06ee3d4SKevin Wolf qemu_iovec_init(*backing_qiov, qiov->niov); 809f06ee3d4SKevin Wolf qemu_iovec_concat(*backing_qiov, qiov, 0, size); 810f06ee3d4SKevin Wolf 811820100fdSPaolo Bonzini BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); 812760e0063SKevin Wolf bdrv_aio_readv(s->bs->backing->bs, pos / BDRV_SECTOR_SIZE, 813f06ee3d4SKevin Wolf *backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque); 814eabba580SStefan Hajnoczi } 815eabba580SStefan Hajnoczi 816eabba580SStefan Hajnoczi typedef struct { 817eabba580SStefan Hajnoczi GenericCB gencb; 818eabba580SStefan Hajnoczi BDRVQEDState *s; 819eabba580SStefan Hajnoczi QEMUIOVector qiov; 820f06ee3d4SKevin Wolf QEMUIOVector *backing_qiov; 821eabba580SStefan Hajnoczi struct iovec iov; 822eabba580SStefan Hajnoczi uint64_t offset; 823eabba580SStefan Hajnoczi } CopyFromBackingFileCB; 824eabba580SStefan Hajnoczi 825eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_cb(void *opaque, int ret) 826eabba580SStefan Hajnoczi { 827eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 828eabba580SStefan Hajnoczi qemu_vfree(copy_cb->iov.iov_base); 829eabba580SStefan Hajnoczi gencb_complete(©_cb->gencb, ret); 830eabba580SStefan Hajnoczi } 831eabba580SStefan Hajnoczi 832eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_write(void *opaque, int ret) 833eabba580SStefan Hajnoczi { 834eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 835eabba580SStefan Hajnoczi BDRVQEDState *s = copy_cb->s; 836eabba580SStefan Hajnoczi 837f06ee3d4SKevin Wolf if (copy_cb->backing_qiov) { 838f06ee3d4SKevin Wolf qemu_iovec_destroy(copy_cb->backing_qiov); 839f06ee3d4SKevin Wolf g_free(copy_cb->backing_qiov); 840f06ee3d4SKevin Wolf copy_cb->backing_qiov = NULL; 841f06ee3d4SKevin Wolf } 842f06ee3d4SKevin Wolf 843eabba580SStefan Hajnoczi if (ret) { 844eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb(copy_cb, ret); 845eabba580SStefan Hajnoczi return; 846eabba580SStefan Hajnoczi } 847eabba580SStefan Hajnoczi 848eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 8499a4f4c31SKevin Wolf bdrv_aio_writev(s->bs->file->bs, copy_cb->offset / BDRV_SECTOR_SIZE, 850ad54ae80SPaolo Bonzini ©_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE, 851eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb, copy_cb); 852eabba580SStefan Hajnoczi } 853eabba580SStefan Hajnoczi 854eabba580SStefan Hajnoczi /** 855eabba580SStefan Hajnoczi * Copy data from backing file into the image 856eabba580SStefan Hajnoczi * 857eabba580SStefan Hajnoczi * @s: QED state 858eabba580SStefan Hajnoczi * @pos: Byte position in device 859eabba580SStefan Hajnoczi * @len: Number of bytes 860eabba580SStefan Hajnoczi * @offset: Byte offset in image file 861eabba580SStefan Hajnoczi * @cb: Completion function 862eabba580SStefan Hajnoczi * @opaque: User data for completion function 863eabba580SStefan Hajnoczi */ 864eabba580SStefan Hajnoczi static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, 865eabba580SStefan Hajnoczi uint64_t len, uint64_t offset, 866097310b5SMarkus Armbruster BlockCompletionFunc *cb, 867eabba580SStefan Hajnoczi void *opaque) 868eabba580SStefan Hajnoczi { 869eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb; 870eabba580SStefan Hajnoczi 871eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 872eabba580SStefan Hajnoczi if (len == 0) { 873eabba580SStefan Hajnoczi cb(opaque, 0); 874eabba580SStefan Hajnoczi return; 875eabba580SStefan Hajnoczi } 876eabba580SStefan Hajnoczi 877eabba580SStefan Hajnoczi copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque); 878eabba580SStefan Hajnoczi copy_cb->s = s; 879eabba580SStefan Hajnoczi copy_cb->offset = offset; 880f06ee3d4SKevin Wolf copy_cb->backing_qiov = NULL; 881eabba580SStefan Hajnoczi copy_cb->iov.iov_base = qemu_blockalign(s->bs, len); 882eabba580SStefan Hajnoczi copy_cb->iov.iov_len = len; 883eabba580SStefan Hajnoczi qemu_iovec_init_external(©_cb->qiov, ©_cb->iov, 1); 884eabba580SStefan Hajnoczi 885f06ee3d4SKevin Wolf qed_read_backing_file(s, pos, ©_cb->qiov, ©_cb->backing_qiov, 886eabba580SStefan Hajnoczi qed_copy_from_backing_file_write, copy_cb); 887eabba580SStefan Hajnoczi } 888eabba580SStefan Hajnoczi 889eabba580SStefan Hajnoczi /** 890eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 891eabba580SStefan Hajnoczi * 892eabba580SStefan Hajnoczi * @s: QED state 893eabba580SStefan Hajnoczi * @table: L2 table 894eabba580SStefan Hajnoczi * @index: First cluster index 895eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 89621df65b6SAnthony Liguori * @cluster: First cluster offset 89721df65b6SAnthony Liguori * 89821df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the 89921df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker. 900eabba580SStefan Hajnoczi */ 901eabba580SStefan Hajnoczi static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index, 902eabba580SStefan Hajnoczi unsigned int n, uint64_t cluster) 903eabba580SStefan Hajnoczi { 904eabba580SStefan Hajnoczi int i; 905eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 906eabba580SStefan Hajnoczi table->offsets[i] = cluster; 90721df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) && 90821df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) { 909eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 910eabba580SStefan Hajnoczi } 911eabba580SStefan Hajnoczi } 91221df65b6SAnthony Liguori } 913eabba580SStefan Hajnoczi 914eabba580SStefan Hajnoczi static void qed_aio_complete_bh(void *opaque) 915eabba580SStefan Hajnoczi { 916eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 917097310b5SMarkus Armbruster BlockCompletionFunc *cb = acb->common.cb; 918eabba580SStefan Hajnoczi void *user_opaque = acb->common.opaque; 919eabba580SStefan Hajnoczi int ret = acb->bh_ret; 920eabba580SStefan Hajnoczi 921eabba580SStefan Hajnoczi qemu_bh_delete(acb->bh); 9228007429aSFam Zheng qemu_aio_unref(acb); 923eabba580SStefan Hajnoczi 924eabba580SStefan Hajnoczi /* Invoke callback */ 925eabba580SStefan Hajnoczi cb(user_opaque, ret); 926eabba580SStefan Hajnoczi } 927eabba580SStefan Hajnoczi 928eabba580SStefan Hajnoczi static void qed_aio_complete(QEDAIOCB *acb, int ret) 929eabba580SStefan Hajnoczi { 930eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 931eabba580SStefan Hajnoczi 932eabba580SStefan Hajnoczi trace_qed_aio_complete(s, acb, ret); 933eabba580SStefan Hajnoczi 934eabba580SStefan Hajnoczi /* Free resources */ 935eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov); 936eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 937eabba580SStefan Hajnoczi 9380e71be19SStefan Hajnoczi /* Free the buffer we may have allocated for zero writes */ 9390e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 9400e71be19SStefan Hajnoczi qemu_vfree(acb->qiov->iov[0].iov_base); 9410e71be19SStefan Hajnoczi acb->qiov->iov[0].iov_base = NULL; 9420e71be19SStefan Hajnoczi } 9430e71be19SStefan Hajnoczi 944eabba580SStefan Hajnoczi /* Arrange for a bh to invoke the completion function */ 945eabba580SStefan Hajnoczi acb->bh_ret = ret; 946a8c868c3SStefan Hajnoczi acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs), 947a8c868c3SStefan Hajnoczi qed_aio_complete_bh, acb); 948eabba580SStefan Hajnoczi qemu_bh_schedule(acb->bh); 949eabba580SStefan Hajnoczi 950eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 951eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 952eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 953eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 954eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 955eabba580SStefan Hajnoczi */ 956eabba580SStefan Hajnoczi if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 957eabba580SStefan Hajnoczi QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next); 958eabba580SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 959eabba580SStefan Hajnoczi if (acb) { 960eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 9616f321e93SStefan Hajnoczi } else if (s->header.features & QED_F_NEED_CHECK) { 9626f321e93SStefan Hajnoczi qed_start_need_check_timer(s); 963eabba580SStefan Hajnoczi } 964eabba580SStefan Hajnoczi } 965eabba580SStefan Hajnoczi } 966eabba580SStefan Hajnoczi 967eabba580SStefan Hajnoczi /** 968eabba580SStefan Hajnoczi * Commit the current L2 table to the cache 969eabba580SStefan Hajnoczi */ 970eabba580SStefan Hajnoczi static void qed_commit_l2_update(void *opaque, int ret) 971eabba580SStefan Hajnoczi { 972eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 973eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 974eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 975e4fc8781SStefan Hajnoczi uint64_t l2_offset = l2_table->offset; 976eabba580SStefan Hajnoczi 977eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 978eabba580SStefan Hajnoczi 979eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 980eabba580SStefan Hajnoczi * cache. 981eabba580SStefan Hajnoczi */ 982e4fc8781SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 983eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 984eabba580SStefan Hajnoczi 985eabba580SStefan Hajnoczi qed_aio_next_io(opaque, ret); 986eabba580SStefan Hajnoczi } 987eabba580SStefan Hajnoczi 988eabba580SStefan Hajnoczi /** 989eabba580SStefan Hajnoczi * Update L1 table with new L2 table offset and write it out 990eabba580SStefan Hajnoczi */ 991eabba580SStefan Hajnoczi static void qed_aio_write_l1_update(void *opaque, int ret) 992eabba580SStefan Hajnoczi { 993eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 994eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 995eabba580SStefan Hajnoczi int index; 996eabba580SStefan Hajnoczi 997eabba580SStefan Hajnoczi if (ret) { 998eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 999eabba580SStefan Hajnoczi return; 1000eabba580SStefan Hajnoczi } 1001eabba580SStefan Hajnoczi 1002eabba580SStefan Hajnoczi index = qed_l1_index(s, acb->cur_pos); 1003eabba580SStefan Hajnoczi s->l1_table->offsets[index] = acb->request.l2_table->offset; 1004eabba580SStefan Hajnoczi 1005eabba580SStefan Hajnoczi qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb); 1006eabba580SStefan Hajnoczi } 1007eabba580SStefan Hajnoczi 1008eabba580SStefan Hajnoczi /** 1009eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 1010eabba580SStefan Hajnoczi */ 10110e71be19SStefan Hajnoczi static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset) 1012eabba580SStefan Hajnoczi { 1013eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1014eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 1015eabba580SStefan Hajnoczi int index; 1016eabba580SStefan Hajnoczi 1017eabba580SStefan Hajnoczi if (ret) { 1018eabba580SStefan Hajnoczi goto err; 1019eabba580SStefan Hajnoczi } 1020eabba580SStefan Hajnoczi 1021eabba580SStefan Hajnoczi if (need_alloc) { 1022eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 1023eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 1024eabba580SStefan Hajnoczi } 1025eabba580SStefan Hajnoczi 1026eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 1027eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 10280e71be19SStefan Hajnoczi offset); 1029eabba580SStefan Hajnoczi 1030eabba580SStefan Hajnoczi if (need_alloc) { 1031eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 1032eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true, 1033eabba580SStefan Hajnoczi qed_aio_write_l1_update, acb); 1034eabba580SStefan Hajnoczi } else { 1035eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 1036eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false, 1037eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1038eabba580SStefan Hajnoczi } 1039eabba580SStefan Hajnoczi return; 1040eabba580SStefan Hajnoczi 1041eabba580SStefan Hajnoczi err: 1042eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1043eabba580SStefan Hajnoczi } 1044eabba580SStefan Hajnoczi 10450e71be19SStefan Hajnoczi static void qed_aio_write_l2_update_cb(void *opaque, int ret) 10460e71be19SStefan Hajnoczi { 10470e71be19SStefan Hajnoczi QEDAIOCB *acb = opaque; 10480e71be19SStefan Hajnoczi qed_aio_write_l2_update(acb, ret, acb->cur_cluster); 10490e71be19SStefan Hajnoczi } 10500e71be19SStefan Hajnoczi 1051eabba580SStefan Hajnoczi /** 1052eabba580SStefan Hajnoczi * Flush new data clusters before updating the L2 table 1053eabba580SStefan Hajnoczi * 1054eabba580SStefan Hajnoczi * This flush is necessary when a backing file is in use. A crash during an 1055eabba580SStefan Hajnoczi * allocating write could result in empty clusters in the image. If the write 1056eabba580SStefan Hajnoczi * only touched a subregion of the cluster, then backing image sectors have 1057eabba580SStefan Hajnoczi * been lost in the untouched region. The solution is to flush after writing a 1058eabba580SStefan Hajnoczi * new data cluster and before updating the L2 table. 1059eabba580SStefan Hajnoczi */ 1060eabba580SStefan Hajnoczi static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) 1061eabba580SStefan Hajnoczi { 1062eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1063eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1064eabba580SStefan Hajnoczi 10659a4f4c31SKevin Wolf if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) { 1066eabba580SStefan Hajnoczi qed_aio_complete(acb, -EIO); 1067eabba580SStefan Hajnoczi } 1068eabba580SStefan Hajnoczi } 1069eabba580SStefan Hajnoczi 1070eabba580SStefan Hajnoczi /** 1071eabba580SStefan Hajnoczi * Write data to the image file 1072eabba580SStefan Hajnoczi */ 1073eabba580SStefan Hajnoczi static void qed_aio_write_main(void *opaque, int ret) 1074eabba580SStefan Hajnoczi { 1075eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1076eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1077eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1078eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 1079097310b5SMarkus Armbruster BlockCompletionFunc *next_fn; 1080eabba580SStefan Hajnoczi 1081eabba580SStefan Hajnoczi trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size); 1082eabba580SStefan Hajnoczi 1083eabba580SStefan Hajnoczi if (ret) { 1084eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1085eabba580SStefan Hajnoczi return; 1086eabba580SStefan Hajnoczi } 1087eabba580SStefan Hajnoczi 1088eabba580SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_FOUND) { 1089eabba580SStefan Hajnoczi next_fn = qed_aio_next_io; 1090eabba580SStefan Hajnoczi } else { 1091760e0063SKevin Wolf if (s->bs->backing) { 1092eabba580SStefan Hajnoczi next_fn = qed_aio_write_flush_before_l2_update; 1093eabba580SStefan Hajnoczi } else { 10940e71be19SStefan Hajnoczi next_fn = qed_aio_write_l2_update_cb; 1095eabba580SStefan Hajnoczi } 1096eabba580SStefan Hajnoczi } 1097eabba580SStefan Hajnoczi 1098eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 10999a4f4c31SKevin Wolf bdrv_aio_writev(s->bs->file->bs, offset / BDRV_SECTOR_SIZE, 1100ad54ae80SPaolo Bonzini &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1101eabba580SStefan Hajnoczi next_fn, acb); 1102eabba580SStefan Hajnoczi } 1103eabba580SStefan Hajnoczi 1104eabba580SStefan Hajnoczi /** 1105eabba580SStefan Hajnoczi * Populate back untouched region of new data cluster 1106eabba580SStefan Hajnoczi */ 1107eabba580SStefan Hajnoczi static void qed_aio_write_postfill(void *opaque, int ret) 1108eabba580SStefan Hajnoczi { 1109eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1110eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1111eabba580SStefan Hajnoczi uint64_t start = acb->cur_pos + acb->cur_qiov.size; 1112eabba580SStefan Hajnoczi uint64_t len = 1113eabba580SStefan Hajnoczi qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1114eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1115eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + 1116eabba580SStefan Hajnoczi acb->cur_qiov.size; 1117eabba580SStefan Hajnoczi 1118eabba580SStefan Hajnoczi if (ret) { 1119eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1120eabba580SStefan Hajnoczi return; 1121eabba580SStefan Hajnoczi } 1122eabba580SStefan Hajnoczi 1123eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 1124eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, offset, 1125eabba580SStefan Hajnoczi qed_aio_write_main, acb); 1126eabba580SStefan Hajnoczi } 1127eabba580SStefan Hajnoczi 1128eabba580SStefan Hajnoczi /** 1129eabba580SStefan Hajnoczi * Populate front untouched region of new data cluster 1130eabba580SStefan Hajnoczi */ 1131eabba580SStefan Hajnoczi static void qed_aio_write_prefill(void *opaque, int ret) 1132eabba580SStefan Hajnoczi { 1133eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1134eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1135eabba580SStefan Hajnoczi uint64_t start = qed_start_of_cluster(s, acb->cur_pos); 1136eabba580SStefan Hajnoczi uint64_t len = qed_offset_into_cluster(s, acb->cur_pos); 1137eabba580SStefan Hajnoczi 1138eabba580SStefan Hajnoczi trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1139eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, acb->cur_cluster, 1140eabba580SStefan Hajnoczi qed_aio_write_postfill, acb); 1141eabba580SStefan Hajnoczi } 1142eabba580SStefan Hajnoczi 1143eabba580SStefan Hajnoczi /** 11440d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write 11450d09c797SStefan Hajnoczi */ 11460d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s) 11470d09c797SStefan Hajnoczi { 11480d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */ 1149760e0063SKevin Wolf if (s->bs->backing) { 11500d09c797SStefan Hajnoczi return false; 11510d09c797SStefan Hajnoczi } 11520d09c797SStefan Hajnoczi 11530d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK); 11540d09c797SStefan Hajnoczi } 11550d09c797SStefan Hajnoczi 11560e71be19SStefan Hajnoczi static void qed_aio_write_zero_cluster(void *opaque, int ret) 11570e71be19SStefan Hajnoczi { 11580e71be19SStefan Hajnoczi QEDAIOCB *acb = opaque; 11590e71be19SStefan Hajnoczi 11600e71be19SStefan Hajnoczi if (ret) { 11610e71be19SStefan Hajnoczi qed_aio_complete(acb, ret); 11620e71be19SStefan Hajnoczi return; 11630e71be19SStefan Hajnoczi } 11640e71be19SStefan Hajnoczi 11650e71be19SStefan Hajnoczi qed_aio_write_l2_update(acb, 0, 1); 11660e71be19SStefan Hajnoczi } 11670e71be19SStefan Hajnoczi 11680d09c797SStefan Hajnoczi /** 1169eabba580SStefan Hajnoczi * Write new data cluster 1170eabba580SStefan Hajnoczi * 1171eabba580SStefan Hajnoczi * @acb: Write request 1172eabba580SStefan Hajnoczi * @len: Length in bytes 1173eabba580SStefan Hajnoczi * 1174eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters. 1175eabba580SStefan Hajnoczi */ 1176eabba580SStefan Hajnoczi static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1177eabba580SStefan Hajnoczi { 1178eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1179097310b5SMarkus Armbruster BlockCompletionFunc *cb; 1180eabba580SStefan Hajnoczi 11816f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */ 11826f321e93SStefan Hajnoczi if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) { 11836f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 11846f321e93SStefan Hajnoczi } 11856f321e93SStefan Hajnoczi 1186eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 1187eabba580SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 1188eabba580SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next); 1189eabba580SStefan Hajnoczi } 11906f321e93SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) || 11916f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged) { 1192eabba580SStefan Hajnoczi return; /* wait for existing request to finish */ 1193eabba580SStefan Hajnoczi } 1194eabba580SStefan Hajnoczi 1195eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 1196eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 11971b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1198eabba580SStefan Hajnoczi 11990e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 12000e71be19SStefan Hajnoczi /* Skip ahead if the clusters are already zero */ 12010e71be19SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { 12020e71be19SStefan Hajnoczi qed_aio_next_io(acb, 0); 12030e71be19SStefan Hajnoczi return; 12040e71be19SStefan Hajnoczi } 12050e71be19SStefan Hajnoczi 12060e71be19SStefan Hajnoczi cb = qed_aio_write_zero_cluster; 12070e71be19SStefan Hajnoczi } else { 12080e71be19SStefan Hajnoczi cb = qed_aio_write_prefill; 12090e71be19SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 12100e71be19SStefan Hajnoczi } 12110e71be19SStefan Hajnoczi 12120d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) { 121301979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK; 12140e71be19SStefan Hajnoczi qed_write_header(s, cb, acb); 12150d09c797SStefan Hajnoczi } else { 12160e71be19SStefan Hajnoczi cb(acb, 0); 12170d09c797SStefan Hajnoczi } 1218eabba580SStefan Hajnoczi } 1219eabba580SStefan Hajnoczi 1220eabba580SStefan Hajnoczi /** 1221eabba580SStefan Hajnoczi * Write data cluster in place 1222eabba580SStefan Hajnoczi * 1223eabba580SStefan Hajnoczi * @acb: Write request 1224eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1225eabba580SStefan Hajnoczi * @len: Length in bytes 1226eabba580SStefan Hajnoczi * 1227eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 1228eabba580SStefan Hajnoczi */ 1229eabba580SStefan Hajnoczi static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) 1230eabba580SStefan Hajnoczi { 12310e71be19SStefan Hajnoczi /* Allocate buffer for zero writes */ 12320e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 12330e71be19SStefan Hajnoczi struct iovec *iov = acb->qiov->iov; 12340e71be19SStefan Hajnoczi 12350e71be19SStefan Hajnoczi if (!iov->iov_base) { 12364f4896dbSKevin Wolf iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len); 12374f4896dbSKevin Wolf if (iov->iov_base == NULL) { 12384f4896dbSKevin Wolf qed_aio_complete(acb, -ENOMEM); 12394f4896dbSKevin Wolf return; 12404f4896dbSKevin Wolf } 12410e71be19SStefan Hajnoczi memset(iov->iov_base, 0, iov->iov_len); 12420e71be19SStefan Hajnoczi } 12430e71be19SStefan Hajnoczi } 12440e71be19SStefan Hajnoczi 1245eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 1246eabba580SStefan Hajnoczi acb->cur_cluster = offset; 12471b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1248eabba580SStefan Hajnoczi 1249eabba580SStefan Hajnoczi /* Do the actual write */ 1250eabba580SStefan Hajnoczi qed_aio_write_main(acb, 0); 1251eabba580SStefan Hajnoczi } 1252eabba580SStefan Hajnoczi 1253eabba580SStefan Hajnoczi /** 1254eabba580SStefan Hajnoczi * Write data cluster 1255eabba580SStefan Hajnoczi * 1256eabba580SStefan Hajnoczi * @opaque: Write request 1257eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1258eabba580SStefan Hajnoczi * or -errno 1259eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1260eabba580SStefan Hajnoczi * @len: Length in bytes 1261eabba580SStefan Hajnoczi * 1262eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1263eabba580SStefan Hajnoczi */ 1264eabba580SStefan Hajnoczi static void qed_aio_write_data(void *opaque, int ret, 1265eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1266eabba580SStefan Hajnoczi { 1267eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1268eabba580SStefan Hajnoczi 1269eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1270eabba580SStefan Hajnoczi 1271eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 1272eabba580SStefan Hajnoczi 1273eabba580SStefan Hajnoczi switch (ret) { 1274eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 1275eabba580SStefan Hajnoczi qed_aio_write_inplace(acb, offset, len); 1276eabba580SStefan Hajnoczi break; 1277eabba580SStefan Hajnoczi 1278eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 1279eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 128021df65b6SAnthony Liguori case QED_CLUSTER_ZERO: 1281eabba580SStefan Hajnoczi qed_aio_write_alloc(acb, len); 1282eabba580SStefan Hajnoczi break; 1283eabba580SStefan Hajnoczi 1284eabba580SStefan Hajnoczi default: 1285eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1286eabba580SStefan Hajnoczi break; 1287eabba580SStefan Hajnoczi } 1288eabba580SStefan Hajnoczi } 1289eabba580SStefan Hajnoczi 1290eabba580SStefan Hajnoczi /** 1291eabba580SStefan Hajnoczi * Read data cluster 1292eabba580SStefan Hajnoczi * 1293eabba580SStefan Hajnoczi * @opaque: Read request 1294eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1295eabba580SStefan Hajnoczi * or -errno 1296eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1297eabba580SStefan Hajnoczi * @len: Length in bytes 1298eabba580SStefan Hajnoczi * 1299eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1300eabba580SStefan Hajnoczi */ 1301eabba580SStefan Hajnoczi static void qed_aio_read_data(void *opaque, int ret, 1302eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1303eabba580SStefan Hajnoczi { 1304eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1305eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1306eabba580SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 1307eabba580SStefan Hajnoczi 1308eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 1309eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 1310eabba580SStefan Hajnoczi 1311eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 1312eabba580SStefan Hajnoczi 1313eabba580SStefan Hajnoczi if (ret < 0) { 1314eabba580SStefan Hajnoczi goto err; 1315eabba580SStefan Hajnoczi } 1316eabba580SStefan Hajnoczi 13171b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1318eabba580SStefan Hajnoczi 131921df65b6SAnthony Liguori /* Handle zero cluster and backing file reads */ 132021df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) { 13213d9b4925SMichael Tokarev qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); 132221df65b6SAnthony Liguori qed_aio_next_io(acb, 0); 132321df65b6SAnthony Liguori return; 132421df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) { 1325eabba580SStefan Hajnoczi qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1326f06ee3d4SKevin Wolf &acb->backing_qiov, qed_aio_next_io, acb); 1327eabba580SStefan Hajnoczi return; 1328eabba580SStefan Hajnoczi } 1329eabba580SStefan Hajnoczi 1330eabba580SStefan Hajnoczi BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 13319a4f4c31SKevin Wolf bdrv_aio_readv(bs->file->bs, offset / BDRV_SECTOR_SIZE, 1332ad54ae80SPaolo Bonzini &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1333eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1334eabba580SStefan Hajnoczi return; 1335eabba580SStefan Hajnoczi 1336eabba580SStefan Hajnoczi err: 1337eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1338eabba580SStefan Hajnoczi } 1339eabba580SStefan Hajnoczi 1340eabba580SStefan Hajnoczi /** 1341eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1342eabba580SStefan Hajnoczi */ 1343eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret) 1344eabba580SStefan Hajnoczi { 1345eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1346eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 13476e4f59bdSStefan Hajnoczi QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ? 13486e4f59bdSStefan Hajnoczi qed_aio_write_data : qed_aio_read_data; 1349eabba580SStefan Hajnoczi 1350eabba580SStefan Hajnoczi trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size); 1351eabba580SStefan Hajnoczi 1352f06ee3d4SKevin Wolf if (acb->backing_qiov) { 1353f06ee3d4SKevin Wolf qemu_iovec_destroy(acb->backing_qiov); 1354f06ee3d4SKevin Wolf g_free(acb->backing_qiov); 1355f06ee3d4SKevin Wolf acb->backing_qiov = NULL; 1356f06ee3d4SKevin Wolf } 1357f06ee3d4SKevin Wolf 1358eabba580SStefan Hajnoczi /* Handle I/O error */ 1359eabba580SStefan Hajnoczi if (ret) { 1360eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1361eabba580SStefan Hajnoczi return; 1362eabba580SStefan Hajnoczi } 1363eabba580SStefan Hajnoczi 1364eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1365eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1366eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1367eabba580SStefan Hajnoczi 1368eabba580SStefan Hajnoczi /* Complete request */ 1369eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 1370eabba580SStefan Hajnoczi qed_aio_complete(acb, 0); 1371eabba580SStefan Hajnoczi return; 1372eabba580SStefan Hajnoczi } 1373eabba580SStefan Hajnoczi 1374eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 1375eabba580SStefan Hajnoczi qed_find_cluster(s, &acb->request, 1376eabba580SStefan Hajnoczi acb->cur_pos, acb->end_pos - acb->cur_pos, 1377eabba580SStefan Hajnoczi io_fn, acb); 1378eabba580SStefan Hajnoczi } 1379eabba580SStefan Hajnoczi 13807c84b1b8SMarkus Armbruster static BlockAIOCB *qed_aio_setup(BlockDriverState *bs, 1381eabba580SStefan Hajnoczi int64_t sector_num, 1382eabba580SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1383097310b5SMarkus Armbruster BlockCompletionFunc *cb, 13846e4f59bdSStefan Hajnoczi void *opaque, int flags) 1385eabba580SStefan Hajnoczi { 1386d7331bedSStefan Hajnoczi QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque); 1387eabba580SStefan Hajnoczi 1388eabba580SStefan Hajnoczi trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, 13896e4f59bdSStefan Hajnoczi opaque, flags); 1390eabba580SStefan Hajnoczi 13916e4f59bdSStefan Hajnoczi acb->flags = flags; 1392eabba580SStefan Hajnoczi acb->qiov = qiov; 1393eabba580SStefan Hajnoczi acb->qiov_offset = 0; 1394eabba580SStefan Hajnoczi acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 1395eabba580SStefan Hajnoczi acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE; 1396f06ee3d4SKevin Wolf acb->backing_qiov = NULL; 1397eabba580SStefan Hajnoczi acb->request.l2_table = NULL; 1398eabba580SStefan Hajnoczi qemu_iovec_init(&acb->cur_qiov, qiov->niov); 1399eabba580SStefan Hajnoczi 1400eabba580SStefan Hajnoczi /* Start request */ 1401eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 1402eabba580SStefan Hajnoczi return &acb->common; 1403eabba580SStefan Hajnoczi } 1404eabba580SStefan Hajnoczi 14057c84b1b8SMarkus Armbruster static BlockAIOCB *bdrv_qed_aio_readv(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, opaque, 0); 141275411d23SStefan Hajnoczi } 141375411d23SStefan Hajnoczi 14147c84b1b8SMarkus Armbruster static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs, 141575411d23SStefan Hajnoczi int64_t sector_num, 141675411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1417097310b5SMarkus Armbruster BlockCompletionFunc *cb, 141875411d23SStefan Hajnoczi void *opaque) 141975411d23SStefan Hajnoczi { 14206e4f59bdSStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, 14216e4f59bdSStefan Hajnoczi opaque, QED_AIOCB_WRITE); 142275411d23SStefan Hajnoczi } 142375411d23SStefan Hajnoczi 14240e71be19SStefan Hajnoczi typedef struct { 14250e71be19SStefan Hajnoczi Coroutine *co; 14260e71be19SStefan Hajnoczi int ret; 14270e71be19SStefan Hajnoczi bool done; 14280e71be19SStefan Hajnoczi } QEDWriteZeroesCB; 14290e71be19SStefan Hajnoczi 14300e71be19SStefan Hajnoczi static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret) 14310e71be19SStefan Hajnoczi { 14320e71be19SStefan Hajnoczi QEDWriteZeroesCB *cb = opaque; 14330e71be19SStefan Hajnoczi 14340e71be19SStefan Hajnoczi cb->done = true; 14350e71be19SStefan Hajnoczi cb->ret = ret; 14360e71be19SStefan Hajnoczi if (cb->co) { 14370e71be19SStefan Hajnoczi qemu_coroutine_enter(cb->co, NULL); 14380e71be19SStefan Hajnoczi } 14390e71be19SStefan Hajnoczi } 14400e71be19SStefan Hajnoczi 14410e71be19SStefan Hajnoczi static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs, 14420e71be19SStefan Hajnoczi int64_t sector_num, 1443aa7bfbffSPeter Lieven int nb_sectors, 1444aa7bfbffSPeter Lieven BdrvRequestFlags flags) 14450e71be19SStefan Hajnoczi { 14467c84b1b8SMarkus Armbruster BlockAIOCB *blockacb; 1447ef72f76eSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 14480e71be19SStefan Hajnoczi QEDWriteZeroesCB cb = { .done = false }; 14490e71be19SStefan Hajnoczi QEMUIOVector qiov; 14500e71be19SStefan Hajnoczi struct iovec iov; 14510e71be19SStefan Hajnoczi 1452ef72f76eSStefan Hajnoczi /* Refuse if there are untouched backing file sectors */ 1453760e0063SKevin Wolf if (bs->backing) { 1454ef72f76eSStefan Hajnoczi if (qed_offset_into_cluster(s, sector_num * BDRV_SECTOR_SIZE) != 0) { 1455ef72f76eSStefan Hajnoczi return -ENOTSUP; 1456ef72f76eSStefan Hajnoczi } 1457ef72f76eSStefan Hajnoczi if (qed_offset_into_cluster(s, nb_sectors * BDRV_SECTOR_SIZE) != 0) { 1458ef72f76eSStefan Hajnoczi return -ENOTSUP; 1459ef72f76eSStefan Hajnoczi } 1460ef72f76eSStefan Hajnoczi } 1461ef72f76eSStefan Hajnoczi 14620e71be19SStefan Hajnoczi /* Zero writes start without an I/O buffer. If a buffer becomes necessary 14630e71be19SStefan Hajnoczi * then it will be allocated during request processing. 14640e71be19SStefan Hajnoczi */ 14650e71be19SStefan Hajnoczi iov.iov_base = NULL, 14660e71be19SStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE, 14670e71be19SStefan Hajnoczi 14680e71be19SStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 14690e71be19SStefan Hajnoczi blockacb = qed_aio_setup(bs, sector_num, &qiov, nb_sectors, 14700e71be19SStefan Hajnoczi qed_co_write_zeroes_cb, &cb, 14710e71be19SStefan Hajnoczi QED_AIOCB_WRITE | QED_AIOCB_ZERO); 14720e71be19SStefan Hajnoczi if (!blockacb) { 14730e71be19SStefan Hajnoczi return -EIO; 14740e71be19SStefan Hajnoczi } 14750e71be19SStefan Hajnoczi if (!cb.done) { 14760e71be19SStefan Hajnoczi cb.co = qemu_coroutine_self(); 14770e71be19SStefan Hajnoczi qemu_coroutine_yield(); 14780e71be19SStefan Hajnoczi } 14790e71be19SStefan Hajnoczi assert(cb.done); 14800e71be19SStefan Hajnoczi return cb.ret; 14810e71be19SStefan Hajnoczi } 14820e71be19SStefan Hajnoczi 148375411d23SStefan Hajnoczi static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset) 148475411d23SStefan Hajnoczi { 148577a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 148677a5a000SStefan Hajnoczi uint64_t old_image_size; 148777a5a000SStefan Hajnoczi int ret; 148877a5a000SStefan Hajnoczi 148977a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size, 149077a5a000SStefan Hajnoczi s->header.table_size)) { 149177a5a000SStefan Hajnoczi return -EINVAL; 149277a5a000SStefan Hajnoczi } 149377a5a000SStefan Hajnoczi 149477a5a000SStefan Hajnoczi /* Shrinking is currently not supported */ 149577a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) { 149675411d23SStefan Hajnoczi return -ENOTSUP; 149775411d23SStefan Hajnoczi } 149875411d23SStefan Hajnoczi 149977a5a000SStefan Hajnoczi old_image_size = s->header.image_size; 150077a5a000SStefan Hajnoczi s->header.image_size = offset; 150177a5a000SStefan Hajnoczi ret = qed_write_header_sync(s); 150277a5a000SStefan Hajnoczi if (ret < 0) { 150377a5a000SStefan Hajnoczi s->header.image_size = old_image_size; 150477a5a000SStefan Hajnoczi } 150577a5a000SStefan Hajnoczi return ret; 150677a5a000SStefan Hajnoczi } 150777a5a000SStefan Hajnoczi 150875411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs) 150975411d23SStefan Hajnoczi { 151075411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 151175411d23SStefan Hajnoczi return s->header.image_size; 151275411d23SStefan Hajnoczi } 151375411d23SStefan Hajnoczi 151475411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 151575411d23SStefan Hajnoczi { 151675411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 151775411d23SStefan Hajnoczi 151875411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi)); 151975411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size; 1520d68dbee8SDong Xu Wang bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; 152195de6d70SPaolo Bonzini bdi->unallocated_blocks_are_zero = true; 152295de6d70SPaolo Bonzini bdi->can_write_zeroes_with_unmap = true; 152375411d23SStefan Hajnoczi return 0; 152475411d23SStefan Hajnoczi } 152575411d23SStefan Hajnoczi 152675411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 152775411d23SStefan Hajnoczi const char *backing_file, 152875411d23SStefan Hajnoczi const char *backing_fmt) 152975411d23SStefan Hajnoczi { 153075411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 153175411d23SStefan Hajnoczi QEDHeader new_header, le_header; 153275411d23SStefan Hajnoczi void *buffer; 153375411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 153475411d23SStefan Hajnoczi int ret; 153575411d23SStefan Hajnoczi 153675411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 153775411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 153875411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 153975411d23SStefan Hajnoczi * add a new string. 154075411d23SStefan Hajnoczi */ 154175411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 154275411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 154375411d23SStefan Hajnoczi return -ENOTSUP; 154475411d23SStefan Hajnoczi } 154575411d23SStefan Hajnoczi 154675411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 154775411d23SStefan Hajnoczi 154875411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 154975411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 155075411d23SStefan Hajnoczi 155175411d23SStefan Hajnoczi /* Adjust feature flags */ 155275411d23SStefan Hajnoczi if (backing_file) { 155375411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 155475411d23SStefan Hajnoczi 155575411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 155675411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 155775411d23SStefan Hajnoczi } 155875411d23SStefan Hajnoczi } 155975411d23SStefan Hajnoczi 156075411d23SStefan Hajnoczi /* Calculate new header size */ 156175411d23SStefan Hajnoczi backing_file_len = 0; 156275411d23SStefan Hajnoczi 156375411d23SStefan Hajnoczi if (backing_file) { 156475411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 156575411d23SStefan Hajnoczi } 156675411d23SStefan Hajnoczi 156775411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 156875411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 156975411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 157075411d23SStefan Hajnoczi buffer_len += backing_file_len; 157175411d23SStefan Hajnoczi 157275411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 157375411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 157475411d23SStefan Hajnoczi return -ENOSPC; 157575411d23SStefan Hajnoczi } 157675411d23SStefan Hajnoczi 157775411d23SStefan Hajnoczi /* Prepare new header */ 15787267c094SAnthony Liguori buffer = g_malloc(buffer_len); 157975411d23SStefan Hajnoczi 158075411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 158175411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 158275411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 158375411d23SStefan Hajnoczi 1584feba23b1SPavel Borzenkov if (backing_file) { 158575411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 158675411d23SStefan Hajnoczi buffer_len += backing_file_len; 1587feba23b1SPavel Borzenkov } 158875411d23SStefan Hajnoczi 158975411d23SStefan Hajnoczi /* Write new header */ 15909a4f4c31SKevin Wolf ret = bdrv_pwrite_sync(bs->file->bs, 0, buffer, buffer_len); 15917267c094SAnthony Liguori g_free(buffer); 159275411d23SStefan Hajnoczi if (ret == 0) { 159375411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 159475411d23SStefan Hajnoczi } 159575411d23SStefan Hajnoczi return ret; 159675411d23SStefan Hajnoczi } 159775411d23SStefan Hajnoczi 15985a8a30dbSKevin Wolf static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp) 1599c82954e5SBenoît Canet { 1600c82954e5SBenoît Canet BDRVQEDState *s = bs->opaque; 16015a8a30dbSKevin Wolf Error *local_err = NULL; 16025a8a30dbSKevin Wolf int ret; 1603c82954e5SBenoît Canet 1604c82954e5SBenoît Canet bdrv_qed_close(bs); 16053456a8d1SKevin Wolf 16069a4f4c31SKevin Wolf bdrv_invalidate_cache(bs->file->bs, &local_err); 16075a8a30dbSKevin Wolf if (local_err) { 16085a8a30dbSKevin Wolf error_propagate(errp, local_err); 16095a8a30dbSKevin Wolf return; 16105a8a30dbSKevin Wolf } 16113456a8d1SKevin Wolf 1612c82954e5SBenoît Canet memset(s, 0, sizeof(BDRVQEDState)); 16135a8a30dbSKevin Wolf ret = bdrv_qed_open(bs, NULL, bs->open_flags, &local_err); 16145a8a30dbSKevin Wolf if (local_err) { 1615e43bfd9cSMarkus Armbruster error_propagate(errp, local_err); 1616e43bfd9cSMarkus Armbruster error_prepend(errp, "Could not reopen qed layer: "); 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, 1691df9a681dSFam 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