]> git.proxmox.com Git - mirror_qemu.git/blame - block/qed.h
qed: Add return value to qed_aio_read/write_data()
[mirror_qemu.git] / block / qed.h
CommitLineData
75411d23
SH
1/*
2 * QEMU Enhanced Disk Format
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
15#ifndef BLOCK_QED_H
16#define BLOCK_QED_H
17
737e150e 18#include "block/block_int.h"
f348b6d1 19#include "qemu/cutils.h"
75411d23
SH
20
21/* The layout of a QED file is as follows:
22 *
23 * +--------+----------+----------+----------+-----+
24 * | header | L1 table | cluster0 | cluster1 | ... |
25 * +--------+----------+----------+----------+-----+
26 *
27 * There is a 2-level pagetable for cluster allocation:
28 *
29 * +----------+
30 * | L1 table |
31 * +----------+
32 * ,------' | '------.
33 * +----------+ | +----------+
34 * | L2 table | ... | L2 table |
35 * +----------+ +----------+
36 * ,------' | '------.
37 * +----------+ | +----------+
38 * | Data | ... | Data |
39 * +----------+ +----------+
40 *
41 * The L1 table is fixed size and always present. L2 tables are allocated on
42 * demand. The L1 table size determines the maximum possible image size; it
43 * can be influenced using the cluster_size and table_size values.
44 *
45 * All fields are little-endian on disk.
46 */
7ab74849 47#define QED_DEFAULT_CLUSTER_SIZE 65536
75411d23
SH
48enum {
49 QED_MAGIC = 'Q' | 'E' << 8 | 'D' << 16 | '\0' << 24,
50
51 /* The image supports a backing file */
52 QED_F_BACKING_FILE = 0x01,
53
01979a98
SH
54 /* The image needs a consistency check before use */
55 QED_F_NEED_CHECK = 0x02,
56
75411d23
SH
57 /* The backing file format must not be probed, treat as raw image */
58 QED_F_BACKING_FORMAT_NO_PROBE = 0x04,
59
60 /* Feature bits must be used when the on-disk format changes */
61 QED_FEATURE_MASK = QED_F_BACKING_FILE | /* supported feature bits */
01979a98 62 QED_F_NEED_CHECK |
75411d23
SH
63 QED_F_BACKING_FORMAT_NO_PROBE,
64 QED_COMPAT_FEATURE_MASK = 0, /* supported compat feature bits */
65 QED_AUTOCLEAR_FEATURE_MASK = 0, /* supported autoclear feature bits */
66
67 /* Data is stored in groups of sectors called clusters. Cluster size must
68 * be large to avoid keeping too much metadata. I/O requests that have
69 * sub-cluster size will require read-modify-write.
70 */
71 QED_MIN_CLUSTER_SIZE = 4 * 1024, /* in bytes */
72 QED_MAX_CLUSTER_SIZE = 64 * 1024 * 1024,
75411d23
SH
73
74 /* Allocated clusters are tracked using a 2-level pagetable. Table size is
75 * a multiple of clusters so large maximum image sizes can be supported
76 * without jacking up the cluster size too much.
77 */
78 QED_MIN_TABLE_SIZE = 1, /* in clusters */
79 QED_MAX_TABLE_SIZE = 16,
80 QED_DEFAULT_TABLE_SIZE = 4,
6f321e93
SH
81
82 /* Delay to flush and clean image after last allocating write completes */
83 QED_NEED_CHECK_TIMEOUT = 5, /* in seconds */
75411d23
SH
84};
85
86typedef struct {
87 uint32_t magic; /* QED\0 */
88
89 uint32_t cluster_size; /* in bytes */
90 uint32_t table_size; /* for L1 and L2 tables, in clusters */
91 uint32_t header_size; /* in clusters */
92
93 uint64_t features; /* format feature bits */
94 uint64_t compat_features; /* compatible feature bits */
95 uint64_t autoclear_features; /* self-resetting feature bits */
96
97 uint64_t l1_table_offset; /* in bytes */
98 uint64_t image_size; /* total logical image size, in bytes */
99
100 /* if (features & QED_F_BACKING_FILE) */
101 uint32_t backing_filename_offset; /* in bytes from start of header */
102 uint32_t backing_filename_size; /* in bytes */
687fb893 103} QEMU_PACKED QEDHeader;
75411d23 104
298800ca
SH
105typedef struct {
106 uint64_t offsets[0]; /* in bytes */
107} QEDTable;
108
109/* The L2 cache is a simple write-through cache for L2 structures */
110typedef struct CachedL2Table {
111 QEDTable *table;
112 uint64_t offset; /* offset=0 indicates an invalidate entry */
113 QTAILQ_ENTRY(CachedL2Table) node;
114 int ref;
115} CachedL2Table;
116
117typedef struct {
118 QTAILQ_HEAD(, CachedL2Table) entries;
119 unsigned int n_entries;
120} L2TableCache;
121
122typedef struct QEDRequest {
123 CachedL2Table *l2_table;
124} QEDRequest;
125
6e4f59bd
SH
126enum {
127 QED_AIOCB_WRITE = 0x0001, /* read or write? */
0e71be19 128 QED_AIOCB_ZERO = 0x0002, /* zero write, used with QED_AIOCB_WRITE */
6e4f59bd
SH
129};
130
eabba580 131typedef struct QEDAIOCB {
7c84b1b8 132 BlockAIOCB common;
eabba580
SH
133 int bh_ret; /* final return status for completion bh */
134 QSIMPLEQ_ENTRY(QEDAIOCB) next; /* next request */
6e4f59bd 135 int flags; /* QED_AIOCB_* bits ORed together */
eabba580
SH
136 uint64_t end_pos; /* request end on block device, in bytes */
137
138 /* User scatter-gather list */
139 QEMUIOVector *qiov;
140 size_t qiov_offset; /* byte count already processed */
141
142 /* Current cluster scatter-gather list */
143 QEMUIOVector cur_qiov;
f06ee3d4 144 QEMUIOVector *backing_qiov;
eabba580
SH
145 uint64_t cur_pos; /* position on block device, in bytes */
146 uint64_t cur_cluster; /* cluster offset in image file */
147 unsigned int cur_nclusters; /* number of clusters being accessed */
148 int find_cluster_ret; /* used for L1/L2 update */
149
150 QEDRequest request;
151} QEDAIOCB;
152
75411d23
SH
153typedef struct {
154 BlockDriverState *bs; /* device */
155 uint64_t file_size; /* length of image file, in bytes */
156
157 QEDHeader header; /* always cpu-endian */
298800ca
SH
158 QEDTable *l1_table;
159 L2TableCache l2_cache; /* l2 table cache */
75411d23
SH
160 uint32_t table_nelems;
161 uint32_t l1_shift;
162 uint32_t l2_shift;
163 uint32_t l2_mask;
eabba580
SH
164
165 /* Allocating write request queue */
166 QSIMPLEQ_HEAD(, QEDAIOCB) allocating_write_reqs;
6f321e93
SH
167 bool allocating_write_reqs_plugged;
168
169 /* Periodic flush and clear need check flag */
170 QEMUTimer *need_check_timer;
75411d23
SH
171} BDRVQEDState;
172
298800ca
SH
173enum {
174 QED_CLUSTER_FOUND, /* cluster found */
21df65b6 175 QED_CLUSTER_ZERO, /* zero cluster found */
298800ca
SH
176 QED_CLUSTER_L2, /* cluster missing in L2 */
177 QED_CLUSTER_L1, /* cluster missing in L1 */
178};
179
2f47da5f
PB
180void qed_acquire(BDRVQEDState *s);
181void qed_release(BDRVQEDState *s);
182
b10170ac
SH
183/**
184 * Header functions
185 */
186int qed_write_header_sync(BDRVQEDState *s);
187
298800ca
SH
188/**
189 * L2 cache functions
190 */
191void qed_init_l2_cache(L2TableCache *l2_cache);
192void qed_free_l2_cache(L2TableCache *l2_cache);
193CachedL2Table *qed_alloc_l2_cache_entry(L2TableCache *l2_cache);
194void qed_unref_l2_cache_entry(CachedL2Table *entry);
195CachedL2Table *qed_find_l2_cache_entry(L2TableCache *l2_cache, uint64_t offset);
196void qed_commit_l2_cache_entry(L2TableCache *l2_cache, CachedL2Table *l2_table);
197
198/**
199 * Table I/O functions
200 */
201int qed_read_l1_table_sync(BDRVQEDState *s);
453e53e2 202int qed_write_l1_table(BDRVQEDState *s, unsigned int index, unsigned int n);
298800ca
SH
203int qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
204 unsigned int n);
205int qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
206 uint64_t offset);
a8165d2d 207int qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset);
453e53e2
KW
208int qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
209 unsigned int index, unsigned int n, bool flush);
298800ca
SH
210int qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
211 unsigned int index, unsigned int n, bool flush);
212
213/**
214 * Cluster functions
215 */
0f21b7a1
KW
216int qed_find_cluster(BDRVQEDState *s, QEDRequest *request, uint64_t pos,
217 size_t *len, uint64_t *img_offset);
298800ca
SH
218
219/**
220 * Consistency check
221 */
222int qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix);
223
224QEDTable *qed_alloc_table(BDRVQEDState *s);
225
75411d23
SH
226/**
227 * Round down to the start of a cluster
228 */
229static inline uint64_t qed_start_of_cluster(BDRVQEDState *s, uint64_t offset)
230{
231 return offset & ~(uint64_t)(s->header.cluster_size - 1);
232}
233
298800ca
SH
234static inline uint64_t qed_offset_into_cluster(BDRVQEDState *s, uint64_t offset)
235{
236 return offset & (s->header.cluster_size - 1);
237}
238
19dfc44a 239static inline uint64_t qed_bytes_to_clusters(BDRVQEDState *s, uint64_t bytes)
298800ca
SH
240{
241 return qed_start_of_cluster(s, bytes + (s->header.cluster_size - 1)) /
242 (s->header.cluster_size - 1);
243}
244
245static inline unsigned int qed_l1_index(BDRVQEDState *s, uint64_t pos)
246{
247 return pos >> s->l1_shift;
248}
249
250static inline unsigned int qed_l2_index(BDRVQEDState *s, uint64_t pos)
251{
252 return (pos >> s->l2_shift) & s->l2_mask;
253}
254
75411d23
SH
255/**
256 * Test if a cluster offset is valid
257 */
258static inline bool qed_check_cluster_offset(BDRVQEDState *s, uint64_t offset)
259{
260 uint64_t header_size = (uint64_t)s->header.header_size *
261 s->header.cluster_size;
262
263 if (offset & (s->header.cluster_size - 1)) {
264 return false;
265 }
266 return offset >= header_size && offset < s->file_size;
267}
268
269/**
270 * Test if a table offset is valid
271 */
272static inline bool qed_check_table_offset(BDRVQEDState *s, uint64_t offset)
273{
274 uint64_t end_offset = offset + (s->header.table_size - 1) *
275 s->header.cluster_size;
276
277 /* Overflow check */
278 if (end_offset <= offset) {
279 return false;
280 }
281
282 return qed_check_cluster_offset(s, offset) &&
283 qed_check_cluster_offset(s, end_offset);
284}
285
21df65b6
AL
286static inline bool qed_offset_is_cluster_aligned(BDRVQEDState *s,
287 uint64_t offset)
288{
289 if (qed_offset_into_cluster(s, offset)) {
290 return false;
291 }
292 return true;
293}
294
295static inline bool qed_offset_is_unalloc_cluster(uint64_t offset)
296{
297 if (offset == 0) {
298 return true;
299 }
300 return false;
301}
302
303static inline bool qed_offset_is_zero_cluster(uint64_t offset)
304{
305 if (offset == 1) {
306 return true;
307 }
308 return false;
309}
310
75411d23 311#endif /* BLOCK_QED_H */