Statistics
| Branch: | Revision:

root / block / qcow2-cluster.c @ 1c02e2a1

History | View | Annotate | Download (27.6 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 1c02e2a1 Kevin Wolf
        qcow2_l2_cache_reset(bs);
192 55c17e98 Kevin Wolf
        return ret;
193 55c17e98 Kevin Wolf
    }
194 55c17e98 Kevin Wolf
195 45aba42f Kevin Wolf
    s->l2_cache_offsets[min_index] = l2_offset;
196 45aba42f Kevin Wolf
    s->l2_cache_counts[min_index] = 1;
197 45aba42f Kevin Wolf
198 55c17e98 Kevin Wolf
    return 0;
199 45aba42f Kevin Wolf
}
200 45aba42f Kevin Wolf
201 45aba42f Kevin Wolf
/*
202 6583e3c7 Kevin Wolf
 * Writes one sector of the L1 table to the disk (can't update single entries
203 6583e3c7 Kevin Wolf
 * and we really don't want bdrv_pread to perform a read-modify-write)
204 6583e3c7 Kevin Wolf
 */
205 6583e3c7 Kevin Wolf
#define L1_ENTRIES_PER_SECTOR (512 / 8)
206 66f82cee Kevin Wolf
static int write_l1_entry(BlockDriverState *bs, int l1_index)
207 6583e3c7 Kevin Wolf
{
208 66f82cee Kevin Wolf
    BDRVQcowState *s = bs->opaque;
209 6583e3c7 Kevin Wolf
    uint64_t buf[L1_ENTRIES_PER_SECTOR];
210 6583e3c7 Kevin Wolf
    int l1_start_index;
211 f7defcb6 Kevin Wolf
    int i, ret;
212 6583e3c7 Kevin Wolf
213 6583e3c7 Kevin Wolf
    l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
214 6583e3c7 Kevin Wolf
    for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) {
215 6583e3c7 Kevin Wolf
        buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
216 6583e3c7 Kevin Wolf
    }
217 6583e3c7 Kevin Wolf
218 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
219 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset + 8 * l1_start_index,
220 f7defcb6 Kevin Wolf
        buf, sizeof(buf));
221 f7defcb6 Kevin Wolf
    if (ret < 0) {
222 f7defcb6 Kevin Wolf
        return ret;
223 6583e3c7 Kevin Wolf
    }
224 6583e3c7 Kevin Wolf
225 6583e3c7 Kevin Wolf
    return 0;
226 6583e3c7 Kevin Wolf
}
227 6583e3c7 Kevin Wolf
228 6583e3c7 Kevin Wolf
/*
229 45aba42f Kevin Wolf
 * l2_allocate
230 45aba42f Kevin Wolf
 *
231 45aba42f Kevin Wolf
 * Allocate a new l2 entry in the file. If l1_index points to an already
232 45aba42f Kevin Wolf
 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
233 45aba42f Kevin Wolf
 * table) copy the contents of the old L2 table into the newly allocated one.
234 45aba42f Kevin Wolf
 * Otherwise the new table is initialized with zeros.
235 45aba42f Kevin Wolf
 *
236 45aba42f Kevin Wolf
 */
237 45aba42f Kevin Wolf
238 c46e1167 Kevin Wolf
static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
239 45aba42f Kevin Wolf
{
240 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
241 45aba42f Kevin Wolf
    int min_index;
242 6583e3c7 Kevin Wolf
    uint64_t old_l2_offset;
243 f4f0d391 Kevin Wolf
    uint64_t *l2_table;
244 f4f0d391 Kevin Wolf
    int64_t l2_offset;
245 c46e1167 Kevin Wolf
    int ret;
246 45aba42f Kevin Wolf
247 45aba42f Kevin Wolf
    old_l2_offset = s->l1_table[l1_index];
248 45aba42f Kevin Wolf
249 45aba42f Kevin Wolf
    /* allocate a new l2 entry */
250 45aba42f Kevin Wolf
251 ed6ccf0f Kevin Wolf
    l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
252 5d757b56 Kevin Wolf
    if (l2_offset < 0) {
253 c46e1167 Kevin Wolf
        return l2_offset;
254 5d757b56 Kevin Wolf
    }
255 29216ed1 Kevin Wolf
    bdrv_flush(bs->file);
256 45aba42f Kevin Wolf
257 45aba42f Kevin Wolf
    /* allocate a new entry in the l2 cache */
258 45aba42f Kevin Wolf
259 45aba42f Kevin Wolf
    min_index = l2_cache_new_entry(bs);
260 45aba42f Kevin Wolf
    l2_table = s->l2_cache + (min_index << s->l2_bits);
261 45aba42f Kevin Wolf
262 45aba42f Kevin Wolf
    if (old_l2_offset == 0) {
263 45aba42f Kevin Wolf
        /* if there was no old l2 table, clear the new table */
264 45aba42f Kevin Wolf
        memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
265 45aba42f Kevin Wolf
    } else {
266 45aba42f Kevin Wolf
        /* if there was an old l2 table, read it from the disk */
267 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
268 66f82cee Kevin Wolf
        ret = bdrv_pread(bs->file, old_l2_offset, l2_table,
269 c46e1167 Kevin Wolf
            s->l2_size * sizeof(uint64_t));
270 c46e1167 Kevin Wolf
        if (ret < 0) {
271 175e1152 Kevin Wolf
            goto fail;
272 c46e1167 Kevin Wolf
        }
273 45aba42f Kevin Wolf
    }
274 45aba42f Kevin Wolf
    /* write the l2 table to the file */
275 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
276 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
277 c46e1167 Kevin Wolf
        s->l2_size * sizeof(uint64_t));
278 c46e1167 Kevin Wolf
    if (ret < 0) {
279 175e1152 Kevin Wolf
        goto fail;
280 175e1152 Kevin Wolf
    }
281 175e1152 Kevin Wolf
282 175e1152 Kevin Wolf
    /* update the L1 entry */
283 175e1152 Kevin Wolf
    s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
284 175e1152 Kevin Wolf
    ret = write_l1_entry(bs, l1_index);
