Statistics
| Branch: | Revision:

root / block / qcow2-cluster.c @ 72893756

History | View | Annotate | Download (27.5 kB)

1 45aba42f Kevin Wolf
/*
2 45aba42f Kevin Wolf
 * Block driver for the QCOW version 2 format
3 45aba42f Kevin Wolf
 *
4 45aba42f Kevin Wolf
 * Copyright (c) 2004-2006 Fabrice Bellard
5 45aba42f Kevin Wolf
 *
6 45aba42f Kevin Wolf
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 45aba42f Kevin Wolf
 * of this software and associated documentation files (the "Software"), to deal
8 45aba42f Kevin Wolf
 * in the Software without restriction, including without limitation the rights
9 45aba42f Kevin Wolf
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 45aba42f Kevin Wolf
 * copies of the Software, and to permit persons to whom the Software is
11 45aba42f Kevin Wolf
 * furnished to do so, subject to the following conditions:
12 45aba42f Kevin Wolf
 *
13 45aba42f Kevin Wolf
 * The above copyright notice and this permission notice shall be included in
14 45aba42f Kevin Wolf
 * all copies or substantial portions of the Software.
15 45aba42f Kevin Wolf
 *
16 45aba42f Kevin Wolf
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 45aba42f Kevin Wolf
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 45aba42f Kevin Wolf
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 45aba42f Kevin Wolf
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 45aba42f Kevin Wolf
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 45aba42f Kevin Wolf
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 45aba42f Kevin Wolf
 * THE SOFTWARE.
23 45aba42f Kevin Wolf
 */
24 45aba42f Kevin Wolf
25 45aba42f Kevin Wolf
#include <zlib.h>
26 45aba42f Kevin Wolf
27 45aba42f Kevin Wolf
#include "qemu-common.h"
28 45aba42f Kevin Wolf
#include "block_int.h"
29 45aba42f Kevin Wolf
#include "block/qcow2.h"
30 45aba42f Kevin Wolf
31 72893756 Stefan Hajnoczi
int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size)
32 45aba42f Kevin Wolf
{
33 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
34 45aba42f Kevin Wolf
    int new_l1_size, new_l1_size2, ret, i;
35 45aba42f Kevin Wolf
    uint64_t *new_l1_table;
36 5d757b56 Kevin Wolf
    int64_t new_l1_table_offset;
37 45aba42f Kevin Wolf
    uint8_t data[12];
38 45aba42f Kevin Wolf
39 72893756 Stefan Hajnoczi
    if (min_size <= s->l1_size)
40 45aba42f Kevin Wolf
        return 0;
41 72893756 Stefan Hajnoczi
42 72893756 Stefan Hajnoczi
    if (exact_size) {
43 72893756 Stefan Hajnoczi
        new_l1_size = min_size;
44 72893756 Stefan Hajnoczi
    } else {
45 72893756 Stefan Hajnoczi
        /* Bump size up to reduce the number of times we have to grow */
46 72893756 Stefan Hajnoczi
        new_l1_size = s->l1_size;
47 72893756 Stefan Hajnoczi
        if (new_l1_size == 0) {
48 72893756 Stefan Hajnoczi
            new_l1_size = 1;
49 72893756 Stefan Hajnoczi
        }
50 72893756 Stefan Hajnoczi
        while (min_size > new_l1_size) {
51 72893756 Stefan Hajnoczi
            new_l1_size = (new_l1_size * 3 + 1) / 2;
52 72893756 Stefan Hajnoczi
        }
53 45aba42f Kevin Wolf
    }
54 72893756 Stefan Hajnoczi
55 45aba42f Kevin Wolf
#ifdef DEBUG_ALLOC2
56 45aba42f Kevin Wolf
    printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
57 45aba42f Kevin Wolf
#endif
58 45aba42f Kevin Wolf
59 45aba42f Kevin Wolf
    new_l1_size2 = sizeof(uint64_t) * new_l1_size;
60 3f6a3ee5 Kevin Wolf
    new_l1_table = qemu_mallocz(align_offset(new_l1_size2, 512));
61 45aba42f Kevin Wolf
    memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
62 45aba42f Kevin Wolf
63 45aba42f Kevin Wolf
    /* write new table (align to cluster) */
64 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE);
65 ed6ccf0f Kevin Wolf
    new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
66 5d757b56 Kevin Wolf
    if (new_l1_table_offset < 0) {
67 5d757b56 Kevin Wolf
        qemu_free(new_l1_table);
68 5d757b56 Kevin Wolf
        return new_l1_table_offset;
69 5d757b56 Kevin Wolf
    }
70 29216ed1 Kevin Wolf
    bdrv_flush(bs->file);
71 45aba42f Kevin Wolf
72 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
73 45aba42f Kevin Wolf
    for(i = 0; i < s->l1_size; i++)
74 45aba42f Kevin Wolf
        new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
75 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, new_l1_table, new_l1_size2);
76 8b3b7206 Kevin Wolf
    if (ret < 0)
77 45aba42f Kevin Wolf
        goto fail;
78 45aba42f Kevin Wolf
    for(i = 0; i < s->l1_size; i++)
79 45aba42f Kevin Wolf
        new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
80 45aba42f Kevin Wolf
81 45aba42f Kevin Wolf
    /* set new table */
82 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
83 45aba42f Kevin Wolf
    cpu_to_be32w((uint32_t*)data, new_l1_size);
84 45aba42f Kevin Wolf
    cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
85 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size), data,sizeof(data));
86 8b3b7206 Kevin Wolf
    if (ret < 0) {
87 45aba42f Kevin Wolf
        goto fail;
88 fb8fa77c Kevin Wolf
    }
89 45aba42f Kevin Wolf
    qemu_free(s->l1_table);
90 ed6ccf0f Kevin Wolf
    qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
91 45aba42f Kevin Wolf
    s->l1_table_offset = new_l1_table_offset;
92 45aba42f Kevin Wolf
    s->l1_table = new_l1_table;
93 45aba42f Kevin Wolf
    s->l1_size = new_l1_size;
94 45aba42f Kevin Wolf
    return 0;
95 45aba42f Kevin Wolf
 fail:
96 fb8fa77c Kevin Wolf
    qemu_free(new_l1_table);
97 fb8fa77c Kevin Wolf
    qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2);
98 8b3b7206 Kevin Wolf
    return ret;
99 45aba42f Kevin Wolf
}
100 45aba42f Kevin Wolf
101 ed6ccf0f Kevin Wolf
void qcow2_l2_cache_reset(BlockDriverState *bs)
102 45aba42f Kevin Wolf
{
103 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
104 45aba42f Kevin Wolf
105 45aba42f Kevin Wolf
    memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
106 45aba42f Kevin Wolf
    memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
107 45aba42f Kevin Wolf
    memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
108 45aba42f Kevin Wolf
}
109 45aba42f Kevin Wolf
110 45aba42f Kevin Wolf
static inline int l2_cache_new_entry(BlockDriverState *bs)
111 45aba42f Kevin Wolf
{
112 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
113 45aba42f Kevin Wolf
    uint32_t min_count;
114 45aba42f Kevin Wolf
    int min_index, i;
115 45aba42f Kevin Wolf
116 45aba42f Kevin Wolf
    /* find a new entry in the least used one */
117 45aba42f Kevin Wolf
    min_index = 0;
118 45aba42f Kevin Wolf
    min_count = 0xffffffff;
119 45aba42f Kevin Wolf
    for(i = 0; i < L2_CACHE_SIZE; i++) {
120 45aba42f Kevin Wolf
        if (s->l2_cache_counts[i] < min_count) {
121 45aba42f Kevin Wolf
            min_count = s->l2_cache_counts[i];
122 45aba42f Kevin Wolf
            min_index = i;
123 45aba42f Kevin Wolf
        }
124 45aba42f Kevin Wolf
    }
125 45aba42f Kevin Wolf
    return min_index;
126 45aba42f Kevin Wolf
}
127 45aba42f Kevin Wolf
128 45aba42f Kevin Wolf
/*
129 45aba42f Kevin Wolf
 * seek_l2_table
130 45aba42f Kevin Wolf
 *
131 45aba42f Kevin Wolf
 * seek l2_offset in the l2_cache table
132 45aba42f Kevin Wolf
 * if not found, return NULL,
133 45aba42f Kevin Wolf
 * if found,
134 45aba42f Kevin Wolf
 *   increments the l2 cache hit count of the entry,
135 45aba42f Kevin Wolf
 *   if counter overflow, divide by two all counters
136 45aba42f Kevin Wolf
 *   return the pointer to the l2 cache entry
137 45aba42f Kevin Wolf
 *
138 45aba42f Kevin Wolf
 */
