Statistics
| Branch: | Revision:

root / block / qcow2-cluster.c @ 37628f11

History | View | Annotate | Download (27.8 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 29c1a730 Kevin Wolf
71 29c1a730 Kevin Wolf
    ret = qcow2_cache_flush(bs, s->refcount_block_cache);
72 29c1a730 Kevin Wolf
    if (ret < 0) {
73 29c1a730 Kevin Wolf
        return ret;
74 29c1a730 Kevin Wolf
    }
75 45aba42f Kevin Wolf
76 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
77 45aba42f Kevin Wolf
    for(i = 0; i < s->l1_size; i++)
78 45aba42f Kevin Wolf
        new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
79 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, new_l1_table, new_l1_size2);
80 8b3b7206 Kevin Wolf
    if (ret < 0)
81 45aba42f Kevin Wolf
        goto fail;
82 45aba42f Kevin Wolf
    for(i = 0; i < s->l1_size; i++)
83 45aba42f Kevin Wolf
        new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
84 45aba42f Kevin Wolf
85 45aba42f Kevin Wolf
    /* set new table */
86 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
87 45aba42f Kevin Wolf
    cpu_to_be32w((uint32_t*)data, new_l1_size);
88 653df36b Aurelien Jarno
    cpu_to_be64wu((uint64_t*)(data + 4), new_l1_table_offset);
89 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size), data,sizeof(data));
90 8b3b7206 Kevin Wolf
    if (ret < 0) {
91 45aba42f Kevin Wolf
        goto fail;
92 fb8fa77c Kevin Wolf
    }
93 45aba42f Kevin Wolf
    qemu_free(s->l1_table);
94 ed6ccf0f Kevin Wolf
    qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
95 45aba42f Kevin Wolf
    s->l1_table_offset = new_l1_table_offset;
96 45aba42f Kevin Wolf
    s->l1_table = new_l1_table;
97 45aba42f Kevin Wolf
    s->l1_size = new_l1_size;
98 45aba42f Kevin Wolf
    return 0;
99 45aba42f Kevin Wolf
 fail:
100 fb8fa77c Kevin Wolf
    qemu_free(new_l1_table);
101 fb8fa77c Kevin Wolf
    qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2);
102 8b3b7206 Kevin Wolf
    return ret;
103 45aba42f Kevin Wolf
}
104 45aba42f Kevin Wolf
105 45aba42f Kevin Wolf
/*
106 45aba42f Kevin Wolf
 * l2_load
107 45aba42f Kevin Wolf
 *
108 45aba42f Kevin Wolf
 * Loads a L2 table into memory. If the table is in the cache, the cache
109 45aba42f Kevin Wolf
 * is used; otherwise the L2 table is loaded from the image file.
110 45aba42f Kevin Wolf
 *
111 45aba42f Kevin Wolf
 * Returns a pointer to the L2 table on success, or NULL if the read from
112 45aba42f Kevin Wolf
 * the image file failed.
113 45aba42f Kevin Wolf
 */
114 45aba42f Kevin Wolf
115 55c17e98 Kevin Wolf
static int l2_load(BlockDriverState *bs, uint64_t l2_offset,
116 55c17e98 Kevin Wolf
    uint64_t **l2_table)
117 45aba42f Kevin Wolf
{
118 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
119 55c17e98 Kevin Wolf
    int ret;
120 45aba42f Kevin Wolf
121 29c1a730 Kevin Wolf
    ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset, (void**) l2_table);
122 45aba42f Kevin Wolf
123 29c1a730 Kevin Wolf
    return ret;
124 45aba42f Kevin Wolf
}
125 45aba42f Kevin Wolf
126 45aba42f Kevin Wolf
/*
127 6583e3c7 Kevin Wolf
 * Writes one sector of the L1 table to the disk (can't update single entries
128 6583e3c7 Kevin Wolf
 * and we really don't want bdrv_pread to perform a read-modify-write)
129 6583e3c7 Kevin Wolf
 */
130 6583e3c7 Kevin Wolf
#define L1_ENTRIES_PER_SECTOR (512 / 8)
131 66f82cee Kevin Wolf
static int write_l1_entry(BlockDriverState *bs, int l1_index)
132 6583e3c7 Kevin Wolf
{
133 66f82cee Kevin Wolf
    BDRVQcowState *s = bs->opaque;
134 6583e3c7 Kevin Wolf
    uint64_t buf[L1_ENTRIES_PER_SECTOR];
135 6583e3c7 Kevin Wolf
    int l1_start_index;
136 f7defcb6 Kevin Wolf
    int i, ret;
137 6583e3c7 Kevin Wolf
138 6583e3c7 Kevin Wolf
    l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
139 6583e3c7 Kevin Wolf
    for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) {
140 6583e3c7 Kevin Wolf
        buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
141 6583e3c7 Kevin Wolf
    }
142 6583e3c7 Kevin Wolf
143 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
144 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset + 8 * l1_start_index,
145 f7defcb6 Kevin Wolf
        buf, sizeof(buf));
146 f7defcb6 Kevin Wolf
    if (ret < 0) {
147 f7defcb6 Kevin Wolf
        return ret;
148 6583e3c7 Kevin Wolf
    }
149 6583e3c7 Kevin Wolf
150 6583e3c7 Kevin Wolf
    return 0;
151 6583e3c7 Kevin Wolf
}
152 6583e3c7 Kevin Wolf
153 6583e3c7 Kevin Wolf
/*
154 45aba42f Kevin Wolf
 * l2_allocate
155 45aba42f Kevin Wolf
 *
156 45aba42f Kevin Wolf
 * Allocate a new l2 entry in the file. If l1_index points to an already
157 45aba42f Kevin Wolf
 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
158 45aba42f Kevin Wolf
 * table) copy the contents of the old L2 table into the newly allocated one.
159 45aba42f Kevin Wolf
 * Otherwise the new table is initialized with zeros.
160 45aba42f Kevin Wolf
 *
161 45aba42f Kevin Wolf
 */