285 175e1152 Kevin Wolf
    if (ret < 0) {
286 175e1152 Kevin Wolf
        goto fail;
287 c46e1167 Kevin Wolf
    }
288 45aba42f Kevin Wolf
289 45aba42f Kevin Wolf
    /* update the l2 cache entry */
290 45aba42f Kevin Wolf
291 45aba42f Kevin Wolf
    s->l2_cache_offsets[min_index] = l2_offset;
292 45aba42f Kevin Wolf
    s->l2_cache_counts[min_index] = 1;
293 45aba42f Kevin Wolf
294 c46e1167 Kevin Wolf
    *table = l2_table;
295 c46e1167 Kevin Wolf
    return 0;
296 175e1152 Kevin Wolf
297 175e1152 Kevin Wolf
fail:
298 68dba0bf Kevin Wolf
    s->l1_table[l1_index] = old_l2_offset;
299 175e1152 Kevin Wolf
    qcow2_l2_cache_reset(bs);
300 175e1152 Kevin Wolf
    return ret;
301 45aba42f Kevin Wolf
}
302 45aba42f Kevin Wolf
303 45aba42f Kevin Wolf
static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
304 45aba42f Kevin Wolf
        uint64_t *l2_table, uint64_t start, uint64_t mask)
305 45aba42f Kevin Wolf
{
306 45aba42f Kevin Wolf
    int i;
307 45aba42f Kevin Wolf
    uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
308 45aba42f Kevin Wolf
309 45aba42f Kevin Wolf
    if (!offset)
310 45aba42f Kevin Wolf
        return 0;
311 45aba42f Kevin Wolf
312 45aba42f Kevin Wolf
    for (i = start; i < start + nb_clusters; i++)
313 80ee15a6 Kevin Wolf
        if (offset + (uint64_t) i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
314 45aba42f Kevin Wolf
            break;
315 45aba42f Kevin Wolf
316 45aba42f Kevin Wolf
        return (i - start);
317 45aba42f Kevin Wolf
}
318 45aba42f Kevin Wolf
319 45aba42f Kevin Wolf
static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
320 45aba42f Kevin Wolf
{
321 45aba42f Kevin Wolf
    int i = 0;
322 45aba42f Kevin Wolf
323 45aba42f Kevin Wolf
    while(nb_clusters-- && l2_table[i] == 0)
324 45aba42f Kevin Wolf
        i++;
325 45aba42f Kevin Wolf
326 45aba42f Kevin Wolf
    return i;
327 45aba42f Kevin Wolf
}
328 45aba42f Kevin Wolf
329 45aba42f Kevin Wolf
/* The crypt function is compatible with the linux cryptoloop
330 45aba42f Kevin Wolf
   algorithm for < 4 GB images. NOTE: out_buf == in_buf is
331 45aba42f Kevin Wolf
   supported */
332 ed6ccf0f Kevin Wolf
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
333 ed6ccf0f Kevin Wolf
                           uint8_t *out_buf, const uint8_t *in_buf,
334 ed6ccf0f Kevin Wolf
                           int nb_sectors, int enc,
335 ed6ccf0f Kevin Wolf
                           const AES_KEY *key)
336 45aba42f Kevin Wolf
{
337 45aba42f Kevin Wolf
    union {
338 45aba42f Kevin Wolf
        uint64_t ll[2];
339 45aba42f Kevin Wolf
        uint8_t b[16];
340 45aba42f Kevin Wolf
    } ivec;
341 45aba42f Kevin Wolf
    int i;
342 45aba42f Kevin Wolf
343 45aba42f Kevin Wolf
    for(i = 0; i < nb_sectors; i++) {
344 45aba42f Kevin Wolf
        ivec.ll[0] = cpu_to_le64(sector_num);
345 45aba42f Kevin Wolf
        ivec.ll[1] = 0;
346 45aba42f Kevin Wolf
        AES_cbc_encrypt(in_buf, out_buf, 512, key,
347 45aba42f Kevin Wolf
                        ivec.b, enc);
348 45aba42f Kevin Wolf
        sector_num++;
349 45aba42f Kevin Wolf
        in_buf += 512;
350 45aba42f Kevin Wolf
        out_buf += 512;
351 45aba42f Kevin Wolf
    }
352 45aba42f Kevin Wolf
}
353 45aba42f Kevin Wolf
354 45aba42f Kevin Wolf
355 72ecf02d Kevin Wolf
static int qcow_read(BlockDriverState *bs, int64_t sector_num,
356 72ecf02d Kevin Wolf
                     uint8_t *buf, int nb_sectors)
357 45aba42f Kevin Wolf
{
358 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
359 45aba42f Kevin Wolf
    int ret, index_in_cluster, n, n1;
360 45aba42f Kevin Wolf
    uint64_t cluster_offset;
361 bd28f835 Kevin Wolf
    struct iovec iov;
362 bd28f835 Kevin Wolf
    QEMUIOVector qiov;
363 45aba42f Kevin Wolf
364 45aba42f Kevin Wolf
    while (nb_sectors > 0) {
365 45aba42f Kevin Wolf
        n = nb_sectors;
366 1c46efaa Kevin Wolf
367 1c46efaa Kevin Wolf
        ret = qcow2_get_cluster_offset(bs, sector_num << 9, &n,
368 1c46efaa Kevin Wolf
            &cluster_offset);
369 1c46efaa Kevin Wolf
        if (ret < 0) {
370 1c46efaa Kevin Wolf
            return ret;
371 1c46efaa Kevin Wolf
        }
372 1c46efaa Kevin Wolf
373 45aba42f Kevin Wolf
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
374 45aba42f Kevin Wolf
        if (!cluster_offset) {
375 45aba42f Kevin Wolf
            if (bs->backing_hd) {
376 45aba42f Kevin Wolf
                /* read from the base image */
377 bd28f835 Kevin Wolf
                iov.iov_base = buf;
378 bd28f835 Kevin Wolf
                iov.iov_len = n * 512;
379 bd28f835 Kevin Wolf
                qemu_iovec_init_external(&qiov, &iov, 1);
380 bd28f835 Kevin Wolf
381 bd28f835 Kevin Wolf
                n1 = qcow2_backing_read1(bs->backing_hd, &qiov, sector_num, n);
382 45aba42f Kevin Wolf
                if (n1 > 0) {
383 66f82cee Kevin Wolf
                    BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING);
384 45aba42f Kevin Wolf
                    ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
385 45aba42f Kevin Wolf
                    if (ret < 0)
386 45aba42f Kevin Wolf
                        return -1;
387 45aba42f Kevin Wolf
                }
388 45aba42f Kevin Wolf
            } else {
389 45aba42f Kevin Wolf
                memset(buf, 0, 512 * n);
390 45aba42f Kevin Wolf
            }
391 45aba42f Kevin Wolf
        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
392 66f82cee Kevin Wolf
            if (qcow2_decompress_cluster(bs, cluster_offset) < 0)
393 45aba42f Kevin Wolf
                return -1;
394 45aba42f Kevin Wolf
            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
395 45aba42f Kevin Wolf
        } else {
396 66f82cee Kevin Wolf
            BLKDBG_EVENT(bs->file, BLKDBG_READ);
397 66f82cee Kevin Wolf
            ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);
398 45aba42f Kevin Wolf
            if (ret != n * 512)
399 45aba42f Kevin Wolf
                return -1;
400 45aba42f Kevin Wolf
            if (s->crypt_method) {
401 ed6ccf0f Kevin Wolf
                qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
402 45aba42f Kevin Wolf
                                &s->aes_decrypt_key);
403 45aba42f Kevin Wolf
            }