139 45aba42f Kevin Wolf
140 45aba42f Kevin Wolf
static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
141 45aba42f Kevin Wolf
{
142 45aba42f Kevin Wolf
    int i, j;
143 45aba42f Kevin Wolf
144 45aba42f Kevin Wolf
    for(i = 0; i < L2_CACHE_SIZE; i++) {
145 45aba42f Kevin Wolf
        if (l2_offset == s->l2_cache_offsets[i]) {
146 45aba42f Kevin Wolf
            /* increment the hit count */
147 45aba42f Kevin Wolf
            if (++s->l2_cache_counts[i] == 0xffffffff) {
148 45aba42f Kevin Wolf
                for(j = 0; j < L2_CACHE_SIZE; j++) {
149 45aba42f Kevin Wolf
                    s->l2_cache_counts[j] >>= 1;
150 45aba42f Kevin Wolf
                }
151 45aba42f Kevin Wolf
            }
152 45aba42f Kevin Wolf
            return s->l2_cache + (i << s->l2_bits);
153 45aba42f Kevin Wolf
        }
154 45aba42f Kevin Wolf
    }
155 45aba42f Kevin Wolf
    return NULL;
156 45aba42f Kevin Wolf
}
157 45aba42f Kevin Wolf
158 45aba42f Kevin Wolf
/*
159 45aba42f Kevin Wolf
 * l2_load
160 45aba42f Kevin Wolf
 *
161 45aba42f Kevin Wolf
 * Loads a L2 table into memory. If the table is in the cache, the cache
162 45aba42f Kevin Wolf
 * is used; otherwise the L2 table is loaded from the image file.
163 45aba42f Kevin Wolf
 *
164 45aba42f Kevin Wolf
 * Returns a pointer to the L2 table on success, or NULL if the read from
165 45aba42f Kevin Wolf
 * the image file failed.
166 45aba42f Kevin Wolf
 */
167 45aba42f Kevin Wolf
168 55c17e98 Kevin Wolf
static int l2_load(BlockDriverState *bs, uint64_t l2_offset,
169 55c17e98 Kevin Wolf
    uint64_t **l2_table)
170 45aba42f Kevin Wolf
{
171 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
172 45aba42f Kevin Wolf
    int min_index;
173 55c17e98 Kevin Wolf
    int ret;
174 45aba42f Kevin Wolf
175 45aba42f Kevin Wolf
    /* seek if the table for the given offset is in the cache */
176 45aba42f Kevin Wolf
177 55c17e98 Kevin Wolf
    *l2_table = seek_l2_table(s, l2_offset);
178 55c17e98 Kevin Wolf
    if (*l2_table != NULL) {
179 55c17e98 Kevin Wolf
        return 0;
180 55c17e98 Kevin Wolf
    }
181 45aba42f Kevin Wolf
182 45aba42f Kevin Wolf
    /* not found: load a new entry in the least used one */
183 45aba42f Kevin Wolf
184 45aba42f Kevin Wolf
    min_index = l2_cache_new_entry(bs);
185 55c17e98 Kevin Wolf
    *l2_table = s->l2_cache + (min_index << s->l2_bits);
186 8252278a Kevin Wolf
187 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
188 55c17e98 Kevin Wolf
    ret = bdrv_pread(bs->file, l2_offset, *l2_table,
189 55c17e98 Kevin Wolf
        s->l2_size * sizeof(uint64_t));
190 55c17e98 Kevin Wolf
    if (ret < 0) {
191 55c17e98 Kevin Wolf
        return ret;
192 55c17e98 Kevin Wolf
    }
193 55c17e98 Kevin Wolf
194 45aba42f Kevin Wolf
    s->l2_cache_offsets[min_index] = l2_offset;
195 45aba42f Kevin Wolf
    s->l2_cache_counts[min_index] = 1;
196 45aba42f Kevin Wolf
197 55c17e98 Kevin Wolf
    return 0;
198 45aba42f Kevin Wolf
}
199 45aba42f Kevin Wolf
200 45aba42f Kevin Wolf
/*
201 6583e3c7 Kevin Wolf
 * Writes one sector of the L1 table to the disk (can't update single entries
202 6583e3c7 Kevin Wolf
 * and we really don't want bdrv_pread to perform a read-modify-write)
203 6583e3c7 Kevin Wolf
 */
204 6583e3c7 Kevin Wolf
#define L1_ENTRIES_PER_SECTOR (512 / 8)
205 66f82cee Kevin Wolf
static int write_l1_entry(BlockDriverState *bs, int l1_index)
206 6583e3c7 Kevin Wolf
{
207 66f82cee Kevin Wolf
    BDRVQcowState *s = bs->opaque;
208 6583e3c7 Kevin Wolf
    uint64_t buf[L1_ENTRIES_PER_SECTOR];
209 6583e3c7 Kevin Wolf
    int l1_start_index;
210 f7defcb6 Kevin Wolf
    int i, ret;
211 6583e3c7 Kevin Wolf
212 6583e3c7 Kevin Wolf
    l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
213 6583e3c7 Kevin Wolf
    for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) {
214 6583e3c7 Kevin Wolf
        buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
215 6583e3c7 Kevin Wolf
    }
216 6583e3c7 Kevin Wolf
217 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
218 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset + 8 * l1_start_index,
219 f7defcb6 Kevin Wolf
        buf, sizeof(buf));
220 f7defcb6 Kevin Wolf
    if (ret < 0) {
221 f7defcb6 Kevin Wolf
        return ret;
222 6583e3c7 Kevin Wolf
    }
223 6583e3c7 Kevin Wolf
224 6583e3c7 Kevin Wolf
    return 0;
225 6583e3c7 Kevin Wolf
}
226 6583e3c7 Kevin Wolf
227 6583e3c7 Kevin Wolf
/*
228 45aba42f Kevin Wolf
 * l2_allocate
229 45aba42f Kevin Wolf
 *
230 45aba42f Kevin Wolf
 * Allocate a new l2 entry in the file. If l1_index points to an already
231 45aba42f Kevin Wolf
 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
232 45aba42f Kevin Wolf
 * table) copy the contents of the old L2 table into the newly allocated one.
233 45aba42f Kevin Wolf
 * Otherwise the new table is initialized with zeros.
234 45aba42f Kevin Wolf
 *
235 45aba42f Kevin Wolf
 */