162 45aba42f Kevin Wolf
163 c46e1167 Kevin Wolf
static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
164 45aba42f Kevin Wolf
{
165 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
166 6583e3c7 Kevin Wolf
    uint64_t old_l2_offset;
167 f4f0d391 Kevin Wolf
    uint64_t *l2_table;
168 f4f0d391 Kevin Wolf
    int64_t l2_offset;
169 c46e1167 Kevin Wolf
    int ret;
170 45aba42f Kevin Wolf
171 45aba42f Kevin Wolf
    old_l2_offset = s->l1_table[l1_index];
172 45aba42f Kevin Wolf
173 45aba42f Kevin Wolf
    /* allocate a new l2 entry */
174 45aba42f Kevin Wolf
175 ed6ccf0f Kevin Wolf
    l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
176 5d757b56 Kevin Wolf
    if (l2_offset < 0) {
177 c46e1167 Kevin Wolf
        return l2_offset;
178 5d757b56 Kevin Wolf
    }
179 29c1a730 Kevin Wolf
180 29c1a730 Kevin Wolf
    ret = qcow2_cache_flush(bs, s->refcount_block_cache);
181 29c1a730 Kevin Wolf
    if (ret < 0) {
182 29c1a730 Kevin Wolf
        goto fail;
183 29c1a730 Kevin Wolf
    }
184 45aba42f Kevin Wolf
185 45aba42f Kevin Wolf
    /* allocate a new entry in the l2 cache */
186 45aba42f Kevin Wolf
187 29c1a730 Kevin Wolf
    ret = qcow2_cache_get_empty(bs, s->l2_table_cache, l2_offset, (void**) table);
188 29c1a730 Kevin Wolf
    if (ret < 0) {
189 29c1a730 Kevin Wolf
        return ret;
190 29c1a730 Kevin Wolf
    }
191 29c1a730 Kevin Wolf
192 29c1a730 Kevin Wolf
    l2_table = *table;
193 45aba42f Kevin Wolf
194 45aba42f Kevin Wolf
    if (old_l2_offset == 0) {
195 45aba42f Kevin Wolf
        /* if there was no old l2 table, clear the new table */
196 45aba42f Kevin Wolf
        memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
197 45aba42f Kevin Wolf
    } else {
198 29c1a730 Kevin Wolf
        uint64_t* old_table;
199 29c1a730 Kevin Wolf
200 45aba42f Kevin Wolf
        /* if there was an old l2 table, read it from the disk */
201 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
202 29c1a730 Kevin Wolf
        ret = qcow2_cache_get(bs, s->l2_table_cache, old_l2_offset,
203 29c1a730 Kevin Wolf
            (void**) &old_table);
204 29c1a730 Kevin Wolf
        if (ret < 0) {
205 29c1a730 Kevin Wolf
            goto fail;
206 29c1a730 Kevin Wolf
        }
207 29c1a730 Kevin Wolf
208 29c1a730 Kevin Wolf
        memcpy(l2_table, old_table, s->cluster_size);
209 29c1a730 Kevin Wolf
210 29c1a730 Kevin Wolf
        ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &old_table);
211 c46e1167 Kevin Wolf
        if (ret < 0) {
212 175e1152 Kevin Wolf
            goto fail;
213 c46e1167 Kevin Wolf
        }
214 45aba42f Kevin Wolf
    }
215 29c1a730 Kevin Wolf
216 45aba42f Kevin Wolf
    /* write the l2 table to the file */
217 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
218 29c1a730 Kevin Wolf
219 29c1a730 Kevin Wolf
    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
220 29c1a730 Kevin Wolf
    ret = qcow2_cache_flush(bs, s->l2_table_cache);
221 c46e1167 Kevin Wolf
    if (ret < 0) {
222 175e1152 Kevin Wolf
        goto fail;
223 175e1152 Kevin Wolf
    }
224 175e1152 Kevin Wolf
225 175e1152 Kevin Wolf
    /* update the L1 entry */
226 175e1152 Kevin Wolf
    s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
227 175e1152 Kevin Wolf
    ret = write_l1_entry(bs, l1_index);
228 175e1152 Kevin Wolf
    if (ret < 0) {
229 175e1152 Kevin Wolf
        goto fail;
230 c46e1167 Kevin Wolf
    }
231 45aba42f Kevin Wolf
232 c46e1167 Kevin Wolf
    *table = l2_table;
233 c46e1167 Kevin Wolf
    return 0;
234 175e1152 Kevin Wolf
235 175e1152 Kevin Wolf
fail:
236 29c1a730 Kevin Wolf
    qcow2_cache_put(bs, s->l2_table_cache, (void**) table);
237 68dba0bf Kevin Wolf
    s->l1_table[l1_index] = old_l2_offset;
238 175e1152 Kevin Wolf
    return ret;
239 45aba42f Kevin Wolf
}
240 45aba42f Kevin Wolf
241 45aba42f Kevin Wolf
static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
242 45aba42f Kevin Wolf
        uint64_t *l2_table, uint64_t start, uint64_t mask)
243 45aba42f Kevin Wolf
{
244 45aba42f Kevin Wolf
    int i;
245 45aba42f Kevin Wolf
    uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
246 45aba42f Kevin Wolf
247 45aba42f Kevin Wolf
    if (!offset)
248 45aba42f Kevin Wolf
        return 0;
249 45aba42f Kevin Wolf
250 45aba42f Kevin Wolf
    for (i = start; i < start + nb_clusters; i++)
251 80ee15a6 Kevin Wolf
        if (offset + (uint64_t) i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
252 45aba42f Kevin Wolf
            break;
253 45aba42f Kevin Wolf
254 45aba42f Kevin Wolf
        return (i - start);
255 45aba42f Kevin Wolf
}
256 45aba42f Kevin Wolf
257 45aba42f Kevin Wolf
static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
258 45aba42f Kevin Wolf
{
259 45aba42f Kevin Wolf
    int i = 0;
260 45aba42f Kevin Wolf
261 45aba42f Kevin Wolf
    while(nb_clusters-- && l2_table[i] == 0)
262 45aba42f Kevin Wolf
        i++;
263 45aba42f Kevin Wolf
264 45aba42f Kevin Wolf
    return i;
265 45aba42f Kevin Wolf
}
266 45aba42f Kevin Wolf
267 45aba42f Kevin Wolf
/* The crypt function is compatible with the linux cryptoloop
268 45aba42f Kevin Wolf
   algorithm for < 4 GB images. NOTE: out_buf == in_buf is
269 45aba42f Kevin Wolf
   supported */
270 ed6ccf0f Kevin Wolf
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
271 ed6ccf0f Kevin Wolf
                           uint8_t *out_buf, const uint8_t *in_buf,
272 ed6ccf0f Kevin Wolf
                           int nb_sectors, int enc,
273 ed6ccf0f Kevin Wolf
                           const AES_KEY *key)
