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. 96*1f01e50bSPaolo Bonzini * 97*1f01e50bSPaolo Bonzini * No new allocating reqs can start while this function runs. 9801979a98SStefan Hajnoczi */ 9987f0d882SKevin Wolf static int coroutine_fn qed_write_header(BDRVQEDState *s) 10001979a98SStefan Hajnoczi { 10101979a98SStefan Hajnoczi /* We must write full sectors for O_DIRECT but cannot necessarily generate 10201979a98SStefan Hajnoczi * the data following the header if an unrecognized compat feature is 10301979a98SStefan Hajnoczi * active. Therefore, first read the sectors containing the header, update 10401979a98SStefan Hajnoczi * them, and write back. 10501979a98SStefan Hajnoczi */ 10601979a98SStefan Hajnoczi 107c41a73ffSLaurent Vivier int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE); 10801979a98SStefan Hajnoczi size_t len = nsectors * BDRV_SECTOR_SIZE; 1097076309aSKevin Wolf uint8_t *buf; 1107076309aSKevin Wolf struct iovec iov; 1117076309aSKevin Wolf QEMUIOVector qiov; 1127076309aSKevin Wolf int ret; 11301979a98SStefan Hajnoczi 114*1f01e50bSPaolo Bonzini assert(s->allocating_acb || s->allocating_write_reqs_plugged); 115*1f01e50bSPaolo Bonzini 1167076309aSKevin Wolf buf = qemu_blockalign(s->bs, len); 1177076309aSKevin Wolf iov = (struct iovec) { 1187076309aSKevin Wolf .iov_base = buf, 1197076309aSKevin Wolf .iov_len = len, 1207076309aSKevin Wolf }; 1217076309aSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 12201979a98SStefan Hajnoczi 1230f714ec7SKevin Wolf ret = bdrv_co_preadv(s->bs->file, 0, qiov.size, &qiov, 0); 1247076309aSKevin Wolf if (ret < 0) { 1257076309aSKevin Wolf goto out; 1267076309aSKevin Wolf } 1277076309aSKevin Wolf 1287076309aSKevin Wolf /* Update header */ 1297076309aSKevin Wolf qed_header_cpu_to_le(&s->header, (QEDHeader *) buf); 1307076309aSKevin Wolf 1310f714ec7SKevin Wolf ret = bdrv_co_pwritev(s->bs->file, 0, qiov.size, &qiov, 0); 1327076309aSKevin Wolf if (ret < 0) { 1337076309aSKevin Wolf goto out; 1347076309aSKevin Wolf } 1357076309aSKevin Wolf 1367076309aSKevin Wolf ret = 0; 1377076309aSKevin Wolf out: 1387076309aSKevin Wolf qemu_vfree(buf); 139f13d712bSKevin Wolf return ret; 14001979a98SStefan Hajnoczi } 14101979a98SStefan Hajnoczi 14275411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 14375411d23SStefan Hajnoczi { 14475411d23SStefan Hajnoczi uint64_t table_entries; 14575411d23SStefan Hajnoczi uint64_t l2_size; 14675411d23SStefan Hajnoczi 14775411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t); 14875411d23SStefan Hajnoczi l2_size = table_entries * cluster_size; 14975411d23SStefan Hajnoczi 15075411d23SStefan Hajnoczi return l2_size * table_entries; 15175411d23SStefan Hajnoczi } 15275411d23SStefan Hajnoczi 15375411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size) 15475411d23SStefan Hajnoczi { 15575411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE || 15675411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) { 15775411d23SStefan Hajnoczi return false; 15875411d23SStefan Hajnoczi } 15975411d23SStefan Hajnoczi if (cluster_size & (cluster_size - 1)) { 16075411d23SStefan Hajnoczi return false; /* not power of 2 */ 16175411d23SStefan Hajnoczi } 16275411d23SStefan Hajnoczi return true; 16375411d23SStefan Hajnoczi } 16475411d23SStefan Hajnoczi 16575411d23SStefan Hajnoczi static bool qed_is_table_size_valid(uint32_t table_size) 16675411d23SStefan Hajnoczi { 16775411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE || 16875411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) { 16975411d23SStefan Hajnoczi return false; 17075411d23SStefan Hajnoczi } 17175411d23SStefan Hajnoczi if (table_size & (table_size - 1)) { 17275411d23SStefan Hajnoczi return false; /* not power of 2 */ 17375411d23SStefan Hajnoczi } 17475411d23SStefan Hajnoczi return true; 17575411d23SStefan Hajnoczi } 17675411d23SStefan Hajnoczi 17775411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 17875411d23SStefan Hajnoczi uint32_t table_size) 17975411d23SStefan Hajnoczi { 18075411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) { 18175411d23SStefan Hajnoczi return false; /* not multiple of sector size */ 18275411d23SStefan Hajnoczi } 18375411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) { 18475411d23SStefan Hajnoczi return false; /* image is too large */ 18575411d23SStefan Hajnoczi } 18675411d23SStefan Hajnoczi return true; 18775411d23SStefan Hajnoczi } 18875411d23SStefan Hajnoczi 18975411d23SStefan Hajnoczi /** 19075411d23SStefan Hajnoczi * Read a string of known length from the image file 19175411d23SStefan Hajnoczi * 19275411d23SStefan Hajnoczi * @file: Image file 19375411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes 19475411d23SStefan Hajnoczi * @n: String length in bytes 19575411d23SStefan Hajnoczi * @buf: Destination buffer 19675411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes 19775411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure 19875411d23SStefan Hajnoczi * 19975411d23SStefan Hajnoczi * The string is NUL-terminated. 20075411d23SStefan Hajnoczi */ 201cf2ab8fcSKevin Wolf static int qed_read_string(BdrvChild *file, uint64_t offset, size_t n, 20275411d23SStefan Hajnoczi char *buf, size_t buflen) 20375411d23SStefan Hajnoczi { 20475411d23SStefan Hajnoczi int ret; 20575411d23SStefan Hajnoczi if (n >= buflen) { 20675411d23SStefan Hajnoczi return -EINVAL; 20775411d23SStefan Hajnoczi } 20875411d23SStefan Hajnoczi ret = bdrv_pread(file, offset, buf, n); 20975411d23SStefan Hajnoczi if (ret < 0) { 21075411d23SStefan Hajnoczi return ret; 21175411d23SStefan Hajnoczi } 21275411d23SStefan Hajnoczi buf[n] = '\0'; 21375411d23SStefan Hajnoczi return 0; 21475411d23SStefan Hajnoczi } 21575411d23SStefan Hajnoczi 216eabba580SStefan Hajnoczi /** 217eabba580SStefan Hajnoczi * Allocate new clusters 218eabba580SStefan Hajnoczi * 219eabba580SStefan Hajnoczi * @s: QED state 220eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate 221eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster 222eabba580SStefan Hajnoczi * 223eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be 224eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image 225eabba580SStefan Hajnoczi * file. 226*1f01e50bSPaolo Bonzini * 227*1f01e50bSPaolo Bonzini * Called with table_lock held. 228eabba580SStefan Hajnoczi */ 229eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 230eabba580SStefan Hajnoczi { 231eabba580SStefan Hajnoczi uint64_t offset = s->file_size; 232eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size; 233eabba580SStefan Hajnoczi return offset; 234eabba580SStefan Hajnoczi } 235eabba580SStefan Hajnoczi 236298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s) 237298800caSStefan Hajnoczi { 238298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */ 239298800caSStefan Hajnoczi return qemu_blockalign(s->bs, 240298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size); 241298800caSStefan Hajnoczi } 242298800caSStefan Hajnoczi 243eabba580SStefan Hajnoczi /** 244eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table 245*1f01e50bSPaolo Bonzini * 246*1f01e50bSPaolo Bonzini * Called with table_lock held. 247eabba580SStefan Hajnoczi */ 248eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 249eabba580SStefan Hajnoczi { 250eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 251eabba580SStefan Hajnoczi 252eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s); 253eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 254eabba580SStefan Hajnoczi 255eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0, 256eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size); 257eabba580SStefan Hajnoczi return l2_table; 258eabba580SStefan Hajnoczi } 259eabba580SStefan Hajnoczi 260*1f01e50bSPaolo Bonzini static bool qed_plug_allocating_write_reqs(BDRVQEDState *s) 2616f321e93SStefan Hajnoczi { 262*1f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 263*1f01e50bSPaolo Bonzini 264*1f01e50bSPaolo Bonzini /* No reentrancy is allowed. */ 2656f321e93SStefan Hajnoczi assert(!s->allocating_write_reqs_plugged); 266*1f01e50bSPaolo Bonzini if (s->allocating_acb != NULL) { 267*1f01e50bSPaolo Bonzini /* Another allocating write came concurrently. This cannot happen 268*1f01e50bSPaolo Bonzini * from bdrv_qed_co_drain, but it can happen when the timer runs. 269*1f01e50bSPaolo Bonzini */ 270*1f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 271*1f01e50bSPaolo Bonzini return false; 272*1f01e50bSPaolo Bonzini } 2736f321e93SStefan Hajnoczi 2746f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = true; 275*1f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 276*1f01e50bSPaolo Bonzini return true; 2776f321e93SStefan Hajnoczi } 2786f321e93SStefan Hajnoczi 2796f321e93SStefan Hajnoczi static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) 2806f321e93SStefan Hajnoczi { 281*1f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 2826f321e93SStefan Hajnoczi assert(s->allocating_write_reqs_plugged); 2836f321e93SStefan Hajnoczi s->allocating_write_reqs_plugged = false; 284*1f01e50bSPaolo Bonzini qemu_co_queue_next(&s->allocating_write_reqs); 285*1f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 2866f321e93SStefan Hajnoczi } 2876f321e93SStefan Hajnoczi 28887f0d882SKevin Wolf static void coroutine_fn qed_need_check_timer_entry(void *opaque) 2896f321e93SStefan Hajnoczi { 2906f321e93SStefan Hajnoczi BDRVQEDState *s = opaque; 291c0e8f989SKevin Wolf int ret; 2926f321e93SStefan Hajnoczi 2936f321e93SStefan Hajnoczi trace_qed_need_check_timer_cb(s); 2946f321e93SStefan Hajnoczi 295*1f01e50bSPaolo Bonzini if (!qed_plug_allocating_write_reqs(s)) { 296*1f01e50bSPaolo Bonzini return; 297*1f01e50bSPaolo Bonzini } 2986f321e93SStefan Hajnoczi 2996f321e93SStefan Hajnoczi /* Ensure writes are on disk before clearing flag */ 300c0e8f989SKevin Wolf ret = bdrv_co_flush(s->bs->file->bs); 301c0e8f989SKevin Wolf if (ret < 0) { 302c0e8f989SKevin Wolf qed_unplug_allocating_write_reqs(s); 303c0e8f989SKevin Wolf return; 304c0e8f989SKevin Wolf } 305c0e8f989SKevin Wolf 306c0e8f989SKevin Wolf s->header.features &= ~QED_F_NEED_CHECK; 307c0e8f989SKevin Wolf ret = qed_write_header(s); 308c0e8f989SKevin Wolf (void) ret; 309c0e8f989SKevin Wolf 310c0e8f989SKevin Wolf qed_unplug_allocating_write_reqs(s); 311c0e8f989SKevin Wolf 312c0e8f989SKevin Wolf ret = bdrv_co_flush(s->bs); 313c0e8f989SKevin Wolf (void) ret; 314c0e8f989SKevin Wolf } 315c0e8f989SKevin Wolf 316c0e8f989SKevin Wolf static void qed_need_check_timer_cb(void *opaque) 317c0e8f989SKevin Wolf { 318c0e8f989SKevin Wolf Coroutine *co = qemu_coroutine_create(qed_need_check_timer_entry, opaque); 319c0e8f989SKevin Wolf qemu_coroutine_enter(co); 3202f47da5fSPaolo Bonzini } 3212f47da5fSPaolo Bonzini 3226f321e93SStefan Hajnoczi static void qed_start_need_check_timer(BDRVQEDState *s) 3236f321e93SStefan Hajnoczi { 3246f321e93SStefan Hajnoczi trace_qed_start_need_check_timer(s); 3256f321e93SStefan Hajnoczi 326bc72ad67SAlex Bligh /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for 3276f321e93SStefan Hajnoczi * migration. 3286f321e93SStefan Hajnoczi */ 329bc72ad67SAlex Bligh timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 33073bcb24dSRutuja Shah NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT); 3316f321e93SStefan Hajnoczi } 3326f321e93SStefan Hajnoczi 3336f321e93SStefan Hajnoczi /* It's okay to call this multiple times or when no timer is started */ 3346f321e93SStefan Hajnoczi static void qed_cancel_need_check_timer(BDRVQEDState *s) 3356f321e93SStefan Hajnoczi { 3366f321e93SStefan Hajnoczi trace_qed_cancel_need_check_timer(s); 337bc72ad67SAlex Bligh timer_del(s->need_check_timer); 3386f321e93SStefan Hajnoczi } 3396f321e93SStefan Hajnoczi 340a8c868c3SStefan Hajnoczi static void bdrv_qed_detach_aio_context(BlockDriverState *bs) 341a8c868c3SStefan Hajnoczi { 342a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 343a8c868c3SStefan Hajnoczi 344a8c868c3SStefan Hajnoczi qed_cancel_need_check_timer(s); 345a8c868c3SStefan Hajnoczi timer_free(s->need_check_timer); 346a8c868c3SStefan Hajnoczi } 347a8c868c3SStefan Hajnoczi 348a8c868c3SStefan Hajnoczi static void bdrv_qed_attach_aio_context(BlockDriverState *bs, 349a8c868c3SStefan Hajnoczi AioContext *new_context) 350a8c868c3SStefan Hajnoczi { 351a8c868c3SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 352a8c868c3SStefan Hajnoczi 353a8c868c3SStefan Hajnoczi s->need_check_timer = aio_timer_new(new_context, 354a8c868c3SStefan Hajnoczi QEMU_CLOCK_VIRTUAL, SCALE_NS, 355a8c868c3SStefan Hajnoczi qed_need_check_timer_cb, s); 356a8c868c3SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 357a8c868c3SStefan Hajnoczi qed_start_need_check_timer(s); 358a8c868c3SStefan Hajnoczi } 359a8c868c3SStefan Hajnoczi } 360a8c868c3SStefan Hajnoczi 36161124f03SPaolo Bonzini static void coroutine_fn bdrv_qed_co_drain(BlockDriverState *bs) 3626653a73dSFam Zheng { 3636653a73dSFam Zheng BDRVQEDState *s = bs->opaque; 3646653a73dSFam Zheng 3656653a73dSFam Zheng /* Fire the timer immediately in order to start doing I/O as soon as the 3666653a73dSFam Zheng * header is flushed. 3676653a73dSFam Zheng */ 3686653a73dSFam Zheng if (s->need_check_timer && timer_pending(s->need_check_timer)) { 3696653a73dSFam Zheng qed_cancel_need_check_timer(s); 37061124f03SPaolo Bonzini qed_need_check_timer_entry(s); 3716653a73dSFam Zheng } 3726653a73dSFam Zheng } 3736653a73dSFam Zheng 37461c7887eSPaolo Bonzini static void bdrv_qed_init_state(BlockDriverState *bs) 37561c7887eSPaolo Bonzini { 37661c7887eSPaolo Bonzini BDRVQEDState *s = bs->opaque; 37761c7887eSPaolo Bonzini 37861c7887eSPaolo Bonzini memset(s, 0, sizeof(BDRVQEDState)); 37961c7887eSPaolo Bonzini s->bs = bs; 380*1f01e50bSPaolo Bonzini qemu_co_mutex_init(&s->table_lock); 38161c7887eSPaolo Bonzini qemu_co_queue_init(&s->allocating_write_reqs); 38261c7887eSPaolo Bonzini } 38361c7887eSPaolo Bonzini 3844e4bf5c4SKevin Wolf static int bdrv_qed_do_open(BlockDriverState *bs, QDict *options, int flags, 385015a1036SMax Reitz Error **errp) 38675411d23SStefan Hajnoczi { 38775411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 38875411d23SStefan Hajnoczi QEDHeader le_header; 38975411d23SStefan Hajnoczi int64_t file_size; 39075411d23SStefan Hajnoczi int ret; 39175411d23SStefan Hajnoczi 392cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); 39375411d23SStefan Hajnoczi if (ret < 0) { 39475411d23SStefan Hajnoczi return ret; 39575411d23SStefan Hajnoczi } 39675411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header); 39775411d23SStefan Hajnoczi 39875411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) { 39976abe407SPaolo Bonzini error_setg(errp, "Image not in QED format"); 40076abe407SPaolo Bonzini return -EINVAL; 40175411d23SStefan Hajnoczi } 40275411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) { 40310b758e8SKevin Wolf /* image uses unsupported feature bits */ 404a55448b3SMax Reitz error_setg(errp, "Unsupported QED features: %" PRIx64, 40510b758e8SKevin Wolf s->header.features & ~QED_FEATURE_MASK); 40610b758e8SKevin Wolf return -ENOTSUP; 40775411d23SStefan Hajnoczi } 40875411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 40975411d23SStefan Hajnoczi return -EINVAL; 41075411d23SStefan Hajnoczi } 41175411d23SStefan Hajnoczi 41275411d23SStefan Hajnoczi /* Round down file size to the last cluster */ 4139a4f4c31SKevin Wolf file_size = bdrv_getlength(bs->file->bs); 41475411d23SStefan Hajnoczi if (file_size < 0) { 41575411d23SStefan Hajnoczi return file_size; 41675411d23SStefan Hajnoczi } 41775411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size); 41875411d23SStefan Hajnoczi 41975411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) { 42075411d23SStefan Hajnoczi return -EINVAL; 42175411d23SStefan Hajnoczi } 42275411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size, 42375411d23SStefan Hajnoczi s->header.cluster_size, 42475411d23SStefan Hajnoczi s->header.table_size)) { 42575411d23SStefan Hajnoczi return -EINVAL; 42675411d23SStefan Hajnoczi } 42775411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 42875411d23SStefan Hajnoczi return -EINVAL; 42975411d23SStefan Hajnoczi } 43075411d23SStefan Hajnoczi 43175411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) / 43275411d23SStefan Hajnoczi sizeof(uint64_t); 433786a4ea8SStefan Hajnoczi s->l2_shift = ctz32(s->header.cluster_size); 43475411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1; 435786a4ea8SStefan Hajnoczi s->l1_shift = s->l2_shift + ctz32(s->table_nelems); 43675411d23SStefan Hajnoczi 4370adfa1edSStefan Hajnoczi /* Header size calculation must not overflow uint32_t */ 4380adfa1edSStefan Hajnoczi if (s->header.header_size > UINT32_MAX / s->header.cluster_size) { 4390adfa1edSStefan Hajnoczi return -EINVAL; 4400adfa1edSStefan Hajnoczi } 4410adfa1edSStefan Hajnoczi 44275411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) { 44375411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset + 44475411d23SStefan Hajnoczi s->header.backing_filename_size > 44575411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) { 44675411d23SStefan Hajnoczi return -EINVAL; 44775411d23SStefan Hajnoczi } 44875411d23SStefan Hajnoczi 449cf2ab8fcSKevin Wolf ret = qed_read_string(bs->file, s->header.backing_filename_offset, 45075411d23SStefan Hajnoczi s->header.backing_filename_size, bs->backing_file, 45175411d23SStefan Hajnoczi sizeof(bs->backing_file)); 45275411d23SStefan Hajnoczi if (ret < 0) { 45375411d23SStefan Hajnoczi return ret; 45475411d23SStefan Hajnoczi } 45575411d23SStefan Hajnoczi 45675411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 45775411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 45875411d23SStefan Hajnoczi } 45975411d23SStefan Hajnoczi } 46075411d23SStefan Hajnoczi 46175411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards 46275411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older 46375411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is 46475411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear 46575411d23SStefan Hajnoczi * feature is no longer valid. 46675411d23SStefan Hajnoczi */ 46775411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 46804c01a5cSKevin Wolf !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) { 46975411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 47075411d23SStefan Hajnoczi 47175411d23SStefan Hajnoczi ret = qed_write_header_sync(s); 47275411d23SStefan Hajnoczi if (ret) { 47375411d23SStefan Hajnoczi return ret; 47475411d23SStefan Hajnoczi } 47575411d23SStefan Hajnoczi 47675411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */ 4779a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 47875411d23SStefan Hajnoczi } 47975411d23SStefan Hajnoczi 480298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s); 481298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache); 482298800caSStefan Hajnoczi 483298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s); 484298800caSStefan Hajnoczi if (ret) { 48501979a98SStefan Hajnoczi goto out; 48601979a98SStefan Hajnoczi } 48701979a98SStefan Hajnoczi 48801979a98SStefan Hajnoczi /* If image was not closed cleanly, check consistency */ 489058f8f16SStefan Hajnoczi if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) { 49001979a98SStefan Hajnoczi /* Read-only images cannot be fixed. There is no risk of corruption 49101979a98SStefan Hajnoczi * since write operations are not possible. Therefore, allow 49201979a98SStefan Hajnoczi * potentially inconsistent images to be opened read-only. This can 49301979a98SStefan Hajnoczi * aid data recovery from an otherwise inconsistent image. 49401979a98SStefan Hajnoczi */ 4959a4f4c31SKevin Wolf if (!bdrv_is_read_only(bs->file->bs) && 49604c01a5cSKevin Wolf !(flags & BDRV_O_INACTIVE)) { 49701979a98SStefan Hajnoczi BdrvCheckResult result = {0}; 49801979a98SStefan Hajnoczi 49901979a98SStefan Hajnoczi ret = qed_check(s, &result, true); 5006f321e93SStefan Hajnoczi if (ret) { 5016f321e93SStefan Hajnoczi goto out; 5026f321e93SStefan Hajnoczi } 50301979a98SStefan Hajnoczi } 50401979a98SStefan Hajnoczi } 50501979a98SStefan Hajnoczi 506a8c868c3SStefan Hajnoczi bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs)); 5076f321e93SStefan Hajnoczi 50801979a98SStefan Hajnoczi out: 50901979a98SStefan Hajnoczi if (ret) { 510298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 511298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 512298800caSStefan Hajnoczi } 51375411d23SStefan Hajnoczi return ret; 51475411d23SStefan Hajnoczi } 51575411d23SStefan Hajnoczi 5164e4bf5c4SKevin Wolf static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags, 5174e4bf5c4SKevin Wolf Error **errp) 5184e4bf5c4SKevin Wolf { 5194e4bf5c4SKevin Wolf bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, 5204e4bf5c4SKevin Wolf false, errp); 5214e4bf5c4SKevin Wolf if (!bs->file) { 5224e4bf5c4SKevin Wolf return -EINVAL; 5234e4bf5c4SKevin Wolf } 5244e4bf5c4SKevin Wolf 52561c7887eSPaolo Bonzini bdrv_qed_init_state(bs); 5264e4bf5c4SKevin Wolf return bdrv_qed_do_open(bs, options, flags, errp); 5274e4bf5c4SKevin Wolf } 5284e4bf5c4SKevin Wolf 5293baca891SKevin Wolf static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp) 530d34682cdSKevin Wolf { 531d34682cdSKevin Wolf BDRVQEDState *s = bs->opaque; 532d34682cdSKevin Wolf 533cf081fcaSEric Blake bs->bl.pwrite_zeroes_alignment = s->header.cluster_size; 534d34682cdSKevin Wolf } 535d34682cdSKevin Wolf 536f9cb20f1SJeff Cody /* We have nothing to do for QED reopen, stubs just return 537f9cb20f1SJeff Cody * success */ 538f9cb20f1SJeff Cody static int bdrv_qed_reopen_prepare(BDRVReopenState *state, 539f9cb20f1SJeff Cody BlockReopenQueue *queue, Error **errp) 540f9cb20f1SJeff Cody { 541f9cb20f1SJeff Cody return 0; 542f9cb20f1SJeff Cody } 543f9cb20f1SJeff Cody 54475411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 54575411d23SStefan Hajnoczi { 546298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 547298800caSStefan Hajnoczi 548a8c868c3SStefan Hajnoczi bdrv_qed_detach_aio_context(bs); 5496f321e93SStefan Hajnoczi 55001979a98SStefan Hajnoczi /* Ensure writes reach stable storage */ 5519a4f4c31SKevin Wolf bdrv_flush(bs->file->bs); 55201979a98SStefan Hajnoczi 55301979a98SStefan Hajnoczi /* Clean shutdown, no check required on next open */ 55401979a98SStefan Hajnoczi if (s->header.features & QED_F_NEED_CHECK) { 55501979a98SStefan Hajnoczi s->header.features &= ~QED_F_NEED_CHECK; 55601979a98SStefan Hajnoczi qed_write_header_sync(s); 55701979a98SStefan Hajnoczi } 55801979a98SStefan Hajnoczi 559298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 560298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 56175411d23SStefan Hajnoczi } 56275411d23SStefan Hajnoczi 56375411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size, 56475411d23SStefan Hajnoczi uint64_t image_size, uint32_t table_size, 5650fea6b79SPaolo Bonzini const char *backing_file, const char *backing_fmt, 5664ab15590SChunyan Liu QemuOpts *opts, Error **errp) 56775411d23SStefan Hajnoczi { 56875411d23SStefan Hajnoczi QEDHeader header = { 56975411d23SStefan Hajnoczi .magic = QED_MAGIC, 57075411d23SStefan Hajnoczi .cluster_size = cluster_size, 57175411d23SStefan Hajnoczi .table_size = table_size, 57275411d23SStefan Hajnoczi .header_size = 1, 57375411d23SStefan Hajnoczi .features = 0, 57475411d23SStefan Hajnoczi .compat_features = 0, 57575411d23SStefan Hajnoczi .l1_table_offset = cluster_size, 57675411d23SStefan Hajnoczi .image_size = image_size, 57775411d23SStefan Hajnoczi }; 57875411d23SStefan Hajnoczi QEDHeader le_header; 57975411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 58075411d23SStefan Hajnoczi size_t l1_size = header.cluster_size * header.table_size; 58134b5d2c6SMax Reitz Error *local_err = NULL; 58275411d23SStefan Hajnoczi int ret = 0; 5838a56fdadSKevin Wolf BlockBackend *blk; 58475411d23SStefan Hajnoczi 5854ab15590SChunyan Liu ret = bdrv_create_file(filename, opts, &local_err); 58675411d23SStefan Hajnoczi if (ret < 0) { 5870fea6b79SPaolo Bonzini error_propagate(errp, local_err); 58875411d23SStefan Hajnoczi return ret; 58975411d23SStefan Hajnoczi } 59075411d23SStefan Hajnoczi 591efaa7c4eSMax Reitz blk = blk_new_open(filename, NULL, NULL, 59255880601SKevin Wolf BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, 59355880601SKevin Wolf &local_err); 5948a56fdadSKevin Wolf if (blk == NULL) { 5950fea6b79SPaolo Bonzini error_propagate(errp, local_err); 5968a56fdadSKevin Wolf return -EIO; 59775411d23SStefan Hajnoczi } 59875411d23SStefan Hajnoczi 5998a56fdadSKevin Wolf blk_set_allow_write_beyond_eof(blk, true); 6008a56fdadSKevin Wolf 601c743849bSStefan Hajnoczi /* File must start empty and grow, check truncate is supported */ 6023a691c50SMax Reitz ret = blk_truncate(blk, 0, PREALLOC_MODE_OFF, errp); 603c743849bSStefan Hajnoczi if (ret < 0) { 604c743849bSStefan Hajnoczi goto out; 605c743849bSStefan Hajnoczi } 606c743849bSStefan Hajnoczi 60775411d23SStefan Hajnoczi if (backing_file) { 60875411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 60975411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 61075411d23SStefan Hajnoczi header.backing_filename_size = strlen(backing_file); 61175411d23SStefan Hajnoczi 61275411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 61375411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 61475411d23SStefan Hajnoczi } 61575411d23SStefan Hajnoczi } 61675411d23SStefan Hajnoczi 61775411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 6188341f00dSEric Blake ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0); 61975411d23SStefan Hajnoczi if (ret < 0) { 62075411d23SStefan Hajnoczi goto out; 62175411d23SStefan Hajnoczi } 6228a56fdadSKevin Wolf ret = blk_pwrite(blk, sizeof(le_header), backing_file, 6238341f00dSEric Blake header.backing_filename_size, 0); 62475411d23SStefan Hajnoczi if (ret < 0) { 62575411d23SStefan Hajnoczi goto out; 62675411d23SStefan Hajnoczi } 62775411d23SStefan Hajnoczi 6287267c094SAnthony Liguori l1_table = g_malloc0(l1_size); 6298341f00dSEric Blake ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0); 63075411d23SStefan Hajnoczi if (ret < 0) { 63175411d23SStefan Hajnoczi goto out; 63275411d23SStefan Hajnoczi } 63375411d23SStefan Hajnoczi 63475411d23SStefan Hajnoczi ret = 0; /* success */ 63575411d23SStefan Hajnoczi out: 6367267c094SAnthony Liguori g_free(l1_table); 6378a56fdadSKevin Wolf blk_unref(blk); 63875411d23SStefan Hajnoczi return ret; 63975411d23SStefan Hajnoczi } 64075411d23SStefan Hajnoczi 6417ab74849SChunyan Liu static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp) 64275411d23SStefan Hajnoczi { 64375411d23SStefan Hajnoczi uint64_t image_size = 0; 64475411d23SStefan Hajnoczi uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 64575411d23SStefan Hajnoczi uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 6467ab74849SChunyan Liu char *backing_file = NULL; 6477ab74849SChunyan Liu char *backing_fmt = NULL; 6487ab74849SChunyan Liu int ret; 64975411d23SStefan Hajnoczi 650c2eb918eSHu Tao image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 651c2eb918eSHu Tao BDRV_SECTOR_SIZE); 6527ab74849SChunyan Liu backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); 6537ab74849SChunyan Liu backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); 6547ab74849SChunyan Liu cluster_size = qemu_opt_get_size_del(opts, 6557ab74849SChunyan Liu BLOCK_OPT_CLUSTER_SIZE, 6567ab74849SChunyan Liu QED_DEFAULT_CLUSTER_SIZE); 6577ab74849SChunyan Liu table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE, 6587ab74849SChunyan Liu QED_DEFAULT_TABLE_SIZE); 65975411d23SStefan Hajnoczi 66075411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(cluster_size)) { 6615ff679b4SAakriti Gupta error_setg(errp, "QED cluster size must be within range [%u, %u] " 6625ff679b4SAakriti Gupta "and power of 2", 66375411d23SStefan Hajnoczi QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 6647ab74849SChunyan Liu ret = -EINVAL; 6657ab74849SChunyan Liu goto finish; 66675411d23SStefan Hajnoczi } 66775411d23SStefan Hajnoczi if (!qed_is_table_size_valid(table_size)) { 6685ff679b4SAakriti Gupta error_setg(errp, "QED table size must be within range [%u, %u] " 6695ff679b4SAakriti Gupta "and power of 2", 67075411d23SStefan Hajnoczi QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 6717ab74849SChunyan Liu ret = -EINVAL; 6727ab74849SChunyan Liu goto finish; 67375411d23SStefan Hajnoczi } 67475411d23SStefan Hajnoczi if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 6755ff679b4SAakriti Gupta error_setg(errp, "QED image size must be a non-zero multiple of " 6765ff679b4SAakriti Gupta "cluster size and less than %" PRIu64 " bytes", 67775411d23SStefan Hajnoczi qed_max_image_size(cluster_size, table_size)); 6787ab74849SChunyan Liu ret = -EINVAL; 6797ab74849SChunyan Liu goto finish; 68075411d23SStefan Hajnoczi } 68175411d23SStefan Hajnoczi 6827ab74849SChunyan Liu ret = qed_create(filename, cluster_size, image_size, table_size, 6834ab15590SChunyan Liu backing_file, backing_fmt, opts, errp); 6847ab74849SChunyan Liu 6857ab74849SChunyan Liu finish: 6867ab74849SChunyan Liu g_free(backing_file); 6877ab74849SChunyan Liu g_free(backing_fmt); 6887ab74849SChunyan Liu return ret; 68975411d23SStefan Hajnoczi } 69075411d23SStefan Hajnoczi 691298800caSStefan Hajnoczi typedef struct { 6924bc74be9SPaolo Bonzini BlockDriverState *bs; 693b7d5a5b8SStefan Hajnoczi Coroutine *co; 6944bc74be9SPaolo Bonzini uint64_t pos; 6954bc74be9SPaolo Bonzini int64_t status; 696298800caSStefan Hajnoczi int *pnum; 69753f1dfd1SFam Zheng BlockDriverState **file; 698298800caSStefan Hajnoczi } QEDIsAllocatedCB; 699298800caSStefan Hajnoczi 700*1f01e50bSPaolo Bonzini /* Called with table_lock held. */ 701298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 702298800caSStefan Hajnoczi { 703298800caSStefan Hajnoczi QEDIsAllocatedCB *cb = opaque; 7044bc74be9SPaolo Bonzini BDRVQEDState *s = cb->bs->opaque; 705298800caSStefan Hajnoczi *cb->pnum = len / BDRV_SECTOR_SIZE; 7064bc74be9SPaolo Bonzini switch (ret) { 7074bc74be9SPaolo Bonzini case QED_CLUSTER_FOUND: 7084bc74be9SPaolo Bonzini offset |= qed_offset_into_cluster(s, cb->pos); 7094bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; 71053f1dfd1SFam Zheng *cb->file = cb->bs->file->bs; 7114bc74be9SPaolo Bonzini break; 7124bc74be9SPaolo Bonzini case QED_CLUSTER_ZERO: 7134bc74be9SPaolo Bonzini cb->status = BDRV_BLOCK_ZERO; 7144bc74be9SPaolo Bonzini break; 7154bc74be9SPaolo Bonzini case QED_CLUSTER_L2: 7164bc74be9SPaolo Bonzini case QED_CLUSTER_L1: 7174bc74be9SPaolo Bonzini cb->status = 0; 7184bc74be9SPaolo Bonzini break; 7194bc74be9SPaolo Bonzini default: 7204bc74be9SPaolo Bonzini assert(ret < 0); 7214bc74be9SPaolo Bonzini cb->status = ret; 7224bc74be9SPaolo Bonzini break; 7234bc74be9SPaolo Bonzini } 7244bc74be9SPaolo Bonzini 725b7d5a5b8SStefan Hajnoczi if (cb->co) { 726b9e413ddSPaolo Bonzini aio_co_wake(cb->co); 727b7d5a5b8SStefan Hajnoczi } 728298800caSStefan Hajnoczi } 729298800caSStefan Hajnoczi 730b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs, 731b7d5a5b8SStefan Hajnoczi int64_t sector_num, 73267a0fd2aSFam Zheng int nb_sectors, int *pnum, 73367a0fd2aSFam Zheng BlockDriverState **file) 73475411d23SStefan Hajnoczi { 735298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 736298800caSStefan Hajnoczi size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 737298800caSStefan Hajnoczi QEDIsAllocatedCB cb = { 7384bc74be9SPaolo Bonzini .bs = bs, 7394bc74be9SPaolo Bonzini .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE, 7404bc74be9SPaolo Bonzini .status = BDRV_BLOCK_OFFSET_MASK, 741298800caSStefan Hajnoczi .pnum = pnum, 74253f1dfd1SFam Zheng .file = file, 743298800caSStefan Hajnoczi }; 744298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 7450f21b7a1SKevin Wolf uint64_t offset; 7460f21b7a1SKevin Wolf int ret; 747298800caSStefan Hajnoczi 748*1f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 7490f21b7a1SKevin Wolf ret = qed_find_cluster(s, &request, cb.pos, &len, &offset); 7500f21b7a1SKevin Wolf qed_is_allocated_cb(&cb, ret, offset, len); 751298800caSStefan Hajnoczi 7520f21b7a1SKevin Wolf /* The callback was invoked immediately */ 7530f21b7a1SKevin Wolf assert(cb.status != BDRV_BLOCK_OFFSET_MASK); 754298800caSStefan Hajnoczi 755298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 756*1f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 757298800caSStefan Hajnoczi 7584bc74be9SPaolo Bonzini return cb.status; 75975411d23SStefan Hajnoczi } 76075411d23SStefan Hajnoczi 761eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 762eabba580SStefan Hajnoczi { 76348cc565eSKevin Wolf return acb->bs->opaque; 764eabba580SStefan Hajnoczi } 765eabba580SStefan Hajnoczi 766eabba580SStefan Hajnoczi /** 767eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 768eabba580SStefan Hajnoczi * 769eabba580SStefan Hajnoczi * @s: QED state 770eabba580SStefan Hajnoczi * @pos: Byte position in device 771eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 772f06ee3d4SKevin Wolf * @backing_qiov: Possibly shortened copy of qiov, to be allocated here 773eabba580SStefan Hajnoczi * @cb: Completion function 774eabba580SStefan Hajnoczi * @opaque: User data for completion function 775eabba580SStefan Hajnoczi * 776eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 777eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 778eabba580SStefan Hajnoczi */ 77987f0d882SKevin Wolf static int coroutine_fn qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 780eabba580SStefan Hajnoczi QEMUIOVector *qiov, 781e85c5281SKevin Wolf QEMUIOVector **backing_qiov) 782eabba580SStefan Hajnoczi { 783eabba580SStefan Hajnoczi uint64_t backing_length = 0; 784eabba580SStefan Hajnoczi size_t size; 785e85c5281SKevin Wolf int ret; 786eabba580SStefan Hajnoczi 787eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 788eabba580SStefan Hajnoczi * backing file like a zero length backing file. 789eabba580SStefan Hajnoczi */ 790760e0063SKevin Wolf if (s->bs->backing) { 791760e0063SKevin Wolf int64_t l = bdrv_getlength(s->bs->backing->bs); 792eabba580SStefan Hajnoczi if (l < 0) { 793e85c5281SKevin Wolf return l; 794eabba580SStefan Hajnoczi } 795eabba580SStefan Hajnoczi backing_length = l; 796eabba580SStefan Hajnoczi } 797eabba580SStefan Hajnoczi 798eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 799eabba580SStefan Hajnoczi if (pos >= backing_length || 800eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 8013d9b4925SMichael Tokarev qemu_iovec_memset(qiov, 0, 0, qiov->size); 802eabba580SStefan Hajnoczi } 803eabba580SStefan Hajnoczi 804eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 805eabba580SStefan Hajnoczi if (pos >= backing_length) { 806e85c5281SKevin Wolf return 0; 807eabba580SStefan Hajnoczi } 808eabba580SStefan Hajnoczi 809eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 810eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 811eabba580SStefan Hajnoczi 812f06ee3d4SKevin Wolf assert(*backing_qiov == NULL); 813f06ee3d4SKevin Wolf *backing_qiov = g_new(QEMUIOVector, 1); 814f06ee3d4SKevin Wolf qemu_iovec_init(*backing_qiov, qiov->niov); 815f06ee3d4SKevin Wolf qemu_iovec_concat(*backing_qiov, qiov, 0, size); 816f06ee3d4SKevin Wolf 817820100fdSPaolo Bonzini BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); 8180f714ec7SKevin Wolf ret = bdrv_co_preadv(s->bs->backing, pos, size, *backing_qiov, 0); 819e85c5281SKevin Wolf if (ret < 0) { 820e85c5281SKevin Wolf return ret; 821e85c5281SKevin Wolf } 822e85c5281SKevin Wolf return 0; 823eabba580SStefan Hajnoczi } 824eabba580SStefan Hajnoczi 825eabba580SStefan Hajnoczi /** 826eabba580SStefan Hajnoczi * Copy data from backing file into the image 827eabba580SStefan Hajnoczi * 828eabba580SStefan Hajnoczi * @s: QED state 829eabba580SStefan Hajnoczi * @pos: Byte position in device 830eabba580SStefan Hajnoczi * @len: Number of bytes 831eabba580SStefan Hajnoczi * @offset: Byte offset in image file 832eabba580SStefan Hajnoczi */ 83387f0d882SKevin Wolf static int coroutine_fn qed_copy_from_backing_file(BDRVQEDState *s, 83487f0d882SKevin Wolf uint64_t pos, uint64_t len, 83587f0d882SKevin Wolf uint64_t offset) 836eabba580SStefan Hajnoczi { 8370f7aa24dSKevin Wolf QEMUIOVector qiov; 8380f7aa24dSKevin Wolf QEMUIOVector *backing_qiov = NULL; 8390f7aa24dSKevin Wolf struct iovec iov; 840e85c5281SKevin Wolf int ret; 841eabba580SStefan Hajnoczi 842eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 843eabba580SStefan Hajnoczi if (len == 0) { 844b4ac32f3SKevin Wolf return 0; 845eabba580SStefan Hajnoczi } 846eabba580SStefan Hajnoczi 8470f7aa24dSKevin Wolf iov = (struct iovec) { 8480f7aa24dSKevin Wolf .iov_base = qemu_blockalign(s->bs, len), 8490f7aa24dSKevin Wolf .iov_len = len, 8500f7aa24dSKevin Wolf }; 8510f7aa24dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 852eabba580SStefan Hajnoczi 8530f7aa24dSKevin Wolf ret = qed_read_backing_file(s, pos, &qiov, &backing_qiov); 8540f7aa24dSKevin Wolf 8550f7aa24dSKevin Wolf if (backing_qiov) { 8560f7aa24dSKevin Wolf qemu_iovec_destroy(backing_qiov); 8570f7aa24dSKevin Wolf g_free(backing_qiov); 8580f7aa24dSKevin Wolf backing_qiov = NULL; 8590f7aa24dSKevin Wolf } 8600f7aa24dSKevin Wolf 8610f7aa24dSKevin Wolf if (ret) { 8620f7aa24dSKevin Wolf goto out; 8630f7aa24dSKevin Wolf } 8640f7aa24dSKevin Wolf 8650f7aa24dSKevin Wolf BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 8660f714ec7SKevin Wolf ret = bdrv_co_pwritev(s->bs->file, offset, qiov.size, &qiov, 0); 8670f7aa24dSKevin Wolf if (ret < 0) { 8680f7aa24dSKevin Wolf goto out; 8690f7aa24dSKevin Wolf } 8700f7aa24dSKevin Wolf ret = 0; 8710f7aa24dSKevin Wolf out: 8720f7aa24dSKevin Wolf qemu_vfree(iov.iov_base); 873b4ac32f3SKevin Wolf return ret; 874eabba580SStefan Hajnoczi } 875eabba580SStefan Hajnoczi 876eabba580SStefan Hajnoczi /** 877eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 878eabba580SStefan Hajnoczi * 879eabba580SStefan Hajnoczi * @s: QED state 880eabba580SStefan Hajnoczi * @table: L2 table 881eabba580SStefan Hajnoczi * @index: First cluster index 882eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 88321df65b6SAnthony Liguori * @cluster: First cluster offset 88421df65b6SAnthony Liguori * 88521df65b6SAnthony Liguori * The cluster offset may be an allocated byte offset in the image file, the 88621df65b6SAnthony Liguori * zero cluster marker, or the unallocated cluster marker. 887*1f01e50bSPaolo Bonzini * 888*1f01e50bSPaolo Bonzini * Called with table_lock held. 889eabba580SStefan Hajnoczi */ 89087f0d882SKevin Wolf static void coroutine_fn qed_update_l2_table(BDRVQEDState *s, QEDTable *table, 89187f0d882SKevin Wolf int index, unsigned int n, 89287f0d882SKevin Wolf uint64_t cluster) 893eabba580SStefan Hajnoczi { 894eabba580SStefan Hajnoczi int i; 895eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 896eabba580SStefan Hajnoczi table->offsets[i] = cluster; 89721df65b6SAnthony Liguori if (!qed_offset_is_unalloc_cluster(cluster) && 89821df65b6SAnthony Liguori !qed_offset_is_zero_cluster(cluster)) { 899eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 900eabba580SStefan Hajnoczi } 901eabba580SStefan Hajnoczi } 90221df65b6SAnthony Liguori } 903eabba580SStefan Hajnoczi 904*1f01e50bSPaolo Bonzini /* Called with table_lock held. */ 90587f0d882SKevin Wolf static void coroutine_fn qed_aio_complete(QEDAIOCB *acb) 906eabba580SStefan Hajnoczi { 907eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 908eabba580SStefan Hajnoczi 909eabba580SStefan Hajnoczi /* Free resources */ 910eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov); 911eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 912eabba580SStefan Hajnoczi 9130e71be19SStefan Hajnoczi /* Free the buffer we may have allocated for zero writes */ 9140e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 9150e71be19SStefan Hajnoczi qemu_vfree(acb->qiov->iov[0].iov_base); 9160e71be19SStefan Hajnoczi acb->qiov->iov[0].iov_base = NULL; 9170e71be19SStefan Hajnoczi } 9180e71be19SStefan Hajnoczi 919eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 920eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 921eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 922eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 923eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 924eabba580SStefan Hajnoczi */ 9250806c3b5SKevin Wolf if (acb == s->allocating_acb) { 9260806c3b5SKevin Wolf s->allocating_acb = NULL; 9270806c3b5SKevin Wolf if (!qemu_co_queue_empty(&s->allocating_write_reqs)) { 928*1f01e50bSPaolo Bonzini qemu_co_queue_next(&s->allocating_write_reqs); 9296f321e93SStefan Hajnoczi } else if (s->header.features & QED_F_NEED_CHECK) { 9306f321e93SStefan Hajnoczi qed_start_need_check_timer(s); 931eabba580SStefan Hajnoczi } 932eabba580SStefan Hajnoczi } 933eabba580SStefan Hajnoczi } 934eabba580SStefan Hajnoczi 935eabba580SStefan Hajnoczi /** 936fae25ac7SKevin Wolf * Update L1 table with new L2 table offset and write it out 937*1f01e50bSPaolo Bonzini * 938*1f01e50bSPaolo Bonzini * Called with table_lock held. 939eabba580SStefan Hajnoczi */ 94087f0d882SKevin Wolf static int coroutine_fn qed_aio_write_l1_update(QEDAIOCB *acb) 941eabba580SStefan Hajnoczi { 942eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 943eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 944e4fc8781SStefan Hajnoczi uint64_t l2_offset = l2_table->offset; 945fb18de21SKevin Wolf int index, ret; 946fae25ac7SKevin Wolf 947fae25ac7SKevin Wolf index = qed_l1_index(s, acb->cur_pos); 948fae25ac7SKevin Wolf s->l1_table->offsets[index] = l2_table->offset; 949fae25ac7SKevin Wolf 950fae25ac7SKevin Wolf ret = qed_write_l1_table(s, index, 1); 951fae25ac7SKevin Wolf 952fae25ac7SKevin Wolf /* Commit the current L2 table to the cache */ 953eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 954eabba580SStefan Hajnoczi 955eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 956eabba580SStefan Hajnoczi * cache. 957eabba580SStefan Hajnoczi */ 958e4fc8781SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 959eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 960eabba580SStefan Hajnoczi 961fb18de21SKevin Wolf return ret; 962eabba580SStefan Hajnoczi } 963eabba580SStefan Hajnoczi 964eabba580SStefan Hajnoczi 965eabba580SStefan Hajnoczi /** 966eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 967*1f01e50bSPaolo Bonzini * 968*1f01e50bSPaolo Bonzini * Called with table_lock held. 969eabba580SStefan Hajnoczi */ 97087f0d882SKevin Wolf static int coroutine_fn qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset) 971eabba580SStefan Hajnoczi { 972eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 973eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 97488d2dd72SKevin Wolf int index, ret; 975eabba580SStefan Hajnoczi 976eabba580SStefan Hajnoczi if (need_alloc) { 977eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 978eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 979eabba580SStefan Hajnoczi } 980eabba580SStefan Hajnoczi 981eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 982eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 9830e71be19SStefan Hajnoczi offset); 984eabba580SStefan Hajnoczi 985eabba580SStefan Hajnoczi if (need_alloc) { 986eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 987453e53e2SKevin Wolf ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true); 988fb18de21SKevin Wolf if (ret) { 98988d2dd72SKevin Wolf return ret; 990fb18de21SKevin Wolf } 99188d2dd72SKevin Wolf return qed_aio_write_l1_update(acb); 992eabba580SStefan Hajnoczi } else { 993eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 994453e53e2SKevin Wolf ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, 995453e53e2SKevin Wolf false); 99688d2dd72SKevin Wolf if (ret) { 99788d2dd72SKevin Wolf return ret; 998eabba580SStefan Hajnoczi } 99988d2dd72SKevin Wolf } 100088d2dd72SKevin Wolf return 0; 1001eabba580SStefan Hajnoczi } 1002eabba580SStefan Hajnoczi 1003eabba580SStefan Hajnoczi /** 1004eabba580SStefan Hajnoczi * Write data to the image file 1005*1f01e50bSPaolo Bonzini * 1006*1f01e50bSPaolo Bonzini * Called with table_lock *not* held. 1007eabba580SStefan Hajnoczi */ 100887f0d882SKevin Wolf static int coroutine_fn qed_aio_write_main(QEDAIOCB *acb) 1009eabba580SStefan Hajnoczi { 1010eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1011eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 1012eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 1013eabba580SStefan Hajnoczi 1014eaf0bc56SKevin Wolf trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size); 1015eabba580SStefan Hajnoczi 1016a4d8f1aeSKevin Wolf BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 1017e7569c18SPaolo Bonzini return bdrv_co_pwritev(s->bs->file, offset, acb->cur_qiov.size, 10180f714ec7SKevin Wolf &acb->cur_qiov, 0); 1019eabba580SStefan Hajnoczi } 1020eabba580SStefan Hajnoczi 1021eabba580SStefan Hajnoczi /** 1022b4ac32f3SKevin Wolf * Populate untouched regions of new data cluster 1023*1f01e50bSPaolo Bonzini * 1024*1f01e50bSPaolo Bonzini * Called with table_lock held. 1025eabba580SStefan Hajnoczi */ 102687f0d882SKevin Wolf static int coroutine_fn qed_aio_write_cow(QEDAIOCB *acb) 1027eabba580SStefan Hajnoczi { 1028eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1029b4ac32f3SKevin Wolf uint64_t start, len, offset; 1030a101341aSKevin Wolf int ret; 1031eabba580SStefan Hajnoczi 1032*1f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 1033*1f01e50bSPaolo Bonzini 1034b4ac32f3SKevin Wolf /* Populate front untouched region of new data cluster */ 1035b4ac32f3SKevin Wolf start = qed_start_of_cluster(s, acb->cur_pos); 1036b4ac32f3SKevin Wolf len = qed_offset_into_cluster(s, acb->cur_pos); 1037b4ac32f3SKevin Wolf 1038b4ac32f3SKevin Wolf trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1039b4ac32f3SKevin Wolf ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster); 1040a101341aSKevin Wolf if (ret < 0) { 1041*1f01e50bSPaolo Bonzini goto out; 1042eabba580SStefan Hajnoczi } 1043eabba580SStefan Hajnoczi 1044b4ac32f3SKevin Wolf /* Populate back untouched region of new data cluster */ 1045b4ac32f3SKevin Wolf start = acb->cur_pos + acb->cur_qiov.size; 1046b4ac32f3SKevin Wolf len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1047b4ac32f3SKevin Wolf offset = acb->cur_cluster + 1048b4ac32f3SKevin Wolf qed_offset_into_cluster(s, acb->cur_pos) + 1049b4ac32f3SKevin Wolf acb->cur_qiov.size; 1050b4ac32f3SKevin Wolf 1051eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 1052b4ac32f3SKevin Wolf ret = qed_copy_from_backing_file(s, start, len, offset); 1053a101341aSKevin Wolf if (ret < 0) { 1054*1f01e50bSPaolo Bonzini goto out; 1055eaf0bc56SKevin Wolf } 1056eabba580SStefan Hajnoczi 1057e7569c18SPaolo Bonzini ret = qed_aio_write_main(acb); 1058e7569c18SPaolo Bonzini if (ret < 0) { 1059*1f01e50bSPaolo Bonzini goto out; 1060e7569c18SPaolo Bonzini } 1061e7569c18SPaolo Bonzini 1062e7569c18SPaolo Bonzini if (s->bs->backing) { 1063e7569c18SPaolo Bonzini /* 1064e7569c18SPaolo Bonzini * Flush new data clusters before updating the L2 table 1065e7569c18SPaolo Bonzini * 1066e7569c18SPaolo Bonzini * This flush is necessary when a backing file is in use. A crash 1067e7569c18SPaolo Bonzini * during an allocating write could result in empty clusters in the 1068e7569c18SPaolo Bonzini * image. If the write only touched a subregion of the cluster, 1069e7569c18SPaolo Bonzini * then backing image sectors have been lost in the untouched 1070e7569c18SPaolo Bonzini * region. The solution is to flush after writing a new data 1071e7569c18SPaolo Bonzini * cluster and before updating the L2 table. 1072e7569c18SPaolo Bonzini */ 1073e7569c18SPaolo Bonzini ret = bdrv_co_flush(s->bs->file->bs); 1074e7569c18SPaolo Bonzini } 1075e7569c18SPaolo Bonzini 1076*1f01e50bSPaolo Bonzini out: 1077*1f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 1078*1f01e50bSPaolo Bonzini return ret; 1079eabba580SStefan Hajnoczi } 1080eabba580SStefan Hajnoczi 1081eabba580SStefan Hajnoczi /** 10820d09c797SStefan Hajnoczi * Check if the QED_F_NEED_CHECK bit should be set during allocating write 10830d09c797SStefan Hajnoczi */ 10840d09c797SStefan Hajnoczi static bool qed_should_set_need_check(BDRVQEDState *s) 10850d09c797SStefan Hajnoczi { 10860d09c797SStefan Hajnoczi /* The flush before L2 update path ensures consistency */ 1087760e0063SKevin Wolf if (s->bs->backing) { 10880d09c797SStefan Hajnoczi return false; 10890d09c797SStefan Hajnoczi } 10900d09c797SStefan Hajnoczi 10910d09c797SStefan Hajnoczi return !(s->header.features & QED_F_NEED_CHECK); 10920d09c797SStefan Hajnoczi } 10930d09c797SStefan Hajnoczi 10940d09c797SStefan Hajnoczi /** 1095eabba580SStefan Hajnoczi * Write new data cluster 1096eabba580SStefan Hajnoczi * 1097eabba580SStefan Hajnoczi * @acb: Write request 1098eabba580SStefan Hajnoczi * @len: Length in bytes 1099eabba580SStefan Hajnoczi * 1100eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters. 1101*1f01e50bSPaolo Bonzini * 1102*1f01e50bSPaolo Bonzini * Called with table_lock held. 1103eabba580SStefan Hajnoczi */ 110487f0d882SKevin Wolf static int coroutine_fn qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1105eabba580SStefan Hajnoczi { 1106eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1107f13d712bSKevin Wolf int ret; 1108eabba580SStefan Hajnoczi 11096f321e93SStefan Hajnoczi /* Cancel timer when the first allocating request comes in */ 11100806c3b5SKevin Wolf if (s->allocating_acb == NULL) { 11116f321e93SStefan Hajnoczi qed_cancel_need_check_timer(s); 11126f321e93SStefan Hajnoczi } 11136f321e93SStefan Hajnoczi 1114eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 11150806c3b5SKevin Wolf if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) { 11160806c3b5SKevin Wolf if (s->allocating_acb != NULL) { 1117*1f01e50bSPaolo Bonzini qemu_co_queue_wait(&s->allocating_write_reqs, &s->table_lock); 11180806c3b5SKevin Wolf assert(s->allocating_acb == NULL); 1119eabba580SStefan Hajnoczi } 11200806c3b5SKevin Wolf s->allocating_acb = acb; 11210806c3b5SKevin Wolf return -EAGAIN; /* start over with looking up table entries */ 1122eabba580SStefan Hajnoczi } 1123eabba580SStefan Hajnoczi 1124eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 1125eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 11261b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1127eabba580SStefan Hajnoczi 11280e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 11290e71be19SStefan Hajnoczi /* Skip ahead if the clusters are already zero */ 11300e71be19SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { 1131d6daddcdSKevin Wolf return 0; 11320e71be19SStefan Hajnoczi } 1133e7569c18SPaolo Bonzini acb->cur_cluster = 1; 11340e71be19SStefan Hajnoczi } else { 11350e71be19SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 11360e71be19SStefan Hajnoczi } 11370e71be19SStefan Hajnoczi 11380d09c797SStefan Hajnoczi if (qed_should_set_need_check(s)) { 113901979a98SStefan Hajnoczi s->header.features |= QED_F_NEED_CHECK; 1140f13d712bSKevin Wolf ret = qed_write_header(s); 1141a101341aSKevin Wolf if (ret < 0) { 1142d6daddcdSKevin Wolf return ret; 11430d09c797SStefan Hajnoczi } 1144eabba580SStefan Hajnoczi } 1145eabba580SStefan Hajnoczi 1146e7569c18SPaolo Bonzini if (!(acb->flags & QED_AIOCB_ZERO)) { 1147a101341aSKevin Wolf ret = qed_aio_write_cow(acb); 1148a101341aSKevin Wolf if (ret < 0) { 1149d6daddcdSKevin Wolf return ret; 1150a101341aSKevin Wolf } 1151e7569c18SPaolo Bonzini } 1152e7569c18SPaolo Bonzini 1153e7569c18SPaolo Bonzini return qed_aio_write_l2_update(acb, acb->cur_cluster); 1154a101341aSKevin Wolf } 1155a101341aSKevin Wolf 1156eabba580SStefan Hajnoczi /** 1157eabba580SStefan Hajnoczi * Write data cluster in place 1158eabba580SStefan Hajnoczi * 1159eabba580SStefan Hajnoczi * @acb: Write request 1160eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1161eabba580SStefan Hajnoczi * @len: Length in bytes 1162eabba580SStefan Hajnoczi * 1163eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 1164*1f01e50bSPaolo Bonzini * 1165*1f01e50bSPaolo Bonzini * Called with table_lock held. 1166eabba580SStefan Hajnoczi */ 116787f0d882SKevin Wolf static int coroutine_fn qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, 116887f0d882SKevin Wolf size_t len) 1169eabba580SStefan Hajnoczi { 1170*1f01e50bSPaolo Bonzini BDRVQEDState *s = acb_to_s(acb); 1171*1f01e50bSPaolo Bonzini int r; 1172*1f01e50bSPaolo Bonzini 1173*1f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 1174*1f01e50bSPaolo Bonzini 11750e71be19SStefan Hajnoczi /* Allocate buffer for zero writes */ 11760e71be19SStefan Hajnoczi if (acb->flags & QED_AIOCB_ZERO) { 11770e71be19SStefan Hajnoczi struct iovec *iov = acb->qiov->iov; 11780e71be19SStefan Hajnoczi 11790e71be19SStefan Hajnoczi if (!iov->iov_base) { 118048cc565eSKevin Wolf iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len); 11814f4896dbSKevin Wolf if (iov->iov_base == NULL) { 1182*1f01e50bSPaolo Bonzini r = -ENOMEM; 1183*1f01e50bSPaolo Bonzini goto out; 11844f4896dbSKevin Wolf } 11850e71be19SStefan Hajnoczi memset(iov->iov_base, 0, iov->iov_len); 11860e71be19SStefan Hajnoczi } 11870e71be19SStefan Hajnoczi } 11880e71be19SStefan Hajnoczi 1189eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 1190eabba580SStefan Hajnoczi acb->cur_cluster = offset; 11911b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1192eabba580SStefan Hajnoczi 1193*1f01e50bSPaolo Bonzini /* Do the actual write. */ 1194*1f01e50bSPaolo Bonzini r = qed_aio_write_main(acb); 1195*1f01e50bSPaolo Bonzini out: 1196*1f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 1197*1f01e50bSPaolo Bonzini return r; 1198eabba580SStefan Hajnoczi } 1199eabba580SStefan Hajnoczi 1200eabba580SStefan Hajnoczi /** 1201eabba580SStefan Hajnoczi * Write data cluster 1202eabba580SStefan Hajnoczi * 1203eabba580SStefan Hajnoczi * @opaque: Write request 12040596be7eSKevin Wolf * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 1205eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1206eabba580SStefan Hajnoczi * @len: Length in bytes 1207*1f01e50bSPaolo Bonzini * 1208*1f01e50bSPaolo Bonzini * Called with table_lock held. 1209eabba580SStefan Hajnoczi */ 121087f0d882SKevin Wolf static int coroutine_fn qed_aio_write_data(void *opaque, int ret, 1211eabba580SStefan Hajnoczi uint64_t offset, size_t len) 1212eabba580SStefan Hajnoczi { 1213eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1214eabba580SStefan Hajnoczi 1215eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1216eabba580SStefan Hajnoczi 1217eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 1218eabba580SStefan Hajnoczi 1219eabba580SStefan Hajnoczi switch (ret) { 1220eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 12210596be7eSKevin Wolf return qed_aio_write_inplace(acb, offset, len); 1222eabba580SStefan Hajnoczi 1223eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 1224eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 122521df65b6SAnthony Liguori case QED_CLUSTER_ZERO: 12260596be7eSKevin Wolf return qed_aio_write_alloc(acb, len); 1227eabba580SStefan Hajnoczi 1228eabba580SStefan Hajnoczi default: 12290596be7eSKevin Wolf g_assert_not_reached(); 1230eabba580SStefan Hajnoczi } 1231eabba580SStefan Hajnoczi } 1232eabba580SStefan Hajnoczi 1233eabba580SStefan Hajnoczi /** 1234eabba580SStefan Hajnoczi * Read data cluster 1235eabba580SStefan Hajnoczi * 1236eabba580SStefan Hajnoczi * @opaque: Read request 12370596be7eSKevin Wolf * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 1238eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 1239eabba580SStefan Hajnoczi * @len: Length in bytes 1240*1f01e50bSPaolo Bonzini * 1241*1f01e50bSPaolo Bonzini * Called with table_lock held. 1242eabba580SStefan Hajnoczi */ 124387f0d882SKevin Wolf static int coroutine_fn qed_aio_read_data(void *opaque, int ret, 124487f0d882SKevin Wolf uint64_t offset, size_t len) 1245eabba580SStefan Hajnoczi { 1246eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1247eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 124848cc565eSKevin Wolf BlockDriverState *bs = acb->bs; 1249*1f01e50bSPaolo Bonzini int r; 1250*1f01e50bSPaolo Bonzini 1251*1f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 1252eabba580SStefan Hajnoczi 1253eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 1254eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 1255eabba580SStefan Hajnoczi 1256eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 1257eabba580SStefan Hajnoczi 12581b093c48SMichael Tokarev qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1259eabba580SStefan Hajnoczi 1260*1f01e50bSPaolo Bonzini /* Handle zero cluster and backing file reads, otherwise read 1261*1f01e50bSPaolo Bonzini * data cluster directly. 1262*1f01e50bSPaolo Bonzini */ 126321df65b6SAnthony Liguori if (ret == QED_CLUSTER_ZERO) { 12643d9b4925SMichael Tokarev qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); 1265*1f01e50bSPaolo Bonzini r = 0; 126621df65b6SAnthony Liguori } else if (ret != QED_CLUSTER_FOUND) { 1267*1f01e50bSPaolo Bonzini r = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1268e85c5281SKevin Wolf &acb->backing_qiov); 1269*1f01e50bSPaolo Bonzini } else { 1270*1f01e50bSPaolo Bonzini BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 1271*1f01e50bSPaolo Bonzini r = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size, 1272*1f01e50bSPaolo Bonzini &acb->cur_qiov, 0); 1273eabba580SStefan Hajnoczi } 1274eabba580SStefan Hajnoczi 1275*1f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 1276*1f01e50bSPaolo Bonzini return r; 1277eabba580SStefan Hajnoczi } 1278eabba580SStefan Hajnoczi 1279eabba580SStefan Hajnoczi /** 1280eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1281eabba580SStefan Hajnoczi */ 128287f0d882SKevin Wolf static int coroutine_fn qed_aio_next_io(QEDAIOCB *acb) 1283eabba580SStefan Hajnoczi { 1284eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 12850f21b7a1SKevin Wolf uint64_t offset; 12860f21b7a1SKevin Wolf size_t len; 1287dddf8db1SKevin Wolf int ret; 1288eabba580SStefan Hajnoczi 1289*1f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 129001859874SKevin Wolf while (1) { 1291dddf8db1SKevin Wolf trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size); 1292eabba580SStefan Hajnoczi 1293f06ee3d4SKevin Wolf if (acb->backing_qiov) { 1294f06ee3d4SKevin Wolf qemu_iovec_destroy(acb->backing_qiov); 1295f06ee3d4SKevin Wolf g_free(acb->backing_qiov); 1296f06ee3d4SKevin Wolf acb->backing_qiov = NULL; 1297f06ee3d4SKevin Wolf } 1298f06ee3d4SKevin Wolf 1299eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1300eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1301eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1302eabba580SStefan Hajnoczi 1303eabba580SStefan Hajnoczi /* Complete request */ 1304eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 130548cc565eSKevin Wolf ret = 0; 130648cc565eSKevin Wolf break; 1307eabba580SStefan Hajnoczi } 1308eabba580SStefan Hajnoczi 1309eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 13100f21b7a1SKevin Wolf len = acb->end_pos - acb->cur_pos; 13110f21b7a1SKevin Wolf ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset); 13120596be7eSKevin Wolf if (ret < 0) { 131348cc565eSKevin Wolf break; 13140596be7eSKevin Wolf } 13150596be7eSKevin Wolf 13160596be7eSKevin Wolf if (acb->flags & QED_AIOCB_WRITE) { 13170596be7eSKevin Wolf ret = qed_aio_write_data(acb, ret, offset, len); 13180596be7eSKevin Wolf } else { 13190596be7eSKevin Wolf ret = qed_aio_read_data(acb, ret, offset, len); 13200596be7eSKevin Wolf } 13210596be7eSKevin Wolf 13220806c3b5SKevin Wolf if (ret < 0 && ret != -EAGAIN) { 132348cc565eSKevin Wolf break; 132401859874SKevin Wolf } 1325eabba580SStefan Hajnoczi } 1326eabba580SStefan Hajnoczi 132748cc565eSKevin Wolf trace_qed_aio_complete(s, acb, ret); 132848cc565eSKevin Wolf qed_aio_complete(acb); 1329*1f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 133048cc565eSKevin Wolf return ret; 133189f89709SKevin Wolf } 133289f89709SKevin Wolf 133389f89709SKevin Wolf static int coroutine_fn qed_co_request(BlockDriverState *bs, int64_t sector_num, 133489f89709SKevin Wolf QEMUIOVector *qiov, int nb_sectors, 133589f89709SKevin Wolf int flags) 133689f89709SKevin Wolf { 133748cc565eSKevin Wolf QEDAIOCB acb = { 133848cc565eSKevin Wolf .bs = bs, 133948cc565eSKevin Wolf .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE, 134048cc565eSKevin Wolf .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE, 134148cc565eSKevin Wolf .qiov = qiov, 134248cc565eSKevin Wolf .flags = flags, 134389f89709SKevin Wolf }; 134448cc565eSKevin Wolf qemu_iovec_init(&acb.cur_qiov, qiov->niov); 134589f89709SKevin Wolf 134648cc565eSKevin Wolf trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags); 1347eabba580SStefan Hajnoczi 1348eabba580SStefan Hajnoczi /* Start request */ 134948cc565eSKevin Wolf return qed_aio_next_io(&acb); 135089f89709SKevin Wolf } 135189f89709SKevin Wolf 135289f89709SKevin Wolf static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs, 135389f89709SKevin Wolf int64_t sector_num, int nb_sectors, 135489f89709SKevin Wolf QEMUIOVector *qiov) 135575411d23SStefan Hajnoczi { 135689f89709SKevin Wolf return qed_co_request(bs, sector_num, qiov, nb_sectors, 0); 135775411d23SStefan Hajnoczi } 135875411d23SStefan Hajnoczi 135989f89709SKevin Wolf static int coroutine_fn bdrv_qed_co_writev(BlockDriverState *bs, 136089f89709SKevin Wolf int64_t sector_num, int nb_sectors, 136189f89709SKevin Wolf QEMUIOVector *qiov) 136275411d23SStefan Hajnoczi { 136389f89709SKevin Wolf return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRITE); 13640e71be19SStefan Hajnoczi } 13650e71be19SStefan Hajnoczi 136649a2e483SEric Blake static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, 136749a2e483SEric Blake int64_t offset, 1368f5a5ca79SManos Pitsidianakis int bytes, 1369aa7bfbffSPeter Lieven BdrvRequestFlags flags) 13700e71be19SStefan Hajnoczi { 1371ef72f76eSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 13720e71be19SStefan Hajnoczi QEMUIOVector qiov; 13730e71be19SStefan Hajnoczi struct iovec iov; 13740e71be19SStefan Hajnoczi 137549a2e483SEric Blake /* Fall back if the request is not aligned */ 137649a2e483SEric Blake if (qed_offset_into_cluster(s, offset) || 1377f5a5ca79SManos Pitsidianakis qed_offset_into_cluster(s, bytes)) { 1378ef72f76eSStefan Hajnoczi return -ENOTSUP; 1379ef72f76eSStefan Hajnoczi } 1380ef72f76eSStefan Hajnoczi 13810e71be19SStefan Hajnoczi /* Zero writes start without an I/O buffer. If a buffer becomes necessary 13820e71be19SStefan Hajnoczi * then it will be allocated during request processing. 13830e71be19SStefan Hajnoczi */ 138449a2e483SEric Blake iov.iov_base = NULL; 1385f5a5ca79SManos Pitsidianakis iov.iov_len = bytes; 13860e71be19SStefan Hajnoczi 13870e71be19SStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 138889f89709SKevin Wolf return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov, 1389f5a5ca79SManos Pitsidianakis bytes >> BDRV_SECTOR_BITS, 13900e71be19SStefan Hajnoczi QED_AIOCB_WRITE | QED_AIOCB_ZERO); 13910e71be19SStefan Hajnoczi } 13920e71be19SStefan Hajnoczi 13938243ccb7SMax Reitz static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset, 13948243ccb7SMax Reitz PreallocMode prealloc, Error **errp) 139575411d23SStefan Hajnoczi { 139677a5a000SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 139777a5a000SStefan Hajnoczi uint64_t old_image_size; 139877a5a000SStefan Hajnoczi int ret; 139977a5a000SStefan Hajnoczi 14008243ccb7SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 14018243ccb7SMax Reitz error_setg(errp, "Unsupported preallocation mode '%s'", 14028243ccb7SMax Reitz PreallocMode_lookup[prealloc]); 14038243ccb7SMax Reitz return -ENOTSUP; 14048243ccb7SMax Reitz } 14058243ccb7SMax Reitz 140677a5a000SStefan Hajnoczi if (!qed_is_image_size_valid(offset, s->header.cluster_size, 140777a5a000SStefan Hajnoczi s->header.table_size)) { 1408f59adb32SMax Reitz error_setg(errp, "Invalid image size specified"); 140977a5a000SStefan Hajnoczi return -EINVAL; 141077a5a000SStefan Hajnoczi } 141177a5a000SStefan Hajnoczi 141277a5a000SStefan Hajnoczi if ((uint64_t)offset < s->header.image_size) { 1413f59adb32SMax Reitz error_setg(errp, "Shrinking images is currently not supported"); 141475411d23SStefan Hajnoczi return -ENOTSUP; 141575411d23SStefan Hajnoczi } 141675411d23SStefan Hajnoczi 141777a5a000SStefan Hajnoczi old_image_size = s->header.image_size; 141877a5a000SStefan Hajnoczi s->header.image_size = offset; 141977a5a000SStefan Hajnoczi ret = qed_write_header_sync(s); 142077a5a000SStefan Hajnoczi if (ret < 0) { 142177a5a000SStefan Hajnoczi s->header.image_size = old_image_size; 1422f59adb32SMax Reitz error_setg_errno(errp, -ret, "Failed to update the image size"); 142377a5a000SStefan Hajnoczi } 142477a5a000SStefan Hajnoczi return ret; 142577a5a000SStefan Hajnoczi } 142677a5a000SStefan Hajnoczi 142775411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs) 142875411d23SStefan Hajnoczi { 142975411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 143075411d23SStefan Hajnoczi return s->header.image_size; 143175411d23SStefan Hajnoczi } 143275411d23SStefan Hajnoczi 143375411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 143475411d23SStefan Hajnoczi { 143575411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 143675411d23SStefan Hajnoczi 143775411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi)); 143875411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size; 1439d68dbee8SDong Xu Wang bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; 144095de6d70SPaolo Bonzini bdi->unallocated_blocks_are_zero = true; 144195de6d70SPaolo Bonzini bdi->can_write_zeroes_with_unmap = true; 144275411d23SStefan Hajnoczi return 0; 144375411d23SStefan Hajnoczi } 144475411d23SStefan Hajnoczi 144575411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 144675411d23SStefan Hajnoczi const char *backing_file, 144775411d23SStefan Hajnoczi const char *backing_fmt) 144875411d23SStefan Hajnoczi { 144975411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 145075411d23SStefan Hajnoczi QEDHeader new_header, le_header; 145175411d23SStefan Hajnoczi void *buffer; 145275411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 145375411d23SStefan Hajnoczi int ret; 145475411d23SStefan Hajnoczi 145575411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 145675411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 145775411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 145875411d23SStefan Hajnoczi * add a new string. 145975411d23SStefan Hajnoczi */ 146075411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 146175411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 146275411d23SStefan Hajnoczi return -ENOTSUP; 146375411d23SStefan Hajnoczi } 146475411d23SStefan Hajnoczi 146575411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 146675411d23SStefan Hajnoczi 146775411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 146875411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 146975411d23SStefan Hajnoczi 147075411d23SStefan Hajnoczi /* Adjust feature flags */ 147175411d23SStefan Hajnoczi if (backing_file) { 147275411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 147375411d23SStefan Hajnoczi 147475411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 147575411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 147675411d23SStefan Hajnoczi } 147775411d23SStefan Hajnoczi } 147875411d23SStefan Hajnoczi 147975411d23SStefan Hajnoczi /* Calculate new header size */ 148075411d23SStefan Hajnoczi backing_file_len = 0; 148175411d23SStefan Hajnoczi 148275411d23SStefan Hajnoczi if (backing_file) { 148375411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 148475411d23SStefan Hajnoczi } 148575411d23SStefan Hajnoczi 148675411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 148775411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 148875411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 148975411d23SStefan Hajnoczi buffer_len += backing_file_len; 149075411d23SStefan Hajnoczi 149175411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 149275411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 149375411d23SStefan Hajnoczi return -ENOSPC; 149475411d23SStefan Hajnoczi } 149575411d23SStefan Hajnoczi 149675411d23SStefan Hajnoczi /* Prepare new header */ 14977267c094SAnthony Liguori buffer = g_malloc(buffer_len); 149875411d23SStefan Hajnoczi 149975411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 150075411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 150175411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 150275411d23SStefan Hajnoczi 1503feba23b1SPavel Borzenkov if (backing_file) { 150475411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 150575411d23SStefan Hajnoczi buffer_len += backing_file_len; 1506feba23b1SPavel Borzenkov } 150775411d23SStefan Hajnoczi 150875411d23SStefan Hajnoczi /* Write new header */ 1509d9ca2ea2SKevin Wolf ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); 15107267c094SAnthony Liguori g_free(buffer); 151175411d23SStefan Hajnoczi if (ret == 0) { 151275411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 151375411d23SStefan Hajnoczi } 151475411d23SStefan Hajnoczi return ret; 151575411d23SStefan Hajnoczi } 151675411d23SStefan Hajnoczi 15175a8a30dbSKevin Wolf static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp) 1518c82954e5SBenoît Canet { 1519*1f01e50bSPaolo Bonzini BDRVQEDState *s = bs->opaque; 15205a8a30dbSKevin Wolf Error *local_err = NULL; 15215a8a30dbSKevin Wolf int ret; 1522c82954e5SBenoît Canet 1523c82954e5SBenoît Canet bdrv_qed_close(bs); 15243456a8d1SKevin Wolf 152561c7887eSPaolo Bonzini bdrv_qed_init_state(bs); 1526*1f01e50bSPaolo Bonzini if (qemu_in_coroutine()) { 1527*1f01e50bSPaolo Bonzini qemu_co_mutex_lock(&s->table_lock); 1528*1f01e50bSPaolo Bonzini } 15294e4bf5c4SKevin Wolf ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, &local_err); 1530*1f01e50bSPaolo Bonzini if (qemu_in_coroutine()) { 1531*1f01e50bSPaolo Bonzini qemu_co_mutex_unlock(&s->table_lock); 1532*1f01e50bSPaolo Bonzini } 15335a8a30dbSKevin Wolf if (local_err) { 1534e43bfd9cSMarkus Armbruster error_propagate(errp, local_err); 1535e43bfd9cSMarkus Armbruster error_prepend(errp, "Could not reopen qed layer: "); 15365a8a30dbSKevin Wolf return; 15375a8a30dbSKevin Wolf } else if (ret < 0) { 15385a8a30dbSKevin Wolf error_setg_errno(errp, -ret, "Could not reopen qed layer"); 15395a8a30dbSKevin Wolf return; 15405a8a30dbSKevin Wolf } 1541c82954e5SBenoît Canet } 1542c82954e5SBenoît Canet 15434534ff54SKevin Wolf static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result, 15444534ff54SKevin Wolf BdrvCheckMode fix) 154575411d23SStefan Hajnoczi { 154601979a98SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 154701979a98SStefan Hajnoczi 15484534ff54SKevin Wolf return qed_check(s, result, !!fix); 154975411d23SStefan Hajnoczi } 155075411d23SStefan Hajnoczi 15517ab74849SChunyan Liu static QemuOptsList qed_create_opts = { 15527ab74849SChunyan Liu .name = "qed-create-opts", 15537ab74849SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head), 15547ab74849SChunyan Liu .desc = { 155575411d23SStefan Hajnoczi { 155675411d23SStefan Hajnoczi .name = BLOCK_OPT_SIZE, 15577ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 15587ab74849SChunyan Liu .help = "Virtual disk size" 15597ab74849SChunyan Liu }, 15607ab74849SChunyan Liu { 156175411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FILE, 15627ab74849SChunyan Liu .type = QEMU_OPT_STRING, 156375411d23SStefan Hajnoczi .help = "File name of a base image" 15647ab74849SChunyan Liu }, 15657ab74849SChunyan Liu { 156675411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FMT, 15677ab74849SChunyan Liu .type = QEMU_OPT_STRING, 156875411d23SStefan Hajnoczi .help = "Image format of the base image" 15697ab74849SChunyan Liu }, 15707ab74849SChunyan Liu { 157175411d23SStefan Hajnoczi .name = BLOCK_OPT_CLUSTER_SIZE, 15727ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 157399cce9faSKevin Wolf .help = "Cluster size (in bytes)", 15747ab74849SChunyan Liu .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE) 15757ab74849SChunyan Liu }, 15767ab74849SChunyan Liu { 157775411d23SStefan Hajnoczi .name = BLOCK_OPT_TABLE_SIZE, 15787ab74849SChunyan Liu .type = QEMU_OPT_SIZE, 157975411d23SStefan Hajnoczi .help = "L1/L2 table size (in clusters)" 158075411d23SStefan Hajnoczi }, 158175411d23SStefan Hajnoczi { /* end of list */ } 15827ab74849SChunyan Liu } 158375411d23SStefan Hajnoczi }; 158475411d23SStefan Hajnoczi 158575411d23SStefan Hajnoczi static BlockDriver bdrv_qed = { 158675411d23SStefan Hajnoczi .format_name = "qed", 158775411d23SStefan Hajnoczi .instance_size = sizeof(BDRVQEDState), 15887ab74849SChunyan Liu .create_opts = &qed_create_opts, 15898ee79e70SKevin Wolf .supports_backing = true, 159075411d23SStefan Hajnoczi 159175411d23SStefan Hajnoczi .bdrv_probe = bdrv_qed_probe, 159275411d23SStefan Hajnoczi .bdrv_open = bdrv_qed_open, 159375411d23SStefan Hajnoczi .bdrv_close = bdrv_qed_close, 1594f9cb20f1SJeff Cody .bdrv_reopen_prepare = bdrv_qed_reopen_prepare, 1595862f215fSKevin Wolf .bdrv_child_perm = bdrv_format_default_perms, 1596c282e1fdSChunyan Liu .bdrv_create = bdrv_qed_create, 15973ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 1598b6b8a333SPaolo Bonzini .bdrv_co_get_block_status = bdrv_qed_co_get_block_status, 159989f89709SKevin Wolf .bdrv_co_readv = bdrv_qed_co_readv, 160089f89709SKevin Wolf .bdrv_co_writev = bdrv_qed_co_writev, 160149a2e483SEric Blake .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes, 160275411d23SStefan Hajnoczi .bdrv_truncate = bdrv_qed_truncate, 160375411d23SStefan Hajnoczi .bdrv_getlength = bdrv_qed_getlength, 160475411d23SStefan Hajnoczi .bdrv_get_info = bdrv_qed_get_info, 1605d34682cdSKevin Wolf .bdrv_refresh_limits = bdrv_qed_refresh_limits, 160675411d23SStefan Hajnoczi .bdrv_change_backing_file = bdrv_qed_change_backing_file, 1607c82954e5SBenoît Canet .bdrv_invalidate_cache = bdrv_qed_invalidate_cache, 160875411d23SStefan Hajnoczi .bdrv_check = bdrv_qed_check, 1609a8c868c3SStefan Hajnoczi .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, 1610a8c868c3SStefan Hajnoczi .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, 161161124f03SPaolo Bonzini .bdrv_co_drain = bdrv_qed_co_drain, 161275411d23SStefan Hajnoczi }; 161375411d23SStefan Hajnoczi 161475411d23SStefan Hajnoczi static void bdrv_qed_init(void) 161575411d23SStefan Hajnoczi { 161675411d23SStefan Hajnoczi bdrv_register(&bdrv_qed); 161775411d23SStefan Hajnoczi } 161875411d23SStefan Hajnoczi 161975411d23SStefan Hajnoczi block_init(bdrv_qed_init); 1620