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 BlockDriverAIOCB *acb; 12701979a98SStefan Hajnoczi 12801979a98SStefan Hajnoczi if (ret) { 12901979a98SStefan Hajnoczi qed_write_header_cb(write_header_cb, ret); 13001979a98SStefan Hajnoczi return; 13101979a98SStefan Hajnoczi } 13201979a98SStefan Hajnoczi 13301979a98SStefan Hajnoczi /* Update header */ 13401979a98SStefan Hajnoczi qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf); 13501979a98SStefan Hajnoczi 13601979a98SStefan Hajnoczi acb = bdrv_aio_writev(s->bs->file, 0, &write_header_cb->qiov, 13701979a98SStefan Hajnoczi write_header_cb->nsectors, qed_write_header_cb, 13801979a98SStefan Hajnoczi write_header_cb); 13901979a98SStefan Hajnoczi if (!acb) { 14001979a98SStefan Hajnoczi qed_write_header_cb(write_header_cb, -EIO); 14101979a98SStefan Hajnoczi } 14201979a98SStefan Hajnoczi } 14301979a98SStefan Hajnoczi 14401979a98SStefan Hajnoczi /** 14501979a98SStefan Hajnoczi * Update header in-place (does not rewrite backing filename or other strings) 14601979a98SStefan Hajnoczi * 14701979a98SStefan Hajnoczi * This function only updates known header fields in-place and does not affect 14801979a98SStefan Hajnoczi * extra data after the QED header. 14901979a98SStefan Hajnoczi */ 15001979a98SStefan Hajnoczi static void qed_write_header(BDRVQEDState *s, BlockDriverCompletionFunc cb, 15101979a98SStefan Hajnoczi void *opaque) 15201979a98SStefan Hajnoczi { 15301979a98SStefan Hajnoczi /* We must write full sectors for O_DIRECT but cannot necessarily generate 15401979a98SStefan Hajnoczi * the data following the header if an unrecognized compat feature is 15501979a98SStefan Hajnoczi * active. Therefore, first read the sectors containing the header, update 15601979a98SStefan Hajnoczi * them, and write back. 15701979a98SStefan Hajnoczi */ 15801979a98SStefan Hajnoczi 15901979a98SStefan Hajnoczi BlockDriverAIOCB *acb; 16001979a98SStefan Hajnoczi int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) / 16101979a98SStefan Hajnoczi BDRV_SECTOR_SIZE; 16201979a98SStefan Hajnoczi size_t len = nsectors * BDRV_SECTOR_SIZE; 16301979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb), 16401979a98SStefan Hajnoczi cb, opaque); 16501979a98SStefan Hajnoczi 16601979a98SStefan Hajnoczi write_header_cb->s = s; 16701979a98SStefan Hajnoczi write_header_cb->nsectors = nsectors; 16801979a98SStefan Hajnoczi write_header_cb->buf = qemu_blockalign(s->bs, len); 16901979a98SStefan Hajnoczi write_header_cb->iov.iov_base = write_header_cb->buf; 17001979a98SStefan Hajnoczi write_header_cb->iov.iov_len = len; 17101979a98SStefan Hajnoczi qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1); 17201979a98SStefan Hajnoczi 17301979a98SStefan Hajnoczi acb = bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors, 17401979a98SStefan Hajnoczi qed_write_header_read_cb, write_header_cb); 17501979a98SStefan Hajnoczi if (!acb) { 17601979a98SStefan Hajnoczi qed_write_header_cb(write_header_cb, -EIO); 17701979a98SStefan Hajnoczi } 17801979a98SStefan Hajnoczi } 17901979a98SStefan Hajnoczi 18075411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 18175411d23SStefan Hajnoczi { 18275411d23SStefan Hajnoczi uint64_t table_entries; 18375411d23SStefan Hajnoczi uint64_t l2_size; 18475411d23SStefan Hajnoczi 18575411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t); 18675411d23SStefan Hajnoczi l2_size = table_entries * cluster_size; 18775411d23SStefan Hajnoczi 18875411d23SStefan Hajnoczi return l2_size * table_entries; 18975411d23SStefan Hajnoczi } 19075411d23SStefan Hajnoczi 19175411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size) 19275411d23SStefan Hajnoczi { 19375411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE || 19475411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) { 19575411d23SStefan Hajnoczi return false; 19675411d23SStefan Hajnoczi } 19775411d23SStefan Hajnoczi if (cluster_size & (cluster_size - 1)) { 19875411d23SStefan Hajnoczi return false; /* not power of 2 */ 19975411d23SStefan Hajnoczi } 20075411d23SStefan Hajnoczi return true; 20175411d23SStefan Hajnoczi } 20275411d23SStefan Hajnoczi 20375411d23SStefan Hajnoczi static bool qed_is_table_size_valid(uint32_t table_size) 20475411d23SStefan Hajnoczi { 20575411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE || 20675411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) { 20775411d23SStefan Hajnoczi return false; 20875411d23SStefan Hajnoczi } 20975411d23SStefan Hajnoczi if (table_size & (table_size - 1)) { 21075411d23SStefan Hajnoczi return false; /* not power of 2 */ 21175411d23SStefan Hajnoczi } 21275411d23SStefan Hajnoczi return true; 21375411d23SStefan Hajnoczi } 21475411d23SStefan Hajnoczi 21575411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 21675411d23SStefan Hajnoczi uint32_t table_size) 21775411d23SStefan Hajnoczi { 21875411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) { 21975411d23SStefan Hajnoczi return false; /* not multiple of sector size */ 22075411d23SStefan Hajnoczi } 22175411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) { 22275411d23SStefan Hajnoczi return false; /* image is too large */ 22375411d23SStefan Hajnoczi } 22475411d23SStefan Hajnoczi return true; 22575411d23SStefan Hajnoczi } 22675411d23SStefan Hajnoczi 22775411d23SStefan Hajnoczi /** 22875411d23SStefan Hajnoczi * Read a string of known length from the image file 22975411d23SStefan Hajnoczi * 23075411d23SStefan Hajnoczi * @file: Image file 23175411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes 23275411d23SStefan Hajnoczi * @n: String length in bytes 23375411d23SStefan Hajnoczi * @buf: Destination buffer 23475411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes 23575411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure 23675411d23SStefan Hajnoczi * 23775411d23SStefan Hajnoczi * The string is NUL-terminated. 23875411d23SStefan Hajnoczi */ 23975411d23SStefan Hajnoczi static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n, 24075411d23SStefan Hajnoczi char *buf, size_t buflen) 24175411d23SStefan Hajnoczi { 24275411d23SStefan Hajnoczi int ret; 24375411d23SStefan Hajnoczi if (n >= buflen) { 24475411d23SStefan Hajnoczi return -EINVAL; 24575411d23SStefan Hajnoczi } 24675411d23SStefan Hajnoczi ret = bdrv_pread(file, offset, buf, n); 24775411d23SStefan Hajnoczi if (ret < 0) { 24875411d23SStefan Hajnoczi return ret; 24975411d23SStefan Hajnoczi } 25075411d23SStefan Hajnoczi buf[n] = '\0'; 25175411d23SStefan Hajnoczi return 0; 25275411d23SStefan Hajnoczi } 25375411d23SStefan Hajnoczi 254eabba580SStefan Hajnoczi /** 255eabba580SStefan Hajnoczi * Allocate new clusters 256eabba580SStefan Hajnoczi * 257eabba580SStefan Hajnoczi * @s: QED state 258eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate 259eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster 260eabba580SStefan Hajnoczi * 261eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be 262eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image 263eabba580SStefan Hajnoczi * file. 264eabba580SStefan Hajnoczi */ 265eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 266eabba580SStefan Hajnoczi { 267eabba580SStefan Hajnoczi uint64_t offset = s->file_size; 268eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size; 269eabba580SStefan Hajnoczi return offset; 270eabba580SStefan Hajnoczi } 271eabba580SStefan Hajnoczi 272298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s) 273298800caSStefan Hajnoczi { 274298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */ 275298800caSStefan Hajnoczi return qemu_blockalign(s->bs, 276298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size); 277298800caSStefan Hajnoczi } 278298800caSStefan Hajnoczi 279eabba580SStefan Hajnoczi /** 280eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table 281eabba580SStefan Hajnoczi */ 282eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 283eabba580SStefan Hajnoczi { 284eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 285eabba580SStefan Hajnoczi 286eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s); 287eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 288eabba580SStefan Hajnoczi 289eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0, 290eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size); 291eabba580SStefan Hajnoczi return l2_table; 292eabba580SStefan Hajnoczi } 293eabba580SStefan Hajnoczi 294eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret); 295eabba580SStefan Hajnoczi 2966f321e93SStefan Hajnoczi static void qed_plug_allocating_write_reqs(BDRVQEDState *s) 2976f321e93SStefan Hajnoczi { 2986f321e93SStefan Hajnoczi assert(!s->allocating_write_reqs_plugged); 2996f321e93SStefan Hajnoczi 3006f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = true; 3016f321e93SStefan Hajnoczi } 3026f321e93SStefan Hajnoczi 3036f321e93SStefan Hajnoczi static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) 3046f321e93SStefan Hajnoczi { 3056f321e93SStefan Hajnoczi QEDAIOCB *acb; 3066f321e93SStefan Hajnoczi 3076f321e93SStefan Hajnoczi assert(s->allocating_write_reqs_plugged); 3086f321e93SStefan Hajnoczi 3096f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = false; 3106f321e93SStefan Hajnoczi 3116f321e93SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 3126f321e93SStefan Hajnoczi if (acb) { 3136f321e93SStefan Hajnoczi qed_aio_next_io(acb, 0); 3146f321e93SStefan Hajnoczi } 3156f321e93SStefan Hajnoczi } 3166f321e93SStefan Hajnoczi 3176f321e93SStefan Hajnoczi static void qed_finish_clear_need_check(void *opaque, int ret) 3186f321e93SStefan Hajnoczi { 3196f321e93SStefan Hajnoczi /* Do nothing */ 3206f321e93SStefan Hajnoczi } 3216f321e93SStefan Hajnoczi 3226f321e93SStefan Hajnoczi static void qed_flush_after_clear_need_check(void *opaque, int ret) 3236f321e93SStefan Hajnoczi { 3246f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3256f321e93SStefan Hajnoczi 3266f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s); 3276f321e93SStefan Hajnoczi 3286f321e93SStefan Hajnoczi /* No need to wait until flush completes */ 3296f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3306f321e93SStefan Hajnoczi } 3316f321e93SStefan Hajnoczi 3326f321e93SStefan Hajnoczi static void qed_clear_need_check(void *opaque, int ret) 3336f321e93SStefan Hajnoczi { 3346f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3356f321e93SStefan Hajnoczi 3366f321e93SStefan Hajnoczi if (ret) { 3376f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3386f321e93SStefan Hajnoczi return; 3396f321e93SStefan Hajnoczi } 3406f321e93SStefan Hajnoczi 3416f321e93SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 3426f321e93SStefan Hajnoczi qed_write_header(s, qed_flush_after_clear_need_check, s); 3436f321e93SStefan Hajnoczi } 3446f321e93SStefan Hajnoczi 3456f321e93SStefan Hajnoczi static void qed_need_check_timer_cb(void *opaque) 3466f321e93SStefan Hajnoczi { 3476f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3486f321e93SStefan Hajnoczi 3496f321e93SStefan Hajnoczi /* The timer should only fire when allocating writes have drained */ 3506f321e93SStefan Hajnoczi assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs)); 3516f321e93SStefan Hajnoczi 3526f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s); 3536f321e93SStefan Hajnoczi 3546f321e93SStefan Hajnoczi qed_plug_allocating_write_reqs(s); 3556f321e93SStefan Hajnoczi 3566f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */ 3576f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_clear_need_check, s); 3586f321e93SStefan Hajnoczi } 3596f321e93SStefan Hajnoczi 3606f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s) 3616f321e93SStefan Hajnoczi { 3626f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s); 3636f321e93SStefan Hajnoczi 3646f321e93SStefan Hajnoczi /* Use vm_clock so we don't alter the image file while suspended for 3656f321e93SStefan Hajnoczi * migration. 3666f321e93SStefan Hajnoczi */ 3676f321e93SStefan Hajnoczi qemu_mod_timer(s->need_check_timer, qemu_get_clock_ns(vm_clock) + 3686f321e93SStefan Hajnoczi get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT); 3696f321e93SStefan Hajnoczi } 3706f321e93SStefan Hajnoczi 3716f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */ 3726f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s) 3736f321e93SStefan Hajnoczi { 3746f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s); 3756f321e93SStefan Hajnoczi qemu_del_timer(s->need_check_timer); 3766f321e93SStefan Hajnoczi } 3776f321e93SStefan Hajnoczi 37875411d23SStefan Hajnoczi static int bdrv_qed_open(BlockDriverState *bs, int flags) 37975411d23SStefan Hajnoczi { 38075411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 38175411d23SStefan Hajnoczi QEDHeader le_header; 38275411d23SStefan Hajnoczi int64_t file_size; 38375411d23SStefan Hajnoczi int ret; 38475411d23SStefan Hajnoczi 38575411d23SStefan Hajnoczi s->bs = bs; 386eabba580SStefan Hajnoczi QSIMPLEQ_INIT(&s->allocating_write_reqs); 38775411d23SStefan Hajnoczi 38875411d23SStefan Hajnoczi ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); 38975411d23SStefan Hajnoczi if (ret < 0) { 39075411d23SStefan Hajnoczi return ret; 39175411d23SStefan Hajnoczi } 39275411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header); 39375411d23SStefan Hajnoczi 39475411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) { 39575411d23SStefan Hajnoczi return -EINVAL; 39675411d23SStefan Hajnoczi } 39775411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) { 39810b758e8SKevin Wolf /* image uses unsupported feature bits */ 39910b758e8SKevin Wolf char buf[64]; 40010b758e8SKevin Wolf snprintf(buf, sizeof(buf), "%" PRIx64, 40110b758e8SKevin Wolf s->header.features & ~QED_FEATURE_MASK); 40210b758e8SKevin Wolf qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, 40310b758e8SKevin Wolf bs->device_name, "QED", buf); 40410b758e8SKevin Wolf return -ENOTSUP; 40575411d23SStefan Hajnoczi } 40675411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 40775411d23SStefan Hajnoczi return -EINVAL; 40875411d23SStefan Hajnoczi } 40975411d23SStefan Hajnoczi 41075411d23SStefan Hajnoczi /* Round down file size to the last cluster */ 41175411d23SStefan Hajnoczi file_size = bdrv_getlength(bs->file); 41275411d23SStefan Hajnoczi if (file_size < 0) { 41375411d23SStefan Hajnoczi return file_size; 41475411d23SStefan Hajnoczi } 41575411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size); 41675411d23SStefan Hajnoczi 41775411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) { 41875411d23SStefan Hajnoczi return -EINVAL; 41975411d23SStefan Hajnoczi } 42075411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size, 42175411d23SStefan Hajnoczi s->header.cluster_size, 42275411d23SStefan Hajnoczi s->header.table_size)) { 42375411d23SStefan Hajnoczi return -EINVAL; 42475411d23SStefan Hajnoczi } 42575411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 42675411d23SStefan Hajnoczi return -EINVAL; 42775411d23SStefan Hajnoczi } 42875411d23SStefan Hajnoczi 42975411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) / 43075411d23SStefan Hajnoczi sizeof(uint64_t); 43175411d23SStefan Hajnoczi s->l2_shift = ffs(s->header.cluster_size) - 1; 43275411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1; 43375411d23SStefan Hajnoczi s->l1_shift = s->l2_shift + ffs(s->table_nelems) - 1; 43475411d23SStefan Hajnoczi 43575411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) { 43675411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset + 43775411d23SStefan Hajnoczi s->header.backing_filename_size > 43875411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) { 43975411d23SStefan Hajnoczi return -EINVAL; 44075411d23SStefan Hajnoczi } 44175411d23SStefan Hajnoczi 44275411d23SStefan Hajnoczi ret = qed_read_string(bs->file, s->header.backing_filename_offset, 44375411d23SStefan Hajnoczi s->header.backing_filename_size, bs->backing_file, 44475411d23SStefan Hajnoczi sizeof(bs->backing_file)); 44575411d23SStefan Hajnoczi if (ret < 0) { 44675411d23SStefan Hajnoczi return ret; 44775411d23SStefan Hajnoczi } 44875411d23SStefan Hajnoczi 44975411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 45075411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 45175411d23SStefan Hajnoczi } 45275411d23SStefan Hajnoczi } 45375411d23SStefan Hajnoczi 45475411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards 45575411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older 45675411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is 45775411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear 45875411d23SStefan Hajnoczi * feature is no longer valid. 45975411d23SStefan Hajnoczi */ 46075411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 46175411d23SStefan Hajnoczi !bdrv_is_read_only(bs->file)) { 46275411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 46375411d23SStefan Hajnoczi 46475411d23SStefan Hajnoczi ret = qed_write_header_sync(s); 46575411d23SStefan Hajnoczi if (ret) { 46675411d23SStefan Hajnoczi return ret; 46775411d23SStefan Hajnoczi } 46875411d23SStefan Hajnoczi 46975411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */ 47075411d23SStefan Hajnoczi bdrv_flush(bs->file); 47175411d23SStefan Hajnoczi } 47275411d23SStefan Hajnoczi 473298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s); 474298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache); 475298800caSStefan Hajnoczi 476298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s); 477298800caSStefan Hajnoczi if (ret) { 47801979a98SStefan Hajnoczi goto out; 47901979a98SStefan Hajnoczi } 48001979a98SStefan Hajnoczi 48101979a98SStefan Hajnoczi /* If image was not closed cleanly, check consistency */ 48201979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 48301979a98SStefan Hajnoczi /* Read-only images cannot be fixed. There is no risk of corruption 48401979a98SStefan Hajnoczi * since write operations are not possible. Therefore, allow 48501979a98SStefan Hajnoczi * potentially inconsistent images to be opened read-only. This can 48601979a98SStefan Hajnoczi * aid data recovery from an otherwise inconsistent image. 48701979a98SStefan Hajnoczi */ 48801979a98SStefan Hajnoczi if (!bdrv_is_read_only(bs->file)) { 48901979a98SStefan Hajnoczi BdrvCheckResult result = {0}; 49001979a98SStefan Hajnoczi 49101979a98SStefan Hajnoczi ret = qed_check(s, &result, true); 4926f321e93SStefan Hajnoczi if (ret) { 4936f321e93SStefan Hajnoczi goto out; 4946f321e93SStefan Hajnoczi } 4956f321e93SStefan Hajnoczi if (!result.corruptions && !result.check_errors) { 49601979a98SStefan Hajnoczi /* Ensure fixes reach storage before clearing check bit */ 49701979a98SStefan Hajnoczi bdrv_flush(s->bs); 49801979a98SStefan Hajnoczi 49901979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 50001979a98SStefan Hajnoczi qed_write_header_sync(s); 50101979a98SStefan Hajnoczi } 50201979a98SStefan Hajnoczi } 50301979a98SStefan Hajnoczi } 50401979a98SStefan Hajnoczi 5056f321e93SStefan Hajnoczi s->need_check_timer = qemu_new_timer_ns(vm_clock, 5066f321e93SStefan Hajnoczi qed_need_check_timer_cb, s); 5076f321e93SStefan Hajnoczi 5081ed520c6SAnthony Liguori error_set(&s->migration_blocker, 5091ed520c6SAnthony Liguori QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, 5101ed520c6SAnthony Liguori "qed", bs->device_name, "live migration"); 5111ed520c6SAnthony Liguori migrate_add_blocker(s->migration_blocker); 5121ed520c6SAnthony Liguori 5131ed520c6SAnthony Liguori 51401979a98SStefan Hajnoczi out: 51501979a98SStefan Hajnoczi if (ret) { 516298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 517298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 518298800caSStefan Hajnoczi } 51975411d23SStefan Hajnoczi return ret; 52075411d23SStefan Hajnoczi } 52175411d23SStefan Hajnoczi 52275411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 52375411d23SStefan Hajnoczi { 524298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 525298800caSStefan Hajnoczi 5261ed520c6SAnthony Liguori migrate_del_blocker(s->migration_blocker); 5271ed520c6SAnthony Liguori error_free(s->migration_blocker); 5281ed520c6SAnthony Liguori 5296f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 5306f321e93SStefan Hajnoczi qemu_free_timer(s->need_check_timer); 5316f321e93SStefan Hajnoczi 53201979a98SStefan Hajnoczi /* Ensure writes reach stable storage */ 53301979a98SStefan Hajnoczi bdrv_flush(bs->file); 53401979a98SStefan Hajnoczi 53501979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */ 53601979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 53701979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 53801979a98SStefan Hajnoczi qed_write_header_sync(s); 53901979a98SStefan Hajnoczi } 54001979a98SStefan Hajnoczi 541298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 542298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 54375411d23SStefan Hajnoczi } 54475411d23SStefan Hajnoczi 54575411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size, 54675411d23SStefan Hajnoczi uint64_t image_size, uint32_t table_size, 54775411d23SStefan Hajnoczi const char *backing_file, const char *backing_fmt) 54875411d23SStefan Hajnoczi { 54975411d23SStefan Hajnoczi QEDHeader header = { 55075411d23SStefan Hajnoczi .magic = QED_MAGIC, 55175411d23SStefan Hajnoczi .cluster_size = cluster_size, 55275411d23SStefan Hajnoczi .table_size = table_size, 55375411d23SStefan Hajnoczi .header_size = 1, 55475411d23SStefan Hajnoczi .features = 0, 55575411d23SStefan Hajnoczi .compat_features = 0, 55675411d23SStefan Hajnoczi .l1_table_offset = cluster_size, 55775411d23SStefan Hajnoczi .image_size = image_size, 55875411d23SStefan Hajnoczi }; 55975411d23SStefan Hajnoczi QEDHeader le_header; 56075411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 56175411d23SStefan Hajnoczi size_t l1_size = header.cluster_size * header.table_size; 56275411d23SStefan Hajnoczi int ret = 0; 56375411d23SStefan Hajnoczi BlockDriverState *bs = NULL; 56475411d23SStefan Hajnoczi 56575411d23SStefan Hajnoczi ret = bdrv_create_file(filename, NULL); 56675411d23SStefan Hajnoczi if (ret < 0) { 56775411d23SStefan Hajnoczi return ret; 56875411d23SStefan Hajnoczi } 56975411d23SStefan Hajnoczi 57075411d23SStefan Hajnoczi ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR | BDRV_O_CACHE_WB); 57175411d23SStefan Hajnoczi if (ret < 0) { 57275411d23SStefan Hajnoczi return ret; 57375411d23SStefan Hajnoczi } 57475411d23SStefan Hajnoczi 575c743849bSStefan Hajnoczi /* File must start empty and grow, check truncate is supported */ 576c743849bSStefan Hajnoczi ret = bdrv_truncate(bs, 0); 577c743849bSStefan Hajnoczi if (ret < 0) { 578c743849bSStefan Hajnoczi goto out; 579c743849bSStefan Hajnoczi } 580c743849bSStefan Hajnoczi 58175411d23SStefan Hajnoczi if (backing_file) { 58275411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 58375411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 58475411d23SStefan Hajnoczi header.backing_filename_size = strlen(backing_file); 58575411d23SStefan Hajnoczi 58675411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 58775411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 58875411d23SStefan Hajnoczi } 58975411d23SStefan Hajnoczi } 59075411d23SStefan Hajnoczi 59175411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 59275411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header)); 59375411d23SStefan Hajnoczi if (ret < 0) { 59475411d23SStefan Hajnoczi goto out; 59575411d23SStefan Hajnoczi } 59675411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, sizeof(le_header), backing_file, 59775411d23SStefan Hajnoczi header.backing_filename_size); 59875411d23SStefan Hajnoczi if (ret < 0) { 59975411d23SStefan Hajnoczi goto out; 60075411d23SStefan Hajnoczi } 60175411d23SStefan Hajnoczi 6027267c094SAnthony Liguori l1_table = g_malloc0(l1_size); 60375411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size); 60475411d23SStefan Hajnoczi if (ret < 0) { 60575411d23SStefan Hajnoczi goto out; 60675411d23SStefan Hajnoczi } 60775411d23SStefan Hajnoczi 60875411d23SStefan Hajnoczi ret = 0; /* success */ 60975411d23SStefan Hajnoczi out: 6107267c094SAnthony Liguori g_free(l1_table); 61175411d23SStefan Hajnoczi bdrv_delete(bs); 61275411d23SStefan Hajnoczi return ret; 61375411d23SStefan Hajnoczi } 61475411d23SStefan Hajnoczi 61575411d23SStefan Hajnoczi static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options) 61675411d23SStefan Hajnoczi { 61775411d23SStefan Hajnoczi uint64_t image_size = 0; 61875411d23SStefan Hajnoczi uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 61975411d23SStefan Hajnoczi uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 62075411d23SStefan Hajnoczi const char *backing_file = NULL; 62175411d23SStefan Hajnoczi const char *backing_fmt = NULL; 62275411d23SStefan Hajnoczi 62375411d23SStefan Hajnoczi while (options && options->name) { 62475411d23SStefan Hajnoczi if (!strcmp(options->name, BLOCK_OPT_SIZE)) { 62575411d23SStefan Hajnoczi image_size = options->value.n; 62675411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { 62775411d23SStefan Hajnoczi backing_file = options->value.s; 62875411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { 62975411d23SStefan Hajnoczi backing_fmt = options->value.s; 63075411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { 63175411d23SStefan Hajnoczi if (options->value.n) { 63275411d23SStefan Hajnoczi cluster_size = options->value.n; 63375411d23SStefan Hajnoczi } 63475411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_TABLE_SIZE)) { 63575411d23SStefan Hajnoczi if (options->value.n) { 63675411d23SStefan Hajnoczi table_size = options->value.n; 63775411d23SStefan Hajnoczi } 63875411d23SStefan Hajnoczi } 63975411d23SStefan Hajnoczi options++; 64075411d23SStefan Hajnoczi } 64175411d23SStefan Hajnoczi 64275411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(cluster_size)) { 64375411d23SStefan Hajnoczi fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n", 64475411d23SStefan Hajnoczi QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 64575411d23SStefan Hajnoczi return -EINVAL; 64675411d23SStefan Hajnoczi } 64775411d23SStefan Hajnoczi if (!qed_is_table_size_valid(table_size)) { 64875411d23SStefan Hajnoczi fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n", 64975411d23SStefan Hajnoczi QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 65075411d23SStefan Hajnoczi return -EINVAL; 65175411d23SStefan Hajnoczi } 65275411d23SStefan Hajnoczi if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 65375411d23SStefan Hajnoczi fprintf(stderr, "QED image size must be a non-zero multiple of " 65475411d23SStefan Hajnoczi "cluster size and less than %" PRIu64 " bytes\n", 65575411d23SStefan Hajnoczi qed_max_image_size(cluster_size, table_size)); 65675411d23SStefan Hajnoczi return -EINVAL; 65775411d23SStefan Hajnoczi } 65875411d23SStefan Hajnoczi 65975411d23SStefan Hajnoczi return qed_create(filename, cluster_size, image_size, table_size, 66075411d23SStefan Hajnoczi backing_file, backing_fmt); 66175411d23SStefan Hajnoczi } 66275411d23SStefan Hajnoczi 663298800caSStefan Hajnoczi typedef struct { 664*b7d5a5b8SStefan Hajnoczi Coroutine *co; 665298800caSStefan Hajnoczi int is_allocated; 666298800caSStefan Hajnoczi int *pnum; 667298800caSStefan Hajnoczi } QEDIsAllocatedCB; 668298800caSStefan Hajnoczi 669298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 670298800caSStefan Hajnoczi { 671298800caSStefan Hajnoczi QEDIsAllocatedCB *cb = opaque; 672298800caSStefan Hajnoczi *cb->pnum = len / BDRV_SECTOR_SIZE; 67321df65b6SAnthony Liguori cb->is_allocated = (ret == QED_CLUSTER_FOUND || ret == QED_CLUSTER_ZERO); 674*b7d5a5b8SStefan Hajnoczi if (cb->co) { 675*b7d5a5b8SStefan Hajnoczi qemu_coroutine_enter(cb->co, NULL); 676*b7d5a5b8SStefan Hajnoczi } 677298800caSStefan Hajnoczi } 678298800caSStefan Hajnoczi 679*b7d5a5b8SStefan Hajnoczi static int coroutine_fn bdrv_qed_co_is_allocated(BlockDriverState *bs, 680*b7d5a5b8SStefan Hajnoczi int64_t sector_num, 68175411d23SStefan Hajnoczi int nb_sectors, int *pnum) 68275411d23SStefan Hajnoczi { 683298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 684298800caSStefan Hajnoczi uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 685298800caSStefan Hajnoczi size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 686298800caSStefan Hajnoczi QEDIsAllocatedCB cb = { 687298800caSStefan Hajnoczi .is_allocated = -1, 688298800caSStefan Hajnoczi .pnum = pnum, 689298800caSStefan Hajnoczi }; 690298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 691298800caSStefan Hajnoczi 692298800caSStefan Hajnoczi qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb); 693298800caSStefan Hajnoczi 694*b7d5a5b8SStefan Hajnoczi /* Now sleep if the callback wasn't invoked immediately */ 695298800caSStefan Hajnoczi while (cb.is_allocated == -1) { 696*b7d5a5b8SStefan Hajnoczi cb.co = qemu_coroutine_self(); 697*b7d5a5b8SStefan Hajnoczi qemu_coroutine_yield(); 698298800caSStefan Hajnoczi } 699298800caSStefan Hajnoczi 700298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 701298800caSStefan Hajnoczi 702298800caSStefan Hajnoczi return cb.is_allocated; 70375411d23SStefan Hajnoczi } 70475411d23SStefan Hajnoczi 70575411d23SStefan Hajnoczi static int bdrv_qed_make_empty(BlockDriverState *bs) 70675411d23SStefan Hajnoczi { 70775411d23SStefan Hajnoczi return -ENOTSUP; 70875411d23SStefan Hajnoczi } 70975411d23SStefan Hajnoczi 710eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 711eabba580SStefan Hajnoczi { 712eabba580SStefan Hajnoczi return acb->common.bs->opaque; 713eabba580SStefan Hajnoczi } 714eabba580SStefan Hajnoczi 715eabba580SStefan Hajnoczi /** 716eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 717eabba580SStefan Hajnoczi * 718eabba580SStefan Hajnoczi * @s: QED state 719eabba580SStefan Hajnoczi * @pos: Byte position in device 720eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 721eabba580SStefan Hajnoczi * @cb: Completion function 722eabba580SStefan Hajnoczi * @opaque: User data for completion function 723eabba580SStefan Hajnoczi * 724eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 725eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 726eabba580SStefan Hajnoczi */ 727eabba580SStefan Hajnoczi static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 728eabba580SStefan Hajnoczi QEMUIOVector *qiov, 729eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, void *opaque) 730eabba580SStefan Hajnoczi { 731eabba580SStefan Hajnoczi BlockDriverAIOCB *aiocb; 732eabba580SStefan Hajnoczi uint64_t backing_length = 0; 733eabba580SStefan Hajnoczi size_t size; 734eabba580SStefan Hajnoczi 735eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 736eabba580SStefan Hajnoczi * backing file like a zero length backing file. 737eabba580SStefan Hajnoczi */ 738eabba580SStefan Hajnoczi if (s->bs->backing_hd) { 739eabba580SStefan Hajnoczi int64_t l = bdrv_getlength(s->bs->backing_hd); 740eabba580SStefan Hajnoczi if (l < 0) { 741eabba580SStefan Hajnoczi cb(opaque, l); 742eabba580SStefan Hajnoczi return; 743eabba580SStefan Hajnoczi } 744eabba580SStefan Hajnoczi backing_length = l; 745eabba580SStefan Hajnoczi } 746eabba580SStefan Hajnoczi 747eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 748eabba580SStefan Hajnoczi if (pos >= backing_length || 749eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 750eabba580SStefan Hajnoczi qemu_iovec_memset(qiov, 0, qiov->size); 751eabba580SStefan Hajnoczi } 752eabba580SStefan Hajnoczi 753eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 754eabba580SStefan Hajnoczi if (pos >= backing_length) { 755eabba580SStefan Hajnoczi cb(opaque, 0); 756eabba580SStefan Hajnoczi return; 757eabba580SStefan Hajnoczi } 758eabba580SStefan Hajnoczi 759eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 760eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 761eabba580SStefan Hajnoczi 762eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING); 763eabba580SStefan Hajnoczi aiocb = bdrv_aio_readv(s->bs->backing_hd, pos / BDRV_SECTOR_SIZE, 764eabba580SStefan Hajnoczi qiov, size / BDRV_SECTOR_SIZE, cb, opaque); 765eabba580SStefan Hajnoczi if (!aiocb) { 766eabba580SStefan Hajnoczi cb(opaque, -EIO); 767eabba580SStefan Hajnoczi } 768eabba580SStefan Hajnoczi } 769eabba580SStefan Hajnoczi 770eabba580SStefan Hajnoczi typedef struct { 771eabba580SStefan Hajnoczi GenericCB gencb; 772eabba580SStefan Hajnoczi BDRVQEDState *s; 773eabba580SStefan Hajnoczi QEMUIOVector qiov; 774eabba580SStefan Hajnoczi struct iovec iov; 775eabba580SStefan Hajnoczi uint64_t offset; 776eabba580SStefan Hajnoczi } CopyFromBackingFileCB; 777eabba580SStefan Hajnoczi 778eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_cb(void *opaque, int ret) 779eabba580SStefan Hajnoczi { 780eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 781eabba580SStefan Hajnoczi qemu_vfree(copy_cb->iov.iov_base); 782eabba580SStefan Hajnoczi gencb_complete(©_cb->gencb, ret); 783eabba580SStefan Hajnoczi } 784eabba580SStefan Hajnoczi 785eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_write(void *opaque, int ret) 786eabba580SStefan Hajnoczi { 787eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 788eabba580SStefan Hajnoczi BDRVQEDState *s = copy_cb->s; 789eabba580SStefan Hajnoczi BlockDriverAIOCB *aiocb; 790eabba580SStefan Hajnoczi 791eabba580SStefan Hajnoczi if (ret) { 792eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb(copy_cb, ret); 793eabba580SStefan Hajnoczi return; 794eabba580SStefan Hajnoczi } 795eabba580SStefan Hajnoczi 796eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 797eabba580SStefan Hajnoczi aiocb = bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE, 798eabba580SStefan Hajnoczi ©_cb->qiov, 799eabba580SStefan Hajnoczi copy_cb->qiov.size / BDRV_SECTOR_SIZE, 800eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb, copy_cb); 801eabba580SStefan Hajnoczi if (!aiocb) { 802eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb(copy_cb, -EIO); 803eabba580SStefan Hajnoczi } 804eabba580SStefan Hajnoczi } 805eabba580SStefan Hajnoczi 806eabba580SStefan Hajnoczi /** 807eabba580SStefan Hajnoczi * Copy data from backing file into the image 808eabba580SStefan Hajnoczi * 809eabba580SStefan Hajnoczi * @s: QED state 810eabba580SStefan Hajnoczi * @pos: Byte position in device 811eabba580SStefan Hajnoczi * @len: Number of bytes 812eabba580SStefan Hajnoczi * @offset: Byte offset in image file 813eabba580SStefan Hajnoczi * @cb: Completion function 814eabba580SStefan Hajnoczi * @opaque: User data for completion function 815eabba580SStefan Hajnoczi */ 816eabba580SStefan Hajnoczi static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, 817eabba580SStefan Hajnoczi uint64_t len, uint64_t offset, 818eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, 819eabba580SStefan Hajnoczi void *opaque) 820eabba580SStefan Hajnoczi { 821eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb; 822eabba580SStefan Hajnoczi 823eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 824eabba580SStefan Hajnoczi if (len == 0) { 825eabba580SStefan Hajnoczi cb(opaque, 0); 826eabba580SStefan Hajnoczi return; 827eabba580SStefan Hajnoczi } 828eabba580SStefan Hajnoczi 829eabba580SStefan Hajnoczi copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque); 830eabba580SStefan Hajnoczi copy_cb->s = s; 831eabba580SStefan Hajnoczi copy_cb->offset = offset; 832eabba580SStefan Hajnoczi copy_cb->iov.iov_base = qemu_blockalign(s->bs, len); 833eabba580SStefan Hajnoczi copy_cb->iov.iov_len = len; 834eabba580SStefan Hajnoczi qemu_iovec_init_external(©_cb->qiov, ©_cb->iov, 1); 835eabba580SStefan Hajnoczi 836eabba580SStefan Hajnoczi qed_read_backing_file(s, pos, ©_cb->qiov, 837eabba580SStefan Hajnoczi qed_copy_from_backing_file_write, copy_cb); 838eabba580SStefan Hajnoczi } 839eabba580SStefan Hajnoczi 840eabba580SStefan Hajnoczi /** 841eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 842eabba580SStefan Hajnoczi * 843eabba580SStefan Hajnoczi * @s: QED state 844eabba580SStefan Hajnoczi * @table: L2 table 845eabba580SStefan Hajnoczi * @index: First cluster index 846eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 84721df65b6SAnthony Liguori * @cluster: First cluster offset 84821df65b6SAnthony Liguori * 84921df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the 85021df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker. 851eabba580SStefan Hajnoczi */ 852eabba580SStefan Hajnoczi static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index, 853eabba580SStefan Hajnoczi unsigned int n, uint64_t cluster) 854eabba580SStefan Hajnoczi { 855eabba580SStefan Hajnoczi int i; 856eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 857eabba580SStefan Hajnoczi table->offsets[i] = cluster; 85821df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) && 85921df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) { 860eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 861eabba580SStefan Hajnoczi } 862eabba580SStefan Hajnoczi } 86321df65b6SAnthony Liguori } 864eabba580SStefan Hajnoczi 865eabba580SStefan Hajnoczi static void qed_aio_complete_bh(void *opaque) 866eabba580SStefan Hajnoczi { 867eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 868eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb = acb->common.cb; 869eabba580SStefan Hajnoczi void *user_opaque = acb->common.opaque; 870eabba580SStefan Hajnoczi int ret = acb->bh_ret; 871eabba580SStefan Hajnoczi bool *finished = acb->finished; 872eabba580SStefan Hajnoczi 873eabba580SStefan Hajnoczi qemu_bh_delete(acb->bh); 874eabba580SStefan Hajnoczi qemu_aio_release(acb); 875eabba580SStefan Hajnoczi 876eabba580SStefan Hajnoczi /* Invoke callback */ 877eabba580SStefan Hajnoczi cb(user_opaque, ret); 878eabba580SStefan Hajnoczi 879eabba580SStefan Hajnoczi /* Signal cancel completion */ 880eabba580SStefan Hajnoczi if (finished) { 881eabba580SStefan Hajnoczi *finished = true; 882eabba580SStefan Hajnoczi } 883eabba580SStefan Hajnoczi } 884eabba580SStefan Hajnoczi 885eabba580SStefan Hajnoczi static void qed_aio_complete(QEDAIOCB *acb, int ret) 886eabba580SStefan Hajnoczi { 887eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 888eabba580SStefan Hajnoczi 889eabba580SStefan Hajnoczi trace_qed_aio_complete(s, acb, ret); 890eabba580SStefan Hajnoczi 891eabba580SStefan Hajnoczi /* Free resources */ 892eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov); 893eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 894eabba580SStefan Hajnoczi 895eabba580SStefan Hajnoczi /* Arrange for a bh to invoke the completion function */ 896eabba580SStefan Hajnoczi acb->bh_ret = ret; 897eabba580SStefan Hajnoczi acb->bh = qemu_bh_new(qed_aio_complete_bh, acb); 898eabba580SStefan Hajnoczi qemu_bh_schedule(acb->bh); 899eabba580SStefan Hajnoczi 900eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 901eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 902eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 903eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 904eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 905eabba580SStefan Hajnoczi */ 906eabba580SStefan Hajnoczi if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 907eabba580SStefan Hajnoczi QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next); 908eabba580SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 909eabba580SStefan Hajnoczi if (acb) { 910eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 9116f321e93SStefan Hajnoczi } else if (s->header.features & QED_F_NEED_CHECK) { 9126f321e93SStefan Hajnoczi qed_start_need_check_timer(s); 913eabba580SStefan Hajnoczi } 914eabba580SStefan Hajnoczi } 915eabba580SStefan Hajnoczi } 916eabba580SStefan Hajnoczi 917eabba580SStefan Hajnoczi /** 918eabba580SStefan Hajnoczi * Commit the current L2 table to the cache 919eabba580SStefan Hajnoczi */ 920eabba580SStefan Hajnoczi static void qed_commit_l2_update(void *opaque, int ret) 921eabba580SStefan Hajnoczi { 922eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 923eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 924eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 925e4fc8781SStefan Hajnoczi uint64_t l2_offset = l2_table->offset; 926eabba580SStefan Hajnoczi 927eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 928eabba580SStefan Hajnoczi 929eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 930eabba580SStefan Hajnoczi * cache. 931eabba580SStefan Hajnoczi */ 932e4fc8781SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 933eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 934eabba580SStefan Hajnoczi 935eabba580SStefan Hajnoczi qed_aio_next_io(opaque, ret); 936eabba580SStefan Hajnoczi } 937eabba580SStefan Hajnoczi 938eabba580SStefan Hajnoczi /** 939eabba580SStefan Hajnoczi * Update L1 table with new L2 table offset and write it out 940eabba580SStefan Hajnoczi */ 941eabba580SStefan Hajnoczi static void qed_aio_write_l1_update(void *opaque, int ret) 942eabba580SStefan Hajnoczi { 943eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 944eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 945eabba580SStefan Hajnoczi int index; 946eabba580SStefan Hajnoczi 947eabba580SStefan Hajnoczi if (ret) { 948eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 949eabba580SStefan Hajnoczi return; 950eabba580SStefan Hajnoczi } 951eabba580SStefan Hajnoczi 952eabba580SStefan Hajnoczi index = qed_l1_index(s, acb->cur_pos); 953eabba580SStefan Hajnoczi s->l1_table->offsets[index] = acb->request.l2_table->offset; 954eabba580SStefan Hajnoczi 955eabba580SStefan Hajnoczi qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb); 956eabba580SStefan Hajnoczi } 957eabba580SStefan Hajnoczi 958eabba580SStefan Hajnoczi /** 959eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 960eabba580SStefan Hajnoczi */ 961eabba580SStefan Hajnoczi static void qed_aio_write_l2_update(void *opaque, int ret) 962eabba580SStefan Hajnoczi { 963eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 964eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 965eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 966eabba580SStefan Hajnoczi int index; 967eabba580SStefan Hajnoczi 968eabba580SStefan Hajnoczi if (ret) { 969eabba580SStefan Hajnoczi goto err; 970eabba580SStefan Hajnoczi } 971eabba580SStefan Hajnoczi 972eabba580SStefan Hajnoczi if (need_alloc) { 973eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 974eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 975eabba580SStefan Hajnoczi } 976eabba580SStefan Hajnoczi 977eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 978eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 979eabba580SStefan Hajnoczi acb->cur_cluster); 980eabba580SStefan Hajnoczi 981eabba580SStefan Hajnoczi if (need_alloc) { 982eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 983eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true, 984eabba580SStefan Hajnoczi qed_aio_write_l1_update, acb); 985eabba580SStefan Hajnoczi } else { 986eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 987eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false, 988eabba580SStefan Hajnoczi qed_aio_next_io, acb); 989eabba580SStefan Hajnoczi } 990eabba580SStefan Hajnoczi return; 991eabba580SStefan Hajnoczi 992eabba580SStefan Hajnoczi err: 993eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 994eabba580SStefan Hajnoczi } 995eabba580SStefan Hajnoczi 996eabba580SStefan Hajnoczi /** 997eabba580SStefan Hajnoczi * Flush new data clusters before updating the L2 table 998eabba580SStefan Hajnoczi * 999eabba580SStefan Hajnoczi * This flush is necessary when a backing file is in use. A crash during an 1000eabba580SStefan Hajnoczi * allocating write could result in empty clusters in the image. If the write 1001eabba580SStefan Hajnoczi * only touched a subregion of the cluster, then backing image sectors have 1002eabba580SStefan Hajnoczi * been lost in the untouched region. The solution is to flush after writing a 1003eabba580SStefan Hajnoczi * new data cluster and before updating the L2 table. 1004eabba580SStefan Hajnoczi */ 1005eabba580SStefan Hajnoczi static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) 1006eabba580SStefan Hajnoczi { 1007eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1008eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1009eabba580SStefan Hajnoczi 1010eabba580SStefan Hajnoczi if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update, opaque)) { 1011eabba580SStefan Hajnoczi qed_aio_complete(acb, -EIO); 1012eabba580SStefan Hajnoczi } 1013eabba580SStefan Hajnoczi } 1014eabba580SStefan Hajnoczi 1015eabba580SStefan Hajnoczi /** 1016eabba580SStefan Hajnoczi * Write data to the image file 1017eabba580SStefan Hajnoczi */ 1018eabba580SStefan Hajnoczi static void qed_aio_write_main(void *opaque, int ret) 1019eabba580SStefan Hajnoczi { 1020eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1021eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1022eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1023eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 1024eabba580SStefan Hajnoczi BlockDriverCompletionFunc *next_fn; 1025eabba580SStefan Hajnoczi BlockDriverAIOCB *file_acb; 1026eabba580SStefan Hajnoczi 1027eabba580SStefan Hajnoczi trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size); 1028eabba580SStefan Hajnoczi 1029eabba580SStefan Hajnoczi if (ret) { 1030eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1031eabba580SStefan Hajnoczi return; 1032eabba580SStefan Hajnoczi } 1033eabba580SStefan Hajnoczi 1034eabba580SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_FOUND) { 1035eabba580SStefan Hajnoczi next_fn = qed_aio_next_io; 1036eabba580SStefan Hajnoczi } else { 1037eabba580SStefan Hajnoczi if (s->bs->backing_hd) { 1038eabba580SStefan Hajnoczi next_fn = qed_aio_write_flush_before_l2_update; 1039eabba580SStefan Hajnoczi } else { 1040eabba580SStefan Hajnoczi next_fn = qed_aio_write_l2_update; 1041eabba580SStefan Hajnoczi } 1042eabba580SStefan Hajnoczi } 1043eabba580SStefan Hajnoczi 1044eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 1045eabba580SStefan Hajnoczi file_acb = bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE, 1046eabba580SStefan Hajnoczi &acb->cur_qiov, 1047eabba580SStefan Hajnoczi acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1048eabba580SStefan Hajnoczi next_fn, acb); 1049eabba580SStefan Hajnoczi if (!file_acb) { 1050eabba580SStefan Hajnoczi qed_aio_complete(acb, -EIO); 1051eabba580SStefan Hajnoczi } 1052eabba580SStefan Hajnoczi } 1053eabba580SStefan Hajnoczi 1054eabba580SStefan Hajnoczi /** 1055eabba580SStefan Hajnoczi * Populate back untouched region of new data cluster 1056eabba580SStefan Hajnoczi */ 1057eabba580SStefan Hajnoczi static void qed_aio_write_postfill(void *opaque, int ret) 1058eabba580SStefan Hajnoczi { 1059eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1060eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1061eabba580SStefan Hajnoczi uint64_t start = acb->cur_pos + acb->cur_qiov.size; 1062eabba580SStefan Hajnoczi uint64_t len = 1063eabba580SStefan Hajnoczi qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1064eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1065eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + 1066eabba580SStefan Hajnoczi acb->cur_qiov.size; 1067eabba580SStefan Hajnoczi 1068eabba580SStefan Hajnoczi if (ret) { 1069eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1070eabba580SStefan Hajnoczi return; 1071eabba580SStefan Hajnoczi } 1072eabba580SStefan Hajnoczi 1073eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 1074eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, offset, 1075eabba580SStefan Hajnoczi qed_aio_write_main, acb); 1076eabba580SStefan Hajnoczi } 1077eabba580SStefan Hajnoczi 1078eabba580SStefan Hajnoczi /** 1079eabba580SStefan Hajnoczi * Populate front untouched region of new data cluster 1080eabba580SStefan Hajnoczi */ 1081eabba580SStefan Hajnoczi static void qed_aio_write_prefill(void *opaque, int ret) 1082eabba580SStefan Hajnoczi { 1083eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1084eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1085eabba580SStefan Hajnoczi uint64_t start = qed_start_of_cluster(s, acb->cur_pos); 1086eabba580SStefan Hajnoczi uint64_t len = qed_offset_into_cluster(s, acb->cur_pos); 1087eabba580SStefan Hajnoczi 1088eabba580SStefan Hajnoczi trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1089eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, acb->cur_cluster, 1090eabba580SStefan Hajnoczi qed_aio_write_postfill, acb); 1091eabba580SStefan Hajnoczi } 1092eabba580SStefan Hajnoczi 1093eabba580SStefan Hajnoczi /** 10940d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write 10950d09c797SStefan Hajnoczi */ 10960d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s) 10970d09c797SStefan Hajnoczi { 10980d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */ 10990d09c797SStefan Hajnoczi if (s->bs->backing_hd) { 11000d09c797SStefan Hajnoczi return false; 11010d09c797SStefan Hajnoczi } 11020d09c797SStefan Hajnoczi 11030d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK); 11040d09c797SStefan Hajnoczi } 11050d09c797SStefan Hajnoczi 11060d09c797SStefan Hajnoczi /** 1107eabba580SStefan Hajnoczi * Write new data cluster 1108eabba580SStefan Hajnoczi * 1109eabba580SStefan Hajnoczi * @acb: Write request 1110eabba580SStefan Hajnoczi * @len: Length in bytes 1111eabba580SStefan Hajnoczi * 1112eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters. 1113eabba580SStefan Hajnoczi */ 1114eabba580SStefan Hajnoczi static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1115eabba580SStefan Hajnoczi { 1116eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1117eabba580SStefan Hajnoczi 11186f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */ 11196f321e93SStefan Hajnoczi if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) { 11206f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 11216f321e93SStefan Hajnoczi } 11226f321e93SStefan Hajnoczi 1123eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 1124eabba580SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 1125eabba580SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next); 1126eabba580SStefan Hajnoczi } 11276f321e93SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) || 11286f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged) { 1129eabba580SStefan Hajnoczi return; /* wait for existing request to finish */ 1130eabba580SStefan Hajnoczi } 1131eabba580SStefan Hajnoczi 1132eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 1133eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 1134eabba580SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 1135eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1136eabba580SStefan Hajnoczi 11370d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) { 113801979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK; 113901979a98SStefan Hajnoczi qed_write_header(s, qed_aio_write_prefill, acb); 11400d09c797SStefan Hajnoczi } else { 11410d09c797SStefan Hajnoczi qed_aio_write_prefill(acb, 0); 11420d09c797SStefan Hajnoczi } 1143eabba580SStefan Hajnoczi } 1144eabba580SStefan Hajnoczi 1145eabba580SStefan Hajnoczi /** 1146eabba580SStefan Hajnoczi * Write data cluster in place 1147eabba580SStefan Hajnoczi * 1148eabba580SStefan Hajnoczi * @acb: Write request 1149eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1150eabba580SStefan Hajnoczi * @len: Length in bytes 1151eabba580SStefan Hajnoczi * 1152eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 1153eabba580SStefan Hajnoczi */ 1154eabba580SStefan Hajnoczi static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) 1155eabba580SStefan Hajnoczi { 1156eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 1157eabba580SStefan Hajnoczi acb->cur_cluster = offset; 1158eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1159eabba580SStefan Hajnoczi 1160eabba580SStefan Hajnoczi /* Do the actual write */ 1161eabba580SStefan Hajnoczi qed_aio_write_main(acb, 0); 1162eabba580SStefan Hajnoczi } 1163eabba580SStefan Hajnoczi 1164eabba580SStefan Hajnoczi /** 1165eabba580SStefan Hajnoczi * Write data cluster 1166eabba580SStefan Hajnoczi * 1167eabba580SStefan Hajnoczi * @opaque: Write request 1168eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1169eabba580SStefan Hajnoczi * or -errno 1170eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1171eabba580SStefan Hajnoczi * @len: Length in bytes 1172eabba580SStefan Hajnoczi * 1173eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1174eabba580SStefan Hajnoczi */ 1175eabba580SStefan Hajnoczi static void qed_aio_write_data(void *opaque, int ret, 1176eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1177eabba580SStefan Hajnoczi { 1178eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1179eabba580SStefan Hajnoczi 1180eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1181eabba580SStefan Hajnoczi 1182eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 1183eabba580SStefan Hajnoczi 1184eabba580SStefan Hajnoczi switch (ret) { 1185eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 1186eabba580SStefan Hajnoczi qed_aio_write_inplace(acb, offset, len); 1187eabba580SStefan Hajnoczi break; 1188eabba580SStefan Hajnoczi 1189eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 1190eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 119121df65b6SAnthony Liguori case QED_CLUSTER_ZERO: 1192eabba580SStefan Hajnoczi qed_aio_write_alloc(acb, len); 1193eabba580SStefan Hajnoczi break; 1194eabba580SStefan Hajnoczi 1195eabba580SStefan Hajnoczi default: 1196eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1197eabba580SStefan Hajnoczi break; 1198eabba580SStefan Hajnoczi } 1199eabba580SStefan Hajnoczi } 1200eabba580SStefan Hajnoczi 1201eabba580SStefan Hajnoczi /** 1202eabba580SStefan Hajnoczi * Read data cluster 1203eabba580SStefan Hajnoczi * 1204eabba580SStefan Hajnoczi * @opaque: Read request 1205eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1206eabba580SStefan Hajnoczi * or -errno 1207eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1208eabba580SStefan Hajnoczi * @len: Length in bytes 1209eabba580SStefan Hajnoczi * 1210eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1211eabba580SStefan Hajnoczi */ 1212eabba580SStefan Hajnoczi static void qed_aio_read_data(void *opaque, int ret, 1213eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1214eabba580SStefan Hajnoczi { 1215eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1216eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1217eabba580SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 1218eabba580SStefan Hajnoczi BlockDriverAIOCB *file_acb; 1219eabba580SStefan Hajnoczi 1220eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 1221eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 1222eabba580SStefan Hajnoczi 1223eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 1224eabba580SStefan Hajnoczi 1225eabba580SStefan Hajnoczi if (ret < 0) { 1226eabba580SStefan Hajnoczi goto err; 1227eabba580SStefan Hajnoczi } 1228eabba580SStefan Hajnoczi 1229eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1230eabba580SStefan Hajnoczi 123121df65b6SAnthony Liguori /* Handle zero cluster and backing file reads */ 123221df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) { 123321df65b6SAnthony Liguori qemu_iovec_memset(&acb->cur_qiov, 0, acb->cur_qiov.size); 123421df65b6SAnthony Liguori qed_aio_next_io(acb, 0); 123521df65b6SAnthony Liguori return; 123621df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) { 1237eabba580SStefan Hajnoczi qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1238eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1239eabba580SStefan Hajnoczi return; 1240eabba580SStefan Hajnoczi } 1241eabba580SStefan Hajnoczi 1242eabba580SStefan Hajnoczi BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 1243eabba580SStefan Hajnoczi file_acb = bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE, 1244eabba580SStefan Hajnoczi &acb->cur_qiov, 1245eabba580SStefan Hajnoczi acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1246eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1247eabba580SStefan Hajnoczi if (!file_acb) { 1248eabba580SStefan Hajnoczi ret = -EIO; 1249eabba580SStefan Hajnoczi goto err; 1250eabba580SStefan Hajnoczi } 1251eabba580SStefan Hajnoczi return; 1252eabba580SStefan Hajnoczi 1253eabba580SStefan Hajnoczi err: 1254eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1255eabba580SStefan Hajnoczi } 1256eabba580SStefan Hajnoczi 1257eabba580SStefan Hajnoczi /** 1258eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1259eabba580SStefan Hajnoczi */ 1260eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret) 1261eabba580SStefan Hajnoczi { 1262eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1263eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1264eabba580SStefan Hajnoczi QEDFindClusterFunc *io_fn = 1265eabba580SStefan Hajnoczi acb->is_write ? qed_aio_write_data : qed_aio_read_data; 1266eabba580SStefan Hajnoczi 1267eabba580SStefan Hajnoczi trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size); 1268eabba580SStefan Hajnoczi 1269eabba580SStefan Hajnoczi /* Handle I/O error */ 1270eabba580SStefan Hajnoczi if (ret) { 1271eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1272eabba580SStefan Hajnoczi return; 1273eabba580SStefan Hajnoczi } 1274eabba580SStefan Hajnoczi 1275eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1276eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1277eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1278eabba580SStefan Hajnoczi 1279eabba580SStefan Hajnoczi /* Complete request */ 1280eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 1281eabba580SStefan Hajnoczi qed_aio_complete(acb, 0); 1282eabba580SStefan Hajnoczi return; 1283eabba580SStefan Hajnoczi } 1284eabba580SStefan Hajnoczi 1285eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 1286eabba580SStefan Hajnoczi qed_find_cluster(s, &acb->request, 1287eabba580SStefan Hajnoczi acb->cur_pos, acb->end_pos - acb->cur_pos, 1288eabba580SStefan Hajnoczi io_fn, acb); 1289eabba580SStefan Hajnoczi } 1290eabba580SStefan Hajnoczi 1291eabba580SStefan Hajnoczi static BlockDriverAIOCB *qed_aio_setup(BlockDriverState *bs, 1292eabba580SStefan Hajnoczi int64_t sector_num, 1293eabba580SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1294eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, 1295eabba580SStefan Hajnoczi void *opaque, bool is_write) 1296eabba580SStefan Hajnoczi { 1297eabba580SStefan Hajnoczi QEDAIOCB *acb = qemu_aio_get(&qed_aio_pool, bs, cb, opaque); 1298eabba580SStefan Hajnoczi 1299eabba580SStefan Hajnoczi trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, 1300eabba580SStefan Hajnoczi opaque, is_write); 1301eabba580SStefan Hajnoczi 1302eabba580SStefan Hajnoczi acb->is_write = is_write; 1303eabba580SStefan Hajnoczi acb->finished = NULL; 1304eabba580SStefan Hajnoczi acb->qiov = qiov; 1305eabba580SStefan Hajnoczi acb->qiov_offset = 0; 1306eabba580SStefan Hajnoczi acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 1307eabba580SStefan Hajnoczi acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE; 1308eabba580SStefan Hajnoczi acb->request.l2_table = NULL; 1309eabba580SStefan Hajnoczi qemu_iovec_init(&acb->cur_qiov, qiov->niov); 1310eabba580SStefan Hajnoczi 1311eabba580SStefan Hajnoczi /* Start request */ 1312eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 1313eabba580SStefan Hajnoczi return &acb->common; 1314eabba580SStefan Hajnoczi } 1315eabba580SStefan Hajnoczi 131675411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs, 131775411d23SStefan Hajnoczi int64_t sector_num, 131875411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 131975411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 132075411d23SStefan Hajnoczi void *opaque) 132175411d23SStefan Hajnoczi { 1322eabba580SStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, false); 132375411d23SStefan Hajnoczi } 132475411d23SStefan Hajnoczi 132575411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs, 132675411d23SStefan Hajnoczi int64_t sector_num, 132775411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 132875411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 132975411d23SStefan Hajnoczi void *opaque) 133075411d23SStefan Hajnoczi { 1331eabba580SStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, true); 133275411d23SStefan Hajnoczi } 133375411d23SStefan Hajnoczi 133475411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_flush(BlockDriverState *bs, 133575411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 133675411d23SStefan Hajnoczi void *opaque) 133775411d23SStefan Hajnoczi { 133875411d23SStefan Hajnoczi return bdrv_aio_flush(bs->file, cb, opaque); 133975411d23SStefan Hajnoczi } 134075411d23SStefan Hajnoczi 134175411d23SStefan Hajnoczi static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset) 134275411d23SStefan Hajnoczi { 134377a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 134477a5a000SStefan Hajnoczi uint64_t old_image_size; 134577a5a000SStefan Hajnoczi int ret; 134677a5a000SStefan Hajnoczi 134777a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size, 134877a5a000SStefan Hajnoczi s->header.table_size)) { 134977a5a000SStefan Hajnoczi return -EINVAL; 135077a5a000SStefan Hajnoczi } 135177a5a000SStefan Hajnoczi 135277a5a000SStefan Hajnoczi /* Shrinking is currently not supported */ 135377a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) { 135475411d23SStefan Hajnoczi return -ENOTSUP; 135575411d23SStefan Hajnoczi } 135675411d23SStefan Hajnoczi 135777a5a000SStefan Hajnoczi old_image_size = s->header.image_size; 135877a5a000SStefan Hajnoczi s->header.image_size = offset; 135977a5a000SStefan Hajnoczi ret = qed_write_header_sync(s); 136077a5a000SStefan Hajnoczi if (ret < 0) { 136177a5a000SStefan Hajnoczi s->header.image_size = old_image_size; 136277a5a000SStefan Hajnoczi } 136377a5a000SStefan Hajnoczi return ret; 136477a5a000SStefan Hajnoczi } 136577a5a000SStefan Hajnoczi 136675411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs) 136775411d23SStefan Hajnoczi { 136875411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 136975411d23SStefan Hajnoczi return s->header.image_size; 137075411d23SStefan Hajnoczi } 137175411d23SStefan Hajnoczi 137275411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 137375411d23SStefan Hajnoczi { 137475411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 137575411d23SStefan Hajnoczi 137675411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi)); 137775411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size; 137875411d23SStefan Hajnoczi return 0; 137975411d23SStefan Hajnoczi } 138075411d23SStefan Hajnoczi 138175411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 138275411d23SStefan Hajnoczi const char *backing_file, 138375411d23SStefan Hajnoczi const char *backing_fmt) 138475411d23SStefan Hajnoczi { 138575411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 138675411d23SStefan Hajnoczi QEDHeader new_header, le_header; 138775411d23SStefan Hajnoczi void *buffer; 138875411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 138975411d23SStefan Hajnoczi int ret; 139075411d23SStefan Hajnoczi 139175411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 139275411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 139375411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 139475411d23SStefan Hajnoczi * add a new string. 139575411d23SStefan Hajnoczi */ 139675411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 139775411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 139875411d23SStefan Hajnoczi return -ENOTSUP; 139975411d23SStefan Hajnoczi } 140075411d23SStefan Hajnoczi 140175411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 140275411d23SStefan Hajnoczi 140375411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 140475411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 140575411d23SStefan Hajnoczi 140675411d23SStefan Hajnoczi /* Adjust feature flags */ 140775411d23SStefan Hajnoczi if (backing_file) { 140875411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 140975411d23SStefan Hajnoczi 141075411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 141175411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 141275411d23SStefan Hajnoczi } 141375411d23SStefan Hajnoczi } 141475411d23SStefan Hajnoczi 141575411d23SStefan Hajnoczi /* Calculate new header size */ 141675411d23SStefan Hajnoczi backing_file_len = 0; 141775411d23SStefan Hajnoczi 141875411d23SStefan Hajnoczi if (backing_file) { 141975411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 142075411d23SStefan Hajnoczi } 142175411d23SStefan Hajnoczi 142275411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 142375411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 142475411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 142575411d23SStefan Hajnoczi buffer_len += backing_file_len; 142675411d23SStefan Hajnoczi 142775411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 142875411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 142975411d23SStefan Hajnoczi return -ENOSPC; 143075411d23SStefan Hajnoczi } 143175411d23SStefan Hajnoczi 143275411d23SStefan Hajnoczi /* Prepare new header */ 14337267c094SAnthony Liguori buffer = g_malloc(buffer_len); 143475411d23SStefan Hajnoczi 143575411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 143675411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 143775411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 143875411d23SStefan Hajnoczi 1439feba23b1SPavel Borzenkov if (backing_file) { 144075411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 144175411d23SStefan Hajnoczi buffer_len += backing_file_len; 1442feba23b1SPavel Borzenkov } 144375411d23SStefan Hajnoczi 144475411d23SStefan Hajnoczi /* Write new header */ 144575411d23SStefan Hajnoczi ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); 14467267c094SAnthony Liguori g_free(buffer); 144775411d23SStefan Hajnoczi if (ret == 0) { 144875411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 144975411d23SStefan Hajnoczi } 145075411d23SStefan Hajnoczi return ret; 145175411d23SStefan Hajnoczi } 145275411d23SStefan Hajnoczi 145375411d23SStefan Hajnoczi static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result) 145475411d23SStefan Hajnoczi { 145501979a98SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 145601979a98SStefan Hajnoczi 145701979a98SStefan Hajnoczi return qed_check(s, result, false); 145875411d23SStefan Hajnoczi } 145975411d23SStefan Hajnoczi 146075411d23SStefan Hajnoczi static QEMUOptionParameter qed_create_options[] = { 146175411d23SStefan Hajnoczi { 146275411d23SStefan Hajnoczi .name = BLOCK_OPT_SIZE, 146375411d23SStefan Hajnoczi .type = OPT_SIZE, 146475411d23SStefan Hajnoczi .help = "Virtual disk size (in bytes)" 146575411d23SStefan Hajnoczi }, { 146675411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FILE, 146775411d23SStefan Hajnoczi .type = OPT_STRING, 146875411d23SStefan Hajnoczi .help = "File name of a base image" 146975411d23SStefan Hajnoczi }, { 147075411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FMT, 147175411d23SStefan Hajnoczi .type = OPT_STRING, 147275411d23SStefan Hajnoczi .help = "Image format of the base image" 147375411d23SStefan Hajnoczi }, { 147475411d23SStefan Hajnoczi .name = BLOCK_OPT_CLUSTER_SIZE, 147575411d23SStefan Hajnoczi .type = OPT_SIZE, 147699cce9faSKevin Wolf .help = "Cluster size (in bytes)", 147799cce9faSKevin Wolf .value = { .n = QED_DEFAULT_CLUSTER_SIZE }, 147875411d23SStefan Hajnoczi }, { 147975411d23SStefan Hajnoczi .name = BLOCK_OPT_TABLE_SIZE, 148075411d23SStefan Hajnoczi .type = OPT_SIZE, 148175411d23SStefan Hajnoczi .help = "L1/L2 table size (in clusters)" 148275411d23SStefan Hajnoczi }, 148375411d23SStefan Hajnoczi { /* end of list */ } 148475411d23SStefan Hajnoczi }; 148575411d23SStefan Hajnoczi 148675411d23SStefan Hajnoczi static BlockDriver bdrv_qed = { 148775411d23SStefan Hajnoczi .format_name = "qed", 148875411d23SStefan Hajnoczi .instance_size = sizeof(BDRVQEDState), 148975411d23SStefan Hajnoczi .create_options = qed_create_options, 149075411d23SStefan Hajnoczi 149175411d23SStefan Hajnoczi .bdrv_probe = bdrv_qed_probe, 149275411d23SStefan Hajnoczi .bdrv_open = bdrv_qed_open, 149375411d23SStefan Hajnoczi .bdrv_close = bdrv_qed_close, 149475411d23SStefan Hajnoczi .bdrv_create = bdrv_qed_create, 1495*b7d5a5b8SStefan Hajnoczi .bdrv_co_is_allocated = bdrv_qed_co_is_allocated, 149675411d23SStefan Hajnoczi .bdrv_make_empty = bdrv_qed_make_empty, 149775411d23SStefan Hajnoczi .bdrv_aio_readv = bdrv_qed_aio_readv, 149875411d23SStefan Hajnoczi .bdrv_aio_writev = bdrv_qed_aio_writev, 149975411d23SStefan Hajnoczi .bdrv_aio_flush = bdrv_qed_aio_flush, 150075411d23SStefan Hajnoczi .bdrv_truncate = bdrv_qed_truncate, 150175411d23SStefan Hajnoczi .bdrv_getlength = bdrv_qed_getlength, 150275411d23SStefan Hajnoczi .bdrv_get_info = bdrv_qed_get_info, 150375411d23SStefan Hajnoczi .bdrv_change_backing_file = bdrv_qed_change_backing_file, 150475411d23SStefan Hajnoczi .bdrv_check = bdrv_qed_check, 150575411d23SStefan Hajnoczi }; 150675411d23SStefan Hajnoczi 150775411d23SStefan Hajnoczi static void bdrv_qed_init(void) 150875411d23SStefan Hajnoczi { 150975411d23SStefan Hajnoczi bdrv_register(&bdrv_qed); 151075411d23SStefan Hajnoczi } 151175411d23SStefan Hajnoczi 151275411d23SStefan Hajnoczi block_init(bdrv_qed_init); 1513