274 45aba42f Kevin Wolf
{
275 45aba42f Kevin Wolf
    union {
276 45aba42f Kevin Wolf
        uint64_t ll[2];
277 45aba42f Kevin Wolf
        uint8_t b[16];
278 45aba42f Kevin Wolf
    } ivec;
279 45aba42f Kevin Wolf
    int i;
280 45aba42f Kevin Wolf
281 45aba42f Kevin Wolf
    for(i = 0; i < nb_sectors; i++) {
282 45aba42f Kevin Wolf
        ivec.ll[0] = cpu_to_le64(sector_num);
283 45aba42f Kevin Wolf
        ivec.ll[1] = 0;
284 45aba42f Kevin Wolf
        AES_cbc_encrypt(in_buf, out_buf, 512, key,
285 45aba42f Kevin Wolf
                        ivec.b, enc);
286 45aba42f Kevin Wolf
        sector_num++;
287 45aba42f Kevin Wolf
        in_buf += 512;
288 45aba42f Kevin Wolf
        out_buf += 512;
289 45aba42f Kevin Wolf
    }
290 45aba42f Kevin Wolf
}
291 45aba42f Kevin Wolf
292 45aba42f Kevin Wolf
293 7c80ab3f Jes Sorensen
static int qcow2_read(BlockDriverState *bs, int64_t sector_num,
294 7c80ab3f Jes Sorensen
                      uint8_t *buf, int nb_sectors)
295 45aba42f Kevin Wolf
{
296 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
297 45aba42f Kevin Wolf
    int ret, index_in_cluster, n, n1;
298 45aba42f Kevin Wolf
    uint64_t cluster_offset;
299 bd28f835 Kevin Wolf
    struct iovec iov;
300 bd28f835 Kevin Wolf
    QEMUIOVector qiov;
301 45aba42f Kevin Wolf
302 45aba42f Kevin Wolf
    while (nb_sectors > 0) {
303 45aba42f Kevin Wolf
        n = nb_sectors;
304 1c46efaa Kevin Wolf
305 1c46efaa Kevin Wolf
        ret = qcow2_get_cluster_offset(bs, sector_num << 9, &n,
306 1c46efaa Kevin Wolf
            &cluster_offset);
307 1c46efaa Kevin Wolf
        if (ret < 0) {
308 1c46efaa Kevin Wolf
            return ret;
309 1c46efaa Kevin Wolf
        }
310 1c46efaa Kevin Wolf
311 45aba42f Kevin Wolf
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
312 45aba42f Kevin Wolf
        if (!cluster_offset) {
313 45aba42f Kevin Wolf
            if (bs->backing_hd) {
314 45aba42f Kevin Wolf
                /* read from the base image */
315 bd28f835 Kevin Wolf
                iov.iov_base = buf;
316 bd28f835 Kevin Wolf
                iov.iov_len = n * 512;
317 bd28f835 Kevin Wolf
                qemu_iovec_init_external(&qiov, &iov, 1);
318 bd28f835 Kevin Wolf
319 bd28f835 Kevin Wolf
                n1 = qcow2_backing_read1(bs->backing_hd, &qiov, sector_num, n);
320 45aba42f Kevin Wolf
                if (n1 > 0) {
321 66f82cee Kevin Wolf
                    BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING);
322 45aba42f Kevin Wolf
                    ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
323 45aba42f Kevin Wolf
                    if (ret < 0)
324 45aba42f Kevin Wolf
                        return -1;
325 45aba42f Kevin Wolf
                }
326 45aba42f Kevin Wolf
            } else {
327 45aba42f Kevin Wolf
                memset(buf, 0, 512 * n);
328 45aba42f Kevin Wolf
            }
329 45aba42f Kevin Wolf
        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
330 66f82cee Kevin Wolf
            if (qcow2_decompress_cluster(bs, cluster_offset) < 0)
331 45aba42f Kevin Wolf
                return -1;
332 45aba42f Kevin Wolf
            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
333 45aba42f Kevin Wolf
        } else {
334 66f82cee Kevin Wolf
            BLKDBG_EVENT(bs->file, BLKDBG_READ);
335 66f82cee Kevin Wolf
            ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);
336 45aba42f Kevin Wolf
            if (ret != n * 512)
337 45aba42f Kevin Wolf
                return -1;
338 45aba42f Kevin Wolf
            if (s->crypt_method) {
339 ed6ccf0f Kevin Wolf
                qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
340 45aba42f Kevin Wolf
                                &s->aes_decrypt_key);
341 45aba42f Kevin Wolf
            }
342 45aba42f Kevin Wolf
        }
343 45aba42f Kevin Wolf
        nb_sectors -= n;
344 45aba42f Kevin Wolf
        sector_num += n;
345 45aba42f Kevin Wolf
        buf += n * 512;
346 45aba42f Kevin Wolf
    }
347 45aba42f Kevin Wolf
    return 0;
348 45aba42f Kevin Wolf
}
349 45aba42f Kevin Wolf
350 45aba42f Kevin Wolf
static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
351 45aba42f Kevin Wolf
                        uint64_t cluster_offset, int n_start, int n_end)