236 45aba42f Kevin Wolf
237 c46e1167 Kevin Wolf
static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
238 45aba42f Kevin Wolf
{
239 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
240 45aba42f Kevin Wolf
    int min_index;
241 6583e3c7 Kevin Wolf
    uint64_t old_l2_offset;
242 f4f0d391 Kevin Wolf
    uint64_t *l2_table;
243 f4f0d391 Kevin Wolf
    int64_t l2_offset;
244 c46e1167 Kevin Wolf
    int ret;
245 45aba42f Kevin Wolf
246 45aba42f Kevin Wolf
    old_l2_offset = s->l1_table[l1_index];
247 45aba42f Kevin Wolf
248 45aba42f Kevin Wolf
    /* allocate a new l2 entry */
249 45aba42f Kevin Wolf
250 ed6ccf0f Kevin Wolf
    l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
251 5d757b56 Kevin Wolf
    if (l2_offset < 0) {
252 c46e1167 Kevin Wolf
        return l2_offset;
253 5d757b56 Kevin Wolf
    }
254 29216ed1 Kevin Wolf
    bdrv_flush(bs->file);
255 45aba42f Kevin Wolf
256 45aba42f Kevin Wolf
    /* allocate a new entry in the l2 cache */
257 45aba42f Kevin Wolf
258 45aba42f Kevin Wolf
    min_index = l2_cache_new_entry(bs);
259 45aba42f Kevin Wolf
    l2_table = s->l2_cache + (min_index << s->l2_bits);
260 45aba42f Kevin Wolf
261 45aba42f Kevin Wolf
    if (old_l2_offset == 0) {
262 45aba42f Kevin Wolf
        /* if there was no old l2 table, clear the new table */
263 45aba42f Kevin Wolf
        memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
264 45aba42f Kevin Wolf
    } else {
265 45aba42f Kevin Wolf
        /* if there was an old l2 table, read it from the disk */
266 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
267 66f82cee Kevin Wolf
        ret = bdrv_pread(bs->file, old_l2_offset, l2_table,
268 c46e1167 Kevin Wolf
            s->l2_size * sizeof(uint64_t));
269 c46e1167 Kevin Wolf
        if (ret < 0) {
270 175e1152 Kevin Wolf
            goto fail;
271 c46e1167 Kevin Wolf
        }
272 45aba42f Kevin Wolf
    }
273 45aba42f Kevin Wolf
    /* write the l2 table to the file */
274 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
275 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
276 c46e1167 Kevin Wolf
        s->l2_size * sizeof(uint64_t));
277 c46e1167 Kevin Wolf
    if (ret < 0) {
278 175e1152 Kevin Wolf
        goto fail;
279 175e1152 Kevin Wolf
    }
280 175e1152 Kevin Wolf
281 175e1152 Kevin Wolf
    /* update the L1 entry */
282 175e1152 Kevin Wolf
    s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
283 175e1152 Kevin Wolf
    ret = write_l1_entry(bs, l1_index);
284 175e1152 Kevin Wolf
    if (ret < 0) {
285 175e1152 Kevin Wolf
        goto fail;
286 c46e1167 Kevin Wolf
    }
287 45aba42f Kevin Wolf
288 45aba42f Kevin Wolf
    /* update the l2 cache entry */
289 45aba42f Kevin Wolf
290 45aba42f Kevin Wolf
    s->l2_cache_offsets[min_index] = l2_offset;
291 45aba42f Kevin Wolf
    s->l2_cache_counts[min_index] = 1;
292 45aba42f Kevin Wolf
293 c46e1167 Kevin Wolf
    *table = l2_table;
294 c46e1167 Kevin Wolf
    return 0;
295 175e1152 Kevin Wolf
296 175e1152 Kevin Wolf
fail:
297 68dba0bf Kevin Wolf
    s->l1_table[l1_index] = old_l2_offset;
298 175e1152 Kevin Wolf
    qcow2_l2_cache_reset(bs);
299 175e1152 Kevin Wolf
    return ret;
300 45aba42f Kevin Wolf
}
301 45aba42f Kevin Wolf
302 45aba42f Kevin Wolf
static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
303 45aba42f Kevin Wolf
        uint64_t *l2_table, uint64_t start, uint64_t mask)
304 45aba42f Kevin Wolf
{
305 45aba42f Kevin Wolf
    int i;
306 45aba42f Kevin Wolf
    uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
307 45aba42f Kevin Wolf
308 45aba42f Kevin Wolf
    if (!offset)
309 45aba42f Kevin Wolf
        return 0;
310 45aba42f Kevin Wolf
311 45aba42f Kevin Wolf
    for (i = start; i < start + nb_clusters; i++)
312 80ee15a6 Kevin Wolf
        if (offset + (uint64_t) i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
313 45aba42f Kevin Wolf
            break;
314 45aba42f Kevin Wolf
315 45aba42f Kevin Wolf
        return (i - start);
316 45aba42f Kevin Wolf
}
317 45aba42f Kevin Wolf
318 45aba42f Kevin Wolf
static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
319 45aba42f Kevin Wolf
{
320 45aba42f Kevin Wolf
    int i = 0;
321 45aba42f Kevin Wolf
322 45aba42f Kevin Wolf
    while(nb_clusters-- && l2_table[i] == 0)
323 45aba42f Kevin Wolf
        i++;
324 45aba42f Kevin Wolf
325 45aba42f Kevin Wolf
    return i;
326 45aba42f Kevin Wolf
}
327 45aba42f Kevin Wolf
328 45aba42f Kevin Wolf
/* The crypt function is compatible with the linux cryptoloop
329 45aba42f Kevin Wolf
   algorithm for < 4 GB images. NOTE: out_buf == in_buf is
330 45aba42f Kevin Wolf
   supported */
331 ed6ccf0f Kevin Wolf
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
332 ed6ccf0f Kevin Wolf
                           uint8_t *out_buf, const uint8_t *in_buf,
333 ed6ccf0f Kevin Wolf
                           int nb_sectors, int enc,
334 ed6ccf0f Kevin Wolf
                           const AES_KEY *key)
335 45aba42f Kevin Wolf
{
336 45aba42f Kevin Wolf
    union {
337 45aba42f Kevin Wolf
        uint64_t ll[2];
338 45aba42f Kevin Wolf
        uint8_t b[16];
339 45aba42f Kevin Wolf
    } ivec;
340 45aba42f Kevin Wolf
    int i;
341 45aba42f Kevin Wolf
342 45aba42f Kevin Wolf
    for(i = 0; i < nb_sectors; i++) {
343 45aba42f Kevin Wolf
        ivec.ll[0] = cpu_to_le64(sector_num);
344 45aba42f Kevin Wolf
        ivec.ll[1] = 0;
345 45aba42f Kevin Wolf
        AES_cbc_encrypt(in_buf, out_buf, 512, key,
346 45aba42f Kevin Wolf
                        ivec.b, enc);
347 45aba42f Kevin Wolf
        sector_num++;
348 45aba42f Kevin Wolf
        in_buf += 512;
349 45aba42f Kevin Wolf
        out_buf += 512;
350 45aba42f Kevin Wolf
    }
351 45aba42f Kevin Wolf
}
352 45aba42f Kevin Wolf
353 45aba42f Kevin Wolf
354 72ecf02d Kevin Wolf
static int qcow_read(BlockDriverState *bs, int64_t sector_num,
355 72ecf02d Kevin Wolf
                     uint8_t *buf, int nb_sectors)