404 45aba42f Kevin Wolf
        }
405 45aba42f Kevin Wolf
        nb_sectors -= n;
406 45aba42f Kevin Wolf
        sector_num += n;
407 45aba42f Kevin Wolf
        buf += n * 512;
408 45aba42f Kevin Wolf
    }
409 45aba42f Kevin Wolf
    return 0;
410 45aba42f Kevin Wolf
}
411 45aba42f Kevin Wolf
412 45aba42f Kevin Wolf
static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
413 45aba42f Kevin Wolf
                        uint64_t cluster_offset, int n_start, int n_end)
414 45aba42f Kevin Wolf
{
415 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
416 45aba42f Kevin Wolf
    int n, ret;
417 45aba42f Kevin Wolf
418 45aba42f Kevin Wolf
    n = n_end - n_start;
419 45aba42f Kevin Wolf
    if (n <= 0)
420 45aba42f Kevin Wolf
        return 0;
421 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
422 72ecf02d Kevin Wolf
    ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
423 45aba42f Kevin Wolf
    if (ret < 0)
424 45aba42f Kevin Wolf
        return ret;
425 45aba42f Kevin Wolf
    if (s->crypt_method) {
426 ed6ccf0f Kevin Wolf
        qcow2_encrypt_sectors(s, start_sect + n_start,
427 45aba42f Kevin Wolf
                        s->cluster_data,
428 45aba42f Kevin Wolf
                        s->cluster_data, n, 1,
429 45aba42f Kevin Wolf
                        &s->aes_encrypt_key);
430 45aba42f Kevin Wolf
    }
431 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
432 9f8e668e Kevin Wolf
    ret = bdrv_write(bs->file, (cluster_offset >> 9) + n_start,
433 8b3b7206 Kevin Wolf
        s->cluster_data, n);
434 45aba42f Kevin Wolf
    if (ret < 0)
435 45aba42f Kevin Wolf
        return ret;
436 45aba42f Kevin Wolf
    return 0;
437 45aba42f Kevin Wolf
}
438 45aba42f Kevin Wolf
439 45aba42f Kevin Wolf
440 45aba42f Kevin Wolf
/*
441 45aba42f Kevin Wolf
 * get_cluster_offset
442 45aba42f Kevin Wolf
 *
443 1c46efaa Kevin Wolf
 * For a given offset of the disk image, find the cluster offset in
444 1c46efaa Kevin Wolf
 * qcow2 file. The offset is stored in *cluster_offset.
445 45aba42f Kevin Wolf
 *
446 45aba42f Kevin Wolf
 * on entry, *num is the number of contiguous clusters we'd like to
447 45aba42f Kevin Wolf
 * access following offset.
448 45aba42f Kevin Wolf
 *
449 45aba42f Kevin Wolf
 * on exit, *num is the number of contiguous clusters we can read.
450 45aba42f Kevin Wolf
 *
451 1c46efaa Kevin Wolf
 * Return 0, if the offset is found
452 1c46efaa Kevin Wolf
 * Return -errno, otherwise.
453 45aba42f Kevin Wolf
 *
454 45aba42f Kevin Wolf
 */
455 45aba42f Kevin Wolf
456 1c46efaa Kevin Wolf
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
457 1c46efaa Kevin Wolf
    int *num, uint64_t *cluster_offset)
