Statistics
| Branch: | Revision:

root / block / qed.h @ f8bf8606

History | View | Annotate | Download (9.8 kB)

1 75411d23 Stefan Hajnoczi
/*
2 75411d23 Stefan Hajnoczi
 * QEMU Enhanced Disk Format
3 75411d23 Stefan Hajnoczi
 *
4 75411d23 Stefan Hajnoczi
 * Copyright IBM, Corp. 2010
5 75411d23 Stefan Hajnoczi
 *
6 75411d23 Stefan Hajnoczi
 * Authors:
7 75411d23 Stefan Hajnoczi
 *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
8 75411d23 Stefan Hajnoczi
 *  Anthony Liguori   <aliguori@us.ibm.com>
9 75411d23 Stefan Hajnoczi
 *
10 75411d23 Stefan Hajnoczi
 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 75411d23 Stefan Hajnoczi
 * See the COPYING.LIB file in the top-level directory.
12 75411d23 Stefan Hajnoczi
 *
13 75411d23 Stefan Hajnoczi
 */
14 75411d23 Stefan Hajnoczi
15 75411d23 Stefan Hajnoczi
#ifndef BLOCK_QED_H
16 75411d23 Stefan Hajnoczi
#define BLOCK_QED_H
17 75411d23 Stefan Hajnoczi
18 75411d23 Stefan Hajnoczi
#include "block_int.h"
19 75411d23 Stefan Hajnoczi
20 75411d23 Stefan Hajnoczi
/* The layout of a QED file is as follows:
21 75411d23 Stefan Hajnoczi
 *
22 75411d23 Stefan Hajnoczi
 * +--------+----------+----------+----------+-----+
23 75411d23 Stefan Hajnoczi
 * | header | L1 table | cluster0 | cluster1 | ... |
24 75411d23 Stefan Hajnoczi
 * +--------+----------+----------+----------+-----+
25 75411d23 Stefan Hajnoczi
 *
26 75411d23 Stefan Hajnoczi
 * There is a 2-level pagetable for cluster allocation:
27 75411d23 Stefan Hajnoczi
 *
28 75411d23 Stefan Hajnoczi
 *                     +----------+
29 75411d23 Stefan Hajnoczi
 *                     | L1 table |
30 75411d23 Stefan Hajnoczi
 *                     +----------+
31 75411d23 Stefan Hajnoczi
 *                ,------'  |  '------.
32 75411d23 Stefan Hajnoczi
 *           +----------+   |    +----------+
33 75411d23 Stefan Hajnoczi
 *           | L2 table |  ...   | L2 table |
34 75411d23 Stefan Hajnoczi
 *           +----------+        +----------+
35 75411d23 Stefan Hajnoczi
 *       ,------'  |  '------.
36 75411d23 Stefan Hajnoczi
 *  +----------+   |    +----------+
37 75411d23 Stefan Hajnoczi
 *  |   Data   |  ...   |   Data   |
38 75411d23 Stefan Hajnoczi
 *  +----------+        +----------+
39 75411d23 Stefan Hajnoczi
 *
40 75411d23 Stefan Hajnoczi
 * The L1 table is fixed size and always present.  L2 tables are allocated on
41 75411d23 Stefan Hajnoczi
 * demand.  The L1 table size determines the maximum possible image size; it
42 75411d23 Stefan Hajnoczi
 * can be influenced using the cluster_size and table_size values.
43 75411d23 Stefan Hajnoczi
 *
44 75411d23 Stefan Hajnoczi
 * All fields are little-endian on disk.
45 75411d23 Stefan Hajnoczi
 */
