blob: 988654cb860263c0f5325cc978ecbca907e36715 [file] [log] [blame]
Stefan Hajnoczi75411d22010-12-06 16:08:00 +00001/*
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
Paolo Bonzini737e1502012-12-17 18:19:44 +010018#include "block/block_int.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020019#include "qemu/cutils.h"
Stefan Hajnoczi75411d22010-12-06 16:08:00 +000020
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 */
Chunyan Liu7ab74842014-06-05 17:21:00 +080047#define QED_DEFAULT_CLUSTER_SIZE 65536
Stefan Hajnoczi75411d22010-12-06 16:08:00 +000048enum {
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
Stefan Hajnoczi01979a92010-12-06 16:08:03 +000054 /* The image needs a consistency check before use */
55 QED_F_NEED_CHECK = 0x02,
56
Stefan Hajnoczi75411d22010-12-06 16:08:00 +000057 /* 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 */
Stefan Hajnoczi01979a92010-12-06 16:08:03 +000062 QED_F_NEED_CHECK |
Stefan Hajnoczi75411d22010-12-06 16:08:00 +000063 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,
Stefan Hajnoczi75411d22010-12-06 16:08:00 +000073
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,
Stefan Hajnoczi6f321e92011-05-09 16:45:40 +010081
82 /* Delay to flush and clean image after last allocating write completes */
83 QED_NEED_CHECK_TIMEOUT = 5, /* in seconds */
Stefan Hajnoczi75411d22010-12-06 16:08:00 +000084};
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 */
Jeff Cody687fb892013-09-25 12:08:51 -0400103} QEMU_PACKED QEDHeader;
Stefan Hajnoczi75411d22010-12-06 16:08:00 +0000104
105typedef struct {
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000106 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
Stefan Hajnoczi6e4f59b2012-02-07 13:27:27 +0000126enum {
127 QED_AIOCB_WRITE = 0x0001, /* read or write? */
Stefan Hajnoczi0e71be12012-02-07 13:27:28 +0000128 QED_AIOCB_ZERO = 0x0002, /* zero write, used with QED_AIOCB_WRITE */
Stefan Hajnoczi6e4f59b2012-02-07 13:27:27 +0000129};
130
Stefan Hajnoczieabba582010-12-06 16:08:02 +0000131typedef struct QEDAIOCB {
Kevin Wolf48cc5652016-11-18 14:47:36 +0100132 BlockDriverState *bs;
Stefan Hajnoczieabba582010-12-06 16:08:02 +0000133 QSIMPLEQ_ENTRY(QEDAIOCB) next; /* next request */
Stefan Hajnoczi6e4f59b2012-02-07 13:27:27 +0000134 int flags; /* QED_AIOCB_* bits ORed together */
Stefan Hajnoczieabba582010-12-06 16:08:02 +0000135 uint64_t end_pos; /* request end on block device, in bytes */
136
137 /* User scatter-gather list */
138 QEMUIOVector *qiov;
139 size_t qiov_offset; /* byte count already processed */
140
141 /* Current cluster scatter-gather list */
142 QEMUIOVector cur_qiov;
143 uint64_t cur_pos; /* position on block device, in bytes */
144 uint64_t cur_cluster; /* cluster offset in image file */
145 unsigned int cur_nclusters; /* number of clusters being accessed */
146 int find_cluster_ret; /* used for L1/L2 update */
147
148 QEDRequest request;
149} QEDAIOCB;
150
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000151typedef struct {
Stefan Hajnoczi75411d22010-12-06 16:08:00 +0000152 BlockDriverState *bs; /* device */
Stefan Hajnoczi75411d22010-12-06 16:08:00 +0000153
Paolo Bonzini1f01e502017-06-29 15:27:47 +0200154 /* Written only by an allocating write or the timer handler (the latter
155 * while allocating reqs are plugged).
156 */
Stefan Hajnoczi75411d22010-12-06 16:08:00 +0000157 QEDHeader header; /* always cpu-endian */
Paolo Bonzini1f01e502017-06-29 15:27:47 +0200158
159 /* Protected by table_lock. */
160 CoMutex table_lock;
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000161 QEDTable *l1_table;
162 L2TableCache l2_cache; /* l2 table cache */
Stefan Hajnoczi75411d22010-12-06 16:08:00 +0000163 uint32_t table_nelems;
164 uint32_t l1_shift;
165 uint32_t l2_shift;
166 uint32_t l2_mask;
Paolo Bonzini1f01e502017-06-29 15:27:47 +0200167 uint64_t file_size; /* length of image file, in bytes */
Stefan Hajnoczieabba582010-12-06 16:08:02 +0000168
169 /* Allocating write request queue */
Kevin Wolf0806c3b2016-11-18 15:32:17 +0100170 QEDAIOCB *allocating_acb;
171 CoQueue allocating_write_reqs;
Stefan Hajnoczi6f321e92011-05-09 16:45:40 +0100172 bool allocating_write_reqs_plugged;
173
174 /* Periodic flush and clear need check flag */
175 QEMUTimer *need_check_timer;
Stefan Hajnoczi75411d22010-12-06 16:08:00 +0000176} BDRVQEDState;
177
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000178enum {
179 QED_CLUSTER_FOUND, /* cluster found */
Anthony Liguori21df65b2010-12-17 15:58:22 +0000180 QED_CLUSTER_ZERO, /* zero cluster found */
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000181 QED_CLUSTER_L2, /* cluster missing in L2 */
182 QED_CLUSTER_L1, /* cluster missing in L1 */
183};
184
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000185/**
Stefan Hajnoczib10170a2012-08-09 13:05:54 +0100186 * Header functions
187 */
188int qed_write_header_sync(BDRVQEDState *s);
189
190/**
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000191 * L2 cache functions
192 */
193void qed_init_l2_cache(L2TableCache *l2_cache);
194void qed_free_l2_cache(L2TableCache *l2_cache);
195CachedL2Table *qed_alloc_l2_cache_entry(L2TableCache *l2_cache);
196void qed_unref_l2_cache_entry(CachedL2Table *entry);
197CachedL2Table *qed_find_l2_cache_entry(L2TableCache *l2_cache, uint64_t offset);
198void qed_commit_l2_cache_entry(L2TableCache *l2_cache, CachedL2Table *l2_table);
199
200/**
201 * Table I/O functions
202 */
Kevin Wolfb9b10c32023-02-03 16:21:50 +0100203int coroutine_fn GRAPH_RDLOCK qed_read_l1_table_sync(BDRVQEDState *s);
Emanuele Giuseppe Esposito88095342023-02-03 16:21:46 +0100204
205int coroutine_fn GRAPH_RDLOCK
206qed_write_l1_table(BDRVQEDState *s, unsigned int index, unsigned int n);
207
208int coroutine_fn GRAPH_RDLOCK
209qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index, unsigned int n);
210
Kevin Wolfb9b10c32023-02-03 16:21:50 +0100211int coroutine_fn GRAPH_RDLOCK
212qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request, uint64_t offset);
213
214int coroutine_fn GRAPH_RDLOCK
215qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset);
Emanuele Giuseppe Esposito88095342023-02-03 16:21:46 +0100216
217int coroutine_fn GRAPH_RDLOCK
218qed_write_l2_table(BDRVQEDState *s, QEDRequest *request, unsigned int index,
219 unsigned int n, bool flush);
220
221int coroutine_fn GRAPH_RDLOCK
222qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
223 unsigned int index, unsigned int n, bool flush);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000224
225/**
226 * Cluster functions
227 */
Kevin Wolfb9b10c32023-02-03 16:21:50 +0100228int coroutine_fn GRAPH_RDLOCK
229qed_find_cluster(BDRVQEDState *s, QEDRequest *request, uint64_t pos,
230 size_t *len, uint64_t *img_offset);
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000231
232/**
233 * Consistency check
234 */
Emanuele Giuseppe Esposito88095342023-02-03 16:21:46 +0100235int coroutine_fn GRAPH_RDLOCK
236qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix);
237
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000238QEDTable *qed_alloc_table(BDRVQEDState *s);
239
Stefan Hajnoczi75411d22010-12-06 16:08:00 +0000240/**
241 * Round down to the start of a cluster
242 */
243static inline uint64_t qed_start_of_cluster(BDRVQEDState *s, uint64_t offset)
244{
245 return offset & ~(uint64_t)(s->header.cluster_size - 1);
246}
247
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000248static inline uint64_t qed_offset_into_cluster(BDRVQEDState *s, uint64_t offset)
249{
250 return offset & (s->header.cluster_size - 1);
251}
252
Stefan Hajnoczi19dfc442011-04-24 18:38:58 +0100253static inline uint64_t qed_bytes_to_clusters(BDRVQEDState *s, uint64_t bytes)
Stefan Hajnoczi298800c2010-12-06 16:08:01 +0000254{
255 return qed_start_of_cluster(s, bytes + (s->header.cluster_size - 1)) /
256 (s->header.cluster_size - 1);
257}
258
259static inline unsigned int qed_l1_index(BDRVQEDState *s, uint64_t pos)
260{
261 return pos >> s->l1_shift;
262}
263
264static inline unsigned int qed_l2_index(BDRVQEDState *s, uint64_t pos)
265{
266 return (pos >> s->l2_shift) & s->l2_mask;
267}
268
Stefan Hajnoczi75411d22010-12-06 16:08:00 +0000269/**
270 * Test if a cluster offset is valid
271 */
272static inline bool qed_check_cluster_offset(BDRVQEDState *s, uint64_t offset)
273{
274 uint64_t header_size = (uint64_t)s->header.header_size *
275 s->header.cluster_size;
276
277 if (offset & (s->header.cluster_size - 1)) {
278 return false;
279 }
280 return offset >= header_size && offset < s->file_size;
281}
282
283/**
284 * Test if a table offset is valid
285 */
286static inline bool qed_check_table_offset(BDRVQEDState *s, uint64_t offset)
287{
288 uint64_t end_offset = offset + (s->header.table_size - 1) *
289 s->header.cluster_size;
290
291 /* Overflow check */
292 if (end_offset <= offset) {
293 return false;
294 }
295
296 return qed_check_cluster_offset(s, offset) &&
297 qed_check_cluster_offset(s, end_offset);
298}
299
Anthony Liguori21df65b2010-12-17 15:58:22 +0000300static inline bool qed_offset_is_cluster_aligned(BDRVQEDState *s,
301 uint64_t offset)
302{
303 if (qed_offset_into_cluster(s, offset)) {
304 return false;
305 }
306 return true;
307}
308
309static inline bool qed_offset_is_unalloc_cluster(uint64_t offset)
310{
311 if (offset == 0) {
312 return true;
313 }
314 return false;
315}
316
317static inline bool qed_offset_is_zero_cluster(uint64_t offset)
318{
319 if (offset == 1) {
320 return true;
321 }
322 return false;
323}
324
Stefan Hajnoczi75411d22010-12-06 16:08:00 +0000325#endif /* BLOCK_QED_H */