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 15*eabba580SStefan Hajnoczi #include "trace.h" 1675411d23SStefan Hajnoczi #include "qed.h" 1775411d23SStefan Hajnoczi 18*eabba580SStefan Hajnoczi static void qed_aio_cancel(BlockDriverAIOCB *blockacb) 19*eabba580SStefan Hajnoczi { 20*eabba580SStefan Hajnoczi QEDAIOCB *acb = (QEDAIOCB *)blockacb; 21*eabba580SStefan Hajnoczi bool finished = false; 22*eabba580SStefan Hajnoczi 23*eabba580SStefan Hajnoczi /* Wait for the request to finish */ 24*eabba580SStefan Hajnoczi acb->finished = &finished; 25*eabba580SStefan Hajnoczi while (!finished) { 26*eabba580SStefan Hajnoczi qemu_aio_wait(); 27*eabba580SStefan Hajnoczi } 28*eabba580SStefan Hajnoczi } 29*eabba580SStefan Hajnoczi 30*eabba580SStefan Hajnoczi static AIOPool qed_aio_pool = { 31*eabba580SStefan Hajnoczi .aiocb_size = sizeof(QEDAIOCB), 32*eabba580SStefan Hajnoczi .cancel = qed_aio_cancel, 33*eabba580SStefan Hajnoczi }; 34*eabba580SStefan Hajnoczi 3575411d23SStefan Hajnoczi static int bdrv_qed_probe(const uint8_t *buf, int buf_size, 3675411d23SStefan Hajnoczi const char *filename) 3775411d23SStefan Hajnoczi { 3875411d23SStefan Hajnoczi const QEDHeader *header = (const QEDHeader *)buf; 3975411d23SStefan Hajnoczi 4075411d23SStefan Hajnoczi if (buf_size < sizeof(*header)) { 4175411d23SStefan Hajnoczi return 0; 4275411d23SStefan Hajnoczi } 4375411d23SStefan Hajnoczi if (le32_to_cpu(header->magic) != QED_MAGIC) { 4475411d23SStefan Hajnoczi return 0; 4575411d23SStefan Hajnoczi } 4675411d23SStefan Hajnoczi return 100; 4775411d23SStefan Hajnoczi } 4875411d23SStefan Hajnoczi 4975411d23SStefan Hajnoczi /** 5075411d23SStefan Hajnoczi * Check whether an image format is raw 5175411d23SStefan Hajnoczi * 5275411d23SStefan Hajnoczi * @fmt: Backing file format, may be NULL 5375411d23SStefan Hajnoczi */ 5475411d23SStefan Hajnoczi static bool qed_fmt_is_raw(const char *fmt) 5575411d23SStefan Hajnoczi { 5675411d23SStefan Hajnoczi return fmt && strcmp(fmt, "raw") == 0; 5775411d23SStefan Hajnoczi } 5875411d23SStefan Hajnoczi 5975411d23SStefan Hajnoczi static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) 6075411d23SStefan Hajnoczi { 6175411d23SStefan Hajnoczi cpu->magic = le32_to_cpu(le->magic); 6275411d23SStefan Hajnoczi cpu->cluster_size = le32_to_cpu(le->cluster_size); 6375411d23SStefan Hajnoczi cpu->table_size = le32_to_cpu(le->table_size); 6475411d23SStefan Hajnoczi cpu->header_size = le32_to_cpu(le->header_size); 6575411d23SStefan Hajnoczi cpu->features = le64_to_cpu(le->features); 6675411d23SStefan Hajnoczi cpu->compat_features = le64_to_cpu(le->compat_features); 6775411d23SStefan Hajnoczi cpu->autoclear_features = le64_to_cpu(le->autoclear_features); 6875411d23SStefan Hajnoczi cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); 6975411d23SStefan Hajnoczi cpu->image_size = le64_to_cpu(le->image_size); 7075411d23SStefan Hajnoczi cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); 7175411d23SStefan Hajnoczi cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); 7275411d23SStefan Hajnoczi } 7375411d23SStefan Hajnoczi 7475411d23SStefan Hajnoczi static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) 7575411d23SStefan Hajnoczi { 7675411d23SStefan Hajnoczi le->magic = cpu_to_le32(cpu->magic); 7775411d23SStefan Hajnoczi le->cluster_size = cpu_to_le32(cpu->cluster_size); 7875411d23SStefan Hajnoczi le->table_size = cpu_to_le32(cpu->table_size); 7975411d23SStefan Hajnoczi le->header_size = cpu_to_le32(cpu->header_size); 8075411d23SStefan Hajnoczi le->features = cpu_to_le64(cpu->features); 8175411d23SStefan Hajnoczi le->compat_features = cpu_to_le64(cpu->compat_features); 8275411d23SStefan Hajnoczi le->autoclear_features = cpu_to_le64(cpu->autoclear_features); 8375411d23SStefan Hajnoczi le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); 8475411d23SStefan Hajnoczi le->image_size = cpu_to_le64(cpu->image_size); 8575411d23SStefan Hajnoczi le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); 8675411d23SStefan Hajnoczi le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); 8775411d23SStefan Hajnoczi } 8875411d23SStefan Hajnoczi 8975411d23SStefan Hajnoczi static int qed_write_header_sync(BDRVQEDState *s) 9075411d23SStefan Hajnoczi { 9175411d23SStefan Hajnoczi QEDHeader le; 9275411d23SStefan Hajnoczi int ret; 9375411d23SStefan Hajnoczi 9475411d23SStefan Hajnoczi qed_header_cpu_to_le(&s->header, &le); 9575411d23SStefan Hajnoczi ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le)); 9675411d23SStefan Hajnoczi if (ret != sizeof(le)) { 9775411d23SStefan Hajnoczi return ret; 9875411d23SStefan Hajnoczi } 9975411d23SStefan Hajnoczi return 0; 10075411d23SStefan Hajnoczi } 10175411d23SStefan Hajnoczi 10275411d23SStefan Hajnoczi static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 10375411d23SStefan Hajnoczi { 10475411d23SStefan Hajnoczi uint64_t table_entries; 10575411d23SStefan Hajnoczi uint64_t l2_size; 10675411d23SStefan Hajnoczi 10775411d23SStefan Hajnoczi table_entries = (table_size * cluster_size) / sizeof(uint64_t); 10875411d23SStefan Hajnoczi l2_size = table_entries * cluster_size; 10975411d23SStefan Hajnoczi 11075411d23SStefan Hajnoczi return l2_size * table_entries; 11175411d23SStefan Hajnoczi } 11275411d23SStefan Hajnoczi 11375411d23SStefan Hajnoczi static bool qed_is_cluster_size_valid(uint32_t cluster_size) 11475411d23SStefan Hajnoczi { 11575411d23SStefan Hajnoczi if (cluster_size < QED_MIN_CLUSTER_SIZE || 11675411d23SStefan Hajnoczi cluster_size > QED_MAX_CLUSTER_SIZE) { 11775411d23SStefan Hajnoczi return false; 11875411d23SStefan Hajnoczi } 11975411d23SStefan Hajnoczi if (cluster_size & (cluster_size - 1)) { 12075411d23SStefan Hajnoczi return false; /* not power of 2 */ 12175411d23SStefan Hajnoczi } 12275411d23SStefan Hajnoczi return true; 12375411d23SStefan Hajnoczi } 12475411d23SStefan Hajnoczi 12575411d23SStefan Hajnoczi static bool qed_is_table_size_valid(uint32_t table_size) 12675411d23SStefan Hajnoczi { 12775411d23SStefan Hajnoczi if (table_size < QED_MIN_TABLE_SIZE || 12875411d23SStefan Hajnoczi table_size > QED_MAX_TABLE_SIZE) { 12975411d23SStefan Hajnoczi return false; 13075411d23SStefan Hajnoczi } 13175411d23SStefan Hajnoczi if (table_size & (table_size - 1)) { 13275411d23SStefan Hajnoczi return false; /* not power of 2 */ 13375411d23SStefan Hajnoczi } 13475411d23SStefan Hajnoczi return true; 13575411d23SStefan Hajnoczi } 13675411d23SStefan Hajnoczi 13775411d23SStefan Hajnoczi static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 13875411d23SStefan Hajnoczi uint32_t table_size) 13975411d23SStefan Hajnoczi { 14075411d23SStefan Hajnoczi if (image_size % BDRV_SECTOR_SIZE != 0) { 14175411d23SStefan Hajnoczi return false; /* not multiple of sector size */ 14275411d23SStefan Hajnoczi } 14375411d23SStefan Hajnoczi if (image_size > qed_max_image_size(cluster_size, table_size)) { 14475411d23SStefan Hajnoczi return false; /* image is too large */ 14575411d23SStefan Hajnoczi } 14675411d23SStefan Hajnoczi return true; 14775411d23SStefan Hajnoczi } 14875411d23SStefan Hajnoczi 14975411d23SStefan Hajnoczi /** 15075411d23SStefan Hajnoczi * Read a string of known length from the image file 15175411d23SStefan Hajnoczi * 15275411d23SStefan Hajnoczi * @file: Image file 15375411d23SStefan Hajnoczi * @offset: File offset to start of string, in bytes 15475411d23SStefan Hajnoczi * @n: String length in bytes 15575411d23SStefan Hajnoczi * @buf: Destination buffer 15675411d23SStefan Hajnoczi * @buflen: Destination buffer length in bytes 15775411d23SStefan Hajnoczi * @ret: 0 on success, -errno on failure 15875411d23SStefan Hajnoczi * 15975411d23SStefan Hajnoczi * The string is NUL-terminated. 16075411d23SStefan Hajnoczi */ 16175411d23SStefan Hajnoczi static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n, 16275411d23SStefan Hajnoczi char *buf, size_t buflen) 16375411d23SStefan Hajnoczi { 16475411d23SStefan Hajnoczi int ret; 16575411d23SStefan Hajnoczi if (n >= buflen) { 16675411d23SStefan Hajnoczi return -EINVAL; 16775411d23SStefan Hajnoczi } 16875411d23SStefan Hajnoczi ret = bdrv_pread(file, offset, buf, n); 16975411d23SStefan Hajnoczi if (ret < 0) { 17075411d23SStefan Hajnoczi return ret; 17175411d23SStefan Hajnoczi } 17275411d23SStefan Hajnoczi buf[n] = '\0'; 17375411d23SStefan Hajnoczi return 0; 17475411d23SStefan Hajnoczi } 17575411d23SStefan Hajnoczi 176*eabba580SStefan Hajnoczi /** 177*eabba580SStefan Hajnoczi * Allocate new clusters 178*eabba580SStefan Hajnoczi * 179*eabba580SStefan Hajnoczi * @s: QED state 180*eabba580SStefan Hajnoczi * @n: Number of contiguous clusters to allocate 181*eabba580SStefan Hajnoczi * @ret: Offset of first allocated cluster 182*eabba580SStefan Hajnoczi * 183*eabba580SStefan Hajnoczi * This function only produces the offset where the new clusters should be 184*eabba580SStefan Hajnoczi * written. It updates BDRVQEDState but does not make any changes to the image 185*eabba580SStefan Hajnoczi * file. 186*eabba580SStefan Hajnoczi */ 187*eabba580SStefan Hajnoczi static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 188*eabba580SStefan Hajnoczi { 189*eabba580SStefan Hajnoczi uint64_t offset = s->file_size; 190*eabba580SStefan Hajnoczi s->file_size += n * s->header.cluster_size; 191*eabba580SStefan Hajnoczi return offset; 192*eabba580SStefan Hajnoczi } 193*eabba580SStefan Hajnoczi 194298800caSStefan Hajnoczi QEDTable *qed_alloc_table(BDRVQEDState *s) 195298800caSStefan Hajnoczi { 196298800caSStefan Hajnoczi /* Honor O_DIRECT memory alignment requirements */ 197298800caSStefan Hajnoczi return qemu_blockalign(s->bs, 198298800caSStefan Hajnoczi s->header.cluster_size * s->header.table_size); 199298800caSStefan Hajnoczi } 200298800caSStefan Hajnoczi 201*eabba580SStefan Hajnoczi /** 202*eabba580SStefan Hajnoczi * Allocate a new zeroed L2 table 203*eabba580SStefan Hajnoczi */ 204*eabba580SStefan Hajnoczi static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 205*eabba580SStefan Hajnoczi { 206*eabba580SStefan Hajnoczi CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 207*eabba580SStefan Hajnoczi 208*eabba580SStefan Hajnoczi l2_table->table = qed_alloc_table(s); 209*eabba580SStefan Hajnoczi l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 210*eabba580SStefan Hajnoczi 211*eabba580SStefan Hajnoczi memset(l2_table->table->offsets, 0, 212*eabba580SStefan Hajnoczi s->header.cluster_size * s->header.table_size); 213*eabba580SStefan Hajnoczi return l2_table; 214*eabba580SStefan Hajnoczi } 215*eabba580SStefan Hajnoczi 216*eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret); 217*eabba580SStefan Hajnoczi 21875411d23SStefan Hajnoczi static int bdrv_qed_open(BlockDriverState *bs, int flags) 21975411d23SStefan Hajnoczi { 22075411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 22175411d23SStefan Hajnoczi QEDHeader le_header; 22275411d23SStefan Hajnoczi int64_t file_size; 22375411d23SStefan Hajnoczi int ret; 22475411d23SStefan Hajnoczi 22575411d23SStefan Hajnoczi s->bs = bs; 226*eabba580SStefan Hajnoczi QSIMPLEQ_INIT(&s->allocating_write_reqs); 22775411d23SStefan Hajnoczi 22875411d23SStefan Hajnoczi ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); 22975411d23SStefan Hajnoczi if (ret < 0) { 23075411d23SStefan Hajnoczi return ret; 23175411d23SStefan Hajnoczi } 23275411d23SStefan Hajnoczi ret = 0; /* ret should always be 0 or -errno */ 23375411d23SStefan Hajnoczi qed_header_le_to_cpu(&le_header, &s->header); 23475411d23SStefan Hajnoczi 23575411d23SStefan Hajnoczi if (s->header.magic != QED_MAGIC) { 23675411d23SStefan Hajnoczi return -EINVAL; 23775411d23SStefan Hajnoczi } 23875411d23SStefan Hajnoczi if (s->header.features & ~QED_FEATURE_MASK) { 23975411d23SStefan Hajnoczi return -ENOTSUP; /* image uses unsupported feature bits */ 24075411d23SStefan Hajnoczi } 24175411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 24275411d23SStefan Hajnoczi return -EINVAL; 24375411d23SStefan Hajnoczi } 24475411d23SStefan Hajnoczi 24575411d23SStefan Hajnoczi /* Round down file size to the last cluster */ 24675411d23SStefan Hajnoczi file_size = bdrv_getlength(bs->file); 24775411d23SStefan Hajnoczi if (file_size < 0) { 24875411d23SStefan Hajnoczi return file_size; 24975411d23SStefan Hajnoczi } 25075411d23SStefan Hajnoczi s->file_size = qed_start_of_cluster(s, file_size); 25175411d23SStefan Hajnoczi 25275411d23SStefan Hajnoczi if (!qed_is_table_size_valid(s->header.table_size)) { 25375411d23SStefan Hajnoczi return -EINVAL; 25475411d23SStefan Hajnoczi } 25575411d23SStefan Hajnoczi if (!qed_is_image_size_valid(s->header.image_size, 25675411d23SStefan Hajnoczi s->header.cluster_size, 25775411d23SStefan Hajnoczi s->header.table_size)) { 25875411d23SStefan Hajnoczi return -EINVAL; 25975411d23SStefan Hajnoczi } 26075411d23SStefan Hajnoczi if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 26175411d23SStefan Hajnoczi return -EINVAL; 26275411d23SStefan Hajnoczi } 26375411d23SStefan Hajnoczi 26475411d23SStefan Hajnoczi s->table_nelems = (s->header.cluster_size * s->header.table_size) / 26575411d23SStefan Hajnoczi sizeof(uint64_t); 26675411d23SStefan Hajnoczi s->l2_shift = ffs(s->header.cluster_size) - 1; 26775411d23SStefan Hajnoczi s->l2_mask = s->table_nelems - 1; 26875411d23SStefan Hajnoczi s->l1_shift = s->l2_shift + ffs(s->table_nelems) - 1; 26975411d23SStefan Hajnoczi 27075411d23SStefan Hajnoczi if ((s->header.features & QED_F_BACKING_FILE)) { 27175411d23SStefan Hajnoczi if ((uint64_t)s->header.backing_filename_offset + 27275411d23SStefan Hajnoczi s->header.backing_filename_size > 27375411d23SStefan Hajnoczi s->header.cluster_size * s->header.header_size) { 27475411d23SStefan Hajnoczi return -EINVAL; 27575411d23SStefan Hajnoczi } 27675411d23SStefan Hajnoczi 27775411d23SStefan Hajnoczi ret = qed_read_string(bs->file, s->header.backing_filename_offset, 27875411d23SStefan Hajnoczi s->header.backing_filename_size, bs->backing_file, 27975411d23SStefan Hajnoczi sizeof(bs->backing_file)); 28075411d23SStefan Hajnoczi if (ret < 0) { 28175411d23SStefan Hajnoczi return ret; 28275411d23SStefan Hajnoczi } 28375411d23SStefan Hajnoczi 28475411d23SStefan Hajnoczi if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 28575411d23SStefan Hajnoczi pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 28675411d23SStefan Hajnoczi } 28775411d23SStefan Hajnoczi } 28875411d23SStefan Hajnoczi 28975411d23SStefan Hajnoczi /* Reset unknown autoclear feature bits. This is a backwards 29075411d23SStefan Hajnoczi * compatibility mechanism that allows images to be opened by older 29175411d23SStefan Hajnoczi * programs, which "knock out" unknown feature bits. When an image is 29275411d23SStefan Hajnoczi * opened by a newer program again it can detect that the autoclear 29375411d23SStefan Hajnoczi * feature is no longer valid. 29475411d23SStefan Hajnoczi */ 29575411d23SStefan Hajnoczi if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 29675411d23SStefan Hajnoczi !bdrv_is_read_only(bs->file)) { 29775411d23SStefan Hajnoczi s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 29875411d23SStefan Hajnoczi 29975411d23SStefan Hajnoczi ret = qed_write_header_sync(s); 30075411d23SStefan Hajnoczi if (ret) { 30175411d23SStefan Hajnoczi return ret; 30275411d23SStefan Hajnoczi } 30375411d23SStefan Hajnoczi 30475411d23SStefan Hajnoczi /* From here on only known autoclear feature bits are valid */ 30575411d23SStefan Hajnoczi bdrv_flush(bs->file); 30675411d23SStefan Hajnoczi } 30775411d23SStefan Hajnoczi 308298800caSStefan Hajnoczi s->l1_table = qed_alloc_table(s); 309298800caSStefan Hajnoczi qed_init_l2_cache(&s->l2_cache); 310298800caSStefan Hajnoczi 311298800caSStefan Hajnoczi ret = qed_read_l1_table_sync(s); 312298800caSStefan Hajnoczi if (ret) { 313298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 314298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 315298800caSStefan Hajnoczi } 31675411d23SStefan Hajnoczi return ret; 31775411d23SStefan Hajnoczi } 31875411d23SStefan Hajnoczi 31975411d23SStefan Hajnoczi static void bdrv_qed_close(BlockDriverState *bs) 32075411d23SStefan Hajnoczi { 321298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 322298800caSStefan Hajnoczi 323298800caSStefan Hajnoczi qed_free_l2_cache(&s->l2_cache); 324298800caSStefan Hajnoczi qemu_vfree(s->l1_table); 32575411d23SStefan Hajnoczi } 32675411d23SStefan Hajnoczi 32775411d23SStefan Hajnoczi static int bdrv_qed_flush(BlockDriverState *bs) 32875411d23SStefan Hajnoczi { 32975411d23SStefan Hajnoczi return bdrv_flush(bs->file); 33075411d23SStefan Hajnoczi } 33175411d23SStefan Hajnoczi 33275411d23SStefan Hajnoczi static int qed_create(const char *filename, uint32_t cluster_size, 33375411d23SStefan Hajnoczi uint64_t image_size, uint32_t table_size, 33475411d23SStefan Hajnoczi const char *backing_file, const char *backing_fmt) 33575411d23SStefan Hajnoczi { 33675411d23SStefan Hajnoczi QEDHeader header = { 33775411d23SStefan Hajnoczi .magic = QED_MAGIC, 33875411d23SStefan Hajnoczi .cluster_size = cluster_size, 33975411d23SStefan Hajnoczi .table_size = table_size, 34075411d23SStefan Hajnoczi .header_size = 1, 34175411d23SStefan Hajnoczi .features = 0, 34275411d23SStefan Hajnoczi .compat_features = 0, 34375411d23SStefan Hajnoczi .l1_table_offset = cluster_size, 34475411d23SStefan Hajnoczi .image_size = image_size, 34575411d23SStefan Hajnoczi }; 34675411d23SStefan Hajnoczi QEDHeader le_header; 34775411d23SStefan Hajnoczi uint8_t *l1_table = NULL; 34875411d23SStefan Hajnoczi size_t l1_size = header.cluster_size * header.table_size; 34975411d23SStefan Hajnoczi int ret = 0; 35075411d23SStefan Hajnoczi BlockDriverState *bs = NULL; 35175411d23SStefan Hajnoczi 35275411d23SStefan Hajnoczi ret = bdrv_create_file(filename, NULL); 35375411d23SStefan Hajnoczi if (ret < 0) { 35475411d23SStefan Hajnoczi return ret; 35575411d23SStefan Hajnoczi } 35675411d23SStefan Hajnoczi 35775411d23SStefan Hajnoczi ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR | BDRV_O_CACHE_WB); 35875411d23SStefan Hajnoczi if (ret < 0) { 35975411d23SStefan Hajnoczi return ret; 36075411d23SStefan Hajnoczi } 36175411d23SStefan Hajnoczi 36275411d23SStefan Hajnoczi if (backing_file) { 36375411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FILE; 36475411d23SStefan Hajnoczi header.backing_filename_offset = sizeof(le_header); 36575411d23SStefan Hajnoczi header.backing_filename_size = strlen(backing_file); 36675411d23SStefan Hajnoczi 36775411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 36875411d23SStefan Hajnoczi header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 36975411d23SStefan Hajnoczi } 37075411d23SStefan Hajnoczi } 37175411d23SStefan Hajnoczi 37275411d23SStefan Hajnoczi qed_header_cpu_to_le(&header, &le_header); 37375411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header)); 37475411d23SStefan Hajnoczi if (ret < 0) { 37575411d23SStefan Hajnoczi goto out; 37675411d23SStefan Hajnoczi } 37775411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, sizeof(le_header), backing_file, 37875411d23SStefan Hajnoczi header.backing_filename_size); 37975411d23SStefan Hajnoczi if (ret < 0) { 38075411d23SStefan Hajnoczi goto out; 38175411d23SStefan Hajnoczi } 38275411d23SStefan Hajnoczi 38375411d23SStefan Hajnoczi l1_table = qemu_mallocz(l1_size); 38475411d23SStefan Hajnoczi ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size); 38575411d23SStefan Hajnoczi if (ret < 0) { 38675411d23SStefan Hajnoczi goto out; 38775411d23SStefan Hajnoczi } 38875411d23SStefan Hajnoczi 38975411d23SStefan Hajnoczi ret = 0; /* success */ 39075411d23SStefan Hajnoczi out: 39175411d23SStefan Hajnoczi qemu_free(l1_table); 39275411d23SStefan Hajnoczi bdrv_delete(bs); 39375411d23SStefan Hajnoczi return ret; 39475411d23SStefan Hajnoczi } 39575411d23SStefan Hajnoczi 39675411d23SStefan Hajnoczi static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options) 39775411d23SStefan Hajnoczi { 39875411d23SStefan Hajnoczi uint64_t image_size = 0; 39975411d23SStefan Hajnoczi uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 40075411d23SStefan Hajnoczi uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 40175411d23SStefan Hajnoczi const char *backing_file = NULL; 40275411d23SStefan Hajnoczi const char *backing_fmt = NULL; 40375411d23SStefan Hajnoczi 40475411d23SStefan Hajnoczi while (options && options->name) { 40575411d23SStefan Hajnoczi if (!strcmp(options->name, BLOCK_OPT_SIZE)) { 40675411d23SStefan Hajnoczi image_size = options->value.n; 40775411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { 40875411d23SStefan Hajnoczi backing_file = options->value.s; 40975411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { 41075411d23SStefan Hajnoczi backing_fmt = options->value.s; 41175411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { 41275411d23SStefan Hajnoczi if (options->value.n) { 41375411d23SStefan Hajnoczi cluster_size = options->value.n; 41475411d23SStefan Hajnoczi } 41575411d23SStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_TABLE_SIZE)) { 41675411d23SStefan Hajnoczi if (options->value.n) { 41775411d23SStefan Hajnoczi table_size = options->value.n; 41875411d23SStefan Hajnoczi } 41975411d23SStefan Hajnoczi } 42075411d23SStefan Hajnoczi options++; 42175411d23SStefan Hajnoczi } 42275411d23SStefan Hajnoczi 42375411d23SStefan Hajnoczi if (!qed_is_cluster_size_valid(cluster_size)) { 42475411d23SStefan Hajnoczi fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n", 42575411d23SStefan Hajnoczi QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 42675411d23SStefan Hajnoczi return -EINVAL; 42775411d23SStefan Hajnoczi } 42875411d23SStefan Hajnoczi if (!qed_is_table_size_valid(table_size)) { 42975411d23SStefan Hajnoczi fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n", 43075411d23SStefan Hajnoczi QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 43175411d23SStefan Hajnoczi return -EINVAL; 43275411d23SStefan Hajnoczi } 43375411d23SStefan Hajnoczi if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 43475411d23SStefan Hajnoczi fprintf(stderr, "QED image size must be a non-zero multiple of " 43575411d23SStefan Hajnoczi "cluster size and less than %" PRIu64 " bytes\n", 43675411d23SStefan Hajnoczi qed_max_image_size(cluster_size, table_size)); 43775411d23SStefan Hajnoczi return -EINVAL; 43875411d23SStefan Hajnoczi } 43975411d23SStefan Hajnoczi 44075411d23SStefan Hajnoczi return qed_create(filename, cluster_size, image_size, table_size, 44175411d23SStefan Hajnoczi backing_file, backing_fmt); 44275411d23SStefan Hajnoczi } 44375411d23SStefan Hajnoczi 444298800caSStefan Hajnoczi typedef struct { 445298800caSStefan Hajnoczi int is_allocated; 446298800caSStefan Hajnoczi int *pnum; 447298800caSStefan Hajnoczi } QEDIsAllocatedCB; 448298800caSStefan Hajnoczi 449298800caSStefan Hajnoczi static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 450298800caSStefan Hajnoczi { 451298800caSStefan Hajnoczi QEDIsAllocatedCB *cb = opaque; 452298800caSStefan Hajnoczi *cb->pnum = len / BDRV_SECTOR_SIZE; 453298800caSStefan Hajnoczi cb->is_allocated = ret == QED_CLUSTER_FOUND; 454298800caSStefan Hajnoczi } 455298800caSStefan Hajnoczi 45675411d23SStefan Hajnoczi static int bdrv_qed_is_allocated(BlockDriverState *bs, int64_t sector_num, 45775411d23SStefan Hajnoczi int nb_sectors, int *pnum) 45875411d23SStefan Hajnoczi { 459298800caSStefan Hajnoczi BDRVQEDState *s = bs->opaque; 460298800caSStefan Hajnoczi uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 461298800caSStefan Hajnoczi size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 462298800caSStefan Hajnoczi QEDIsAllocatedCB cb = { 463298800caSStefan Hajnoczi .is_allocated = -1, 464298800caSStefan Hajnoczi .pnum = pnum, 465298800caSStefan Hajnoczi }; 466298800caSStefan Hajnoczi QEDRequest request = { .l2_table = NULL }; 467298800caSStefan Hajnoczi 468298800caSStefan Hajnoczi async_context_push(); 469298800caSStefan Hajnoczi 470298800caSStefan Hajnoczi qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb); 471298800caSStefan Hajnoczi 472298800caSStefan Hajnoczi while (cb.is_allocated == -1) { 473298800caSStefan Hajnoczi qemu_aio_wait(); 474298800caSStefan Hajnoczi } 475298800caSStefan Hajnoczi 476298800caSStefan Hajnoczi async_context_pop(); 477298800caSStefan Hajnoczi 478298800caSStefan Hajnoczi qed_unref_l2_cache_entry(request.l2_table); 479298800caSStefan Hajnoczi 480298800caSStefan Hajnoczi return cb.is_allocated; 48175411d23SStefan Hajnoczi } 48275411d23SStefan Hajnoczi 48375411d23SStefan Hajnoczi static int bdrv_qed_make_empty(BlockDriverState *bs) 48475411d23SStefan Hajnoczi { 48575411d23SStefan Hajnoczi return -ENOTSUP; 48675411d23SStefan Hajnoczi } 48775411d23SStefan Hajnoczi 488*eabba580SStefan Hajnoczi static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 489*eabba580SStefan Hajnoczi { 490*eabba580SStefan Hajnoczi return acb->common.bs->opaque; 491*eabba580SStefan Hajnoczi } 492*eabba580SStefan Hajnoczi 493*eabba580SStefan Hajnoczi /** 494*eabba580SStefan Hajnoczi * Read from the backing file or zero-fill if no backing file 495*eabba580SStefan Hajnoczi * 496*eabba580SStefan Hajnoczi * @s: QED state 497*eabba580SStefan Hajnoczi * @pos: Byte position in device 498*eabba580SStefan Hajnoczi * @qiov: Destination I/O vector 499*eabba580SStefan Hajnoczi * @cb: Completion function 500*eabba580SStefan Hajnoczi * @opaque: User data for completion function 501*eabba580SStefan Hajnoczi * 502*eabba580SStefan Hajnoczi * This function reads qiov->size bytes starting at pos from the backing file. 503*eabba580SStefan Hajnoczi * If there is no backing file then zeroes are read. 504*eabba580SStefan Hajnoczi */ 505*eabba580SStefan Hajnoczi static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 506*eabba580SStefan Hajnoczi QEMUIOVector *qiov, 507*eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, void *opaque) 508*eabba580SStefan Hajnoczi { 509*eabba580SStefan Hajnoczi BlockDriverAIOCB *aiocb; 510*eabba580SStefan Hajnoczi uint64_t backing_length = 0; 511*eabba580SStefan Hajnoczi size_t size; 512*eabba580SStefan Hajnoczi 513*eabba580SStefan Hajnoczi /* If there is a backing file, get its length. Treat the absence of a 514*eabba580SStefan Hajnoczi * backing file like a zero length backing file. 515*eabba580SStefan Hajnoczi */ 516*eabba580SStefan Hajnoczi if (s->bs->backing_hd) { 517*eabba580SStefan Hajnoczi int64_t l = bdrv_getlength(s->bs->backing_hd); 518*eabba580SStefan Hajnoczi if (l < 0) { 519*eabba580SStefan Hajnoczi cb(opaque, l); 520*eabba580SStefan Hajnoczi return; 521*eabba580SStefan Hajnoczi } 522*eabba580SStefan Hajnoczi backing_length = l; 523*eabba580SStefan Hajnoczi } 524*eabba580SStefan Hajnoczi 525*eabba580SStefan Hajnoczi /* Zero all sectors if reading beyond the end of the backing file */ 526*eabba580SStefan Hajnoczi if (pos >= backing_length || 527*eabba580SStefan Hajnoczi pos + qiov->size > backing_length) { 528*eabba580SStefan Hajnoczi qemu_iovec_memset(qiov, 0, qiov->size); 529*eabba580SStefan Hajnoczi } 530*eabba580SStefan Hajnoczi 531*eabba580SStefan Hajnoczi /* Complete now if there are no backing file sectors to read */ 532*eabba580SStefan Hajnoczi if (pos >= backing_length) { 533*eabba580SStefan Hajnoczi cb(opaque, 0); 534*eabba580SStefan Hajnoczi return; 535*eabba580SStefan Hajnoczi } 536*eabba580SStefan Hajnoczi 537*eabba580SStefan Hajnoczi /* If the read straddles the end of the backing file, shorten it */ 538*eabba580SStefan Hajnoczi size = MIN((uint64_t)backing_length - pos, qiov->size); 539*eabba580SStefan Hajnoczi 540*eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING); 541*eabba580SStefan Hajnoczi aiocb = bdrv_aio_readv(s->bs->backing_hd, pos / BDRV_SECTOR_SIZE, 542*eabba580SStefan Hajnoczi qiov, size / BDRV_SECTOR_SIZE, cb, opaque); 543*eabba580SStefan Hajnoczi if (!aiocb) { 544*eabba580SStefan Hajnoczi cb(opaque, -EIO); 545*eabba580SStefan Hajnoczi } 546*eabba580SStefan Hajnoczi } 547*eabba580SStefan Hajnoczi 548*eabba580SStefan Hajnoczi typedef struct { 549*eabba580SStefan Hajnoczi GenericCB gencb; 550*eabba580SStefan Hajnoczi BDRVQEDState *s; 551*eabba580SStefan Hajnoczi QEMUIOVector qiov; 552*eabba580SStefan Hajnoczi struct iovec iov; 553*eabba580SStefan Hajnoczi uint64_t offset; 554*eabba580SStefan Hajnoczi } CopyFromBackingFileCB; 555*eabba580SStefan Hajnoczi 556*eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_cb(void *opaque, int ret) 557*eabba580SStefan Hajnoczi { 558*eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 559*eabba580SStefan Hajnoczi qemu_vfree(copy_cb->iov.iov_base); 560*eabba580SStefan Hajnoczi gencb_complete(©_cb->gencb, ret); 561*eabba580SStefan Hajnoczi } 562*eabba580SStefan Hajnoczi 563*eabba580SStefan Hajnoczi static void qed_copy_from_backing_file_write(void *opaque, int ret) 564*eabba580SStefan Hajnoczi { 565*eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb = opaque; 566*eabba580SStefan Hajnoczi BDRVQEDState *s = copy_cb->s; 567*eabba580SStefan Hajnoczi BlockDriverAIOCB *aiocb; 568*eabba580SStefan Hajnoczi 569*eabba580SStefan Hajnoczi if (ret) { 570*eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb(copy_cb, ret); 571*eabba580SStefan Hajnoczi return; 572*eabba580SStefan Hajnoczi } 573*eabba580SStefan Hajnoczi 574*eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 575*eabba580SStefan Hajnoczi aiocb = bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE, 576*eabba580SStefan Hajnoczi ©_cb->qiov, 577*eabba580SStefan Hajnoczi copy_cb->qiov.size / BDRV_SECTOR_SIZE, 578*eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb, copy_cb); 579*eabba580SStefan Hajnoczi if (!aiocb) { 580*eabba580SStefan Hajnoczi qed_copy_from_backing_file_cb(copy_cb, -EIO); 581*eabba580SStefan Hajnoczi } 582*eabba580SStefan Hajnoczi } 583*eabba580SStefan Hajnoczi 584*eabba580SStefan Hajnoczi /** 585*eabba580SStefan Hajnoczi * Copy data from backing file into the image 586*eabba580SStefan Hajnoczi * 587*eabba580SStefan Hajnoczi * @s: QED state 588*eabba580SStefan Hajnoczi * @pos: Byte position in device 589*eabba580SStefan Hajnoczi * @len: Number of bytes 590*eabba580SStefan Hajnoczi * @offset: Byte offset in image file 591*eabba580SStefan Hajnoczi * @cb: Completion function 592*eabba580SStefan Hajnoczi * @opaque: User data for completion function 593*eabba580SStefan Hajnoczi */ 594*eabba580SStefan Hajnoczi static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, 595*eabba580SStefan Hajnoczi uint64_t len, uint64_t offset, 596*eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, 597*eabba580SStefan Hajnoczi void *opaque) 598*eabba580SStefan Hajnoczi { 599*eabba580SStefan Hajnoczi CopyFromBackingFileCB *copy_cb; 600*eabba580SStefan Hajnoczi 601*eabba580SStefan Hajnoczi /* Skip copy entirely if there is no work to do */ 602*eabba580SStefan Hajnoczi if (len == 0) { 603*eabba580SStefan Hajnoczi cb(opaque, 0); 604*eabba580SStefan Hajnoczi return; 605*eabba580SStefan Hajnoczi } 606*eabba580SStefan Hajnoczi 607*eabba580SStefan Hajnoczi copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque); 608*eabba580SStefan Hajnoczi copy_cb->s = s; 609*eabba580SStefan Hajnoczi copy_cb->offset = offset; 610*eabba580SStefan Hajnoczi copy_cb->iov.iov_base = qemu_blockalign(s->bs, len); 611*eabba580SStefan Hajnoczi copy_cb->iov.iov_len = len; 612*eabba580SStefan Hajnoczi qemu_iovec_init_external(©_cb->qiov, ©_cb->iov, 1); 613*eabba580SStefan Hajnoczi 614*eabba580SStefan Hajnoczi qed_read_backing_file(s, pos, ©_cb->qiov, 615*eabba580SStefan Hajnoczi qed_copy_from_backing_file_write, copy_cb); 616*eabba580SStefan Hajnoczi } 617*eabba580SStefan Hajnoczi 618*eabba580SStefan Hajnoczi /** 619*eabba580SStefan Hajnoczi * Link one or more contiguous clusters into a table 620*eabba580SStefan Hajnoczi * 621*eabba580SStefan Hajnoczi * @s: QED state 622*eabba580SStefan Hajnoczi * @table: L2 table 623*eabba580SStefan Hajnoczi * @index: First cluster index 624*eabba580SStefan Hajnoczi * @n: Number of contiguous clusters 625*eabba580SStefan Hajnoczi * @cluster: First cluster byte offset in image file 626*eabba580SStefan Hajnoczi */ 627*eabba580SStefan Hajnoczi static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index, 628*eabba580SStefan Hajnoczi unsigned int n, uint64_t cluster) 629*eabba580SStefan Hajnoczi { 630*eabba580SStefan Hajnoczi int i; 631*eabba580SStefan Hajnoczi for (i = index; i < index + n; i++) { 632*eabba580SStefan Hajnoczi table->offsets[i] = cluster; 633*eabba580SStefan Hajnoczi cluster += s->header.cluster_size; 634*eabba580SStefan Hajnoczi } 635*eabba580SStefan Hajnoczi } 636*eabba580SStefan Hajnoczi 637*eabba580SStefan Hajnoczi static void qed_aio_complete_bh(void *opaque) 638*eabba580SStefan Hajnoczi { 639*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 640*eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb = acb->common.cb; 641*eabba580SStefan Hajnoczi void *user_opaque = acb->common.opaque; 642*eabba580SStefan Hajnoczi int ret = acb->bh_ret; 643*eabba580SStefan Hajnoczi bool *finished = acb->finished; 644*eabba580SStefan Hajnoczi 645*eabba580SStefan Hajnoczi qemu_bh_delete(acb->bh); 646*eabba580SStefan Hajnoczi qemu_aio_release(acb); 647*eabba580SStefan Hajnoczi 648*eabba580SStefan Hajnoczi /* Invoke callback */ 649*eabba580SStefan Hajnoczi cb(user_opaque, ret); 650*eabba580SStefan Hajnoczi 651*eabba580SStefan Hajnoczi /* Signal cancel completion */ 652*eabba580SStefan Hajnoczi if (finished) { 653*eabba580SStefan Hajnoczi *finished = true; 654*eabba580SStefan Hajnoczi } 655*eabba580SStefan Hajnoczi } 656*eabba580SStefan Hajnoczi 657*eabba580SStefan Hajnoczi static void qed_aio_complete(QEDAIOCB *acb, int ret) 658*eabba580SStefan Hajnoczi { 659*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 660*eabba580SStefan Hajnoczi 661*eabba580SStefan Hajnoczi trace_qed_aio_complete(s, acb, ret); 662*eabba580SStefan Hajnoczi 663*eabba580SStefan Hajnoczi /* Free resources */ 664*eabba580SStefan Hajnoczi qemu_iovec_destroy(&acb->cur_qiov); 665*eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 666*eabba580SStefan Hajnoczi 667*eabba580SStefan Hajnoczi /* Arrange for a bh to invoke the completion function */ 668*eabba580SStefan Hajnoczi acb->bh_ret = ret; 669*eabba580SStefan Hajnoczi acb->bh = qemu_bh_new(qed_aio_complete_bh, acb); 670*eabba580SStefan Hajnoczi qemu_bh_schedule(acb->bh); 671*eabba580SStefan Hajnoczi 672*eabba580SStefan Hajnoczi /* Start next allocating write request waiting behind this one. Note that 673*eabba580SStefan Hajnoczi * requests enqueue themselves when they first hit an unallocated cluster 674*eabba580SStefan Hajnoczi * but they wait until the entire request is finished before waking up the 675*eabba580SStefan Hajnoczi * next request in the queue. This ensures that we don't cycle through 676*eabba580SStefan Hajnoczi * requests multiple times but rather finish one at a time completely. 677*eabba580SStefan Hajnoczi */ 678*eabba580SStefan Hajnoczi if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 679*eabba580SStefan Hajnoczi QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next); 680*eabba580SStefan Hajnoczi acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); 681*eabba580SStefan Hajnoczi if (acb) { 682*eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 683*eabba580SStefan Hajnoczi } 684*eabba580SStefan Hajnoczi } 685*eabba580SStefan Hajnoczi } 686*eabba580SStefan Hajnoczi 687*eabba580SStefan Hajnoczi /** 688*eabba580SStefan Hajnoczi * Commit the current L2 table to the cache 689*eabba580SStefan Hajnoczi */ 690*eabba580SStefan Hajnoczi static void qed_commit_l2_update(void *opaque, int ret) 691*eabba580SStefan Hajnoczi { 692*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 693*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 694*eabba580SStefan Hajnoczi CachedL2Table *l2_table = acb->request.l2_table; 695*eabba580SStefan Hajnoczi 696*eabba580SStefan Hajnoczi qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 697*eabba580SStefan Hajnoczi 698*eabba580SStefan Hajnoczi /* This is guaranteed to succeed because we just committed the entry to the 699*eabba580SStefan Hajnoczi * cache. 700*eabba580SStefan Hajnoczi */ 701*eabba580SStefan Hajnoczi acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, 702*eabba580SStefan Hajnoczi l2_table->offset); 703*eabba580SStefan Hajnoczi assert(acb->request.l2_table != NULL); 704*eabba580SStefan Hajnoczi 705*eabba580SStefan Hajnoczi qed_aio_next_io(opaque, ret); 706*eabba580SStefan Hajnoczi } 707*eabba580SStefan Hajnoczi 708*eabba580SStefan Hajnoczi /** 709*eabba580SStefan Hajnoczi * Update L1 table with new L2 table offset and write it out 710*eabba580SStefan Hajnoczi */ 711*eabba580SStefan Hajnoczi static void qed_aio_write_l1_update(void *opaque, int ret) 712*eabba580SStefan Hajnoczi { 713*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 714*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 715*eabba580SStefan Hajnoczi int index; 716*eabba580SStefan Hajnoczi 717*eabba580SStefan Hajnoczi if (ret) { 718*eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 719*eabba580SStefan Hajnoczi return; 720*eabba580SStefan Hajnoczi } 721*eabba580SStefan Hajnoczi 722*eabba580SStefan Hajnoczi index = qed_l1_index(s, acb->cur_pos); 723*eabba580SStefan Hajnoczi s->l1_table->offsets[index] = acb->request.l2_table->offset; 724*eabba580SStefan Hajnoczi 725*eabba580SStefan Hajnoczi qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb); 726*eabba580SStefan Hajnoczi } 727*eabba580SStefan Hajnoczi 728*eabba580SStefan Hajnoczi /** 729*eabba580SStefan Hajnoczi * Update L2 table with new cluster offsets and write them out 730*eabba580SStefan Hajnoczi */ 731*eabba580SStefan Hajnoczi static void qed_aio_write_l2_update(void *opaque, int ret) 732*eabba580SStefan Hajnoczi { 733*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 734*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 735*eabba580SStefan Hajnoczi bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 736*eabba580SStefan Hajnoczi int index; 737*eabba580SStefan Hajnoczi 738*eabba580SStefan Hajnoczi if (ret) { 739*eabba580SStefan Hajnoczi goto err; 740*eabba580SStefan Hajnoczi } 741*eabba580SStefan Hajnoczi 742*eabba580SStefan Hajnoczi if (need_alloc) { 743*eabba580SStefan Hajnoczi qed_unref_l2_cache_entry(acb->request.l2_table); 744*eabba580SStefan Hajnoczi acb->request.l2_table = qed_new_l2_table(s); 745*eabba580SStefan Hajnoczi } 746*eabba580SStefan Hajnoczi 747*eabba580SStefan Hajnoczi index = qed_l2_index(s, acb->cur_pos); 748*eabba580SStefan Hajnoczi qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 749*eabba580SStefan Hajnoczi acb->cur_cluster); 750*eabba580SStefan Hajnoczi 751*eabba580SStefan Hajnoczi if (need_alloc) { 752*eabba580SStefan Hajnoczi /* Write out the whole new L2 table */ 753*eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true, 754*eabba580SStefan Hajnoczi qed_aio_write_l1_update, acb); 755*eabba580SStefan Hajnoczi } else { 756*eabba580SStefan Hajnoczi /* Write out only the updated part of the L2 table */ 757*eabba580SStefan Hajnoczi qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false, 758*eabba580SStefan Hajnoczi qed_aio_next_io, acb); 759*eabba580SStefan Hajnoczi } 760*eabba580SStefan Hajnoczi return; 761*eabba580SStefan Hajnoczi 762*eabba580SStefan Hajnoczi err: 763*eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 764*eabba580SStefan Hajnoczi } 765*eabba580SStefan Hajnoczi 766*eabba580SStefan Hajnoczi /** 767*eabba580SStefan Hajnoczi * Flush new data clusters before updating the L2 table 768*eabba580SStefan Hajnoczi * 769*eabba580SStefan Hajnoczi * This flush is necessary when a backing file is in use. A crash during an 770*eabba580SStefan Hajnoczi * allocating write could result in empty clusters in the image. If the write 771*eabba580SStefan Hajnoczi * only touched a subregion of the cluster, then backing image sectors have 772*eabba580SStefan Hajnoczi * been lost in the untouched region. The solution is to flush after writing a 773*eabba580SStefan Hajnoczi * new data cluster and before updating the L2 table. 774*eabba580SStefan Hajnoczi */ 775*eabba580SStefan Hajnoczi static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) 776*eabba580SStefan Hajnoczi { 777*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 778*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 779*eabba580SStefan Hajnoczi 780*eabba580SStefan Hajnoczi if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update, opaque)) { 781*eabba580SStefan Hajnoczi qed_aio_complete(acb, -EIO); 782*eabba580SStefan Hajnoczi } 783*eabba580SStefan Hajnoczi } 784*eabba580SStefan Hajnoczi 785*eabba580SStefan Hajnoczi /** 786*eabba580SStefan Hajnoczi * Write data to the image file 787*eabba580SStefan Hajnoczi */ 788*eabba580SStefan Hajnoczi static void qed_aio_write_main(void *opaque, int ret) 789*eabba580SStefan Hajnoczi { 790*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 791*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 792*eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 793*eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos); 794*eabba580SStefan Hajnoczi BlockDriverCompletionFunc *next_fn; 795*eabba580SStefan Hajnoczi BlockDriverAIOCB *file_acb; 796*eabba580SStefan Hajnoczi 797*eabba580SStefan Hajnoczi trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size); 798*eabba580SStefan Hajnoczi 799*eabba580SStefan Hajnoczi if (ret) { 800*eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 801*eabba580SStefan Hajnoczi return; 802*eabba580SStefan Hajnoczi } 803*eabba580SStefan Hajnoczi 804*eabba580SStefan Hajnoczi if (acb->find_cluster_ret == QED_CLUSTER_FOUND) { 805*eabba580SStefan Hajnoczi next_fn = qed_aio_next_io; 806*eabba580SStefan Hajnoczi } else { 807*eabba580SStefan Hajnoczi if (s->bs->backing_hd) { 808*eabba580SStefan Hajnoczi next_fn = qed_aio_write_flush_before_l2_update; 809*eabba580SStefan Hajnoczi } else { 810*eabba580SStefan Hajnoczi next_fn = qed_aio_write_l2_update; 811*eabba580SStefan Hajnoczi } 812*eabba580SStefan Hajnoczi } 813*eabba580SStefan Hajnoczi 814*eabba580SStefan Hajnoczi BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 815*eabba580SStefan Hajnoczi file_acb = bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE, 816*eabba580SStefan Hajnoczi &acb->cur_qiov, 817*eabba580SStefan Hajnoczi acb->cur_qiov.size / BDRV_SECTOR_SIZE, 818*eabba580SStefan Hajnoczi next_fn, acb); 819*eabba580SStefan Hajnoczi if (!file_acb) { 820*eabba580SStefan Hajnoczi qed_aio_complete(acb, -EIO); 821*eabba580SStefan Hajnoczi } 822*eabba580SStefan Hajnoczi } 823*eabba580SStefan Hajnoczi 824*eabba580SStefan Hajnoczi /** 825*eabba580SStefan Hajnoczi * Populate back untouched region of new data cluster 826*eabba580SStefan Hajnoczi */ 827*eabba580SStefan Hajnoczi static void qed_aio_write_postfill(void *opaque, int ret) 828*eabba580SStefan Hajnoczi { 829*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 830*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 831*eabba580SStefan Hajnoczi uint64_t start = acb->cur_pos + acb->cur_qiov.size; 832*eabba580SStefan Hajnoczi uint64_t len = 833*eabba580SStefan Hajnoczi qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 834*eabba580SStefan Hajnoczi uint64_t offset = acb->cur_cluster + 835*eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + 836*eabba580SStefan Hajnoczi acb->cur_qiov.size; 837*eabba580SStefan Hajnoczi 838*eabba580SStefan Hajnoczi if (ret) { 839*eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 840*eabba580SStefan Hajnoczi return; 841*eabba580SStefan Hajnoczi } 842*eabba580SStefan Hajnoczi 843*eabba580SStefan Hajnoczi trace_qed_aio_write_postfill(s, acb, start, len, offset); 844*eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, offset, 845*eabba580SStefan Hajnoczi qed_aio_write_main, acb); 846*eabba580SStefan Hajnoczi } 847*eabba580SStefan Hajnoczi 848*eabba580SStefan Hajnoczi /** 849*eabba580SStefan Hajnoczi * Populate front untouched region of new data cluster 850*eabba580SStefan Hajnoczi */ 851*eabba580SStefan Hajnoczi static void qed_aio_write_prefill(void *opaque, int ret) 852*eabba580SStefan Hajnoczi { 853*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 854*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 855*eabba580SStefan Hajnoczi uint64_t start = qed_start_of_cluster(s, acb->cur_pos); 856*eabba580SStefan Hajnoczi uint64_t len = qed_offset_into_cluster(s, acb->cur_pos); 857*eabba580SStefan Hajnoczi 858*eabba580SStefan Hajnoczi trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 859*eabba580SStefan Hajnoczi qed_copy_from_backing_file(s, start, len, acb->cur_cluster, 860*eabba580SStefan Hajnoczi qed_aio_write_postfill, acb); 861*eabba580SStefan Hajnoczi } 862*eabba580SStefan Hajnoczi 863*eabba580SStefan Hajnoczi /** 864*eabba580SStefan Hajnoczi * Write new data cluster 865*eabba580SStefan Hajnoczi * 866*eabba580SStefan Hajnoczi * @acb: Write request 867*eabba580SStefan Hajnoczi * @len: Length in bytes 868*eabba580SStefan Hajnoczi * 869*eabba580SStefan Hajnoczi * This path is taken when writing to previously unallocated clusters. 870*eabba580SStefan Hajnoczi */ 871*eabba580SStefan Hajnoczi static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 872*eabba580SStefan Hajnoczi { 873*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 874*eabba580SStefan Hajnoczi 875*eabba580SStefan Hajnoczi /* Freeze this request if another allocating write is in progress */ 876*eabba580SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 877*eabba580SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next); 878*eabba580SStefan Hajnoczi } 879*eabba580SStefan Hajnoczi if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { 880*eabba580SStefan Hajnoczi return; /* wait for existing request to finish */ 881*eabba580SStefan Hajnoczi } 882*eabba580SStefan Hajnoczi 883*eabba580SStefan Hajnoczi acb->cur_nclusters = qed_bytes_to_clusters(s, 884*eabba580SStefan Hajnoczi qed_offset_into_cluster(s, acb->cur_pos) + len); 885*eabba580SStefan Hajnoczi acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 886*eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 887*eabba580SStefan Hajnoczi 888*eabba580SStefan Hajnoczi /* Write new cluster */ 889*eabba580SStefan Hajnoczi qed_aio_write_prefill(acb, 0); 890*eabba580SStefan Hajnoczi } 891*eabba580SStefan Hajnoczi 892*eabba580SStefan Hajnoczi /** 893*eabba580SStefan Hajnoczi * Write data cluster in place 894*eabba580SStefan Hajnoczi * 895*eabba580SStefan Hajnoczi * @acb: Write request 896*eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 897*eabba580SStefan Hajnoczi * @len: Length in bytes 898*eabba580SStefan Hajnoczi * 899*eabba580SStefan Hajnoczi * This path is taken when writing to already allocated clusters. 900*eabba580SStefan Hajnoczi */ 901*eabba580SStefan Hajnoczi static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) 902*eabba580SStefan Hajnoczi { 903*eabba580SStefan Hajnoczi /* Calculate the I/O vector */ 904*eabba580SStefan Hajnoczi acb->cur_cluster = offset; 905*eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 906*eabba580SStefan Hajnoczi 907*eabba580SStefan Hajnoczi /* Do the actual write */ 908*eabba580SStefan Hajnoczi qed_aio_write_main(acb, 0); 909*eabba580SStefan Hajnoczi } 910*eabba580SStefan Hajnoczi 911*eabba580SStefan Hajnoczi /** 912*eabba580SStefan Hajnoczi * Write data cluster 913*eabba580SStefan Hajnoczi * 914*eabba580SStefan Hajnoczi * @opaque: Write request 915*eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 916*eabba580SStefan Hajnoczi * or -errno 917*eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 918*eabba580SStefan Hajnoczi * @len: Length in bytes 919*eabba580SStefan Hajnoczi * 920*eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 921*eabba580SStefan Hajnoczi */ 922*eabba580SStefan Hajnoczi static void qed_aio_write_data(void *opaque, int ret, 923*eabba580SStefan Hajnoczi uint64_t offset, size_t len) 924*eabba580SStefan Hajnoczi { 925*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 926*eabba580SStefan Hajnoczi 927*eabba580SStefan Hajnoczi trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 928*eabba580SStefan Hajnoczi 929*eabba580SStefan Hajnoczi acb->find_cluster_ret = ret; 930*eabba580SStefan Hajnoczi 931*eabba580SStefan Hajnoczi switch (ret) { 932*eabba580SStefan Hajnoczi case QED_CLUSTER_FOUND: 933*eabba580SStefan Hajnoczi qed_aio_write_inplace(acb, offset, len); 934*eabba580SStefan Hajnoczi break; 935*eabba580SStefan Hajnoczi 936*eabba580SStefan Hajnoczi case QED_CLUSTER_L2: 937*eabba580SStefan Hajnoczi case QED_CLUSTER_L1: 938*eabba580SStefan Hajnoczi qed_aio_write_alloc(acb, len); 939*eabba580SStefan Hajnoczi break; 940*eabba580SStefan Hajnoczi 941*eabba580SStefan Hajnoczi default: 942*eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 943*eabba580SStefan Hajnoczi break; 944*eabba580SStefan Hajnoczi } 945*eabba580SStefan Hajnoczi } 946*eabba580SStefan Hajnoczi 947*eabba580SStefan Hajnoczi /** 948*eabba580SStefan Hajnoczi * Read data cluster 949*eabba580SStefan Hajnoczi * 950*eabba580SStefan Hajnoczi * @opaque: Read request 951*eabba580SStefan Hajnoczi * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, 952*eabba580SStefan Hajnoczi * or -errno 953*eabba580SStefan Hajnoczi * @offset: Cluster offset in bytes 954*eabba580SStefan Hajnoczi * @len: Length in bytes 955*eabba580SStefan Hajnoczi * 956*eabba580SStefan Hajnoczi * Callback from qed_find_cluster(). 957*eabba580SStefan Hajnoczi */ 958*eabba580SStefan Hajnoczi static void qed_aio_read_data(void *opaque, int ret, 959*eabba580SStefan Hajnoczi uint64_t offset, size_t len) 960*eabba580SStefan Hajnoczi { 961*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 962*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 963*eabba580SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 964*eabba580SStefan Hajnoczi BlockDriverAIOCB *file_acb; 965*eabba580SStefan Hajnoczi 966*eabba580SStefan Hajnoczi /* Adjust offset into cluster */ 967*eabba580SStefan Hajnoczi offset += qed_offset_into_cluster(s, acb->cur_pos); 968*eabba580SStefan Hajnoczi 969*eabba580SStefan Hajnoczi trace_qed_aio_read_data(s, acb, ret, offset, len); 970*eabba580SStefan Hajnoczi 971*eabba580SStefan Hajnoczi if (ret < 0) { 972*eabba580SStefan Hajnoczi goto err; 973*eabba580SStefan Hajnoczi } 974*eabba580SStefan Hajnoczi 975*eabba580SStefan Hajnoczi qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 976*eabba580SStefan Hajnoczi 977*eabba580SStefan Hajnoczi /* Handle backing file and unallocated sparse hole reads */ 978*eabba580SStefan Hajnoczi if (ret != QED_CLUSTER_FOUND) { 979*eabba580SStefan Hajnoczi qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 980*eabba580SStefan Hajnoczi qed_aio_next_io, acb); 981*eabba580SStefan Hajnoczi return; 982*eabba580SStefan Hajnoczi } 983*eabba580SStefan Hajnoczi 984*eabba580SStefan Hajnoczi BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 985*eabba580SStefan Hajnoczi file_acb = bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE, 986*eabba580SStefan Hajnoczi &acb->cur_qiov, 987*eabba580SStefan Hajnoczi acb->cur_qiov.size / BDRV_SECTOR_SIZE, 988*eabba580SStefan Hajnoczi qed_aio_next_io, acb); 989*eabba580SStefan Hajnoczi if (!file_acb) { 990*eabba580SStefan Hajnoczi ret = -EIO; 991*eabba580SStefan Hajnoczi goto err; 992*eabba580SStefan Hajnoczi } 993*eabba580SStefan Hajnoczi return; 994*eabba580SStefan Hajnoczi 995*eabba580SStefan Hajnoczi err: 996*eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 997*eabba580SStefan Hajnoczi } 998*eabba580SStefan Hajnoczi 999*eabba580SStefan Hajnoczi /** 1000*eabba580SStefan Hajnoczi * Begin next I/O or complete the request 1001*eabba580SStefan Hajnoczi */ 1002*eabba580SStefan Hajnoczi static void qed_aio_next_io(void *opaque, int ret) 1003*eabba580SStefan Hajnoczi { 1004*eabba580SStefan Hajnoczi QEDAIOCB *acb = opaque; 1005*eabba580SStefan Hajnoczi BDRVQEDState *s = acb_to_s(acb); 1006*eabba580SStefan Hajnoczi QEDFindClusterFunc *io_fn = 1007*eabba580SStefan Hajnoczi acb->is_write ? qed_aio_write_data : qed_aio_read_data; 1008*eabba580SStefan Hajnoczi 1009*eabba580SStefan Hajnoczi trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size); 1010*eabba580SStefan Hajnoczi 1011*eabba580SStefan Hajnoczi /* Handle I/O error */ 1012*eabba580SStefan Hajnoczi if (ret) { 1013*eabba580SStefan Hajnoczi qed_aio_complete(acb, ret); 1014*eabba580SStefan Hajnoczi return; 1015*eabba580SStefan Hajnoczi } 1016*eabba580SStefan Hajnoczi 1017*eabba580SStefan Hajnoczi acb->qiov_offset += acb->cur_qiov.size; 1018*eabba580SStefan Hajnoczi acb->cur_pos += acb->cur_qiov.size; 1019*eabba580SStefan Hajnoczi qemu_iovec_reset(&acb->cur_qiov); 1020*eabba580SStefan Hajnoczi 1021*eabba580SStefan Hajnoczi /* Complete request */ 1022*eabba580SStefan Hajnoczi if (acb->cur_pos >= acb->end_pos) { 1023*eabba580SStefan Hajnoczi qed_aio_complete(acb, 0); 1024*eabba580SStefan Hajnoczi return; 1025*eabba580SStefan Hajnoczi } 1026*eabba580SStefan Hajnoczi 1027*eabba580SStefan Hajnoczi /* Find next cluster and start I/O */ 1028*eabba580SStefan Hajnoczi qed_find_cluster(s, &acb->request, 1029*eabba580SStefan Hajnoczi acb->cur_pos, acb->end_pos - acb->cur_pos, 1030*eabba580SStefan Hajnoczi io_fn, acb); 1031*eabba580SStefan Hajnoczi } 1032*eabba580SStefan Hajnoczi 1033*eabba580SStefan Hajnoczi static BlockDriverAIOCB *qed_aio_setup(BlockDriverState *bs, 1034*eabba580SStefan Hajnoczi int64_t sector_num, 1035*eabba580SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 1036*eabba580SStefan Hajnoczi BlockDriverCompletionFunc *cb, 1037*eabba580SStefan Hajnoczi void *opaque, bool is_write) 1038*eabba580SStefan Hajnoczi { 1039*eabba580SStefan Hajnoczi QEDAIOCB *acb = qemu_aio_get(&qed_aio_pool, bs, cb, opaque); 1040*eabba580SStefan Hajnoczi 1041*eabba580SStefan Hajnoczi trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, 1042*eabba580SStefan Hajnoczi opaque, is_write); 1043*eabba580SStefan Hajnoczi 1044*eabba580SStefan Hajnoczi acb->is_write = is_write; 1045*eabba580SStefan Hajnoczi acb->finished = NULL; 1046*eabba580SStefan Hajnoczi acb->qiov = qiov; 1047*eabba580SStefan Hajnoczi acb->qiov_offset = 0; 1048*eabba580SStefan Hajnoczi acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; 1049*eabba580SStefan Hajnoczi acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE; 1050*eabba580SStefan Hajnoczi acb->request.l2_table = NULL; 1051*eabba580SStefan Hajnoczi qemu_iovec_init(&acb->cur_qiov, qiov->niov); 1052*eabba580SStefan Hajnoczi 1053*eabba580SStefan Hajnoczi /* Start request */ 1054*eabba580SStefan Hajnoczi qed_aio_next_io(acb, 0); 1055*eabba580SStefan Hajnoczi return &acb->common; 1056*eabba580SStefan Hajnoczi } 1057*eabba580SStefan Hajnoczi 105875411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs, 105975411d23SStefan Hajnoczi int64_t sector_num, 106075411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 106175411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 106275411d23SStefan Hajnoczi void *opaque) 106375411d23SStefan Hajnoczi { 1064*eabba580SStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, false); 106575411d23SStefan Hajnoczi } 106675411d23SStefan Hajnoczi 106775411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs, 106875411d23SStefan Hajnoczi int64_t sector_num, 106975411d23SStefan Hajnoczi QEMUIOVector *qiov, int nb_sectors, 107075411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 107175411d23SStefan Hajnoczi void *opaque) 107275411d23SStefan Hajnoczi { 1073*eabba580SStefan Hajnoczi return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, true); 107475411d23SStefan Hajnoczi } 107575411d23SStefan Hajnoczi 107675411d23SStefan Hajnoczi static BlockDriverAIOCB *bdrv_qed_aio_flush(BlockDriverState *bs, 107775411d23SStefan Hajnoczi BlockDriverCompletionFunc *cb, 107875411d23SStefan Hajnoczi void *opaque) 107975411d23SStefan Hajnoczi { 108075411d23SStefan Hajnoczi return bdrv_aio_flush(bs->file, cb, opaque); 108175411d23SStefan Hajnoczi } 108275411d23SStefan Hajnoczi 108375411d23SStefan Hajnoczi static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset) 108475411d23SStefan Hajnoczi { 108575411d23SStefan Hajnoczi return -ENOTSUP; 108675411d23SStefan Hajnoczi } 108775411d23SStefan Hajnoczi 108875411d23SStefan Hajnoczi static int64_t bdrv_qed_getlength(BlockDriverState *bs) 108975411d23SStefan Hajnoczi { 109075411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 109175411d23SStefan Hajnoczi return s->header.image_size; 109275411d23SStefan Hajnoczi } 109375411d23SStefan Hajnoczi 109475411d23SStefan Hajnoczi static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 109575411d23SStefan Hajnoczi { 109675411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 109775411d23SStefan Hajnoczi 109875411d23SStefan Hajnoczi memset(bdi, 0, sizeof(*bdi)); 109975411d23SStefan Hajnoczi bdi->cluster_size = s->header.cluster_size; 110075411d23SStefan Hajnoczi return 0; 110175411d23SStefan Hajnoczi } 110275411d23SStefan Hajnoczi 110375411d23SStefan Hajnoczi static int bdrv_qed_change_backing_file(BlockDriverState *bs, 110475411d23SStefan Hajnoczi const char *backing_file, 110575411d23SStefan Hajnoczi const char *backing_fmt) 110675411d23SStefan Hajnoczi { 110775411d23SStefan Hajnoczi BDRVQEDState *s = bs->opaque; 110875411d23SStefan Hajnoczi QEDHeader new_header, le_header; 110975411d23SStefan Hajnoczi void *buffer; 111075411d23SStefan Hajnoczi size_t buffer_len, backing_file_len; 111175411d23SStefan Hajnoczi int ret; 111275411d23SStefan Hajnoczi 111375411d23SStefan Hajnoczi /* Refuse to set backing filename if unknown compat feature bits are 111475411d23SStefan Hajnoczi * active. If the image uses an unknown compat feature then we may not 111575411d23SStefan Hajnoczi * know the layout of data following the header structure and cannot safely 111675411d23SStefan Hajnoczi * add a new string. 111775411d23SStefan Hajnoczi */ 111875411d23SStefan Hajnoczi if (backing_file && (s->header.compat_features & 111975411d23SStefan Hajnoczi ~QED_COMPAT_FEATURE_MASK)) { 112075411d23SStefan Hajnoczi return -ENOTSUP; 112175411d23SStefan Hajnoczi } 112275411d23SStefan Hajnoczi 112375411d23SStefan Hajnoczi memcpy(&new_header, &s->header, sizeof(new_header)); 112475411d23SStefan Hajnoczi 112575411d23SStefan Hajnoczi new_header.features &= ~(QED_F_BACKING_FILE | 112675411d23SStefan Hajnoczi QED_F_BACKING_FORMAT_NO_PROBE); 112775411d23SStefan Hajnoczi 112875411d23SStefan Hajnoczi /* Adjust feature flags */ 112975411d23SStefan Hajnoczi if (backing_file) { 113075411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FILE; 113175411d23SStefan Hajnoczi 113275411d23SStefan Hajnoczi if (qed_fmt_is_raw(backing_fmt)) { 113375411d23SStefan Hajnoczi new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 113475411d23SStefan Hajnoczi } 113575411d23SStefan Hajnoczi } 113675411d23SStefan Hajnoczi 113775411d23SStefan Hajnoczi /* Calculate new header size */ 113875411d23SStefan Hajnoczi backing_file_len = 0; 113975411d23SStefan Hajnoczi 114075411d23SStefan Hajnoczi if (backing_file) { 114175411d23SStefan Hajnoczi backing_file_len = strlen(backing_file); 114275411d23SStefan Hajnoczi } 114375411d23SStefan Hajnoczi 114475411d23SStefan Hajnoczi buffer_len = sizeof(new_header); 114575411d23SStefan Hajnoczi new_header.backing_filename_offset = buffer_len; 114675411d23SStefan Hajnoczi new_header.backing_filename_size = backing_file_len; 114775411d23SStefan Hajnoczi buffer_len += backing_file_len; 114875411d23SStefan Hajnoczi 114975411d23SStefan Hajnoczi /* Make sure we can rewrite header without failing */ 115075411d23SStefan Hajnoczi if (buffer_len > new_header.header_size * new_header.cluster_size) { 115175411d23SStefan Hajnoczi return -ENOSPC; 115275411d23SStefan Hajnoczi } 115375411d23SStefan Hajnoczi 115475411d23SStefan Hajnoczi /* Prepare new header */ 115575411d23SStefan Hajnoczi buffer = qemu_malloc(buffer_len); 115675411d23SStefan Hajnoczi 115775411d23SStefan Hajnoczi qed_header_cpu_to_le(&new_header, &le_header); 115875411d23SStefan Hajnoczi memcpy(buffer, &le_header, sizeof(le_header)); 115975411d23SStefan Hajnoczi buffer_len = sizeof(le_header); 116075411d23SStefan Hajnoczi 116175411d23SStefan Hajnoczi memcpy(buffer + buffer_len, backing_file, backing_file_len); 116275411d23SStefan Hajnoczi buffer_len += backing_file_len; 116375411d23SStefan Hajnoczi 116475411d23SStefan Hajnoczi /* Write new header */ 116575411d23SStefan Hajnoczi ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); 116675411d23SStefan Hajnoczi qemu_free(buffer); 116775411d23SStefan Hajnoczi if (ret == 0) { 116875411d23SStefan Hajnoczi memcpy(&s->header, &new_header, sizeof(new_header)); 116975411d23SStefan Hajnoczi } 117075411d23SStefan Hajnoczi return ret; 117175411d23SStefan Hajnoczi } 117275411d23SStefan Hajnoczi 117375411d23SStefan Hajnoczi static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result) 117475411d23SStefan Hajnoczi { 117575411d23SStefan Hajnoczi return -ENOTSUP; 117675411d23SStefan Hajnoczi } 117775411d23SStefan Hajnoczi 117875411d23SStefan Hajnoczi static QEMUOptionParameter qed_create_options[] = { 117975411d23SStefan Hajnoczi { 118075411d23SStefan Hajnoczi .name = BLOCK_OPT_SIZE, 118175411d23SStefan Hajnoczi .type = OPT_SIZE, 118275411d23SStefan Hajnoczi .help = "Virtual disk size (in bytes)" 118375411d23SStefan Hajnoczi }, { 118475411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FILE, 118575411d23SStefan Hajnoczi .type = OPT_STRING, 118675411d23SStefan Hajnoczi .help = "File name of a base image" 118775411d23SStefan Hajnoczi }, { 118875411d23SStefan Hajnoczi .name = BLOCK_OPT_BACKING_FMT, 118975411d23SStefan Hajnoczi .type = OPT_STRING, 119075411d23SStefan Hajnoczi .help = "Image format of the base image" 119175411d23SStefan Hajnoczi }, { 119275411d23SStefan Hajnoczi .name = BLOCK_OPT_CLUSTER_SIZE, 119375411d23SStefan Hajnoczi .type = OPT_SIZE, 119475411d23SStefan Hajnoczi .help = "Cluster size (in bytes)" 119575411d23SStefan Hajnoczi }, { 119675411d23SStefan Hajnoczi .name = BLOCK_OPT_TABLE_SIZE, 119775411d23SStefan Hajnoczi .type = OPT_SIZE, 119875411d23SStefan Hajnoczi .help = "L1/L2 table size (in clusters)" 119975411d23SStefan Hajnoczi }, 120075411d23SStefan Hajnoczi { /* end of list */ } 120175411d23SStefan Hajnoczi }; 120275411d23SStefan Hajnoczi 120375411d23SStefan Hajnoczi static BlockDriver bdrv_qed = { 120475411d23SStefan Hajnoczi .format_name = "qed", 120575411d23SStefan Hajnoczi .instance_size = sizeof(BDRVQEDState), 120675411d23SStefan Hajnoczi .create_options = qed_create_options, 120775411d23SStefan Hajnoczi 120875411d23SStefan Hajnoczi .bdrv_probe = bdrv_qed_probe, 120975411d23SStefan Hajnoczi .bdrv_open = bdrv_qed_open, 121075411d23SStefan Hajnoczi .bdrv_close = bdrv_qed_close, 121175411d23SStefan Hajnoczi .bdrv_create = bdrv_qed_create, 121275411d23SStefan Hajnoczi .bdrv_flush = bdrv_qed_flush, 121375411d23SStefan Hajnoczi .bdrv_is_allocated = bdrv_qed_is_allocated, 121475411d23SStefan Hajnoczi .bdrv_make_empty = bdrv_qed_make_empty, 121575411d23SStefan Hajnoczi .bdrv_aio_readv = bdrv_qed_aio_readv, 121675411d23SStefan Hajnoczi .bdrv_aio_writev = bdrv_qed_aio_writev, 121775411d23SStefan Hajnoczi .bdrv_aio_flush = bdrv_qed_aio_flush, 121875411d23SStefan Hajnoczi .bdrv_truncate = bdrv_qed_truncate, 121975411d23SStefan Hajnoczi .bdrv_getlength = bdrv_qed_getlength, 122075411d23SStefan Hajnoczi .bdrv_get_info = bdrv_qed_get_info, 122175411d23SStefan Hajnoczi .bdrv_change_backing_file = bdrv_qed_change_backing_file, 122275411d23SStefan Hajnoczi .bdrv_check = bdrv_qed_check, 122375411d23SStefan Hajnoczi }; 122475411d23SStefan Hajnoczi 122575411d23SStefan Hajnoczi static void bdrv_qed_init(void) 122675411d23SStefan Hajnoczi { 122775411d23SStefan Hajnoczi bdrv_register(&bdrv_qed); 122875411d23SStefan Hajnoczi } 122975411d23SStefan Hajnoczi 123075411d23SStefan Hajnoczi block_init(bdrv_qed_init); 1231