46 75411d23 Stefan Hajnoczi
47 75411d23 Stefan Hajnoczi
enum {
48 75411d23 Stefan Hajnoczi
    QED_MAGIC = 'Q' | 'E' << 8 | 'D' << 16 | '\0' << 24,
49 75411d23 Stefan Hajnoczi
50 75411d23 Stefan Hajnoczi
    /* The image supports a backing file */
51 75411d23 Stefan Hajnoczi
    QED_F_BACKING_FILE = 0x01,
52 75411d23 Stefan Hajnoczi
53 01979a98 Stefan Hajnoczi
    /* The image needs a consistency check before use */
54 01979a98 Stefan Hajnoczi
    QED_F_NEED_CHECK = 0x02,
55 01979a98 Stefan Hajnoczi
56 75411d23 Stefan Hajnoczi
    /* The backing file format must not be probed, treat as raw image */
57 75411d23 Stefan Hajnoczi
    QED_F_BACKING_FORMAT_NO_PROBE = 0x04,
58 75411d23 Stefan Hajnoczi
59 75411d23 Stefan Hajnoczi
    /* Feature bits must be used when the on-disk format changes */
60 75411d23 Stefan Hajnoczi
    QED_FEATURE_MASK = QED_F_BACKING_FILE | /* supported feature bits */
61 01979a98 Stefan Hajnoczi
                       QED_F_NEED_CHECK |
62 75411d23 Stefan Hajnoczi
                       QED_F_BACKING_FORMAT_NO_PROBE,
63 75411d23 Stefan Hajnoczi
    QED_COMPAT_FEATURE_MASK = 0,            /* supported compat feature bits */
64 75411d23 Stefan Hajnoczi
    QED_AUTOCLEAR_FEATURE_MASK = 0,         /* supported autoclear feature bits */
65 75411d23 Stefan Hajnoczi
66 75411d23 Stefan Hajnoczi
    /* Data is stored in groups of sectors called clusters.  Cluster size must
67 75411d23 Stefan Hajnoczi
     * be large to avoid keeping too much metadata.  I/O requests that have
68 75411d23 Stefan Hajnoczi
     * sub-cluster size will require read-modify-write.
69 75411d23 Stefan Hajnoczi
     */
70 75411d23 Stefan Hajnoczi
    QED_MIN_CLUSTER_SIZE = 4 * 1024, /* in bytes */
71 75411d23 Stefan Hajnoczi
    QED_MAX_CLUSTER_SIZE = 64 * 1024 * 1024,
72 75411d23 Stefan Hajnoczi
    QED_DEFAULT_CLUSTER_SIZE = 64 * 1024,
73 75411d23 Stefan Hajnoczi
74 75411d23 Stefan Hajnoczi
    /* Allocated clusters are tracked using a 2-level pagetable.  Table size is
75 75411d23 Stefan Hajnoczi
     * a multiple of clusters so large maximum image sizes can be supported
76 75411d23 Stefan Hajnoczi
     * without jacking up the cluster size too much.
77 75411d23 Stefan Hajnoczi
     */
78 75411d23 Stefan Hajnoczi
    QED_MIN_TABLE_SIZE = 1,        /* in clusters */
79 75411d23 Stefan Hajnoczi
    QED_MAX_TABLE_SIZE = 16,
80 75411d23 Stefan Hajnoczi
    QED_DEFAULT_TABLE_SIZE = 4,
81 75411d23 Stefan Hajnoczi
};
82 75411d23 Stefan Hajnoczi
83 75411d23 Stefan Hajnoczi
typedef struct {
84 75411d23 Stefan Hajnoczi
    uint32_t magic;                 /* QED\0 */
85 75411d23 Stefan Hajnoczi
86 75411d23 Stefan Hajnoczi
    uint32_t cluster_size;          /* in bytes */
87 75411d23 Stefan Hajnoczi
    uint32_t table_size;            /* for L1 and L2 tables, in clusters */
88 75411d23 Stefan Hajnoczi
    uint32_t header_size;           /* in clusters */
89 75411d23 Stefan Hajnoczi
90 75411d23 Stefan Hajnoczi
    uint64_t features;              /* format feature bits */
91 75411d23 Stefan Hajnoczi
    uint64_t compat_features;       /* compatible feature bits */
92 75411d23 Stefan Hajnoczi
    uint64_t autoclear_features;    /* self-resetting feature bits */
93 75411d23 Stefan Hajnoczi
94 75411d23 Stefan Hajnoczi
    uint64_t l1_table_offset;       /* in bytes */
95 75411d23 Stefan Hajnoczi
    uint64_t image_size;            /* total logical image size, in bytes */
96 75411d23 Stefan Hajnoczi
97 75411d23 Stefan Hajnoczi
    /* if (features & QED_F_BACKING_FILE) */
98 75411d23 Stefan Hajnoczi
    uint32_t backing_filename_offset; /* in bytes from start of header */
99 75411d23 Stefan Hajnoczi
    uint32_t backing_filename_size;   /* in bytes */
100 75411d23 Stefan Hajnoczi
} QEDHeader;
101 75411d23 Stefan Hajnoczi
102 75411d23 Stefan Hajnoczi
typedef struct {
103 298800ca Stefan Hajnoczi
    uint64_t offsets[0];            /* in bytes */
104 298800ca Stefan Hajnoczi
} QEDTable;
105 298800ca Stefan Hajnoczi
106 298800ca Stefan Hajnoczi
/* The L2 cache is a simple write-through cache for L2 structures */
107 298800ca Stefan Hajnoczi
typedef struct CachedL2Table {
108 298800ca Stefan Hajnoczi
    QEDTable *table;
109 298800ca Stefan Hajnoczi
    uint64_t offset;    /* offset=0 indicates an invalidate entry */
110 298800ca Stefan Hajnoczi
    QTAILQ_ENTRY(CachedL2Table) node;
111 298800ca Stefan Hajnoczi
    int ref;
112 298800ca Stefan Hajnoczi
} CachedL2Table;
113 298800ca Stefan Hajnoczi
114 298800ca Stefan Hajnoczi
typedef struct {
115 298800ca Stefan Hajnoczi
    QTAILQ_HEAD(, CachedL2Table) entries;
116 298800ca Stefan Hajnoczi
    unsigned int n_entries;
117 298800ca Stefan Hajnoczi
} L2TableCache;
118 298800ca Stefan Hajnoczi
119 298800ca Stefan Hajnoczi
typedef struct QEDRequest {
120 298800ca Stefan Hajnoczi
    CachedL2Table *l2_table;
121 298800ca Stefan Hajnoczi
} QEDRequest;
122 298800ca Stefan Hajnoczi
123 eabba580 Stefan Hajnoczi
typedef struct QEDAIOCB {
124 eabba580 Stefan Hajnoczi
    BlockDriverAIOCB common;
125 eabba580 Stefan Hajnoczi
    QEMUBH *bh;
126 eabba580 Stefan Hajnoczi
    int bh_ret;                     /* final return status for completion bh */
127 eabba580 Stefan Hajnoczi
    QSIMPLEQ_ENTRY(QEDAIOCB) next;  /* next request */
128 eabba580 Stefan Hajnoczi
    bool is_write;                  /* false - read, true - write */
129 eabba580 Stefan Hajnoczi
    bool *finished;                 /* signal for cancel completion */
130 eabba580 Stefan Hajnoczi
    uint64_t end_pos;               /* request end on block device, in bytes */
131 eabba580 Stefan Hajnoczi
132 eabba580 Stefan Hajnoczi
    /* User scatter-gather list */
133 eabba580 Stefan Hajnoczi
    QEMUIOVector *qiov;
134 eabba580 Stefan Hajnoczi
    size_t qiov_offset;             /* byte count already processed */
135 eabba580 Stefan Hajnoczi
136 eabba580 Stefan Hajnoczi
    /* Current cluster scatter-gather list */
137 eabba580 Stefan Hajnoczi
    QEMUIOVector cur_qiov;
138 eabba580 Stefan Hajnoczi
    uint64_t cur_pos;               /* position on block device, in bytes */
139 eabba580 Stefan Hajnoczi
    uint64_t cur_cluster;           /* cluster offset in image file */
140 eabba580 Stefan Hajnoczi
    unsigned int cur_nclusters;     /* number of clusters being accessed */
141 eabba580 Stefan Hajnoczi
    int find_cluster_ret;           /* used for L1/L2 update */
142 eabba580 Stefan Hajnoczi
143 eabba580 Stefan Hajnoczi
    QEDRequest request;
144 eabba580 Stefan Hajnoczi
} QEDAIOCB;
145 eabba580 Stefan Hajnoczi
146 298800ca Stefan Hajnoczi
typedef struct {
147 75411d23 Stefan Hajnoczi
    BlockDriverState *bs;           /* device */
148 75411d23 Stefan Hajnoczi
    uint64_t file_size;             /* length of image file, in bytes */
149 75411d23 Stefan Hajnoczi
150 75411d23 Stefan Hajnoczi
    QEDHeader header;               /* always cpu-endian */
151 298800ca Stefan Hajnoczi
    QEDTable *l1_table;
152 298800ca Stefan Hajnoczi
    L2TableCache l2_cache;          /* l2 table cache */
153 75411d23 Stefan Hajnoczi
    uint32_t table_nelems;
154 75411d23 Stefan Hajnoczi
    uint32_t l1_shift;
155 75411d23 Stefan Hajnoczi
    uint32_t l2_shift;
156 75411d23 Stefan Hajnoczi
    uint32_t l2_mask;
157 eabba580 Stefan Hajnoczi
158 eabba580 Stefan Hajnoczi
    /* Allocating write request queue */
159 eabba580 Stefan Hajnoczi
    QSIMPLEQ_HEAD(, QEDAIOCB) allocating_write_reqs;
160 75411d23 Stefan Hajnoczi
} BDRVQEDState;
161 75411d23 Stefan Hajnoczi
162 298800ca Stefan Hajnoczi
enum {
163 298800ca Stefan Hajnoczi
    QED_CLUSTER_FOUND,         /* cluster found */
164 298800ca Stefan Hajnoczi
    QED_CLUSTER_L2,            /* cluster missing in L2 */
165 298800ca Stefan Hajnoczi
    QED_CLUSTER_L1,            /* cluster missing in L1 */
166 298800ca Stefan Hajnoczi
};
167 298800ca Stefan Hajnoczi
168 298800ca Stefan Hajnoczi
/**
169 298800ca Stefan Hajnoczi
 * qed_find_cluster() completion callback
170 298800ca Stefan Hajnoczi
 *
171 298800ca Stefan Hajnoczi
 * @opaque:     User data for completion callback
172 298800ca Stefan Hajnoczi
 * @ret:        QED_CLUSTER_FOUND   Success
173 298800ca Stefan Hajnoczi
 *              QED_CLUSTER_L2      Data cluster unallocated in L2
174 298800ca Stefan Hajnoczi
 *              QED_CLUSTER_L1      L2 unallocated in L1
175 298800ca Stefan Hajnoczi
 *              -errno              POSIX error occurred
176 298800ca Stefan Hajnoczi
 * @offset:     Data cluster offset
177 298800ca Stefan Hajnoczi
 * @len:        Contiguous bytes starting from cluster offset
178 298800ca Stefan Hajnoczi
 *
179 298800ca Stefan Hajnoczi
 * This function is invoked when qed_find_cluster() completes.
180 298800ca Stefan Hajnoczi
 *
181 298800ca Stefan Hajnoczi
 * On success ret is QED_CLUSTER_FOUND and offset/len are a contiguous range
182 298800ca Stefan Hajnoczi
 * in the image file.
183 298800ca Stefan Hajnoczi
 *
184 298800ca Stefan Hajnoczi
 * On failure ret is QED_CLUSTER_L2 or QED_CLUSTER_L1 for missing L2 or L1
185 298800ca Stefan Hajnoczi
 * table offset, respectively.  len is number of contiguous unallocated bytes.
186 298800ca Stefan Hajnoczi
 */