356 45aba42f Kevin Wolf
{
357 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
358 45aba42f Kevin Wolf
    int ret, index_in_cluster, n, n1;
359 45aba42f Kevin Wolf
    uint64_t cluster_offset;
360 bd28f835 Kevin Wolf
    struct iovec iov;
361 bd28f835 Kevin Wolf
    QEMUIOVector qiov;
362 45aba42f Kevin Wolf
363 45aba42f Kevin Wolf
    while (nb_sectors > 0) {
364 45aba42f Kevin Wolf
        n = nb_sectors;
365 1c46efaa Kevin Wolf
366 1c46efaa Kevin Wolf
        ret = qcow2_get_cluster_offset(bs, sector_num << 9, &n,
367 1c46efaa Kevin Wolf
            &cluster_offset);
368 1c46efaa Kevin Wolf
        if (ret < 0) {
369 1c46efaa Kevin Wolf
            return ret;
370 1c46efaa Kevin Wolf
        }
371 1c46efaa Kevin Wolf
372 45aba42f Kevin Wolf
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
373 45aba42f Kevin Wolf
        if (!cluster_offset) {
374 45aba42f Kevin Wolf
            if (bs->backing_hd) {
375 45aba42f Kevin Wolf
                /* read from the base image */
376 bd28f835 Kevin Wolf
                iov.iov_base = buf;
377 bd28f835 Kevin Wolf
                iov.iov_len = n * 512;
378 bd28f835 Kevin Wolf
                qemu_iovec_init_external(&qiov, &iov, 1);
379 bd28f835 Kevin Wolf
380 bd28f835 Kevin Wolf
                n1 = qcow2_backing_read1(bs->backing_hd, &qiov, sector_num, n);
381 45aba42f Kevin Wolf
                if (n1 > 0) {
382 66f82cee Kevin Wolf
                    BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING);
383 45aba42f Kevin Wolf
                    ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
384 45aba42f Kevin Wolf
                    if (ret < 0)
385 45aba42f Kevin Wolf
                        return -1;
386 45aba42f Kevin Wolf
                }
387 45aba42f Kevin Wolf
            } else {
388 45aba42f Kevin Wolf
                memset(buf, 0, 512 * n);
389 45aba42f Kevin Wolf
            }
390 45aba42f Kevin Wolf
        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
391 66f82cee Kevin Wolf
            if (qcow2_decompress_cluster(bs, cluster_offset) < 0)
392 45aba42f Kevin Wolf
                return -1;
393 45aba42f Kevin Wolf
            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
394 45aba42f Kevin Wolf
        } else {
395 66f82cee Kevin Wolf
            BLKDBG_EVENT(bs->file, BLKDBG_READ);
396 66f82cee Kevin Wolf
            ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);
397 45aba42f Kevin Wolf
            if (ret != n * 512)
398 45aba42f Kevin Wolf
                return -1;
399 45aba42f Kevin Wolf
            if (s->crypt_method) {
400 ed6ccf0f Kevin Wolf
                qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
401 45aba42f Kevin Wolf
                                &s->aes_decrypt_key);
402 45aba42f Kevin Wolf
            }
403 45aba42f Kevin Wolf
        }
404 45aba42f Kevin Wolf
        nb_sectors -= n;
405 45aba42f Kevin Wolf
        sector_num += n;
406 45aba42f Kevin Wolf
        buf += n * 512;
407 45aba42f Kevin Wolf
    }
408 45aba42f Kevin Wolf
    return 0;
409 45aba42f Kevin Wolf
}
410 45aba42f Kevin Wolf
411 45aba42f Kevin Wolf
static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
412 45aba42f Kevin Wolf
                        uint64_t cluster_offset, int n_start, int n_end)
413 45aba42f Kevin Wolf
{
414 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
415 45aba42f Kevin Wolf
    int n, ret;
416 45aba42f Kevin Wolf
417 45aba42f Kevin Wolf
    n = n_end - n_start;
418 45aba42f Kevin Wolf
    if (n <= 0)
419 45aba42f Kevin Wolf
        return 0;
420 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
421 72ecf02d Kevin Wolf
    ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
422 45aba42f Kevin Wolf
    if (ret < 0)
423 45aba42f Kevin Wolf
        return ret;
424 45aba42f Kevin Wolf
    if (s->crypt_method) {
425 ed6ccf0f Kevin Wolf
        qcow2_encrypt_sectors(s, start_sect + n_start,
426 45aba42f Kevin Wolf
                        s->cluster_data,
427 45aba42f Kevin Wolf
                        s->cluster_data, n, 1,
428 45aba42f Kevin Wolf
                        &s->aes_encrypt_key);
429 45aba42f Kevin Wolf
    }
430 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
431 9f8e668e Kevin Wolf
    ret = bdrv_write(bs->file, (cluster_offset >> 9) + n_start,
432 8b3b7206 Kevin Wolf
        s->cluster_data, n);
433 45aba42f Kevin Wolf
    if (ret < 0)
434 45aba42f Kevin Wolf
        return ret;
435 45aba42f Kevin Wolf
    return 0;
436 45aba42f Kevin Wolf
}
437 45aba42f Kevin Wolf
438 45aba42f Kevin Wolf
439 45aba42f Kevin Wolf
/*
440 45aba42f Kevin Wolf
 * get_cluster_offset
441 45aba42f Kevin Wolf
 *
442 1c46efaa Kevin Wolf
 * For a given offset of the disk image, find the cluster offset in
443 1c46efaa Kevin Wolf
 * qcow2 file. The offset is stored in *cluster_offset.
444 45aba42f Kevin Wolf
 *
445 45aba42f Kevin Wolf
 * on entry, *num is the number of contiguous clusters we'd like to
446 45aba42f Kevin Wolf
 * access following offset.
447 45aba42f Kevin Wolf
 *
448 45aba42f Kevin Wolf
 * on exit, *num is the number of contiguous clusters we can read.
449 45aba42f Kevin Wolf
 *
450 1c46efaa Kevin Wolf
 * Return 0, if the offset is found
451 1c46efaa Kevin Wolf
 * Return -errno, otherwise.
452 45aba42f Kevin Wolf
 *
453 45aba42f Kevin Wolf
 */
454 45aba42f Kevin Wolf
455 1c46efaa Kevin Wolf
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
456 1c46efaa Kevin Wolf
    int *num, uint64_t *cluster_offset)