352 45aba42f Kevin Wolf
{
353 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
354 45aba42f Kevin Wolf
    int n, ret;
355 45aba42f Kevin Wolf
356 45aba42f Kevin Wolf
    n = n_end - n_start;
357 45aba42f Kevin Wolf
    if (n <= 0)
358 45aba42f Kevin Wolf
        return 0;
359 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
360 7c80ab3f Jes Sorensen
    ret = qcow2_read(bs, start_sect + n_start, s->cluster_data, n);
361 45aba42f Kevin Wolf
    if (ret < 0)
362 45aba42f Kevin Wolf
        return ret;
363 45aba42f Kevin Wolf
    if (s->crypt_method) {
364 ed6ccf0f Kevin Wolf
        qcow2_encrypt_sectors(s, start_sect + n_start,
365 45aba42f Kevin Wolf
                        s->cluster_data,
366 45aba42f Kevin Wolf
                        s->cluster_data, n, 1,
367 45aba42f Kevin Wolf
                        &s->aes_encrypt_key);
368 45aba42f Kevin Wolf
    }
369 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
370 9f8e668e Kevin Wolf
    ret = bdrv_write(bs->file, (cluster_offset >> 9) + n_start,
371 8b3b7206 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 1c46efaa Kevin Wolf
 * For a given offset of the disk image, find the cluster offset in
382 1c46efaa Kevin Wolf
 * qcow2 file. The offset is stored in *cluster_offset.
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 1c46efaa Kevin Wolf
 * Return 0, if the offset is found
390 1c46efaa Kevin Wolf
 * Return -errno, otherwise.
391 45aba42f Kevin Wolf
 *
392 45aba42f Kevin Wolf
 */
393 45aba42f Kevin Wolf
394 1c46efaa Kevin Wolf
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
395 1c46efaa Kevin Wolf
    int *num, uint64_t *cluster_offset)
396 45aba42f Kevin Wolf
{
397 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
398 80ee15a6 Kevin Wolf
    unsigned int l1_index, l2_index;
399 1c46efaa Kevin Wolf
    uint64_t l2_offset, *l2_table;
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 55c17e98 Kevin Wolf
    int ret;
404 45aba42f Kevin Wolf
405 45aba42f Kevin Wolf
    index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
406 45aba42f Kevin Wolf
    nb_needed = *num + index_in_cluster;
407 45aba42f Kevin Wolf
408 45aba42f Kevin Wolf
    l1_bits = s->l2_bits + s->cluster_bits;
409 45aba42f Kevin Wolf
410 45aba42f Kevin Wolf
    /* compute how many bytes there are between the offset and
411 45aba42f Kevin Wolf
     * the end of the l1 entry
412 45aba42f Kevin Wolf
     */
413 45aba42f Kevin Wolf
414 80ee15a6 Kevin Wolf
    nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
415 45aba42f Kevin Wolf
416 45aba42f Kevin Wolf
    /* compute the number of available sectors */
417 45aba42f Kevin Wolf
418 45aba42f Kevin Wolf
    nb_available = (nb_available >> 9) + index_in_cluster;
419 45aba42f Kevin Wolf
420 45aba42f Kevin Wolf
    if (nb_needed > nb_available) {
421 45aba42f Kevin Wolf
        nb_needed = nb_available;
422 45aba42f Kevin Wolf
    }
423 45aba42f Kevin Wolf
424 1c46efaa Kevin Wolf
    *cluster_offset = 0;
425 45aba42f Kevin Wolf
426 45aba42f Kevin Wolf
    /* seek the the l2 offset in the l1 table */
427 45aba42f Kevin Wolf
428 45aba42f Kevin Wolf
    l1_index = offset >> l1_bits;
429 45aba42f Kevin Wolf
    if (l1_index >= s->l1_size)
430 45aba42f Kevin Wolf
        goto out;
431 45aba42f Kevin Wolf
432 45aba42f Kevin Wolf
    l2_offset = s->l1_table[l1_index];
433 45aba42f Kevin Wolf
434 45aba42f Kevin Wolf
    /* seek the l2 table of the given l2 offset */
435 45aba42f Kevin Wolf
436 45aba42f Kevin Wolf
    if (!l2_offset)
437 45aba42f Kevin Wolf
        goto out;
438 45aba42f Kevin Wolf
439 45aba42f Kevin Wolf
    /* load the l2 table in memory */
440 45aba42f Kevin Wolf
441 45aba42f Kevin Wolf
    l2_offset &= ~QCOW_OFLAG_COPIED;
442 55c17e98 Kevin Wolf
    ret = l2_load(bs, l2_offset, &l2_table);
443 55c17e98 Kevin Wolf
    if (ret < 0) {
444 55c17e98 Kevin Wolf
        return ret;
445 1c46efaa Kevin Wolf
    }
446 45aba42f Kevin Wolf
447 45aba42f Kevin Wolf
    /* find the cluster offset for the given disk offset */
448 45aba42f Kevin Wolf
449 45aba42f Kevin Wolf
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
450 1c46efaa Kevin Wolf
    *cluster_offset = be64_to_cpu(l2_table[l2_index]);
451 45aba42f Kevin Wolf
    nb_clusters = size_to_clusters(s, nb_needed << 9);
452 45aba42f Kevin Wolf
453 1c46efaa Kevin Wolf
    if (!*cluster_offset) {
454 45aba42f Kevin Wolf
        /* how many empty clusters ? */
455 45aba42f Kevin Wolf
        c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
456 45aba42f Kevin Wolf
    } else {
457 45aba42f Kevin Wolf
        /* how many allocated clusters ? */
458 45aba42f Kevin Wolf
        c = count_contiguous_clusters(nb_clusters, s->cluster_size,
459 45aba42f Kevin Wolf
                &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
460 45aba42f Kevin Wolf
    }
461 45aba42f Kevin Wolf
462 29c1a730 Kevin Wolf
    qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
463 29c1a730 Kevin Wolf
464 45aba42f Kevin Wolf
   nb_available = (c * s->cluster_sectors);
465 45aba42f Kevin Wolf
out:
466 45aba42f Kevin Wolf
    if (nb_available > nb_needed)
467 45aba42f Kevin Wolf
        nb_available = nb_needed;
468 45aba42f Kevin Wolf
469 45aba42f Kevin Wolf
    *num = nb_available - index_in_cluster;
470 45aba42f Kevin Wolf
471 1c46efaa Kevin Wolf
    *cluster_offset &=~QCOW_OFLAG_COPIED;
472 1c46efaa Kevin Wolf
    return 0;
473 45aba42f Kevin Wolf
}
474 45aba42f Kevin Wolf
475 45aba42f Kevin Wolf
/*
476 45aba42f Kevin Wolf
 * get_cluster_table
477 45aba42f Kevin Wolf
 *
478 45aba42f Kevin Wolf
 * for a given disk offset, load (and allocate if needed)
479 45aba42f Kevin Wolf
 * the l2 table.
480 45aba42f Kevin Wolf
 *
481 45aba42f Kevin Wolf
 * the l2 table offset in the qcow2 file and the cluster index
482 45aba42f Kevin Wolf
 * in the l2 table are given to the caller.
483 45aba42f Kevin Wolf
 *
484 1e3e8f1a Kevin Wolf
 * Returns 0 on success, -errno in failure case
485 45aba42f Kevin Wolf
 */
486 45aba42f Kevin Wolf
static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
487 45aba42f Kevin Wolf
                             uint64_t **new_l2_table,
488 45aba42f Kevin Wolf
                             uint64_t *new_l2_offset,
489 45aba42f Kevin Wolf
                             int *new_l2_index)
490 45aba42f Kevin Wolf
{
491 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
492 80ee15a6 Kevin Wolf
    unsigned int l1_index, l2_index;
493 c46e1167 Kevin Wolf
    uint64_t l2_offset;
494 c46e1167 Kevin Wolf
    uint64_t *l2_table = NULL;
495 80ee15a6 Kevin Wolf
    int ret;
496 45aba42f Kevin Wolf
497 45aba42f Kevin Wolf
    /* seek the the l2 offset in the l1 table */
498 45aba42f Kevin Wolf
499 45aba42f Kevin Wolf
    l1_index = offset >> (s->l2_bits + s->cluster_bits);
500 45aba42f Kevin Wolf
    if (l1_index >= s->l1_size) {
501 72893756 Stefan Hajnoczi
        ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
502 1e3e8f1a Kevin Wolf
        if (ret < 0) {
503 1e3e8f1a Kevin Wolf
            return ret;
504 1e3e8f1a Kevin Wolf
        }
505 45aba42f Kevin Wolf
    }
506 45aba42f Kevin Wolf
    l2_offset = s->l1_table[l1_index];
507 45aba42f Kevin Wolf
508 45aba42f Kevin Wolf
    /* seek the l2 table of the given l2 offset */
509 45aba42f Kevin Wolf
510 45aba42f Kevin Wolf
    if (l2_offset & QCOW_OFLAG_COPIED) {
511 45aba42f Kevin Wolf
        /* load the l2 table in memory */
512 45aba42f Kevin Wolf
        l2_offset &= ~QCOW_OFLAG_COPIED;
513 55c17e98 Kevin Wolf
        ret = l2_load(bs, l2_offset, &l2_table);
514 55c17e98 Kevin Wolf
        if (ret < 0) {
515 55c17e98 Kevin Wolf
            return ret;
516 1e3e8f1a Kevin Wolf
        }
517 45aba42f Kevin Wolf
    } else {
518 16fde5f2 Kevin Wolf
        /* First allocate a new L2 table (and do COW if needed) */
519 c46e1167 Kevin Wolf
        ret = l2_allocate(bs, l1_index, &l2_table);
520 c46e1167 Kevin Wolf
        if (ret < 0) {
521 c46e1167 Kevin Wolf
            return ret;
522 1e3e8f1a Kevin Wolf
        }
523 16fde5f2 Kevin Wolf
524 16fde5f2 Kevin Wolf
        /* Then decrease the refcount of the old table */
525 16fde5f2 Kevin Wolf
        if (l2_offset) {
526 16fde5f2 Kevin Wolf
            qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
527 16fde5f2 Kevin Wolf
        }
528 45aba42f Kevin Wolf
        l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
529 45aba42f Kevin Wolf
    }
530 45aba42f Kevin Wolf
531 45aba42f Kevin Wolf
    /* find the cluster offset for the given disk offset */
532 45aba42f Kevin Wolf
533 45aba42f Kevin Wolf
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
534 45aba42f Kevin Wolf
535 45aba42f Kevin Wolf
    *new_l2_table = l2_table;
536 45aba42f Kevin Wolf
    *new_l2_offset = l2_offset;
537 45aba42f Kevin Wolf
    *new_l2_index = l2_index;
538 45aba42f Kevin Wolf
539 1e3e8f1a Kevin Wolf
    return 0;
540 45aba42f Kevin Wolf
}
541 45aba42f Kevin Wolf
542 45aba42f Kevin Wolf
/*
543 45aba42f Kevin Wolf
 * alloc_compressed_cluster_offset
544 45aba42f Kevin Wolf
 *
545 45aba42f Kevin Wolf
 * For a given offset of the disk image, return cluster offset in
546 45aba42f Kevin Wolf
 * qcow2 file.
547 45aba42f Kevin Wolf
 *
548 45aba42f Kevin Wolf
 * If the offset is not found, allocate a new compressed cluster.
549 45aba42f Kevin Wolf
 *
550 45aba42f Kevin Wolf
 * Return the cluster offset if successful,
551 45aba42f Kevin Wolf
 * Return 0, otherwise.
552 45aba42f Kevin Wolf
 *
553 45aba42f Kevin Wolf
 */
554 45aba42f Kevin Wolf
555 ed6ccf0f Kevin Wolf
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
556 ed6ccf0f Kevin Wolf
                                               uint64_t offset,
557 ed6ccf0f Kevin Wolf
                                               int compressed_size)