187 298800ca Stefan Hajnoczi
typedef void QEDFindClusterFunc(void *opaque, int ret, uint64_t offset, size_t len);
188 298800ca Stefan Hajnoczi
189 298800ca Stefan Hajnoczi
/**
190 298800ca Stefan Hajnoczi
 * Generic callback for chaining async callbacks
191 298800ca Stefan Hajnoczi
 */
192 298800ca Stefan Hajnoczi
typedef struct {
193 298800ca Stefan Hajnoczi
    BlockDriverCompletionFunc *cb;
194 298800ca Stefan Hajnoczi
    void *opaque;
195 298800ca Stefan Hajnoczi
} GenericCB;
196 298800ca Stefan Hajnoczi
197 298800ca Stefan Hajnoczi
void *gencb_alloc(size_t len, BlockDriverCompletionFunc *cb, void *opaque);
198 298800ca Stefan Hajnoczi
void gencb_complete(void *opaque, int ret);
199 298800ca Stefan Hajnoczi
200 298800ca Stefan Hajnoczi
/**
201 298800ca Stefan Hajnoczi
 * L2 cache functions
202 298800ca Stefan Hajnoczi
 */
203 298800ca Stefan Hajnoczi
void qed_init_l2_cache(L2TableCache *l2_cache);
204 298800ca Stefan Hajnoczi
void qed_free_l2_cache(L2TableCache *l2_cache);
205 298800ca Stefan Hajnoczi
CachedL2Table *qed_alloc_l2_cache_entry(L2TableCache *l2_cache);
206 298800ca Stefan Hajnoczi
void qed_unref_l2_cache_entry(CachedL2Table *entry);
207 298800ca Stefan Hajnoczi
CachedL2Table *qed_find_l2_cache_entry(L2TableCache *l2_cache, uint64_t offset);
208 298800ca Stefan Hajnoczi
void qed_commit_l2_cache_entry(L2TableCache *l2_cache, CachedL2Table *l2_table);
209 298800ca Stefan Hajnoczi
210 298800ca Stefan Hajnoczi
/**
211 298800ca Stefan Hajnoczi
 * Table I/O functions
212 298800ca Stefan Hajnoczi
 */
