Statistics
| Branch: | Revision:

root / block / qcow2-cluster.c @ 2dedf83e

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