Statistics
| Branch: | Revision:

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

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