457 45aba42f Kevin Wolf
{
458 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
459 80ee15a6 Kevin Wolf
    unsigned int l1_index, l2_index;
460 1c46efaa Kevin Wolf
    uint64_t l2_offset, *l2_table;
461 45aba42f Kevin Wolf
    int l1_bits, c;
462 80ee15a6 Kevin Wolf
    unsigned int index_in_cluster, nb_clusters;
463 80ee15a6 Kevin Wolf
    uint64_t nb_available, nb_needed;
464 55c17e98 Kevin Wolf
    int ret;
465 45aba42f Kevin Wolf
466 45aba42f Kevin Wolf
    index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
467 45aba42f Kevin Wolf
    nb_needed = *num + index_in_cluster;
468 45aba42f Kevin Wolf
469 45aba42f Kevin Wolf
    l1_bits = s->l2_bits + s->cluster_bits;
470 45aba42f Kevin Wolf
471 45aba42f Kevin Wolf
    /* compute how many bytes there are between the offset and
472 45aba42f Kevin Wolf
     * the end of the l1 entry
473 45aba42f Kevin Wolf
     */
474 45aba42f Kevin Wolf
475 80ee15a6 Kevin Wolf
    nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
476 45aba42f Kevin Wolf
477 45aba42f Kevin Wolf
    /* compute the number of available sectors */
478 45aba42f Kevin Wolf
479 45aba42f Kevin Wolf
    nb_available = (nb_available >> 9) + index_in_cluster;
480 45aba42f Kevin Wolf
481 45aba42f Kevin Wolf
    if (nb_needed > nb_available) {
482 45aba42f Kevin Wolf
        nb_needed = nb_available;
483 45aba42f Kevin Wolf
    }
484 45aba42f Kevin Wolf
485 1c46efaa Kevin Wolf
    *cluster_offset = 0;
486 45aba42f Kevin Wolf
487 45aba42f Kevin Wolf
    /* seek the the l2 offset in the l1 table */
488 45aba42f Kevin Wolf
489 45aba42f Kevin Wolf
    l1_index = offset >> l1_bits;
490 45aba42f Kevin Wolf
    if (l1_index >= s->l1_size)
491 45aba42f Kevin Wolf
        goto out;
492 45aba42f Kevin Wolf
493 45aba42f Kevin Wolf
    l2_offset = s->l1_table[l1_index];
494 45aba42f Kevin Wolf
495 45aba42f Kevin Wolf
    /* seek the l2 table of the given l2 offset */
496 45aba42f Kevin Wolf
497 45aba42f Kevin Wolf
    if (!l2_offset)
498 45aba42f Kevin Wolf
        goto out;
499 45aba42f Kevin Wolf
500 45aba42f Kevin Wolf
    /* load the l2 table in memory */
501 45aba42f Kevin Wolf
502 45aba42f Kevin Wolf
    l2_offset &= ~QCOW_OFLAG_COPIED;
503 55c17e98 Kevin Wolf
    ret = l2_load(bs, l2_offset, &l2_table);
504 55c17e98 Kevin Wolf
    if (ret < 0) {
505 55c17e98 Kevin Wolf
        return ret;
506 1c46efaa Kevin Wolf
    }
507 45aba42f Kevin Wolf
508 45aba42f Kevin Wolf
    /* find the cluster offset for the given disk offset */
509 45aba42f Kevin Wolf
510 45aba42f Kevin Wolf
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
511 1c46efaa Kevin Wolf
    *cluster_offset = be64_to_cpu(l2_table[l2_index]);
512 45aba42f Kevin Wolf
    nb_clusters = size_to_clusters(s, nb_needed << 9);
513 45aba42f Kevin Wolf
514 1c46efaa Kevin Wolf
    if (!*cluster_offset) {
515 45aba42f Kevin Wolf
        /* how many empty clusters ? */
516 45aba42f Kevin Wolf
        c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
517 45aba42f Kevin Wolf
    } else {
518 45aba42f Kevin Wolf
        /* how many allocated clusters ? */
519 45aba42f Kevin Wolf
        c = count_contiguous_clusters(nb_clusters, s->cluster_size,
520 45aba42f Kevin Wolf
                &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
521 45aba42f Kevin Wolf
    }
522 45aba42f Kevin Wolf
523 45aba42f Kevin Wolf
   nb_available = (c * s->cluster_sectors);
524 45aba42f Kevin Wolf
out:
525 45aba42f Kevin Wolf
    if (nb_available > nb_needed)
526 45aba42f Kevin Wolf
        nb_available = nb_needed;
527 45aba42f Kevin Wolf
528 45aba42f Kevin Wolf
    *num = nb_available - index_in_cluster;
529 45aba42f Kevin Wolf
530 1c46efaa Kevin Wolf
    *cluster_offset &=~QCOW_OFLAG_COPIED;
531 1c46efaa Kevin Wolf
    return 0;
532 45aba42f Kevin Wolf
}
533 45aba42f Kevin Wolf
534 45aba42f Kevin Wolf
/*
535 45aba42f Kevin Wolf
 * get_cluster_table
536 45aba42f Kevin Wolf
 *
537 45aba42f Kevin Wolf
 * for a given disk offset, load (and allocate if needed)
538 45aba42f Kevin Wolf
 * the l2 table.
539 45aba42f Kevin Wolf
 *
540 45aba42f Kevin Wolf
 * the l2 table offset in the qcow2 file and the cluster index
541 45aba42f Kevin Wolf
 * in the l2 table are given to the caller.
542 45aba42f Kevin Wolf
 *
543 1e3e8f1a Kevin Wolf
 * Returns 0 on success, -errno in failure case
544 45aba42f Kevin Wolf
 */
545 45aba42f Kevin Wolf
static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
546 45aba42f Kevin Wolf
                             uint64_t **new_l2_table,
547 45aba42f Kevin Wolf
                             uint64_t *new_l2_offset,
548 45aba42f Kevin Wolf
                             int *new_l2_index)
549 45aba42f Kevin Wolf
{
550 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
551 80ee15a6 Kevin Wolf
    unsigned int l1_index, l2_index;
552 c46e1167 Kevin Wolf
    uint64_t l2_offset;
553 c46e1167 Kevin Wolf
    uint64_t *l2_table = NULL;
554 80ee15a6 Kevin Wolf
    int ret;
555 45aba42f Kevin Wolf
556 45aba42f Kevin Wolf
    /* seek the the l2 offset in the l1 table */
557 45aba42f Kevin Wolf
558 45aba42f Kevin Wolf
    l1_index = offset >> (s->l2_bits + s->cluster_bits);
559 45aba42f Kevin Wolf
    if (l1_index >= s->l1_size) {
560 72893756 Stefan Hajnoczi
        ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
561 1e3e8f1a Kevin Wolf
        if (ret < 0) {
562 1e3e8f1a Kevin Wolf
            return ret;
563 1e3e8f1a Kevin Wolf
        }
564 45aba42f Kevin Wolf
    }
565 45aba42f Kevin Wolf
    l2_offset = s->l1_table[l1_index];
566 45aba42f Kevin Wolf
567 45aba42f Kevin Wolf
    /* seek the l2 table of the given l2 offset */
568 45aba42f Kevin Wolf
569 45aba42f Kevin Wolf
    if (l2_offset & QCOW_OFLAG_COPIED) {
570 45aba42f Kevin Wolf
        /* load the l2 table in memory */
571 45aba42f Kevin Wolf
        l2_offset &= ~QCOW_OFLAG_COPIED;
572 55c17e98 Kevin Wolf
        ret = l2_load(bs, l2_offset, &l2_table);
573 55c17e98 Kevin Wolf
        if (ret < 0) {
574 55c17e98 Kevin Wolf
            return ret;
575 1e3e8f1a Kevin Wolf
        }
576 45aba42f Kevin Wolf
    } else {
577 45aba42f Kevin Wolf
        if (l2_offset)
578 ed6ccf0f Kevin Wolf
            qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
579 c46e1167 Kevin Wolf
        ret = l2_allocate(bs, l1_index, &l2_table);
580 c46e1167 Kevin Wolf
        if (ret < 0) {
581 c46e1167 Kevin Wolf
            return ret;
582 1e3e8f1a Kevin Wolf
        }
583 45aba42f Kevin Wolf
        l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
584 45aba42f Kevin Wolf
    }
585 45aba42f Kevin Wolf
586 45aba42f Kevin Wolf
    /* find the cluster offset for the given disk offset */
587 45aba42f Kevin Wolf
588 45aba42f Kevin Wolf
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
589 45aba42f Kevin Wolf
590 45aba42f Kevin Wolf
    *new_l2_table = l2_table;
591 45aba42f Kevin Wolf
    *new_l2_offset = l2_offset;
592 45aba42f Kevin Wolf
    *new_l2_index = l2_index;
593 45aba42f Kevin Wolf
594 1e3e8f1a Kevin Wolf
    return 0;
595 45aba42f Kevin Wolf
}
596 45aba42f Kevin Wolf
597 45aba42f Kevin Wolf
/*
598 45aba42f Kevin Wolf
 * alloc_compressed_cluster_offset
599 45aba42f Kevin Wolf
 *
600 45aba42f Kevin Wolf
 * For a given offset of the disk image, return cluster offset in
601 45aba42f Kevin Wolf
 * qcow2 file.
602 45aba42f Kevin Wolf
 *
603 45aba42f Kevin Wolf
 * If the offset is not found, allocate a new compressed cluster.
604 45aba42f Kevin Wolf
 *
605 45aba42f Kevin Wolf
 * Return the cluster offset if successful,
606 45aba42f Kevin Wolf
 * Return 0, otherwise.
607 45aba42f Kevin Wolf
 *
608 45aba42f Kevin Wolf
 */