458 45aba42f Kevin Wolf
{
459 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
460 80ee15a6 Kevin Wolf
    unsigned int l1_index, l2_index;
461 1c46efaa Kevin Wolf
    uint64_t l2_offset, *l2_table;
462 45aba42f Kevin Wolf
    int l1_bits, c;
463 80ee15a6 Kevin Wolf
    unsigned int index_in_cluster, nb_clusters;
464 80ee15a6 Kevin Wolf
    uint64_t nb_available, nb_needed;
465 55c17e98 Kevin Wolf
    int ret;
466 45aba42f Kevin Wolf
467 45aba42f Kevin Wolf
    index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
468 45aba42f Kevin Wolf
    nb_needed = *num + index_in_cluster;
469 45aba42f Kevin Wolf
470 45aba42f Kevin Wolf
    l1_bits = s->l2_bits + s->cluster_bits;
471 45aba42f Kevin Wolf
472 45aba42f Kevin Wolf
    /* compute how many bytes there are between the offset and
473 45aba42f Kevin Wolf
     * the end of the l1 entry
474 45aba42f Kevin Wolf
     */
475 45aba42f Kevin Wolf
476 80ee15a6 Kevin Wolf
    nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
477 45aba42f Kevin Wolf
478 45aba42f Kevin Wolf
    /* compute the number of available sectors */
479 45aba42f Kevin Wolf
480 45aba42f Kevin Wolf
    nb_available = (nb_available >> 9) + index_in_cluster;
481 45aba42f Kevin Wolf
482 45aba42f Kevin Wolf
    if (nb_needed > nb_available) {
483 45aba42f Kevin Wolf
        nb_needed = nb_available;
484 45aba42f Kevin Wolf
    }
485 45aba42f Kevin Wolf
486 1c46efaa Kevin Wolf
    *cluster_offset = 0;
487 45aba42f Kevin Wolf
488 45aba42f Kevin Wolf
    /* seek the the l2 offset in the l1 table */
489 45aba42f Kevin Wolf
490 45aba42f Kevin Wolf
    l1_index = offset >> l1_bits;
491 45aba42f Kevin Wolf
    if (l1_index >= s->l1_size)
492 45aba42f Kevin Wolf
        goto out;
493 45aba42f Kevin Wolf
494 45aba42f Kevin Wolf
    l2_offset = s->l1_table[l1_index];
495 45aba42f Kevin Wolf
496 45aba42f Kevin Wolf
    /* seek the l2 table of the given l2 offset */
497 45aba42f Kevin Wolf
498 45aba42f Kevin Wolf
    if (!l2_offset)
499 45aba42f Kevin Wolf
        goto out;
500 45aba42f Kevin Wolf
501 45aba42f Kevin Wolf
    /* load the l2 table in memory */
502 45aba42f Kevin Wolf
503 45aba42f Kevin Wolf
    l2_offset &= ~QCOW_OFLAG_COPIED;
504 55c17e98 Kevin Wolf
    ret = l2_load(bs, l2_offset, &l2_table);
505 55c17e98 Kevin Wolf
    if (ret < 0) {
506 55c17e98 Kevin Wolf
        return ret;
507 1c46efaa Kevin Wolf
    }
508 45aba42f Kevin Wolf
509 45aba42f Kevin Wolf
    /* find the cluster offset for the given disk offset */
510 45aba42f Kevin Wolf
511 45aba42f Kevin Wolf
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
512 1c46efaa Kevin Wolf
    *cluster_offset = be64_to_cpu(l2_table[l2_index]);
513 45aba42f Kevin Wolf
    nb_clusters = size_to_clusters(s, nb_needed << 9);
514 45aba42f Kevin Wolf
515 1c46efaa Kevin Wolf
    if (!*cluster_offset) {
516 45aba42f Kevin Wolf
        /* how many empty clusters ? */
517 45aba42f Kevin Wolf
        c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
518 45aba42f Kevin Wolf
    } else {
519 45aba42f Kevin Wolf
        /* how many allocated clusters ? */
520 45aba42f Kevin Wolf
        c = count_contiguous_clusters(nb_clusters, s->cluster_size,
521 45aba42f Kevin Wolf
                &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
522 45aba42f Kevin Wolf
    }
523 45aba42f Kevin Wolf
524 45aba42f Kevin Wolf
   nb_available = (c * s->cluster_sectors);
525 45aba42f Kevin Wolf
out:
526 45aba42f Kevin Wolf
    if (nb_available > nb_needed)
527 45aba42f Kevin Wolf
        nb_available = nb_needed;
528 45aba42f Kevin Wolf
529 45aba42f Kevin Wolf
    *num = nb_available - index_in_cluster;
530 45aba42f Kevin Wolf
531 1c46efaa Kevin Wolf
    *cluster_offset &=~QCOW_OFLAG_COPIED;
532 1c46efaa Kevin Wolf
    return 0;
533 45aba42f Kevin Wolf
}
534 45aba42f Kevin Wolf
535 45aba42f Kevin Wolf
/*
536 45aba42f Kevin Wolf
 * get_cluster_table
537 45aba42f Kevin Wolf
 *
538 45aba42f Kevin Wolf
 * for a given disk offset, load (and allocate if needed)
539 45aba42f Kevin Wolf
 * the l2 table.
540 45aba42f Kevin Wolf
 *
541 45aba42f Kevin Wolf
 * the l2 table offset in the qcow2 file and the cluster index
542 45aba42f Kevin Wolf
 * in the l2 table are given to the caller.
543 45aba42f Kevin Wolf
 *
544 1e3e8f1a Kevin Wolf
 * Returns 0 on success, -errno in failure case
545 45aba42f Kevin Wolf
 */
546 45aba42f Kevin Wolf
static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
547 45aba42f Kevin Wolf
                             uint64_t **new_l2_table,
548 45aba42f Kevin Wolf
                             uint64_t *new_l2_offset,
549 45aba42f Kevin Wolf
                             int *new_l2_index)
