175411d23SStefan Hajnoczi /* 275411d23SStefan Hajnoczi * QEMU Enhanced Disk Format 375411d23SStefan Hajnoczi * 475411d23SStefan Hajnoczi * Copyright IBM, Corp. 2010 575411d23SStefan Hajnoczi * 675411d23SStefan Hajnoczi * Authors: 775411d23SStefan Hajnoczi * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 875411d23SStefan Hajnoczi * Anthony Liguori <aliguori@us.ibm.com> 975411d23SStefan Hajnoczi * 1075411d23SStefan Hajnoczi * This work is licensed under the terms of the GNU LGPL, version 2 or later. 1175411d23SStefan Hajnoczi * See the COPYING.LIB file in the top-level directory. 1275411d23SStefan Hajnoczi * 1375411d23SStefan Hajnoczi */ 1475411d23SStefan Hajnoczi 1580c71a24SPeter Maydell #include "qemu/osdep.h" 16da34e65cSMarkus Armbruster #include "qapi/error.h" 171de7afc9SPaolo Bonzini #include "qemu/timer.h" 1858369e22SPaolo Bonzini #include "qemu/bswap.h" 19eabba580SStefan Hajnoczi #include "trace.h" 2075411d23SStefan Hajnoczi #include "qed.h" 217b1b5d19SPaolo Bonzini #include "qapi/qmp/qerror.h" 228a56fdadSKevin Wolf #include "sysemu/block-backend.h" 2375411d23SStefan Hajnoczi 2475411d23SStefan Hajnoczi static int bdrv_qed_probe(const uint8_t *buf, int buf_size, 2575411d23SStefan Hajnoczi const char *filename) 2675411d23SStefan Hajnoczi { 2775411d23SStefan Hajnoczi const QEDHeader *header = (const QEDHeader *)buf; 2875411d23SStefan Hajnoczi 2975411d23SStefan Hajnoczi if (buf_size < sizeof(*header)) { 3075411d23SStefan Hajnoczi return 0; 3175411d23SStefan Hajnoczi } 3275411d23SStefan Hajnoczi if (le32_to_cpu(header->magic) != QED_MAGIC) { 3375411d23SStefan Hajnoczi return 0; 3475411d23SStefan Hajnoczi } 3575411d23SStefan Hajnoczi return 100; 3675411d23SStefan Hajnoczi } 3775411d23SStefan Hajnoczi 3875411d23SStefan Hajnoczi /** 3975411d23SStefan Hajnoczi * Check whether an image format is raw 4075411d23SStefan Hajnoczi * 4175411d23SStefan Hajnoczi * @fmt: Backing file format, may be NULL 4275411d23SStefan Hajnoczi */ 4375411d23SStefan Hajnoczi static bool qed_fmt_is_raw(const char *fmt) 4475411d23SStefan Hajnoczi { 4575411d23SStefan Hajnoczi return fmt && strcmp(fmt, "raw") == 0; 4675411d23SStefan Hajnoczi } 4775411d23SStefan Hajnoczi 4875411d23SStefan Hajnoczi static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) 4975411d23SStefan Hajnoczi { 5075411d23SStefan Hajnoczi cpu->magic = le32_to_cpu(le->magic); 5175411d23SStefan Hajnoczi cpu->cluster_size = le32_to_cpu(le->cluster_size); 5275411d23SStefan Hajnoczi cpu->table_size = le32_to_cpu(le->table_size); 5375411d23SStefan Hajnoczi cpu->header_size = le32_to_cpu(le->header_size); 5475411d23SStefan Hajnoczi cpu->features = le64_to_cpu(le->features); 5575411d23SStefan Hajnoczi cpu->compat_features = le64_to_cpu(le->compat_features); 5675411d23SStefan Hajnoczi cpu->autoclear_features = le64_to_cpu(le->autoclear_features); 5775411d23SStefan Hajnoczi cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); 5875411d23SStefan Hajnoczi cpu->image_size = le64_to_cpu(le->image_size); 5975411d23SStefan Hajnoczi cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); 6075411d23SStefan Hajnoczi cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); 6175411d23SStefan Hajnoczi } 6275411d23SStefan Hajnoczi 6375411d23SStefan Hajnoczi static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) 6475411d23SStefan Hajnoczi { 6575411d23SStefan Hajnoczi le->magic = cpu_to_le32(cpu->magic); 6675411d23SStefan Hajnoczi le->cluster_size = cpu_to_le32(cpu->cluster_size); 6775411d23SStefan Hajnoczi le->table_size = cpu_to_le32(cpu->table_size); 6875411d23SStefan Hajnoczi le->header_size = cpu_to_le32(cpu->header_size); 6975411d23SStefan Hajnoczi le->features = cpu_to_le64(cpu->features); 7075411d23SStefan Hajnoczi le->compat_features = cpu_to_le64(cpu->compat_features); 7175411d23SStefan Hajnoczi le->autoclear_features = cpu_to_le64(cpu->autoclear_features); 7275411d23SStefan Hajnoczi le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); 7375411d23SStefan Hajnoczi le->image_size = cpu_to_le64(cpu->image_size); 7475411d23SStefan Hajnoczi le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); 7575411d23SStefan Hajnoczi le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); 7675411d23SStefan Hajnoczi } 7775411d23SStefan Hajnoczi 78b10170acSStefan Hajnoczi int qed_write_header_sync(BDRVQEDState *s) 7975411d23SStefan Hajnoczi { 8075411d23SStefan Hajnoczi QEDHeader le; 8175411d23SStefan Hajnoczi int ret; 8275411d23SStefan Hajnoczi 8375411d23SStefan Hajnoczi qed_header_cpu_to_le(&s->header, &le); 84d9ca2ea2SKevin Wolf ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le)); 8575411d23SStefan Hajnoczi if (ret != sizeof(le)) { 8675411d23SStefan Hajnoczi return ret; 8775411d23SStefan Hajnoczi } 8875411d23SStefan Hajnoczi return 0; 8975411d23SStefan Hajnoczi } 9075411d23SStefan Hajnoczi 9101979a98SStefan Hajnoczi /** 9201979a98SStefan Hajnoczi * Update header in-place (does not rewrite backing filename or other strings) 9301979a98SStefan Hajnoczi * 9401979a98SStefan Hajnoczi * This function only updates known header fields in-place and does not affect 9501979a98SStefan Hajnoczi * extra data after the QED header. 9601979a98SStefan Hajnoczi */ 9787f0d882SKevin Wolf static int coroutine_fn qed_write_header(BDRVQEDState *s) 9801979a98SStefan Hajnoczi { 9901979a98SStefan Hajnoczi /* We must write full sectors for O_DIRECT but cannot necessarily generate 10001979a98SStefan Hajnoczi * the data following the header if an unrecognized compat feature is 10101979a98SStefan Hajnoczi * active. Therefore, first read the sectors containing the header, update 10201979a98SStefan Hajnoczi * them, and write back. 10301979a98SStefan Hajnoczi */ 10401979a98SStefan Hajnoczi 105c41a73ffSLaurent Vivier int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE); 10601979a98SStefan Hajnoczi size_t len = nsectors * BDRV_SECTOR_SIZE; 1077076309aSKevin Wolf uint8_t *buf; 1087076309aSKevin Wolf struct iovec iov; 1097076309aSKevin Wolf QEMUIOVector qiov; 1107076309aSKevin Wolf int ret; 11101979a98SStefan Hajnoczi 1127076309aSKevin Wolf buf = qemu_blockalign(s->bs, len); 1137076309aSKevin Wolf iov = (struct iovec) { 1147076309aSKevin Wolf .iov_base = buf, 1157076309aSKevin Wolf .iov_len = len, 1167076309aSKevin Wolf }; 1177076309aSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 11801979a98SStefan Hajnoczi 119*0f714ec7SKevin Wolf ret = bdrv_co_preadv(s->bs->file, 0, qiov.size, &qiov, 0); 1207076309aSKevin Wolf if (ret < 0) { 1217076309aSKevin Wolf goto out; 1227076309aSKevin Wolf } 1237076309aSKevin Wolf 1247076309aSKevin Wolf /* Update header */ 1257076309aSKevin Wolf qed_header_cpu_to_le(&s->header, (QEDHeader *) buf); 1267076309aSKevin Wolf 127*0f714ec7SKevin Wolf ret = bdrv_co_pwritev(s->bs->file, 0, qiov.size, &qiov, 0); 1287076309aSKevin Wolf if (ret < 0) { 1297076309aSKevin Wolf goto out; 1307076309aSKevin Wolf } 1317076309aSKevin Wolf 1327076309aSKevin Wolf ret = 0; 1337076309aSKevin Wolf out: 1347076309aSKevin Wolf qemu_vfree(buf); 135f13d712bSKevin Wolf return ret; 13601979a98SStefan Hajnoczi } 13701979a98SStefan Hajnoczi 13875411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 13975411d23SStefan Hajnoczi { 14075411d23SStefan Hajnoczi uint64_t table_entries; 14175411d23SStefan Hajnoczi uint64_t l2_size; 14275411d23SStefan Hajnoczi 14375411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t); 14475411d23SStefan Hajnoczi l2_size = table_entries * cluster_size; 14575411d23SStefan Hajnoczi 14675411d23SStefan Hajnoczi return l2_size * table_entries; 14775411d23SStefan Hajnoczi } 14875411d23SStefan Hajnoczi 14975411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size) 15075411d23SStefan Hajnoczi { 15175411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE || 15275411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) { 15375411d23SStefan Hajnoczi return false; 15475411d23SStefan Hajnoczi } 15575411d23SStefan Hajnoczi if (cluster_size & (cluster_size - 1)) { 15675411d23SStefan Hajnoczi return false; /* not power of 2 */ 15775411d23SStefan Hajnoczi } 15875411d23SStefan Hajnoczi return true; 15975411d23SStefan Hajnoczi } 16075411d23SStefan Hajnoczi 16175411d23SStefan Hajnoczi static bool qed_is_table_size_valid(uint32_t table_size) 16275411d23SStefan Hajnoczi { 16375411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE || 16475411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) { 16575411d23SStefan Hajnoczi return false; 16675411d23SStefan Hajnoczi } 16775411d23SStefan Hajnoczi if (table_size & (table_size - 1)) { 16875411d23SStefan Hajnoczi return false; /* not power of 2 */ 16975411d23SStefan Hajnoczi } 17075411d23SStefan Hajnoczi return true; 17175411d23SStefan Hajnoczi } 17275411d23SStefan Hajnoczi 17375411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 17475411d23SStefan Hajnoczi uint32_t table_size) 17575411d23SStefan Hajnoczi { 17675411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) { 17775411d23SStefan Hajnoczi return false; /* not multiple of sector size */ 17875411d23SStefan Hajnoczi } 17975411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) { 18075411d23SStefan Hajnoczi return false; /* image is too large */ 18175411d23SStefan Hajnoczi } 18275411d23SStefan Hajnoczi return true; 18375411d23SStefan Hajnoczi } 18475411d23SStefan Hajnoczi 18575411d23SStefan Hajnoczi /** 18675411d23SStefan Hajnoczi * Read a string of known length from the image file 18775411d23SStefan Hajnoczi * 18875411d23SStefan Hajnoczi * @file: Image file 18975411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes 19075411d23SStefan Hajnoczi * @n: String length in bytes 19175411d23SStefan Hajnoczi * @buf: Destination buffer 19275411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes 19375411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure 19475411d23SStefan Hajnoczi * 19575411d23SStefan Hajnoczi * The string is NUL-terminated. 19675411d23SStefan Hajnoczi */ 197cf2ab8fcSKevin Wolf static int qed_read_string(BdrvChild *file, uint64_t offset, size_t n, 19875411d23SStefan Hajnoczi char *buf, size_t buflen) 19975411d23SStefan Hajnoczi { 20075411d23SStefan Hajnoczi int ret; 20175411d23SStefan Hajnoczi if (n >= buflen) { 20275411d23SStefan Hajnoczi return -EINVAL; 20375411d23SStefan Hajnoczi } 20475411d23SStefan Hajnoczi ret = bdrv_pread(file, offset, buf, n); 20575411d23SStefan Hajnoczi if (ret < 0) { 20675411d23SStefan Hajnoczi return ret; 20775411d23SStefan Hajnoczi } 20875411d23SStefan Hajnoczi buf[n] = '\0'; 20975411d23SStefan Hajnoczi return 0; 21075411d23SStefan Hajnoczi } 21175411d23SStefan Hajnoczi 212eabba580SStefan Hajnoczi /** 213eabba580SStefan Hajnoczi * Allocate new clusters 214eabba580SStefan Hajnoczi * 215eabba580SStefan Hajnoczi * @s: QED state 216eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate 217eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster 218eabba580SStefan Hajnoczi * 219eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be 220eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image 221eabba580SStefan Hajnoczi * file. 222eabba580SStefan Hajnoczi */ 223eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 224eabba580SStefan Hajnoczi { 225eabba580SStefan Hajnoczi uint64_t offset = s->file_size; 226eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size; 227eabba580SStefan Hajnoczi return offset; 228eabba580SStefan Hajnoczi } 229eabba580SStefan Hajnoczi 230298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s) 231298800caSStefan Hajnoczi { 232298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */ 233298800caSStefan Hajnoczi return qemu_blockalign(s->bs, 234298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size); 235298800caSStefan Hajnoczi } 236298800caSStefan Hajnoczi 237eabba580SStefan Hajnoczi /** 238eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table 239eabba580SStefan Hajnoczi */ 240eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 241eabba580SStefan Hajnoczi { 242eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 243eabba580SStefan Hajnoczi 244eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s); 245eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 246eabba580SStefan Hajnoczi 247eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0, 248eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size); 249eabba580SStefan Hajnoczi return l2_table; 250eabba580SStefan Hajnoczi } 251eabba580SStefan Hajnoczi 2526f321e93SStefan Hajnoczi static void qed_plug_allocating_write_reqs(BDRVQEDState *s) 2536f321e93SStefan Hajnoczi { 2546f321e93SStefan Hajnoczi assert(!s->allocating_write_reqs_plugged); 2556f321e93SStefan Hajnoczi 2566f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = true; 2576f321e93SStefan Hajnoczi } 2586f321e93SStefan Hajnoczi 2596f321e93SStefan Hajnoczi static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) 2606f321e93SStefan Hajnoczi { 2616f321e93SStefan Hajnoczi assert(s->allocating_write_reqs_plugged); 2626f321e93SStefan Hajnoczi 2636f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = false; 2640806c3b5SKevin Wolf qemu_co_enter_next(&s->allocating_write_reqs); 2656f321e93SStefan Hajnoczi } 2666f321e93SStefan Hajnoczi 26787f0d882SKevin Wolf static void coroutine_fn qed_need_check_timer_entry(void *opaque) 2686f321e93SStefan Hajnoczi { 2696f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 270c0e8f989SKevin Wolf int ret; 2716f321e93SStefan Hajnoczi 2726f321e93SStefan Hajnoczi /* The timer should only fire when allocating writes have drained */ 2730806c3b5SKevin Wolf assert(!s->allocating_acb); 2746f321e93SStefan Hajnoczi 2756f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s); 2766f321e93SStefan Hajnoczi 2772f47da5fSPaolo Bonzini qed_acquire(s); 2786f321e93SStefan Hajnoczi qed_plug_allocating_write_reqs(s); 2796f321e93SStefan Hajnoczi 2806f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */ 281c0e8f989SKevin Wolf ret = bdrv_co_flush(s->bs->file->bs); 2822f47da5fSPaolo Bonzini qed_release(s); 283c0e8f989SKevin Wolf if (ret < 0) { 284c0e8f989SKevin Wolf qed_unplug_allocating_write_reqs(s); 285c0e8f989SKevin Wolf return; 286c0e8f989SKevin Wolf } 287c0e8f989SKevin Wolf 288c0e8f989SKevin Wolf s->header.features &= ~QED_F_NEED_CHECK; 289c0e8f989SKevin Wolf ret = qed_write_header(s); 290c0e8f989SKevin Wolf (void) ret; 291c0e8f989SKevin Wolf 292c0e8f989SKevin Wolf qed_unplug_allocating_write_reqs(s); 293c0e8f989SKevin Wolf 294c0e8f989SKevin Wolf ret = bdrv_co_flush(s->bs); 295c0e8f989SKevin Wolf (void) ret; 296c0e8f989SKevin Wolf } 297c0e8f989SKevin Wolf 298c0e8f989SKevin Wolf static void qed_need_check_timer_cb(void *opaque) 299c0e8f989SKevin Wolf { 300c0e8f989SKevin Wolf Coroutine *co = qemu_coroutine_create(qed_need_check_timer_entry, opaque); 301c0e8f989SKevin Wolf qemu_coroutine_enter(co); 3022f47da5fSPaolo Bonzini } 3032f47da5fSPaolo Bonzini 3042f47da5fSPaolo Bonzini void qed_acquire(BDRVQEDState *s) 3052f47da5fSPaolo Bonzini { 3062f47da5fSPaolo Bonzini aio_context_acquire(bdrv_get_aio_context(s->bs)); 3072f47da5fSPaolo Bonzini } 3082f47da5fSPaolo Bonzini 3092f47da5fSPaolo Bonzini void qed_release(BDRVQEDState *s) 3102f47da5fSPaolo Bonzini { 3112f47da5fSPaolo Bonzini aio_context_release(bdrv_get_aio_context(s->bs)); 3126f321e93SStefan Hajnoczi } 3136f321e93SStefan Hajnoczi 3146f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s) 3156f321e93SStefan Hajnoczi { 3166f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s); 3176f321e93SStefan Hajnoczi 318bc72ad67SAlex Bligh /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for 3196f321e93SStefan Hajnoczi * migration. 3206f321e93SStefan Hajnoczi */ 321bc72ad67SAlex Bligh timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 32273bcb24dSRutuja Shah NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT); 3236f321e93SStefan Hajnoczi } 3246f321e93SStefan Hajnoczi 3256f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */ 3266f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s) 3276f321e93SStefan Hajnoczi { 3286f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s); 329bc72ad67SAlex Bligh timer_del(s->need_check_timer); 3306f321e93SStefan Hajnoczi } 3316f321e93SStefan Hajnoczi 332a8c868c3SStefan Hajnoczi static void bdrv_qed_detach_aio_context(BlockDriverState *bs) 333a8c868c3SStefan Hajnoczi { 334a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 335a8c868c3SStefan Hajnoczi 336a8c868c3SStefan Hajnoczi qed_cancel_need_check_timer(s); 337a8c868c3SStefan Hajnoczi timer_free(s->need_check_timer); 338a8c868c3SStefan Hajnoczi } 339a8c868c3SStefan Hajnoczi 340a8c868c3SStefan Hajnoczi static void bdrv_qed_attach_aio_context(BlockDriverState *bs, 341a8c868c3SStefan Hajnoczi AioContext *new_context) 342a8c868c3SStefan Hajnoczi { 343a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 344a8c868c3SStefan Hajnoczi 345a8c868c3SStefan Hajnoczi s->need_check_timer = aio_timer_new(new_context, 346a8c868c3SStefan Hajnoczi QEMU_CLOCK_VIRTUAL, SCALE_NS, 347a8c868c3SStefan Hajnoczi qed_need_check_timer_cb, s); 348a8c868c3SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 349a8c868c3SStefan Hajnoczi qed_start_need_check_timer(s); 350a8c868c3SStefan Hajnoczi } 351a8c868c3SStefan Hajnoczi } 352a8c868c3SStefan Hajnoczi 3536653a73dSFam Zheng static void bdrv_qed_drain(BlockDriverState *bs) 3546653a73dSFam Zheng { 3556653a73dSFam Zheng BDRVQEDState *s = bs->opaque; 3566653a73dSFam Zheng 3576653a73dSFam Zheng /* Fire the timer immediately in order to start doing I/O as soon as the 3586653a73dSFam Zheng * header is flushed. 3596653a73dSFam Zheng */ 3606653a73dSFam Zheng if (s->need_check_timer && timer_pending(s->need_check_timer)) { 3616653a73dSFam Zheng qed_cancel_need_check_timer(s); 3626653a73dSFam Zheng qed_need_check_timer_cb(s); 3636653a73dSFam Zheng } 3646653a73dSFam Zheng } 3656653a73dSFam Zheng 3664e4bf5c4SKevin Wolf static int bdrv_qed_do_open(BlockDriverState *bs, QDict *options, int flags, 367015a1036SMax Reitz Error **errp) 36875411d23SStefan Hajnoczi { 36975411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 37075411d23SStefan Hajnoczi QEDHeader le_header; 37175411d23SStefan Hajnoczi int64_t file_size; 37275411d23SStefan Hajnoczi int ret; 37375411d23SStefan Hajnoczi 37475411d23SStefan Hajnoczi s->bs = bs; 3750806c3b5SKevin Wolf qemu_co_queue_init(&s->allocating_write_reqs); 37675411d23SStefan Hajnoczi 377cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); 37875411d23SStefan Hajnoczi if (ret < 0) { 37975411d23SStefan Hajnoczi return ret; 38075411d23SStefan Hajnoczi } 38175411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header); 38275411d23SStefan Hajnoczi 38375411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) { 38476abe407SPaolo Bonzini error_setg(errp, "Image not in QED format"); 38576abe407SPaolo Bonzini return -EINVAL; 38675411d23SStefan Hajnoczi } 38775411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) { 38810b758e8SKevin Wolf /* image uses unsupported feature bits */ 389a55448b3SMax Reitz error_setg(errp, "Unsupported QED features: %" PRIx64, 39010b758e8SKevin Wolf s->header.features & ~QED_FEATURE_MASK); 39110b758e8SKevin Wolf return -ENOTSUP; 39275411d23SStefan Hajnoczi } 39375411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 39475411d23SStefan Hajnoczi return -EINVAL; 39575411d23SStefan Hajnoczi } 39675411d23SStefan Hajnoczi 39775411d23SStefan Hajnoczi /* Round down file size to the last cluster */ 3989a4f4c31SKevin Wolf file_size = bdrv_getlength(bs->file->bs); 39975411d23SStefan Hajnoczi if (file_size < 0) { 40075411d23SStefan Hajnoczi return file_size; 40175411d23SStefan Hajnoczi } 40275411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size); 40375411d23SStefan Hajnoczi 40475411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) { 40575411d23SStefan Hajnoczi return -EINVAL; 40675411d23SStefan Hajnoczi } 40775411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size, 40875411d23SStefan Hajnoczi s->header.cluster_size, 40975411d23SStefan Hajnoczi s->header.table_size)) { 41075411d23SStefan Hajnoczi return -EINVAL; 41175411d23SStefan Hajnoczi } 41275411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 41375411d23SStefan Hajnoczi return -EINVAL; 41475411d23SStefan Hajnoczi } 41575411d23SStefan Hajnoczi 41675411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) / 41775411d23SStefan Hajnoczi sizeof(uint64_t); 418786a4ea8SStefan Hajnoczi s->l2_shift = ctz32(s->header.cluster_size); 41975411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1; 420786a4ea8SStefan Hajnoczi s->l1_shift = s->l2_shift + ctz32(s->table_nelems); 42175411d23SStefan Hajnoczi 4220adfa1edSStefan Hajnoczi /* Header size calculation must not overflow uint32_t */ 4230adfa1edSStefan Hajnoczi if (s->header.header_size > UINT32_MAX / s->header.cluster_size) { 4240adfa1edSStefan Hajnoczi return -EINVAL; 4250adfa1edSStefan Hajnoczi } 4260adfa1edSStefan Hajnoczi 42775411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) { 42875411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset + 42975411d23SStefan Hajnoczi s->header.backing_filename_size > 43075411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) { 43175411d23SStefan Hajnoczi return -EINVAL; 43275411d23SStefan Hajnoczi } 43375411d23SStefan Hajnoczi 434cf2ab8fcSKevin Wolf ret = qed_read_string(bs->file, s->header.backing_filename_offset, 43575411d23SStefan Hajnoczi s->header.backing_filename_size, bs->backing_file, 43675411d23SStefan Hajnoczi sizeof(bs->backing_file)); 43775411d23SStefan Hajnoczi if (ret < 0) { 43875411d23SStefan Hajnoczi return ret; 43975411d23SStefan Hajnoczi } 44075411d23SStefan Hajnoczi 44175411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 44275411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 44375411d23SStefan Hajnoczi } 44475411d23SStefan Hajnoczi } 44575411d23SStefan Hajnoczi 44675411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards 44775411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older 44875411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is 44975411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear 45075411d23SStefan Hajnoczi * feature is no longer valid. 45175411d23SStefan Hajnoczi */ 45275411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 45304c01a5cSKevin Wolf !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) { 45475411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 45575411d23SStefan Hajnoczi 45675411d23SStefan Hajnoczi ret = qed_write_header_sync(s); 45775411d23SStefan Hajnoczi if (ret) { 45875411d23SStefan Hajnoczi return ret; 45975411d23SStefan Hajnoczi } 46075411d23SStefan Hajnoczi 46175411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */ 4629a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 46375411d23SStefan Hajnoczi } 46475411d23SStefan Hajnoczi 465298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s); 466298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache); 467298800caSStefan Hajnoczi 468298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s); 469298800caSStefan Hajnoczi if (ret) { 47001979a98SStefan Hajnoczi goto out; 47101979a98SStefan Hajnoczi } 47201979a98SStefan Hajnoczi 47301979a98SStefan Hajnoczi /* If image was not closed cleanly, check consistency */ 474058f8f16SStefan Hajnoczi if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) { 47501979a98SStefan Hajnoczi /* Read-only images cannot be fixed. There is no risk of corruption 47601979a98SStefan Hajnoczi * since write operations are not possible. Therefore, allow 47701979a98SStefan Hajnoczi * potentially inconsistent images to be opened read-only. This can 47801979a98SStefan Hajnoczi * aid data recovery from an otherwise inconsistent image. 47901979a98SStefan Hajnoczi */ 4809a4f4c31SKevin Wolf if (!bdrv_is_read_only(bs->file->bs) && 48104c01a5cSKevin Wolf !(flags & BDRV_O_INACTIVE)) { 48201979a98SStefan Hajnoczi BdrvCheckResult result = {0}; 48301979a98SStefan Hajnoczi 48401979a98SStefan Hajnoczi ret = qed_check(s, &result, true); 4856f321e93SStefan Hajnoczi if (ret) { 4866f321e93SStefan Hajnoczi goto out; 4876f321e93SStefan Hajnoczi } 48801979a98SStefan Hajnoczi } 48901979a98SStefan Hajnoczi } 49001979a98SStefan Hajnoczi 491a8c868c3SStefan Hajnoczi bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs)); 4926f321e93SStefan Hajnoczi 49301979a98SStefan Hajnoczi out: 49401979a98SStefan Hajnoczi if (ret) { 495298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 496298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 497298800caSStefan Hajnoczi } 49875411d23SStefan Hajnoczi return ret; 49975411d23SStefan Hajnoczi } 50075411d23SStefan Hajnoczi 5014e4bf5c4SKevin Wolf static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags, 5024e4bf5c4SKevin Wolf Error **errp) 5034e4bf5c4SKevin Wolf { 5044e4bf5c4SKevin Wolf bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, 5054e4bf5c4SKevin Wolf false, errp); 5064e4bf5c4SKevin Wolf if (!bs->file) { 5074e4bf5c4SKevin Wolf return -EINVAL; 5084e4bf5c4SKevin Wolf } 5094e4bf5c4SKevin Wolf 5104e4bf5c4SKevin Wolf return bdrv_qed_do_open(bs, options, flags, errp); 5114e4bf5c4SKevin Wolf } 5124e4bf5c4SKevin Wolf 5133baca891SKevin Wolf static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp) 514d34682cdSKevin Wolf { 515d34682cdSKevin Wolf BDRVQEDState *s = bs->opaque; 516d34682cdSKevin Wolf 517cf081fcaSEric Blake bs->bl.pwrite_zeroes_alignment = s->header.cluster_size; 518d34682cdSKevin Wolf } 519d34682cdSKevin Wolf 520f9cb20f1SJeff Cody /* We have nothing to do for QED reopen, stubs just return 521f9cb20f1SJeff Cody * success */ 522f9cb20f1SJeff Cody static int bdrv_qed_reopen_prepare(BDRVReopenState *state, 523f9cb20f1SJeff Cody BlockReopenQueue *queue, Error **errp) 524f9cb20f1SJeff Cody { 525f9cb20f1SJeff Cody return 0; 526f9cb20f1SJeff Cody } 527f9cb20f1SJeff Cody 52875411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 52975411d23SStefan Hajnoczi { 530298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 531298800caSStefan Hajnoczi 532a8c868c3SStefan Hajnoczi bdrv_qed_detach_aio_context(bs); 5336f321e93SStefan Hajnoczi 53401979a98SStefan Hajnoczi /* Ensure writes reach stable storage */ 5359a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 53601979a98SStefan Hajnoczi 53701979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */ 53801979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 53901979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 54001979a98SStefan Hajnoczi qed_write_header_sync(s); 54101979a98SStefan Hajnoczi } 54201979a98SStefan Hajnoczi 543298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 544298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 54575411d23SStefan Hajnoczi } 54675411d23SStefan Hajnoczi 54775411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size, 54875411d23SStefan Hajnoczi uint64_t image_size, uint32_t table_size, 5490fea6b79SPaolo Bonzini const char *backing_file, const char *backing_fmt, 5504ab15590SChunyan Liu QemuOpts *opts, Error **errp) 55175411d23SStefan Hajnoczi { 55275411d23SStefan Hajnoczi QEDHeader header = { 55375411d23SStefan Hajnoczi .magic = QED_MAGIC, 55475411d23SStefan Hajnoczi .cluster_size = cluster_size, 55575411d23SStefan Hajnoczi .table_size = table_size, 55675411d23SStefan Hajnoczi .header_size = 1, 55775411d23SStefan Hajnoczi .features = 0, 55875411d23SStefan Hajnoczi .compat_features = 0, 55975411d23SStefan Hajnoczi .l1_table_offset = cluster_size, 56075411d23SStefan Hajnoczi .image_size = image_size, 56175411d23SStefan Hajnoczi }; 56275411d23SStefan Hajnoczi QEDHeader le_header; 56375411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 56475411d23SStefan Hajnoczi size_t l1_size = header.cluster_size * header.table_size; 56534b5d2c6SMax Reitz Error *local_err = NULL; 56675411d23SStefan Hajnoczi int ret = 0; 5678a56fdadSKevin Wolf BlockBackend *blk; 56875411d23SStefan Hajnoczi 5694ab15590SChunyan Liu ret = bdrv_create_file(filename, opts, &local_err); 57075411d23SStefan Hajnoczi if (ret < 0) { 5710fea6b79SPaolo Bonzini error_propagate(errp, local_err); 57275411d23SStefan Hajnoczi return ret; 57375411d23SStefan Hajnoczi } 57475411d23SStefan Hajnoczi 575efaa7c4eSMax Reitz blk = blk_new_open(filename, NULL, NULL, 57655880601SKevin Wolf BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, 57755880601SKevin Wolf &local_err); 5788a56fdadSKevin Wolf if (blk == NULL) { 5790fea6b79SPaolo Bonzini error_propagate(errp, local_err); 5808a56fdadSKevin Wolf return -EIO; 58175411d23SStefan Hajnoczi } 58275411d23SStefan Hajnoczi 5838a56fdadSKevin Wolf blk_set_allow_write_beyond_eof(blk, true); 5848a56fdadSKevin Wolf 585c743849bSStefan Hajnoczi /* File must start empty and grow, check truncate is supported */ 586ed3d2ec9SMax Reitz ret = blk_truncate(blk, 0, errp); 587c743849bSStefan Hajnoczi if (ret < 0) { 588c743849bSStefan Hajnoczi goto out; 589c743849bSStefan Hajnoczi } 590c743849bSStefan Hajnoczi 59175411d23SStefan Hajnoczi if (backing_file) { 59275411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 59375411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 59475411d23SStefan Hajnoczi header.backing_filename_size = strlen(backing_file); 59575411d23SStefan Hajnoczi 59675411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 59775411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 59875411d23SStefan Hajnoczi } 59975411d23SStefan Hajnoczi } 60075411d23SStefan Hajnoczi 60175411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 6028341f00dSEric Blake ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0); 60375411d23SStefan Hajnoczi if (ret < 0) { 60475411d23SStefan Hajnoczi goto out; 60575411d23SStefan Hajnoczi } 6068a56fdadSKevin Wolf ret = blk_pwrite(blk, sizeof(le_header), backing_file, 6078341f00dSEric Blake header.backing_filename_size, 0); 60875411d23SStefan Hajnoczi if (ret < 0) { 60975411d23SStefan Hajnoczi goto out; 61075411d23SStefan Hajnoczi } 61175411d23SStefan Hajnoczi 6127267c094SAnthony Liguori l1_table = g_malloc0(l1_size); 6138341f00dSEric Blake ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0); 61475411d23SStefan Hajnoczi if (ret < 0) { 61575411d23SStefan Hajnoczi goto out; 61675411d23SStefan Hajnoczi } 61775411d23SStefan Hajnoczi 61875411d23SStefan Hajnoczi ret = 0; /* success */ 61975411d23SStefan Hajnoczi out: 6207267c094SAnthony Liguori g_free(l1_table); 6218a56fdadSKevin Wolf blk_unref(blk); 62275411d23SStefan Hajnoczi return ret; 62375411d23SStefan Hajnoczi } 62475411d23SStefan Hajnoczi 6257ab74849SChunyan Liu static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp) 62675411d23SStefan Hajnoczi { 62775411d23SStefan Hajnoczi uint64_t image_size = 0; 62875411d23SStefan Hajnoczi uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 62975411d23SStefan Hajnoczi uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 6307ab74849SChunyan Liu char *backing_file = NULL; 6317ab74849SChunyan Liu char *backing_fmt = NULL; 6327ab74849SChunyan Liu int ret; 63375411d23SStefan Hajnoczi 634c2eb918eSHu Tao image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 635c2eb918eSHu Tao BDRV_SECTOR_SIZE); 6367ab74849SChunyan Liu backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); 6377ab74849SChunyan Liu backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); 6387ab74849SChunyan Liu cluster_size = qemu_opt_get_size_del(opts, 6397ab74849SChunyan Liu BLOCK_OPT_CLUSTER_SIZE, 6407ab74849SChunyan Liu QED_DEFAULT_CLUSTER_SIZE); 6417ab74849SChunyan Liu table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE, 6427ab74849SChunyan Liu QED_DEFAULT_TABLE_SIZE); 64375411d23SStefan Hajnoczi 64475411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(cluster_size)) { 6455ff679b4SAakriti Gupta error_setg(errp, "QED cluster size must be within range [%u, %u] " 6465ff679b4SAakriti Gupta "and power of 2", 64775411d23SStefan Hajnoczi QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 6487ab74849SChunyan Liu ret = -EINVAL; 6497ab74849SChunyan Liu goto finish; 65075411d23SStefan Hajnoczi } 65175411d23SStefan Hajnoczi if (!qed_is_table_size_valid(table_size)) { 6525ff679b4SAakriti Gupta error_setg(errp, "QED table size must be within range [%u, %u] " 6535ff679b4SAakriti Gupta "and power of 2", 65475411d23SStefan Hajnoczi QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 6557ab74849SChunyan Liu ret = -EINVAL; 6567ab74849SChunyan Liu goto finish; 65775411d23SStefan Hajnoczi } 65875411d23SStefan Hajnoczi if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 6595ff679b4SAakriti Gupta error_setg(errp, "QED image size must be a non-zero multiple of " 6605ff679b4SAakriti Gupta "cluster size and less than %" PRIu64 " bytes", 66175411d23SStefan Hajnoczi qed_max_image_size(cluster_size, table_size)); 6627ab74849SChunyan Liu ret = -EINVAL; 6637ab74849SChunyan Liu goto finish; 66475411d23SStefan Hajnoczi } 66575411d23SStefan Hajnoczi 6667ab74849SChunyan Liu ret = qed_create(filename, cluster_size, image_size, table_size, 6674ab15590SChunyan Liu backing_file, backing_fmt, opts, errp); 6687ab74849SChunyan Liu 6697ab74849SChunyan Liu finish: 6707ab74849SChunyan Liu g_free(backing_file); 6717ab74849SChunyan Liu g_free(backing_fmt); 6727ab74849SChunyan Liu return ret; 67375411d23SStefan Hajnoczi } 67475411d23SStefan Hajnoczi 675298800caSStefan Hajnoczi typedef struct { 6764bc74be9SPaolo Bonzini BlockDriverState *bs; 677b7d5a5b8SStefan Hajnoczi Coroutine *co; 6784bc74be9SPaolo Bonzini uint64_t pos; 6794bc74be9SPaolo Bonzini int64_t status; 680298800caSStefan Hajnoczi int *pnum; 68153f1dfd1SFam Zheng BlockDriverState **file; 682298800caSStefan Hajnoczi } QEDIsAllocatedCB; 683298800caSStefan Hajnoczi 684298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 685298800caSStefan Hajnoczi { 686298800caSStefan Hajnoczi QEDIsAllocatedCB *cb = opaque; 6874bc74be9SPaolo Bonzini BDRVQEDState *s = cb->bs->opaque; 688298800caSStefan Hajnoczi *cb->pnum = len / BDRV_SECTOR_SIZE; 6894bc74be9SPaolo Bonzini switch (ret) { 6904bc74be9SPaolo Bonzini case QED_CLUSTER_FOUND: 6914bc74be9SPaolo Bonzini offset |= qed_offset_into_cluster(s, cb->pos); 6924bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; 69353f1dfd1SFam Zheng *cb->file = cb->bs->file->bs; 6944bc74be9SPaolo Bonzini break; 6954bc74be9SPaolo Bonzini case QED_CLUSTER_ZERO: 6964bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_ZERO; 6974bc74be9SPaolo Bonzini break; 6984bc74be9SPaolo Bonzini case QED_CLUSTER_L2: 6994bc74be9SPaolo Bonzini case QED_CLUSTER_L1: 7004bc74be9SPaolo Bonzini cb->status = 0; 7014bc74be9SPaolo Bonzini break; 7024bc74be9SPaolo Bonzini default: 7034bc74be9SPaolo Bonzini assert(ret < 0); 7044bc74be9SPaolo Bonzini cb->status = ret; 7054bc74be9SPaolo Bonzini break; 7064bc74be9SPaolo Bonzini } 7074bc74be9SPaolo Bonzini 708b7d5a5b8SStefan Hajnoczi if (cb->co) { 709b9e413ddSPaolo Bonzini aio_co_wake(cb->co); 710b7d5a5b8SStefan Hajnoczi } 711298800caSStefan Hajnoczi } 712298800caSStefan Hajnoczi 713b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs, 714b7d5a5b8SStefan Hajnoczi int64_t sector_num, 71567a0fd2aSFam Zheng int nb_sectors, int *pnum, 71667a0fd2aSFam Zheng BlockDriverState **file) 71775411d23SStefan Hajnoczi { 718298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 719298800caSStefan Hajnoczi size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 720298800caSStefan Hajnoczi QEDIsAllocatedCB cb = { 7214bc74be9SPaolo Bonzini .bs = bs, 7224bc74be9SPaolo Bonzini .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE, 7234bc74be9SPaolo Bonzini .status = BDRV_BLOCK_OFFSET_MASK, 724298800caSStefan Hajnoczi .pnum = pnum, 72553f1dfd1SFam Zheng .file = file, 726298800caSStefan Hajnoczi }; 727298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 7280f21b7a1SKevin Wolf uint64_t offset; 7290f21b7a1SKevin Wolf int ret; 730298800caSStefan Hajnoczi 7310f21b7a1SKevin Wolf ret = qed_find_cluster(s, &request, cb.pos, &len, &offset); 7320f21b7a1SKevin Wolf qed_is_allocated_cb(&cb, ret, offset, len); 733298800caSStefan Hajnoczi 7340f21b7a1SKevin Wolf /* The callback was invoked immediately */ 7350f21b7a1SKevin Wolf assert(cb.status != BDRV_BLOCK_OFFSET_MASK); 736298800caSStefan Hajnoczi 737298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 738298800caSStefan Hajnoczi 7394bc74be9SPaolo Bonzini return cb.status; 74075411d23SStefan Hajnoczi } 74175411d23SStefan Hajnoczi 742eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 743eabba580SStefan Hajnoczi { 74448cc565eSKevin Wolf return acb->bs->opaque; 745eabba580SStefan Hajnoczi } 746eabba580SStefan Hajnoczi 747eabba580SStefan Hajnoczi /** 748eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 749eabba580SStefan Hajnoczi * 750eabba580SStefan Hajnoczi * @s: QED state 751eabba580SStefan Hajnoczi * @pos: Byte position in device 752eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 753f06ee3d4SKevin Wolf * @backing_qiov: Possibly shortened copy of qiov, to be allocated here 754eabba580SStefan Hajnoczi * @cb: Completion function 755eabba580SStefan Hajnoczi * @opaque: User data for completion function 756eabba580SStefan Hajnoczi * 757eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 758eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 759eabba580SStefan Hajnoczi */ 76087f0d882SKevin Wolf static int coroutine_fn qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 761eabba580SStefan Hajnoczi QEMUIOVector *qiov, 762e85c5281SKevin Wolf QEMUIOVector **backing_qiov) 763eabba580SStefan Hajnoczi { 764eabba580SStefan Hajnoczi uint64_t backing_length = 0; 765eabba580SStefan Hajnoczi size_t size; 766e85c5281SKevin Wolf int ret; 767eabba580SStefan Hajnoczi 768eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 769eabba580SStefan Hajnoczi * backing file like a zero length backing file. 770eabba580SStefan Hajnoczi */ 771760e0063SKevin Wolf if (s->bs->backing) { 772760e0063SKevin Wolf int64_t l = bdrv_getlength(s->bs->backing->bs); 773eabba580SStefan Hajnoczi if (l < 0) { 774e85c5281SKevin Wolf return l; 775eabba580SStefan Hajnoczi } 776eabba580SStefan Hajnoczi backing_length = l; 777eabba580SStefan Hajnoczi } 778eabba580SStefan Hajnoczi 779eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 780eabba580SStefan Hajnoczi if (pos >= backing_length || 781eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 7823d9b4925SMichael Tokarev qemu_iovec_memset(qiov, 0, 0, qiov->size); 783eabba580SStefan Hajnoczi } 784eabba580SStefan Hajnoczi 785eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 786eabba580SStefan Hajnoczi if (pos >= backing_length) { 787e85c5281SKevin Wolf return 0; 788eabba580SStefan Hajnoczi } 789eabba580SStefan Hajnoczi 790eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 791eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 792eabba580SStefan Hajnoczi 793f06ee3d4SKevin Wolf assert(*backing_qiov == NULL); 794f06ee3d4SKevin Wolf *backing_qiov = g_new(QEMUIOVector, 1); 795f06ee3d4SKevin Wolf qemu_iovec_init(*backing_qiov, qiov->niov); 796f06ee3d4SKevin Wolf qemu_iovec_concat(*backing_qiov, qiov, 0, size); 797f06ee3d4SKevin Wolf 798820100fdSPaolo Bonzini BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); 799*0f714ec7SKevin Wolf ret = bdrv_co_preadv(s->bs->backing, pos, size, *backing_qiov, 0); 800e85c5281SKevin Wolf if (ret < 0) { 801e85c5281SKevin Wolf return ret; 802e85c5281SKevin Wolf } 803e85c5281SKevin Wolf return 0; 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 */ 81487f0d882SKevin Wolf static int coroutine_fn qed_copy_from_backing_file(BDRVQEDState *s, 81587f0d882SKevin Wolf uint64_t pos, uint64_t len, 81687f0d882SKevin Wolf uint64_t offset) 817eabba580SStefan Hajnoczi { 8180f7aa24dSKevin Wolf QEMUIOVector qiov; 8190f7aa24dSKevin Wolf QEMUIOVector *backing_qiov = NULL; 8200f7aa24dSKevin Wolf struct iovec iov; 821e85c5281SKevin Wolf int ret; 822eabba580SStefan Hajnoczi 823eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 824eabba580SStefan Hajnoczi if (len == 0) { 825b4ac32f3SKevin Wolf return 0; 826eabba580SStefan Hajnoczi } 827eabba580SStefan Hajnoczi 8280f7aa24dSKevin Wolf iov = (struct iovec) { 8290f7aa24dSKevin Wolf .iov_base = qemu_blockalign(s->bs, len), 8300f7aa24dSKevin Wolf .iov_len = len, 8310f7aa24dSKevin Wolf }; 8320f7aa24dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 833eabba580SStefan Hajnoczi 8340f7aa24dSKevin Wolf ret = qed_read_backing_file(s, pos, &qiov, &backing_qiov); 8350f7aa24dSKevin Wolf 8360f7aa24dSKevin Wolf if (backing_qiov) { 8370f7aa24dSKevin Wolf qemu_iovec_destroy(backing_qiov); 8380f7aa24dSKevin Wolf g_free(backing_qiov); 8390f7aa24dSKevin Wolf backing_qiov = NULL; 8400f7aa24dSKevin Wolf } 8410f7aa24dSKevin Wolf 8420f7aa24dSKevin Wolf if (ret) { 8430f7aa24dSKevin Wolf goto out; 8440f7aa24dSKevin Wolf } 8450f7aa24dSKevin Wolf 8460f7aa24dSKevin Wolf BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 847*0f714ec7SKevin Wolf ret = bdrv_co_pwritev(s->bs->file, offset, qiov.size, &qiov, 0); 8480f7aa24dSKevin Wolf if (ret < 0) { 8490f7aa24dSKevin Wolf goto out; 8500f7aa24dSKevin Wolf } 8510f7aa24dSKevin Wolf ret = 0; 8520f7aa24dSKevin Wolf out: 8530f7aa24dSKevin Wolf qemu_vfree(iov.iov_base); 854b4ac32f3SKevin Wolf return ret; 855eabba580SStefan Hajnoczi } 856eabba580SStefan Hajnoczi 857eabba580SStefan Hajnoczi /** 858eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 859eabba580SStefan Hajnoczi * 860eabba580SStefan Hajnoczi * @s: QED state 861eabba580SStefan Hajnoczi * @table: L2 table 862eabba580SStefan Hajnoczi * @index: First cluster index 863eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 86421df65b6SAnthony Liguori * @cluster: First cluster offset 86521df65b6SAnthony Liguori * 86621df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the 86721df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker. 868eabba580SStefan Hajnoczi */ 86987f0d882SKevin Wolf static void coroutine_fn qed_update_l2_table(BDRVQEDState *s, QEDTable *table, 87087f0d882SKevin Wolf int index, unsigned int n, 87187f0d882SKevin Wolf uint64_t cluster) 872eabba580SStefan Hajnoczi { 873eabba580SStefan Hajnoczi int i; 874eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 875eabba580SStefan Hajnoczi table->offsets[i] = cluster; 87621df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) && 87721df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) { 878eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 879eabba580SStefan Hajnoczi } 880eabba580SStefan Hajnoczi } 88121df65b6SAnthony Liguori } 882eabba580SStefan Hajnoczi 88387f0d882SKevin Wolf static void coroutine_fn qed_aio_complete(QEDAIOCB *acb) 884eabba580SStefan Hajnoczi { 885eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 886eabba580SStefan Hajnoczi 887eabba580SStefan Hajnoczi /* Free resources */ 888eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov); 889eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 890eabba580SStefan Hajnoczi 8910e71be19SStefan Hajnoczi /* Free the buffer we may have allocated for zero writes */ 8920e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 8930e71be19SStefan Hajnoczi qemu_vfree(acb->qiov->iov[0].iov_base); 8940e71be19SStefan Hajnoczi acb->qiov->iov[0].iov_base = NULL; 8950e71be19SStefan Hajnoczi } 8960e71be19SStefan Hajnoczi 897eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 898eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 899eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 900eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 901eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 902eabba580SStefan Hajnoczi */ 9030806c3b5SKevin Wolf if (acb == s->allocating_acb) { 9040806c3b5SKevin Wolf s->allocating_acb = NULL; 9050806c3b5SKevin Wolf if (!qemu_co_queue_empty(&s->allocating_write_reqs)) { 9060806c3b5SKevin Wolf qemu_co_enter_next(&s->allocating_write_reqs); 9076f321e93SStefan Hajnoczi } else if (s->header.features & QED_F_NEED_CHECK) { 9086f321e93SStefan Hajnoczi qed_start_need_check_timer(s); 909eabba580SStefan Hajnoczi } 910eabba580SStefan Hajnoczi } 911eabba580SStefan Hajnoczi } 912eabba580SStefan Hajnoczi 913eabba580SStefan Hajnoczi /** 914fae25ac7SKevin Wolf * Update L1 table with new L2 table offset and write it out 915eabba580SStefan Hajnoczi */ 91687f0d882SKevin Wolf static int coroutine_fn qed_aio_write_l1_update(QEDAIOCB *acb) 917eabba580SStefan Hajnoczi { 918eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 919eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 920e4fc8781SStefan Hajnoczi uint64_t l2_offset = l2_table->offset; 921fb18de21SKevin Wolf int index, ret; 922fae25ac7SKevin Wolf 923fae25ac7SKevin Wolf index = qed_l1_index(s, acb->cur_pos); 924fae25ac7SKevin Wolf s->l1_table->offsets[index] = l2_table->offset; 925fae25ac7SKevin Wolf 926fae25ac7SKevin Wolf ret = qed_write_l1_table(s, index, 1); 927fae25ac7SKevin Wolf 928fae25ac7SKevin Wolf /* Commit the current L2 table to the cache */ 929eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 930eabba580SStefan Hajnoczi 931eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 932eabba580SStefan Hajnoczi * cache. 933eabba580SStefan Hajnoczi */ 934e4fc8781SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 935eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 936eabba580SStefan Hajnoczi 937fb18de21SKevin Wolf return ret; 938eabba580SStefan Hajnoczi } 939eabba580SStefan Hajnoczi 940eabba580SStefan Hajnoczi 941eabba580SStefan Hajnoczi /** 942eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 943eabba580SStefan Hajnoczi */ 94487f0d882SKevin Wolf static int coroutine_fn qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset) 945eabba580SStefan Hajnoczi { 946eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 947eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 94888d2dd72SKevin Wolf int index, ret; 949eabba580SStefan Hajnoczi 950eabba580SStefan Hajnoczi if (need_alloc) { 951eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 952eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 953eabba580SStefan Hajnoczi } 954eabba580SStefan Hajnoczi 955eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 956eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 9570e71be19SStefan Hajnoczi offset); 958eabba580SStefan Hajnoczi 959eabba580SStefan Hajnoczi if (need_alloc) { 960eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 961453e53e2SKevin Wolf ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true); 962fb18de21SKevin Wolf if (ret) { 96388d2dd72SKevin Wolf return ret; 964fb18de21SKevin Wolf } 96588d2dd72SKevin Wolf return qed_aio_write_l1_update(acb); 966eabba580SStefan Hajnoczi } else { 967eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 968453e53e2SKevin Wolf ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, 969453e53e2SKevin Wolf false); 97088d2dd72SKevin Wolf if (ret) { 97188d2dd72SKevin Wolf return ret; 972eabba580SStefan Hajnoczi } 97388d2dd72SKevin Wolf } 97488d2dd72SKevin Wolf return 0; 975eabba580SStefan Hajnoczi } 976eabba580SStefan Hajnoczi 977eabba580SStefan Hajnoczi /** 978eabba580SStefan Hajnoczi * Write data to the image file 979eabba580SStefan Hajnoczi */ 98087f0d882SKevin Wolf static int coroutine_fn qed_aio_write_main(QEDAIOCB *acb) 981eabba580SStefan Hajnoczi { 982eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 983eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 984eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 985eaf0bc56SKevin Wolf int ret; 986eabba580SStefan Hajnoczi 987eaf0bc56SKevin Wolf trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size); 988eabba580SStefan Hajnoczi 989a4d8f1aeSKevin Wolf BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 990*0f714ec7SKevin Wolf ret = bdrv_co_pwritev(s->bs->file, offset, acb->cur_qiov.size, 991*0f714ec7SKevin Wolf &acb->cur_qiov, 0); 992eaf0bc56SKevin Wolf if (ret < 0) { 993eaf0bc56SKevin Wolf return ret; 994eabba580SStefan Hajnoczi } 995eabba580SStefan Hajnoczi 996eaf0bc56SKevin Wolf if (acb->find_cluster_ret != QED_CLUSTER_FOUND) { 997a4d8f1aeSKevin Wolf if (s->bs->backing) { 998a4d8f1aeSKevin Wolf /* 999a4d8f1aeSKevin Wolf * Flush new data clusters before updating the L2 table 1000a4d8f1aeSKevin Wolf * 1001a4d8f1aeSKevin Wolf * This flush is necessary when a backing file is in use. A crash 1002a4d8f1aeSKevin Wolf * during an allocating write could result in empty clusters in the 1003a4d8f1aeSKevin Wolf * image. If the write only touched a subregion of the cluster, 1004a4d8f1aeSKevin Wolf * then backing image sectors have been lost in the untouched 1005a4d8f1aeSKevin Wolf * region. The solution is to flush after writing a new data 1006a4d8f1aeSKevin Wolf * cluster and before updating the L2 table. 1007a4d8f1aeSKevin Wolf */ 1008*0f714ec7SKevin Wolf ret = bdrv_co_flush(s->bs->file->bs); 1009eaf0bc56SKevin Wolf if (ret < 0) { 1010eaf0bc56SKevin Wolf return ret; 1011a4d8f1aeSKevin Wolf } 1012a4d8f1aeSKevin Wolf } 101388d2dd72SKevin Wolf ret = qed_aio_write_l2_update(acb, acb->cur_cluster); 1014eaf0bc56SKevin Wolf if (ret < 0) { 1015eaf0bc56SKevin Wolf return ret; 101688d2dd72SKevin Wolf } 101788d2dd72SKevin Wolf } 1018eaf0bc56SKevin Wolf return 0; 1019eabba580SStefan Hajnoczi } 1020eabba580SStefan Hajnoczi 1021eabba580SStefan Hajnoczi /** 1022b4ac32f3SKevin Wolf * Populate untouched regions of new data cluster 1023eabba580SStefan Hajnoczi */ 102487f0d882SKevin Wolf static int coroutine_fn qed_aio_write_cow(QEDAIOCB *acb) 1025eabba580SStefan Hajnoczi { 1026eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1027b4ac32f3SKevin Wolf uint64_t start, len, offset; 1028a101341aSKevin Wolf int ret; 1029eabba580SStefan Hajnoczi 1030b4ac32f3SKevin Wolf /* Populate front untouched region of new data cluster */ 1031b4ac32f3SKevin Wolf start = qed_start_of_cluster(s, acb->cur_pos); 1032b4ac32f3SKevin Wolf len = qed_offset_into_cluster(s, acb->cur_pos); 1033b4ac32f3SKevin Wolf 1034b4ac32f3SKevin Wolf trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1035b4ac32f3SKevin Wolf ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster); 1036a101341aSKevin Wolf if (ret < 0) { 1037a101341aSKevin Wolf return ret; 1038eabba580SStefan Hajnoczi } 1039eabba580SStefan Hajnoczi 1040b4ac32f3SKevin Wolf /* Populate back untouched region of new data cluster */ 1041b4ac32f3SKevin Wolf start = acb->cur_pos + acb->cur_qiov.size; 1042b4ac32f3SKevin Wolf len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1043b4ac32f3SKevin Wolf offset = acb->cur_cluster + 1044b4ac32f3SKevin Wolf qed_offset_into_cluster(s, acb->cur_pos) + 1045b4ac32f3SKevin Wolf acb->cur_qiov.size; 1046b4ac32f3SKevin Wolf 1047eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 1048b4ac32f3SKevin Wolf ret = qed_copy_from_backing_file(s, start, len, offset); 1049a101341aSKevin Wolf if (ret < 0) { 1050a101341aSKevin Wolf return ret; 1051eaf0bc56SKevin Wolf } 1052eabba580SStefan Hajnoczi 1053a101341aSKevin Wolf return qed_aio_write_main(acb); 1054eabba580SStefan Hajnoczi } 1055eabba580SStefan Hajnoczi 1056eabba580SStefan Hajnoczi /** 10570d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write 10580d09c797SStefan Hajnoczi */ 10590d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s) 10600d09c797SStefan Hajnoczi { 10610d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */ 1062760e0063SKevin Wolf if (s->bs->backing) { 10630d09c797SStefan Hajnoczi return false; 10640d09c797SStefan Hajnoczi } 10650d09c797SStefan Hajnoczi 10660d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK); 10670d09c797SStefan Hajnoczi } 10680d09c797SStefan Hajnoczi 10690d09c797SStefan Hajnoczi /** 1070eabba580SStefan Hajnoczi * Write new data cluster 1071eabba580SStefan Hajnoczi * 1072eabba580SStefan Hajnoczi * @acb: Write request 1073eabba580SStefan Hajnoczi * @len: Length in bytes 1074eabba580SStefan Hajnoczi * 1075eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters. 1076eabba580SStefan Hajnoczi */ 107787f0d882SKevin Wolf static int coroutine_fn qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1078eabba580SStefan Hajnoczi { 1079eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1080f13d712bSKevin Wolf int ret; 1081eabba580SStefan Hajnoczi 10826f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */ 10830806c3b5SKevin Wolf if (s->allocating_acb == NULL) { 10846f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 10856f321e93SStefan Hajnoczi } 10866f321e93SStefan Hajnoczi 1087eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 10880806c3b5SKevin Wolf if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) { 10890806c3b5SKevin Wolf if (s->allocating_acb != NULL) { 10900806c3b5SKevin Wolf qemu_co_queue_wait(&s->allocating_write_reqs, NULL); 10910806c3b5SKevin Wolf assert(s->allocating_acb == NULL); 1092eabba580SStefan Hajnoczi } 10930806c3b5SKevin Wolf s->allocating_acb = acb; 10940806c3b5SKevin Wolf return -EAGAIN; /* start over with looking up table entries */ 1095eabba580SStefan Hajnoczi } 1096eabba580SStefan Hajnoczi 1097eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 1098eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 10991b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1100eabba580SStefan Hajnoczi 11010e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 11020e71be19SStefan Hajnoczi /* Skip ahead if the clusters are already zero */ 11030e71be19SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { 1104d6daddcdSKevin Wolf return 0; 11050e71be19SStefan Hajnoczi } 11060e71be19SStefan Hajnoczi } else { 11070e71be19SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 11080e71be19SStefan Hajnoczi } 11090e71be19SStefan Hajnoczi 11100d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) { 111101979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK; 1112f13d712bSKevin Wolf ret = qed_write_header(s); 1113a101341aSKevin Wolf if (ret < 0) { 1114d6daddcdSKevin Wolf return ret; 11150d09c797SStefan Hajnoczi } 1116eabba580SStefan Hajnoczi } 1117eabba580SStefan Hajnoczi 1118a101341aSKevin Wolf if (acb->flags & QED_AIOCB_ZERO) { 1119a101341aSKevin Wolf ret = qed_aio_write_l2_update(acb, 1); 1120a101341aSKevin Wolf } else { 1121a101341aSKevin Wolf ret = qed_aio_write_cow(acb); 1122a101341aSKevin Wolf } 1123a101341aSKevin Wolf if (ret < 0) { 1124d6daddcdSKevin Wolf return ret; 1125a101341aSKevin Wolf } 1126d6daddcdSKevin Wolf return 0; 1127a101341aSKevin Wolf } 1128a101341aSKevin Wolf 1129eabba580SStefan Hajnoczi /** 1130eabba580SStefan Hajnoczi * Write data cluster in place 1131eabba580SStefan Hajnoczi * 1132eabba580SStefan Hajnoczi * @acb: Write request 1133eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1134eabba580SStefan Hajnoczi * @len: Length in bytes 1135eabba580SStefan Hajnoczi * 1136eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 1137eabba580SStefan Hajnoczi */ 113887f0d882SKevin Wolf static int coroutine_fn qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, 113987f0d882SKevin Wolf size_t len) 1140eabba580SStefan Hajnoczi { 11410e71be19SStefan Hajnoczi /* Allocate buffer for zero writes */ 11420e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 11430e71be19SStefan Hajnoczi struct iovec *iov = acb->qiov->iov; 11440e71be19SStefan Hajnoczi 11450e71be19SStefan Hajnoczi if (!iov->iov_base) { 114648cc565eSKevin Wolf iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len); 11474f4896dbSKevin Wolf if (iov->iov_base == NULL) { 1148d6daddcdSKevin Wolf return -ENOMEM; 11494f4896dbSKevin Wolf } 11500e71be19SStefan Hajnoczi memset(iov->iov_base, 0, iov->iov_len); 11510e71be19SStefan Hajnoczi } 11520e71be19SStefan Hajnoczi } 11530e71be19SStefan Hajnoczi 1154eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 1155eabba580SStefan Hajnoczi acb->cur_cluster = offset; 11561b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1157eabba580SStefan Hajnoczi 1158eabba580SStefan Hajnoczi /* Do the actual write */ 1159d6daddcdSKevin Wolf return qed_aio_write_main(acb); 1160eabba580SStefan Hajnoczi } 1161eabba580SStefan Hajnoczi 1162eabba580SStefan Hajnoczi /** 1163eabba580SStefan Hajnoczi * Write data cluster 1164eabba580SStefan Hajnoczi * 1165eabba580SStefan Hajnoczi * @opaque: Write request 11660596be7eSKevin Wolf * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 1167eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1168eabba580SStefan Hajnoczi * @len: Length in bytes 1169eabba580SStefan Hajnoczi */ 117087f0d882SKevin Wolf static int coroutine_fn qed_aio_write_data(void *opaque, int ret, 1171eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1172eabba580SStefan Hajnoczi { 1173eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1174eabba580SStefan Hajnoczi 1175eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1176eabba580SStefan Hajnoczi 1177eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 1178eabba580SStefan Hajnoczi 1179eabba580SStefan Hajnoczi switch (ret) { 1180eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 11810596be7eSKevin Wolf return qed_aio_write_inplace(acb, offset, len); 1182eabba580SStefan Hajnoczi 1183eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 1184eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 118521df65b6SAnthony Liguori case QED_CLUSTER_ZERO: 11860596be7eSKevin Wolf return qed_aio_write_alloc(acb, len); 1187eabba580SStefan Hajnoczi 1188eabba580SStefan Hajnoczi default: 11890596be7eSKevin Wolf g_assert_not_reached(); 1190eabba580SStefan Hajnoczi } 1191eabba580SStefan Hajnoczi } 1192eabba580SStefan Hajnoczi 1193eabba580SStefan Hajnoczi /** 1194eabba580SStefan Hajnoczi * Read data cluster 1195eabba580SStefan Hajnoczi * 1196eabba580SStefan Hajnoczi * @opaque: Read request 11970596be7eSKevin Wolf * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 1198eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1199eabba580SStefan Hajnoczi * @len: Length in bytes 1200eabba580SStefan Hajnoczi */ 120187f0d882SKevin Wolf static int coroutine_fn qed_aio_read_data(void *opaque, int ret, 120287f0d882SKevin Wolf uint64_t offset, size_t len) 1203eabba580SStefan Hajnoczi { 1204eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1205eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 120648cc565eSKevin Wolf BlockDriverState *bs = acb->bs; 1207eabba580SStefan Hajnoczi 1208eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 1209eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 1210eabba580SStefan Hajnoczi 1211eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 1212eabba580SStefan Hajnoczi 12131b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1214eabba580SStefan Hajnoczi 121521df65b6SAnthony Liguori /* Handle zero cluster and backing file reads */ 121621df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) { 12173d9b4925SMichael Tokarev qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); 12180596be7eSKevin Wolf return 0; 121921df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) { 12200596be7eSKevin Wolf return qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1221e85c5281SKevin Wolf &acb->backing_qiov); 1222eabba580SStefan Hajnoczi } 1223eabba580SStefan Hajnoczi 1224eabba580SStefan Hajnoczi BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 1225*0f714ec7SKevin Wolf ret = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size, 1226*0f714ec7SKevin Wolf &acb->cur_qiov, 0); 12273e248cdcSKevin Wolf if (ret < 0) { 12280596be7eSKevin Wolf return ret; 12293e248cdcSKevin Wolf } 12300596be7eSKevin Wolf return 0; 1231eabba580SStefan Hajnoczi } 1232eabba580SStefan Hajnoczi 1233eabba580SStefan Hajnoczi /** 1234eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1235eabba580SStefan Hajnoczi */ 123687f0d882SKevin Wolf static int coroutine_fn qed_aio_next_io(QEDAIOCB *acb) 1237eabba580SStefan Hajnoczi { 1238eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 12390f21b7a1SKevin Wolf uint64_t offset; 12400f21b7a1SKevin Wolf size_t len; 1241dddf8db1SKevin Wolf int ret; 1242eabba580SStefan Hajnoczi 124301859874SKevin Wolf while (1) { 1244dddf8db1SKevin Wolf trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size); 1245eabba580SStefan Hajnoczi 1246f06ee3d4SKevin Wolf if (acb->backing_qiov) { 1247f06ee3d4SKevin Wolf qemu_iovec_destroy(acb->backing_qiov); 1248f06ee3d4SKevin Wolf g_free(acb->backing_qiov); 1249f06ee3d4SKevin Wolf acb->backing_qiov = NULL; 1250f06ee3d4SKevin Wolf } 1251f06ee3d4SKevin Wolf 1252eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1253eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1254eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1255eabba580SStefan Hajnoczi 1256eabba580SStefan Hajnoczi /* Complete request */ 1257eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 125848cc565eSKevin Wolf ret = 0; 125948cc565eSKevin Wolf break; 1260eabba580SStefan Hajnoczi } 1261eabba580SStefan Hajnoczi 1262eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 12630f21b7a1SKevin Wolf len = acb->end_pos - acb->cur_pos; 12640f21b7a1SKevin Wolf ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset); 12650596be7eSKevin Wolf if (ret < 0) { 126648cc565eSKevin Wolf break; 12670596be7eSKevin Wolf } 12680596be7eSKevin Wolf 12690596be7eSKevin Wolf if (acb->flags & QED_AIOCB_WRITE) { 12700596be7eSKevin Wolf ret = qed_aio_write_data(acb, ret, offset, len); 12710596be7eSKevin Wolf } else { 12720596be7eSKevin Wolf ret = qed_aio_read_data(acb, ret, offset, len); 12730596be7eSKevin Wolf } 12740596be7eSKevin Wolf 12750806c3b5SKevin Wolf if (ret < 0 && ret != -EAGAIN) { 127648cc565eSKevin Wolf break; 127701859874SKevin Wolf } 1278eabba580SStefan Hajnoczi } 1279eabba580SStefan Hajnoczi 128048cc565eSKevin Wolf trace_qed_aio_complete(s, acb, ret); 128148cc565eSKevin Wolf qed_aio_complete(acb); 128248cc565eSKevin Wolf return ret; 128389f89709SKevin Wolf } 128489f89709SKevin Wolf 128589f89709SKevin Wolf static int coroutine_fn qed_co_request(BlockDriverState *bs, int64_t sector_num, 128689f89709SKevin Wolf QEMUIOVector *qiov, int nb_sectors, 128789f89709SKevin Wolf int flags) 128889f89709SKevin Wolf { 128948cc565eSKevin Wolf QEDAIOCB acb = { 129048cc565eSKevin Wolf .bs = bs, 129148cc565eSKevin Wolf .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE, 129248cc565eSKevin Wolf .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE, 129348cc565eSKevin Wolf .qiov = qiov, 129448cc565eSKevin Wolf .flags = flags, 129589f89709SKevin Wolf }; 129648cc565eSKevin Wolf qemu_iovec_init(&acb.cur_qiov, qiov->niov); 129789f89709SKevin Wolf 129848cc565eSKevin Wolf trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags); 1299eabba580SStefan Hajnoczi 1300eabba580SStefan Hajnoczi /* Start request */ 130148cc565eSKevin Wolf return qed_aio_next_io(&acb); 130289f89709SKevin Wolf } 130389f89709SKevin Wolf 130489f89709SKevin Wolf static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs, 130589f89709SKevin Wolf int64_t sector_num, int nb_sectors, 130689f89709SKevin Wolf QEMUIOVector *qiov) 130775411d23SStefan Hajnoczi { 130889f89709SKevin Wolf return qed_co_request(bs, sector_num, qiov, nb_sectors, 0); 130975411d23SStefan Hajnoczi } 131075411d23SStefan Hajnoczi 131189f89709SKevin Wolf static int coroutine_fn bdrv_qed_co_writev(BlockDriverState *bs, 131289f89709SKevin Wolf int64_t sector_num, int nb_sectors, 131389f89709SKevin Wolf QEMUIOVector *qiov) 131475411d23SStefan Hajnoczi { 131589f89709SKevin Wolf return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRITE); 13160e71be19SStefan Hajnoczi } 13170e71be19SStefan Hajnoczi 131849a2e483SEric Blake static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, 131949a2e483SEric Blake int64_t offset, 132049a2e483SEric Blake int count, 1321aa7bfbffSPeter Lieven BdrvRequestFlags flags) 13220e71be19SStefan Hajnoczi { 1323ef72f76eSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 13240e71be19SStefan Hajnoczi QEMUIOVector qiov; 13250e71be19SStefan Hajnoczi struct iovec iov; 13260e71be19SStefan Hajnoczi 132749a2e483SEric Blake /* Fall back if the request is not aligned */ 132849a2e483SEric Blake if (qed_offset_into_cluster(s, offset) || 132949a2e483SEric Blake qed_offset_into_cluster(s, count)) { 1330ef72f76eSStefan Hajnoczi return -ENOTSUP; 1331ef72f76eSStefan Hajnoczi } 1332ef72f76eSStefan Hajnoczi 13330e71be19SStefan Hajnoczi /* Zero writes start without an I/O buffer. If a buffer becomes necessary 13340e71be19SStefan Hajnoczi * then it will be allocated during request processing. 13350e71be19SStefan Hajnoczi */ 133649a2e483SEric Blake iov.iov_base = NULL; 133749a2e483SEric Blake iov.iov_len = count; 13380e71be19SStefan Hajnoczi 13390e71be19SStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 134089f89709SKevin Wolf return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov, 134149a2e483SEric Blake count >> BDRV_SECTOR_BITS, 13420e71be19SStefan Hajnoczi QED_AIOCB_WRITE | QED_AIOCB_ZERO); 13430e71be19SStefan Hajnoczi } 13440e71be19SStefan Hajnoczi 13454bff28b8SMax Reitz static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset, Error **errp) 134675411d23SStefan Hajnoczi { 134777a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 134877a5a000SStefan Hajnoczi uint64_t old_image_size; 134977a5a000SStefan Hajnoczi int ret; 135077a5a000SStefan Hajnoczi 135177a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size, 135277a5a000SStefan Hajnoczi s->header.table_size)) { 1353f59adb32SMax Reitz error_setg(errp, "Invalid image size specified"); 135477a5a000SStefan Hajnoczi return -EINVAL; 135577a5a000SStefan Hajnoczi } 135677a5a000SStefan Hajnoczi 135777a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) { 1358f59adb32SMax Reitz error_setg(errp, "Shrinking images is currently not supported"); 135975411d23SStefan Hajnoczi return -ENOTSUP; 136075411d23SStefan Hajnoczi } 136175411d23SStefan Hajnoczi 136277a5a000SStefan Hajnoczi old_image_size = s->header.image_size; 136377a5a000SStefan Hajnoczi s->header.image_size = offset; 136477a5a000SStefan Hajnoczi ret = qed_write_header_sync(s); 136577a5a000SStefan Hajnoczi if (ret < 0) { 136677a5a000SStefan Hajnoczi s->header.image_size = old_image_size; 1367f59adb32SMax Reitz error_setg_errno(errp, -ret, "Failed to update the image size"); 136877a5a000SStefan Hajnoczi } 136977a5a000SStefan Hajnoczi return ret; 137077a5a000SStefan Hajnoczi } 137177a5a000SStefan Hajnoczi 137275411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs) 137375411d23SStefan Hajnoczi { 137475411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 137575411d23SStefan Hajnoczi return s->header.image_size; 137675411d23SStefan Hajnoczi } 137775411d23SStefan Hajnoczi 137875411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 137975411d23SStefan Hajnoczi { 138075411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 138175411d23SStefan Hajnoczi 138275411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi)); 138375411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size; 1384d68dbee8SDong Xu Wang bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; 138595de6d70SPaolo Bonzini bdi->unallocated_blocks_are_zero = true; 138695de6d70SPaolo Bonzini bdi->can_write_zeroes_with_unmap = true; 138775411d23SStefan Hajnoczi return 0; 138875411d23SStefan Hajnoczi } 138975411d23SStefan Hajnoczi 139075411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 139175411d23SStefan Hajnoczi const char *backing_file, 139275411d23SStefan Hajnoczi const char *backing_fmt) 139375411d23SStefan Hajnoczi { 139475411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 139575411d23SStefan Hajnoczi QEDHeader new_header, le_header; 139675411d23SStefan Hajnoczi void *buffer; 139775411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 139875411d23SStefan Hajnoczi int ret; 139975411d23SStefan Hajnoczi 140075411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 140175411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 140275411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 140375411d23SStefan Hajnoczi * add a new string. 140475411d23SStefan Hajnoczi */ 140575411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 140675411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 140775411d23SStefan Hajnoczi return -ENOTSUP; 140875411d23SStefan Hajnoczi } 140975411d23SStefan Hajnoczi 141075411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 141175411d23SStefan Hajnoczi 141275411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 141375411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 141475411d23SStefan Hajnoczi 141575411d23SStefan Hajnoczi /* Adjust feature flags */ 141675411d23SStefan Hajnoczi if (backing_file) { 141775411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 141875411d23SStefan Hajnoczi 141975411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 142075411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 142175411d23SStefan Hajnoczi } 142275411d23SStefan Hajnoczi } 142375411d23SStefan Hajnoczi 142475411d23SStefan Hajnoczi /* Calculate new header size */ 142575411d23SStefan Hajnoczi backing_file_len = 0; 142675411d23SStefan Hajnoczi 142775411d23SStefan Hajnoczi if (backing_file) { 142875411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 142975411d23SStefan Hajnoczi } 143075411d23SStefan Hajnoczi 143175411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 143275411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 143375411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 143475411d23SStefan Hajnoczi buffer_len += backing_file_len; 143575411d23SStefan Hajnoczi 143675411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 143775411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 143875411d23SStefan Hajnoczi return -ENOSPC; 143975411d23SStefan Hajnoczi } 144075411d23SStefan Hajnoczi 144175411d23SStefan Hajnoczi /* Prepare new header */ 14427267c094SAnthony Liguori buffer = g_malloc(buffer_len); 144375411d23SStefan Hajnoczi 144475411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 144575411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 144675411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 144775411d23SStefan Hajnoczi 1448feba23b1SPavel Borzenkov if (backing_file) { 144975411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 145075411d23SStefan Hajnoczi buffer_len += backing_file_len; 1451feba23b1SPavel Borzenkov } 145275411d23SStefan Hajnoczi 145375411d23SStefan Hajnoczi /* Write new header */ 1454d9ca2ea2SKevin Wolf ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); 14557267c094SAnthony Liguori g_free(buffer); 145675411d23SStefan Hajnoczi if (ret == 0) { 145775411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 145875411d23SStefan Hajnoczi } 145975411d23SStefan Hajnoczi return ret; 146075411d23SStefan Hajnoczi } 146175411d23SStefan Hajnoczi 14625a8a30dbSKevin Wolf static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp) 1463c82954e5SBenoît Canet { 1464c82954e5SBenoît Canet BDRVQEDState *s = bs->opaque; 14655a8a30dbSKevin Wolf Error *local_err = NULL; 14665a8a30dbSKevin Wolf int ret; 1467c82954e5SBenoît Canet 1468c82954e5SBenoît Canet bdrv_qed_close(bs); 14693456a8d1SKevin Wolf 1470c82954e5SBenoît Canet memset(s, 0, sizeof(BDRVQEDState)); 14714e4bf5c4SKevin Wolf ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, &local_err); 14725a8a30dbSKevin Wolf if (local_err) { 1473e43bfd9cSMarkus Armbruster error_propagate(errp, local_err); 1474e43bfd9cSMarkus Armbruster error_prepend(errp, "Could not reopen qed layer: "); 14755a8a30dbSKevin Wolf return; 14765a8a30dbSKevin Wolf } else if (ret < 0) { 14775a8a30dbSKevin Wolf error_setg_errno(errp, -ret, "Could not reopen qed layer"); 14785a8a30dbSKevin Wolf return; 14795a8a30dbSKevin Wolf } 1480c82954e5SBenoît Canet } 1481c82954e5SBenoît Canet 14824534ff54SKevin Wolf static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result, 14834534ff54SKevin Wolf BdrvCheckMode fix) 148475411d23SStefan Hajnoczi { 148501979a98SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 148601979a98SStefan Hajnoczi 14874534ff54SKevin Wolf return qed_check(s, result, !!fix); 148875411d23SStefan Hajnoczi } 148975411d23SStefan Hajnoczi 14907ab74849SChunyan Liu static QemuOptsList qed_create_opts = { 14917ab74849SChunyan Liu .name = "qed-create-opts", 14927ab74849SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head), 14937ab74849SChunyan Liu .desc = { 149475411d23SStefan Hajnoczi { 149575411d23SStefan Hajnoczi .name = BLOCK_OPT_SIZE, 14967ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 14977ab74849SChunyan Liu .help = "Virtual disk size" 14987ab74849SChunyan Liu }, 14997ab74849SChunyan Liu { 150075411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FILE, 15017ab74849SChunyan Liu .type = QEMU_OPT_STRING, 150275411d23SStefan Hajnoczi .help = "File name of a base image" 15037ab74849SChunyan Liu }, 15047ab74849SChunyan Liu { 150575411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FMT, 15067ab74849SChunyan Liu .type = QEMU_OPT_STRING, 150775411d23SStefan Hajnoczi .help = "Image format of the base image" 15087ab74849SChunyan Liu }, 15097ab74849SChunyan Liu { 151075411d23SStefan Hajnoczi .name = BLOCK_OPT_CLUSTER_SIZE, 15117ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 151299cce9faSKevin Wolf .help = "Cluster size (in bytes)", 15137ab74849SChunyan Liu .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE) 15147ab74849SChunyan Liu }, 15157ab74849SChunyan Liu { 151675411d23SStefan Hajnoczi .name = BLOCK_OPT_TABLE_SIZE, 15177ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 151875411d23SStefan Hajnoczi .help = "L1/L2 table size (in clusters)" 151975411d23SStefan Hajnoczi }, 152075411d23SStefan Hajnoczi { /* end of list */ } 15217ab74849SChunyan Liu } 152275411d23SStefan Hajnoczi }; 152375411d23SStefan Hajnoczi 152475411d23SStefan Hajnoczi static BlockDriver bdrv_qed = { 152575411d23SStefan Hajnoczi .format_name = "qed", 152675411d23SStefan Hajnoczi .instance_size = sizeof(BDRVQEDState), 15277ab74849SChunyan Liu .create_opts = &qed_create_opts, 15288ee79e70SKevin Wolf .supports_backing = true, 152975411d23SStefan Hajnoczi 153075411d23SStefan Hajnoczi .bdrv_probe = bdrv_qed_probe, 153175411d23SStefan Hajnoczi .bdrv_open = bdrv_qed_open, 153275411d23SStefan Hajnoczi .bdrv_close = bdrv_qed_close, 1533f9cb20f1SJeff Cody .bdrv_reopen_prepare = bdrv_qed_reopen_prepare, 1534862f215fSKevin Wolf .bdrv_child_perm = bdrv_format_default_perms, 1535c282e1fdSChunyan Liu .bdrv_create = bdrv_qed_create, 15363ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 1537b6b8a333SPaolo Bonzini .bdrv_co_get_block_status = bdrv_qed_co_get_block_status, 153889f89709SKevin Wolf .bdrv_co_readv = bdrv_qed_co_readv, 153989f89709SKevin Wolf .bdrv_co_writev = bdrv_qed_co_writev, 154049a2e483SEric Blake .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes, 154175411d23SStefan Hajnoczi .bdrv_truncate = bdrv_qed_truncate, 154275411d23SStefan Hajnoczi .bdrv_getlength = bdrv_qed_getlength, 154375411d23SStefan Hajnoczi .bdrv_get_info = bdrv_qed_get_info, 1544d34682cdSKevin Wolf .bdrv_refresh_limits = bdrv_qed_refresh_limits, 154575411d23SStefan Hajnoczi .bdrv_change_backing_file = bdrv_qed_change_backing_file, 1546c82954e5SBenoît Canet .bdrv_invalidate_cache = bdrv_qed_invalidate_cache, 154775411d23SStefan Hajnoczi .bdrv_check = bdrv_qed_check, 1548a8c868c3SStefan Hajnoczi .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, 1549a8c868c3SStefan Hajnoczi .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, 15506653a73dSFam Zheng .bdrv_drain = bdrv_qed_drain, 155175411d23SStefan Hajnoczi }; 155275411d23SStefan Hajnoczi 155375411d23SStefan Hajnoczi static void bdrv_qed_init(void) 155475411d23SStefan Hajnoczi { 155575411d23SStefan Hajnoczi bdrv_register(&bdrv_qed); 155675411d23SStefan Hajnoczi } 155775411d23SStefan Hajnoczi 155875411d23SStefan Hajnoczi block_init(bdrv_qed_init); 1559