609 45aba42f Kevin Wolf
610 ed6ccf0f Kevin Wolf
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
611 ed6ccf0f Kevin Wolf
                                               uint64_t offset,
612 ed6ccf0f Kevin Wolf
                                               int compressed_size)
613 45aba42f Kevin Wolf
{
614 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
615 45aba42f Kevin Wolf
    int l2_index, ret;
616 f4f0d391 Kevin Wolf
    uint64_t l2_offset, *l2_table;
617 f4f0d391 Kevin Wolf
    int64_t cluster_offset;
618 45aba42f Kevin Wolf
    int nb_csectors;
619 45aba42f Kevin Wolf
620 45aba42f Kevin Wolf
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
621 1e3e8f1a Kevin Wolf
    if (ret < 0) {
622 45aba42f Kevin Wolf
        return 0;
623 1e3e8f1a Kevin Wolf
    }
624 45aba42f Kevin Wolf
625 45aba42f Kevin Wolf
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
626 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COPIED)
627 45aba42f Kevin Wolf
        return cluster_offset & ~QCOW_OFLAG_COPIED;
628 45aba42f Kevin Wolf
629 45aba42f Kevin Wolf
    if (cluster_offset)
630 ed6ccf0f Kevin Wolf
        qcow2_free_any_clusters(bs, cluster_offset, 1);
631 45aba42f Kevin Wolf
632 ed6ccf0f Kevin Wolf
    cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
633 5d757b56 Kevin Wolf
    if (cluster_offset < 0) {
634 5d757b56 Kevin Wolf
        return 0;
635 5d757b56 Kevin Wolf
    }
636 5d757b56 Kevin Wolf
637 45aba42f Kevin Wolf
    nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
638 45aba42f Kevin Wolf
                  (cluster_offset >> 9);
639 45aba42f Kevin Wolf
640 45aba42f Kevin Wolf
    cluster_offset |= QCOW_OFLAG_COMPRESSED |
641 45aba42f Kevin Wolf
                      ((uint64_t)nb_csectors << s->csize_shift);
642 45aba42f Kevin Wolf
643 45aba42f Kevin Wolf
    /* update L2 table */
644 45aba42f Kevin Wolf
645 45aba42f Kevin Wolf
    /* compressed clusters never have the copied flag */
646 45aba42f Kevin Wolf
647 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
648 45aba42f Kevin Wolf
    l2_table[l2_index] = cpu_to_be64(cluster_offset);
649 8b3b7206 Kevin Wolf
    if (bdrv_pwrite_sync(bs->file,
650 45aba42f Kevin Wolf
                    l2_offset + l2_index * sizeof(uint64_t),
651 45aba42f Kevin Wolf
                    l2_table + l2_index,
652 8b3b7206 Kevin Wolf
                    sizeof(uint64_t)) < 0)
653 45aba42f Kevin Wolf
        return 0;
654 45aba42f Kevin Wolf
655 45aba42f Kevin Wolf
    return cluster_offset;
656 45aba42f Kevin Wolf
}
657 45aba42f Kevin Wolf
658 4c1612d9 Kevin Wolf
/*
659 4c1612d9 Kevin Wolf
 * Write L2 table updates to disk, writing whole sectors to avoid a
660 4c1612d9 Kevin Wolf
 * read-modify-write in bdrv_pwrite
661 4c1612d9 Kevin Wolf
 */
662 4c1612d9 Kevin Wolf
#define L2_ENTRIES_PER_SECTOR (512 / 8)
663 66f82cee Kevin Wolf
static int write_l2_entries(BlockDriverState *bs, uint64_t *l2_table,
664 4c1612d9 Kevin Wolf
    uint64_t l2_offset, int l2_index, int num)
665 4c1612d9 Kevin Wolf
{
666 4c1612d9 Kevin Wolf
    int l2_start_index = l2_index & ~(L1_ENTRIES_PER_SECTOR - 1);
667 4c1612d9 Kevin Wolf
    int start_offset = (8 * l2_index) & ~511;
668 4c1612d9 Kevin Wolf
    int end_offset = (8 * (l2_index + num) + 511) & ~511;
669 4c1612d9 Kevin Wolf
    size_t len = end_offset - start_offset;
670 79a31189 Kevin Wolf
    int ret;
671 4c1612d9 Kevin Wolf
672 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
673 7ec5e6a4 Kevin Wolf
    ret = bdrv_pwrite(bs->file, l2_offset + start_offset,
674 79a31189 Kevin Wolf
        &l2_table[l2_start_index], len);
675 79a31189 Kevin Wolf
    if (ret < 0) {
676 79a31189 Kevin Wolf
        return ret;
677 4c1612d9 Kevin Wolf
    }
678 4c1612d9 Kevin Wolf
679 4c1612d9 Kevin Wolf
    return 0;
680 4c1612d9 Kevin Wolf
}
681 4c1612d9 Kevin Wolf
682 148da7ea Kevin Wolf
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
683 45aba42f Kevin Wolf
{
684 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
685 45aba42f Kevin Wolf
    int i, j = 0, l2_index, ret;
686 45aba42f Kevin Wolf
    uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
687 148da7ea Kevin Wolf
    uint64_t cluster_offset = m->cluster_offset;
688 45aba42f Kevin Wolf
689 45aba42f Kevin Wolf
    if (m->nb_clusters == 0)
690 45aba42f Kevin Wolf
        return 0;
691 45aba42f Kevin Wolf
692 45aba42f Kevin Wolf
    old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
693 45aba42f Kevin Wolf
694 45aba42f Kevin Wolf
    /* copy content of unmodified sectors */
695 45aba42f Kevin Wolf
    start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
696 45aba42f Kevin Wolf
    if (m->n_start) {
697 45aba42f Kevin Wolf
        ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
698 45aba42f Kevin Wolf
        if (ret < 0)
699 45aba42f Kevin Wolf
            goto err;
700 45aba42f Kevin Wolf
    }
701 45aba42f Kevin Wolf
702 45aba42f Kevin Wolf
    if (m->nb_available & (s->cluster_sectors - 1)) {
703 45aba42f Kevin Wolf
        uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
704 45aba42f Kevin Wolf
        ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
705 45aba42f Kevin Wolf
                m->nb_available - end, s->cluster_sectors);
706 45aba42f Kevin Wolf
        if (ret < 0)
707 45aba42f Kevin Wolf
            goto err;
708 45aba42f Kevin Wolf
    }
709 45aba42f Kevin Wolf
710 45aba42f Kevin Wolf
    /* update L2 table */
711 1e3e8f1a Kevin Wolf
    ret = get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index);
712 1e3e8f1a Kevin Wolf
    if (ret < 0) {
713 45aba42f Kevin Wolf
        goto err;
714 1e3e8f1a Kevin Wolf
    }
