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 156f321e93SStefan Hajnoczi #include "qemu-timer.h" 16eabba580SStefan Hajnoczi #include "trace.h" 1775411d23SStefan Hajnoczi #include "qed.h" 1810b758e8SKevin Wolf #include "qerror.h" 191ed520c6SAnthony Liguori #include "migration.h" 2075411d23SStefan Hajnoczi 21eabba580SStefan Hajnoczi static void qed_aio_cancel(BlockDriverAIOCB *blockacb) 22eabba580SStefan Hajnoczi { 23eabba580SStefan Hajnoczi QEDAIOCB *acb = (QEDAIOCB *)blockacb; 24eabba580SStefan Hajnoczi bool finished = false; 25eabba580SStefan Hajnoczi 26eabba580SStefan Hajnoczi /* Wait for the request to finish */ 27eabba580SStefan Hajnoczi acb->finished = &finished; 28eabba580SStefan Hajnoczi while (!finished) { 29eabba580SStefan Hajnoczi qemu_aio_wait(); 30eabba580SStefan Hajnoczi } 31eabba580SStefan Hajnoczi } 32eabba580SStefan Hajnoczi 33eabba580SStefan Hajnoczi static AIOPool qed_aio_pool = { 34eabba580SStefan Hajnoczi .aiocb_size = sizeof(QEDAIOCB), 35eabba580SStefan Hajnoczi .cancel = qed_aio_cancel, 36eabba580SStefan Hajnoczi }; 37eabba580SStefan Hajnoczi 3875411d23SStefan Hajnoczi static int bdrv_qed_probe(const uint8_t *buf, int buf_size, 3975411d23SStefan Hajnoczi const char *filename) 4075411d23SStefan Hajnoczi { 4175411d23SStefan Hajnoczi const QEDHeader *header = (const QEDHeader *)buf; 4275411d23SStefan Hajnoczi 4375411d23SStefan Hajnoczi if (buf_size < sizeof(*header)) { 4475411d23SStefan Hajnoczi return 0; 4575411d23SStefan Hajnoczi } 4675411d23SStefan Hajnoczi if (le32_to_cpu(header->magic) != QED_MAGIC) { 4775411d23SStefan Hajnoczi return 0; 4875411d23SStefan Hajnoczi } 4975411d23SStefan Hajnoczi return 100; 5075411d23SStefan Hajnoczi } 5175411d23SStefan Hajnoczi 5275411d23SStefan Hajnoczi /** 5375411d23SStefan Hajnoczi * Check whether an image format is raw 5475411d23SStefan Hajnoczi * 5575411d23SStefan Hajnoczi * @fmt: Backing file format, may be NULL 5675411d23SStefan Hajnoczi */ 5775411d23SStefan Hajnoczi static bool qed_fmt_is_raw(const char *fmt) 5875411d23SStefan Hajnoczi { 5975411d23SStefan Hajnoczi return fmt && strcmp(fmt, "raw") == 0; 6075411d23SStefan Hajnoczi } 6175411d23SStefan Hajnoczi 6275411d23SStefan Hajnoczi static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) 6375411d23SStefan Hajnoczi { 6475411d23SStefan Hajnoczi cpu->magic = le32_to_cpu(le->magic); 6575411d23SStefan Hajnoczi cpu->cluster_size = le32_to_cpu(le->cluster_size); 6675411d23SStefan Hajnoczi cpu->table_size = le32_to_cpu(le->table_size); 6775411d23SStefan Hajnoczi cpu->header_size = le32_to_cpu(le->header_size); 6875411d23SStefan Hajnoczi cpu->features = le64_to_cpu(le->features); 6975411d23SStefan Hajnoczi cpu->compat_features = le64_to_cpu(le->compat_features); 7075411d23SStefan Hajnoczi cpu->autoclear_features = le64_to_cpu(le->autoclear_features); 7175411d23SStefan Hajnoczi cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); 7275411d23SStefan Hajnoczi cpu->image_size = le64_to_cpu(le->image_size); 7375411d23SStefan Hajnoczi cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); 7475411d23SStefan Hajnoczi cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); 7575411d23SStefan Hajnoczi } 7675411d23SStefan Hajnoczi 7775411d23SStefan Hajnoczi static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) 7875411d23SStefan Hajnoczi { 7975411d23SStefan Hajnoczi le->magic = cpu_to_le32(cpu->magic); 8075411d23SStefan Hajnoczi le->cluster_size = cpu_to_le32(cpu->cluster_size); 8175411d23SStefan Hajnoczi le->table_size = cpu_to_le32(cpu->table_size); 8275411d23SStefan Hajnoczi le->header_size = cpu_to_le32(cpu->header_size); 8375411d23SStefan Hajnoczi le->features = cpu_to_le64(cpu->features); 8475411d23SStefan Hajnoczi le->compat_features = cpu_to_le64(cpu->compat_features); 8575411d23SStefan Hajnoczi le->autoclear_features = cpu_to_le64(cpu->autoclear_features); 8675411d23SStefan Hajnoczi le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); 8775411d23SStefan Hajnoczi le->image_size = cpu_to_le64(cpu->image_size); 8875411d23SStefan Hajnoczi le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); 8975411d23SStefan Hajnoczi le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); 9075411d23SStefan Hajnoczi } 9175411d23SStefan Hajnoczi 9275411d23SStefan Hajnoczi static int qed_write_header_sync(BDRVQEDState *s) 9375411d23SStefan Hajnoczi { 9475411d23SStefan Hajnoczi QEDHeader le; 9575411d23SStefan Hajnoczi int ret; 9675411d23SStefan Hajnoczi 9775411d23SStefan Hajnoczi qed_header_cpu_to_le(&s->header, &le); 9875411d23SStefan Hajnoczi ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le)); 9975411d23SStefan Hajnoczi if (ret != sizeof(le)) { 10075411d23SStefan Hajnoczi return ret; 10175411d23SStefan Hajnoczi } 10275411d23SStefan Hajnoczi return 0; 10375411d23SStefan Hajnoczi } 10475411d23SStefan Hajnoczi 10501979a98SStefan Hajnoczi typedef struct { 10601979a98SStefan Hajnoczi GenericCB gencb; 10701979a98SStefan Hajnoczi BDRVQEDState *s; 10801979a98SStefan Hajnoczi struct iovec iov; 10901979a98SStefan Hajnoczi QEMUIOVector qiov; 11001979a98SStefan Hajnoczi int nsectors; 11101979a98SStefan Hajnoczi uint8_t *buf; 11201979a98SStefan Hajnoczi } QEDWriteHeaderCB; 11301979a98SStefan Hajnoczi 11401979a98SStefan Hajnoczi static void qed_write_header_cb(void *opaque, int ret) 11501979a98SStefan Hajnoczi { 11601979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = opaque; 11701979a98SStefan Hajnoczi 11801979a98SStefan Hajnoczi qemu_vfree(write_header_cb->buf); 11901979a98SStefan Hajnoczi gencb_complete(write_header_cb, ret); 12001979a98SStefan Hajnoczi } 12101979a98SStefan Hajnoczi 12201979a98SStefan Hajnoczi static void qed_write_header_read_cb(void *opaque, int ret) 12301979a98SStefan Hajnoczi { 12401979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = opaque; 12501979a98SStefan Hajnoczi BDRVQEDState *s = write_header_cb->s; 12601979a98SStefan Hajnoczi 12701979a98SStefan Hajnoczi if (ret) { 12801979a98SStefan Hajnoczi qed_write_header_cb(write_header_cb, ret); 12901979a98SStefan Hajnoczi return; 13001979a98SStefan Hajnoczi } 13101979a98SStefan Hajnoczi 13201979a98SStefan Hajnoczi /* Update header */ 13301979a98SStefan Hajnoczi qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf); 13401979a98SStefan Hajnoczi 135ad54ae80SPaolo Bonzini bdrv_aio_writev(s->bs->file, 0, &write_header_cb->qiov, 13601979a98SStefan Hajnoczi write_header_cb->nsectors, qed_write_header_cb, 13701979a98SStefan Hajnoczi write_header_cb); 13801979a98SStefan Hajnoczi } 13901979a98SStefan Hajnoczi 14001979a98SStefan Hajnoczi /** 14101979a98SStefan Hajnoczi * Update header in-place (does not rewrite backing filename or other strings) 14201979a98SStefan Hajnoczi * 14301979a98SStefan Hajnoczi * This function only updates known header fields in-place and does not affect 14401979a98SStefan Hajnoczi * extra data after the QED header. 14501979a98SStefan Hajnoczi */ 14601979a98SStefan Hajnoczi static void qed_write_header(BDRVQEDState *s, BlockDriverCompletionFunc cb, 14701979a98SStefan Hajnoczi void *opaque) 14801979a98SStefan Hajnoczi { 14901979a98SStefan Hajnoczi /* We must write full sectors for O_DIRECT but cannot necessarily generate 15001979a98SStefan Hajnoczi * the data following the header if an unrecognized compat feature is 15101979a98SStefan Hajnoczi * active. Therefore, first read the sectors containing the header, update 15201979a98SStefan Hajnoczi * them, and write back. 15301979a98SStefan Hajnoczi */ 15401979a98SStefan Hajnoczi 15501979a98SStefan Hajnoczi int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) / 15601979a98SStefan Hajnoczi BDRV_SECTOR_SIZE; 15701979a98SStefan Hajnoczi size_t len = nsectors * BDRV_SECTOR_SIZE; 15801979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb), 15901979a98SStefan Hajnoczi cb, opaque); 16001979a98SStefan Hajnoczi 16101979a98SStefan Hajnoczi write_header_cb->s = s; 16201979a98SStefan Hajnoczi write_header_cb->nsectors = nsectors; 16301979a98SStefan Hajnoczi write_header_cb->buf = qemu_blockalign(s->bs, len); 16401979a98SStefan Hajnoczi write_header_cb->iov.iov_base = write_header_cb->buf; 16501979a98SStefan Hajnoczi write_header_cb->iov.iov_len = len; 16601979a98SStefan Hajnoczi qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1); 16701979a98SStefan Hajnoczi 168ad54ae80SPaolo Bonzini bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors, 16901979a98SStefan Hajnoczi qed_write_header_read_cb, write_header_cb); 17001979a98SStefan Hajnoczi } 17101979a98SStefan Hajnoczi 17275411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 17375411d23SStefan Hajnoczi { 17475411d23SStefan Hajnoczi uint64_t table_entries; 17575411d23SStefan Hajnoczi uint64_t l2_size; 17675411d23SStefan Hajnoczi 17775411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t); 17875411d23SStefan Hajnoczi l2_size = table_entries * cluster_size; 17975411d23SStefan Hajnoczi 18075411d23SStefan Hajnoczi return l2_size * table_entries; 18175411d23SStefan Hajnoczi } 18275411d23SStefan Hajnoczi 18375411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size) 18475411d23SStefan Hajnoczi { 18575411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE || 18675411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) { 18775411d23SStefan Hajnoczi return false; 18875411d23SStefan Hajnoczi } 18975411d23SStefan Hajnoczi if (cluster_size & (cluster_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_table_size_valid(uint32_t table_size) 19675411d23SStefan Hajnoczi { 19775411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE || 19875411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) { 19975411d23SStefan Hajnoczi return false; 20075411d23SStefan Hajnoczi } 20175411d23SStefan Hajnoczi if (table_size & (table_size - 1)) { 20275411d23SStefan Hajnoczi return false; /* not power of 2 */ 20375411d23SStefan Hajnoczi } 20475411d23SStefan Hajnoczi return true; 20575411d23SStefan Hajnoczi } 20675411d23SStefan Hajnoczi 20775411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 20875411d23SStefan Hajnoczi uint32_t table_size) 20975411d23SStefan Hajnoczi { 21075411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) { 21175411d23SStefan Hajnoczi return false; /* not multiple of sector size */ 21275411d23SStefan Hajnoczi } 21375411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) { 21475411d23SStefan Hajnoczi return false; /* image is too large */ 21575411d23SStefan Hajnoczi } 21675411d23SStefan Hajnoczi return true; 21775411d23SStefan Hajnoczi } 21875411d23SStefan Hajnoczi 21975411d23SStefan Hajnoczi /** 22075411d23SStefan Hajnoczi * Read a string of known length from the image file 22175411d23SStefan Hajnoczi * 22275411d23SStefan Hajnoczi * @file: Image file 22375411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes 22475411d23SStefan Hajnoczi * @n: String length in bytes 22575411d23SStefan Hajnoczi * @buf: Destination buffer 22675411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes 22775411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure 22875411d23SStefan Hajnoczi * 22975411d23SStefan Hajnoczi * The string is NUL-terminated. 23075411d23SStefan Hajnoczi */ 23175411d23SStefan Hajnoczi static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n, 23275411d23SStefan Hajnoczi char *buf, size_t buflen) 23375411d23SStefan Hajnoczi { 23475411d23SStefan Hajnoczi int ret; 23575411d23SStefan Hajnoczi if (n >= buflen) { 23675411d23SStefan Hajnoczi return -EINVAL; 23775411d23SStefan Hajnoczi } 23875411d23SStefan Hajnoczi ret = bdrv_pread(file, offset, buf, n); 23975411d23SStefan Hajnoczi if (ret < 0) { 24075411d23SStefan Hajnoczi return ret; 24175411d23SStefan Hajnoczi } 24275411d23SStefan Hajnoczi buf[n] = '\0'; 24375411d23SStefan Hajnoczi return 0; 24475411d23SStefan Hajnoczi } 24575411d23SStefan Hajnoczi 246eabba580SStefan Hajnoczi /** 247eabba580SStefan Hajnoczi * Allocate new clusters 248eabba580SStefan Hajnoczi * 249eabba580SStefan Hajnoczi * @s: QED state 250eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate 251eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster 252eabba580SStefan Hajnoczi * 253eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be 254eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image 255eabba580SStefan Hajnoczi * file. 256eabba580SStefan Hajnoczi */ 257eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 258eabba580SStefan Hajnoczi { 259eabba580SStefan Hajnoczi uint64_t offset = s->file_size; 260eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size; 261eabba580SStefan Hajnoczi return offset; 262eabba580SStefan Hajnoczi } 263eabba580SStefan Hajnoczi 264298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s) 265298800caSStefan Hajnoczi { 266298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */ 267298800caSStefan Hajnoczi return qemu_blockalign(s->bs, 268298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size); 269298800caSStefan Hajnoczi } 270298800caSStefan Hajnoczi 271eabba580SStefan Hajnoczi /** 272eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table 273eabba580SStefan Hajnoczi */ 274eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 275eabba580SStefan Hajnoczi { 276eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 277eabba580SStefan Hajnoczi 278eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s); 279eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 280eabba580SStefan Hajnoczi 281eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0, 282eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size); 283eabba580SStefan Hajnoczi return l2_table; 284eabba580SStefan Hajnoczi } 285eabba580SStefan Hajnoczi 286eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret); 287eabba580SStefan Hajnoczi 2886f321e93SStefan Hajnoczi static void qed_plug_allocating_write_reqs(BDRVQEDState *s) 2896f321e93SStefan Hajnoczi { 2906f321e93SStefan Hajnoczi assert(!s->allocating_write_reqs_plugged); 2916f321e93SStefan Hajnoczi 2926f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = true; 2936f321e93SStefan Hajnoczi } 2946f321e93SStefan Hajnoczi 2956f321e93SStefan Hajnoczi static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) 2966f321e93SStefan Hajnoczi { 2976f321e93SStefan Hajnoczi QEDAIOCB *acb; 2986f321e93SStefan Hajnoczi 2996f321e93SStefan Hajnoczi assert(s->allocating_write_reqs_plugged); 3006f321e93SStefan Hajnoczi 3016f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = false; 3026f321e93SStefan Hajnoczi 3036f321e93SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 3046f321e93SStefan Hajnoczi if (acb) { 3056f321e93SStefan Hajnoczi qed_aio_next_io(acb, 0); 3066f321e93SStefan Hajnoczi } 3076f321e93SStefan Hajnoczi } 3086f321e93SStefan Hajnoczi 3096f321e93SStefan Hajnoczi static void qed_finish_clear_need_check(void *opaque, int ret) 3106f321e93SStefan Hajnoczi { 3116f321e93SStefan Hajnoczi /* Do nothing */ 3126f321e93SStefan Hajnoczi } 3136f321e93SStefan Hajnoczi 3146f321e93SStefan Hajnoczi static void qed_flush_after_clear_need_check(void *opaque, int ret) 3156f321e93SStefan Hajnoczi { 3166f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3176f321e93SStefan Hajnoczi 3186f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s); 3196f321e93SStefan Hajnoczi 3206f321e93SStefan Hajnoczi /* No need to wait until flush completes */ 3216f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3226f321e93SStefan Hajnoczi } 3236f321e93SStefan Hajnoczi 3246f321e93SStefan Hajnoczi static void qed_clear_need_check(void *opaque, int ret) 3256f321e93SStefan Hajnoczi { 3266f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3276f321e93SStefan Hajnoczi 3286f321e93SStefan Hajnoczi if (ret) { 3296f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3306f321e93SStefan Hajnoczi return; 3316f321e93SStefan Hajnoczi } 3326f321e93SStefan Hajnoczi 3336f321e93SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 3346f321e93SStefan Hajnoczi qed_write_header(s, qed_flush_after_clear_need_check, s); 3356f321e93SStefan Hajnoczi } 3366f321e93SStefan Hajnoczi 3376f321e93SStefan Hajnoczi static void qed_need_check_timer_cb(void *opaque) 3386f321e93SStefan Hajnoczi { 3396f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3406f321e93SStefan Hajnoczi 3416f321e93SStefan Hajnoczi /* The timer should only fire when allocating writes have drained */ 3426f321e93SStefan Hajnoczi assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs)); 3436f321e93SStefan Hajnoczi 3446f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s); 3456f321e93SStefan Hajnoczi 3466f321e93SStefan Hajnoczi qed_plug_allocating_write_reqs(s); 3476f321e93SStefan Hajnoczi 3486f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */ 3496f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_clear_need_check, s); 3506f321e93SStefan Hajnoczi } 3516f321e93SStefan Hajnoczi 3526f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s) 3536f321e93SStefan Hajnoczi { 3546f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s); 3556f321e93SStefan Hajnoczi 3566f321e93SStefan Hajnoczi /* Use vm_clock so we don't alter the image file while suspended for 3576f321e93SStefan Hajnoczi * migration. 3586f321e93SStefan Hajnoczi */ 3596f321e93SStefan Hajnoczi qemu_mod_timer(s->need_check_timer, qemu_get_clock_ns(vm_clock) + 3606f321e93SStefan Hajnoczi get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT); 3616f321e93SStefan Hajnoczi } 3626f321e93SStefan Hajnoczi 3636f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */ 3646f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s) 3656f321e93SStefan Hajnoczi { 3666f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s); 3676f321e93SStefan Hajnoczi qemu_del_timer(s->need_check_timer); 3686f321e93SStefan Hajnoczi } 3696f321e93SStefan Hajnoczi 37075411d23SStefan Hajnoczi static int bdrv_qed_open(BlockDriverState *bs, int flags) 37175411d23SStefan Hajnoczi { 37275411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 37375411d23SStefan Hajnoczi QEDHeader le_header; 37475411d23SStefan Hajnoczi int64_t file_size; 37575411d23SStefan Hajnoczi int ret; 37675411d23SStefan Hajnoczi 37775411d23SStefan Hajnoczi s->bs = bs; 378eabba580SStefan Hajnoczi QSIMPLEQ_INIT(&s->allocating_write_reqs); 37975411d23SStefan Hajnoczi 38075411d23SStefan Hajnoczi ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); 38175411d23SStefan Hajnoczi if (ret < 0) { 38275411d23SStefan Hajnoczi return ret; 38375411d23SStefan Hajnoczi } 38475411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header); 38575411d23SStefan Hajnoczi 38675411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) { 38775411d23SStefan Hajnoczi return -EINVAL; 38875411d23SStefan Hajnoczi } 38975411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) { 39010b758e8SKevin Wolf /* image uses unsupported feature bits */ 39110b758e8SKevin Wolf char buf[64]; 39210b758e8SKevin Wolf snprintf(buf, sizeof(buf), "%" PRIx64, 39310b758e8SKevin Wolf s->header.features & ~QED_FEATURE_MASK); 39410b758e8SKevin Wolf qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, 39510b758e8SKevin Wolf bs->device_name, "QED", buf); 39610b758e8SKevin Wolf return -ENOTSUP; 39775411d23SStefan Hajnoczi } 39875411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 39975411d23SStefan Hajnoczi return -EINVAL; 40075411d23SStefan Hajnoczi } 40175411d23SStefan Hajnoczi 40275411d23SStefan Hajnoczi /* Round down file size to the last cluster */ 40375411d23SStefan Hajnoczi file_size = bdrv_getlength(bs->file); 40475411d23SStefan Hajnoczi if (file_size < 0) { 40575411d23SStefan Hajnoczi return file_size; 40675411d23SStefan Hajnoczi } 40775411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size); 40875411d23SStefan Hajnoczi 40975411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) { 41075411d23SStefan Hajnoczi return -EINVAL; 41175411d23SStefan Hajnoczi } 41275411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size, 41375411d23SStefan Hajnoczi s->header.cluster_size, 41475411d23SStefan Hajnoczi s->header.table_size)) { 41575411d23SStefan Hajnoczi return -EINVAL; 41675411d23SStefan Hajnoczi } 41775411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 41875411d23SStefan Hajnoczi return -EINVAL; 41975411d23SStefan Hajnoczi } 42075411d23SStefan Hajnoczi 42175411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) / 42275411d23SStefan Hajnoczi sizeof(uint64_t); 42375411d23SStefan Hajnoczi s->l2_shift = ffs(s->header.cluster_size) - 1; 42475411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1; 42575411d23SStefan Hajnoczi s->l1_shift = s->l2_shift + ffs(s->table_nelems) - 1; 42675411d23SStefan Hajnoczi 42775411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) { 42875411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset + 42975411d23SStefan Hajnoczi s->header.backing_filename_size > 43075411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) { 43175411d23SStefan Hajnoczi return -EINVAL; 43275411d23SStefan Hajnoczi } 43375411d23SStefan Hajnoczi 43475411d23SStefan Hajnoczi ret = qed_read_string(bs->file, s->header.backing_filename_offset, 43575411d23SStefan Hajnoczi s->header.backing_filename_size, bs->backing_file, 43675411d23SStefan Hajnoczi sizeof(bs->backing_file)); 43775411d23SStefan Hajnoczi if (ret < 0) { 43875411d23SStefan Hajnoczi return ret; 43975411d23SStefan Hajnoczi } 44075411d23SStefan Hajnoczi 44175411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 44275411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 44375411d23SStefan Hajnoczi } 44475411d23SStefan Hajnoczi } 44575411d23SStefan Hajnoczi 44675411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards 44775411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older 44875411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is 44975411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear 45075411d23SStefan Hajnoczi * feature is no longer valid. 45175411d23SStefan Hajnoczi */ 45275411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 45375411d23SStefan Hajnoczi !bdrv_is_read_only(bs->file)) { 45475411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 45575411d23SStefan Hajnoczi 45675411d23SStefan Hajnoczi ret = qed_write_header_sync(s); 45775411d23SStefan Hajnoczi if (ret) { 45875411d23SStefan Hajnoczi return ret; 45975411d23SStefan Hajnoczi } 46075411d23SStefan Hajnoczi 46175411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */ 46275411d23SStefan Hajnoczi bdrv_flush(bs->file); 46375411d23SStefan Hajnoczi } 46475411d23SStefan Hajnoczi 465298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s); 466298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache); 467298800caSStefan Hajnoczi 468298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s); 469298800caSStefan Hajnoczi if (ret) { 47001979a98SStefan Hajnoczi goto out; 47101979a98SStefan Hajnoczi } 47201979a98SStefan Hajnoczi 47301979a98SStefan Hajnoczi /* If image was not closed cleanly, check consistency */ 47401979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 47501979a98SStefan Hajnoczi /* Read-only images cannot be fixed. There is no risk of corruption 47601979a98SStefan Hajnoczi * since write operations are not possible. Therefore, allow 47701979a98SStefan Hajnoczi * potentially inconsistent images to be opened read-only. This can 47801979a98SStefan Hajnoczi * aid data recovery from an otherwise inconsistent image. 47901979a98SStefan Hajnoczi */ 48001979a98SStefan Hajnoczi if (!bdrv_is_read_only(bs->file)) { 48101979a98SStefan Hajnoczi BdrvCheckResult result = {0}; 48201979a98SStefan Hajnoczi 48301979a98SStefan Hajnoczi ret = qed_check(s, &result, true); 4846f321e93SStefan Hajnoczi if (ret) { 4856f321e93SStefan Hajnoczi goto out; 4866f321e93SStefan Hajnoczi } 4876f321e93SStefan Hajnoczi if (!result.corruptions && !result.check_errors) { 48801979a98SStefan Hajnoczi /* Ensure fixes reach storage before clearing check bit */ 48901979a98SStefan Hajnoczi bdrv_flush(s->bs); 49001979a98SStefan Hajnoczi 49101979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 49201979a98SStefan Hajnoczi qed_write_header_sync(s); 49301979a98SStefan Hajnoczi } 49401979a98SStefan Hajnoczi } 49501979a98SStefan Hajnoczi } 49601979a98SStefan Hajnoczi 4976f321e93SStefan Hajnoczi s->need_check_timer = qemu_new_timer_ns(vm_clock, 4986f321e93SStefan Hajnoczi qed_need_check_timer_cb, s); 4996f321e93SStefan Hajnoczi 5001ed520c6SAnthony Liguori error_set(&s->migration_blocker, 5011ed520c6SAnthony Liguori QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, 5021ed520c6SAnthony Liguori "qed", bs->device_name, "live migration"); 5031ed520c6SAnthony Liguori migrate_add_blocker(s->migration_blocker); 5041ed520c6SAnthony Liguori 5051ed520c6SAnthony Liguori 50601979a98SStefan Hajnoczi out: 50701979a98SStefan Hajnoczi if (ret) { 508298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 509298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 510298800caSStefan Hajnoczi } 51175411d23SStefan Hajnoczi return ret; 51275411d23SStefan Hajnoczi } 51375411d23SStefan Hajnoczi 51475411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 51575411d23SStefan Hajnoczi { 516298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 517298800caSStefan Hajnoczi 5181ed520c6SAnthony Liguori migrate_del_blocker(s->migration_blocker); 5191ed520c6SAnthony Liguori error_free(s->migration_blocker); 5201ed520c6SAnthony Liguori 5216f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 5226f321e93SStefan Hajnoczi qemu_free_timer(s->need_check_timer); 5236f321e93SStefan Hajnoczi 52401979a98SStefan Hajnoczi /* Ensure writes reach stable storage */ 52501979a98SStefan Hajnoczi bdrv_flush(bs->file); 52601979a98SStefan Hajnoczi 52701979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */ 52801979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 52901979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 53001979a98SStefan Hajnoczi qed_write_header_sync(s); 53101979a98SStefan Hajnoczi } 53201979a98SStefan Hajnoczi 533298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 534298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 53575411d23SStefan Hajnoczi } 53675411d23SStefan Hajnoczi 53775411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size, 53875411d23SStefan Hajnoczi uint64_t image_size, uint32_t table_size, 53975411d23SStefan Hajnoczi const char *backing_file, const char *backing_fmt) 54075411d23SStefan Hajnoczi { 54175411d23SStefan Hajnoczi QEDHeader header = { 54275411d23SStefan Hajnoczi .magic = QED_MAGIC, 54375411d23SStefan Hajnoczi .cluster_size = cluster_size, 54475411d23SStefan Hajnoczi .table_size = table_size, 54575411d23SStefan Hajnoczi .header_size = 1, 54675411d23SStefan Hajnoczi .features = 0, 54775411d23SStefan Hajnoczi .compat_features = 0, 54875411d23SStefan Hajnoczi .l1_table_offset = cluster_size, 54975411d23SStefan Hajnoczi .image_size = image_size, 55075411d23SStefan Hajnoczi }; 55175411d23SStefan Hajnoczi QEDHeader le_header; 55275411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 55375411d23SStefan Hajnoczi size_t l1_size = header.cluster_size * header.table_size; 55475411d23SStefan Hajnoczi int ret = 0; 55575411d23SStefan Hajnoczi BlockDriverState *bs = NULL; 55675411d23SStefan Hajnoczi 55775411d23SStefan Hajnoczi ret = bdrv_create_file(filename, NULL); 55875411d23SStefan Hajnoczi if (ret < 0) { 55975411d23SStefan Hajnoczi return ret; 56075411d23SStefan Hajnoczi } 56175411d23SStefan Hajnoczi 56275411d23SStefan Hajnoczi ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR | BDRV_O_CACHE_WB); 56375411d23SStefan Hajnoczi if (ret < 0) { 56475411d23SStefan Hajnoczi return ret; 56575411d23SStefan Hajnoczi } 56675411d23SStefan Hajnoczi 567c743849bSStefan Hajnoczi /* File must start empty and grow, check truncate is supported */ 568c743849bSStefan Hajnoczi ret = bdrv_truncate(bs, 0); 569c743849bSStefan Hajnoczi if (ret < 0) { 570c743849bSStefan Hajnoczi goto out; 571c743849bSStefan Hajnoczi } 572c743849bSStefan Hajnoczi 57375411d23SStefan Hajnoczi if (backing_file) { 57475411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 57575411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 57675411d23SStefan Hajnoczi header.backing_filename_size = strlen(backing_file); 57775411d23SStefan Hajnoczi 57875411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 57975411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 58075411d23SStefan Hajnoczi } 58175411d23SStefan Hajnoczi } 58275411d23SStefan Hajnoczi 58375411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 58475411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header)); 58575411d23SStefan Hajnoczi if (ret < 0) { 58675411d23SStefan Hajnoczi goto out; 58775411d23SStefan Hajnoczi } 58875411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, sizeof(le_header), backing_file, 58975411d23SStefan Hajnoczi header.backing_filename_size); 59075411d23SStefan Hajnoczi if (ret < 0) { 59175411d23SStefan Hajnoczi goto out; 59275411d23SStefan Hajnoczi } 59375411d23SStefan Hajnoczi 5947267c094SAnthony Liguori l1_table = g_malloc0(l1_size); 59575411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size); 59675411d23SStefan Hajnoczi if (ret < 0) { 59775411d23SStefan Hajnoczi goto out; 59875411d23SStefan Hajnoczi } 59975411d23SStefan Hajnoczi 60075411d23SStefan Hajnoczi ret = 0; /* success */ 60175411d23SStefan Hajnoczi out: 6027267c094SAnthony Liguori g_free(l1_table); 60375411d23SStefan Hajnoczi bdrv_delete(bs); 60475411d23SStefan Hajnoczi return ret; 60575411d23SStefan Hajnoczi } 60675411d23SStefan Hajnoczi 60775411d23SStefan Hajnoczi static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options) 60875411d23SStefan Hajnoczi { 60975411d23SStefan Hajnoczi uint64_t image_size = 0; 61075411d23SStefan Hajnoczi uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 61175411d23SStefan Hajnoczi uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 61275411d23SStefan Hajnoczi const char *backing_file = NULL; 61375411d23SStefan Hajnoczi const char *backing_fmt = NULL; 61475411d23SStefan Hajnoczi 61575411d23SStefan Hajnoczi while (options && options->name) { 61675411d23SStefan Hajnoczi if (!strcmp(options->name, BLOCK_OPT_SIZE)) { 61775411d23SStefan Hajnoczi image_size = options->value.n; 61875411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { 61975411d23SStefan Hajnoczi backing_file = options->value.s; 62075411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { 62175411d23SStefan Hajnoczi backing_fmt = options->value.s; 62275411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { 62375411d23SStefan Hajnoczi if (options->value.n) { 62475411d23SStefan Hajnoczi cluster_size = options->value.n; 62575411d23SStefan Hajnoczi } 62675411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_TABLE_SIZE)) { 62775411d23SStefan Hajnoczi if (options->value.n) { 62875411d23SStefan Hajnoczi table_size = options->value.n; 62975411d23SStefan Hajnoczi } 63075411d23SStefan Hajnoczi } 63175411d23SStefan Hajnoczi options++; 63275411d23SStefan Hajnoczi } 63375411d23SStefan Hajnoczi 63475411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(cluster_size)) { 63575411d23SStefan Hajnoczi fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n", 63675411d23SStefan Hajnoczi QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 63775411d23SStefan Hajnoczi return -EINVAL; 63875411d23SStefan Hajnoczi } 63975411d23SStefan Hajnoczi if (!qed_is_table_size_valid(table_size)) { 64075411d23SStefan Hajnoczi fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n", 64175411d23SStefan Hajnoczi QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 64275411d23SStefan Hajnoczi return -EINVAL; 64375411d23SStefan Hajnoczi } 64475411d23SStefan Hajnoczi if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 64575411d23SStefan Hajnoczi fprintf(stderr, "QED image size must be a non-zero multiple of " 64675411d23SStefan Hajnoczi "cluster size and less than %" PRIu64 " bytes\n", 64775411d23SStefan Hajnoczi qed_max_image_size(cluster_size, table_size)); 64875411d23SStefan Hajnoczi return -EINVAL; 64975411d23SStefan Hajnoczi } 65075411d23SStefan Hajnoczi 65175411d23SStefan Hajnoczi return qed_create(filename, cluster_size, image_size, table_size, 65275411d23SStefan Hajnoczi backing_file, backing_fmt); 65375411d23SStefan Hajnoczi } 65475411d23SStefan Hajnoczi 655298800caSStefan Hajnoczi typedef struct { 656b7d5a5b8SStefan Hajnoczi Coroutine *co; 657298800caSStefan Hajnoczi int is_allocated; 658298800caSStefan Hajnoczi int *pnum; 659298800caSStefan Hajnoczi } QEDIsAllocatedCB; 660298800caSStefan Hajnoczi 661298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 662298800caSStefan Hajnoczi { 663298800caSStefan Hajnoczi QEDIsAllocatedCB *cb = opaque; 664298800caSStefan Hajnoczi *cb->pnum = len / BDRV_SECTOR_SIZE; 66521df65b6SAnthony Liguori cb->is_allocated = (ret == QED_CLUSTER_FOUND || ret == QED_CLUSTER_ZERO); 666b7d5a5b8SStefan Hajnoczi if (cb->co) { 667b7d5a5b8SStefan Hajnoczi qemu_coroutine_enter(cb->co, NULL); 668b7d5a5b8SStefan Hajnoczi } 669298800caSStefan Hajnoczi } 670298800caSStefan Hajnoczi 671b7d5a5b8SStefan Hajnoczi static int coroutine_fn bdrv_qed_co_is_allocated(BlockDriverState *bs, 672b7d5a5b8SStefan Hajnoczi int64_t sector_num, 67375411d23SStefan Hajnoczi int nb_sectors, int *pnum) 67475411d23SStefan Hajnoczi { 675298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 676298800caSStefan Hajnoczi uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 677298800caSStefan Hajnoczi size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 678298800caSStefan Hajnoczi QEDIsAllocatedCB cb = { 679298800caSStefan Hajnoczi .is_allocated = -1, 680298800caSStefan Hajnoczi .pnum = pnum, 681298800caSStefan Hajnoczi }; 682298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 683298800caSStefan Hajnoczi 684298800caSStefan Hajnoczi qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb); 685298800caSStefan Hajnoczi 686b7d5a5b8SStefan Hajnoczi /* Now sleep if the callback wasn't invoked immediately */ 687298800caSStefan Hajnoczi while (cb.is_allocated == -1) { 688b7d5a5b8SStefan Hajnoczi cb.co = qemu_coroutine_self(); 689b7d5a5b8SStefan Hajnoczi qemu_coroutine_yield(); 690298800caSStefan Hajnoczi } 691298800caSStefan Hajnoczi 692298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 693298800caSStefan Hajnoczi 694298800caSStefan Hajnoczi return cb.is_allocated; 69575411d23SStefan Hajnoczi } 69675411d23SStefan Hajnoczi 69775411d23SStefan Hajnoczi static int bdrv_qed_make_empty(BlockDriverState *bs) 69875411d23SStefan Hajnoczi { 69975411d23SStefan Hajnoczi return -ENOTSUP; 70075411d23SStefan Hajnoczi } 70175411d23SStefan Hajnoczi 702eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 703eabba580SStefan Hajnoczi { 704eabba580SStefan Hajnoczi return acb->common.bs->opaque; 705eabba580SStefan Hajnoczi } 706eabba580SStefan Hajnoczi 707eabba580SStefan Hajnoczi /** 708eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 709eabba580SStefan Hajnoczi * 710eabba580SStefan Hajnoczi * @s: QED state 711eabba580SStefan Hajnoczi * @pos: Byte position in device 712eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 713eabba580SStefan Hajnoczi * @cb: Completion function 714eabba580SStefan Hajnoczi * @opaque: User data for completion function 715eabba580SStefan Hajnoczi * 716eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 717eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 718eabba580SStefan Hajnoczi */ 719eabba580SStefan Hajnoczi static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 720eabba580SStefan Hajnoczi QEMUIOVector *qiov, 721eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, void *opaque) 722eabba580SStefan Hajnoczi { 723eabba580SStefan Hajnoczi uint64_t backing_length = 0; 724eabba580SStefan Hajnoczi size_t size; 725eabba580SStefan Hajnoczi 726eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 727eabba580SStefan Hajnoczi * backing file like a zero length backing file. 728eabba580SStefan Hajnoczi */ 729eabba580SStefan Hajnoczi if (s->bs->backing_hd) { 730eabba580SStefan Hajnoczi int64_t l = bdrv_getlength(s->bs->backing_hd); 731eabba580SStefan Hajnoczi if (l < 0) { 732eabba580SStefan Hajnoczi cb(opaque, l); 733eabba580SStefan Hajnoczi return; 734eabba580SStefan Hajnoczi } 735eabba580SStefan Hajnoczi backing_length = l; 736eabba580SStefan Hajnoczi } 737eabba580SStefan Hajnoczi 738eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 739eabba580SStefan Hajnoczi if (pos >= backing_length || 740eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 741eabba580SStefan Hajnoczi qemu_iovec_memset(qiov, 0, qiov->size); 742eabba580SStefan Hajnoczi } 743eabba580SStefan Hajnoczi 744eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 745eabba580SStefan Hajnoczi if (pos >= backing_length) { 746eabba580SStefan Hajnoczi cb(opaque, 0); 747eabba580SStefan Hajnoczi return; 748eabba580SStefan Hajnoczi } 749eabba580SStefan Hajnoczi 750eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 751eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 752eabba580SStefan Hajnoczi 753eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING); 754ad54ae80SPaolo Bonzini bdrv_aio_readv(s->bs->backing_hd, pos / BDRV_SECTOR_SIZE, 755eabba580SStefan Hajnoczi qiov, size / BDRV_SECTOR_SIZE, cb, opaque); 756eabba580SStefan Hajnoczi } 757eabba580SStefan Hajnoczi 758eabba580SStefan Hajnoczi typedef struct { 759eabba580SStefan Hajnoczi GenericCB gencb; 760eabba580SStefan Hajnoczi BDRVQEDState *s; 761eabba580SStefan Hajnoczi QEMUIOVector qiov; 762eabba580SStefan Hajnoczi struct iovec iov; 763eabba580SStefan Hajnoczi uint64_t offset; 764eabba580SStefan Hajnoczi } CopyFromBackingFileCB; 765eabba580SStefan Hajnoczi 766eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_cb(void *opaque, int ret) 767eabba580SStefan Hajnoczi { 768eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 769eabba580SStefan Hajnoczi qemu_vfree(copy_cb->iov.iov_base); 770eabba580SStefan Hajnoczi gencb_complete(©_cb->gencb, ret); 771eabba580SStefan Hajnoczi } 772eabba580SStefan Hajnoczi 773eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_write(void *opaque, int ret) 774eabba580SStefan Hajnoczi { 775eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 776eabba580SStefan Hajnoczi BDRVQEDState *s = copy_cb->s; 777eabba580SStefan Hajnoczi 778eabba580SStefan Hajnoczi if (ret) { 779eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb(copy_cb, ret); 780eabba580SStefan Hajnoczi return; 781eabba580SStefan Hajnoczi } 782eabba580SStefan Hajnoczi 783eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 784ad54ae80SPaolo Bonzini bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE, 785ad54ae80SPaolo Bonzini ©_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE, 786eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb, copy_cb); 787eabba580SStefan Hajnoczi } 788eabba580SStefan Hajnoczi 789eabba580SStefan Hajnoczi /** 790eabba580SStefan Hajnoczi * Copy data from backing file into the image 791eabba580SStefan Hajnoczi * 792eabba580SStefan Hajnoczi * @s: QED state 793eabba580SStefan Hajnoczi * @pos: Byte position in device 794eabba580SStefan Hajnoczi * @len: Number of bytes 795eabba580SStefan Hajnoczi * @offset: Byte offset in image file 796eabba580SStefan Hajnoczi * @cb: Completion function 797eabba580SStefan Hajnoczi * @opaque: User data for completion function 798eabba580SStefan Hajnoczi */ 799eabba580SStefan Hajnoczi static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, 800eabba580SStefan Hajnoczi uint64_t len, uint64_t offset, 801eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, 802eabba580SStefan Hajnoczi void *opaque) 803eabba580SStefan Hajnoczi { 804eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb; 805eabba580SStefan Hajnoczi 806eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 807eabba580SStefan Hajnoczi if (len == 0) { 808eabba580SStefan Hajnoczi cb(opaque, 0); 809eabba580SStefan Hajnoczi return; 810eabba580SStefan Hajnoczi } 811eabba580SStefan Hajnoczi 812eabba580SStefan Hajnoczi copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque); 813eabba580SStefan Hajnoczi copy_cb->s = s; 814eabba580SStefan Hajnoczi copy_cb->offset = offset; 815eabba580SStefan Hajnoczi copy_cb->iov.iov_base = qemu_blockalign(s->bs, len); 816eabba580SStefan Hajnoczi copy_cb->iov.iov_len = len; 817eabba580SStefan Hajnoczi qemu_iovec_init_external(©_cb->qiov, ©_cb->iov, 1); 818eabba580SStefan Hajnoczi 819eabba580SStefan Hajnoczi qed_read_backing_file(s, pos, ©_cb->qiov, 820eabba580SStefan Hajnoczi qed_copy_from_backing_file_write, copy_cb); 821eabba580SStefan Hajnoczi } 822eabba580SStefan Hajnoczi 823eabba580SStefan Hajnoczi /** 824eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 825eabba580SStefan Hajnoczi * 826eabba580SStefan Hajnoczi * @s: QED state 827eabba580SStefan Hajnoczi * @table: L2 table 828eabba580SStefan Hajnoczi * @index: First cluster index 829eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 83021df65b6SAnthony Liguori * @cluster: First cluster offset 83121df65b6SAnthony Liguori * 83221df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the 83321df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker. 834eabba580SStefan Hajnoczi */ 835eabba580SStefan Hajnoczi static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index, 836eabba580SStefan Hajnoczi unsigned int n, uint64_t cluster) 837eabba580SStefan Hajnoczi { 838eabba580SStefan Hajnoczi int i; 839eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 840eabba580SStefan Hajnoczi table->offsets[i] = cluster; 84121df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) && 84221df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) { 843eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 844eabba580SStefan Hajnoczi } 845eabba580SStefan Hajnoczi } 84621df65b6SAnthony Liguori } 847eabba580SStefan Hajnoczi 848eabba580SStefan Hajnoczi static void qed_aio_complete_bh(void *opaque) 849eabba580SStefan Hajnoczi { 850eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 851eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb = acb->common.cb; 852eabba580SStefan Hajnoczi void *user_opaque = acb->common.opaque; 853eabba580SStefan Hajnoczi int ret = acb->bh_ret; 854eabba580SStefan Hajnoczi bool *finished = acb->finished; 855eabba580SStefan Hajnoczi 856eabba580SStefan Hajnoczi qemu_bh_delete(acb->bh); 857eabba580SStefan Hajnoczi qemu_aio_release(acb); 858eabba580SStefan Hajnoczi 859eabba580SStefan Hajnoczi /* Invoke callback */ 860eabba580SStefan Hajnoczi cb(user_opaque, ret); 861eabba580SStefan Hajnoczi 862eabba580SStefan Hajnoczi /* Signal cancel completion */ 863eabba580SStefan Hajnoczi if (finished) { 864eabba580SStefan Hajnoczi *finished = true; 865eabba580SStefan Hajnoczi } 866eabba580SStefan Hajnoczi } 867eabba580SStefan Hajnoczi 868eabba580SStefan Hajnoczi static void qed_aio_complete(QEDAIOCB *acb, int ret) 869eabba580SStefan Hajnoczi { 870eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 871eabba580SStefan Hajnoczi 872eabba580SStefan Hajnoczi trace_qed_aio_complete(s, acb, ret); 873eabba580SStefan Hajnoczi 874eabba580SStefan Hajnoczi /* Free resources */ 875eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov); 876eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 877eabba580SStefan Hajnoczi 8780e71be19SStefan Hajnoczi /* Free the buffer we may have allocated for zero writes */ 8790e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 8800e71be19SStefan Hajnoczi qemu_vfree(acb->qiov->iov[0].iov_base); 8810e71be19SStefan Hajnoczi acb->qiov->iov[0].iov_base = NULL; 8820e71be19SStefan Hajnoczi } 8830e71be19SStefan Hajnoczi 884eabba580SStefan Hajnoczi /* Arrange for a bh to invoke the completion function */ 885eabba580SStefan Hajnoczi acb->bh_ret = ret; 886eabba580SStefan Hajnoczi acb->bh = qemu_bh_new(qed_aio_complete_bh, acb); 887eabba580SStefan Hajnoczi qemu_bh_schedule(acb->bh); 888eabba580SStefan Hajnoczi 889eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 890eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 891eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 892eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 893eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 894eabba580SStefan Hajnoczi */ 895eabba580SStefan Hajnoczi if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 896eabba580SStefan Hajnoczi QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next); 897eabba580SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 898eabba580SStefan Hajnoczi if (acb) { 899eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 9006f321e93SStefan Hajnoczi } else if (s->header.features & QED_F_NEED_CHECK) { 9016f321e93SStefan Hajnoczi qed_start_need_check_timer(s); 902eabba580SStefan Hajnoczi } 903eabba580SStefan Hajnoczi } 904eabba580SStefan Hajnoczi } 905eabba580SStefan Hajnoczi 906eabba580SStefan Hajnoczi /** 907eabba580SStefan Hajnoczi * Commit the current L2 table to the cache 908eabba580SStefan Hajnoczi */ 909eabba580SStefan Hajnoczi static void qed_commit_l2_update(void *opaque, int ret) 910eabba580SStefan Hajnoczi { 911eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 912eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 913eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 914e4fc8781SStefan Hajnoczi uint64_t l2_offset = l2_table->offset; 915eabba580SStefan Hajnoczi 916eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 917eabba580SStefan Hajnoczi 918eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 919eabba580SStefan Hajnoczi * cache. 920eabba580SStefan Hajnoczi */ 921e4fc8781SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 922eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 923eabba580SStefan Hajnoczi 924eabba580SStefan Hajnoczi qed_aio_next_io(opaque, ret); 925eabba580SStefan Hajnoczi } 926eabba580SStefan Hajnoczi 927eabba580SStefan Hajnoczi /** 928eabba580SStefan Hajnoczi * Update L1 table with new L2 table offset and write it out 929eabba580SStefan Hajnoczi */ 930eabba580SStefan Hajnoczi static void qed_aio_write_l1_update(void *opaque, int ret) 931eabba580SStefan Hajnoczi { 932eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 933eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 934eabba580SStefan Hajnoczi int index; 935eabba580SStefan Hajnoczi 936eabba580SStefan Hajnoczi if (ret) { 937eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 938eabba580SStefan Hajnoczi return; 939eabba580SStefan Hajnoczi } 940eabba580SStefan Hajnoczi 941eabba580SStefan Hajnoczi index = qed_l1_index(s, acb->cur_pos); 942eabba580SStefan Hajnoczi s->l1_table->offsets[index] = acb->request.l2_table->offset; 943eabba580SStefan Hajnoczi 944eabba580SStefan Hajnoczi qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb); 945eabba580SStefan Hajnoczi } 946eabba580SStefan Hajnoczi 947eabba580SStefan Hajnoczi /** 948eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 949eabba580SStefan Hajnoczi */ 9500e71be19SStefan Hajnoczi static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset) 951eabba580SStefan Hajnoczi { 952eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 953eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 954eabba580SStefan Hajnoczi int index; 955eabba580SStefan Hajnoczi 956eabba580SStefan Hajnoczi if (ret) { 957eabba580SStefan Hajnoczi goto err; 958eabba580SStefan Hajnoczi } 959eabba580SStefan Hajnoczi 960eabba580SStefan Hajnoczi if (need_alloc) { 961eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 962eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 963eabba580SStefan Hajnoczi } 964eabba580SStefan Hajnoczi 965eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 966eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 9670e71be19SStefan Hajnoczi offset); 968eabba580SStefan Hajnoczi 969eabba580SStefan Hajnoczi if (need_alloc) { 970eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 971eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true, 972eabba580SStefan Hajnoczi qed_aio_write_l1_update, acb); 973eabba580SStefan Hajnoczi } else { 974eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 975eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false, 976eabba580SStefan Hajnoczi qed_aio_next_io, acb); 977eabba580SStefan Hajnoczi } 978eabba580SStefan Hajnoczi return; 979eabba580SStefan Hajnoczi 980eabba580SStefan Hajnoczi err: 981eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 982eabba580SStefan Hajnoczi } 983eabba580SStefan Hajnoczi 9840e71be19SStefan Hajnoczi static void qed_aio_write_l2_update_cb(void *opaque, int ret) 9850e71be19SStefan Hajnoczi { 9860e71be19SStefan Hajnoczi QEDAIOCB *acb = opaque; 9870e71be19SStefan Hajnoczi qed_aio_write_l2_update(acb, ret, acb->cur_cluster); 9880e71be19SStefan Hajnoczi } 9890e71be19SStefan Hajnoczi 990eabba580SStefan Hajnoczi /** 991eabba580SStefan Hajnoczi * Flush new data clusters before updating the L2 table 992eabba580SStefan Hajnoczi * 993eabba580SStefan Hajnoczi * This flush is necessary when a backing file is in use. A crash during an 994eabba580SStefan Hajnoczi * allocating write could result in empty clusters in the image. If the write 995eabba580SStefan Hajnoczi * only touched a subregion of the cluster, then backing image sectors have 996eabba580SStefan Hajnoczi * been lost in the untouched region. The solution is to flush after writing a 997eabba580SStefan Hajnoczi * new data cluster and before updating the L2 table. 998eabba580SStefan Hajnoczi */ 999eabba580SStefan Hajnoczi static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) 1000eabba580SStefan Hajnoczi { 1001eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1002eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1003eabba580SStefan Hajnoczi 10040e71be19SStefan Hajnoczi if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update_cb, opaque)) { 1005eabba580SStefan Hajnoczi qed_aio_complete(acb, -EIO); 1006eabba580SStefan Hajnoczi } 1007eabba580SStefan Hajnoczi } 1008eabba580SStefan Hajnoczi 1009eabba580SStefan Hajnoczi /** 1010eabba580SStefan Hajnoczi * Write data to the image file 1011eabba580SStefan Hajnoczi */ 1012eabba580SStefan Hajnoczi static void qed_aio_write_main(void *opaque, int ret) 1013eabba580SStefan Hajnoczi { 1014eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1015eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1016eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1017eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 1018eabba580SStefan Hajnoczi BlockDriverCompletionFunc *next_fn; 1019eabba580SStefan Hajnoczi 1020eabba580SStefan Hajnoczi trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size); 1021eabba580SStefan Hajnoczi 1022eabba580SStefan Hajnoczi if (ret) { 1023eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1024eabba580SStefan Hajnoczi return; 1025eabba580SStefan Hajnoczi } 1026eabba580SStefan Hajnoczi 1027eabba580SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_FOUND) { 1028eabba580SStefan Hajnoczi next_fn = qed_aio_next_io; 1029eabba580SStefan Hajnoczi } else { 1030eabba580SStefan Hajnoczi if (s->bs->backing_hd) { 1031eabba580SStefan Hajnoczi next_fn = qed_aio_write_flush_before_l2_update; 1032eabba580SStefan Hajnoczi } else { 10330e71be19SStefan Hajnoczi next_fn = qed_aio_write_l2_update_cb; 1034eabba580SStefan Hajnoczi } 1035eabba580SStefan Hajnoczi } 1036eabba580SStefan Hajnoczi 1037eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 1038ad54ae80SPaolo Bonzini bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE, 1039ad54ae80SPaolo Bonzini &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1040eabba580SStefan Hajnoczi next_fn, acb); 1041eabba580SStefan Hajnoczi } 1042eabba580SStefan Hajnoczi 1043eabba580SStefan Hajnoczi /** 1044eabba580SStefan Hajnoczi * Populate back untouched region of new data cluster 1045eabba580SStefan Hajnoczi */ 1046eabba580SStefan Hajnoczi static void qed_aio_write_postfill(void *opaque, int ret) 1047eabba580SStefan Hajnoczi { 1048eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1049eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1050eabba580SStefan Hajnoczi uint64_t start = acb->cur_pos + acb->cur_qiov.size; 1051eabba580SStefan Hajnoczi uint64_t len = 1052eabba580SStefan Hajnoczi qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1053eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1054eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + 1055eabba580SStefan Hajnoczi acb->cur_qiov.size; 1056eabba580SStefan Hajnoczi 1057eabba580SStefan Hajnoczi if (ret) { 1058eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1059eabba580SStefan Hajnoczi return; 1060eabba580SStefan Hajnoczi } 1061eabba580SStefan Hajnoczi 1062eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 1063eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, offset, 1064eabba580SStefan Hajnoczi qed_aio_write_main, acb); 1065eabba580SStefan Hajnoczi } 1066eabba580SStefan Hajnoczi 1067eabba580SStefan Hajnoczi /** 1068eabba580SStefan Hajnoczi * Populate front untouched region of new data cluster 1069eabba580SStefan Hajnoczi */ 1070eabba580SStefan Hajnoczi static void qed_aio_write_prefill(void *opaque, int ret) 1071eabba580SStefan Hajnoczi { 1072eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1073eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1074eabba580SStefan Hajnoczi uint64_t start = qed_start_of_cluster(s, acb->cur_pos); 1075eabba580SStefan Hajnoczi uint64_t len = qed_offset_into_cluster(s, acb->cur_pos); 1076eabba580SStefan Hajnoczi 1077eabba580SStefan Hajnoczi trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1078eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, acb->cur_cluster, 1079eabba580SStefan Hajnoczi qed_aio_write_postfill, acb); 1080eabba580SStefan Hajnoczi } 1081eabba580SStefan Hajnoczi 1082eabba580SStefan Hajnoczi /** 10830d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write 10840d09c797SStefan Hajnoczi */ 10850d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s) 10860d09c797SStefan Hajnoczi { 10870d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */ 10880d09c797SStefan Hajnoczi if (s->bs->backing_hd) { 10890d09c797SStefan Hajnoczi return false; 10900d09c797SStefan Hajnoczi } 10910d09c797SStefan Hajnoczi 10920d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK); 10930d09c797SStefan Hajnoczi } 10940d09c797SStefan Hajnoczi 10950e71be19SStefan Hajnoczi static void qed_aio_write_zero_cluster(void *opaque, int ret) 10960e71be19SStefan Hajnoczi { 10970e71be19SStefan Hajnoczi QEDAIOCB *acb = opaque; 10980e71be19SStefan Hajnoczi 10990e71be19SStefan Hajnoczi if (ret) { 11000e71be19SStefan Hajnoczi qed_aio_complete(acb, ret); 11010e71be19SStefan Hajnoczi return; 11020e71be19SStefan Hajnoczi } 11030e71be19SStefan Hajnoczi 11040e71be19SStefan Hajnoczi qed_aio_write_l2_update(acb, 0, 1); 11050e71be19SStefan Hajnoczi } 11060e71be19SStefan Hajnoczi 11070d09c797SStefan Hajnoczi /** 1108eabba580SStefan Hajnoczi * Write new data cluster 1109eabba580SStefan Hajnoczi * 1110eabba580SStefan Hajnoczi * @acb: Write request 1111eabba580SStefan Hajnoczi * @len: Length in bytes 1112eabba580SStefan Hajnoczi * 1113eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters. 1114eabba580SStefan Hajnoczi */ 1115eabba580SStefan Hajnoczi static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1116eabba580SStefan Hajnoczi { 1117eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 11180e71be19SStefan Hajnoczi BlockDriverCompletionFunc *cb; 1119eabba580SStefan Hajnoczi 11206f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */ 11216f321e93SStefan Hajnoczi if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) { 11226f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 11236f321e93SStefan Hajnoczi } 11246f321e93SStefan Hajnoczi 1125eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 1126eabba580SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 1127eabba580SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next); 1128eabba580SStefan Hajnoczi } 11296f321e93SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) || 11306f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged) { 1131eabba580SStefan Hajnoczi return; /* wait for existing request to finish */ 1132eabba580SStefan Hajnoczi } 1133eabba580SStefan Hajnoczi 1134eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 1135eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 1136eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1137eabba580SStefan Hajnoczi 11380e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 11390e71be19SStefan Hajnoczi /* Skip ahead if the clusters are already zero */ 11400e71be19SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { 11410e71be19SStefan Hajnoczi qed_aio_next_io(acb, 0); 11420e71be19SStefan Hajnoczi return; 11430e71be19SStefan Hajnoczi } 11440e71be19SStefan Hajnoczi 11450e71be19SStefan Hajnoczi cb = qed_aio_write_zero_cluster; 11460e71be19SStefan Hajnoczi } else { 11470e71be19SStefan Hajnoczi cb = qed_aio_write_prefill; 11480e71be19SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 11490e71be19SStefan Hajnoczi } 11500e71be19SStefan Hajnoczi 11510d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) { 115201979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK; 11530e71be19SStefan Hajnoczi qed_write_header(s, cb, acb); 11540d09c797SStefan Hajnoczi } else { 11550e71be19SStefan Hajnoczi cb(acb, 0); 11560d09c797SStefan Hajnoczi } 1157eabba580SStefan Hajnoczi } 1158eabba580SStefan Hajnoczi 1159eabba580SStefan Hajnoczi /** 1160eabba580SStefan Hajnoczi * Write data cluster in place 1161eabba580SStefan Hajnoczi * 1162eabba580SStefan Hajnoczi * @acb: Write request 1163eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1164eabba580SStefan Hajnoczi * @len: Length in bytes 1165eabba580SStefan Hajnoczi * 1166eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 1167eabba580SStefan Hajnoczi */ 1168eabba580SStefan Hajnoczi static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) 1169eabba580SStefan Hajnoczi { 11700e71be19SStefan Hajnoczi /* Allocate buffer for zero writes */ 11710e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 11720e71be19SStefan Hajnoczi struct iovec *iov = acb->qiov->iov; 11730e71be19SStefan Hajnoczi 11740e71be19SStefan Hajnoczi if (!iov->iov_base) { 11750e71be19SStefan Hajnoczi iov->iov_base = qemu_blockalign(acb->common.bs, iov->iov_len); 11760e71be19SStefan Hajnoczi memset(iov->iov_base, 0, iov->iov_len); 11770e71be19SStefan Hajnoczi } 11780e71be19SStefan Hajnoczi } 11790e71be19SStefan Hajnoczi 1180eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 1181eabba580SStefan Hajnoczi acb->cur_cluster = offset; 1182eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1183eabba580SStefan Hajnoczi 1184eabba580SStefan Hajnoczi /* Do the actual write */ 1185eabba580SStefan Hajnoczi qed_aio_write_main(acb, 0); 1186eabba580SStefan Hajnoczi } 1187eabba580SStefan Hajnoczi 1188eabba580SStefan Hajnoczi /** 1189eabba580SStefan Hajnoczi * Write data cluster 1190eabba580SStefan Hajnoczi * 1191eabba580SStefan Hajnoczi * @opaque: Write request 1192eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1193eabba580SStefan Hajnoczi * or -errno 1194eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1195eabba580SStefan Hajnoczi * @len: Length in bytes 1196eabba580SStefan Hajnoczi * 1197eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1198eabba580SStefan Hajnoczi */ 1199eabba580SStefan Hajnoczi static void qed_aio_write_data(void *opaque, int ret, 1200eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1201eabba580SStefan Hajnoczi { 1202eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1203eabba580SStefan Hajnoczi 1204eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1205eabba580SStefan Hajnoczi 1206eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 1207eabba580SStefan Hajnoczi 1208eabba580SStefan Hajnoczi switch (ret) { 1209eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 1210eabba580SStefan Hajnoczi qed_aio_write_inplace(acb, offset, len); 1211eabba580SStefan Hajnoczi break; 1212eabba580SStefan Hajnoczi 1213eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 1214eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 121521df65b6SAnthony Liguori case QED_CLUSTER_ZERO: 1216eabba580SStefan Hajnoczi qed_aio_write_alloc(acb, len); 1217eabba580SStefan Hajnoczi break; 1218eabba580SStefan Hajnoczi 1219eabba580SStefan Hajnoczi default: 1220eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1221eabba580SStefan Hajnoczi break; 1222eabba580SStefan Hajnoczi } 1223eabba580SStefan Hajnoczi } 1224eabba580SStefan Hajnoczi 1225eabba580SStefan Hajnoczi /** 1226eabba580SStefan Hajnoczi * Read data cluster 1227eabba580SStefan Hajnoczi * 1228eabba580SStefan Hajnoczi * @opaque: Read request 1229eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1230eabba580SStefan Hajnoczi * or -errno 1231eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1232eabba580SStefan Hajnoczi * @len: Length in bytes 1233eabba580SStefan Hajnoczi * 1234eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1235eabba580SStefan Hajnoczi */ 1236eabba580SStefan Hajnoczi static void qed_aio_read_data(void *opaque, int ret, 1237eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1238eabba580SStefan Hajnoczi { 1239eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1240eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1241eabba580SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 1242eabba580SStefan Hajnoczi 1243eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 1244eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 1245eabba580SStefan Hajnoczi 1246eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 1247eabba580SStefan Hajnoczi 1248eabba580SStefan Hajnoczi if (ret < 0) { 1249eabba580SStefan Hajnoczi goto err; 1250eabba580SStefan Hajnoczi } 1251eabba580SStefan Hajnoczi 1252eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1253eabba580SStefan Hajnoczi 125421df65b6SAnthony Liguori /* Handle zero cluster and backing file reads */ 125521df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) { 125621df65b6SAnthony Liguori qemu_iovec_memset(&acb->cur_qiov, 0, acb->cur_qiov.size); 125721df65b6SAnthony Liguori qed_aio_next_io(acb, 0); 125821df65b6SAnthony Liguori return; 125921df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) { 1260eabba580SStefan Hajnoczi qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1261eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1262eabba580SStefan Hajnoczi return; 1263eabba580SStefan Hajnoczi } 1264eabba580SStefan Hajnoczi 1265eabba580SStefan Hajnoczi BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 1266ad54ae80SPaolo Bonzini bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE, 1267ad54ae80SPaolo Bonzini &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1268eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1269eabba580SStefan Hajnoczi return; 1270eabba580SStefan Hajnoczi 1271eabba580SStefan Hajnoczi err: 1272eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1273eabba580SStefan Hajnoczi } 1274eabba580SStefan Hajnoczi 1275eabba580SStefan Hajnoczi /** 1276eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1277eabba580SStefan Hajnoczi */ 1278eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret) 1279eabba580SStefan Hajnoczi { 1280eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1281eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 12826e4f59bdSStefan Hajnoczi QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ? 12836e4f59bdSStefan Hajnoczi qed_aio_write_data : qed_aio_read_data; 1284eabba580SStefan Hajnoczi 1285eabba580SStefan Hajnoczi trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size); 1286eabba580SStefan Hajnoczi 1287eabba580SStefan Hajnoczi /* Handle I/O error */ 1288eabba580SStefan Hajnoczi if (ret) { 1289eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1290eabba580SStefan Hajnoczi return; 1291eabba580SStefan Hajnoczi } 1292eabba580SStefan Hajnoczi 1293eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1294eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1295eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1296eabba580SStefan Hajnoczi 1297eabba580SStefan Hajnoczi /* Complete request */ 1298eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 1299eabba580SStefan Hajnoczi qed_aio_complete(acb, 0); 1300eabba580SStefan Hajnoczi return; 1301eabba580SStefan Hajnoczi } 1302eabba580SStefan Hajnoczi 1303eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 1304eabba580SStefan Hajnoczi qed_find_cluster(s, &acb->request, 1305eabba580SStefan Hajnoczi acb->cur_pos, acb->end_pos - acb->cur_pos, 1306eabba580SStefan Hajnoczi io_fn, acb); 1307eabba580SStefan Hajnoczi } 1308eabba580SStefan Hajnoczi 1309eabba580SStefan Hajnoczi static BlockDriverAIOCB *qed_aio_setup(BlockDriverState *bs, 1310eabba580SStefan Hajnoczi int64_t sector_num, 1311eabba580SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1312eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, 13136e4f59bdSStefan Hajnoczi void *opaque, int flags) 1314eabba580SStefan Hajnoczi { 1315eabba580SStefan Hajnoczi QEDAIOCB *acb = qemu_aio_get(&qed_aio_pool, bs, cb, opaque); 1316eabba580SStefan Hajnoczi 1317eabba580SStefan Hajnoczi trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, 13186e4f59bdSStefan Hajnoczi opaque, flags); 1319eabba580SStefan Hajnoczi 13206e4f59bdSStefan Hajnoczi acb->flags = flags; 1321eabba580SStefan Hajnoczi acb->finished = NULL; 1322eabba580SStefan Hajnoczi acb->qiov = qiov; 1323eabba580SStefan Hajnoczi acb->qiov_offset = 0; 1324eabba580SStefan Hajnoczi acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 1325eabba580SStefan Hajnoczi acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE; 1326eabba580SStefan Hajnoczi acb->request.l2_table = NULL; 1327eabba580SStefan Hajnoczi qemu_iovec_init(&acb->cur_qiov, qiov->niov); 1328eabba580SStefan Hajnoczi 1329eabba580SStefan Hajnoczi /* Start request */ 1330eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 1331eabba580SStefan Hajnoczi return &acb->common; 1332eabba580SStefan Hajnoczi } 1333eabba580SStefan Hajnoczi 133475411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs, 133575411d23SStefan Hajnoczi int64_t sector_num, 133675411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 133775411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 133875411d23SStefan Hajnoczi void *opaque) 133975411d23SStefan Hajnoczi { 13406e4f59bdSStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 134175411d23SStefan Hajnoczi } 134275411d23SStefan Hajnoczi 134375411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs, 134475411d23SStefan Hajnoczi int64_t sector_num, 134575411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 134675411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 134775411d23SStefan Hajnoczi void *opaque) 134875411d23SStefan Hajnoczi { 13496e4f59bdSStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, 13506e4f59bdSStefan Hajnoczi opaque, QED_AIOCB_WRITE); 135175411d23SStefan Hajnoczi } 135275411d23SStefan Hajnoczi 13530e71be19SStefan Hajnoczi typedef struct { 13540e71be19SStefan Hajnoczi Coroutine *co; 13550e71be19SStefan Hajnoczi int ret; 13560e71be19SStefan Hajnoczi bool done; 13570e71be19SStefan Hajnoczi } QEDWriteZeroesCB; 13580e71be19SStefan Hajnoczi 13590e71be19SStefan Hajnoczi static void coroutine_fn qed_co_write_zeroes_cb(void *opaque, int ret) 13600e71be19SStefan Hajnoczi { 13610e71be19SStefan Hajnoczi QEDWriteZeroesCB *cb = opaque; 13620e71be19SStefan Hajnoczi 13630e71be19SStefan Hajnoczi cb->done = true; 13640e71be19SStefan Hajnoczi cb->ret = ret; 13650e71be19SStefan Hajnoczi if (cb->co) { 13660e71be19SStefan Hajnoczi qemu_coroutine_enter(cb->co, NULL); 13670e71be19SStefan Hajnoczi } 13680e71be19SStefan Hajnoczi } 13690e71be19SStefan Hajnoczi 13700e71be19SStefan Hajnoczi static int coroutine_fn bdrv_qed_co_write_zeroes(BlockDriverState *bs, 13710e71be19SStefan Hajnoczi int64_t sector_num, 13720e71be19SStefan Hajnoczi int nb_sectors) 13730e71be19SStefan Hajnoczi { 13740e71be19SStefan Hajnoczi BlockDriverAIOCB *blockacb; 13750e71be19SStefan Hajnoczi QEDWriteZeroesCB cb = { .done = false }; 13760e71be19SStefan Hajnoczi QEMUIOVector qiov; 13770e71be19SStefan Hajnoczi struct iovec iov; 13780e71be19SStefan Hajnoczi 13790e71be19SStefan Hajnoczi /* Zero writes start without an I/O buffer. If a buffer becomes necessary 13800e71be19SStefan Hajnoczi * then it will be allocated during request processing. 13810e71be19SStefan Hajnoczi */ 13820e71be19SStefan Hajnoczi iov.iov_base = NULL, 13830e71be19SStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE, 13840e71be19SStefan Hajnoczi 13850e71be19SStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 13860e71be19SStefan Hajnoczi blockacb = qed_aio_setup(bs, sector_num, &qiov, nb_sectors, 13870e71be19SStefan Hajnoczi qed_co_write_zeroes_cb, &cb, 13880e71be19SStefan Hajnoczi QED_AIOCB_WRITE | QED_AIOCB_ZERO); 13890e71be19SStefan Hajnoczi if (!blockacb) { 13900e71be19SStefan Hajnoczi return -EIO; 13910e71be19SStefan Hajnoczi } 13920e71be19SStefan Hajnoczi if (!cb.done) { 13930e71be19SStefan Hajnoczi cb.co = qemu_coroutine_self(); 13940e71be19SStefan Hajnoczi qemu_coroutine_yield(); 13950e71be19SStefan Hajnoczi } 13960e71be19SStefan Hajnoczi assert(cb.done); 13970e71be19SStefan Hajnoczi return cb.ret; 13980e71be19SStefan Hajnoczi } 13990e71be19SStefan Hajnoczi 140075411d23SStefan Hajnoczi static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset) 140175411d23SStefan Hajnoczi { 140277a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 140377a5a000SStefan Hajnoczi uint64_t old_image_size; 140477a5a000SStefan Hajnoczi int ret; 140577a5a000SStefan Hajnoczi 140677a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size, 140777a5a000SStefan Hajnoczi s->header.table_size)) { 140877a5a000SStefan Hajnoczi return -EINVAL; 140977a5a000SStefan Hajnoczi } 141077a5a000SStefan Hajnoczi 141177a5a000SStefan Hajnoczi /* Shrinking is currently not supported */ 141277a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) { 141375411d23SStefan Hajnoczi return -ENOTSUP; 141475411d23SStefan Hajnoczi } 141575411d23SStefan Hajnoczi 141677a5a000SStefan Hajnoczi old_image_size = s->header.image_size; 141777a5a000SStefan Hajnoczi s->header.image_size = offset; 141877a5a000SStefan Hajnoczi ret = qed_write_header_sync(s); 141977a5a000SStefan Hajnoczi if (ret < 0) { 142077a5a000SStefan Hajnoczi s->header.image_size = old_image_size; 142177a5a000SStefan Hajnoczi } 142277a5a000SStefan Hajnoczi return ret; 142377a5a000SStefan Hajnoczi } 142477a5a000SStefan Hajnoczi 142575411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs) 142675411d23SStefan Hajnoczi { 142775411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 142875411d23SStefan Hajnoczi return s->header.image_size; 142975411d23SStefan Hajnoczi } 143075411d23SStefan Hajnoczi 143175411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 143275411d23SStefan Hajnoczi { 143375411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 143475411d23SStefan Hajnoczi 143575411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi)); 143675411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size; 1437d68dbee8SDong Xu Wang bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; 143875411d23SStefan Hajnoczi return 0; 143975411d23SStefan Hajnoczi } 144075411d23SStefan Hajnoczi 144175411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 144275411d23SStefan Hajnoczi const char *backing_file, 144375411d23SStefan Hajnoczi const char *backing_fmt) 144475411d23SStefan Hajnoczi { 144575411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 144675411d23SStefan Hajnoczi QEDHeader new_header, le_header; 144775411d23SStefan Hajnoczi void *buffer; 144875411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 144975411d23SStefan Hajnoczi int ret; 145075411d23SStefan Hajnoczi 145175411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 145275411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 145375411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 145475411d23SStefan Hajnoczi * add a new string. 145575411d23SStefan Hajnoczi */ 145675411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 145775411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 145875411d23SStefan Hajnoczi return -ENOTSUP; 145975411d23SStefan Hajnoczi } 146075411d23SStefan Hajnoczi 146175411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 146275411d23SStefan Hajnoczi 146375411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 146475411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 146575411d23SStefan Hajnoczi 146675411d23SStefan Hajnoczi /* Adjust feature flags */ 146775411d23SStefan Hajnoczi if (backing_file) { 146875411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 146975411d23SStefan Hajnoczi 147075411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 147175411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 147275411d23SStefan Hajnoczi } 147375411d23SStefan Hajnoczi } 147475411d23SStefan Hajnoczi 147575411d23SStefan Hajnoczi /* Calculate new header size */ 147675411d23SStefan Hajnoczi backing_file_len = 0; 147775411d23SStefan Hajnoczi 147875411d23SStefan Hajnoczi if (backing_file) { 147975411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 148075411d23SStefan Hajnoczi } 148175411d23SStefan Hajnoczi 148275411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 148375411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 148475411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 148575411d23SStefan Hajnoczi buffer_len += backing_file_len; 148675411d23SStefan Hajnoczi 148775411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 148875411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 148975411d23SStefan Hajnoczi return -ENOSPC; 149075411d23SStefan Hajnoczi } 149175411d23SStefan Hajnoczi 149275411d23SStefan Hajnoczi /* Prepare new header */ 14937267c094SAnthony Liguori buffer = g_malloc(buffer_len); 149475411d23SStefan Hajnoczi 149575411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 149675411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 149775411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 149875411d23SStefan Hajnoczi 1499feba23b1SPavel Borzenkov if (backing_file) { 150075411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 150175411d23SStefan Hajnoczi buffer_len += backing_file_len; 1502feba23b1SPavel Borzenkov } 150375411d23SStefan Hajnoczi 150475411d23SStefan Hajnoczi /* Write new header */ 150575411d23SStefan Hajnoczi ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); 15067267c094SAnthony Liguori g_free(buffer); 150775411d23SStefan Hajnoczi if (ret == 0) { 150875411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 150975411d23SStefan Hajnoczi } 151075411d23SStefan Hajnoczi return ret; 151175411d23SStefan Hajnoczi } 151275411d23SStefan Hajnoczi 1513*c82954e5SBenoît Canet static void bdrv_qed_invalidate_cache(BlockDriverState *bs) 1514*c82954e5SBenoît Canet { 1515*c82954e5SBenoît Canet BDRVQEDState *s = bs->opaque; 1516*c82954e5SBenoît Canet 1517*c82954e5SBenoît Canet bdrv_qed_close(bs); 1518*c82954e5SBenoît Canet memset(s, 0, sizeof(BDRVQEDState)); 1519*c82954e5SBenoît Canet bdrv_qed_open(bs, bs->open_flags); 1520*c82954e5SBenoît Canet } 1521*c82954e5SBenoît Canet 152275411d23SStefan Hajnoczi static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result) 152375411d23SStefan Hajnoczi { 152401979a98SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 152501979a98SStefan Hajnoczi 152601979a98SStefan Hajnoczi return qed_check(s, result, false); 152775411d23SStefan Hajnoczi } 152875411d23SStefan Hajnoczi 152975411d23SStefan Hajnoczi static QEMUOptionParameter qed_create_options[] = { 153075411d23SStefan Hajnoczi { 153175411d23SStefan Hajnoczi .name = BLOCK_OPT_SIZE, 153275411d23SStefan Hajnoczi .type = OPT_SIZE, 153375411d23SStefan Hajnoczi .help = "Virtual disk size (in bytes)" 153475411d23SStefan Hajnoczi }, { 153575411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FILE, 153675411d23SStefan Hajnoczi .type = OPT_STRING, 153775411d23SStefan Hajnoczi .help = "File name of a base image" 153875411d23SStefan Hajnoczi }, { 153975411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FMT, 154075411d23SStefan Hajnoczi .type = OPT_STRING, 154175411d23SStefan Hajnoczi .help = "Image format of the base image" 154275411d23SStefan Hajnoczi }, { 154375411d23SStefan Hajnoczi .name = BLOCK_OPT_CLUSTER_SIZE, 154475411d23SStefan Hajnoczi .type = OPT_SIZE, 154599cce9faSKevin Wolf .help = "Cluster size (in bytes)", 154699cce9faSKevin Wolf .value = { .n = QED_DEFAULT_CLUSTER_SIZE }, 154775411d23SStefan Hajnoczi }, { 154875411d23SStefan Hajnoczi .name = BLOCK_OPT_TABLE_SIZE, 154975411d23SStefan Hajnoczi .type = OPT_SIZE, 155075411d23SStefan Hajnoczi .help = "L1/L2 table size (in clusters)" 155175411d23SStefan Hajnoczi }, 155275411d23SStefan Hajnoczi { /* end of list */ } 155375411d23SStefan Hajnoczi }; 155475411d23SStefan Hajnoczi 155575411d23SStefan Hajnoczi static BlockDriver bdrv_qed = { 155675411d23SStefan Hajnoczi .format_name = "qed", 155775411d23SStefan Hajnoczi .instance_size = sizeof(BDRVQEDState), 155875411d23SStefan Hajnoczi .create_options = qed_create_options, 155975411d23SStefan Hajnoczi 156075411d23SStefan Hajnoczi .bdrv_probe = bdrv_qed_probe, 156175411d23SStefan Hajnoczi .bdrv_open = bdrv_qed_open, 156275411d23SStefan Hajnoczi .bdrv_close = bdrv_qed_close, 156375411d23SStefan Hajnoczi .bdrv_create = bdrv_qed_create, 1564b7d5a5b8SStefan Hajnoczi .bdrv_co_is_allocated = bdrv_qed_co_is_allocated, 156575411d23SStefan Hajnoczi .bdrv_make_empty = bdrv_qed_make_empty, 156675411d23SStefan Hajnoczi .bdrv_aio_readv = bdrv_qed_aio_readv, 156775411d23SStefan Hajnoczi .bdrv_aio_writev = bdrv_qed_aio_writev, 15680e71be19SStefan Hajnoczi .bdrv_co_write_zeroes = bdrv_qed_co_write_zeroes, 156975411d23SStefan Hajnoczi .bdrv_truncate = bdrv_qed_truncate, 157075411d23SStefan Hajnoczi .bdrv_getlength = bdrv_qed_getlength, 157175411d23SStefan Hajnoczi .bdrv_get_info = bdrv_qed_get_info, 157275411d23SStefan Hajnoczi .bdrv_change_backing_file = bdrv_qed_change_backing_file, 1573*c82954e5SBenoît Canet .bdrv_invalidate_cache = bdrv_qed_invalidate_cache, 157475411d23SStefan Hajnoczi .bdrv_check = bdrv_qed_check, 157575411d23SStefan Hajnoczi }; 157675411d23SStefan Hajnoczi 157775411d23SStefan Hajnoczi static void bdrv_qed_init(void) 157875411d23SStefan Hajnoczi { 157975411d23SStefan Hajnoczi bdrv_register(&bdrv_qed); 158075411d23SStefan Hajnoczi } 158175411d23SStefan Hajnoczi 158275411d23SStefan Hajnoczi block_init(bdrv_qed_init); 1583