213 298800ca Stefan Hajnoczi
int qed_read_l1_table_sync(BDRVQEDState *s);
214 298800ca Stefan Hajnoczi
void qed_write_l1_table(BDRVQEDState *s, unsigned int index, unsigned int n,
215 298800ca Stefan Hajnoczi
                        BlockDriverCompletionFunc *cb, void *opaque);
216 298800ca Stefan Hajnoczi
int qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
217 298800ca Stefan Hajnoczi
                            unsigned int n);
218 298800ca Stefan Hajnoczi
int qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
219 298800ca Stefan Hajnoczi
                           uint64_t offset);
220 298800ca Stefan Hajnoczi
void qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset,
221 298800ca Stefan Hajnoczi
                       BlockDriverCompletionFunc *cb, void *opaque);
222 298800ca Stefan Hajnoczi
void qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
223 298800ca Stefan Hajnoczi
                        unsigned int index, unsigned int n, bool flush,
224 298800ca Stefan Hajnoczi
                        BlockDriverCompletionFunc *cb, void *opaque);
225 298800ca Stefan Hajnoczi
int qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
226 298800ca Stefan Hajnoczi
                            unsigned int index, unsigned int n, bool flush);
227 298800ca Stefan Hajnoczi
228 298800ca Stefan Hajnoczi
/**
229 298800ca Stefan Hajnoczi
 * Cluster functions
230 298800ca Stefan Hajnoczi
 */