715 45aba42f Kevin Wolf
716 45aba42f Kevin Wolf
    for (i = 0; i < m->nb_clusters; i++) {
717 45aba42f Kevin Wolf
        /* if two concurrent writes happen to the same unallocated cluster
718 45aba42f Kevin Wolf
         * each write allocates separate cluster and writes data concurrently.
719 45aba42f Kevin Wolf
         * The first one to complete updates l2 table with pointer to its
720 45aba42f Kevin Wolf
         * cluster the second one has to do RMW (which is done above by
721 45aba42f Kevin Wolf
         * copy_sectors()), update l2 table with its cluster pointer and free
722 45aba42f Kevin Wolf
         * old cluster. This is what this loop does */
723 45aba42f Kevin Wolf
        if(l2_table[l2_index + i] != 0)
724 45aba42f Kevin Wolf
            old_cluster[j++] = l2_table[l2_index + i];
725 45aba42f Kevin Wolf
726 45aba42f Kevin Wolf
        l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
727 45aba42f Kevin Wolf
                    (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
728 45aba42f Kevin Wolf
     }
729 45aba42f Kevin Wolf
730 9f8e668e Kevin Wolf
    /*
731 9f8e668e Kevin Wolf
     * Before we update the L2 table to actually point to the new cluster, we
732 9f8e668e Kevin Wolf
     * need to be sure that the refcounts have been increased and COW was
733 9f8e668e Kevin Wolf
     * handled.
734 9f8e668e Kevin Wolf
     */
735 9f8e668e Kevin Wolf
    bdrv_flush(bs->file);
736 9f8e668e Kevin Wolf
737 66f82cee Kevin Wolf
    ret = write_l2_entries(bs, l2_table, l2_offset, l2_index, m->nb_clusters);
738 c835d00f Kevin Wolf
    if (ret < 0) {
739 1b7c801b Kevin Wolf
        qcow2_l2_cache_reset(bs);
740 45aba42f Kevin Wolf
        goto err;
741 4c1612d9 Kevin Wolf
    }
742 45aba42f Kevin Wolf
743 7ec5e6a4 Kevin Wolf
    /*
744 7ec5e6a4 Kevin Wolf
     * If this was a COW, we need to decrease the refcount of the old cluster.
745 7ec5e6a4 Kevin Wolf
     * Also flush bs->file to get the right order for L2 and refcount update.
746 7ec5e6a4 Kevin Wolf
     */
747 7ec5e6a4 Kevin Wolf
    if (j != 0) {
748 7ec5e6a4 Kevin Wolf
        bdrv_flush(bs->file);
749 7ec5e6a4 Kevin Wolf
        for (i = 0; i < j; i++) {
750 7ec5e6a4 Kevin Wolf
            qcow2_free_any_clusters(bs,
751 7ec5e6a4 Kevin Wolf
                be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
752 7ec5e6a4 Kevin Wolf
        }
753 7ec5e6a4 Kevin Wolf
    }
754 45aba42f Kevin Wolf
755 45aba42f Kevin Wolf
    ret = 0;
756 45aba42f Kevin Wolf
err:
757 45aba42f Kevin Wolf
    qemu_free(old_cluster);
758 45aba42f Kevin Wolf
    return ret;
759 45aba42f Kevin Wolf
 }
760 45aba42f Kevin Wolf
761 45aba42f Kevin Wolf
/*
762 45aba42f Kevin Wolf
 * alloc_cluster_offset
763 45aba42f Kevin Wolf
 *
764 148da7ea Kevin Wolf
 * For a given offset of the disk image, return cluster offset in qcow2 file.
765 45aba42f Kevin Wolf
 * If the offset is not found, allocate a new cluster.
766 45aba42f Kevin Wolf
 *
767 148da7ea Kevin Wolf
 * If the cluster was already allocated, m->nb_clusters is set to 0,
768 148da7ea Kevin Wolf
 * m->depends_on is set to NULL and the other fields in m are meaningless.
769 148da7ea Kevin Wolf
 *
770 148da7ea Kevin Wolf
 * If the cluster is newly allocated, m->nb_clusters is set to the number of
771 148da7ea Kevin Wolf
 * contiguous clusters that have been allocated. This may be 0 if the request
772 148da7ea Kevin Wolf
 * conflict with another write request in flight; in this case, m->depends_on
773 148da7ea Kevin Wolf
 * is set and the remaining fields of m are meaningless.
774 45aba42f Kevin Wolf
 *
775 148da7ea Kevin Wolf
 * If m->nb_clusters is non-zero, the other fields of m are valid and contain
776 148da7ea Kevin Wolf
 * information about the first allocated cluster.
777 148da7ea Kevin Wolf
 *
778 148da7ea Kevin Wolf
 * Return 0 on success and -errno in error cases
779 45aba42f Kevin Wolf
 */
780 f4f0d391 Kevin Wolf
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
781 f4f0d391 Kevin Wolf
    int n_start, int n_end, int *num, QCowL2Meta *m)
782 45aba42f Kevin Wolf
{
783 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
784 45aba42f Kevin Wolf
    int l2_index, ret;
785 5d757b56 Kevin Wolf
    uint64_t l2_offset, *l2_table;
786 5d757b56 Kevin Wolf
    int64_t cluster_offset;
787 80ee15a6 Kevin Wolf
    unsigned int nb_clusters, i = 0;
788 f214978a Kevin Wolf
    QCowL2Meta *old_alloc;
789 45aba42f Kevin Wolf
790 45aba42f Kevin Wolf
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
791 1e3e8f1a Kevin Wolf
    if (ret < 0) {
792 148da7ea Kevin Wolf
        return ret;
793 1e3e8f1a Kevin Wolf
    }
794 45aba42f Kevin Wolf
795 45aba42f Kevin Wolf
    nb_clusters = size_to_clusters(s, n_end << 9);
796 45aba42f Kevin Wolf
797 45aba42f Kevin Wolf
    nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
798 45aba42f Kevin Wolf
799 45aba42f Kevin Wolf
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
800 45aba42f Kevin Wolf
801 45aba42f Kevin Wolf
    /* We keep all QCOW_OFLAG_COPIED clusters */
802 45aba42f Kevin Wolf
803 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COPIED) {
804 45aba42f Kevin Wolf
        nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
805 45aba42f Kevin Wolf
                &l2_table[l2_index], 0, 0);
806 45aba42f Kevin Wolf
807 45aba42f Kevin Wolf
        cluster_offset &= ~QCOW_OFLAG_COPIED;
808 45aba42f Kevin Wolf
        m->nb_clusters = 0;
809 148da7ea Kevin Wolf
        m->depends_on = NULL;
810 45aba42f Kevin Wolf
811 45aba42f Kevin Wolf
        goto out;
812 45aba42f Kevin Wolf
    }
813 45aba42f Kevin Wolf
814 45aba42f Kevin Wolf
    /* for the moment, multiple compressed clusters are not managed */
815 45aba42f Kevin Wolf
816 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COMPRESSED)
817 45aba42f Kevin Wolf
        nb_clusters = 1;
818 45aba42f Kevin Wolf
819 45aba42f Kevin Wolf
    /* how many available clusters ? */
820 45aba42f Kevin Wolf
821 45aba42f Kevin Wolf
    while (i < nb_clusters) {
822 45aba42f Kevin Wolf
        i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
823 45aba42f Kevin Wolf
                &l2_table[l2_index], i, 0);
824 4805bb66 Kevin Wolf
        if ((i >= nb_clusters) || be64_to_cpu(l2_table[l2_index + i])) {
825 45aba42f Kevin Wolf
            break;
826 4805bb66 Kevin Wolf
        }
827 45aba42f Kevin Wolf
828 45aba42f Kevin Wolf
        i += count_contiguous_free_clusters(nb_clusters - i,
829 45aba42f Kevin Wolf
                &l2_table[l2_index + i]);
830 4805bb66 Kevin Wolf
        if (i >= nb_clusters) {
831 4805bb66 Kevin Wolf
            break;
832 4805bb66 Kevin Wolf
        }
