blob: f04520d4c8349ee8ecca70d98662648a2359171f [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"
Markus Armbrustere2c1c342022-12-21 14:35:49 +010016#include "block/block-io.h"
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000017#include "trace.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000019#include "qed.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010020#include "qemu/bswap.h"
Peter Maydell5df022c2022-02-26 18:07:23 +000021#include "qemu/memalign.h"
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000022
Paolo Bonzini2fd61632018-03-01 17:36:19 +010023/* Called with table_lock held. */
Kevin Wolfb9b10c32023-02-03 16:21:50 +010024static int coroutine_fn GRAPH_RDLOCK
25qed_read_table(BDRVQEDState *s, uint64_t offset, QEDTable *table)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000026{
Vladimir Sementsov-Ogievskiy696e8cb2019-04-22 17:58:33 +030027 unsigned int bytes = s->header.cluster_size * s->header.table_size;
28
Kevin Wolf11273072016-11-14 14:56:32 +010029 int noffsets;
30 int i, ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000031
Kevin Wolf11273072016-11-14 14:56:32 +010032 trace_qed_read_table(s, offset, table);
33
Paolo Bonzini2fd61632018-03-01 17:36:19 +010034 qemu_co_mutex_unlock(&s->table_lock);
Vladimir Sementsov-Ogievskiy696e8cb2019-04-22 17:58:33 +030035 ret = bdrv_co_pread(s->bs->file, offset, bytes, table->offsets, 0);
Paolo Bonzini2fd61632018-03-01 17:36:19 +010036 qemu_co_mutex_lock(&s->table_lock);
Kevin Wolf11273072016-11-14 14:56:32 +010037 if (ret < 0) {
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000038 goto out;
39 }
40
41 /* Byteswap offsets */
Vladimir Sementsov-Ogievskiy696e8cb2019-04-22 17:58:33 +030042 noffsets = bytes / sizeof(uint64_t);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000043 for (i = 0; i < noffsets; i++) {
44 table->offsets[i] = le64_to_cpu(table->offsets[i]);
45 }
46
Kevin Wolf11273072016-11-14 14:56:32 +010047 ret = 0;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000048out:
49 /* Completion */
Kevin Wolf11273072016-11-14 14:56:32 +010050 trace_qed_read_table_cb(s, table, ret);
Kevin Wolff6513522016-11-14 16:08:44 +010051 return ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000052}
53
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000054/**
55 * Write out an updated part or all of a table
56 *
57 * @s: QED state
58 * @offset: Offset of table in image file, in bytes
59 * @table: Table
60 * @index: Index of first element
61 * @n: Number of elements
62 * @flush: Whether or not to sync to disk
Paolo Bonzini1f01e502017-06-29 15:27:47 +020063 *
Paolo Bonzini2fd61632018-03-01 17:36:19 +010064 * Called with table_lock held.
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000065 */
Emanuele Giuseppe Esposito88095342023-02-03 16:21:46 +010066static int coroutine_fn GRAPH_RDLOCK
67qed_write_table(BDRVQEDState *s, uint64_t offset, QEDTable *table,
68 unsigned int index, unsigned int n, bool flush)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000069{
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000070 unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;
71 unsigned int start, end, i;
Kevin Wolf602b57f2016-11-14 14:56:32 +010072 QEDTable *new_table;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000073 size_t len_bytes;
Kevin Wolf602b57f2016-11-14 14:56:32 +010074 int ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000075
76 trace_qed_write_table(s, offset, table, index, n);
77
78 /* Calculate indices of the first and one after last elements */
79 start = index & ~sector_mask;
80 end = (index + n + sector_mask) & ~sector_mask;
81
82 len_bytes = (end - start) * sizeof(uint64_t);
83
Kevin Wolf602b57f2016-11-14 14:56:32 +010084 new_table = qemu_blockalign(s->bs, len_bytes);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000085
86 /* Byteswap table */
87 for (i = start; i < end; i++) {
88 uint64_t le_offset = cpu_to_le64(table->offsets[i]);
Kevin Wolf602b57f2016-11-14 14:56:32 +010089 new_table->offsets[i - start] = le_offset;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +000090 }
91
92 /* Adjust for offset into table */
93 offset += start * sizeof(uint64_t);
94
Paolo Bonzini2fd61632018-03-01 17:36:19 +010095 qemu_co_mutex_unlock(&s->table_lock);
Vladimir Sementsov-Ogievskiy696e8cb2019-04-22 17:58:33 +030096 ret = bdrv_co_pwrite(s->bs->file, offset, len_bytes, new_table->offsets, 0);
Paolo Bonzini2fd61632018-03-01 17:36:19 +010097 qemu_co_mutex_lock(&s->table_lock);
Kevin Wolf602b57f2016-11-14 14:56:32 +010098 trace_qed_write_table_cb(s, table, flush, ret);
99 if (ret < 0) {
100 goto out;
101 }
102
103 if (flush) {
Alberto Faria3aba34a2022-10-13 14:37:07 +0200104 ret = bdrv_co_flush(s->bs);
Kevin Wolf602b57f2016-11-14 14:56:32 +0100105 if (ret < 0) {
106 goto out;
107 }
108 }
109
110 ret = 0;
111out:
112 qemu_vfree(new_table);
Kevin Wolf453e53e2016-11-15 11:14:01 +0100113 return ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000114}
115
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300116int coroutine_fn qed_read_l1_table_sync(BDRVQEDState *s)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000117{
Kevin Wolff6513522016-11-14 16:08:44 +0100118 return qed_read_table(s, s->header.l1_table_offset, s->l1_table);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000119}
120
Paolo Bonzini2fd61632018-03-01 17:36:19 +0100121/* Called with table_lock held. */
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300122int coroutine_fn qed_write_l1_table(BDRVQEDState *s, unsigned int index,
123 unsigned int n)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000124{
Paolo Bonzini17362392023-06-01 13:51:45 +0200125 BLKDBG_CO_EVENT(s->bs->file, BLKDBG_L1_UPDATE);
Kevin Wolf453e53e2016-11-15 11:14:01 +0100126 return qed_write_table(s, s->header.l1_table_offset,
127 s->l1_table, index, n, false);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000128}
129
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300130int coroutine_fn qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
131 unsigned int n)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000132{
Kevin Wolf453e53e2016-11-15 11:14:01 +0100133 return qed_write_l1_table(s, index, n);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000134}
135
Paolo Bonzini2fd61632018-03-01 17:36:19 +0100136/* Called with table_lock held. */
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300137int coroutine_fn qed_read_l2_table(BDRVQEDState *s, QEDRequest *request,
138 uint64_t offset)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000139{
Kevin Wolff6513522016-11-14 16:08:44 +0100140 int ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000141
142 qed_unref_l2_cache_entry(request->l2_table);
143
144 /* Check for cached L2 entry */
145 request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
146 if (request->l2_table) {
Kevin Wolfa8165d22016-11-14 16:26:14 +0100147 return 0;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000148 }
149
150 request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
151 request->l2_table->table = qed_alloc_table(s);
152
Paolo Bonzini17362392023-06-01 13:51:45 +0200153 BLKDBG_CO_EVENT(s->bs->file, BLKDBG_L2_LOAD);
Kevin Wolff6513522016-11-14 16:08:44 +0100154 ret = qed_read_table(s, offset, request->l2_table->table);
155
Kevin Wolff6513522016-11-14 16:08:44 +0100156 if (ret) {
157 /* can't trust loaded L2 table anymore */
158 qed_unref_l2_cache_entry(request->l2_table);
159 request->l2_table = NULL;
160 } else {
161 request->l2_table->offset = offset;
162
163 qed_commit_l2_cache_entry(&s->l2_cache, request->l2_table);
164
165 /* This is guaranteed to succeed because we just committed the entry
166 * to the cache.
167 */
168 request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);
169 assert(request->l2_table != NULL);
170 }
Kevin Wolff6513522016-11-14 16:08:44 +0100171
Kevin Wolfa8165d22016-11-14 16:26:14 +0100172 return ret;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000173}
174
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300175int coroutine_fn qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
176 uint64_t offset)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000177{
Kevin Wolfa8165d22016-11-14 16:26:14 +0100178 return qed_read_l2_table(s, request, offset);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000179}
180
Paolo Bonzini2fd61632018-03-01 17:36:19 +0100181/* Called with table_lock held. */
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300182int coroutine_fn qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
183 unsigned int index, unsigned int n,
184 bool flush)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000185{
Paolo Bonzini17362392023-06-01 13:51:45 +0200186 BLKDBG_CO_EVENT(s->bs->file, BLKDBG_L2_UPDATE);
Kevin Wolf453e53e2016-11-15 11:14:01 +0100187 return qed_write_table(s, request->l2_table->offset,
188 request->l2_table->table, index, n, flush);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000189}
190
Vladimir Sementsov-Ogievskiy54277a22019-04-30 15:36:11 +0300191int coroutine_fn qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
192 unsigned int index, unsigned int n,
193 bool flush)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000194{
Kevin Wolf453e53e2016-11-15 11:14:01 +0100195 return qed_write_l2_table(s, request, index, n, flush);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000196}