558 45aba42f Kevin Wolf
{
559 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
560 45aba42f Kevin Wolf
    int l2_index, ret;
561 f4f0d391 Kevin Wolf
    uint64_t l2_offset, *l2_table;
562 f4f0d391 Kevin Wolf
    int64_t cluster_offset;
563 45aba42f Kevin Wolf
    int nb_csectors;
564 45aba42f Kevin Wolf
565 45aba42f Kevin Wolf
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
566 1e3e8f1a Kevin Wolf
    if (ret < 0) {
567 45aba42f Kevin Wolf
        return 0;
568 1e3e8f1a Kevin Wolf
    }
569 45aba42f Kevin Wolf
570 45aba42f Kevin Wolf
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
571 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COPIED)
572 45aba42f Kevin Wolf
        return cluster_offset & ~QCOW_OFLAG_COPIED;
573 45aba42f Kevin Wolf
574 45aba42f Kevin Wolf
    if (cluster_offset)
575 ed6ccf0f Kevin Wolf
        qcow2_free_any_clusters(bs, cluster_offset, 1);
576 45aba42f Kevin Wolf
577 ed6ccf0f Kevin Wolf
    cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
578 5d757b56 Kevin Wolf
    if (cluster_offset < 0) {
579 29c1a730 Kevin Wolf
        qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
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 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
594 29c1a730 Kevin Wolf
    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
595 45aba42f Kevin Wolf
    l2_table[l2_index] = cpu_to_be64(cluster_offset);
596 29c1a730 Kevin Wolf
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
597 79a31189 Kevin Wolf
    if (ret < 0) {
598 29c1a730 Kevin Wolf
        return 0;
599 4c1612d9 Kevin Wolf
    }
600 4c1612d9 Kevin Wolf
601 29c1a730 Kevin Wolf
    return cluster_offset;
602 4c1612d9 Kevin Wolf
}
603 4c1612d9 Kevin Wolf
604 148da7ea Kevin Wolf
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
605 45aba42f Kevin Wolf
{
606 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
607 45aba42f Kevin Wolf
    int i, j = 0, l2_index, ret;
608 45aba42f Kevin Wolf
    uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
609 148da7ea Kevin Wolf
    uint64_t cluster_offset = m->cluster_offset;
610 29c1a730 Kevin Wolf
    bool cow = false;
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 29c1a730 Kevin Wolf
        cow = true;
621 45aba42f Kevin Wolf
        ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
622 45aba42f Kevin Wolf
        if (ret < 0)
623 45aba42f Kevin Wolf
            goto err;
624 45aba42f Kevin Wolf
    }
625 45aba42f Kevin Wolf
626 45aba42f Kevin Wolf
    if (m->nb_available & (s->cluster_sectors - 1)) {
627 45aba42f Kevin Wolf
        uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
628 29c1a730 Kevin Wolf
        cow = true;
629 45aba42f Kevin Wolf
        ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
630 45aba42f Kevin Wolf
                m->nb_available - end, s->cluster_sectors);