833 45aba42f Kevin Wolf
834 45aba42f Kevin Wolf
        cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
835 45aba42f Kevin Wolf
836 45aba42f Kevin Wolf
        if ((cluster_offset & QCOW_OFLAG_COPIED) ||
837 45aba42f Kevin Wolf
                (cluster_offset & QCOW_OFLAG_COMPRESSED))
838 45aba42f Kevin Wolf
            break;
839 45aba42f Kevin Wolf
    }
840 4805bb66 Kevin Wolf
    assert(i <= nb_clusters);
841 45aba42f Kevin Wolf
    nb_clusters = i;
842 45aba42f Kevin Wolf
843 f214978a Kevin Wolf
    /*
844 f214978a Kevin Wolf
     * Check if there already is an AIO write request in flight which allocates
845 f214978a Kevin Wolf
     * the same cluster. In this case we need to wait until the previous
846 f214978a Kevin Wolf
     * request has completed and updated the L2 table accordingly.
847 f214978a Kevin Wolf
     */
848 72cf2d4f Blue Swirl
    QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
849 f214978a Kevin Wolf
850 f214978a Kevin Wolf
        uint64_t end_offset = offset + nb_clusters * s->cluster_size;
851 f214978a Kevin Wolf
        uint64_t old_offset = old_alloc->offset;
852 f214978a Kevin Wolf
        uint64_t old_end_offset = old_alloc->offset +
853 f214978a Kevin Wolf
            old_alloc->nb_clusters * s->cluster_size;
854 f214978a Kevin Wolf
855 f214978a Kevin Wolf
        if (end_offset < old_offset || offset > old_end_offset) {
856 f214978a Kevin Wolf
            /* No intersection */
857 f214978a Kevin Wolf
        } else {
858 f214978a Kevin Wolf
            if (offset < old_offset) {
859 f214978a Kevin Wolf
                /* Stop at the start of a running allocation */
860 f214978a Kevin Wolf
                nb_clusters = (old_offset - offset) >> s->cluster_bits;
861 f214978a Kevin Wolf
            } else {
862 f214978a Kevin Wolf
                nb_clusters = 0;
863 f214978a Kevin Wolf
            }
864 f214978a Kevin Wolf
865 f214978a Kevin Wolf
            if (nb_clusters == 0) {
866 f214978a Kevin Wolf
                /* Set dependency and wait for a callback */
867 f214978a Kevin Wolf
                m->depends_on = old_alloc;
868 f214978a Kevin Wolf
                m->nb_clusters = 0;
869 f214978a Kevin Wolf
                *num = 0;
870 f214978a Kevin Wolf
                return 0;
871 f214978a Kevin Wolf
            }
872 f214978a Kevin Wolf
        }
873 f214978a Kevin Wolf
    }
874 f214978a Kevin Wolf
875 f214978a Kevin Wolf
    if (!nb_clusters) {
876 f214978a Kevin Wolf
        abort();
877 f214978a Kevin Wolf
    }
878 f214978a Kevin Wolf
879 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
880 f214978a Kevin Wolf
881 45aba42f Kevin Wolf
    /* allocate a new cluster */
882 45aba42f Kevin Wolf
883 ed6ccf0f Kevin Wolf
    cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
884 5d757b56 Kevin Wolf
    if (cluster_offset < 0) {
885 c644db3d Kevin Wolf
        QLIST_REMOVE(m, next_in_flight);
886 5d757b56 Kevin Wolf
        return cluster_offset;
887 5d757b56 Kevin Wolf
    }
888 45aba42f Kevin Wolf
889 45aba42f Kevin Wolf
    /* save info needed for meta data update */
890 45aba42f Kevin Wolf
    m->offset = offset;
891 45aba42f Kevin Wolf
    m->n_start = n_start;
892 45aba42f Kevin Wolf
    m->nb_clusters = nb_clusters;
893 45aba42f Kevin Wolf
894 45aba42f Kevin Wolf
out:
895 45aba42f Kevin Wolf
    m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
896 148da7ea Kevin Wolf
    m->cluster_offset = cluster_offset;
897 45aba42f Kevin Wolf
898 45aba42f Kevin Wolf
    *num = m->nb_available - n_start;
899 45aba42f Kevin Wolf
900 148da7ea Kevin Wolf
    return 0;
901 45aba42f Kevin Wolf
}
902 45aba42f Kevin Wolf
903 45aba42f Kevin Wolf
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
904 45aba42f Kevin Wolf
                             const uint8_t *buf, int buf_size)
905 45aba42f Kevin Wolf
{
906 45aba42f Kevin Wolf
    z_stream strm1, *strm = &strm1;
907 45aba42f Kevin Wolf
    int ret, out_len;
908 45aba42f Kevin Wolf
909 45aba42f Kevin Wolf
    memset(strm, 0, sizeof(*strm));
910 45aba42f Kevin Wolf
911 45aba42f Kevin Wolf
    strm->next_in = (uint8_t *)buf;
912 45aba42f Kevin Wolf
    strm->avail_in = buf_size;
913 45aba42f Kevin Wolf
    strm->next_out = out_buf;
914 45aba42f Kevin Wolf
    strm->avail_out = out_buf_size;
915 45aba42f Kevin Wolf
916 45aba42f Kevin Wolf
    ret = inflateInit2(strm, -12);
917 45aba42f Kevin Wolf
    if (ret != Z_OK)
918 45aba42f Kevin Wolf
        return -1;
919 45aba42f Kevin Wolf
    ret = inflate(strm, Z_FINISH);
920 45aba42f Kevin Wolf
    out_len = strm->next_out - out_buf;
921 45aba42f Kevin Wolf
    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
922 45aba42f Kevin Wolf
        out_len != out_buf_size) {
923 45aba42f Kevin Wolf
        inflateEnd(strm);
924 45aba42f Kevin Wolf
        return -1;
925 45aba42f Kevin Wolf
    }
926 45aba42f Kevin Wolf
    inflateEnd(strm);
927 45aba42f Kevin Wolf
    return 0;
928 45aba42f Kevin Wolf
}
929 45aba42f Kevin Wolf
930 66f82cee Kevin Wolf
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
931 45aba42f Kevin Wolf
{
932 66f82cee Kevin Wolf
    BDRVQcowState *s = bs->opaque;
933 45aba42f Kevin Wolf
    int ret, csize, nb_csectors, sector_offset;
934 45aba42f Kevin Wolf
    uint64_t coffset;
935 45aba42f Kevin Wolf
936 45aba42f Kevin Wolf
    coffset = cluster_offset & s->cluster_offset_mask;
937 45aba42f Kevin Wolf
    if (s->cluster_cache_offset != coffset) {
938 45aba42f Kevin Wolf
        nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
939 45aba42f Kevin Wolf
        sector_offset = coffset & 511;
940 45aba42f Kevin Wolf
        csize = nb_csectors * 512 - sector_offset;
941 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
942 66f82cee Kevin Wolf
        ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data, nb_csectors);
943 45aba42f Kevin Wolf
        if (ret < 0) {
944 45aba42f Kevin Wolf
            return -1;
945 45aba42f Kevin Wolf
        }
946 45aba42f Kevin Wolf
        if (decompress_buffer(s->cluster_cache, s->cluster_size,
947 45aba42f Kevin Wolf
                              s->cluster_data + sector_offset, csize) < 0) {
948 45aba42f Kevin Wolf
            return -1;
949 45aba42f Kevin Wolf
        }
950 45aba42f Kevin Wolf
        s->cluster_cache_offset = coffset;
951 45aba42f Kevin Wolf
    }
952 45aba42f Kevin Wolf
    return 0;
953 45aba42f Kevin Wolf
}