550 45aba42f Kevin Wolf
{
551 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
552 80ee15a6 Kevin Wolf
    unsigned int l1_index, l2_index;
553 c46e1167 Kevin Wolf
    uint64_t l2_offset;
554 c46e1167 Kevin Wolf
    uint64_t *l2_table = NULL;
555 80ee15a6 Kevin Wolf
    int ret;
556 45aba42f Kevin Wolf
557 45aba42f Kevin Wolf
    /* seek the the l2 offset in the l1 table */
558 45aba42f Kevin Wolf
559 45aba42f Kevin Wolf
    l1_index = offset >> (s->l2_bits + s->cluster_bits);
560 45aba42f Kevin Wolf
    if (l1_index >= s->l1_size) {
561 72893756 Stefan Hajnoczi
        ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
562 1e3e8f1a Kevin Wolf
        if (ret < 0) {
563 1e3e8f1a Kevin Wolf
            return ret;
564 1e3e8f1a Kevin Wolf
        }
565 45aba42f Kevin Wolf
    }
566 45aba42f Kevin Wolf
    l2_offset = s->l1_table[l1_index];
567 45aba42f Kevin Wolf
568 45aba42f Kevin Wolf
    /* seek the l2 table of the given l2 offset */
569 45aba42f Kevin Wolf
570 45aba42f Kevin Wolf
    if (l2_offset & QCOW_OFLAG_COPIED) {
571 45aba42f Kevin Wolf
        /* load the l2 table in memory */
572 45aba42f Kevin Wolf
        l2_offset &= ~QCOW_OFLAG_COPIED;
573 55c17e98 Kevin Wolf
        ret = l2_load(bs, l2_offset, &l2_table);
574 55c17e98 Kevin Wolf
        if (ret < 0) {
575 55c17e98 Kevin Wolf
            return ret;
576 1e3e8f1a Kevin Wolf
        }
577 45aba42f Kevin Wolf
    } else {
578 45aba42f Kevin Wolf
        if (l2_offset)
579 ed6ccf0f Kevin Wolf
            qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
580 c46e1167 Kevin Wolf
        ret = l2_allocate(bs, l1_index, &l2_table);
581 c46e1167 Kevin Wolf
        if (ret < 0) {
582 c46e1167 Kevin Wolf
            return ret;
583 1e3e8f1a Kevin Wolf
        }
584 45aba42f Kevin Wolf
        l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
585 45aba42f Kevin Wolf
    }
586 45aba42f Kevin Wolf
587 45aba42f Kevin Wolf
    /* find the cluster offset for the given disk offset */
588 45aba42f Kevin Wolf
589 45aba42f Kevin Wolf
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
590 45aba42f Kevin Wolf
591 45aba42f Kevin Wolf
    *new_l2_table = l2_table;
592 45aba42f Kevin Wolf
    *new_l2_offset = l2_offset;
593 45aba42f Kevin Wolf
    *new_l2_index = l2_index;
594 45aba42f Kevin Wolf
595 1e3e8f1a Kevin Wolf
    return 0;
596 45aba42f Kevin Wolf
}
597 45aba42f Kevin Wolf
598 45aba42f Kevin Wolf
/*
599 45aba42f Kevin Wolf
 * alloc_compressed_cluster_offset
600 45aba42f Kevin Wolf
 *
601 45aba42f Kevin Wolf
 * For a given offset of the disk image, return cluster offset in
602 45aba42f Kevin Wolf
 * qcow2 file.
603 45aba42f Kevin Wolf
 *
604 45aba42f Kevin Wolf
 * If the offset is not found, allocate a new compressed cluster.
605 45aba42f Kevin Wolf
 *
606 45aba42f Kevin Wolf
 * Return the cluster offset if successful,
607 45aba42f Kevin Wolf
 * Return 0, otherwise.
608 45aba42f Kevin Wolf
 *
609 45aba42f Kevin Wolf
 */
610 45aba42f Kevin Wolf
611 ed6ccf0f Kevin Wolf
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
612 ed6ccf0f Kevin Wolf
                                               uint64_t offset,
613 ed6ccf0f Kevin Wolf
                                               int compressed_size)
614 45aba42f Kevin Wolf
{
615 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
616 45aba42f Kevin Wolf
    int l2_index, ret;
617 f4f0d391 Kevin Wolf
    uint64_t l2_offset, *l2_table;
618 f4f0d391 Kevin Wolf
    int64_t cluster_offset;
619 45aba42f Kevin Wolf
    int nb_csectors;
620 45aba42f Kevin Wolf
621 45aba42f Kevin Wolf
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
622 1e3e8f1a Kevin Wolf
    if (ret < 0) {
623 45aba42f Kevin Wolf
        return 0;
624 1e3e8f1a Kevin Wolf
    }
625 45aba42f Kevin Wolf
626 45aba42f Kevin Wolf
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
627 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COPIED)
628 45aba42f Kevin Wolf
        return cluster_offset & ~QCOW_OFLAG_COPIED;
629 45aba42f Kevin Wolf
630 45aba42f Kevin Wolf
    if (cluster_offset)
631 ed6ccf0f Kevin Wolf
        qcow2_free_any_clusters(bs, cluster_offset, 1);
632 45aba42f Kevin Wolf
633 ed6ccf0f Kevin Wolf
    cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
634 5d757b56 Kevin Wolf
    if (cluster_offset < 0) {
635 5d757b56 Kevin Wolf
        return 0;
636 5d757b56 Kevin Wolf
    }
637 5d757b56 Kevin Wolf
638 45aba42f Kevin Wolf
    nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
639 45aba42f Kevin Wolf
                  (cluster_offset >> 9);
640 45aba42f Kevin Wolf
641 45aba42f Kevin Wolf
    cluster_offset |= QCOW_OFLAG_COMPRESSED |
642 45aba42f Kevin Wolf
                      ((uint64_t)nb_csectors << s->csize_shift);
643 45aba42f Kevin Wolf
644 45aba42f Kevin Wolf
    /* update L2 table */
645 45aba42f Kevin Wolf
646 45aba42f Kevin Wolf
    /* compressed clusters never have the copied flag */
647 45aba42f Kevin Wolf
648 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
649 45aba42f Kevin Wolf
    l2_table[l2_index] = cpu_to_be64(cluster_offset);
650 8b3b7206 Kevin Wolf
    if (bdrv_pwrite_sync(bs->file,
651 45aba42f Kevin Wolf
                    l2_offset + l2_index * sizeof(uint64_t),
652 45aba42f Kevin Wolf
                    l2_table + l2_index,
653 8b3b7206 Kevin Wolf
                    sizeof(uint64_t)) < 0)
654 45aba42f Kevin Wolf
        return 0;
655 45aba42f Kevin Wolf
656 45aba42f Kevin Wolf
    return cluster_offset;
657 45aba42f Kevin Wolf
}
658 45aba42f Kevin Wolf
659 4c1612d9 Kevin Wolf
/*
660 4c1612d9 Kevin Wolf
 * Write L2 table updates to disk, writing whole sectors to avoid a
661 4c1612d9 Kevin Wolf
 * read-modify-write in bdrv_pwrite
662 4c1612d9 Kevin Wolf
 */
663 4c1612d9 Kevin Wolf
#define L2_ENTRIES_PER_SECTOR (512 / 8)
664 66f82cee Kevin Wolf
static int write_l2_entries(BlockDriverState *bs, uint64_t *l2_table,
665 4c1612d9 Kevin Wolf
    uint64_t l2_offset, int l2_index, int num)