631 45aba42f Kevin Wolf
        if (ret < 0)
632 45aba42f Kevin Wolf
            goto err;
633 45aba42f Kevin Wolf
    }
634 45aba42f Kevin Wolf
635 29c1a730 Kevin Wolf
    /*
636 29c1a730 Kevin Wolf
     * Update L2 table.
637 29c1a730 Kevin Wolf
     *
638 29c1a730 Kevin Wolf
     * Before we update the L2 table to actually point to the new cluster, we
639 29c1a730 Kevin Wolf
     * need to be sure that the refcounts have been increased and COW was
640 29c1a730 Kevin Wolf
     * handled.
641 29c1a730 Kevin Wolf
     */
642 29c1a730 Kevin Wolf
    if (cow) {
643 3de0a294 Kevin Wolf
        qcow2_cache_depends_on_flush(s->l2_table_cache);
644 29c1a730 Kevin Wolf
    }
645 29c1a730 Kevin Wolf
646 29c1a730 Kevin Wolf
    qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache);
647 1e3e8f1a Kevin Wolf
    ret = get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index);
648 1e3e8f1a Kevin Wolf
    if (ret < 0) {
649 45aba42f Kevin Wolf
        goto err;
650 1e3e8f1a Kevin Wolf
    }
651 29c1a730 Kevin Wolf
    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
652 45aba42f Kevin Wolf
653 45aba42f Kevin Wolf
    for (i = 0; i < m->nb_clusters; i++) {
654 45aba42f Kevin Wolf
        /* if two concurrent writes happen to the same unallocated cluster
655 45aba42f Kevin Wolf
         * each write allocates separate cluster and writes data concurrently.
656 45aba42f Kevin Wolf
         * The first one to complete updates l2 table with pointer to its
657 45aba42f Kevin Wolf
         * cluster the second one has to do RMW (which is done above by
658 45aba42f Kevin Wolf
         * copy_sectors()), update l2 table with its cluster pointer and free
659 45aba42f Kevin Wolf
         * old cluster. This is what this loop does */
660 45aba42f Kevin Wolf
        if(l2_table[l2_index + i] != 0)
661 45aba42f Kevin Wolf
            old_cluster[j++] = l2_table[l2_index + i];
662 45aba42f Kevin Wolf
663 45aba42f Kevin Wolf
        l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
664 45aba42f Kevin Wolf
                    (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
665 45aba42f Kevin Wolf
     }
666 45aba42f Kevin Wolf
667 9f8e668e Kevin Wolf
668 29c1a730 Kevin Wolf
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
669 c835d00f Kevin Wolf
    if (ret < 0) {
670 45aba42f Kevin Wolf
        goto err;
671 4c1612d9 Kevin Wolf
    }
672 45aba42f Kevin Wolf
673 7ec5e6a4 Kevin Wolf
    /*
674 7ec5e6a4 Kevin Wolf
     * If this was a COW, we need to decrease the refcount of the old cluster.
675 7ec5e6a4 Kevin Wolf
     * Also flush bs->file to get the right order for L2 and refcount update.
676 7ec5e6a4 Kevin Wolf
     */
677 7ec5e6a4 Kevin Wolf
    if (j != 0) {
678 7ec5e6a4 Kevin Wolf
        for (i = 0; i < j; i++) {
679 7ec5e6a4 Kevin Wolf
            qcow2_free_any_clusters(bs,
680 7ec5e6a4 Kevin Wolf
                be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
681 7ec5e6a4 Kevin Wolf
        }
682 7ec5e6a4 Kevin Wolf
    }
683 45aba42f Kevin Wolf
684 45aba42f Kevin Wolf
    ret = 0;
685 45aba42f Kevin Wolf
err:
686 45aba42f Kevin Wolf
    qemu_free(old_cluster);
687 45aba42f Kevin Wolf
    return ret;
688 45aba42f Kevin Wolf
 }
689 45aba42f Kevin Wolf
690 45aba42f Kevin Wolf
/*
691 45aba42f Kevin Wolf
 * alloc_cluster_offset
692 45aba42f Kevin Wolf
 *
693 148da7ea Kevin Wolf
 * For a given offset of the disk image, return cluster offset in qcow2 file.
694 45aba42f Kevin Wolf
 * If the offset is not found, allocate a new cluster.
695 45aba42f Kevin Wolf
 *
696 148da7ea Kevin Wolf
 * If the cluster was already allocated, m->nb_clusters is set to 0,
697 148da7ea Kevin Wolf
 * m->depends_on is set to NULL and the other fields in m are meaningless.
698 148da7ea Kevin Wolf
 *
699 148da7ea Kevin Wolf
 * If the cluster is newly allocated, m->nb_clusters is set to the number of
700 148da7ea Kevin Wolf
 * contiguous clusters that have been allocated. This may be 0 if the request
701 148da7ea Kevin Wolf
 * conflict with another write request in flight; in this case, m->depends_on
702 148da7ea Kevin Wolf
 * is set and the remaining fields of m are meaningless.
703 45aba42f Kevin Wolf
 *
704 148da7ea Kevin Wolf
 * If m->nb_clusters is non-zero, the other fields of m are valid and contain
705 148da7ea Kevin Wolf
 * information about the first allocated cluster.
706 148da7ea Kevin Wolf
 *
707 148da7ea Kevin Wolf
 * Return 0 on success and -errno in error cases
708 45aba42f Kevin Wolf
 */
709 f4f0d391 Kevin Wolf
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
710 f4f0d391 Kevin Wolf
    int n_start, int n_end, 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 4805bb66 Kevin Wolf
        if ((i >= nb_clusters) || be64_to_cpu(l2_table[l2_index + i])) {
754 45aba42f Kevin Wolf
            break;
755 4805bb66 Kevin Wolf
        }
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 4805bb66 Kevin Wolf
        if (i >= nb_clusters) {
760 4805bb66 Kevin Wolf
            break;
761 4805bb66 Kevin Wolf
        }
762 45aba42f Kevin Wolf
763 45aba42f Kevin Wolf
        cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
764 45aba42f Kevin Wolf
765 45aba42f Kevin Wolf
        if ((cluster_offset & QCOW_OFLAG_COPIED) ||
766 45aba42f Kevin Wolf
                (cluster_offset & QCOW_OFLAG_COMPRESSED))
767 45aba42f Kevin Wolf
            break;
768 45aba42f Kevin Wolf
    }
769 4805bb66 Kevin Wolf
    assert(i <= nb_clusters);
