2 * QEMU Enhanced Disk Format
4 * Copyright IBM, Corp. 2010
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Anthony Liguori <aliguori@us.ibm.com>
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.
18 #include "block_int.h"
20 /* The layout of a QED file is as follows:
22 * +--------+----------+----------+----------+-----+
23 * | header | L1 table | cluster0 | cluster1 | ... |
24 * +--------+----------+----------+----------+-----+
26 * There is a 2-level pagetable for cluster allocation:
32 * +----------+ | +----------+
33 * | L2 table | ... | L2 table |
34 * +----------+ +----------+
36 * +----------+ | +----------+
37 * | Data | ... | Data |
38 * +----------+ +----------+
40 * The L1 table is fixed size and always present. L2 tables are allocated on
41 * demand. The L1 table size determines the maximum possible image size; it
42 * can be influenced using the cluster_size and table_size values.
44 * All fields are little-endian on disk.
48 QED_MAGIC
= 'Q' | 'E' << 8 | 'D' << 16 | '\0' << 24,
50 /* The image supports a backing file */
51 QED_F_BACKING_FILE
= 0x01,
53 /* The backing file format must not be probed, treat as raw image */
54 QED_F_BACKING_FORMAT_NO_PROBE
= 0x04,
56 /* Feature bits must be used when the on-disk format changes */
57 QED_FEATURE_MASK
= QED_F_BACKING_FILE
| /* supported feature bits */
58 QED_F_BACKING_FORMAT_NO_PROBE
,
59 QED_COMPAT_FEATURE_MASK
= 0, /* supported compat feature bits */
60 QED_AUTOCLEAR_FEATURE_MASK
= 0, /* supported autoclear feature bits */
62 /* Data is stored in groups of sectors called clusters. Cluster size must
63 * be large to avoid keeping too much metadata. I/O requests that have
64 * sub-cluster size will require read-modify-write.
66 QED_MIN_CLUSTER_SIZE
= 4 * 1024, /* in bytes */
67 QED_MAX_CLUSTER_SIZE
= 64 * 1024 * 1024,
68 QED_DEFAULT_CLUSTER_SIZE
= 64 * 1024,
70 /* Allocated clusters are tracked using a 2-level pagetable. Table size is
71 * a multiple of clusters so large maximum image sizes can be supported
72 * without jacking up the cluster size too much.
74 QED_MIN_TABLE_SIZE
= 1, /* in clusters */
75 QED_MAX_TABLE_SIZE
= 16,
76 QED_DEFAULT_TABLE_SIZE
= 4,
80 uint32_t magic
; /* QED\0 */
82 uint32_t cluster_size
; /* in bytes */
83 uint32_t table_size
; /* for L1 and L2 tables, in clusters */
84 uint32_t header_size
; /* in clusters */
86 uint64_t features
; /* format feature bits */
87 uint64_t compat_features
; /* compatible feature bits */
88 uint64_t autoclear_features
; /* self-resetting feature bits */
90 uint64_t l1_table_offset
; /* in bytes */
91 uint64_t image_size
; /* total logical image size, in bytes */
93 /* if (features & QED_F_BACKING_FILE) */
94 uint32_t backing_filename_offset
; /* in bytes from start of header */
95 uint32_t backing_filename_size
; /* in bytes */
99 uint64_t offsets
[0]; /* in bytes */
102 /* The L2 cache is a simple write-through cache for L2 structures */
103 typedef struct CachedL2Table
{
105 uint64_t offset
; /* offset=0 indicates an invalidate entry */
106 QTAILQ_ENTRY(CachedL2Table
) node
;
111 QTAILQ_HEAD(, CachedL2Table
) entries
;
112 unsigned int n_entries
;
115 typedef struct QEDRequest
{
116 CachedL2Table
*l2_table
;
119 typedef struct QEDAIOCB
{
120 BlockDriverAIOCB common
;
122 int bh_ret
; /* final return status for completion bh */
123 QSIMPLEQ_ENTRY(QEDAIOCB
) next
; /* next request */
124 bool is_write
; /* false - read, true - write */
125 bool *finished
; /* signal for cancel completion */
126 uint64_t end_pos
; /* request end on block device, in bytes */
128 /* User scatter-gather list */
130 size_t qiov_offset
; /* byte count already processed */
132 /* Current cluster scatter-gather list */
133 QEMUIOVector cur_qiov
;
134 uint64_t cur_pos
; /* position on block device, in bytes */
135 uint64_t cur_cluster
; /* cluster offset in image file */
136 unsigned int cur_nclusters
; /* number of clusters being accessed */
137 int find_cluster_ret
; /* used for L1/L2 update */
143 BlockDriverState
*bs
; /* device */
144 uint64_t file_size
; /* length of image file, in bytes */
146 QEDHeader header
; /* always cpu-endian */
148 L2TableCache l2_cache
; /* l2 table cache */
149 uint32_t table_nelems
;
154 /* Allocating write request queue */
155 QSIMPLEQ_HEAD(, QEDAIOCB
) allocating_write_reqs
;
159 QED_CLUSTER_FOUND
, /* cluster found */
160 QED_CLUSTER_L2
, /* cluster missing in L2 */
161 QED_CLUSTER_L1
, /* cluster missing in L1 */
165 * qed_find_cluster() completion callback
167 * @opaque: User data for completion callback
168 * @ret: QED_CLUSTER_FOUND Success
169 * QED_CLUSTER_L2 Data cluster unallocated in L2
170 * QED_CLUSTER_L1 L2 unallocated in L1
171 * -errno POSIX error occurred
172 * @offset: Data cluster offset
173 * @len: Contiguous bytes starting from cluster offset
175 * This function is invoked when qed_find_cluster() completes.
177 * On success ret is QED_CLUSTER_FOUND and offset/len are a contiguous range
180 * On failure ret is QED_CLUSTER_L2 or QED_CLUSTER_L1 for missing L2 or L1
181 * table offset, respectively. len is number of contiguous unallocated bytes.
183 typedef void QEDFindClusterFunc(void *opaque
, int ret
, uint64_t offset
, size_t len
);
186 * Generic callback for chaining async callbacks
189 BlockDriverCompletionFunc
*cb
;
193 void *gencb_alloc(size_t len
, BlockDriverCompletionFunc
*cb
, void *opaque
);
194 void gencb_complete(void *opaque
, int ret
);
199 void qed_init_l2_cache(L2TableCache
*l2_cache
);
200 void qed_free_l2_cache(L2TableCache
*l2_cache
);
201 CachedL2Table
*qed_alloc_l2_cache_entry(L2TableCache
*l2_cache
);
202 void qed_unref_l2_cache_entry(CachedL2Table
*entry
);
203 CachedL2Table
*qed_find_l2_cache_entry(L2TableCache
*l2_cache
, uint64_t offset
);
204 void qed_commit_l2_cache_entry(L2TableCache
*l2_cache
, CachedL2Table
*l2_table
);
207 * Table I/O functions
209 int qed_read_l1_table_sync(BDRVQEDState
*s
);
210 void qed_write_l1_table(BDRVQEDState
*s
, unsigned int index
, unsigned int n
,
211 BlockDriverCompletionFunc
*cb
, void *opaque
);
212 int qed_write_l1_table_sync(BDRVQEDState
*s
, unsigned int index
,
214 int qed_read_l2_table_sync(BDRVQEDState
*s
, QEDRequest
*request
,
216 void qed_read_l2_table(BDRVQEDState
*s
, QEDRequest
*request
, uint64_t offset
,
217 BlockDriverCompletionFunc
*cb
, void *opaque
);
218 void qed_write_l2_table(BDRVQEDState
*s
, QEDRequest
*request
,
219 unsigned int index
, unsigned int n
, bool flush
,
220 BlockDriverCompletionFunc
*cb
, void *opaque
);
221 int qed_write_l2_table_sync(BDRVQEDState
*s
, QEDRequest
*request
,
222 unsigned int index
, unsigned int n
, bool flush
);
227 void qed_find_cluster(BDRVQEDState
*s
, QEDRequest
*request
, uint64_t pos
,
228 size_t len
, QEDFindClusterFunc
*cb
, void *opaque
);
233 int qed_check(BDRVQEDState
*s
, BdrvCheckResult
*result
, bool fix
);
235 QEDTable
*qed_alloc_table(BDRVQEDState
*s
);
238 * Round down to the start of a cluster
240 static inline uint64_t qed_start_of_cluster(BDRVQEDState
*s
, uint64_t offset
)
242 return offset
& ~(uint64_t)(s
->header
.cluster_size
- 1);
245 static inline uint64_t qed_offset_into_cluster(BDRVQEDState
*s
, uint64_t offset
)
247 return offset
& (s
->header
.cluster_size
- 1);
250 static inline unsigned int qed_bytes_to_clusters(BDRVQEDState
*s
, size_t bytes
)
252 return qed_start_of_cluster(s
, bytes
+ (s
->header
.cluster_size
- 1)) /
253 (s
->header
.cluster_size
- 1);
256 static inline unsigned int qed_l1_index(BDRVQEDState
*s
, uint64_t pos
)
258 return pos
>> s
->l1_shift
;
261 static inline unsigned int qed_l2_index(BDRVQEDState
*s
, uint64_t pos
)
263 return (pos
>> s
->l2_shift
) & s
->l2_mask
;
267 * Test if a cluster offset is valid
269 static inline bool qed_check_cluster_offset(BDRVQEDState
*s
, uint64_t offset
)
271 uint64_t header_size
= (uint64_t)s
->header
.header_size
*
272 s
->header
.cluster_size
;
274 if (offset
& (s
->header
.cluster_size
- 1)) {
277 return offset
>= header_size
&& offset
< s
->file_size
;
281 * Test if a table offset is valid
283 static inline bool qed_check_table_offset(BDRVQEDState
*s
, uint64_t offset
)
285 uint64_t end_offset
= offset
+ (s
->header
.table_size
- 1) *
286 s
->header
.cluster_size
;
289 if (end_offset
<= offset
) {
293 return qed_check_cluster_offset(s
, offset
) &&
294 qed_check_cluster_offset(s
, end_offset
);
297 #endif /* BLOCK_QED_H */