666 4c1612d9 Kevin Wolf
{
667 4c1612d9 Kevin Wolf
    int l2_start_index = l2_index & ~(L1_ENTRIES_PER_SECTOR - 1);
668 4c1612d9 Kevin Wolf
    int start_offset = (8 * l2_index) & ~511;
669 4c1612d9 Kevin Wolf
    int end_offset = (8 * (l2_index + num) + 511) & ~511;
670 4c1612d9 Kevin Wolf
    size_t len = end_offset - start_offset;
671 79a31189 Kevin Wolf
    int ret;
672 4c1612d9 Kevin Wolf
673 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
674 7ec5e6a4 Kevin Wolf
    ret = bdrv_pwrite(bs->file, l2_offset + start_offset,
675 79a31189 Kevin Wolf
        &l2_table[l2_start_index], len);
676 79a31189 Kevin Wolf
    if (ret < 0) {
677 79a31189 Kevin Wolf
        return ret;
678 4c1612d9 Kevin Wolf
    }
679 4c1612d9 Kevin Wolf
680 4c1612d9 Kevin Wolf
    return 0;
681 4c1612d9 Kevin Wolf
}
682 4c1612d9 Kevin Wolf
683 148da7ea Kevin Wolf
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
684 45aba42f Kevin Wolf
{
685 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
686 45aba42f Kevin Wolf
    int i, j = 0, l2_index, ret;
687 45aba42f Kevin Wolf
    uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
688 148da7ea Kevin Wolf
    uint64_t cluster_offset = m->cluster_offset;
689 45aba42f Kevin Wolf
690 45aba42f Kevin Wolf
    if (m->nb_clusters == 0)
691 45aba42f Kevin Wolf
        return 0;
692 45aba42f Kevin Wolf
693 45aba42f Kevin Wolf
    old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
694 45aba42f Kevin Wolf
695 45aba42f Kevin Wolf
    /* copy content of unmodified sectors */
696 45aba42f Kevin Wolf
    start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
697 45aba42f Kevin Wolf
    if (m->n_start) {
698 45aba42f Kevin Wolf
        ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
699 45aba42f Kevin Wolf
        if (ret < 0)
700 45aba42f Kevin Wolf
            goto err;
701 45aba42f Kevin Wolf
    }
702 45aba42f Kevin Wolf
703 45aba42f Kevin Wolf
    if (m->nb_available & (s->cluster_sectors - 1)) {
704 45aba42f Kevin Wolf
        uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
705 45aba42f Kevin Wolf
        ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
706 45aba42f Kevin Wolf
                m->nb_available - end, s->cluster_sectors);
707 45aba42f Kevin Wolf
        if (ret < 0)
708 45aba42f Kevin Wolf
            goto err;
709 45aba42f Kevin Wolf
    }
710 45aba42f Kevin Wolf
711 45aba42f Kevin Wolf
    /* update L2 table */
712 1e3e8f1a Kevin Wolf
    ret = get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index);
713 1e3e8f1a Kevin Wolf
    if (ret < 0) {
714 45aba42f Kevin Wolf
        goto err;
715 1e3e8f1a Kevin Wolf
    }
716 45aba42f Kevin Wolf
717 45aba42f Kevin Wolf
    for (i = 0; i < m->nb_clusters; i++) {
718 45aba42f Kevin Wolf
        /* if two concurrent writes happen to the same unallocated cluster
719 45aba42f Kevin Wolf
         * each write allocates separate cluster and writes data concurrently.
720 45aba42f Kevin Wolf
         * The first one to complete updates l2 table with pointer to its
721 45aba42f Kevin Wolf
         * cluster the second one has to do RMW (which is done above by
722 45aba42f Kevin Wolf
         * copy_sectors()), update l2 table with its cluster pointer and free
723 45aba42f Kevin Wolf
         * old cluster. This is what this loop does */
724 45aba42f Kevin Wolf
        if(l2_table[l2_index + i] != 0)
725 45aba42f Kevin Wolf
            old_cluster[j++] = l2_table[l2_index + i];
726 45aba42f Kevin Wolf
727 45aba42f Kevin Wolf
        l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
728 45aba42f Kevin Wolf
                    (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
729 45aba42f Kevin Wolf
     }
730 45aba42f Kevin Wolf
731 9f8e668e Kevin Wolf
    /*
732 9f8e668e Kevin Wolf
     * Before we update the L2 table to actually point to the new cluster, we
733 9f8e668e Kevin Wolf
     * need to be sure that the refcounts have been increased and COW was
734 9f8e668e Kevin Wolf
     * handled.
735 9f8e668e Kevin Wolf
     */
736 9f8e668e Kevin Wolf
    bdrv_flush(bs->file);
737 9f8e668e Kevin Wolf
738 66f82cee Kevin Wolf
    ret = write_l2_entries(bs, l2_table, l2_offset, l2_index, m->nb_clusters);
739 c835d00f Kevin Wolf
    if (ret < 0) {
740 1b7c801b Kevin Wolf
        qcow2_l2_cache_reset(bs);
741 45aba42f Kevin Wolf
        goto err;
742 4c1612d9 Kevin Wolf
    }
743 45aba42f Kevin Wolf
744 7ec5e6a4 Kevin Wolf
    /*
745 7ec5e6a4 Kevin Wolf
     * If this was a COW, we need to decrease the refcount of the old cluster.
746 7ec5e6a4 Kevin Wolf
     * Also flush bs->file to get the right order for L2 and refcount update.
747 7ec5e6a4 Kevin Wolf
     */
748 7ec5e6a4 Kevin Wolf
    if (j != 0) {
749 7ec5e6a4 Kevin Wolf
        bdrv_flush(bs->file);
750 7ec5e6a4 Kevin Wolf
        for (i = 0; i < j; i++) {
751 7ec5e6a4 Kevin Wolf
            qcow2_free_any_clusters(bs,
752 7ec5e6a4 Kevin Wolf
                be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
753 7ec5e6a4 Kevin Wolf
        }
754 7ec5e6a4 Kevin Wolf
    }
755 45aba42f Kevin Wolf
756 45aba42f Kevin Wolf
    ret = 0;
757 45aba42f Kevin Wolf
err:
758 45aba42f Kevin Wolf
    qemu_free(old_cluster);
759 45aba42f Kevin Wolf
    return ret;
760 45aba42f Kevin Wolf
 }
761 45aba42f Kevin Wolf
762 45aba42f Kevin Wolf
/*
763 45aba42f Kevin Wolf
 * alloc_cluster_offset
764 45aba42f Kevin Wolf
 *
765 148da7ea Kevin Wolf
 * For a given offset of the disk image, return cluster offset in qcow2 file.
766 45aba42f Kevin Wolf
 * If the offset is not found, allocate a new cluster.
767 45aba42f Kevin Wolf
 *
768 148da7ea Kevin Wolf
 * If the cluster was already allocated, m->nb_clusters is set to 0,
769 148da7ea Kevin Wolf
 * m->depends_on is set to NULL and the other fields in m are meaningless.
770 148da7ea Kevin Wolf
 *
771 148da7ea Kevin Wolf
 * If the cluster is newly allocated, m->nb_clusters is set to the number of
772 148da7ea Kevin Wolf
 * contiguous clusters that have been allocated. This may be 0 if the request
773 148da7ea Kevin Wolf
 * conflict with another write request in flight; in this case, m->depends_on
774 148da7ea Kevin Wolf
 * is set and the remaining fields of m are meaningless.
775 45aba42f Kevin Wolf
 *
776 148da7ea Kevin Wolf
 * If m->nb_clusters is non-zero, the other fields of m are valid and contain
777 148da7ea Kevin Wolf
 * information about the first allocated cluster.
778 148da7ea Kevin Wolf
 *
779 148da7ea Kevin Wolf
 * Return 0 on success and -errno in error cases
780 45aba42f Kevin Wolf
 */
781 f4f0d391 Kevin Wolf
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
782 f4f0d391 Kevin Wolf
    int n_start, int n_end, int *num, QCowL2Meta *m)