770 45aba42f Kevin Wolf
    nb_clusters = i;
771 45aba42f Kevin Wolf
772 f214978a Kevin Wolf
    /*
773 f214978a Kevin Wolf
     * Check if there already is an AIO write request in flight which allocates
774 f214978a Kevin Wolf
     * the same cluster. In this case we need to wait until the previous
775 f214978a Kevin Wolf
     * request has completed and updated the L2 table accordingly.
776 f214978a Kevin Wolf
     */
777 72cf2d4f Blue Swirl
    QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
778 f214978a Kevin Wolf
779 f214978a Kevin Wolf
        uint64_t end_offset = offset + nb_clusters * s->cluster_size;
780 f214978a Kevin Wolf
        uint64_t old_offset = old_alloc->offset;
781 f214978a Kevin Wolf
        uint64_t old_end_offset = old_alloc->offset +
782 f214978a Kevin Wolf
            old_alloc->nb_clusters * s->cluster_size;
783 f214978a Kevin Wolf
784 f214978a Kevin Wolf
        if (end_offset < old_offset || offset > old_end_offset) {
785 f214978a Kevin Wolf
            /* No intersection */
786 f214978a Kevin Wolf
        } else {
787 f214978a Kevin Wolf
            if (offset < old_offset) {
788 f214978a Kevin Wolf
                /* Stop at the start of a running allocation */
789 f214978a Kevin Wolf
                nb_clusters = (old_offset - offset) >> s->cluster_bits;
790 f214978a Kevin Wolf
            } else {
791 f214978a Kevin Wolf
                nb_clusters = 0;
792 f214978a Kevin Wolf
            }
793 f214978a Kevin Wolf
794 f214978a Kevin Wolf
            if (nb_clusters == 0) {
795 f214978a Kevin Wolf
                /* Set dependency and wait for a callback */
796 f214978a Kevin Wolf
                m->depends_on = old_alloc;
797 f214978a Kevin Wolf
                m->nb_clusters = 0;
798 f214978a Kevin Wolf
                *num = 0;
799 29c1a730 Kevin Wolf
                ret = 0;
800 29c1a730 Kevin Wolf
                goto fail;
801 f214978a Kevin Wolf
            }
802 f214978a Kevin Wolf
        }
803 f214978a Kevin Wolf
    }
804 f214978a Kevin Wolf
805 f214978a Kevin Wolf
    if (!nb_clusters) {
806 f214978a Kevin Wolf
        abort();
807 f214978a Kevin Wolf
    }
808 f214978a Kevin Wolf
809 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
810 f214978a Kevin Wolf
811 45aba42f Kevin Wolf
    /* allocate a new cluster */
812 45aba42f Kevin Wolf
813 ed6ccf0f Kevin Wolf
    cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
814 5d757b56 Kevin Wolf
    if (cluster_offset < 0) {
815 c644db3d Kevin Wolf
        QLIST_REMOVE(m, next_in_flight);
816 29c1a730 Kevin Wolf
        ret = cluster_offset;
817 29c1a730 Kevin Wolf
        goto fail;
818 5d757b56 Kevin Wolf
    }
819 45aba42f Kevin Wolf
820 45aba42f Kevin Wolf
    /* save info needed for meta data update */
821 45aba42f Kevin Wolf
    m->offset = offset;
822 45aba42f Kevin Wolf
    m->n_start = n_start;
823 45aba42f Kevin Wolf
    m->nb_clusters = nb_clusters;
824 45aba42f Kevin Wolf
825 45aba42f Kevin Wolf
out:
826 29c1a730 Kevin Wolf
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
827 29c1a730 Kevin Wolf
    if (ret < 0) {
828 29c1a730 Kevin Wolf
        return ret;
829 29c1a730 Kevin Wolf
    }
830 29c1a730 Kevin Wolf
831 45aba42f Kevin Wolf
    m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
832 148da7ea Kevin Wolf
    m->cluster_offset = cluster_offset;
833 45aba42f Kevin Wolf
834 45aba42f Kevin Wolf
    *num = m->nb_available - n_start;
835 45aba42f Kevin Wolf
836 148da7ea Kevin Wolf
    return 0;
837 29c1a730 Kevin Wolf
838 29c1a730 Kevin Wolf
fail:
839 29c1a730 Kevin Wolf
    qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
840 29c1a730 Kevin Wolf
    return ret;
841 45aba42f Kevin Wolf
}
842 45aba42f Kevin Wolf
843 45aba42f Kevin Wolf
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
844 45aba42f Kevin Wolf
                             const uint8_t *buf, int buf_size)
845 45aba42f Kevin Wolf
{
846 45aba42f Kevin Wolf
    z_stream strm1, *strm = &strm1;
847 45aba42f Kevin Wolf
    int ret, out_len;
848 45aba42f Kevin Wolf
849 45aba42f Kevin Wolf
    memset(strm, 0, sizeof(*strm));
850 45aba42f Kevin Wolf
851 45aba42f Kevin Wolf
    strm->next_in = (uint8_t *)buf;
852 45aba42f Kevin Wolf
    strm->avail_in = buf_size;
853 45aba42f Kevin Wolf
    strm->next_out = out_buf;
854 45aba42f Kevin Wolf
    strm->avail_out = out_buf_size;
855 45aba42f Kevin Wolf
856 45aba42f Kevin Wolf
    ret = inflateInit2(strm, -12);
857 45aba42f Kevin Wolf
    if (ret != Z_OK)
858 45aba42f Kevin Wolf
        return -1;
859 45aba42f Kevin Wolf
    ret = inflate(strm, Z_FINISH);
860 45aba42f Kevin Wolf
    out_len = strm->next_out - out_buf;
861 45aba42f Kevin Wolf
    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
862 45aba42f Kevin Wolf
        out_len != out_buf_size) {
863 45aba42f Kevin Wolf
        inflateEnd(strm);
864 45aba42f Kevin Wolf
        return -1;
865 45aba42f Kevin Wolf
    }
866 45aba42f Kevin Wolf
    inflateEnd(strm);
867 45aba42f Kevin Wolf
    return 0;
