Statistics
| Branch: | Revision:

root / block / qed.h @ 298800ca

History | View | Annotate | Download (8.7 kB)

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

    
18
#include "block_int.h"
19

    
20
/* The layout of a QED file is as follows:
21
 *
22
 * +--------+----------+----------+----------+-----+
23
 * | header | L1 table | cluster0 | cluster1 | ... |
24
 * +--------+----------+----------+----------+-----+
25
 *
26
 * There is a 2-level pagetable for cluster allocation:
27
 *
28
 *                     +----------+
29
 *                     | L1 table |
30
 *                     +----------+
31
 *                ,------'  |  '------.
32
 *           +----------+   |    +----------+
33
 *           | L2 table |  ...   | L2 table |
34
 *           +----------+        +----------+
35
 *       ,------'  |  '------.
36
 *  +----------+   |    +----------+
37
 *  |   Data   |  ...   |   Data   |
38
 *  +----------+        +----------+
39
 *
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.
43
 *
44
 * All fields are little-endian on disk.
45
 */
46

    
47
enum {
48
    QED_MAGIC = 'Q' | 'E' << 8 | 'D' << 16 | '\0' << 24,
49

    
50
    /* The image supports a backing file */
51
    QED_F_BACKING_FILE = 0x01,
52

    
53
    /* The backing file format must not be probed, treat as raw image */
54
    QED_F_BACKING_FORMAT_NO_PROBE = 0x04,
55

    
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 */
61

    
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.
65
     */
66
    QED_MIN_CLUSTER_SIZE = 4 * 1024, /* in bytes */
67
    QED_MAX_CLUSTER_SIZE = 64 * 1024 * 1024,
68
    QED_DEFAULT_CLUSTER_SIZE = 64 * 1024,
69

    
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.
73
     */
74
    QED_MIN_TABLE_SIZE = 1,        /* in clusters */
75
    QED_MAX_TABLE_SIZE = 16,
76
    QED_DEFAULT_TABLE_SIZE = 4,
77
};
78

    
79
typedef struct {
80
    uint32_t magic;                 /* QED\0 */
81

    
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 */
85

    
86
    uint64_t features;              /* format feature bits */
87
    uint64_t compat_features;       /* compatible feature bits */
88
    uint64_t autoclear_features;    /* self-resetting feature bits */
89

    
90
    uint64_t l1_table_offset;       /* in bytes */
91
    uint64_t image_size;            /* total logical image size, in bytes */
92

    
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 */
96
} QEDHeader;
97

    
98
typedef struct {
99
    uint64_t offsets[0];            /* in bytes */
100
} QEDTable;
101

    
102
/* The L2 cache is a simple write-through cache for L2 structures */
103
typedef struct CachedL2Table {
104
    QEDTable *table;
105
    uint64_t offset;    /* offset=0 indicates an invalidate entry */
106
    QTAILQ_ENTRY(CachedL2Table) node;
107
    int ref;
108
} CachedL2Table;
109

    
110
typedef struct {
111
    QTAILQ_HEAD(, CachedL2Table) entries;
112
    unsigned int n_entries;
113
} L2TableCache;
114

    
115
typedef struct QEDRequest {
116
    CachedL2Table *l2_table;
117
} QEDRequest;
118

    
119
typedef struct {
120
    BlockDriverState *bs;           /* device */
121
    uint64_t file_size;             /* length of image file, in bytes */
122

    
123
    QEDHeader header;               /* always cpu-endian */
124
    QEDTable *l1_table;
125
    L2TableCache l2_cache;          /* l2 table cache */
126
    uint32_t table_nelems;
127
    uint32_t l1_shift;
128
    uint32_t l2_shift;
129
    uint32_t l2_mask;
130
} BDRVQEDState;
131

    
132
enum {
133
    QED_CLUSTER_FOUND,         /* cluster found */
134
    QED_CLUSTER_L2,            /* cluster missing in L2 */
135
    QED_CLUSTER_L1,            /* cluster missing in L1 */
136
};
137

    
138
/**
139
 * qed_find_cluster() completion callback
140
 *
141
 * @opaque:     User data for completion callback
142
 * @ret:        QED_CLUSTER_FOUND   Success
143
 *              QED_CLUSTER_L2      Data cluster unallocated in L2
144
 *              QED_CLUSTER_L1      L2 unallocated in L1
145
 *              -errno              POSIX error occurred
146
 * @offset:     Data cluster offset
147
 * @len:        Contiguous bytes starting from cluster offset
148
 *
149
 * This function is invoked when qed_find_cluster() completes.
150
 *
151
 * On success ret is QED_CLUSTER_FOUND and offset/len are a contiguous range
152
 * in the image file.
153
 *
154
 * On failure ret is QED_CLUSTER_L2 or QED_CLUSTER_L1 for missing L2 or L1
155
 * table offset, respectively.  len is number of contiguous unallocated bytes.
156
 */