783 45aba42f Kevin Wolf
{
784 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
785 45aba42f Kevin Wolf
    int l2_index, ret;
786 5d757b56 Kevin Wolf
    uint64_t l2_offset, *l2_table;
787 5d757b56 Kevin Wolf
    int64_t cluster_offset;
788 80ee15a6 Kevin Wolf
    unsigned int nb_clusters, i = 0;
789 f214978a Kevin Wolf
    QCowL2Meta *old_alloc;
790 45aba42f Kevin Wolf
791 45aba42f Kevin Wolf
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
792 1e3e8f1a Kevin Wolf
    if (ret < 0) {
793 148da7ea Kevin Wolf
        return ret;
794 1e3e8f1a Kevin Wolf
    }
795 45aba42f Kevin Wolf
796 45aba42f Kevin Wolf
    nb_clusters = size_to_clusters(s, n_end << 9);
797 45aba42f Kevin Wolf
798 45aba42f Kevin Wolf
    nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
799 45aba42f Kevin Wolf
800 45aba42f Kevin Wolf
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
801 45aba42f Kevin Wolf
802 45aba42f Kevin Wolf
    /* We keep all QCOW_OFLAG_COPIED clusters */
803 45aba42f Kevin Wolf
804 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COPIED) {
805 45aba42f Kevin Wolf
        nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
806 45aba42f Kevin Wolf
                &l2_table[l2_index], 0, 0);
807 45aba42f Kevin Wolf
808 45aba42f Kevin Wolf
        cluster_offset &= ~QCOW_OFLAG_COPIED;
809 45aba42f Kevin Wolf
        m->nb_clusters = 0;
810 148da7ea Kevin Wolf
        m->depends_on = NULL;
811 45aba42f Kevin Wolf
812 45aba42f Kevin Wolf
        goto out;
813 45aba42f Kevin Wolf
    }
814 45aba42f Kevin Wolf
815 45aba42f Kevin Wolf
    /* for the moment, multiple compressed clusters are not managed */
816 45aba42f Kevin Wolf
817 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COMPRESSED)
818 45aba42f Kevin Wolf
        nb_clusters = 1;
819 45aba42f Kevin Wolf
820 45aba42f Kevin Wolf
    /* how many available clusters ? */
821 45aba42f Kevin Wolf
822 45aba42f Kevin Wolf
    while (i < nb_clusters) {
823 45aba42f Kevin Wolf
        i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
824 45aba42f Kevin Wolf
                &l2_table[l2_index], i, 0);
825 4805bb66 Kevin Wolf
        if ((i >= nb_clusters) || be64_to_cpu(l2_table[l2_index + i])) {
826 45aba42f Kevin Wolf
            break;
827 4805bb66 Kevin Wolf
        }
828 45aba42f Kevin Wolf
829 45aba42f Kevin Wolf
        i += count_contiguous_free_clusters(nb_clusters - i,
830 45aba42f Kevin Wolf
                &l2_table[l2_index + i]);
831 4805bb66 Kevin Wolf
        if (i >= nb_clusters) {
832 4805bb66 Kevin Wolf
            break;
833 4805bb66 Kevin Wolf
        }
834 45aba42f Kevin Wolf
835 45aba42f Kevin Wolf
        cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
836 45aba42f Kevin Wolf
837 45aba42f Kevin Wolf
        if ((cluster_offset & QCOW_OFLAG_COPIED) ||
838 45aba42f Kevin Wolf
                (cluster_offset & QCOW_OFLAG_COMPRESSED))
839 45aba42f Kevin Wolf
            break;
840 45aba42f Kevin Wolf
    }
841 4805bb66 Kevin Wolf
    assert(i <= nb_clusters);
842 45aba42f Kevin Wolf
    nb_clusters = i;
843 45aba42f Kevin Wolf
844 f214978a Kevin Wolf
    /*
845 f214978a Kevin Wolf
     * Check if there already is an AIO write request in flight which allocates
846 f214978a Kevin Wolf
     * the same cluster. In this case we need to wait until the previous
847 f214978a Kevin Wolf
     * request has completed and updated the L2 table accordingly.
848 f214978a Kevin Wolf
     */
849 72cf2d4f Blue Swirl
    QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
850 f214978a Kevin Wolf
851 f214978a Kevin Wolf
        uint64_t end_offset = offset + nb_clusters * s->cluster_size;
852 f214978a Kevin Wolf
        uint64_t old_offset = old_alloc->offset;
853 f214978a Kevin Wolf
        uint64_t old_end_offset = old_alloc->offset +
854 f214978a Kevin Wolf
            old_alloc->nb_clusters * s->cluster_size;