231 298800ca Stefan Hajnoczi
void qed_find_cluster(BDRVQEDState *s, QEDRequest *request, uint64_t pos,
232 298800ca Stefan Hajnoczi
                      size_t len, QEDFindClusterFunc *cb, void *opaque);
233 298800ca Stefan Hajnoczi
234 298800ca Stefan Hajnoczi
/**
235 298800ca Stefan Hajnoczi
 * Consistency check
236 298800ca Stefan Hajnoczi
 */
237 298800ca Stefan Hajnoczi
int qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix);
238 298800ca Stefan Hajnoczi
239 298800ca Stefan Hajnoczi
QEDTable *qed_alloc_table(BDRVQEDState *s);
240 298800ca Stefan Hajnoczi
241 75411d23 Stefan Hajnoczi
/**
242 75411d23 Stefan Hajnoczi
 * Round down to the start of a cluster
243 75411d23 Stefan Hajnoczi
 */
244 75411d23 Stefan Hajnoczi
static inline uint64_t qed_start_of_cluster(BDRVQEDState *s, uint64_t offset)
245 75411d23 Stefan Hajnoczi
{
246 75411d23 Stefan Hajnoczi
    return offset & ~(uint64_t)(s->header.cluster_size - 1);
247 75411d23 Stefan Hajnoczi
}
248 75411d23 Stefan Hajnoczi
249 298800ca Stefan Hajnoczi
static inline uint64_t qed_offset_into_cluster(BDRVQEDState *s, uint64_t offset)
250 298800ca Stefan Hajnoczi
{
251 298800ca Stefan Hajnoczi
    return offset & (s->header.cluster_size - 1);
252 298800ca Stefan Hajnoczi
}
253 298800ca Stefan Hajnoczi
254 298800ca Stefan Hajnoczi
static inline unsigned int qed_bytes_to_clusters(BDRVQEDState *s, size_t bytes)
255 298800ca Stefan Hajnoczi
{
256 298800ca Stefan Hajnoczi
    return qed_start_of_cluster(s, bytes + (s->header.cluster_size - 1)) /
257 298800ca Stefan Hajnoczi
           (s->header.cluster_size - 1);
258 298800ca Stefan Hajnoczi
}
259 298800ca Stefan Hajnoczi
260 298800ca Stefan Hajnoczi
static inline unsigned int qed_l1_index(BDRVQEDState *s, uint64_t pos)
261 298800ca Stefan Hajnoczi
{
262 298800ca Stefan Hajnoczi
    return pos >> s->l1_shift;
263 298800ca Stefan Hajnoczi
}
264 298800ca Stefan Hajnoczi
265 298800ca Stefan Hajnoczi
static inline unsigned int qed_l2_index(BDRVQEDState *s, uint64_t pos)
266 298800ca Stefan Hajnoczi
{
267 298800ca Stefan Hajnoczi
    return (pos >> s->l2_shift) & s->l2_mask;
268 298800ca Stefan Hajnoczi
}
269 298800ca Stefan Hajnoczi
270 75411d23 Stefan Hajnoczi
/**
271 75411d23 Stefan Hajnoczi
 * Test if a cluster offset is valid
272 75411d23 Stefan Hajnoczi
 */