157
typedef void QEDFindClusterFunc(void *opaque, int ret, uint64_t offset, size_t len);
158

    
159
/**
160
 * Generic callback for chaining async callbacks
161
 */
162
typedef struct {
163
    BlockDriverCompletionFunc *cb;
164
    void *opaque;
165
} GenericCB;
166

    
167
void *gencb_alloc(size_t len, BlockDriverCompletionFunc *cb, void *opaque);
168
void gencb_complete(void *opaque, int ret);
169

    
170
/**
171
 * L2 cache functions
172
 */
173
void qed_init_l2_cache(L2TableCache *l2_cache);
174
void qed_free_l2_cache(L2TableCache *l2_cache);
175
CachedL2Table *qed_alloc_l2_cache_entry(L2TableCache *l2_cache);
176
void qed_unref_l2_cache_entry(CachedL2Table *entry);
177
CachedL2Table *qed_find_l2_cache_entry(L2TableCache *l2_cache, uint64_t offset);
178
void qed_commit_l2_cache_entry(L2TableCache *l2_cache, CachedL2Table *l2_table);
179

    
180
/**
181
 * Table I/O functions
182
 */
183
int qed_read_l1_table_sync(BDRVQEDState *s);
184
void qed_write_l1_table(BDRVQEDState *s, unsigned int index, unsigned int n,
185
                        BlockDriverCompletionFunc *cb, void *opaque);
186
int qed_write_l1_table_sync(BDRVQEDState *s, unsigned int index,
187
                            unsigned int n);
188
int qed_read_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
189
                           uint64_t offset);
190
void qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset,
191
                       BlockDriverCompletionFunc *cb, void *opaque);
192
void qed_write_l2_table(BDRVQEDState *s, QEDRequest *request,
193
                        unsigned int index, unsigned int n, bool flush,
194
                        BlockDriverCompletionFunc *cb, void *opaque);
195
int qed_write_l2_table_sync(BDRVQEDState *s, QEDRequest *request,
196
                            unsigned int index, unsigned int n, bool flush);
197

    
198
/**
199
 * Cluster functions
200
 */
201
void qed_find_cluster(BDRVQEDState *s, QEDRequest *request, uint64_t pos,
202
                      size_t len, QEDFindClusterFunc *cb, void *opaque);
203

    
204
/**
205
 * Consistency check
206
 */
207
int qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix);
208

    
209
QEDTable *qed_alloc_table(BDRVQEDState *s);
210

    
211
/**
212
 * Round down to the start of a cluster
213
 */
214
static inline uint64_t qed_start_of_cluster(BDRVQEDState *s, uint64_t offset)
215
{
216
    return offset & ~(uint64_t)(s->header.cluster_size - 1);
217
}
218

    
219
static inline uint64_t qed_offset_into_cluster(BDRVQEDState *s, uint64_t offset)
220
{
221
    return offset & (s->header.cluster_size - 1);
222
}
223

    
224
static inline unsigned int qed_bytes_to_clusters(BDRVQEDState *s, size_t bytes)
225
{
226
    return qed_start_of_cluster(s, bytes + (s->header.cluster_size - 1)) /
227
           (s->header.cluster_size - 1);
228
}
229

    
230
static inline unsigned int qed_l1_index(BDRVQEDState *s, uint64_t pos)
231
{
232
    return pos >> s->l1_shift;
233
}
234

    
235
static inline unsigned int qed_l2_index(BDRVQEDState *s, uint64_t pos)
236
{
237
    return (pos >> s->l2_shift) & s->l2_mask;
238
}
239

    
240
/**
241
 * Test if a cluster offset is valid
242
 */
243
static inline bool qed_check_cluster_offset(BDRVQEDState *s, uint64_t offset)
244
{
245
    uint64_t header_size = (uint64_t)s->header.header_size *
246
                           s->header.cluster_size;
247

    
248
    if (offset & (s->header.cluster_size - 1)) {
249
        return false;
250
    }
251
    return offset >= header_size && offset < s->file_size;
252
}
253

    
254
/**
255
 * Test if a table offset is valid
256
 */
257
static inline bool qed_check_table_offset(BDRVQEDState *s, uint64_t offset)
258
{
259
    uint64_t end_offset = offset + (s->header.table_size - 1) *
260
                          s->header.cluster_size;
261

    
262
    /* Overflow check */
263
    if (end_offset <= offset) {
264
        return false;
265
    }
266

    
267
    return qed_check_cluster_offset(s, offset) &&
268
           qed_check_cluster_offset(s, end_offset);
269
}
270

    
271
#endif /* BLOCK_QED_H */