855 f214978a Kevin Wolf
856 f214978a Kevin Wolf
        if (end_offset < old_offset || offset > old_end_offset) {
857 f214978a Kevin Wolf
            /* No intersection */
858 f214978a Kevin Wolf
        } else {
859 f214978a Kevin Wolf
            if (offset < old_offset) {
860 f214978a Kevin Wolf
                /* Stop at the start of a running allocation */
861 f214978a Kevin Wolf
                nb_clusters = (old_offset - offset) >> s->cluster_bits;
862 f214978a Kevin Wolf
            } else {
863 f214978a Kevin Wolf
                nb_clusters = 0;
864 f214978a Kevin Wolf
            }
865 f214978a Kevin Wolf
866 f214978a Kevin Wolf
            if (nb_clusters == 0) {
867 f214978a Kevin Wolf
                /* Set dependency and wait for a callback */
868 f214978a Kevin Wolf
                m->depends_on = old_alloc;
869 f214978a Kevin Wolf
                m->nb_clusters = 0;
870 f214978a Kevin Wolf
                *num = 0;
871 f214978a Kevin Wolf
                return 0;
872 f214978a Kevin Wolf
            }
873 f214978a Kevin Wolf
        }
874 f214978a Kevin Wolf
    }
875 f214978a Kevin Wolf
876 f214978a Kevin Wolf
    if (!nb_clusters) {
877 f214978a Kevin Wolf
        abort();
878 f214978a Kevin Wolf
    }
879 f214978a Kevin Wolf
880 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
881 f214978a Kevin Wolf
882 45aba42f Kevin Wolf
    /* allocate a new cluster */
883 45aba42f Kevin Wolf
884 ed6ccf0f Kevin Wolf
    cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
885 5d757b56 Kevin Wolf
    if (cluster_offset < 0) {
886 c644db3d Kevin Wolf
        QLIST_REMOVE(m, next_in_flight);
887 5d757b56 Kevin Wolf
        return cluster_offset;
888 5d757b56 Kevin Wolf
    }
889 45aba42f Kevin Wolf
890 45aba42f Kevin Wolf
    /* save info needed for meta data update */
891 45aba42f Kevin Wolf
    m->offset = offset;
892 45aba42f Kevin Wolf
    m->n_start = n_start;
893 45aba42f Kevin Wolf
    m->nb_clusters = nb_clusters;
894 45aba42f Kevin Wolf
895 45aba42f Kevin Wolf
out:
896 45aba42f Kevin Wolf
    m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
897 148da7ea Kevin Wolf
    m->cluster_offset = cluster_offset;
898 45aba42f Kevin Wolf
899 45aba42f Kevin Wolf
    *num = m->nb_available - n_start;
900 45aba42f Kevin Wolf
901 148da7ea Kevin Wolf
    return 0;
902 45aba42f Kevin Wolf
}
903 45aba42f Kevin Wolf
904 45aba42f Kevin Wolf
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
905 45aba42f Kevin Wolf
                             const uint8_t *buf, int buf_size)
906 45aba42f Kevin Wolf
{
907 45aba42f Kevin Wolf
    z_stream strm1, *strm = &strm1;
908 45aba42f Kevin Wolf
    int ret, out_len;
909 45aba42f Kevin Wolf
910 45aba42f Kevin Wolf
    memset(strm, 0, sizeof(*strm));
911 45aba42f Kevin Wolf
912 45aba42f Kevin Wolf
    strm->next_in = (uint8_t *)buf;
913 45aba42f Kevin Wolf
    strm->avail_in = buf_size;
914 45aba42f Kevin Wolf
    strm->next_out = out_buf;
915 45aba42f Kevin Wolf
    strm->avail_out = out_buf_size;
916 45aba42f Kevin Wolf
917 45aba42f Kevin Wolf
    ret = inflateInit2(strm, -12);
918 45aba42f Kevin Wolf
    if (ret != Z_OK)
919 45aba42f Kevin Wolf
        return -1;
920 45aba42f Kevin Wolf
    ret = inflate(strm, Z_FINISH);
921 45aba42f Kevin Wolf
    out_len = strm->next_out - out_buf;
922 45aba42f Kevin Wolf
    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
923 45aba42f Kevin Wolf
        out_len != out_buf_size) {
924 45aba42f Kevin Wolf
        inflateEnd(strm);
925 45aba42f Kevin Wolf
        return -1;
926 45aba42f Kevin Wolf
    }
927 45aba42f Kevin Wolf
    inflateEnd(strm);
928 45aba42f Kevin Wolf
    return 0;
929 45aba42f Kevin Wolf
}
930 45aba42f Kevin Wolf
931 66f82cee Kevin Wolf
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
932 45aba42f Kevin Wolf
{
933 66f82cee Kevin Wolf
    BDRVQcowState *s = bs->opaque;
934 45aba42f Kevin Wolf
    int ret, csize, nb_csectors, sector_offset;
935 45aba42f Kevin Wolf
    uint64_t coffset;
936 45aba42f Kevin Wolf
937 45aba42f Kevin Wolf
    coffset = cluster_offset & s->cluster_offset_mask;
938 45aba42f Kevin Wolf
    if (s->cluster_cache_offset != coffset) {
939 45aba42f Kevin Wolf
        nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
940 45aba42f Kevin Wolf
        sector_offset = coffset & 511;
941 45aba42f Kevin Wolf
        csize = nb_csectors * 512 - sector_offset;
942 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
943 66f82cee Kevin Wolf
        ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data, nb_csectors);
944 45aba42f Kevin Wolf
        if (ret < 0) {
945 45aba42f Kevin Wolf
            return -1;
946 45aba42f Kevin Wolf
        }
947 45aba42f Kevin Wolf
        if (decompress_buffer(s->cluster_cache, s->cluster_size,
948 45aba42f Kevin Wolf
                              s->cluster_data + sector_offset, csize) < 0) {
949 45aba42f Kevin Wolf
            return -1;
950 45aba42f Kevin Wolf
        }
951 45aba42f Kevin Wolf
        s->cluster_cache_offset = coffset;
952 45aba42f Kevin Wolf
    }
953 45aba42f Kevin Wolf
    return 0;
954 45aba42f Kevin Wolf
}