273 75411d23 Stefan Hajnoczi
static inline bool qed_check_cluster_offset(BDRVQEDState *s, uint64_t offset)
274 75411d23 Stefan Hajnoczi
{
275 75411d23 Stefan Hajnoczi
    uint64_t header_size = (uint64_t)s->header.header_size *
276 75411d23 Stefan Hajnoczi
                           s->header.cluster_size;
277 75411d23 Stefan Hajnoczi
278 75411d23 Stefan Hajnoczi
    if (offset & (s->header.cluster_size - 1)) {
279 75411d23 Stefan Hajnoczi
        return false;
280 75411d23 Stefan Hajnoczi
    }
281 75411d23 Stefan Hajnoczi
    return offset >= header_size && offset < s->file_size;
282 75411d23 Stefan Hajnoczi
}
283 75411d23 Stefan Hajnoczi
284 75411d23 Stefan Hajnoczi
/**
285 75411d23 Stefan Hajnoczi
 * Test if a table offset is valid
286 75411d23 Stefan Hajnoczi
 */
287 75411d23 Stefan Hajnoczi
static inline bool qed_check_table_offset(BDRVQEDState *s, uint64_t offset)
288 75411d23 Stefan Hajnoczi
{
289 75411d23 Stefan Hajnoczi
    uint64_t end_offset = offset + (s->header.table_size - 1) *
290 75411d23 Stefan Hajnoczi
                          s->header.cluster_size;
291 75411d23 Stefan Hajnoczi
292 75411d23 Stefan Hajnoczi
    /* Overflow check */
293 75411d23 Stefan Hajnoczi
    if (end_offset <= offset) {
294 75411d23 Stefan Hajnoczi
        return false;
295 75411d23 Stefan Hajnoczi
    }
296 75411d23 Stefan Hajnoczi
297 75411d23 Stefan Hajnoczi
    return qed_check_cluster_offset(s, offset) &&
298 75411d23 Stefan Hajnoczi
           qed_check_cluster_offset(s, end_offset);
299 75411d23 Stefan Hajnoczi
}
300 75411d23 Stefan Hajnoczi
301 75411d23 Stefan Hajnoczi
#endif /* BLOCK_QED_H */