868 45aba42f Kevin Wolf
}
869 45aba42f Kevin Wolf
870 66f82cee Kevin Wolf
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
871 45aba42f Kevin Wolf
{
872 66f82cee Kevin Wolf
    BDRVQcowState *s = bs->opaque;
873 45aba42f Kevin Wolf
    int ret, csize, nb_csectors, sector_offset;
874 45aba42f Kevin Wolf
    uint64_t coffset;
875 45aba42f Kevin Wolf
876 45aba42f Kevin Wolf
    coffset = cluster_offset & s->cluster_offset_mask;
877 45aba42f Kevin Wolf
    if (s->cluster_cache_offset != coffset) {
878 45aba42f Kevin Wolf
        nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
879 45aba42f Kevin Wolf
        sector_offset = coffset & 511;
880 45aba42f Kevin Wolf
        csize = nb_csectors * 512 - sector_offset;
881 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
882 66f82cee Kevin Wolf
        ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data, nb_csectors);
883 45aba42f Kevin Wolf
        if (ret < 0) {
884 8af36488 Kevin Wolf
            return ret;
885 45aba42f Kevin Wolf
        }
886 45aba42f Kevin Wolf
        if (decompress_buffer(s->cluster_cache, s->cluster_size,
887 45aba42f Kevin Wolf
                              s->cluster_data + sector_offset, csize) < 0) {
888 8af36488 Kevin Wolf
            return -EIO;
889 45aba42f Kevin Wolf
        }
890 45aba42f Kevin Wolf
        s->cluster_cache_offset = coffset;
891 45aba42f Kevin Wolf
    }
892 45aba42f Kevin Wolf
    return 0;
893 45aba42f Kevin Wolf
}
894 5ea929e3 Kevin Wolf
895 5ea929e3 Kevin Wolf
/*
896 5ea929e3 Kevin Wolf
 * This discards as many clusters of nb_clusters as possible at once (i.e.
897 5ea929e3 Kevin Wolf
 * all clusters in the same L2 table) and returns the number of discarded
898 5ea929e3 Kevin Wolf
 * clusters.
899 5ea929e3 Kevin Wolf
 */
900 5ea929e3 Kevin Wolf
static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
901 5ea929e3 Kevin Wolf
    unsigned int nb_clusters)
902 5ea929e3 Kevin Wolf
{
903 5ea929e3 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
904 5ea929e3 Kevin Wolf
    uint64_t l2_offset, *l2_table;
905 5ea929e3 Kevin Wolf
    int l2_index;
906 5ea929e3 Kevin Wolf
    int ret;
907 5ea929e3 Kevin Wolf
    int i;
908 5ea929e3 Kevin Wolf
909 5ea929e3 Kevin Wolf
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
910 5ea929e3 Kevin Wolf
    if (ret < 0) {
911 5ea929e3 Kevin Wolf
        return ret;
912 5ea929e3 Kevin Wolf
    }
913 5ea929e3 Kevin Wolf
914 5ea929e3 Kevin Wolf
    /* Limit nb_clusters to one L2 table */
915 5ea929e3 Kevin Wolf
    nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
916 5ea929e3 Kevin Wolf
917 5ea929e3 Kevin Wolf
    for (i = 0; i < nb_clusters; i++) {
918 5ea929e3 Kevin Wolf
        uint64_t old_offset;
919 5ea929e3 Kevin Wolf
920 5ea929e3 Kevin Wolf
        old_offset = be64_to_cpu(l2_table[l2_index + i]);
921 5ea929e3 Kevin Wolf
        old_offset &= ~QCOW_OFLAG_COPIED;
922 5ea929e3 Kevin Wolf
923 5ea929e3 Kevin Wolf
        if (old_offset == 0) {
924 5ea929e3 Kevin Wolf
            continue;
925 5ea929e3 Kevin Wolf
        }
926 5ea929e3 Kevin Wolf
927 5ea929e3 Kevin Wolf
        /* First remove L2 entries */
928 5ea929e3 Kevin Wolf
        qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
929 5ea929e3 Kevin Wolf
        l2_table[l2_index + i] = cpu_to_be64(0);
930 5ea929e3 Kevin Wolf
931 5ea929e3 Kevin Wolf
        /* Then decrease the refcount */
932 5ea929e3 Kevin Wolf
        qcow2_free_any_clusters(bs, old_offset, 1);
933 5ea929e3 Kevin Wolf
    }
934 5ea929e3 Kevin Wolf
935 5ea929e3 Kevin Wolf
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
936 5ea929e3 Kevin Wolf
    if (ret < 0) {
937 5ea929e3 Kevin Wolf
        return ret;
938 5ea929e3 Kevin Wolf
    }
939 5ea929e3 Kevin Wolf
940 5ea929e3 Kevin Wolf
    return nb_clusters;
941 5ea929e3 Kevin Wolf
}
942 5ea929e3 Kevin Wolf
943 5ea929e3 Kevin Wolf
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
944 5ea929e3 Kevin Wolf
    int nb_sectors)
945 5ea929e3 Kevin Wolf
{
946 5ea929e3 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
947 5ea929e3 Kevin Wolf
    uint64_t end_offset;
948 5ea929e3 Kevin Wolf
    unsigned int nb_clusters;
949 5ea929e3 Kevin Wolf
    int ret;
950 5ea929e3 Kevin Wolf
951 5ea929e3 Kevin Wolf
    end_offset = offset + (nb_sectors << BDRV_SECTOR_BITS);
952 5ea929e3 Kevin Wolf
953 5ea929e3 Kevin Wolf
    /* Round start up and end down */
954 5ea929e3 Kevin Wolf
    offset = align_offset(offset, s->cluster_size);
955 5ea929e3 Kevin Wolf
    end_offset &= ~(s->cluster_size - 1);
956 5ea929e3 Kevin Wolf
957 5ea929e3 Kevin Wolf
    if (offset > end_offset) {
958 5ea929e3 Kevin Wolf
        return 0;
959 5ea929e3 Kevin Wolf
    }
960 5ea929e3 Kevin Wolf
961 5ea929e3 Kevin Wolf
    nb_clusters = size_to_clusters(s, end_offset - offset);
962 5ea929e3 Kevin Wolf
963 5ea929e3 Kevin Wolf
    /* Each L2 table is handled by its own loop iteration */
964 5ea929e3 Kevin Wolf
    while (nb_clusters > 0) {
965 5ea929e3 Kevin Wolf
        ret = discard_single_l2(bs, offset, nb_clusters);
966 5ea929e3 Kevin Wolf
        if (ret < 0) {
967 5ea929e3 Kevin Wolf
            return ret;
968 5ea929e3 Kevin Wolf
        }
969 5ea929e3 Kevin Wolf
970 5ea929e3 Kevin Wolf
        nb_clusters -= ret;
971 5ea929e3 Kevin Wolf
        offset += (ret * s->cluster_size);
972 5ea929e3 Kevin Wolf
    }
973 5ea929e3 Kevin Wolf
974 5ea929e3 Kevin Wolf
    return 0;
975 5ea929e3 Kevin Wolf
}