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 */ 97f13d712bSKevin Wolf static int 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 1197076309aSKevin Wolf ret = bdrv_preadv(s->bs->file, 0, &qiov); 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 1277076309aSKevin Wolf ret = bdrv_pwritev(s->bs->file, 0, &qiov); 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 2676f321e93SStefan Hajnoczi static void qed_clear_need_check(void *opaque, int ret) 2686f321e93SStefan Hajnoczi { 2696f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 2706f321e93SStefan Hajnoczi 2716f321e93SStefan Hajnoczi if (ret) { 2726f321e93SStefan Hajnoczi qed_unplug_allocating_write_reqs(s); 2736f321e93SStefan Hajnoczi return; 2746f321e93SStefan Hajnoczi } 2756f321e93SStefan Hajnoczi 2766f321e93SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 277f13d712bSKevin Wolf ret = qed_write_header(s); 278f13d712bSKevin Wolf (void) ret; 279f13d712bSKevin Wolf 280f13d712bSKevin Wolf qed_unplug_allocating_write_reqs(s); 281f13d712bSKevin Wolf 282f13d712bSKevin Wolf ret = bdrv_flush(s->bs); 283f13d712bSKevin Wolf (void) ret; 2846f321e93SStefan Hajnoczi } 2856f321e93SStefan Hajnoczi 2866f321e93SStefan Hajnoczi static void qed_need_check_timer_cb(void *opaque) 2876f321e93SStefan Hajnoczi { 2886f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 2896f321e93SStefan Hajnoczi 2906f321e93SStefan Hajnoczi /* The timer should only fire when allocating writes have drained */ 2910806c3b5SKevin Wolf assert(!s->allocating_acb); 2926f321e93SStefan Hajnoczi 2936f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s); 2946f321e93SStefan Hajnoczi 2952f47da5fSPaolo Bonzini qed_acquire(s); 2966f321e93SStefan Hajnoczi qed_plug_allocating_write_reqs(s); 2976f321e93SStefan Hajnoczi 2986f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */ 2996653a73dSFam Zheng bdrv_aio_flush(s->bs->file->bs, qed_clear_need_check, s); 3002f47da5fSPaolo Bonzini qed_release(s); 3012f47da5fSPaolo Bonzini } 3022f47da5fSPaolo Bonzini 3032f47da5fSPaolo Bonzini void qed_acquire(BDRVQEDState *s) 3042f47da5fSPaolo Bonzini { 3052f47da5fSPaolo Bonzini aio_context_acquire(bdrv_get_aio_context(s->bs)); 3062f47da5fSPaolo Bonzini } 3072f47da5fSPaolo Bonzini 3082f47da5fSPaolo Bonzini void qed_release(BDRVQEDState *s) 3092f47da5fSPaolo Bonzini { 3102f47da5fSPaolo Bonzini aio_context_release(bdrv_get_aio_context(s->bs)); 3116f321e93SStefan Hajnoczi } 3126f321e93SStefan Hajnoczi 3136f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s) 3146f321e93SStefan Hajnoczi { 3156f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s); 3166f321e93SStefan Hajnoczi 317bc72ad67SAlex Bligh /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for 3186f321e93SStefan Hajnoczi * migration. 3196f321e93SStefan Hajnoczi */ 320bc72ad67SAlex Bligh timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 32173bcb24dSRutuja Shah NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT); 3226f321e93SStefan Hajnoczi } 3236f321e93SStefan Hajnoczi 3246f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */ 3256f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s) 3266f321e93SStefan Hajnoczi { 3276f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s); 328bc72ad67SAlex Bligh timer_del(s->need_check_timer); 3296f321e93SStefan Hajnoczi } 3306f321e93SStefan Hajnoczi 331a8c868c3SStefan Hajnoczi static void bdrv_qed_detach_aio_context(BlockDriverState *bs) 332a8c868c3SStefan Hajnoczi { 333a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 334a8c868c3SStefan Hajnoczi 335a8c868c3SStefan Hajnoczi qed_cancel_need_check_timer(s); 336a8c868c3SStefan Hajnoczi timer_free(s->need_check_timer); 337a8c868c3SStefan Hajnoczi } 338a8c868c3SStefan Hajnoczi 339a8c868c3SStefan Hajnoczi static void bdrv_qed_attach_aio_context(BlockDriverState *bs, 340a8c868c3SStefan Hajnoczi AioContext *new_context) 341a8c868c3SStefan Hajnoczi { 342a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 343a8c868c3SStefan Hajnoczi 344a8c868c3SStefan Hajnoczi s->need_check_timer = aio_timer_new(new_context, 345a8c868c3SStefan Hajnoczi QEMU_CLOCK_VIRTUAL, SCALE_NS, 346a8c868c3SStefan Hajnoczi qed_need_check_timer_cb, s); 347a8c868c3SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 348a8c868c3SStefan Hajnoczi qed_start_need_check_timer(s); 349a8c868c3SStefan Hajnoczi } 350a8c868c3SStefan Hajnoczi } 351a8c868c3SStefan Hajnoczi 3526653a73dSFam Zheng static void bdrv_qed_drain(BlockDriverState *bs) 3536653a73dSFam Zheng { 3546653a73dSFam Zheng BDRVQEDState *s = bs->opaque; 3556653a73dSFam Zheng 3566653a73dSFam Zheng /* Fire the timer immediately in order to start doing I/O as soon as the 3576653a73dSFam Zheng * header is flushed. 3586653a73dSFam Zheng */ 3596653a73dSFam Zheng if (s->need_check_timer && timer_pending(s->need_check_timer)) { 3606653a73dSFam Zheng qed_cancel_need_check_timer(s); 3616653a73dSFam Zheng qed_need_check_timer_cb(s); 3626653a73dSFam Zheng } 3636653a73dSFam Zheng } 3646653a73dSFam Zheng 3654e4bf5c4SKevin Wolf static int bdrv_qed_do_open(BlockDriverState *bs, QDict *options, int flags, 366015a1036SMax Reitz Error **errp) 36775411d23SStefan Hajnoczi { 36875411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 36975411d23SStefan Hajnoczi QEDHeader le_header; 37075411d23SStefan Hajnoczi int64_t file_size; 37175411d23SStefan Hajnoczi int ret; 37275411d23SStefan Hajnoczi 37375411d23SStefan Hajnoczi s->bs = bs; 3740806c3b5SKevin Wolf qemu_co_queue_init(&s->allocating_write_reqs); 37575411d23SStefan Hajnoczi 376cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); 37775411d23SStefan Hajnoczi if (ret < 0) { 37875411d23SStefan Hajnoczi return ret; 37975411d23SStefan Hajnoczi } 38075411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header); 38175411d23SStefan Hajnoczi 38275411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) { 38376abe407SPaolo Bonzini error_setg(errp, "Image not in QED format"); 38476abe407SPaolo Bonzini return -EINVAL; 38575411d23SStefan Hajnoczi } 38675411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) { 38710b758e8SKevin Wolf /* image uses unsupported feature bits */ 388a55448b3SMax Reitz error_setg(errp, "Unsupported QED features: %" PRIx64, 38910b758e8SKevin Wolf s->header.features & ~QED_FEATURE_MASK); 39010b758e8SKevin Wolf return -ENOTSUP; 39175411d23SStefan Hajnoczi } 39275411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 39375411d23SStefan Hajnoczi return -EINVAL; 39475411d23SStefan Hajnoczi } 39575411d23SStefan Hajnoczi 39675411d23SStefan Hajnoczi /* Round down file size to the last cluster */ 3979a4f4c31SKevin Wolf file_size = bdrv_getlength(bs->file->bs); 39875411d23SStefan Hajnoczi if (file_size < 0) { 39975411d23SStefan Hajnoczi return file_size; 40075411d23SStefan Hajnoczi } 40175411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size); 40275411d23SStefan Hajnoczi 40375411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) { 40475411d23SStefan Hajnoczi return -EINVAL; 40575411d23SStefan Hajnoczi } 40675411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size, 40775411d23SStefan Hajnoczi s->header.cluster_size, 40875411d23SStefan Hajnoczi s->header.table_size)) { 40975411d23SStefan Hajnoczi return -EINVAL; 41075411d23SStefan Hajnoczi } 41175411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 41275411d23SStefan Hajnoczi return -EINVAL; 41375411d23SStefan Hajnoczi } 41475411d23SStefan Hajnoczi 41575411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) / 41675411d23SStefan Hajnoczi sizeof(uint64_t); 417786a4ea8SStefan Hajnoczi s->l2_shift = ctz32(s->header.cluster_size); 41875411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1; 419786a4ea8SStefan Hajnoczi s->l1_shift = s->l2_shift + ctz32(s->table_nelems); 42075411d23SStefan Hajnoczi 4210adfa1edSStefan Hajnoczi /* Header size calculation must not overflow uint32_t */ 4220adfa1edSStefan Hajnoczi if (s->header.header_size > UINT32_MAX / s->header.cluster_size) { 4230adfa1edSStefan Hajnoczi return -EINVAL; 4240adfa1edSStefan Hajnoczi } 4250adfa1edSStefan Hajnoczi 42675411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) { 42775411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset + 42875411d23SStefan Hajnoczi s->header.backing_filename_size > 42975411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) { 43075411d23SStefan Hajnoczi return -EINVAL; 43175411d23SStefan Hajnoczi } 43275411d23SStefan Hajnoczi 433cf2ab8fcSKevin Wolf ret = qed_read_string(bs->file, s->header.backing_filename_offset, 43475411d23SStefan Hajnoczi s->header.backing_filename_size, bs->backing_file, 43575411d23SStefan Hajnoczi sizeof(bs->backing_file)); 43675411d23SStefan Hajnoczi if (ret < 0) { 43775411d23SStefan Hajnoczi return ret; 43875411d23SStefan Hajnoczi } 43975411d23SStefan Hajnoczi 44075411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 44175411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 44275411d23SStefan Hajnoczi } 44375411d23SStefan Hajnoczi } 44475411d23SStefan Hajnoczi 44575411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards 44675411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older 44775411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is 44875411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear 44975411d23SStefan Hajnoczi * feature is no longer valid. 45075411d23SStefan Hajnoczi */ 45175411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 45204c01a5cSKevin Wolf !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) { 45375411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 45475411d23SStefan Hajnoczi 45575411d23SStefan Hajnoczi ret = qed_write_header_sync(s); 45675411d23SStefan Hajnoczi if (ret) { 45775411d23SStefan Hajnoczi return ret; 45875411d23SStefan Hajnoczi } 45975411d23SStefan Hajnoczi 46075411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */ 4619a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 46275411d23SStefan Hajnoczi } 46375411d23SStefan Hajnoczi 464298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s); 465298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache); 466298800caSStefan Hajnoczi 467298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s); 468298800caSStefan Hajnoczi if (ret) { 46901979a98SStefan Hajnoczi goto out; 47001979a98SStefan Hajnoczi } 47101979a98SStefan Hajnoczi 47201979a98SStefan Hajnoczi /* If image was not closed cleanly, check consistency */ 473058f8f16SStefan Hajnoczi if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) { 47401979a98SStefan Hajnoczi /* Read-only images cannot be fixed. There is no risk of corruption 47501979a98SStefan Hajnoczi * since write operations are not possible. Therefore, allow 47601979a98SStefan Hajnoczi * potentially inconsistent images to be opened read-only. This can 47701979a98SStefan Hajnoczi * aid data recovery from an otherwise inconsistent image. 47801979a98SStefan Hajnoczi */ 4799a4f4c31SKevin Wolf if (!bdrv_is_read_only(bs->file->bs) && 48004c01a5cSKevin Wolf !(flags & BDRV_O_INACTIVE)) { 48101979a98SStefan Hajnoczi BdrvCheckResult result = {0}; 48201979a98SStefan Hajnoczi 48301979a98SStefan Hajnoczi ret = qed_check(s, &result, true); 4846f321e93SStefan Hajnoczi if (ret) { 4856f321e93SStefan Hajnoczi goto out; 4866f321e93SStefan Hajnoczi } 48701979a98SStefan Hajnoczi } 48801979a98SStefan Hajnoczi } 48901979a98SStefan Hajnoczi 490a8c868c3SStefan Hajnoczi bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs)); 4916f321e93SStefan Hajnoczi 49201979a98SStefan Hajnoczi out: 49301979a98SStefan Hajnoczi if (ret) { 494298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 495298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 496298800caSStefan Hajnoczi } 49775411d23SStefan Hajnoczi return ret; 49875411d23SStefan Hajnoczi } 49975411d23SStefan Hajnoczi 5004e4bf5c4SKevin Wolf static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags, 5014e4bf5c4SKevin Wolf Error **errp) 5024e4bf5c4SKevin Wolf { 5034e4bf5c4SKevin Wolf bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, 5044e4bf5c4SKevin Wolf false, errp); 5054e4bf5c4SKevin Wolf if (!bs->file) { 5064e4bf5c4SKevin Wolf return -EINVAL; 5074e4bf5c4SKevin Wolf } 5084e4bf5c4SKevin Wolf 5094e4bf5c4SKevin Wolf return bdrv_qed_do_open(bs, options, flags, errp); 5104e4bf5c4SKevin Wolf } 5114e4bf5c4SKevin Wolf 5123baca891SKevin Wolf static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp) 513d34682cdSKevin Wolf { 514d34682cdSKevin Wolf BDRVQEDState *s = bs->opaque; 515d34682cdSKevin Wolf 516cf081fcaSEric Blake bs->bl.pwrite_zeroes_alignment = s->header.cluster_size; 517d34682cdSKevin Wolf } 518d34682cdSKevin Wolf 519f9cb20f1SJeff Cody /* We have nothing to do for QED reopen, stubs just return 520f9cb20f1SJeff Cody * success */ 521f9cb20f1SJeff Cody static int bdrv_qed_reopen_prepare(BDRVReopenState *state, 522f9cb20f1SJeff Cody BlockReopenQueue *queue, Error **errp) 523f9cb20f1SJeff Cody { 524f9cb20f1SJeff Cody return 0; 525f9cb20f1SJeff Cody } 526f9cb20f1SJeff Cody 52775411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 52875411d23SStefan Hajnoczi { 529298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 530298800caSStefan Hajnoczi 531a8c868c3SStefan Hajnoczi bdrv_qed_detach_aio_context(bs); 5326f321e93SStefan Hajnoczi 53301979a98SStefan Hajnoczi /* Ensure writes reach stable storage */ 5349a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 53501979a98SStefan Hajnoczi 53601979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */ 53701979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 53801979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 53901979a98SStefan Hajnoczi qed_write_header_sync(s); 54001979a98SStefan Hajnoczi } 54101979a98SStefan Hajnoczi 542298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 543298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 54475411d23SStefan Hajnoczi } 54575411d23SStefan Hajnoczi 54675411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size, 54775411d23SStefan Hajnoczi uint64_t image_size, uint32_t table_size, 5480fea6b79SPaolo Bonzini const char *backing_file, const char *backing_fmt, 5494ab15590SChunyan Liu QemuOpts *opts, Error **errp) 55075411d23SStefan Hajnoczi { 55175411d23SStefan Hajnoczi QEDHeader header = { 55275411d23SStefan Hajnoczi .magic = QED_MAGIC, 55375411d23SStefan Hajnoczi .cluster_size = cluster_size, 55475411d23SStefan Hajnoczi .table_size = table_size, 55575411d23SStefan Hajnoczi .header_size = 1, 55675411d23SStefan Hajnoczi .features = 0, 55775411d23SStefan Hajnoczi .compat_features = 0, 55875411d23SStefan Hajnoczi .l1_table_offset = cluster_size, 55975411d23SStefan Hajnoczi .image_size = image_size, 56075411d23SStefan Hajnoczi }; 56175411d23SStefan Hajnoczi QEDHeader le_header; 56275411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 56375411d23SStefan Hajnoczi size_t l1_size = header.cluster_size * header.table_size; 56434b5d2c6SMax Reitz Error *local_err = NULL; 56575411d23SStefan Hajnoczi int ret = 0; 5668a56fdadSKevin Wolf BlockBackend *blk; 56775411d23SStefan Hajnoczi 5684ab15590SChunyan Liu ret = bdrv_create_file(filename, opts, &local_err); 56975411d23SStefan Hajnoczi if (ret < 0) { 5700fea6b79SPaolo Bonzini error_propagate(errp, local_err); 57175411d23SStefan Hajnoczi return ret; 57275411d23SStefan Hajnoczi } 57375411d23SStefan Hajnoczi 574efaa7c4eSMax Reitz blk = blk_new_open(filename, NULL, NULL, 57555880601SKevin Wolf BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, 57655880601SKevin Wolf &local_err); 5778a56fdadSKevin Wolf if (blk == NULL) { 5780fea6b79SPaolo Bonzini error_propagate(errp, local_err); 5798a56fdadSKevin Wolf return -EIO; 58075411d23SStefan Hajnoczi } 58175411d23SStefan Hajnoczi 5828a56fdadSKevin Wolf blk_set_allow_write_beyond_eof(blk, true); 5838a56fdadSKevin Wolf 584c743849bSStefan Hajnoczi /* File must start empty and grow, check truncate is supported */ 585ed3d2ec9SMax Reitz ret = blk_truncate(blk, 0, errp); 586c743849bSStefan Hajnoczi if (ret < 0) { 587c743849bSStefan Hajnoczi goto out; 588c743849bSStefan Hajnoczi } 589c743849bSStefan Hajnoczi 59075411d23SStefan Hajnoczi if (backing_file) { 59175411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 59275411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 59375411d23SStefan Hajnoczi header.backing_filename_size = strlen(backing_file); 59475411d23SStefan Hajnoczi 59575411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 59675411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 59775411d23SStefan Hajnoczi } 59875411d23SStefan Hajnoczi } 59975411d23SStefan Hajnoczi 60075411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 6018341f00dSEric Blake ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0); 60275411d23SStefan Hajnoczi if (ret < 0) { 60375411d23SStefan Hajnoczi goto out; 60475411d23SStefan Hajnoczi } 6058a56fdadSKevin Wolf ret = blk_pwrite(blk, sizeof(le_header), backing_file, 6068341f00dSEric Blake header.backing_filename_size, 0); 60775411d23SStefan Hajnoczi if (ret < 0) { 60875411d23SStefan Hajnoczi goto out; 60975411d23SStefan Hajnoczi } 61075411d23SStefan Hajnoczi 6117267c094SAnthony Liguori l1_table = g_malloc0(l1_size); 6128341f00dSEric Blake ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0); 61375411d23SStefan Hajnoczi if (ret < 0) { 61475411d23SStefan Hajnoczi goto out; 61575411d23SStefan Hajnoczi } 61675411d23SStefan Hajnoczi 61775411d23SStefan Hajnoczi ret = 0; /* success */ 61875411d23SStefan Hajnoczi out: 6197267c094SAnthony Liguori g_free(l1_table); 6208a56fdadSKevin Wolf blk_unref(blk); 62175411d23SStefan Hajnoczi return ret; 62275411d23SStefan Hajnoczi } 62375411d23SStefan Hajnoczi 6247ab74849SChunyan Liu static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp) 62575411d23SStefan Hajnoczi { 62675411d23SStefan Hajnoczi uint64_t image_size = 0; 62775411d23SStefan Hajnoczi uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 62875411d23SStefan Hajnoczi uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 6297ab74849SChunyan Liu char *backing_file = NULL; 6307ab74849SChunyan Liu char *backing_fmt = NULL; 6317ab74849SChunyan Liu int ret; 63275411d23SStefan Hajnoczi 633c2eb918eSHu Tao image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 634c2eb918eSHu Tao BDRV_SECTOR_SIZE); 6357ab74849SChunyan Liu backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); 6367ab74849SChunyan Liu backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); 6377ab74849SChunyan Liu cluster_size = qemu_opt_get_size_del(opts, 6387ab74849SChunyan Liu BLOCK_OPT_CLUSTER_SIZE, 6397ab74849SChunyan Liu QED_DEFAULT_CLUSTER_SIZE); 6407ab74849SChunyan Liu table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE, 6417ab74849SChunyan Liu QED_DEFAULT_TABLE_SIZE); 64275411d23SStefan Hajnoczi 64375411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(cluster_size)) { 6445ff679b4SAakriti Gupta error_setg(errp, "QED cluster size must be within range [%u, %u] " 6455ff679b4SAakriti Gupta "and power of 2", 64675411d23SStefan Hajnoczi QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 6477ab74849SChunyan Liu ret = -EINVAL; 6487ab74849SChunyan Liu goto finish; 64975411d23SStefan Hajnoczi } 65075411d23SStefan Hajnoczi if (!qed_is_table_size_valid(table_size)) { 6515ff679b4SAakriti Gupta error_setg(errp, "QED table size must be within range [%u, %u] " 6525ff679b4SAakriti Gupta "and power of 2", 65375411d23SStefan Hajnoczi QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 6547ab74849SChunyan Liu ret = -EINVAL; 6557ab74849SChunyan Liu goto finish; 65675411d23SStefan Hajnoczi } 65775411d23SStefan Hajnoczi if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 6585ff679b4SAakriti Gupta error_setg(errp, "QED image size must be a non-zero multiple of " 6595ff679b4SAakriti Gupta "cluster size and less than %" PRIu64 " bytes", 66075411d23SStefan Hajnoczi qed_max_image_size(cluster_size, table_size)); 6617ab74849SChunyan Liu ret = -EINVAL; 6627ab74849SChunyan Liu goto finish; 66375411d23SStefan Hajnoczi } 66475411d23SStefan Hajnoczi 6657ab74849SChunyan Liu ret = qed_create(filename, cluster_size, image_size, table_size, 6664ab15590SChunyan Liu backing_file, backing_fmt, opts, errp); 6677ab74849SChunyan Liu 6687ab74849SChunyan Liu finish: 6697ab74849SChunyan Liu g_free(backing_file); 6707ab74849SChunyan Liu g_free(backing_fmt); 6717ab74849SChunyan Liu return ret; 67275411d23SStefan Hajnoczi } 67375411d23SStefan Hajnoczi 674298800caSStefan Hajnoczi typedef struct { 6754bc74be9SPaolo Bonzini BlockDriverState *bs; 676b7d5a5b8SStefan Hajnoczi Coroutine *co; 6774bc74be9SPaolo Bonzini uint64_t pos; 6784bc74be9SPaolo Bonzini int64_t status; 679298800caSStefan Hajnoczi int *pnum; 68053f1dfd1SFam Zheng BlockDriverState **file; 681298800caSStefan Hajnoczi } QEDIsAllocatedCB; 682298800caSStefan Hajnoczi 683298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 684298800caSStefan Hajnoczi { 685298800caSStefan Hajnoczi QEDIsAllocatedCB *cb = opaque; 6864bc74be9SPaolo Bonzini BDRVQEDState *s = cb->bs->opaque; 687298800caSStefan Hajnoczi *cb->pnum = len / BDRV_SECTOR_SIZE; 6884bc74be9SPaolo Bonzini switch (ret) { 6894bc74be9SPaolo Bonzini case QED_CLUSTER_FOUND: 6904bc74be9SPaolo Bonzini offset |= qed_offset_into_cluster(s, cb->pos); 6914bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; 69253f1dfd1SFam Zheng *cb->file = cb->bs->file->bs; 6934bc74be9SPaolo Bonzini break; 6944bc74be9SPaolo Bonzini case QED_CLUSTER_ZERO: 6954bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_ZERO; 6964bc74be9SPaolo Bonzini break; 6974bc74be9SPaolo Bonzini case QED_CLUSTER_L2: 6984bc74be9SPaolo Bonzini case QED_CLUSTER_L1: 6994bc74be9SPaolo Bonzini cb->status = 0; 7004bc74be9SPaolo Bonzini break; 7014bc74be9SPaolo Bonzini default: 7024bc74be9SPaolo Bonzini assert(ret < 0); 7034bc74be9SPaolo Bonzini cb->status = ret; 7044bc74be9SPaolo Bonzini break; 7054bc74be9SPaolo Bonzini } 7064bc74be9SPaolo Bonzini 707b7d5a5b8SStefan Hajnoczi if (cb->co) { 708b9e413ddSPaolo Bonzini aio_co_wake(cb->co); 709b7d5a5b8SStefan Hajnoczi } 710298800caSStefan Hajnoczi } 711298800caSStefan Hajnoczi 712b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs, 713b7d5a5b8SStefan Hajnoczi int64_t sector_num, 71467a0fd2aSFam Zheng int nb_sectors, int *pnum, 71567a0fd2aSFam Zheng BlockDriverState **file) 71675411d23SStefan Hajnoczi { 717298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 718298800caSStefan Hajnoczi size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 719298800caSStefan Hajnoczi QEDIsAllocatedCB cb = { 7204bc74be9SPaolo Bonzini .bs = bs, 7214bc74be9SPaolo Bonzini .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE, 7224bc74be9SPaolo Bonzini .status = BDRV_BLOCK_OFFSET_MASK, 723298800caSStefan Hajnoczi .pnum = pnum, 72453f1dfd1SFam Zheng .file = file, 725298800caSStefan Hajnoczi }; 726298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 7270f21b7a1SKevin Wolf uint64_t offset; 7280f21b7a1SKevin Wolf int ret; 729298800caSStefan Hajnoczi 7300f21b7a1SKevin Wolf ret = qed_find_cluster(s, &request, cb.pos, &len, &offset); 7310f21b7a1SKevin Wolf qed_is_allocated_cb(&cb, ret, offset, len); 732298800caSStefan Hajnoczi 7330f21b7a1SKevin Wolf /* The callback was invoked immediately */ 7340f21b7a1SKevin Wolf assert(cb.status != BDRV_BLOCK_OFFSET_MASK); 735298800caSStefan Hajnoczi 736298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 737298800caSStefan Hajnoczi 7384bc74be9SPaolo Bonzini return cb.status; 73975411d23SStefan Hajnoczi } 74075411d23SStefan Hajnoczi 741eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 742eabba580SStefan Hajnoczi { 743*48cc565eSKevin Wolf return acb->bs->opaque; 744eabba580SStefan Hajnoczi } 745eabba580SStefan Hajnoczi 746eabba580SStefan Hajnoczi /** 747eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 748eabba580SStefan Hajnoczi * 749eabba580SStefan Hajnoczi * @s: QED state 750eabba580SStefan Hajnoczi * @pos: Byte position in device 751eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 752f06ee3d4SKevin Wolf * @backing_qiov: Possibly shortened copy of qiov, to be allocated here 753eabba580SStefan Hajnoczi * @cb: Completion function 754eabba580SStefan Hajnoczi * @opaque: User data for completion function 755eabba580SStefan Hajnoczi * 756eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 757eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 758eabba580SStefan Hajnoczi */ 759e85c5281SKevin Wolf static int qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 760eabba580SStefan Hajnoczi QEMUIOVector *qiov, 761e85c5281SKevin Wolf QEMUIOVector **backing_qiov) 762eabba580SStefan Hajnoczi { 763eabba580SStefan Hajnoczi uint64_t backing_length = 0; 764eabba580SStefan Hajnoczi size_t size; 765e85c5281SKevin Wolf int ret; 766eabba580SStefan Hajnoczi 767eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 768eabba580SStefan Hajnoczi * backing file like a zero length backing file. 769eabba580SStefan Hajnoczi */ 770760e0063SKevin Wolf if (s->bs->backing) { 771760e0063SKevin Wolf int64_t l = bdrv_getlength(s->bs->backing->bs); 772eabba580SStefan Hajnoczi if (l < 0) { 773e85c5281SKevin Wolf return l; 774eabba580SStefan Hajnoczi } 775eabba580SStefan Hajnoczi backing_length = l; 776eabba580SStefan Hajnoczi } 777eabba580SStefan Hajnoczi 778eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 779eabba580SStefan Hajnoczi if (pos >= backing_length || 780eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 7813d9b4925SMichael Tokarev qemu_iovec_memset(qiov, 0, 0, qiov->size); 782eabba580SStefan Hajnoczi } 783eabba580SStefan Hajnoczi 784eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 785eabba580SStefan Hajnoczi if (pos >= backing_length) { 786e85c5281SKevin Wolf return 0; 787eabba580SStefan Hajnoczi } 788eabba580SStefan Hajnoczi 789eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 790eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 791eabba580SStefan Hajnoczi 792f06ee3d4SKevin Wolf assert(*backing_qiov == NULL); 793f06ee3d4SKevin Wolf *backing_qiov = g_new(QEMUIOVector, 1); 794f06ee3d4SKevin Wolf qemu_iovec_init(*backing_qiov, qiov->niov); 795f06ee3d4SKevin Wolf qemu_iovec_concat(*backing_qiov, qiov, 0, size); 796f06ee3d4SKevin Wolf 797820100fdSPaolo Bonzini BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); 798e85c5281SKevin Wolf ret = bdrv_preadv(s->bs->backing, pos, *backing_qiov); 799e85c5281SKevin Wolf if (ret < 0) { 800e85c5281SKevin Wolf return ret; 801e85c5281SKevin Wolf } 802e85c5281SKevin Wolf return 0; 803eabba580SStefan Hajnoczi } 804eabba580SStefan Hajnoczi 805eabba580SStefan Hajnoczi /** 806eabba580SStefan Hajnoczi * Copy data from backing file into the image 807eabba580SStefan Hajnoczi * 808eabba580SStefan Hajnoczi * @s: QED state 809eabba580SStefan Hajnoczi * @pos: Byte position in device 810eabba580SStefan Hajnoczi * @len: Number of bytes 811eabba580SStefan Hajnoczi * @offset: Byte offset in image file 812eabba580SStefan Hajnoczi */ 813b4ac32f3SKevin Wolf static int qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, 814b4ac32f3SKevin Wolf uint64_t len, uint64_t offset) 815eabba580SStefan Hajnoczi { 8160f7aa24dSKevin Wolf QEMUIOVector qiov; 8170f7aa24dSKevin Wolf QEMUIOVector *backing_qiov = NULL; 8180f7aa24dSKevin Wolf struct iovec iov; 819e85c5281SKevin Wolf int ret; 820eabba580SStefan Hajnoczi 821eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 822eabba580SStefan Hajnoczi if (len == 0) { 823b4ac32f3SKevin Wolf return 0; 824eabba580SStefan Hajnoczi } 825eabba580SStefan Hajnoczi 8260f7aa24dSKevin Wolf iov = (struct iovec) { 8270f7aa24dSKevin Wolf .iov_base = qemu_blockalign(s->bs, len), 8280f7aa24dSKevin Wolf .iov_len = len, 8290f7aa24dSKevin Wolf }; 8300f7aa24dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 831eabba580SStefan Hajnoczi 8320f7aa24dSKevin Wolf ret = qed_read_backing_file(s, pos, &qiov, &backing_qiov); 8330f7aa24dSKevin Wolf 8340f7aa24dSKevin Wolf if (backing_qiov) { 8350f7aa24dSKevin Wolf qemu_iovec_destroy(backing_qiov); 8360f7aa24dSKevin Wolf g_free(backing_qiov); 8370f7aa24dSKevin Wolf backing_qiov = NULL; 8380f7aa24dSKevin Wolf } 8390f7aa24dSKevin Wolf 8400f7aa24dSKevin Wolf if (ret) { 8410f7aa24dSKevin Wolf goto out; 8420f7aa24dSKevin Wolf } 8430f7aa24dSKevin Wolf 8440f7aa24dSKevin Wolf BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 8450f7aa24dSKevin Wolf ret = bdrv_pwritev(s->bs->file, offset, &qiov); 8460f7aa24dSKevin Wolf if (ret < 0) { 8470f7aa24dSKevin Wolf goto out; 8480f7aa24dSKevin Wolf } 8490f7aa24dSKevin Wolf ret = 0; 8500f7aa24dSKevin Wolf out: 8510f7aa24dSKevin Wolf qemu_vfree(iov.iov_base); 852b4ac32f3SKevin Wolf return ret; 853eabba580SStefan Hajnoczi } 854eabba580SStefan Hajnoczi 855eabba580SStefan Hajnoczi /** 856eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 857eabba580SStefan Hajnoczi * 858eabba580SStefan Hajnoczi * @s: QED state 859eabba580SStefan Hajnoczi * @table: L2 table 860eabba580SStefan Hajnoczi * @index: First cluster index 861eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 86221df65b6SAnthony Liguori * @cluster: First cluster offset 86321df65b6SAnthony Liguori * 86421df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the 86521df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker. 866eabba580SStefan Hajnoczi */ 867eabba580SStefan Hajnoczi static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index, 868eabba580SStefan Hajnoczi unsigned int n, uint64_t cluster) 869eabba580SStefan Hajnoczi { 870eabba580SStefan Hajnoczi int i; 871eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 872eabba580SStefan Hajnoczi table->offsets[i] = cluster; 87321df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) && 87421df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) { 875eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 876eabba580SStefan Hajnoczi } 877eabba580SStefan Hajnoczi } 87821df65b6SAnthony Liguori } 879eabba580SStefan Hajnoczi 880*48cc565eSKevin Wolf static void qed_aio_complete(QEDAIOCB *acb) 881eabba580SStefan Hajnoczi { 882eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 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 8880e71be19SStefan Hajnoczi /* Free the buffer we may have allocated for zero writes */ 8890e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 8900e71be19SStefan Hajnoczi qemu_vfree(acb->qiov->iov[0].iov_base); 8910e71be19SStefan Hajnoczi acb->qiov->iov[0].iov_base = NULL; 8920e71be19SStefan Hajnoczi } 8930e71be19SStefan Hajnoczi 894eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 895eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 896eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 897eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 898eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 899eabba580SStefan Hajnoczi */ 9000806c3b5SKevin Wolf if (acb == s->allocating_acb) { 9010806c3b5SKevin Wolf s->allocating_acb = NULL; 9020806c3b5SKevin Wolf if (!qemu_co_queue_empty(&s->allocating_write_reqs)) { 9030806c3b5SKevin Wolf qemu_co_enter_next(&s->allocating_write_reqs); 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 /** 911fae25ac7SKevin Wolf * Update L1 table with new L2 table offset and write it out 912eabba580SStefan Hajnoczi */ 913fb18de21SKevin Wolf static int qed_aio_write_l1_update(QEDAIOCB *acb) 914eabba580SStefan Hajnoczi { 915eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 916eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 917e4fc8781SStefan Hajnoczi uint64_t l2_offset = l2_table->offset; 918fb18de21SKevin Wolf int index, ret; 919fae25ac7SKevin Wolf 920fae25ac7SKevin Wolf index = qed_l1_index(s, acb->cur_pos); 921fae25ac7SKevin Wolf s->l1_table->offsets[index] = l2_table->offset; 922fae25ac7SKevin Wolf 923fae25ac7SKevin Wolf ret = qed_write_l1_table(s, index, 1); 924fae25ac7SKevin Wolf 925fae25ac7SKevin Wolf /* Commit the current L2 table to the cache */ 926eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 927eabba580SStefan Hajnoczi 928eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 929eabba580SStefan Hajnoczi * cache. 930eabba580SStefan Hajnoczi */ 931e4fc8781SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 932eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 933eabba580SStefan Hajnoczi 934fb18de21SKevin Wolf return ret; 935eabba580SStefan Hajnoczi } 936eabba580SStefan Hajnoczi 937eabba580SStefan Hajnoczi 938eabba580SStefan Hajnoczi /** 939eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 940eabba580SStefan Hajnoczi */ 94188d2dd72SKevin Wolf static int qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset) 942eabba580SStefan Hajnoczi { 943eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 944eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 94588d2dd72SKevin Wolf int index, ret; 946eabba580SStefan Hajnoczi 947eabba580SStefan Hajnoczi if (need_alloc) { 948eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 949eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 950eabba580SStefan Hajnoczi } 951eabba580SStefan Hajnoczi 952eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 953eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 9540e71be19SStefan Hajnoczi offset); 955eabba580SStefan Hajnoczi 956eabba580SStefan Hajnoczi if (need_alloc) { 957eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 958453e53e2SKevin Wolf ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true); 959fb18de21SKevin Wolf if (ret) { 96088d2dd72SKevin Wolf return ret; 961fb18de21SKevin Wolf } 96288d2dd72SKevin Wolf return qed_aio_write_l1_update(acb); 963eabba580SStefan Hajnoczi } else { 964eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 965453e53e2SKevin Wolf ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, 966453e53e2SKevin Wolf false); 96788d2dd72SKevin Wolf if (ret) { 96888d2dd72SKevin Wolf return ret; 969eabba580SStefan Hajnoczi } 97088d2dd72SKevin Wolf } 97188d2dd72SKevin Wolf return 0; 972eabba580SStefan Hajnoczi } 973eabba580SStefan Hajnoczi 974eabba580SStefan Hajnoczi /** 975eabba580SStefan Hajnoczi * Write data to the image file 976eabba580SStefan Hajnoczi */ 977eaf0bc56SKevin Wolf static int qed_aio_write_main(QEDAIOCB *acb) 978eabba580SStefan Hajnoczi { 979eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 980eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 981eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 982eaf0bc56SKevin Wolf int ret; 983eabba580SStefan Hajnoczi 984eaf0bc56SKevin Wolf trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size); 985eabba580SStefan Hajnoczi 986a4d8f1aeSKevin Wolf BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 987a4d8f1aeSKevin Wolf ret = bdrv_pwritev(s->bs->file, offset, &acb->cur_qiov); 988eaf0bc56SKevin Wolf if (ret < 0) { 989eaf0bc56SKevin Wolf return ret; 990eabba580SStefan Hajnoczi } 991eabba580SStefan Hajnoczi 992eaf0bc56SKevin Wolf if (acb->find_cluster_ret != QED_CLUSTER_FOUND) { 993a4d8f1aeSKevin Wolf if (s->bs->backing) { 994a4d8f1aeSKevin Wolf /* 995a4d8f1aeSKevin Wolf * Flush new data clusters before updating the L2 table 996a4d8f1aeSKevin Wolf * 997a4d8f1aeSKevin Wolf * This flush is necessary when a backing file is in use. A crash 998a4d8f1aeSKevin Wolf * during an allocating write could result in empty clusters in the 999a4d8f1aeSKevin Wolf * image. If the write only touched a subregion of the cluster, 1000a4d8f1aeSKevin Wolf * then backing image sectors have been lost in the untouched 1001a4d8f1aeSKevin Wolf * region. The solution is to flush after writing a new data 1002a4d8f1aeSKevin Wolf * cluster and before updating the L2 table. 1003a4d8f1aeSKevin Wolf */ 1004a4d8f1aeSKevin Wolf ret = bdrv_flush(s->bs->file->bs); 1005eaf0bc56SKevin Wolf if (ret < 0) { 1006eaf0bc56SKevin Wolf return ret; 1007a4d8f1aeSKevin Wolf } 1008a4d8f1aeSKevin Wolf } 100988d2dd72SKevin Wolf ret = qed_aio_write_l2_update(acb, acb->cur_cluster); 1010eaf0bc56SKevin Wolf if (ret < 0) { 1011eaf0bc56SKevin Wolf return ret; 101288d2dd72SKevin Wolf } 101388d2dd72SKevin Wolf } 1014eaf0bc56SKevin Wolf return 0; 1015eabba580SStefan Hajnoczi } 1016eabba580SStefan Hajnoczi 1017eabba580SStefan Hajnoczi /** 1018b4ac32f3SKevin Wolf * Populate untouched regions of new data cluster 1019eabba580SStefan Hajnoczi */ 1020a101341aSKevin Wolf static int qed_aio_write_cow(QEDAIOCB *acb) 1021eabba580SStefan Hajnoczi { 1022eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1023b4ac32f3SKevin Wolf uint64_t start, len, offset; 1024a101341aSKevin Wolf int ret; 1025eabba580SStefan Hajnoczi 1026b4ac32f3SKevin Wolf /* Populate front untouched region of new data cluster */ 1027b4ac32f3SKevin Wolf start = qed_start_of_cluster(s, acb->cur_pos); 1028b4ac32f3SKevin Wolf len = qed_offset_into_cluster(s, acb->cur_pos); 1029b4ac32f3SKevin Wolf 1030b4ac32f3SKevin Wolf trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1031b4ac32f3SKevin Wolf ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster); 1032a101341aSKevin Wolf if (ret < 0) { 1033a101341aSKevin Wolf return ret; 1034eabba580SStefan Hajnoczi } 1035eabba580SStefan Hajnoczi 1036b4ac32f3SKevin Wolf /* Populate back untouched region of new data cluster */ 1037b4ac32f3SKevin Wolf start = acb->cur_pos + acb->cur_qiov.size; 1038b4ac32f3SKevin Wolf len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1039b4ac32f3SKevin Wolf offset = acb->cur_cluster + 1040b4ac32f3SKevin Wolf qed_offset_into_cluster(s, acb->cur_pos) + 1041b4ac32f3SKevin Wolf acb->cur_qiov.size; 1042b4ac32f3SKevin Wolf 1043eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 1044b4ac32f3SKevin Wolf ret = qed_copy_from_backing_file(s, start, len, offset); 1045a101341aSKevin Wolf if (ret < 0) { 1046a101341aSKevin Wolf return ret; 1047eaf0bc56SKevin Wolf } 1048eabba580SStefan Hajnoczi 1049a101341aSKevin Wolf return qed_aio_write_main(acb); 1050eabba580SStefan Hajnoczi } 1051eabba580SStefan Hajnoczi 1052eabba580SStefan Hajnoczi /** 10530d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write 10540d09c797SStefan Hajnoczi */ 10550d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s) 10560d09c797SStefan Hajnoczi { 10570d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */ 1058760e0063SKevin Wolf if (s->bs->backing) { 10590d09c797SStefan Hajnoczi return false; 10600d09c797SStefan Hajnoczi } 10610d09c797SStefan Hajnoczi 10620d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK); 10630d09c797SStefan Hajnoczi } 10640d09c797SStefan Hajnoczi 10650d09c797SStefan Hajnoczi /** 1066eabba580SStefan Hajnoczi * Write new data cluster 1067eabba580SStefan Hajnoczi * 1068eabba580SStefan Hajnoczi * @acb: Write request 1069eabba580SStefan Hajnoczi * @len: Length in bytes 1070eabba580SStefan Hajnoczi * 1071eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters. 1072eabba580SStefan Hajnoczi */ 1073d6daddcdSKevin Wolf static int qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1074eabba580SStefan Hajnoczi { 1075eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1076f13d712bSKevin Wolf int ret; 1077eabba580SStefan Hajnoczi 10786f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */ 10790806c3b5SKevin Wolf if (s->allocating_acb == NULL) { 10806f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 10816f321e93SStefan Hajnoczi } 10826f321e93SStefan Hajnoczi 1083eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 10840806c3b5SKevin Wolf if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) { 10850806c3b5SKevin Wolf if (s->allocating_acb != NULL) { 10860806c3b5SKevin Wolf qemu_co_queue_wait(&s->allocating_write_reqs, NULL); 10870806c3b5SKevin Wolf assert(s->allocating_acb == NULL); 1088eabba580SStefan Hajnoczi } 10890806c3b5SKevin Wolf s->allocating_acb = acb; 10900806c3b5SKevin Wolf return -EAGAIN; /* start over with looking up table entries */ 1091eabba580SStefan Hajnoczi } 1092eabba580SStefan Hajnoczi 1093eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 1094eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 10951b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1096eabba580SStefan Hajnoczi 10970e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 10980e71be19SStefan Hajnoczi /* Skip ahead if the clusters are already zero */ 10990e71be19SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { 1100d6daddcdSKevin Wolf return 0; 11010e71be19SStefan Hajnoczi } 11020e71be19SStefan Hajnoczi } else { 11030e71be19SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 11040e71be19SStefan Hajnoczi } 11050e71be19SStefan Hajnoczi 11060d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) { 110701979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK; 1108f13d712bSKevin Wolf ret = qed_write_header(s); 1109a101341aSKevin Wolf if (ret < 0) { 1110d6daddcdSKevin Wolf return ret; 11110d09c797SStefan Hajnoczi } 1112eabba580SStefan Hajnoczi } 1113eabba580SStefan Hajnoczi 1114a101341aSKevin Wolf if (acb->flags & QED_AIOCB_ZERO) { 1115a101341aSKevin Wolf ret = qed_aio_write_l2_update(acb, 1); 1116a101341aSKevin Wolf } else { 1117a101341aSKevin Wolf ret = qed_aio_write_cow(acb); 1118a101341aSKevin Wolf } 1119a101341aSKevin Wolf if (ret < 0) { 1120d6daddcdSKevin Wolf return ret; 1121a101341aSKevin Wolf } 1122d6daddcdSKevin Wolf return 0; 1123a101341aSKevin Wolf } 1124a101341aSKevin Wolf 1125eabba580SStefan Hajnoczi /** 1126eabba580SStefan Hajnoczi * Write data cluster in place 1127eabba580SStefan Hajnoczi * 1128eabba580SStefan Hajnoczi * @acb: Write request 1129eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1130eabba580SStefan Hajnoczi * @len: Length in bytes 1131eabba580SStefan Hajnoczi * 1132eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 1133eabba580SStefan Hajnoczi */ 1134d6daddcdSKevin Wolf static int qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) 1135eabba580SStefan Hajnoczi { 11360e71be19SStefan Hajnoczi /* Allocate buffer for zero writes */ 11370e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 11380e71be19SStefan Hajnoczi struct iovec *iov = acb->qiov->iov; 11390e71be19SStefan Hajnoczi 11400e71be19SStefan Hajnoczi if (!iov->iov_base) { 1141*48cc565eSKevin Wolf iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len); 11424f4896dbSKevin Wolf if (iov->iov_base == NULL) { 1143d6daddcdSKevin Wolf return -ENOMEM; 11444f4896dbSKevin Wolf } 11450e71be19SStefan Hajnoczi memset(iov->iov_base, 0, iov->iov_len); 11460e71be19SStefan Hajnoczi } 11470e71be19SStefan Hajnoczi } 11480e71be19SStefan Hajnoczi 1149eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 1150eabba580SStefan Hajnoczi acb->cur_cluster = offset; 11511b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1152eabba580SStefan Hajnoczi 1153eabba580SStefan Hajnoczi /* Do the actual write */ 1154d6daddcdSKevin Wolf return qed_aio_write_main(acb); 1155eabba580SStefan Hajnoczi } 1156eabba580SStefan Hajnoczi 1157eabba580SStefan Hajnoczi /** 1158eabba580SStefan Hajnoczi * Write data cluster 1159eabba580SStefan Hajnoczi * 1160eabba580SStefan Hajnoczi * @opaque: Write request 11610596be7eSKevin Wolf * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 1162eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1163eabba580SStefan Hajnoczi * @len: Length in bytes 1164eabba580SStefan Hajnoczi */ 11650596be7eSKevin Wolf static int qed_aio_write_data(void *opaque, int ret, 1166eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1167eabba580SStefan Hajnoczi { 1168eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1169eabba580SStefan Hajnoczi 1170eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1171eabba580SStefan Hajnoczi 1172eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 1173eabba580SStefan Hajnoczi 1174eabba580SStefan Hajnoczi switch (ret) { 1175eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 11760596be7eSKevin Wolf return qed_aio_write_inplace(acb, offset, len); 1177eabba580SStefan Hajnoczi 1178eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 1179eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 118021df65b6SAnthony Liguori case QED_CLUSTER_ZERO: 11810596be7eSKevin Wolf return qed_aio_write_alloc(acb, len); 1182eabba580SStefan Hajnoczi 1183eabba580SStefan Hajnoczi default: 11840596be7eSKevin Wolf g_assert_not_reached(); 1185eabba580SStefan Hajnoczi } 1186eabba580SStefan Hajnoczi } 1187eabba580SStefan Hajnoczi 1188eabba580SStefan Hajnoczi /** 1189eabba580SStefan Hajnoczi * Read data cluster 1190eabba580SStefan Hajnoczi * 1191eabba580SStefan Hajnoczi * @opaque: Read request 11920596be7eSKevin Wolf * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 1193eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1194eabba580SStefan Hajnoczi * @len: Length in bytes 1195eabba580SStefan Hajnoczi */ 11960596be7eSKevin Wolf static int qed_aio_read_data(void *opaque, int ret, uint64_t offset, size_t len) 1197eabba580SStefan Hajnoczi { 1198eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1199eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1200*48cc565eSKevin Wolf BlockDriverState *bs = acb->bs; 1201eabba580SStefan Hajnoczi 1202eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 1203eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 1204eabba580SStefan Hajnoczi 1205eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 1206eabba580SStefan Hajnoczi 12071b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1208eabba580SStefan Hajnoczi 120921df65b6SAnthony Liguori /* Handle zero cluster and backing file reads */ 121021df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) { 12113d9b4925SMichael Tokarev qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); 12120596be7eSKevin Wolf return 0; 121321df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) { 12140596be7eSKevin Wolf return qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1215e85c5281SKevin Wolf &acb->backing_qiov); 1216eabba580SStefan Hajnoczi } 1217eabba580SStefan Hajnoczi 1218eabba580SStefan Hajnoczi BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 12193e248cdcSKevin Wolf ret = bdrv_preadv(bs->file, offset, &acb->cur_qiov); 12203e248cdcSKevin Wolf if (ret < 0) { 12210596be7eSKevin Wolf return ret; 12223e248cdcSKevin Wolf } 12230596be7eSKevin Wolf return 0; 1224eabba580SStefan Hajnoczi } 1225eabba580SStefan Hajnoczi 1226eabba580SStefan Hajnoczi /** 1227eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1228eabba580SStefan Hajnoczi */ 1229*48cc565eSKevin Wolf static int qed_aio_next_io(QEDAIOCB *acb) 1230eabba580SStefan Hajnoczi { 1231eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 12320f21b7a1SKevin Wolf uint64_t offset; 12330f21b7a1SKevin Wolf size_t len; 1234dddf8db1SKevin Wolf int ret; 1235eabba580SStefan Hajnoczi 123601859874SKevin Wolf while (1) { 1237dddf8db1SKevin Wolf trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size); 1238eabba580SStefan Hajnoczi 1239f06ee3d4SKevin Wolf if (acb->backing_qiov) { 1240f06ee3d4SKevin Wolf qemu_iovec_destroy(acb->backing_qiov); 1241f06ee3d4SKevin Wolf g_free(acb->backing_qiov); 1242f06ee3d4SKevin Wolf acb->backing_qiov = NULL; 1243f06ee3d4SKevin Wolf } 1244f06ee3d4SKevin Wolf 1245eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1246eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1247eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1248eabba580SStefan Hajnoczi 1249eabba580SStefan Hajnoczi /* Complete request */ 1250eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 1251*48cc565eSKevin Wolf ret = 0; 1252*48cc565eSKevin Wolf break; 1253eabba580SStefan Hajnoczi } 1254eabba580SStefan Hajnoczi 1255eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 12560f21b7a1SKevin Wolf len = acb->end_pos - acb->cur_pos; 12570f21b7a1SKevin Wolf ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset); 12580596be7eSKevin Wolf if (ret < 0) { 1259*48cc565eSKevin Wolf break; 12600596be7eSKevin Wolf } 12610596be7eSKevin Wolf 12620596be7eSKevin Wolf if (acb->flags & QED_AIOCB_WRITE) { 12630596be7eSKevin Wolf ret = qed_aio_write_data(acb, ret, offset, len); 12640596be7eSKevin Wolf } else { 12650596be7eSKevin Wolf ret = qed_aio_read_data(acb, ret, offset, len); 12660596be7eSKevin Wolf } 12670596be7eSKevin Wolf 12680806c3b5SKevin Wolf if (ret < 0 && ret != -EAGAIN) { 1269*48cc565eSKevin Wolf break; 127001859874SKevin Wolf } 1271eabba580SStefan Hajnoczi } 1272eabba580SStefan Hajnoczi 1273*48cc565eSKevin Wolf trace_qed_aio_complete(s, acb, ret); 1274*48cc565eSKevin Wolf qed_aio_complete(acb); 1275*48cc565eSKevin Wolf return ret; 127689f89709SKevin Wolf } 127789f89709SKevin Wolf 127889f89709SKevin Wolf static int coroutine_fn qed_co_request(BlockDriverState *bs, int64_t sector_num, 127989f89709SKevin Wolf QEMUIOVector *qiov, int nb_sectors, 128089f89709SKevin Wolf int flags) 128189f89709SKevin Wolf { 1282*48cc565eSKevin Wolf QEDAIOCB acb = { 1283*48cc565eSKevin Wolf .bs = bs, 1284*48cc565eSKevin Wolf .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE, 1285*48cc565eSKevin Wolf .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE, 1286*48cc565eSKevin Wolf .qiov = qiov, 1287*48cc565eSKevin Wolf .flags = flags, 128889f89709SKevin Wolf }; 1289*48cc565eSKevin Wolf qemu_iovec_init(&acb.cur_qiov, qiov->niov); 129089f89709SKevin Wolf 1291*48cc565eSKevin Wolf trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags); 1292eabba580SStefan Hajnoczi 1293eabba580SStefan Hajnoczi /* Start request */ 1294*48cc565eSKevin Wolf return qed_aio_next_io(&acb); 129589f89709SKevin Wolf } 129689f89709SKevin Wolf 129789f89709SKevin Wolf static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs, 129889f89709SKevin Wolf int64_t sector_num, int nb_sectors, 129989f89709SKevin Wolf QEMUIOVector *qiov) 130075411d23SStefan Hajnoczi { 130189f89709SKevin Wolf return qed_co_request(bs, sector_num, qiov, nb_sectors, 0); 130275411d23SStefan Hajnoczi } 130375411d23SStefan Hajnoczi 130489f89709SKevin Wolf static int coroutine_fn bdrv_qed_co_writev(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, QED_AIOCB_WRITE); 13090e71be19SStefan Hajnoczi } 13100e71be19SStefan Hajnoczi 131149a2e483SEric Blake static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, 131249a2e483SEric Blake int64_t offset, 131349a2e483SEric Blake int count, 1314aa7bfbffSPeter Lieven BdrvRequestFlags flags) 13150e71be19SStefan Hajnoczi { 1316ef72f76eSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 13170e71be19SStefan Hajnoczi QEMUIOVector qiov; 13180e71be19SStefan Hajnoczi struct iovec iov; 13190e71be19SStefan Hajnoczi 132049a2e483SEric Blake /* Fall back if the request is not aligned */ 132149a2e483SEric Blake if (qed_offset_into_cluster(s, offset) || 132249a2e483SEric Blake qed_offset_into_cluster(s, count)) { 1323ef72f76eSStefan Hajnoczi return -ENOTSUP; 1324ef72f76eSStefan Hajnoczi } 1325ef72f76eSStefan Hajnoczi 13260e71be19SStefan Hajnoczi /* Zero writes start without an I/O buffer. If a buffer becomes necessary 13270e71be19SStefan Hajnoczi * then it will be allocated during request processing. 13280e71be19SStefan Hajnoczi */ 132949a2e483SEric Blake iov.iov_base = NULL; 133049a2e483SEric Blake iov.iov_len = count; 13310e71be19SStefan Hajnoczi 13320e71be19SStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 133389f89709SKevin Wolf return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov, 133449a2e483SEric Blake count >> BDRV_SECTOR_BITS, 13350e71be19SStefan Hajnoczi QED_AIOCB_WRITE | QED_AIOCB_ZERO); 13360e71be19SStefan Hajnoczi } 13370e71be19SStefan Hajnoczi 13384bff28b8SMax Reitz static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset, Error **errp) 133975411d23SStefan Hajnoczi { 134077a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 134177a5a000SStefan Hajnoczi uint64_t old_image_size; 134277a5a000SStefan Hajnoczi int ret; 134377a5a000SStefan Hajnoczi 134477a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size, 134577a5a000SStefan Hajnoczi s->header.table_size)) { 1346f59adb32SMax Reitz error_setg(errp, "Invalid image size specified"); 134777a5a000SStefan Hajnoczi return -EINVAL; 134877a5a000SStefan Hajnoczi } 134977a5a000SStefan Hajnoczi 135077a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) { 1351f59adb32SMax Reitz error_setg(errp, "Shrinking images is currently not supported"); 135275411d23SStefan Hajnoczi return -ENOTSUP; 135375411d23SStefan Hajnoczi } 135475411d23SStefan Hajnoczi 135577a5a000SStefan Hajnoczi old_image_size = s->header.image_size; 135677a5a000SStefan Hajnoczi s->header.image_size = offset; 135777a5a000SStefan Hajnoczi ret = qed_write_header_sync(s); 135877a5a000SStefan Hajnoczi if (ret < 0) { 135977a5a000SStefan Hajnoczi s->header.image_size = old_image_size; 1360f59adb32SMax Reitz error_setg_errno(errp, -ret, "Failed to update the image size"); 136177a5a000SStefan Hajnoczi } 136277a5a000SStefan Hajnoczi return ret; 136377a5a000SStefan Hajnoczi } 136477a5a000SStefan Hajnoczi 136575411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs) 136675411d23SStefan Hajnoczi { 136775411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 136875411d23SStefan Hajnoczi return s->header.image_size; 136975411d23SStefan Hajnoczi } 137075411d23SStefan Hajnoczi 137175411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 137275411d23SStefan Hajnoczi { 137375411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 137475411d23SStefan Hajnoczi 137575411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi)); 137675411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size; 1377d68dbee8SDong Xu Wang bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; 137895de6d70SPaolo Bonzini bdi->unallocated_blocks_are_zero = true; 137995de6d70SPaolo Bonzini bdi->can_write_zeroes_with_unmap = true; 138075411d23SStefan Hajnoczi return 0; 138175411d23SStefan Hajnoczi } 138275411d23SStefan Hajnoczi 138375411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 138475411d23SStefan Hajnoczi const char *backing_file, 138575411d23SStefan Hajnoczi const char *backing_fmt) 138675411d23SStefan Hajnoczi { 138775411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 138875411d23SStefan Hajnoczi QEDHeader new_header, le_header; 138975411d23SStefan Hajnoczi void *buffer; 139075411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 139175411d23SStefan Hajnoczi int ret; 139275411d23SStefan Hajnoczi 139375411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 139475411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 139575411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 139675411d23SStefan Hajnoczi * add a new string. 139775411d23SStefan Hajnoczi */ 139875411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 139975411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 140075411d23SStefan Hajnoczi return -ENOTSUP; 140175411d23SStefan Hajnoczi } 140275411d23SStefan Hajnoczi 140375411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 140475411d23SStefan Hajnoczi 140575411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 140675411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 140775411d23SStefan Hajnoczi 140875411d23SStefan Hajnoczi /* Adjust feature flags */ 140975411d23SStefan Hajnoczi if (backing_file) { 141075411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 141175411d23SStefan Hajnoczi 141275411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 141375411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 141475411d23SStefan Hajnoczi } 141575411d23SStefan Hajnoczi } 141675411d23SStefan Hajnoczi 141775411d23SStefan Hajnoczi /* Calculate new header size */ 141875411d23SStefan Hajnoczi backing_file_len = 0; 141975411d23SStefan Hajnoczi 142075411d23SStefan Hajnoczi if (backing_file) { 142175411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 142275411d23SStefan Hajnoczi } 142375411d23SStefan Hajnoczi 142475411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 142575411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 142675411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 142775411d23SStefan Hajnoczi buffer_len += backing_file_len; 142875411d23SStefan Hajnoczi 142975411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 143075411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 143175411d23SStefan Hajnoczi return -ENOSPC; 143275411d23SStefan Hajnoczi } 143375411d23SStefan Hajnoczi 143475411d23SStefan Hajnoczi /* Prepare new header */ 14357267c094SAnthony Liguori buffer = g_malloc(buffer_len); 143675411d23SStefan Hajnoczi 143775411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 143875411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 143975411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 144075411d23SStefan Hajnoczi 1441feba23b1SPavel Borzenkov if (backing_file) { 144275411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 144375411d23SStefan Hajnoczi buffer_len += backing_file_len; 1444feba23b1SPavel Borzenkov } 144575411d23SStefan Hajnoczi 144675411d23SStefan Hajnoczi /* Write new header */ 1447d9ca2ea2SKevin Wolf ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); 14487267c094SAnthony Liguori g_free(buffer); 144975411d23SStefan Hajnoczi if (ret == 0) { 145075411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 145175411d23SStefan Hajnoczi } 145275411d23SStefan Hajnoczi return ret; 145375411d23SStefan Hajnoczi } 145475411d23SStefan Hajnoczi 14555a8a30dbSKevin Wolf static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp) 1456c82954e5SBenoît Canet { 1457c82954e5SBenoît Canet BDRVQEDState *s = bs->opaque; 14585a8a30dbSKevin Wolf Error *local_err = NULL; 14595a8a30dbSKevin Wolf int ret; 1460c82954e5SBenoît Canet 1461c82954e5SBenoît Canet bdrv_qed_close(bs); 14623456a8d1SKevin Wolf 1463c82954e5SBenoît Canet memset(s, 0, sizeof(BDRVQEDState)); 14644e4bf5c4SKevin Wolf ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, &local_err); 14655a8a30dbSKevin Wolf if (local_err) { 1466e43bfd9cSMarkus Armbruster error_propagate(errp, local_err); 1467e43bfd9cSMarkus Armbruster error_prepend(errp, "Could not reopen qed layer: "); 14685a8a30dbSKevin Wolf return; 14695a8a30dbSKevin Wolf } else if (ret < 0) { 14705a8a30dbSKevin Wolf error_setg_errno(errp, -ret, "Could not reopen qed layer"); 14715a8a30dbSKevin Wolf return; 14725a8a30dbSKevin Wolf } 1473c82954e5SBenoît Canet } 1474c82954e5SBenoît Canet 14754534ff54SKevin Wolf static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result, 14764534ff54SKevin Wolf BdrvCheckMode fix) 147775411d23SStefan Hajnoczi { 147801979a98SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 147901979a98SStefan Hajnoczi 14804534ff54SKevin Wolf return qed_check(s, result, !!fix); 148175411d23SStefan Hajnoczi } 148275411d23SStefan Hajnoczi 14837ab74849SChunyan Liu static QemuOptsList qed_create_opts = { 14847ab74849SChunyan Liu .name = "qed-create-opts", 14857ab74849SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head), 14867ab74849SChunyan Liu .desc = { 148775411d23SStefan Hajnoczi { 148875411d23SStefan Hajnoczi .name = BLOCK_OPT_SIZE, 14897ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 14907ab74849SChunyan Liu .help = "Virtual disk size" 14917ab74849SChunyan Liu }, 14927ab74849SChunyan Liu { 149375411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FILE, 14947ab74849SChunyan Liu .type = QEMU_OPT_STRING, 149575411d23SStefan Hajnoczi .help = "File name of a base image" 14967ab74849SChunyan Liu }, 14977ab74849SChunyan Liu { 149875411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FMT, 14997ab74849SChunyan Liu .type = QEMU_OPT_STRING, 150075411d23SStefan Hajnoczi .help = "Image format of the base image" 15017ab74849SChunyan Liu }, 15027ab74849SChunyan Liu { 150375411d23SStefan Hajnoczi .name = BLOCK_OPT_CLUSTER_SIZE, 15047ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 150599cce9faSKevin Wolf .help = "Cluster size (in bytes)", 15067ab74849SChunyan Liu .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE) 15077ab74849SChunyan Liu }, 15087ab74849SChunyan Liu { 150975411d23SStefan Hajnoczi .name = BLOCK_OPT_TABLE_SIZE, 15107ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 151175411d23SStefan Hajnoczi .help = "L1/L2 table size (in clusters)" 151275411d23SStefan Hajnoczi }, 151375411d23SStefan Hajnoczi { /* end of list */ } 15147ab74849SChunyan Liu } 151575411d23SStefan Hajnoczi }; 151675411d23SStefan Hajnoczi 151775411d23SStefan Hajnoczi static BlockDriver bdrv_qed = { 151875411d23SStefan Hajnoczi .format_name = "qed", 151975411d23SStefan Hajnoczi .instance_size = sizeof(BDRVQEDState), 15207ab74849SChunyan Liu .create_opts = &qed_create_opts, 15218ee79e70SKevin Wolf .supports_backing = true, 152275411d23SStefan Hajnoczi 152375411d23SStefan Hajnoczi .bdrv_probe = bdrv_qed_probe, 152475411d23SStefan Hajnoczi .bdrv_open = bdrv_qed_open, 152575411d23SStefan Hajnoczi .bdrv_close = bdrv_qed_close, 1526f9cb20f1SJeff Cody .bdrv_reopen_prepare = bdrv_qed_reopen_prepare, 1527862f215fSKevin Wolf .bdrv_child_perm = bdrv_format_default_perms, 1528c282e1fdSChunyan Liu .bdrv_create = bdrv_qed_create, 15293ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 1530b6b8a333SPaolo Bonzini .bdrv_co_get_block_status = bdrv_qed_co_get_block_status, 153189f89709SKevin Wolf .bdrv_co_readv = bdrv_qed_co_readv, 153289f89709SKevin Wolf .bdrv_co_writev = bdrv_qed_co_writev, 153349a2e483SEric Blake .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes, 153475411d23SStefan Hajnoczi .bdrv_truncate = bdrv_qed_truncate, 153575411d23SStefan Hajnoczi .bdrv_getlength = bdrv_qed_getlength, 153675411d23SStefan Hajnoczi .bdrv_get_info = bdrv_qed_get_info, 1537d34682cdSKevin Wolf .bdrv_refresh_limits = bdrv_qed_refresh_limits, 153875411d23SStefan Hajnoczi .bdrv_change_backing_file = bdrv_qed_change_backing_file, 1539c82954e5SBenoît Canet .bdrv_invalidate_cache = bdrv_qed_invalidate_cache, 154075411d23SStefan Hajnoczi .bdrv_check = bdrv_qed_check, 1541a8c868c3SStefan Hajnoczi .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, 1542a8c868c3SStefan Hajnoczi .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, 15436653a73dSFam Zheng .bdrv_drain = bdrv_qed_drain, 154475411d23SStefan Hajnoczi }; 154575411d23SStefan Hajnoczi 154675411d23SStefan Hajnoczi static void bdrv_qed_init(void) 154775411d23SStefan Hajnoczi { 154875411d23SStefan Hajnoczi bdrv_register(&bdrv_qed); 154975411d23SStefan Hajnoczi } 155075411d23SStefan Hajnoczi 155175411d23SStefan Hajnoczi block_init(bdrv_qed_init); 1552