blob: 405d446cbe70ea6087de48916f2e3efc713b1f2c [file] [log] [blame]
Stefan Hajnoczi298800c2010-12-06 16:08:01 +00001/*
2 * QEMU Enhanced Disk Format Table I/O
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Anthony Liguori <aliguori@us.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
Peter Maydell80c71a22016-01-18 18:01:42 +000015#include "qemu/osdep.h"
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000016#include "trace.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000018#include "qed.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010019#include "qemu/bswap.h"
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000020
Paolo Bonzini2fd61632018-03-01 17:36:19 +010021/* Called with table_lock held. */
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +030022static int coroutine_fn qed_read_table(BDRVQEDState *s, uint64_t offset,
23 QEDTable *table)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000024{
Vladimir Sementsov-Ogievskiy696e8cb2019-04-22 17:58:33 +030025 unsigned int bytes = s->header.cluster_size * s->header.table_size;
26
Kevin Wolf11273072016-11-14 14:56:32 +010027 int noffsets;
28 int i, ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000029
Kevin Wolf11273072016-11-14 14:56:32 +010030 trace_qed_read_table(s, offset, table);
31
Paolo Bonzini2fd61632018-03-01 17:36:19 +010032 qemu_co_mutex_unlock(&s->table_lock);
Vladimir Sementsov-Ogievskiy696e8cb2019-04-22 17:58:33 +030033 ret = bdrv_co_pread(s->bs->file, offset, bytes, table->offsets, 0);
Paolo Bonzini2fd61632018-03-01 17:36:19 +010034 qemu_co_mutex_lock(&s->table_lock);
Kevin Wolf11273072016-11-14 14:56:32 +010035 if (ret < 0) {
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000036 goto out;
37 }
38
39 /* Byteswap offsets */
Vladimir Sementsov-Ogievskiy696e8cb2019-04-22 17:58:33 +030040 noffsets = bytes / sizeof(uint64_t);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000041 for (i = 0; i < noffsets; i++) {
42 table->offsets[i] = le64_to_cpu(table->offsets[i]);
43 }
44
Kevin Wolf11273072016-11-14 14:56:32 +010045 ret = 0;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000046out:
47 /* Completion */
Kevin Wolf11273072016-11-14 14:56:32 +010048 trace_qed_read_table_cb(s, table, ret);
Kevin Wolff6513522016-11-14 16:08:44 +010049 return ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000050}
51
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000052/**
53 * Write out an updated part or all of a table
54 *
55 * @s: QED state
56 * @offset: Offset of table in image file, in bytes
57 * @table: Table
58 * @index: Index of first element
59 * @n: Number of elements
60 * @flush: Whether or not to sync to disk
Paolo Bonzini1f01e502017-06-29 15:27:47 +020061 *
Paolo Bonzini2fd61632018-03-01 17:36:19 +010062 * Called with table_lock held.
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000063 */
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +030064static int coroutine_fn qed_write_table(BDRVQEDState *s, uint64_t offset,
65 QEDTable *table, unsigned int index,
66 unsigned int n, bool flush)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000067{
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000068 unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;
69 unsigned int start, end, i;
Kevin Wolf602b57f2016-11-14 14:56:32 +010070 QEDTable *new_table;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000071 size_t len_bytes;
Kevin Wolf602b57f2016-11-14 14:56:32 +010072 int ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000073
74 trace_qed_write_table(s, offset, table, index, n);
75
76 /* Calculate indices of the first and one after last elements */
77 start = index & ~sector_mask;
78 end = (index + n + sector_mask) & ~sector_mask;
79
80 len_bytes = (end - start) * sizeof(uint64_t);
81
Kevin Wolf602b57f2016-11-14 14:56:32 +010082 new_table = qemu_blockalign(s->bs, len_bytes);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000083
84 /* Byteswap table */
85 for (i = start; i < end; i++) {
86 uint64_t le_offset = cpu_to_le64(table->offsets[i]);
Kevin Wolf602b57f2016-11-14 14:56:32 +010087 new_table->offsets[i - start] = le_offset;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000088 }
89
90 /* Adjust for offset into table */
91 offset += start * sizeof(uint64_t);
92
Paolo Bonzini2fd61632018-03-01 17:36:19 +010093 qemu_co_mutex_unlock(&s->table_lock);
Vladimir Sementsov-Ogievskiy696e8cb2019-04-22 17:58:33 +030094 ret = bdrv_co_pwrite(s->bs->file, offset, len_bytes, new_table->offsets, 0);
Paolo Bonzini2fd61632018-03-01 17:36:19 +010095 qemu_co_mutex_lock(&s->table_lock);
Kevin Wolf602b57f2016-11-14 14:56:32 +010096 trace_qed_write_table_cb(s, table, flush, ret);
97 if (ret < 0) {
98 goto out;
99 }
100
101 if (flush) {
Kevin Wolf602b57f2016-11-14 14:56:32 +0100102 ret = bdrv_flush(s->bs);
Kevin Wolf602b57f2016-11-14 14:56:32 +0100103 if (ret < 0) {
104 goto out;
105 }
106 }
107
108 ret = 0;
109out:
110 qemu_vfree(new_table);
Kevin Wolf453e53e2016-11-15 11:14:01 +0100111 return ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000112}
113
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300114int coroutine_fn qed_read_l1_table_sync(BDRVQEDState *s)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000115{
Kevin Wolff6513522016-11-14 16:08:44 +0100116 return qed_read_table(s, s->header.l1_table_offset, s->l1_table);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000117}
118
Paolo Bonzini2fd61632018-03-01 17:36:19 +0100119/* Called with table_lock held. */
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300120int coroutine_fn qed_write_l1_table(BDRVQEDState *s, unsigned int index,
121 unsigned int n)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000122{
123 BLKDBG_EVENT(s->bs->file, BLKDBG_L1_UPDATE);
Kevin Wolf453e53e2016-11-15 11:14:01 +0100124 return qed_write_table(s, s->header.l1_table_offset,
125 s->l1_table, index, n, false);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000126}
127
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300128int coroutine_fn qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
129 unsigned int n)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000130{
Kevin Wolf453e53e2016-11-15 11:14:01 +0100131 return qed_write_l1_table(s, index, n);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000132}
133
Paolo Bonzini2fd61632018-03-01 17:36:19 +0100134/* Called with table_lock held. */
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300135int coroutine_fn qed_read_l2_table(BDRVQEDState *s, QEDRequest *request,
136 uint64_t offset)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000137{
Kevin Wolff6513522016-11-14 16:08:44 +0100138 int ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000139
140 qed_unref_l2_cache_entry(request->l2_table);
141
142 /* Check for cached L2 entry */
143 request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
144 if (request->l2_table) {
Kevin Wolfa8165d22016-11-14 16:26:14 +0100145 return 0;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000146 }
147
148 request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
149 request->l2_table->table = qed_alloc_table(s);
150
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000151 BLKDBG_EVENT(s->bs->file, BLKDBG_L2_LOAD);
Kevin Wolff6513522016-11-14 16:08:44 +0100152 ret = qed_read_table(s, offset, request->l2_table->table);
153
Kevin Wolff6513522016-11-14 16:08:44 +0100154 if (ret) {
155 /* can't trust loaded L2 table anymore */
156 qed_unref_l2_cache_entry(request->l2_table);
157 request->l2_table = NULL;
158 } else {
159 request->l2_table->offset = offset;
160
161 qed_commit_l2_cache_entry(&s->l2_cache, request->l2_table);
162
163 /* This is guaranteed to succeed because we just committed the entry
164 * to the cache.
165 */
166 request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
167 assert(request->l2_table != NULL);
168 }
Kevin Wolff6513522016-11-14 16:08:44 +0100169
Kevin Wolfa8165d22016-11-14 16:26:14 +0100170 return ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000171}
172
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300173int coroutine_fn qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
174 uint64_t offset)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000175{
Kevin Wolfa8165d22016-11-14 16:26:14 +0100176 return qed_read_l2_table(s, request, offset);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000177}
178
Paolo Bonzini2fd61632018-03-01 17:36:19 +0100179/* Called with table_lock held. */
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300180int coroutine_fn qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
181 unsigned int index, unsigned int n,
182 bool flush)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000183{
184 BLKDBG_EVENT(s->bs->file, BLKDBG_L2_UPDATE);
Kevin Wolf453e53e2016-11-15 11:14:01 +0100185 return qed_write_table(s, request->l2_table->offset,
186 request->l2_table->table, index, n, flush);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000187}
188
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300189int coroutine_fn qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
190 unsigned int index, unsigned int n,
191 bool flush)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000192{
Kevin Wolf453e53e2016-11-15 11:14:01 +0100193 return qed_write_l2_table(s, request, index, n, flush);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000194}