]> git.proxmox.com Git - mirror_qemu.git/blame - block/qed-table.c
block/dirty-bitmap: return int from bdrv_remove_persistent_dirty_bitmap
[mirror_qemu.git] / block / qed-table.c
CommitLineData
298800ca
SH
1/*
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
80c71a24 15#include "qemu/osdep.h"
298800ca 16#include "trace.h"
1de7afc9 17#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
298800ca 18#include "qed.h"
58369e22 19#include "qemu/bswap.h"
298800ca 20
2fd61638 21/* Called with table_lock held. */
54277a2a
VSO
22static int coroutine_fn qed_read_table(BDRVQEDState *s, uint64_t offset,
23 QEDTable *table)
11273076 24{
696e8cb2
VSO
25 unsigned int bytes = s->header.cluster_size * s->header.table_size;
26
11273076
KW
27 int noffsets;
28 int i, ret;
298800ca 29
11273076
KW
30 trace_qed_read_table(s, offset, table);
31
2fd61638 32 qemu_co_mutex_unlock(&s->table_lock);
696e8cb2 33 ret = bdrv_co_pread(s->bs->file, offset, bytes, table->offsets, 0);
2fd61638 34 qemu_co_mutex_lock(&s->table_lock);
11273076 35 if (ret < 0) {
298800ca
SH
36 goto out;
37 }
38
39 /* Byteswap offsets */
696e8cb2 40 noffsets = bytes / sizeof(uint64_t);
298800ca
SH
41 for (i = 0; i < noffsets; i++) {
42 table->offsets[i] = le64_to_cpu(table->offsets[i]);
43 }
44
11273076 45 ret = 0;
298800ca
SH
46out:
47 /* Completion */
11273076 48 trace_qed_read_table_cb(s, table, ret);
f6513529 49 return ret;
298800ca
SH
50}
51
298800ca
SH
52/**
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
1f01e50b 61 *
2fd61638 62 * Called with table_lock held.
298800ca 63 */
54277a2a
VSO
64static int coroutine_fn qed_write_table(BDRVQEDState *s, uint64_t offset,
65 QEDTable *table, unsigned int index,
66 unsigned int n, bool flush)
298800ca 67{
298800ca
SH
68 unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;
69 unsigned int start, end, i;
602b57fb 70 QEDTable *new_table;
298800ca 71 size_t len_bytes;
602b57fb 72 int ret;
298800ca
SH
73
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
602b57fb 82 new_table = qemu_blockalign(s->bs, len_bytes);
298800ca
SH
83
84 /* Byteswap table */
85 for (i = start; i < end; i++) {
86 uint64_t le_offset = cpu_to_le64(table->offsets[i]);
602b57fb 87 new_table->offsets[i - start] = le_offset;
298800ca
SH
88 }
89
90 /* Adjust for offset into table */
91 offset += start * sizeof(uint64_t);
92
2fd61638 93 qemu_co_mutex_unlock(&s->table_lock);
696e8cb2 94 ret = bdrv_co_pwrite(s->bs->file, offset, len_bytes, new_table->offsets, 0);
2fd61638 95 qemu_co_mutex_lock(&s->table_lock);
602b57fb
KW
96 trace_qed_write_table_cb(s, table, flush, ret);
97 if (ret < 0) {
98 goto out;
99 }
100
101 if (flush) {
602b57fb 102 ret = bdrv_flush(s->bs);
602b57fb
KW
103 if (ret < 0) {
104 goto out;
105 }
106 }
107
108 ret = 0;
109out:
110 qemu_vfree(new_table);
453e53e2 111 return ret;
298800ca
SH
112}
113
54277a2a 114int coroutine_fn qed_read_l1_table_sync(BDRVQEDState *s)
298800ca 115{
f6513529 116 return qed_read_table(s, s->header.l1_table_offset, s->l1_table);
298800ca
SH
117}
118
2fd61638 119/* Called with table_lock held. */
54277a2a
VSO
120int coroutine_fn qed_write_l1_table(BDRVQEDState *s, unsigned int index,
121 unsigned int n)
298800ca
SH
122{
123 BLKDBG_EVENT(s->bs->file, BLKDBG_L1_UPDATE);
453e53e2
KW
124 return qed_write_table(s, s->header.l1_table_offset,
125 s->l1_table, index, n, false);
298800ca
SH
126}
127
54277a2a
VSO
128int coroutine_fn qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
129 unsigned int n)
298800ca 130{
453e53e2 131 return qed_write_l1_table(s, index, n);
298800ca
SH
132}
133
2fd61638 134/* Called with table_lock held. */
54277a2a
VSO
135int coroutine_fn qed_read_l2_table(BDRVQEDState *s, QEDRequest *request,
136 uint64_t offset)
298800ca 137{
f6513529 138 int ret;
298800ca
SH
139
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) {
a8165d2d 145 return 0;
298800ca
SH
146 }
147
148 request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
149 request->l2_table->table = qed_alloc_table(s);
150
298800ca 151 BLKDBG_EVENT(s->bs->file, BLKDBG_L2_LOAD);
f6513529
KW
152 ret = qed_read_table(s, offset, request->l2_table->table);
153
f6513529
KW
154 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 }
f6513529 169
a8165d2d 170 return ret;
298800ca
SH
171}
172
54277a2a
VSO
173int coroutine_fn qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
174 uint64_t offset)
298800ca 175{
a8165d2d 176 return qed_read_l2_table(s, request, offset);
298800ca
SH
177}
178
2fd61638 179/* Called with table_lock held. */
54277a2a
VSO
180int coroutine_fn qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
181 unsigned int index, unsigned int n,
182 bool flush)
298800ca
SH
183{
184 BLKDBG_EVENT(s->bs->file, BLKDBG_L2_UPDATE);
453e53e2
KW
185 return qed_write_table(s, request->l2_table->offset,
186 request->l2_table->table, index, n, flush);
298800ca
SH
187}
188
54277a2a
VSO
189int coroutine_fn qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
190 unsigned int index, unsigned int n,
191 bool flush)
298800ca 192{
453e53e2 193 return qed_write_l2_table(s, request, index, n, flush);
298800ca 194}