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" 1975411d23SStefan Hajnoczi 20eabba580SStefan Hajnoczi static void qed_aio_cancel(BlockDriverAIOCB *blockacb) 21eabba580SStefan Hajnoczi { 22eabba580SStefan Hajnoczi QEDAIOCB *acb = (QEDAIOCB *)blockacb; 23eabba580SStefan Hajnoczi bool finished = false; 24eabba580SStefan Hajnoczi 25eabba580SStefan Hajnoczi /* Wait for the request to finish */ 26eabba580SStefan Hajnoczi acb->finished = &finished; 27eabba580SStefan Hajnoczi while (!finished) { 28eabba580SStefan Hajnoczi qemu_aio_wait(); 29eabba580SStefan Hajnoczi } 30eabba580SStefan Hajnoczi } 31eabba580SStefan Hajnoczi 32eabba580SStefan Hajnoczi static AIOPool qed_aio_pool = { 33eabba580SStefan Hajnoczi .aiocb_size = sizeof(QEDAIOCB), 34eabba580SStefan Hajnoczi .cancel = qed_aio_cancel, 35eabba580SStefan Hajnoczi }; 36eabba580SStefan Hajnoczi 3775411d23SStefan Hajnoczi static int bdrv_qed_probe(const uint8_t *buf, int buf_size, 3875411d23SStefan Hajnoczi const char *filename) 3975411d23SStefan Hajnoczi { 4075411d23SStefan Hajnoczi const QEDHeader *header = (const QEDHeader *)buf; 4175411d23SStefan Hajnoczi 4275411d23SStefan Hajnoczi if (buf_size < sizeof(*header)) { 4375411d23SStefan Hajnoczi return 0; 4475411d23SStefan Hajnoczi } 4575411d23SStefan Hajnoczi if (le32_to_cpu(header->magic) != QED_MAGIC) { 4675411d23SStefan Hajnoczi return 0; 4775411d23SStefan Hajnoczi } 4875411d23SStefan Hajnoczi return 100; 4975411d23SStefan Hajnoczi } 5075411d23SStefan Hajnoczi 5175411d23SStefan Hajnoczi /** 5275411d23SStefan Hajnoczi * Check whether an image format is raw 5375411d23SStefan Hajnoczi * 5475411d23SStefan Hajnoczi * @fmt: Backing file format, may be NULL 5575411d23SStefan Hajnoczi */ 5675411d23SStefan Hajnoczi static bool qed_fmt_is_raw(const char *fmt) 5775411d23SStefan Hajnoczi { 5875411d23SStefan Hajnoczi return fmt && strcmp(fmt, "raw") == 0; 5975411d23SStefan Hajnoczi } 6075411d23SStefan Hajnoczi 6175411d23SStefan Hajnoczi static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) 6275411d23SStefan Hajnoczi { 6375411d23SStefan Hajnoczi cpu->magic = le32_to_cpu(le->magic); 6475411d23SStefan Hajnoczi cpu->cluster_size = le32_to_cpu(le->cluster_size); 6575411d23SStefan Hajnoczi cpu->table_size = le32_to_cpu(le->table_size); 6675411d23SStefan Hajnoczi cpu->header_size = le32_to_cpu(le->header_size); 6775411d23SStefan Hajnoczi cpu->features = le64_to_cpu(le->features); 6875411d23SStefan Hajnoczi cpu->compat_features = le64_to_cpu(le->compat_features); 6975411d23SStefan Hajnoczi cpu->autoclear_features = le64_to_cpu(le->autoclear_features); 7075411d23SStefan Hajnoczi cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); 7175411d23SStefan Hajnoczi cpu->image_size = le64_to_cpu(le->image_size); 7275411d23SStefan Hajnoczi cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); 7375411d23SStefan Hajnoczi cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); 7475411d23SStefan Hajnoczi } 7575411d23SStefan Hajnoczi 7675411d23SStefan Hajnoczi static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) 7775411d23SStefan Hajnoczi { 7875411d23SStefan Hajnoczi le->magic = cpu_to_le32(cpu->magic); 7975411d23SStefan Hajnoczi le->cluster_size = cpu_to_le32(cpu->cluster_size); 8075411d23SStefan Hajnoczi le->table_size = cpu_to_le32(cpu->table_size); 8175411d23SStefan Hajnoczi le->header_size = cpu_to_le32(cpu->header_size); 8275411d23SStefan Hajnoczi le->features = cpu_to_le64(cpu->features); 8375411d23SStefan Hajnoczi le->compat_features = cpu_to_le64(cpu->compat_features); 8475411d23SStefan Hajnoczi le->autoclear_features = cpu_to_le64(cpu->autoclear_features); 8575411d23SStefan Hajnoczi le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); 8675411d23SStefan Hajnoczi le->image_size = cpu_to_le64(cpu->image_size); 8775411d23SStefan Hajnoczi le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); 8875411d23SStefan Hajnoczi le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); 8975411d23SStefan Hajnoczi } 9075411d23SStefan Hajnoczi 9175411d23SStefan Hajnoczi static int qed_write_header_sync(BDRVQEDState *s) 9275411d23SStefan Hajnoczi { 9375411d23SStefan Hajnoczi QEDHeader le; 9475411d23SStefan Hajnoczi int ret; 9575411d23SStefan Hajnoczi 9675411d23SStefan Hajnoczi qed_header_cpu_to_le(&s->header, &le); 9775411d23SStefan Hajnoczi ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le)); 9875411d23SStefan Hajnoczi if (ret != sizeof(le)) { 9975411d23SStefan Hajnoczi return ret; 10075411d23SStefan Hajnoczi } 10175411d23SStefan Hajnoczi return 0; 10275411d23SStefan Hajnoczi } 10375411d23SStefan Hajnoczi 10401979a98SStefan Hajnoczi typedef struct { 10501979a98SStefan Hajnoczi GenericCB gencb; 10601979a98SStefan Hajnoczi BDRVQEDState *s; 10701979a98SStefan Hajnoczi struct iovec iov; 10801979a98SStefan Hajnoczi QEMUIOVector qiov; 10901979a98SStefan Hajnoczi int nsectors; 11001979a98SStefan Hajnoczi uint8_t *buf; 11101979a98SStefan Hajnoczi } QEDWriteHeaderCB; 11201979a98SStefan Hajnoczi 11301979a98SStefan Hajnoczi static void qed_write_header_cb(void *opaque, int ret) 11401979a98SStefan Hajnoczi { 11501979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = opaque; 11601979a98SStefan Hajnoczi 11701979a98SStefan Hajnoczi qemu_vfree(write_header_cb->buf); 11801979a98SStefan Hajnoczi gencb_complete(write_header_cb, ret); 11901979a98SStefan Hajnoczi } 12001979a98SStefan Hajnoczi 12101979a98SStefan Hajnoczi static void qed_write_header_read_cb(void *opaque, int ret) 12201979a98SStefan Hajnoczi { 12301979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = opaque; 12401979a98SStefan Hajnoczi BDRVQEDState *s = write_header_cb->s; 12501979a98SStefan Hajnoczi BlockDriverAIOCB *acb; 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 13501979a98SStefan Hajnoczi acb = 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 if (!acb) { 13901979a98SStefan Hajnoczi qed_write_header_cb(write_header_cb, -EIO); 14001979a98SStefan Hajnoczi } 14101979a98SStefan Hajnoczi } 14201979a98SStefan Hajnoczi 14301979a98SStefan Hajnoczi /** 14401979a98SStefan Hajnoczi * Update header in-place (does not rewrite backing filename or other strings) 14501979a98SStefan Hajnoczi * 14601979a98SStefan Hajnoczi * This function only updates known header fields in-place and does not affect 14701979a98SStefan Hajnoczi * extra data after the QED header. 14801979a98SStefan Hajnoczi */ 14901979a98SStefan Hajnoczi static void qed_write_header(BDRVQEDState *s, BlockDriverCompletionFunc cb, 15001979a98SStefan Hajnoczi void *opaque) 15101979a98SStefan Hajnoczi { 15201979a98SStefan Hajnoczi /* We must write full sectors for O_DIRECT but cannot necessarily generate 15301979a98SStefan Hajnoczi * the data following the header if an unrecognized compat feature is 15401979a98SStefan Hajnoczi * active. Therefore, first read the sectors containing the header, update 15501979a98SStefan Hajnoczi * them, and write back. 15601979a98SStefan Hajnoczi */ 15701979a98SStefan Hajnoczi 15801979a98SStefan Hajnoczi BlockDriverAIOCB *acb; 15901979a98SStefan Hajnoczi int nsectors = (sizeof(QEDHeader) + BDRV_SECTOR_SIZE - 1) / 16001979a98SStefan Hajnoczi BDRV_SECTOR_SIZE; 16101979a98SStefan Hajnoczi size_t len = nsectors * BDRV_SECTOR_SIZE; 16201979a98SStefan Hajnoczi QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb), 16301979a98SStefan Hajnoczi cb, opaque); 16401979a98SStefan Hajnoczi 16501979a98SStefan Hajnoczi write_header_cb->s = s; 16601979a98SStefan Hajnoczi write_header_cb->nsectors = nsectors; 16701979a98SStefan Hajnoczi write_header_cb->buf = qemu_blockalign(s->bs, len); 16801979a98SStefan Hajnoczi write_header_cb->iov.iov_base = write_header_cb->buf; 16901979a98SStefan Hajnoczi write_header_cb->iov.iov_len = len; 17001979a98SStefan Hajnoczi qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1); 17101979a98SStefan Hajnoczi 17201979a98SStefan Hajnoczi acb = bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors, 17301979a98SStefan Hajnoczi qed_write_header_read_cb, write_header_cb); 17401979a98SStefan Hajnoczi if (!acb) { 17501979a98SStefan Hajnoczi qed_write_header_cb(write_header_cb, -EIO); 17601979a98SStefan Hajnoczi } 17701979a98SStefan Hajnoczi } 17801979a98SStefan Hajnoczi 17975411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 18075411d23SStefan Hajnoczi { 18175411d23SStefan Hajnoczi uint64_t table_entries; 18275411d23SStefan Hajnoczi uint64_t l2_size; 18375411d23SStefan Hajnoczi 18475411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t); 18575411d23SStefan Hajnoczi l2_size = table_entries * cluster_size; 18675411d23SStefan Hajnoczi 18775411d23SStefan Hajnoczi return l2_size * table_entries; 18875411d23SStefan Hajnoczi } 18975411d23SStefan Hajnoczi 19075411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size) 19175411d23SStefan Hajnoczi { 19275411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE || 19375411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) { 19475411d23SStefan Hajnoczi return false; 19575411d23SStefan Hajnoczi } 19675411d23SStefan Hajnoczi if (cluster_size & (cluster_size - 1)) { 19775411d23SStefan Hajnoczi return false; /* not power of 2 */ 19875411d23SStefan Hajnoczi } 19975411d23SStefan Hajnoczi return true; 20075411d23SStefan Hajnoczi } 20175411d23SStefan Hajnoczi 20275411d23SStefan Hajnoczi static bool qed_is_table_size_valid(uint32_t table_size) 20375411d23SStefan Hajnoczi { 20475411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE || 20575411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) { 20675411d23SStefan Hajnoczi return false; 20775411d23SStefan Hajnoczi } 20875411d23SStefan Hajnoczi if (table_size & (table_size - 1)) { 20975411d23SStefan Hajnoczi return false; /* not power of 2 */ 21075411d23SStefan Hajnoczi } 21175411d23SStefan Hajnoczi return true; 21275411d23SStefan Hajnoczi } 21375411d23SStefan Hajnoczi 21475411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 21575411d23SStefan Hajnoczi uint32_t table_size) 21675411d23SStefan Hajnoczi { 21775411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) { 21875411d23SStefan Hajnoczi return false; /* not multiple of sector size */ 21975411d23SStefan Hajnoczi } 22075411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) { 22175411d23SStefan Hajnoczi return false; /* image is too large */ 22275411d23SStefan Hajnoczi } 22375411d23SStefan Hajnoczi return true; 22475411d23SStefan Hajnoczi } 22575411d23SStefan Hajnoczi 22675411d23SStefan Hajnoczi /** 22775411d23SStefan Hajnoczi * Read a string of known length from the image file 22875411d23SStefan Hajnoczi * 22975411d23SStefan Hajnoczi * @file: Image file 23075411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes 23175411d23SStefan Hajnoczi * @n: String length in bytes 23275411d23SStefan Hajnoczi * @buf: Destination buffer 23375411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes 23475411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure 23575411d23SStefan Hajnoczi * 23675411d23SStefan Hajnoczi * The string is NUL-terminated. 23775411d23SStefan Hajnoczi */ 23875411d23SStefan Hajnoczi static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n, 23975411d23SStefan Hajnoczi char *buf, size_t buflen) 24075411d23SStefan Hajnoczi { 24175411d23SStefan Hajnoczi int ret; 24275411d23SStefan Hajnoczi if (n >= buflen) { 24375411d23SStefan Hajnoczi return -EINVAL; 24475411d23SStefan Hajnoczi } 24575411d23SStefan Hajnoczi ret = bdrv_pread(file, offset, buf, n); 24675411d23SStefan Hajnoczi if (ret < 0) { 24775411d23SStefan Hajnoczi return ret; 24875411d23SStefan Hajnoczi } 24975411d23SStefan Hajnoczi buf[n] = '\0'; 25075411d23SStefan Hajnoczi return 0; 25175411d23SStefan Hajnoczi } 25275411d23SStefan Hajnoczi 253eabba580SStefan Hajnoczi /** 254eabba580SStefan Hajnoczi * Allocate new clusters 255eabba580SStefan Hajnoczi * 256eabba580SStefan Hajnoczi * @s: QED state 257eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate 258eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster 259eabba580SStefan Hajnoczi * 260eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be 261eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image 262eabba580SStefan Hajnoczi * file. 263eabba580SStefan Hajnoczi */ 264eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 265eabba580SStefan Hajnoczi { 266eabba580SStefan Hajnoczi uint64_t offset = s->file_size; 267eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size; 268eabba580SStefan Hajnoczi return offset; 269eabba580SStefan Hajnoczi } 270eabba580SStefan Hajnoczi 271298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s) 272298800caSStefan Hajnoczi { 273298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */ 274298800caSStefan Hajnoczi return qemu_blockalign(s->bs, 275298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size); 276298800caSStefan Hajnoczi } 277298800caSStefan Hajnoczi 278eabba580SStefan Hajnoczi /** 279eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table 280eabba580SStefan Hajnoczi */ 281eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 282eabba580SStefan Hajnoczi { 283eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 284eabba580SStefan Hajnoczi 285eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s); 286eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 287eabba580SStefan Hajnoczi 288eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0, 289eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size); 290eabba580SStefan Hajnoczi return l2_table; 291eabba580SStefan Hajnoczi } 292eabba580SStefan Hajnoczi 293eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret); 294eabba580SStefan Hajnoczi 2956f321e93SStefan Hajnoczi static void qed_plug_allocating_write_reqs(BDRVQEDState *s) 2966f321e93SStefan Hajnoczi { 2976f321e93SStefan Hajnoczi assert(!s->allocating_write_reqs_plugged); 2986f321e93SStefan Hajnoczi 2996f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = true; 3006f321e93SStefan Hajnoczi } 3016f321e93SStefan Hajnoczi 3026f321e93SStefan Hajnoczi static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) 3036f321e93SStefan Hajnoczi { 3046f321e93SStefan Hajnoczi QEDAIOCB *acb; 3056f321e93SStefan Hajnoczi 3066f321e93SStefan Hajnoczi assert(s->allocating_write_reqs_plugged); 3076f321e93SStefan Hajnoczi 3086f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = false; 3096f321e93SStefan Hajnoczi 3106f321e93SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 3116f321e93SStefan Hajnoczi if (acb) { 3126f321e93SStefan Hajnoczi qed_aio_next_io(acb, 0); 3136f321e93SStefan Hajnoczi } 3146f321e93SStefan Hajnoczi } 3156f321e93SStefan Hajnoczi 3166f321e93SStefan Hajnoczi static void qed_finish_clear_need_check(void *opaque, int ret) 3176f321e93SStefan Hajnoczi { 3186f321e93SStefan Hajnoczi /* Do nothing */ 3196f321e93SStefan Hajnoczi } 3206f321e93SStefan Hajnoczi 3216f321e93SStefan Hajnoczi static void qed_flush_after_clear_need_check(void *opaque, int ret) 3226f321e93SStefan Hajnoczi { 3236f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3246f321e93SStefan Hajnoczi 3256f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s); 3266f321e93SStefan Hajnoczi 3276f321e93SStefan Hajnoczi /* No need to wait until flush completes */ 3286f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3296f321e93SStefan Hajnoczi } 3306f321e93SStefan Hajnoczi 3316f321e93SStefan Hajnoczi static void qed_clear_need_check(void *opaque, int ret) 3326f321e93SStefan Hajnoczi { 3336f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3346f321e93SStefan Hajnoczi 3356f321e93SStefan Hajnoczi if (ret) { 3366f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 3376f321e93SStefan Hajnoczi return; 3386f321e93SStefan Hajnoczi } 3396f321e93SStefan Hajnoczi 3406f321e93SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 3416f321e93SStefan Hajnoczi qed_write_header(s, qed_flush_after_clear_need_check, s); 3426f321e93SStefan Hajnoczi } 3436f321e93SStefan Hajnoczi 3446f321e93SStefan Hajnoczi static void qed_need_check_timer_cb(void *opaque) 3456f321e93SStefan Hajnoczi { 3466f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 3476f321e93SStefan Hajnoczi 3486f321e93SStefan Hajnoczi /* The timer should only fire when allocating writes have drained */ 3496f321e93SStefan Hajnoczi assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs)); 3506f321e93SStefan Hajnoczi 3516f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s); 3526f321e93SStefan Hajnoczi 3536f321e93SStefan Hajnoczi qed_plug_allocating_write_reqs(s); 3546f321e93SStefan Hajnoczi 3556f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */ 3566f321e93SStefan Hajnoczi bdrv_aio_flush(s->bs, qed_clear_need_check, s); 3576f321e93SStefan Hajnoczi } 3586f321e93SStefan Hajnoczi 3596f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s) 3606f321e93SStefan Hajnoczi { 3616f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s); 3626f321e93SStefan Hajnoczi 3636f321e93SStefan Hajnoczi /* Use vm_clock so we don't alter the image file while suspended for 3646f321e93SStefan Hajnoczi * migration. 3656f321e93SStefan Hajnoczi */ 3666f321e93SStefan Hajnoczi qemu_mod_timer(s->need_check_timer, qemu_get_clock_ns(vm_clock) + 3676f321e93SStefan Hajnoczi get_ticks_per_sec() * QED_NEED_CHECK_TIMEOUT); 3686f321e93SStefan Hajnoczi } 3696f321e93SStefan Hajnoczi 3706f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */ 3716f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s) 3726f321e93SStefan Hajnoczi { 3736f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s); 3746f321e93SStefan Hajnoczi qemu_del_timer(s->need_check_timer); 3756f321e93SStefan Hajnoczi } 3766f321e93SStefan Hajnoczi 37775411d23SStefan Hajnoczi static int bdrv_qed_open(BlockDriverState *bs, int flags) 37875411d23SStefan Hajnoczi { 37975411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 38075411d23SStefan Hajnoczi QEDHeader le_header; 38175411d23SStefan Hajnoczi int64_t file_size; 38275411d23SStefan Hajnoczi int ret; 38375411d23SStefan Hajnoczi 38475411d23SStefan Hajnoczi s->bs = bs; 385eabba580SStefan Hajnoczi QSIMPLEQ_INIT(&s->allocating_write_reqs); 38675411d23SStefan Hajnoczi 38775411d23SStefan Hajnoczi ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); 38875411d23SStefan Hajnoczi if (ret < 0) { 38975411d23SStefan Hajnoczi return ret; 39075411d23SStefan Hajnoczi } 39175411d23SStefan Hajnoczi ret = 0; /* ret should always be 0 or -errno */ 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 50801979a98SStefan Hajnoczi out: 50901979a98SStefan Hajnoczi if (ret) { 510298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 511298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 512298800caSStefan Hajnoczi } 51375411d23SStefan Hajnoczi return ret; 51475411d23SStefan Hajnoczi } 51575411d23SStefan Hajnoczi 51675411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 51775411d23SStefan Hajnoczi { 518298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 519298800caSStefan Hajnoczi 5206f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 5216f321e93SStefan Hajnoczi qemu_free_timer(s->need_check_timer); 5226f321e93SStefan Hajnoczi 52301979a98SStefan Hajnoczi /* Ensure writes reach stable storage */ 52401979a98SStefan Hajnoczi bdrv_flush(bs->file); 52501979a98SStefan Hajnoczi 52601979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */ 52701979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 52801979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 52901979a98SStefan Hajnoczi qed_write_header_sync(s); 53001979a98SStefan Hajnoczi } 53101979a98SStefan Hajnoczi 532298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 533298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 53475411d23SStefan Hajnoczi } 53575411d23SStefan Hajnoczi 53675411d23SStefan Hajnoczi static int bdrv_qed_flush(BlockDriverState *bs) 53775411d23SStefan Hajnoczi { 53875411d23SStefan Hajnoczi return bdrv_flush(bs->file); 53975411d23SStefan Hajnoczi } 54075411d23SStefan Hajnoczi 54175411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size, 54275411d23SStefan Hajnoczi uint64_t image_size, uint32_t table_size, 54375411d23SStefan Hajnoczi const char *backing_file, const char *backing_fmt) 54475411d23SStefan Hajnoczi { 54575411d23SStefan Hajnoczi QEDHeader header = { 54675411d23SStefan Hajnoczi .magic = QED_MAGIC, 54775411d23SStefan Hajnoczi .cluster_size = cluster_size, 54875411d23SStefan Hajnoczi .table_size = table_size, 54975411d23SStefan Hajnoczi .header_size = 1, 55075411d23SStefan Hajnoczi .features = 0, 55175411d23SStefan Hajnoczi .compat_features = 0, 55275411d23SStefan Hajnoczi .l1_table_offset = cluster_size, 55375411d23SStefan Hajnoczi .image_size = image_size, 55475411d23SStefan Hajnoczi }; 55575411d23SStefan Hajnoczi QEDHeader le_header; 55675411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 55775411d23SStefan Hajnoczi size_t l1_size = header.cluster_size * header.table_size; 55875411d23SStefan Hajnoczi int ret = 0; 55975411d23SStefan Hajnoczi BlockDriverState *bs = NULL; 56075411d23SStefan Hajnoczi 56175411d23SStefan Hajnoczi ret = bdrv_create_file(filename, NULL); 56275411d23SStefan Hajnoczi if (ret < 0) { 56375411d23SStefan Hajnoczi return ret; 56475411d23SStefan Hajnoczi } 56575411d23SStefan Hajnoczi 56675411d23SStefan Hajnoczi ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR | BDRV_O_CACHE_WB); 56775411d23SStefan Hajnoczi if (ret < 0) { 56875411d23SStefan Hajnoczi return ret; 56975411d23SStefan Hajnoczi } 57075411d23SStefan Hajnoczi 571c743849bSStefan Hajnoczi /* File must start empty and grow, check truncate is supported */ 572c743849bSStefan Hajnoczi ret = bdrv_truncate(bs, 0); 573c743849bSStefan Hajnoczi if (ret < 0) { 574c743849bSStefan Hajnoczi goto out; 575c743849bSStefan Hajnoczi } 576c743849bSStefan Hajnoczi 57775411d23SStefan Hajnoczi if (backing_file) { 57875411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 57975411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 58075411d23SStefan Hajnoczi header.backing_filename_size = strlen(backing_file); 58175411d23SStefan Hajnoczi 58275411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 58375411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 58475411d23SStefan Hajnoczi } 58575411d23SStefan Hajnoczi } 58675411d23SStefan Hajnoczi 58775411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 58875411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header)); 58975411d23SStefan Hajnoczi if (ret < 0) { 59075411d23SStefan Hajnoczi goto out; 59175411d23SStefan Hajnoczi } 59275411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, sizeof(le_header), backing_file, 59375411d23SStefan Hajnoczi header.backing_filename_size); 59475411d23SStefan Hajnoczi if (ret < 0) { 59575411d23SStefan Hajnoczi goto out; 59675411d23SStefan Hajnoczi } 59775411d23SStefan Hajnoczi 59875411d23SStefan Hajnoczi l1_table = qemu_mallocz(l1_size); 59975411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size); 60075411d23SStefan Hajnoczi if (ret < 0) { 60175411d23SStefan Hajnoczi goto out; 60275411d23SStefan Hajnoczi } 60375411d23SStefan Hajnoczi 60475411d23SStefan Hajnoczi ret = 0; /* success */ 60575411d23SStefan Hajnoczi out: 60675411d23SStefan Hajnoczi qemu_free(l1_table); 60775411d23SStefan Hajnoczi bdrv_delete(bs); 60875411d23SStefan Hajnoczi return ret; 60975411d23SStefan Hajnoczi } 61075411d23SStefan Hajnoczi 61175411d23SStefan Hajnoczi static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options) 61275411d23SStefan Hajnoczi { 61375411d23SStefan Hajnoczi uint64_t image_size = 0; 61475411d23SStefan Hajnoczi uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 61575411d23SStefan Hajnoczi uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 61675411d23SStefan Hajnoczi const char *backing_file = NULL; 61775411d23SStefan Hajnoczi const char *backing_fmt = NULL; 61875411d23SStefan Hajnoczi 61975411d23SStefan Hajnoczi while (options && options->name) { 62075411d23SStefan Hajnoczi if (!strcmp(options->name, BLOCK_OPT_SIZE)) { 62175411d23SStefan Hajnoczi image_size = options->value.n; 62275411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { 62375411d23SStefan Hajnoczi backing_file = options->value.s; 62475411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { 62575411d23SStefan Hajnoczi backing_fmt = options->value.s; 62675411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { 62775411d23SStefan Hajnoczi if (options->value.n) { 62875411d23SStefan Hajnoczi cluster_size = options->value.n; 62975411d23SStefan Hajnoczi } 63075411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_TABLE_SIZE)) { 63175411d23SStefan Hajnoczi if (options->value.n) { 63275411d23SStefan Hajnoczi table_size = options->value.n; 63375411d23SStefan Hajnoczi } 63475411d23SStefan Hajnoczi } 63575411d23SStefan Hajnoczi options++; 63675411d23SStefan Hajnoczi } 63775411d23SStefan Hajnoczi 63875411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(cluster_size)) { 63975411d23SStefan Hajnoczi fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n", 64075411d23SStefan Hajnoczi QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 64175411d23SStefan Hajnoczi return -EINVAL; 64275411d23SStefan Hajnoczi } 64375411d23SStefan Hajnoczi if (!qed_is_table_size_valid(table_size)) { 64475411d23SStefan Hajnoczi fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n", 64575411d23SStefan Hajnoczi QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 64675411d23SStefan Hajnoczi return -EINVAL; 64775411d23SStefan Hajnoczi } 64875411d23SStefan Hajnoczi if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 64975411d23SStefan Hajnoczi fprintf(stderr, "QED image size must be a non-zero multiple of " 65075411d23SStefan Hajnoczi "cluster size and less than %" PRIu64 " bytes\n", 65175411d23SStefan Hajnoczi qed_max_image_size(cluster_size, table_size)); 65275411d23SStefan Hajnoczi return -EINVAL; 65375411d23SStefan Hajnoczi } 65475411d23SStefan Hajnoczi 65575411d23SStefan Hajnoczi return qed_create(filename, cluster_size, image_size, table_size, 65675411d23SStefan Hajnoczi backing_file, backing_fmt); 65775411d23SStefan Hajnoczi } 65875411d23SStefan Hajnoczi 659298800caSStefan Hajnoczi typedef struct { 660298800caSStefan Hajnoczi int is_allocated; 661298800caSStefan Hajnoczi int *pnum; 662298800caSStefan Hajnoczi } QEDIsAllocatedCB; 663298800caSStefan Hajnoczi 664298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 665298800caSStefan Hajnoczi { 666298800caSStefan Hajnoczi QEDIsAllocatedCB *cb = opaque; 667298800caSStefan Hajnoczi *cb->pnum = len / BDRV_SECTOR_SIZE; 66821df65b6SAnthony Liguori cb->is_allocated = (ret == QED_CLUSTER_FOUND || ret == QED_CLUSTER_ZERO); 669298800caSStefan Hajnoczi } 670298800caSStefan Hajnoczi 67175411d23SStefan Hajnoczi static int bdrv_qed_is_allocated(BlockDriverState *bs, int64_t sector_num, 67275411d23SStefan Hajnoczi int nb_sectors, int *pnum) 67375411d23SStefan Hajnoczi { 674298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 675298800caSStefan Hajnoczi uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 676298800caSStefan Hajnoczi size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 677298800caSStefan Hajnoczi QEDIsAllocatedCB cb = { 678298800caSStefan Hajnoczi .is_allocated = -1, 679298800caSStefan Hajnoczi .pnum = pnum, 680298800caSStefan Hajnoczi }; 681298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 682298800caSStefan Hajnoczi 683298800caSStefan Hajnoczi async_context_push(); 684298800caSStefan Hajnoczi 685298800caSStefan Hajnoczi qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb); 686298800caSStefan Hajnoczi 687298800caSStefan Hajnoczi while (cb.is_allocated == -1) { 688298800caSStefan Hajnoczi qemu_aio_wait(); 689298800caSStefan Hajnoczi } 690298800caSStefan Hajnoczi 691298800caSStefan Hajnoczi async_context_pop(); 692298800caSStefan Hajnoczi 693298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 694298800caSStefan Hajnoczi 695298800caSStefan Hajnoczi return cb.is_allocated; 69675411d23SStefan Hajnoczi } 69775411d23SStefan Hajnoczi 69875411d23SStefan Hajnoczi static int bdrv_qed_make_empty(BlockDriverState *bs) 69975411d23SStefan Hajnoczi { 70075411d23SStefan Hajnoczi return -ENOTSUP; 70175411d23SStefan Hajnoczi } 70275411d23SStefan Hajnoczi 703eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 704eabba580SStefan Hajnoczi { 705eabba580SStefan Hajnoczi return acb->common.bs->opaque; 706eabba580SStefan Hajnoczi } 707eabba580SStefan Hajnoczi 708eabba580SStefan Hajnoczi /** 709eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 710eabba580SStefan Hajnoczi * 711eabba580SStefan Hajnoczi * @s: QED state 712eabba580SStefan Hajnoczi * @pos: Byte position in device 713eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 714eabba580SStefan Hajnoczi * @cb: Completion function 715eabba580SStefan Hajnoczi * @opaque: User data for completion function 716eabba580SStefan Hajnoczi * 717eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 718eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 719eabba580SStefan Hajnoczi */ 720eabba580SStefan Hajnoczi static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 721eabba580SStefan Hajnoczi QEMUIOVector *qiov, 722eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, void *opaque) 723eabba580SStefan Hajnoczi { 724eabba580SStefan Hajnoczi BlockDriverAIOCB *aiocb; 725eabba580SStefan Hajnoczi uint64_t backing_length = 0; 726eabba580SStefan Hajnoczi size_t size; 727eabba580SStefan Hajnoczi 728eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 729eabba580SStefan Hajnoczi * backing file like a zero length backing file. 730eabba580SStefan Hajnoczi */ 731eabba580SStefan Hajnoczi if (s->bs->backing_hd) { 732eabba580SStefan Hajnoczi int64_t l = bdrv_getlength(s->bs->backing_hd); 733eabba580SStefan Hajnoczi if (l < 0) { 734eabba580SStefan Hajnoczi cb(opaque, l); 735eabba580SStefan Hajnoczi return; 736eabba580SStefan Hajnoczi } 737eabba580SStefan Hajnoczi backing_length = l; 738eabba580SStefan Hajnoczi } 739eabba580SStefan Hajnoczi 740eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 741eabba580SStefan Hajnoczi if (pos >= backing_length || 742eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 743eabba580SStefan Hajnoczi qemu_iovec_memset(qiov, 0, qiov->size); 744eabba580SStefan Hajnoczi } 745eabba580SStefan Hajnoczi 746eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 747eabba580SStefan Hajnoczi if (pos >= backing_length) { 748eabba580SStefan Hajnoczi cb(opaque, 0); 749eabba580SStefan Hajnoczi return; 750eabba580SStefan Hajnoczi } 751eabba580SStefan Hajnoczi 752eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 753eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 754eabba580SStefan Hajnoczi 755eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING); 756eabba580SStefan Hajnoczi aiocb = bdrv_aio_readv(s->bs->backing_hd, pos / BDRV_SECTOR_SIZE, 757eabba580SStefan Hajnoczi qiov, size / BDRV_SECTOR_SIZE, cb, opaque); 758eabba580SStefan Hajnoczi if (!aiocb) { 759eabba580SStefan Hajnoczi cb(opaque, -EIO); 760eabba580SStefan Hajnoczi } 761eabba580SStefan Hajnoczi } 762eabba580SStefan Hajnoczi 763eabba580SStefan Hajnoczi typedef struct { 764eabba580SStefan Hajnoczi GenericCB gencb; 765eabba580SStefan Hajnoczi BDRVQEDState *s; 766eabba580SStefan Hajnoczi QEMUIOVector qiov; 767eabba580SStefan Hajnoczi struct iovec iov; 768eabba580SStefan Hajnoczi uint64_t offset; 769eabba580SStefan Hajnoczi } CopyFromBackingFileCB; 770eabba580SStefan Hajnoczi 771eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_cb(void *opaque, int ret) 772eabba580SStefan Hajnoczi { 773eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 774eabba580SStefan Hajnoczi qemu_vfree(copy_cb->iov.iov_base); 775eabba580SStefan Hajnoczi gencb_complete(©_cb->gencb, ret); 776eabba580SStefan Hajnoczi } 777eabba580SStefan Hajnoczi 778eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_write(void *opaque, int ret) 779eabba580SStefan Hajnoczi { 780eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 781eabba580SStefan Hajnoczi BDRVQEDState *s = copy_cb->s; 782eabba580SStefan Hajnoczi BlockDriverAIOCB *aiocb; 783eabba580SStefan Hajnoczi 784eabba580SStefan Hajnoczi if (ret) { 785eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb(copy_cb, ret); 786eabba580SStefan Hajnoczi return; 787eabba580SStefan Hajnoczi } 788eabba580SStefan Hajnoczi 789eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 790eabba580SStefan Hajnoczi aiocb = bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE, 791eabba580SStefan Hajnoczi ©_cb->qiov, 792eabba580SStefan Hajnoczi copy_cb->qiov.size / BDRV_SECTOR_SIZE, 793eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb, copy_cb); 794eabba580SStefan Hajnoczi if (!aiocb) { 795eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb(copy_cb, -EIO); 796eabba580SStefan Hajnoczi } 797eabba580SStefan Hajnoczi } 798eabba580SStefan Hajnoczi 799eabba580SStefan Hajnoczi /** 800eabba580SStefan Hajnoczi * Copy data from backing file into the image 801eabba580SStefan Hajnoczi * 802eabba580SStefan Hajnoczi * @s: QED state 803eabba580SStefan Hajnoczi * @pos: Byte position in device 804eabba580SStefan Hajnoczi * @len: Number of bytes 805eabba580SStefan Hajnoczi * @offset: Byte offset in image file 806eabba580SStefan Hajnoczi * @cb: Completion function 807eabba580SStefan Hajnoczi * @opaque: User data for completion function 808eabba580SStefan Hajnoczi */ 809eabba580SStefan Hajnoczi static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, 810eabba580SStefan Hajnoczi uint64_t len, uint64_t offset, 811eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, 812eabba580SStefan Hajnoczi void *opaque) 813eabba580SStefan Hajnoczi { 814eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb; 815eabba580SStefan Hajnoczi 816eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 817eabba580SStefan Hajnoczi if (len == 0) { 818eabba580SStefan Hajnoczi cb(opaque, 0); 819eabba580SStefan Hajnoczi return; 820eabba580SStefan Hajnoczi } 821eabba580SStefan Hajnoczi 822eabba580SStefan Hajnoczi copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque); 823eabba580SStefan Hajnoczi copy_cb->s = s; 824eabba580SStefan Hajnoczi copy_cb->offset = offset; 825eabba580SStefan Hajnoczi copy_cb->iov.iov_base = qemu_blockalign(s->bs, len); 826eabba580SStefan Hajnoczi copy_cb->iov.iov_len = len; 827eabba580SStefan Hajnoczi qemu_iovec_init_external(©_cb->qiov, ©_cb->iov, 1); 828eabba580SStefan Hajnoczi 829eabba580SStefan Hajnoczi qed_read_backing_file(s, pos, ©_cb->qiov, 830eabba580SStefan Hajnoczi qed_copy_from_backing_file_write, copy_cb); 831eabba580SStefan Hajnoczi } 832eabba580SStefan Hajnoczi 833eabba580SStefan Hajnoczi /** 834eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 835eabba580SStefan Hajnoczi * 836eabba580SStefan Hajnoczi * @s: QED state 837eabba580SStefan Hajnoczi * @table: L2 table 838eabba580SStefan Hajnoczi * @index: First cluster index 839eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 84021df65b6SAnthony Liguori * @cluster: First cluster offset 84121df65b6SAnthony Liguori * 84221df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the 84321df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker. 844eabba580SStefan Hajnoczi */ 845eabba580SStefan Hajnoczi static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index, 846eabba580SStefan Hajnoczi unsigned int n, uint64_t cluster) 847eabba580SStefan Hajnoczi { 848eabba580SStefan Hajnoczi int i; 849eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 850eabba580SStefan Hajnoczi table->offsets[i] = cluster; 85121df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) && 85221df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) { 853eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 854eabba580SStefan Hajnoczi } 855eabba580SStefan Hajnoczi } 85621df65b6SAnthony Liguori } 857eabba580SStefan Hajnoczi 858eabba580SStefan Hajnoczi static void qed_aio_complete_bh(void *opaque) 859eabba580SStefan Hajnoczi { 860eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 861eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb = acb->common.cb; 862eabba580SStefan Hajnoczi void *user_opaque = acb->common.opaque; 863eabba580SStefan Hajnoczi int ret = acb->bh_ret; 864eabba580SStefan Hajnoczi bool *finished = acb->finished; 865eabba580SStefan Hajnoczi 866eabba580SStefan Hajnoczi qemu_bh_delete(acb->bh); 867eabba580SStefan Hajnoczi qemu_aio_release(acb); 868eabba580SStefan Hajnoczi 869eabba580SStefan Hajnoczi /* Invoke callback */ 870eabba580SStefan Hajnoczi cb(user_opaque, ret); 871eabba580SStefan Hajnoczi 872eabba580SStefan Hajnoczi /* Signal cancel completion */ 873eabba580SStefan Hajnoczi if (finished) { 874eabba580SStefan Hajnoczi *finished = true; 875eabba580SStefan Hajnoczi } 876eabba580SStefan Hajnoczi } 877eabba580SStefan Hajnoczi 878eabba580SStefan Hajnoczi static void qed_aio_complete(QEDAIOCB *acb, int ret) 879eabba580SStefan Hajnoczi { 880eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 881eabba580SStefan Hajnoczi 882eabba580SStefan Hajnoczi trace_qed_aio_complete(s, acb, ret); 883eabba580SStefan Hajnoczi 884eabba580SStefan Hajnoczi /* Free resources */ 885eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov); 886eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 887eabba580SStefan Hajnoczi 888eabba580SStefan Hajnoczi /* Arrange for a bh to invoke the completion function */ 889eabba580SStefan Hajnoczi acb->bh_ret = ret; 890eabba580SStefan Hajnoczi acb->bh = qemu_bh_new(qed_aio_complete_bh, acb); 891eabba580SStefan Hajnoczi qemu_bh_schedule(acb->bh); 892eabba580SStefan Hajnoczi 893eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 894eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 895eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 896eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 897eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 898eabba580SStefan Hajnoczi */ 899eabba580SStefan Hajnoczi if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 900eabba580SStefan Hajnoczi QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next); 901eabba580SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 902eabba580SStefan Hajnoczi if (acb) { 903eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 9046f321e93SStefan Hajnoczi } else if (s->header.features & QED_F_NEED_CHECK) { 9056f321e93SStefan Hajnoczi qed_start_need_check_timer(s); 906eabba580SStefan Hajnoczi } 907eabba580SStefan Hajnoczi } 908eabba580SStefan Hajnoczi } 909eabba580SStefan Hajnoczi 910eabba580SStefan Hajnoczi /** 911eabba580SStefan Hajnoczi * Commit the current L2 table to the cache 912eabba580SStefan Hajnoczi */ 913eabba580SStefan Hajnoczi static void qed_commit_l2_update(void *opaque, int ret) 914eabba580SStefan Hajnoczi { 915eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 916eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 917eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 918eabba580SStefan Hajnoczi 919eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 920eabba580SStefan Hajnoczi 921eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 922eabba580SStefan Hajnoczi * cache. 923eabba580SStefan Hajnoczi */ 924eabba580SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, 925eabba580SStefan Hajnoczi l2_table->offset); 926eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 927eabba580SStefan Hajnoczi 928eabba580SStefan Hajnoczi qed_aio_next_io(opaque, ret); 929eabba580SStefan Hajnoczi } 930eabba580SStefan Hajnoczi 931eabba580SStefan Hajnoczi /** 932eabba580SStefan Hajnoczi * Update L1 table with new L2 table offset and write it out 933eabba580SStefan Hajnoczi */ 934eabba580SStefan Hajnoczi static void qed_aio_write_l1_update(void *opaque, int ret) 935eabba580SStefan Hajnoczi { 936eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 937eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 938eabba580SStefan Hajnoczi int index; 939eabba580SStefan Hajnoczi 940eabba580SStefan Hajnoczi if (ret) { 941eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 942eabba580SStefan Hajnoczi return; 943eabba580SStefan Hajnoczi } 944eabba580SStefan Hajnoczi 945eabba580SStefan Hajnoczi index = qed_l1_index(s, acb->cur_pos); 946eabba580SStefan Hajnoczi s->l1_table->offsets[index] = acb->request.l2_table->offset; 947eabba580SStefan Hajnoczi 948eabba580SStefan Hajnoczi qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb); 949eabba580SStefan Hajnoczi } 950eabba580SStefan Hajnoczi 951eabba580SStefan Hajnoczi /** 952eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 953eabba580SStefan Hajnoczi */ 954eabba580SStefan Hajnoczi static void qed_aio_write_l2_update(void *opaque, int ret) 955eabba580SStefan Hajnoczi { 956eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 957eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 958eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 959eabba580SStefan Hajnoczi int index; 960eabba580SStefan Hajnoczi 961eabba580SStefan Hajnoczi if (ret) { 962eabba580SStefan Hajnoczi goto err; 963eabba580SStefan Hajnoczi } 964eabba580SStefan Hajnoczi 965eabba580SStefan Hajnoczi if (need_alloc) { 966eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 967eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 968eabba580SStefan Hajnoczi } 969eabba580SStefan Hajnoczi 970eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 971eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 972eabba580SStefan Hajnoczi acb->cur_cluster); 973eabba580SStefan Hajnoczi 974eabba580SStefan Hajnoczi if (need_alloc) { 975eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 976eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true, 977eabba580SStefan Hajnoczi qed_aio_write_l1_update, acb); 978eabba580SStefan Hajnoczi } else { 979eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 980eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false, 981eabba580SStefan Hajnoczi qed_aio_next_io, acb); 982eabba580SStefan Hajnoczi } 983eabba580SStefan Hajnoczi return; 984eabba580SStefan Hajnoczi 985eabba580SStefan Hajnoczi err: 986eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 987eabba580SStefan Hajnoczi } 988eabba580SStefan Hajnoczi 989eabba580SStefan Hajnoczi /** 990eabba580SStefan Hajnoczi * Flush new data clusters before updating the L2 table 991eabba580SStefan Hajnoczi * 992eabba580SStefan Hajnoczi * This flush is necessary when a backing file is in use. A crash during an 993eabba580SStefan Hajnoczi * allocating write could result in empty clusters in the image. If the write 994eabba580SStefan Hajnoczi * only touched a subregion of the cluster, then backing image sectors have 995eabba580SStefan Hajnoczi * been lost in the untouched region. The solution is to flush after writing a 996eabba580SStefan Hajnoczi * new data cluster and before updating the L2 table. 997eabba580SStefan Hajnoczi */ 998eabba580SStefan Hajnoczi static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) 999eabba580SStefan Hajnoczi { 1000eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1001eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1002eabba580SStefan Hajnoczi 1003eabba580SStefan Hajnoczi if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update, opaque)) { 1004eabba580SStefan Hajnoczi qed_aio_complete(acb, -EIO); 1005eabba580SStefan Hajnoczi } 1006eabba580SStefan Hajnoczi } 1007eabba580SStefan Hajnoczi 1008eabba580SStefan Hajnoczi /** 1009eabba580SStefan Hajnoczi * Write data to the image file 1010eabba580SStefan Hajnoczi */ 1011eabba580SStefan Hajnoczi static void qed_aio_write_main(void *opaque, int ret) 1012eabba580SStefan Hajnoczi { 1013eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1014eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1015eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1016eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 1017eabba580SStefan Hajnoczi BlockDriverCompletionFunc *next_fn; 1018eabba580SStefan Hajnoczi BlockDriverAIOCB *file_acb; 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 { 1033eabba580SStefan Hajnoczi next_fn = qed_aio_write_l2_update; 1034eabba580SStefan Hajnoczi } 1035eabba580SStefan Hajnoczi } 1036eabba580SStefan Hajnoczi 1037eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 1038eabba580SStefan Hajnoczi file_acb = bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE, 1039eabba580SStefan Hajnoczi &acb->cur_qiov, 1040eabba580SStefan Hajnoczi acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1041eabba580SStefan Hajnoczi next_fn, acb); 1042eabba580SStefan Hajnoczi if (!file_acb) { 1043eabba580SStefan Hajnoczi qed_aio_complete(acb, -EIO); 1044eabba580SStefan Hajnoczi } 1045eabba580SStefan Hajnoczi } 1046eabba580SStefan Hajnoczi 1047eabba580SStefan Hajnoczi /** 1048eabba580SStefan Hajnoczi * Populate back untouched region of new data cluster 1049eabba580SStefan Hajnoczi */ 1050eabba580SStefan Hajnoczi static void qed_aio_write_postfill(void *opaque, int ret) 1051eabba580SStefan Hajnoczi { 1052eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1053eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1054eabba580SStefan Hajnoczi uint64_t start = acb->cur_pos + acb->cur_qiov.size; 1055eabba580SStefan Hajnoczi uint64_t len = 1056eabba580SStefan Hajnoczi qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1057eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1058eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + 1059eabba580SStefan Hajnoczi acb->cur_qiov.size; 1060eabba580SStefan Hajnoczi 1061eabba580SStefan Hajnoczi if (ret) { 1062eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1063eabba580SStefan Hajnoczi return; 1064eabba580SStefan Hajnoczi } 1065eabba580SStefan Hajnoczi 1066eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 1067eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, offset, 1068eabba580SStefan Hajnoczi qed_aio_write_main, acb); 1069eabba580SStefan Hajnoczi } 1070eabba580SStefan Hajnoczi 1071eabba580SStefan Hajnoczi /** 1072eabba580SStefan Hajnoczi * Populate front untouched region of new data cluster 1073eabba580SStefan Hajnoczi */ 1074eabba580SStefan Hajnoczi static void qed_aio_write_prefill(void *opaque, int ret) 1075eabba580SStefan Hajnoczi { 1076eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1077eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1078eabba580SStefan Hajnoczi uint64_t start = qed_start_of_cluster(s, acb->cur_pos); 1079eabba580SStefan Hajnoczi uint64_t len = qed_offset_into_cluster(s, acb->cur_pos); 1080eabba580SStefan Hajnoczi 1081eabba580SStefan Hajnoczi trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1082eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, acb->cur_cluster, 1083eabba580SStefan Hajnoczi qed_aio_write_postfill, acb); 1084eabba580SStefan Hajnoczi } 1085eabba580SStefan Hajnoczi 1086eabba580SStefan Hajnoczi /** 10870d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write 10880d09c797SStefan Hajnoczi */ 10890d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s) 10900d09c797SStefan Hajnoczi { 10910d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */ 10920d09c797SStefan Hajnoczi if (s->bs->backing_hd) { 10930d09c797SStefan Hajnoczi return false; 10940d09c797SStefan Hajnoczi } 10950d09c797SStefan Hajnoczi 10960d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK); 10970d09c797SStefan Hajnoczi } 10980d09c797SStefan Hajnoczi 10990d09c797SStefan Hajnoczi /** 1100eabba580SStefan Hajnoczi * Write new data cluster 1101eabba580SStefan Hajnoczi * 1102eabba580SStefan Hajnoczi * @acb: Write request 1103eabba580SStefan Hajnoczi * @len: Length in bytes 1104eabba580SStefan Hajnoczi * 1105eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters. 1106eabba580SStefan Hajnoczi */ 1107eabba580SStefan Hajnoczi static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1108eabba580SStefan Hajnoczi { 1109eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1110eabba580SStefan Hajnoczi 11116f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */ 11126f321e93SStefan Hajnoczi if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) { 11136f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 11146f321e93SStefan Hajnoczi } 11156f321e93SStefan Hajnoczi 1116eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 1117eabba580SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 1118eabba580SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next); 1119eabba580SStefan Hajnoczi } 11206f321e93SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) || 11216f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged) { 1122eabba580SStefan Hajnoczi return; /* wait for existing request to finish */ 1123eabba580SStefan Hajnoczi } 1124eabba580SStefan Hajnoczi 1125eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 1126eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 1127eabba580SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 1128eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1129eabba580SStefan Hajnoczi 11300d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) { 113101979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK; 113201979a98SStefan Hajnoczi qed_write_header(s, qed_aio_write_prefill, acb); 11330d09c797SStefan Hajnoczi } else { 11340d09c797SStefan Hajnoczi qed_aio_write_prefill(acb, 0); 11350d09c797SStefan Hajnoczi } 1136eabba580SStefan Hajnoczi } 1137eabba580SStefan Hajnoczi 1138eabba580SStefan Hajnoczi /** 1139eabba580SStefan Hajnoczi * Write data cluster in place 1140eabba580SStefan Hajnoczi * 1141eabba580SStefan Hajnoczi * @acb: Write request 1142eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1143eabba580SStefan Hajnoczi * @len: Length in bytes 1144eabba580SStefan Hajnoczi * 1145eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 1146eabba580SStefan Hajnoczi */ 1147eabba580SStefan Hajnoczi static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) 1148eabba580SStefan Hajnoczi { 1149eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 1150eabba580SStefan Hajnoczi acb->cur_cluster = offset; 1151eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1152eabba580SStefan Hajnoczi 1153eabba580SStefan Hajnoczi /* Do the actual write */ 1154eabba580SStefan Hajnoczi qed_aio_write_main(acb, 0); 1155eabba580SStefan Hajnoczi } 1156eabba580SStefan Hajnoczi 1157eabba580SStefan Hajnoczi /** 1158eabba580SStefan Hajnoczi * Write data cluster 1159eabba580SStefan Hajnoczi * 1160eabba580SStefan Hajnoczi * @opaque: Write request 1161eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1162eabba580SStefan Hajnoczi * or -errno 1163eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1164eabba580SStefan Hajnoczi * @len: Length in bytes 1165eabba580SStefan Hajnoczi * 1166eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1167eabba580SStefan Hajnoczi */ 1168eabba580SStefan Hajnoczi static void qed_aio_write_data(void *opaque, int ret, 1169eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1170eabba580SStefan Hajnoczi { 1171eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1172eabba580SStefan Hajnoczi 1173eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1174eabba580SStefan Hajnoczi 1175eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 1176eabba580SStefan Hajnoczi 1177eabba580SStefan Hajnoczi switch (ret) { 1178eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 1179eabba580SStefan Hajnoczi qed_aio_write_inplace(acb, offset, len); 1180eabba580SStefan Hajnoczi break; 1181eabba580SStefan Hajnoczi 1182eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 1183eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 118421df65b6SAnthony Liguori case QED_CLUSTER_ZERO: 1185eabba580SStefan Hajnoczi qed_aio_write_alloc(acb, len); 1186eabba580SStefan Hajnoczi break; 1187eabba580SStefan Hajnoczi 1188eabba580SStefan Hajnoczi default: 1189eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1190eabba580SStefan Hajnoczi break; 1191eabba580SStefan Hajnoczi } 1192eabba580SStefan Hajnoczi } 1193eabba580SStefan Hajnoczi 1194eabba580SStefan Hajnoczi /** 1195eabba580SStefan Hajnoczi * Read data cluster 1196eabba580SStefan Hajnoczi * 1197eabba580SStefan Hajnoczi * @opaque: Read request 1198eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 1199eabba580SStefan Hajnoczi * or -errno 1200eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1201eabba580SStefan Hajnoczi * @len: Length in bytes 1202eabba580SStefan Hajnoczi * 1203eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 1204eabba580SStefan Hajnoczi */ 1205eabba580SStefan Hajnoczi static void qed_aio_read_data(void *opaque, int ret, 1206eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1207eabba580SStefan Hajnoczi { 1208eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1209eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1210eabba580SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 1211eabba580SStefan Hajnoczi BlockDriverAIOCB *file_acb; 1212eabba580SStefan Hajnoczi 1213eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 1214eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 1215eabba580SStefan Hajnoczi 1216eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 1217eabba580SStefan Hajnoczi 1218eabba580SStefan Hajnoczi if (ret < 0) { 1219eabba580SStefan Hajnoczi goto err; 1220eabba580SStefan Hajnoczi } 1221eabba580SStefan Hajnoczi 1222eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1223eabba580SStefan Hajnoczi 122421df65b6SAnthony Liguori /* Handle zero cluster and backing file reads */ 122521df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) { 122621df65b6SAnthony Liguori qemu_iovec_memset(&acb->cur_qiov, 0, acb->cur_qiov.size); 122721df65b6SAnthony Liguori qed_aio_next_io(acb, 0); 122821df65b6SAnthony Liguori return; 122921df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) { 1230eabba580SStefan Hajnoczi qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1231eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1232eabba580SStefan Hajnoczi return; 1233eabba580SStefan Hajnoczi } 1234eabba580SStefan Hajnoczi 1235eabba580SStefan Hajnoczi BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 1236eabba580SStefan Hajnoczi file_acb = bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE, 1237eabba580SStefan Hajnoczi &acb->cur_qiov, 1238eabba580SStefan Hajnoczi acb->cur_qiov.size / BDRV_SECTOR_SIZE, 1239eabba580SStefan Hajnoczi qed_aio_next_io, acb); 1240eabba580SStefan Hajnoczi if (!file_acb) { 1241eabba580SStefan Hajnoczi ret = -EIO; 1242eabba580SStefan Hajnoczi goto err; 1243eabba580SStefan Hajnoczi } 1244eabba580SStefan Hajnoczi return; 1245eabba580SStefan Hajnoczi 1246eabba580SStefan Hajnoczi err: 1247eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1248eabba580SStefan Hajnoczi } 1249eabba580SStefan Hajnoczi 1250eabba580SStefan Hajnoczi /** 1251eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1252eabba580SStefan Hajnoczi */ 1253eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret) 1254eabba580SStefan Hajnoczi { 1255eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1256eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1257eabba580SStefan Hajnoczi QEDFindClusterFunc *io_fn = 1258eabba580SStefan Hajnoczi acb->is_write ? qed_aio_write_data : qed_aio_read_data; 1259eabba580SStefan Hajnoczi 1260eabba580SStefan Hajnoczi trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size); 1261eabba580SStefan Hajnoczi 1262eabba580SStefan Hajnoczi /* Handle I/O error */ 1263eabba580SStefan Hajnoczi if (ret) { 1264eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1265eabba580SStefan Hajnoczi return; 1266eabba580SStefan Hajnoczi } 1267eabba580SStefan Hajnoczi 1268eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1269eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1270eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1271eabba580SStefan Hajnoczi 1272eabba580SStefan Hajnoczi /* Complete request */ 1273eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 1274eabba580SStefan Hajnoczi qed_aio_complete(acb, 0); 1275eabba580SStefan Hajnoczi return; 1276eabba580SStefan Hajnoczi } 1277eabba580SStefan Hajnoczi 1278eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 1279eabba580SStefan Hajnoczi qed_find_cluster(s, &acb->request, 1280eabba580SStefan Hajnoczi acb->cur_pos, acb->end_pos - acb->cur_pos, 1281eabba580SStefan Hajnoczi io_fn, acb); 1282eabba580SStefan Hajnoczi } 1283eabba580SStefan Hajnoczi 1284eabba580SStefan Hajnoczi static BlockDriverAIOCB *qed_aio_setup(BlockDriverState *bs, 1285eabba580SStefan Hajnoczi int64_t sector_num, 1286eabba580SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1287eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, 1288eabba580SStefan Hajnoczi void *opaque, bool is_write) 1289eabba580SStefan Hajnoczi { 1290eabba580SStefan Hajnoczi QEDAIOCB *acb = qemu_aio_get(&qed_aio_pool, bs, cb, opaque); 1291eabba580SStefan Hajnoczi 1292eabba580SStefan Hajnoczi trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, 1293eabba580SStefan Hajnoczi opaque, is_write); 1294eabba580SStefan Hajnoczi 1295eabba580SStefan Hajnoczi acb->is_write = is_write; 1296eabba580SStefan Hajnoczi acb->finished = NULL; 1297eabba580SStefan Hajnoczi acb->qiov = qiov; 1298eabba580SStefan Hajnoczi acb->qiov_offset = 0; 1299eabba580SStefan Hajnoczi acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 1300eabba580SStefan Hajnoczi acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE; 1301eabba580SStefan Hajnoczi acb->request.l2_table = NULL; 1302eabba580SStefan Hajnoczi qemu_iovec_init(&acb->cur_qiov, qiov->niov); 1303eabba580SStefan Hajnoczi 1304eabba580SStefan Hajnoczi /* Start request */ 1305eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 1306eabba580SStefan Hajnoczi return &acb->common; 1307eabba580SStefan Hajnoczi } 1308eabba580SStefan Hajnoczi 130975411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs, 131075411d23SStefan Hajnoczi int64_t sector_num, 131175411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 131275411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 131375411d23SStefan Hajnoczi void *opaque) 131475411d23SStefan Hajnoczi { 1315eabba580SStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, false); 131675411d23SStefan Hajnoczi } 131775411d23SStefan Hajnoczi 131875411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs, 131975411d23SStefan Hajnoczi int64_t sector_num, 132075411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 132175411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 132275411d23SStefan Hajnoczi void *opaque) 132375411d23SStefan Hajnoczi { 1324eabba580SStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, true); 132575411d23SStefan Hajnoczi } 132675411d23SStefan Hajnoczi 132775411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_flush(BlockDriverState *bs, 132875411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 132975411d23SStefan Hajnoczi void *opaque) 133075411d23SStefan Hajnoczi { 133175411d23SStefan Hajnoczi return bdrv_aio_flush(bs->file, cb, opaque); 133275411d23SStefan Hajnoczi } 133375411d23SStefan Hajnoczi 133475411d23SStefan Hajnoczi static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset) 133575411d23SStefan Hajnoczi { 1336*77a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 1337*77a5a000SStefan Hajnoczi uint64_t old_image_size; 1338*77a5a000SStefan Hajnoczi int ret; 1339*77a5a000SStefan Hajnoczi 1340*77a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size, 1341*77a5a000SStefan Hajnoczi s->header.table_size)) { 1342*77a5a000SStefan Hajnoczi return -EINVAL; 1343*77a5a000SStefan Hajnoczi } 1344*77a5a000SStefan Hajnoczi 1345*77a5a000SStefan Hajnoczi /* Shrinking is currently not supported */ 1346*77a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) { 134775411d23SStefan Hajnoczi return -ENOTSUP; 134875411d23SStefan Hajnoczi } 134975411d23SStefan Hajnoczi 1350*77a5a000SStefan Hajnoczi old_image_size = s->header.image_size; 1351*77a5a000SStefan Hajnoczi s->header.image_size = offset; 1352*77a5a000SStefan Hajnoczi ret = qed_write_header_sync(s); 1353*77a5a000SStefan Hajnoczi if (ret < 0) { 1354*77a5a000SStefan Hajnoczi s->header.image_size = old_image_size; 1355*77a5a000SStefan Hajnoczi } 1356*77a5a000SStefan Hajnoczi return ret; 1357*77a5a000SStefan Hajnoczi } 1358*77a5a000SStefan Hajnoczi 135975411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs) 136075411d23SStefan Hajnoczi { 136175411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 136275411d23SStefan Hajnoczi return s->header.image_size; 136375411d23SStefan Hajnoczi } 136475411d23SStefan Hajnoczi 136575411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 136675411d23SStefan Hajnoczi { 136775411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 136875411d23SStefan Hajnoczi 136975411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi)); 137075411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size; 137175411d23SStefan Hajnoczi return 0; 137275411d23SStefan Hajnoczi } 137375411d23SStefan Hajnoczi 137475411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 137575411d23SStefan Hajnoczi const char *backing_file, 137675411d23SStefan Hajnoczi const char *backing_fmt) 137775411d23SStefan Hajnoczi { 137875411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 137975411d23SStefan Hajnoczi QEDHeader new_header, le_header; 138075411d23SStefan Hajnoczi void *buffer; 138175411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 138275411d23SStefan Hajnoczi int ret; 138375411d23SStefan Hajnoczi 138475411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 138575411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 138675411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 138775411d23SStefan Hajnoczi * add a new string. 138875411d23SStefan Hajnoczi */ 138975411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 139075411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 139175411d23SStefan Hajnoczi return -ENOTSUP; 139275411d23SStefan Hajnoczi } 139375411d23SStefan Hajnoczi 139475411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 139575411d23SStefan Hajnoczi 139675411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 139775411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 139875411d23SStefan Hajnoczi 139975411d23SStefan Hajnoczi /* Adjust feature flags */ 140075411d23SStefan Hajnoczi if (backing_file) { 140175411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 140275411d23SStefan Hajnoczi 140375411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 140475411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 140575411d23SStefan Hajnoczi } 140675411d23SStefan Hajnoczi } 140775411d23SStefan Hajnoczi 140875411d23SStefan Hajnoczi /* Calculate new header size */ 140975411d23SStefan Hajnoczi backing_file_len = 0; 141075411d23SStefan Hajnoczi 141175411d23SStefan Hajnoczi if (backing_file) { 141275411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 141375411d23SStefan Hajnoczi } 141475411d23SStefan Hajnoczi 141575411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 141675411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 141775411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 141875411d23SStefan Hajnoczi buffer_len += backing_file_len; 141975411d23SStefan Hajnoczi 142075411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 142175411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 142275411d23SStefan Hajnoczi return -ENOSPC; 142375411d23SStefan Hajnoczi } 142475411d23SStefan Hajnoczi 142575411d23SStefan Hajnoczi /* Prepare new header */ 142675411d23SStefan Hajnoczi buffer = qemu_malloc(buffer_len); 142775411d23SStefan Hajnoczi 142875411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 142975411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 143075411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 143175411d23SStefan Hajnoczi 143275411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 143375411d23SStefan Hajnoczi buffer_len += backing_file_len; 143475411d23SStefan Hajnoczi 143575411d23SStefan Hajnoczi /* Write new header */ 143675411d23SStefan Hajnoczi ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); 143775411d23SStefan Hajnoczi qemu_free(buffer); 143875411d23SStefan Hajnoczi if (ret == 0) { 143975411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 144075411d23SStefan Hajnoczi } 144175411d23SStefan Hajnoczi return ret; 144275411d23SStefan Hajnoczi } 144375411d23SStefan Hajnoczi 144475411d23SStefan Hajnoczi static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result) 144575411d23SStefan Hajnoczi { 144601979a98SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 144701979a98SStefan Hajnoczi 144801979a98SStefan Hajnoczi return qed_check(s, result, false); 144975411d23SStefan Hajnoczi } 145075411d23SStefan Hajnoczi 145175411d23SStefan Hajnoczi static QEMUOptionParameter qed_create_options[] = { 145275411d23SStefan Hajnoczi { 145375411d23SStefan Hajnoczi .name = BLOCK_OPT_SIZE, 145475411d23SStefan Hajnoczi .type = OPT_SIZE, 145575411d23SStefan Hajnoczi .help = "Virtual disk size (in bytes)" 145675411d23SStefan Hajnoczi }, { 145775411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FILE, 145875411d23SStefan Hajnoczi .type = OPT_STRING, 145975411d23SStefan Hajnoczi .help = "File name of a base image" 146075411d23SStefan Hajnoczi }, { 146175411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FMT, 146275411d23SStefan Hajnoczi .type = OPT_STRING, 146375411d23SStefan Hajnoczi .help = "Image format of the base image" 146475411d23SStefan Hajnoczi }, { 146575411d23SStefan Hajnoczi .name = BLOCK_OPT_CLUSTER_SIZE, 146675411d23SStefan Hajnoczi .type = OPT_SIZE, 146775411d23SStefan Hajnoczi .help = "Cluster size (in bytes)" 146875411d23SStefan Hajnoczi }, { 146975411d23SStefan Hajnoczi .name = BLOCK_OPT_TABLE_SIZE, 147075411d23SStefan Hajnoczi .type = OPT_SIZE, 147175411d23SStefan Hajnoczi .help = "L1/L2 table size (in clusters)" 147275411d23SStefan Hajnoczi }, 147375411d23SStefan Hajnoczi { /* end of list */ } 147475411d23SStefan Hajnoczi }; 147575411d23SStefan Hajnoczi 147675411d23SStefan Hajnoczi static BlockDriver bdrv_qed = { 147775411d23SStefan Hajnoczi .format_name = "qed", 147875411d23SStefan Hajnoczi .instance_size = sizeof(BDRVQEDState), 147975411d23SStefan Hajnoczi .create_options = qed_create_options, 148075411d23SStefan Hajnoczi 148175411d23SStefan Hajnoczi .bdrv_probe = bdrv_qed_probe, 148275411d23SStefan Hajnoczi .bdrv_open = bdrv_qed_open, 148375411d23SStefan Hajnoczi .bdrv_close = bdrv_qed_close, 148475411d23SStefan Hajnoczi .bdrv_create = bdrv_qed_create, 148575411d23SStefan Hajnoczi .bdrv_flush = bdrv_qed_flush, 148675411d23SStefan Hajnoczi .bdrv_is_allocated = bdrv_qed_is_allocated, 148775411d23SStefan Hajnoczi .bdrv_make_empty = bdrv_qed_make_empty, 148875411d23SStefan Hajnoczi .bdrv_aio_readv = bdrv_qed_aio_readv, 148975411d23SStefan Hajnoczi .bdrv_aio_writev = bdrv_qed_aio_writev, 149075411d23SStefan Hajnoczi .bdrv_aio_flush = bdrv_qed_aio_flush, 149175411d23SStefan Hajnoczi .bdrv_truncate = bdrv_qed_truncate, 149275411d23SStefan Hajnoczi .bdrv_getlength = bdrv_qed_getlength, 149375411d23SStefan Hajnoczi .bdrv_get_info = bdrv_qed_get_info, 149475411d23SStefan Hajnoczi .bdrv_change_backing_file = bdrv_qed_change_backing_file, 149575411d23SStefan Hajnoczi .bdrv_check = bdrv_qed_check, 149675411d23SStefan Hajnoczi }; 149775411d23SStefan Hajnoczi 149875411d23SStefan Hajnoczi static void bdrv_qed_init(void) 149975411d23SStefan Hajnoczi { 150075411d23SStefan Hajnoczi bdrv_register(&bdrv_qed); 150175411d23SStefan Hajnoczi } 150275411d23SStefan Hajnoczi 150375411d23SStefan Hajnoczi block_init(bdrv_qed_init); 1504