Statistics
| Branch: | Revision:

root / block / qcow2-cluster.c @ 73f5e313

History | View | Annotate | Download (27 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 35ee5e39 Frediano Ziglio
    fprintf(stderr, "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 7267c094 Anthony Liguori
    new_l1_table = g_malloc0(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 7267c094 Anthony Liguori
        g_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 80fa3341 Kevin Wolf
        goto fail;
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 7267c094 Anthony Liguori
    g_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 7267c094 Anthony Liguori
    g_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 aef4acb6 Stefan Hajnoczi
static int coroutine_fn copy_sectors(BlockDriverState *bs,
293 aef4acb6 Stefan Hajnoczi
                                     uint64_t start_sect,
294 aef4acb6 Stefan Hajnoczi
                                     uint64_t cluster_offset,
295 aef4acb6 Stefan Hajnoczi
                                     int n_start, int n_end)
296 45aba42f Kevin Wolf
{
297 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
298 aef4acb6 Stefan Hajnoczi
    QEMUIOVector qiov;
299 aef4acb6 Stefan Hajnoczi
    struct iovec iov;
300 45aba42f Kevin Wolf
    int n, ret;
301 1b9f1491 Kevin Wolf
302 1b9f1491 Kevin Wolf
    /*
303 1b9f1491 Kevin Wolf
     * If this is the last cluster and it is only partially used, we must only
304 1b9f1491 Kevin Wolf
     * copy until the end of the image, or bdrv_check_request will fail for the
305 1b9f1491 Kevin Wolf
     * bdrv_read/write calls below.
306 1b9f1491 Kevin Wolf
     */
307 1b9f1491 Kevin Wolf
    if (start_sect + n_end > bs->total_sectors) {
308 1b9f1491 Kevin Wolf
        n_end = bs->total_sectors - start_sect;
309 1b9f1491 Kevin Wolf
    }
310 45aba42f Kevin Wolf
311 45aba42f Kevin Wolf
    n = n_end - n_start;
312 1b9f1491 Kevin Wolf
    if (n <= 0) {
313 45aba42f Kevin Wolf
        return 0;
314 1b9f1491 Kevin Wolf
    }
315 1b9f1491 Kevin Wolf
316 aef4acb6 Stefan Hajnoczi
    iov.iov_len = n * BDRV_SECTOR_SIZE;
317 aef4acb6 Stefan Hajnoczi
    iov.iov_base = qemu_blockalign(bs, iov.iov_len);
318 aef4acb6 Stefan Hajnoczi
319 aef4acb6 Stefan Hajnoczi
    qemu_iovec_init_external(&qiov, &iov, 1);
320 1b9f1491 Kevin Wolf
321 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
322 aef4acb6 Stefan Hajnoczi
323 aef4acb6 Stefan Hajnoczi
    /* Call .bdrv_co_readv() directly instead of using the public block-layer
324 aef4acb6 Stefan Hajnoczi
     * interface.  This avoids double I/O throttling and request tracking,
325 aef4acb6 Stefan Hajnoczi
     * which can lead to deadlock when block layer copy-on-read is enabled.
326 aef4acb6 Stefan Hajnoczi
     */
327 aef4acb6 Stefan Hajnoczi
    ret = bs->drv->bdrv_co_readv(bs, start_sect + n_start, n, &qiov);
328 1b9f1491 Kevin Wolf
    if (ret < 0) {
329 1b9f1491 Kevin Wolf
        goto out;
330 1b9f1491 Kevin Wolf
    }
331 1b9f1491 Kevin Wolf
332 45aba42f Kevin Wolf
    if (s->crypt_method) {
333 ed6ccf0f Kevin Wolf
        qcow2_encrypt_sectors(s, start_sect + n_start,
334 aef4acb6 Stefan Hajnoczi
                        iov.iov_base, iov.iov_base, n, 1,
335 45aba42f Kevin Wolf
                        &s->aes_encrypt_key);
336 45aba42f Kevin Wolf
    }
337 1b9f1491 Kevin Wolf
338 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
339 aef4acb6 Stefan Hajnoczi
    ret = bdrv_co_writev(bs->file, (cluster_offset >> 9) + n_start, n, &qiov);
340 1b9f1491 Kevin Wolf
    if (ret < 0) {
341 1b9f1491 Kevin Wolf
        goto out;
342 1b9f1491 Kevin Wolf
    }
343 1b9f1491 Kevin Wolf
344 1b9f1491 Kevin Wolf
    ret = 0;
345 1b9f1491 Kevin Wolf
out:
346 aef4acb6 Stefan Hajnoczi
    qemu_vfree(iov.iov_base);
347 1b9f1491 Kevin Wolf
    return ret;
348 45aba42f Kevin Wolf
}
349 45aba42f Kevin Wolf
350 45aba42f Kevin Wolf
351 45aba42f Kevin Wolf
/*
352 45aba42f Kevin Wolf
 * get_cluster_offset
353 45aba42f Kevin Wolf
 *
354 1c46efaa Kevin Wolf
 * For a given offset of the disk image, find the cluster offset in
355 1c46efaa Kevin Wolf
 * qcow2 file. The offset is stored in *cluster_offset.
356 45aba42f Kevin Wolf
 *
357 d57237f2 Devin Nakamura
 * on entry, *num is the number of contiguous sectors we'd like to
358 45aba42f Kevin Wolf
 * access following offset.
359 45aba42f Kevin Wolf
 *
360 d57237f2 Devin Nakamura
 * on exit, *num is the number of contiguous sectors we can read.
361 45aba42f Kevin Wolf
 *
362 1c46efaa Kevin Wolf
 * Return 0, if the offset is found
363 1c46efaa Kevin Wolf
 * Return -errno, otherwise.
364 45aba42f Kevin Wolf
 *
365 45aba42f Kevin Wolf
 */
366 45aba42f Kevin Wolf
367 1c46efaa Kevin Wolf
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
368 1c46efaa Kevin Wolf
    int *num, uint64_t *cluster_offset)
369 45aba42f Kevin Wolf
{
370 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
371 80ee15a6 Kevin Wolf
    unsigned int l1_index, l2_index;
372 1c46efaa Kevin Wolf
    uint64_t l2_offset, *l2_table;
373 45aba42f Kevin Wolf
    int l1_bits, c;
374 80ee15a6 Kevin Wolf
    unsigned int index_in_cluster, nb_clusters;
375 80ee15a6 Kevin Wolf
    uint64_t nb_available, nb_needed;
376 55c17e98 Kevin Wolf
    int ret;
377 45aba42f Kevin Wolf
378 45aba42f Kevin Wolf
    index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
379 45aba42f Kevin Wolf
    nb_needed = *num + index_in_cluster;
380 45aba42f Kevin Wolf
381 45aba42f Kevin Wolf
    l1_bits = s->l2_bits + s->cluster_bits;
382 45aba42f Kevin Wolf
383 45aba42f Kevin Wolf
    /* compute how many bytes there are between the offset and
384 45aba42f Kevin Wolf
     * the end of the l1 entry
385 45aba42f Kevin Wolf
     */
386 45aba42f Kevin Wolf
387 80ee15a6 Kevin Wolf
    nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1));
388 45aba42f Kevin Wolf
389 45aba42f Kevin Wolf
    /* compute the number of available sectors */
390 45aba42f Kevin Wolf
391 45aba42f Kevin Wolf
    nb_available = (nb_available >> 9) + index_in_cluster;
392 45aba42f Kevin Wolf
393 45aba42f Kevin Wolf
    if (nb_needed > nb_available) {
394 45aba42f Kevin Wolf
        nb_needed = nb_available;
395 45aba42f Kevin Wolf
    }
396 45aba42f Kevin Wolf
397 1c46efaa Kevin Wolf
    *cluster_offset = 0;
398 45aba42f Kevin Wolf
399 45aba42f Kevin Wolf
    /* seek the the l2 offset in the l1 table */
400 45aba42f Kevin Wolf
401 45aba42f Kevin Wolf
    l1_index = offset >> l1_bits;
402 45aba42f Kevin Wolf
    if (l1_index >= s->l1_size)
403 45aba42f Kevin Wolf
        goto out;
404 45aba42f Kevin Wolf
405 45aba42f Kevin Wolf
    l2_offset = s->l1_table[l1_index];
406 45aba42f Kevin Wolf
407 45aba42f Kevin Wolf
    /* seek the l2 table of the given l2 offset */
408 45aba42f Kevin Wolf
409 45aba42f Kevin Wolf
    if (!l2_offset)
410 45aba42f Kevin Wolf
        goto out;
411 45aba42f Kevin Wolf
412 45aba42f Kevin Wolf
    /* load the l2 table in memory */
413 45aba42f Kevin Wolf
414 45aba42f Kevin Wolf
    l2_offset &= ~QCOW_OFLAG_COPIED;
415 55c17e98 Kevin Wolf
    ret = l2_load(bs, l2_offset, &l2_table);
416 55c17e98 Kevin Wolf
    if (ret < 0) {
417 55c17e98 Kevin Wolf
        return ret;
418 1c46efaa Kevin Wolf
    }
419 45aba42f Kevin Wolf
420 45aba42f Kevin Wolf
    /* find the cluster offset for the given disk offset */
421 45aba42f Kevin Wolf
422 45aba42f Kevin Wolf
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
423 1c46efaa Kevin Wolf
    *cluster_offset = be64_to_cpu(l2_table[l2_index]);
424 45aba42f Kevin Wolf
    nb_clusters = size_to_clusters(s, nb_needed << 9);
425 45aba42f Kevin Wolf
426 1c46efaa Kevin Wolf
    if (!*cluster_offset) {
427 45aba42f Kevin Wolf
        /* how many empty clusters ? */
428 45aba42f Kevin Wolf
        c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
429 45aba42f Kevin Wolf
    } else {
430 45aba42f Kevin Wolf
        /* how many allocated clusters ? */
431 45aba42f Kevin Wolf
        c = count_contiguous_clusters(nb_clusters, s->cluster_size,
432 45aba42f Kevin Wolf
                &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
433 45aba42f Kevin Wolf
    }
434 45aba42f Kevin Wolf
435 29c1a730 Kevin Wolf
    qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
436 29c1a730 Kevin Wolf
437 45aba42f Kevin Wolf
   nb_available = (c * s->cluster_sectors);
438 45aba42f Kevin Wolf
out:
439 45aba42f Kevin Wolf
    if (nb_available > nb_needed)
440 45aba42f Kevin Wolf
        nb_available = nb_needed;
441 45aba42f Kevin Wolf
442 45aba42f Kevin Wolf
    *num = nb_available - index_in_cluster;
443 45aba42f Kevin Wolf
444 1c46efaa Kevin Wolf
    *cluster_offset &=~QCOW_OFLAG_COPIED;
445 1c46efaa Kevin Wolf
    return 0;
446 45aba42f Kevin Wolf
}
447 45aba42f Kevin Wolf
448 45aba42f Kevin Wolf
/*
449 45aba42f Kevin Wolf
 * get_cluster_table
450 45aba42f Kevin Wolf
 *
451 45aba42f Kevin Wolf
 * for a given disk offset, load (and allocate if needed)
452 45aba42f Kevin Wolf
 * the l2 table.
453 45aba42f Kevin Wolf
 *
454 45aba42f Kevin Wolf
 * the l2 table offset in the qcow2 file and the cluster index
455 45aba42f Kevin Wolf
 * in the l2 table are given to the caller.
456 45aba42f Kevin Wolf
 *
457 1e3e8f1a Kevin Wolf
 * Returns 0 on success, -errno in failure case
458 45aba42f Kevin Wolf
 */
459 45aba42f Kevin Wolf
static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
460 45aba42f Kevin Wolf
                             uint64_t **new_l2_table,
461 45aba42f Kevin Wolf
                             uint64_t *new_l2_offset,
462 45aba42f Kevin Wolf
                             int *new_l2_index)
463 45aba42f Kevin Wolf
{
464 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
465 80ee15a6 Kevin Wolf
    unsigned int l1_index, l2_index;
466 c46e1167 Kevin Wolf
    uint64_t l2_offset;
467 c46e1167 Kevin Wolf
    uint64_t *l2_table = NULL;
468 80ee15a6 Kevin Wolf
    int ret;
469 45aba42f Kevin Wolf
470 45aba42f Kevin Wolf
    /* seek the the l2 offset in the l1 table */
471 45aba42f Kevin Wolf
472 45aba42f Kevin Wolf
    l1_index = offset >> (s->l2_bits + s->cluster_bits);
473 45aba42f Kevin Wolf
    if (l1_index >= s->l1_size) {
474 72893756 Stefan Hajnoczi
        ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
475 1e3e8f1a Kevin Wolf
        if (ret < 0) {
476 1e3e8f1a Kevin Wolf
            return ret;
477 1e3e8f1a Kevin Wolf
        }
478 45aba42f Kevin Wolf
    }
479 45aba42f Kevin Wolf
    l2_offset = s->l1_table[l1_index];
480 45aba42f Kevin Wolf
481 45aba42f Kevin Wolf
    /* seek the l2 table of the given l2 offset */
482 45aba42f Kevin Wolf
483 45aba42f Kevin Wolf
    if (l2_offset & QCOW_OFLAG_COPIED) {
484 45aba42f Kevin Wolf
        /* load the l2 table in memory */
485 45aba42f Kevin Wolf
        l2_offset &= ~QCOW_OFLAG_COPIED;
486 55c17e98 Kevin Wolf
        ret = l2_load(bs, l2_offset, &l2_table);
487 55c17e98 Kevin Wolf
        if (ret < 0) {
488 55c17e98 Kevin Wolf
            return ret;
489 1e3e8f1a Kevin Wolf
        }
490 45aba42f Kevin Wolf
    } else {
491 16fde5f2 Kevin Wolf
        /* First allocate a new L2 table (and do COW if needed) */
492 c46e1167 Kevin Wolf
        ret = l2_allocate(bs, l1_index, &l2_table);
493 c46e1167 Kevin Wolf
        if (ret < 0) {
494 c46e1167 Kevin Wolf
            return ret;
495 1e3e8f1a Kevin Wolf
        }
496 16fde5f2 Kevin Wolf
497 16fde5f2 Kevin Wolf
        /* Then decrease the refcount of the old table */
498 16fde5f2 Kevin Wolf
        if (l2_offset) {
499 16fde5f2 Kevin Wolf
            qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
500 16fde5f2 Kevin Wolf
        }
501 45aba42f Kevin Wolf
        l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
502 45aba42f Kevin Wolf
    }
503 45aba42f Kevin Wolf
504 45aba42f Kevin Wolf
    /* find the cluster offset for the given disk offset */
505 45aba42f Kevin Wolf
506 45aba42f Kevin Wolf
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
507 45aba42f Kevin Wolf
508 45aba42f Kevin Wolf
    *new_l2_table = l2_table;
509 45aba42f Kevin Wolf
    *new_l2_offset = l2_offset;
510 45aba42f Kevin Wolf
    *new_l2_index = l2_index;
511 45aba42f Kevin Wolf
512 1e3e8f1a Kevin Wolf
    return 0;
513 45aba42f Kevin Wolf
}
514 45aba42f Kevin Wolf
515 45aba42f Kevin Wolf
/*
516 45aba42f Kevin Wolf
 * alloc_compressed_cluster_offset
517 45aba42f Kevin Wolf
 *
518 45aba42f Kevin Wolf
 * For a given offset of the disk image, return cluster offset in
519 45aba42f Kevin Wolf
 * qcow2 file.
520 45aba42f Kevin Wolf
 *
521 45aba42f Kevin Wolf
 * If the offset is not found, allocate a new compressed cluster.
522 45aba42f Kevin Wolf
 *
523 45aba42f Kevin Wolf
 * Return the cluster offset if successful,
524 45aba42f Kevin Wolf
 * Return 0, otherwise.
525 45aba42f Kevin Wolf
 *
526 45aba42f Kevin Wolf
 */
527 45aba42f Kevin Wolf
528 ed6ccf0f Kevin Wolf
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
529 ed6ccf0f Kevin Wolf
                                               uint64_t offset,
530 ed6ccf0f Kevin Wolf
                                               int compressed_size)
531 45aba42f Kevin Wolf
{
532 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
533 45aba42f Kevin Wolf
    int l2_index, ret;
534 f4f0d391 Kevin Wolf
    uint64_t l2_offset, *l2_table;
535 f4f0d391 Kevin Wolf
    int64_t cluster_offset;
536 45aba42f Kevin Wolf
    int nb_csectors;
537 45aba42f Kevin Wolf
538 45aba42f Kevin Wolf
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
539 1e3e8f1a Kevin Wolf
    if (ret < 0) {
540 45aba42f Kevin Wolf
        return 0;
541 1e3e8f1a Kevin Wolf
    }
542 45aba42f Kevin Wolf
543 45aba42f Kevin Wolf
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
544 8f1efd00 Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COPIED) {
545 8f1efd00 Kevin Wolf
        qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
546 8f1efd00 Kevin Wolf
        return 0;
547 8f1efd00 Kevin Wolf
    }
548 45aba42f Kevin Wolf
549 45aba42f Kevin Wolf
    if (cluster_offset)
550 ed6ccf0f Kevin Wolf
        qcow2_free_any_clusters(bs, cluster_offset, 1);
551 45aba42f Kevin Wolf
552 ed6ccf0f Kevin Wolf
    cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
553 5d757b56 Kevin Wolf
    if (cluster_offset < 0) {
554 29c1a730 Kevin Wolf
        qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
555 5d757b56 Kevin Wolf
        return 0;
556 5d757b56 Kevin Wolf
    }
557 5d757b56 Kevin Wolf
558 45aba42f Kevin Wolf
    nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
559 45aba42f Kevin Wolf
                  (cluster_offset >> 9);
560 45aba42f Kevin Wolf
561 45aba42f Kevin Wolf
    cluster_offset |= QCOW_OFLAG_COMPRESSED |
562 45aba42f Kevin Wolf
                      ((uint64_t)nb_csectors << s->csize_shift);
563 45aba42f Kevin Wolf
564 45aba42f Kevin Wolf
    /* update L2 table */
565 45aba42f Kevin Wolf
566 45aba42f Kevin Wolf
    /* compressed clusters never have the copied flag */
567 45aba42f Kevin Wolf
568 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
569 29c1a730 Kevin Wolf
    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
570 45aba42f Kevin Wolf
    l2_table[l2_index] = cpu_to_be64(cluster_offset);
571 29c1a730 Kevin Wolf
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
572 79a31189 Kevin Wolf
    if (ret < 0) {
573 29c1a730 Kevin Wolf
        return 0;
574 4c1612d9 Kevin Wolf
    }
575 4c1612d9 Kevin Wolf
576 29c1a730 Kevin Wolf
    return cluster_offset;
577 4c1612d9 Kevin Wolf
}
578 4c1612d9 Kevin Wolf
579 148da7ea Kevin Wolf
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
580 45aba42f Kevin Wolf
{
581 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
582 45aba42f Kevin Wolf
    int i, j = 0, l2_index, ret;
583 45aba42f Kevin Wolf
    uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
584 148da7ea Kevin Wolf
    uint64_t cluster_offset = m->cluster_offset;
585 29c1a730 Kevin Wolf
    bool cow = false;
586 45aba42f Kevin Wolf
587 45aba42f Kevin Wolf
    if (m->nb_clusters == 0)
588 45aba42f Kevin Wolf
        return 0;
589 45aba42f Kevin Wolf
590 7267c094 Anthony Liguori
    old_cluster = g_malloc(m->nb_clusters * sizeof(uint64_t));
591 45aba42f Kevin Wolf
592 45aba42f Kevin Wolf
    /* copy content of unmodified sectors */
593 45aba42f Kevin Wolf
    start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
594 45aba42f Kevin Wolf
    if (m->n_start) {
595 29c1a730 Kevin Wolf
        cow = true;
596 1b9f1491 Kevin Wolf
        qemu_co_mutex_unlock(&s->lock);
597 45aba42f Kevin Wolf
        ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
598 1b9f1491 Kevin Wolf
        qemu_co_mutex_lock(&s->lock);
599 45aba42f Kevin Wolf
        if (ret < 0)
600 45aba42f Kevin Wolf
            goto err;
601 45aba42f Kevin Wolf
    }
602 45aba42f Kevin Wolf
603 45aba42f Kevin Wolf
    if (m->nb_available & (s->cluster_sectors - 1)) {
604 45aba42f Kevin Wolf
        uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
605 29c1a730 Kevin Wolf
        cow = true;
606 1b9f1491 Kevin Wolf
        qemu_co_mutex_unlock(&s->lock);
607 45aba42f Kevin Wolf
        ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
608 45aba42f Kevin Wolf
                m->nb_available - end, s->cluster_sectors);
609 1b9f1491 Kevin Wolf
        qemu_co_mutex_lock(&s->lock);
610 45aba42f Kevin Wolf
        if (ret < 0)
611 45aba42f Kevin Wolf
            goto err;
612 45aba42f Kevin Wolf
    }
613 45aba42f Kevin Wolf
614 29c1a730 Kevin Wolf
    /*
615 29c1a730 Kevin Wolf
     * Update L2 table.
616 29c1a730 Kevin Wolf
     *
617 29c1a730 Kevin Wolf
     * Before we update the L2 table to actually point to the new cluster, we
618 29c1a730 Kevin Wolf
     * need to be sure that the refcounts have been increased and COW was
619 29c1a730 Kevin Wolf
     * handled.
620 29c1a730 Kevin Wolf
     */
621 29c1a730 Kevin Wolf
    if (cow) {
622 3de0a294 Kevin Wolf
        qcow2_cache_depends_on_flush(s->l2_table_cache);
623 29c1a730 Kevin Wolf
    }
624 29c1a730 Kevin Wolf
625 29c1a730 Kevin Wolf
    qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache);
626 1e3e8f1a Kevin Wolf
    ret = get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index);
627 1e3e8f1a Kevin Wolf
    if (ret < 0) {
628 45aba42f Kevin Wolf
        goto err;
629 1e3e8f1a Kevin Wolf
    }
630 29c1a730 Kevin Wolf
    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
631 45aba42f Kevin Wolf
632 45aba42f Kevin Wolf
    for (i = 0; i < m->nb_clusters; i++) {
633 45aba42f Kevin Wolf
        /* if two concurrent writes happen to the same unallocated cluster
634 45aba42f Kevin Wolf
         * each write allocates separate cluster and writes data concurrently.
635 45aba42f Kevin Wolf
         * The first one to complete updates l2 table with pointer to its
636 45aba42f Kevin Wolf
         * cluster the second one has to do RMW (which is done above by
637 45aba42f Kevin Wolf
         * copy_sectors()), update l2 table with its cluster pointer and free
638 45aba42f Kevin Wolf
         * old cluster. This is what this loop does */
639 45aba42f Kevin Wolf
        if(l2_table[l2_index + i] != 0)
640 45aba42f Kevin Wolf
            old_cluster[j++] = l2_table[l2_index + i];
641 45aba42f Kevin Wolf
642 45aba42f Kevin Wolf
        l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
643 45aba42f Kevin Wolf
                    (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
644 45aba42f Kevin Wolf
     }
645 45aba42f Kevin Wolf
646 9f8e668e Kevin Wolf
647 29c1a730 Kevin Wolf
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
648 c835d00f Kevin Wolf
    if (ret < 0) {
649 45aba42f Kevin Wolf
        goto err;
650 4c1612d9 Kevin Wolf
    }
651 45aba42f Kevin Wolf
652 7ec5e6a4 Kevin Wolf
    /*
653 7ec5e6a4 Kevin Wolf
     * If this was a COW, we need to decrease the refcount of the old cluster.
654 7ec5e6a4 Kevin Wolf
     * Also flush bs->file to get the right order for L2 and refcount update.
655 7ec5e6a4 Kevin Wolf
     */
656 7ec5e6a4 Kevin Wolf
    if (j != 0) {
657 7ec5e6a4 Kevin Wolf
        for (i = 0; i < j; i++) {
658 7ec5e6a4 Kevin Wolf
            qcow2_free_any_clusters(bs,
659 7ec5e6a4 Kevin Wolf
                be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
660 7ec5e6a4 Kevin Wolf
        }
661 7ec5e6a4 Kevin Wolf
    }
662 45aba42f Kevin Wolf
663 45aba42f Kevin Wolf
    ret = 0;
664 45aba42f Kevin Wolf
err:
665 7267c094 Anthony Liguori
    g_free(old_cluster);
666 45aba42f Kevin Wolf
    return ret;
667 45aba42f Kevin Wolf
 }
668 45aba42f Kevin Wolf
669 45aba42f Kevin Wolf
/*
670 45aba42f Kevin Wolf
 * alloc_cluster_offset
671 45aba42f Kevin Wolf
 *
672 148da7ea Kevin Wolf
 * For a given offset of the disk image, return cluster offset in qcow2 file.
673 45aba42f Kevin Wolf
 * If the offset is not found, allocate a new cluster.
674 45aba42f Kevin Wolf
 *
675 148da7ea Kevin Wolf
 * If the cluster was already allocated, m->nb_clusters is set to 0,
676 a7912369 Frediano Ziglio
 * other fields in m are meaningless.
677 148da7ea Kevin Wolf
 *
678 148da7ea Kevin Wolf
 * If the cluster is newly allocated, m->nb_clusters is set to the number of
679 68d100e9 Kevin Wolf
 * contiguous clusters that have been allocated. In this case, the other
680 68d100e9 Kevin Wolf
 * fields of m are valid and contain information about the first allocated
681 68d100e9 Kevin Wolf
 * cluster.
682 45aba42f Kevin Wolf
 *
683 68d100e9 Kevin Wolf
 * If the request conflicts with another write request in flight, the coroutine
684 68d100e9 Kevin Wolf
 * is queued and will be reentered when the dependency has completed.
685 148da7ea Kevin Wolf
 *
686 148da7ea Kevin Wolf
 * Return 0 on success and -errno in error cases
687 45aba42f Kevin Wolf
 */
688 f4f0d391 Kevin Wolf
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
689 f4f0d391 Kevin Wolf
    int n_start, int n_end, int *num, QCowL2Meta *m)
690 45aba42f Kevin Wolf
{
691 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
692 45aba42f Kevin Wolf
    int l2_index, ret;
693 5d757b56 Kevin Wolf
    uint64_t l2_offset, *l2_table;
694 5d757b56 Kevin Wolf
    int64_t cluster_offset;
695 80ee15a6 Kevin Wolf
    unsigned int nb_clusters, i = 0;
696 f214978a Kevin Wolf
    QCowL2Meta *old_alloc;
697 45aba42f Kevin Wolf
698 45aba42f Kevin Wolf
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
699 1e3e8f1a Kevin Wolf
    if (ret < 0) {
700 148da7ea Kevin Wolf
        return ret;
701 1e3e8f1a Kevin Wolf
    }
702 45aba42f Kevin Wolf
703 68d100e9 Kevin Wolf
again:
704 45aba42f Kevin Wolf
    nb_clusters = size_to_clusters(s, n_end << 9);
705 45aba42f Kevin Wolf
706 45aba42f Kevin Wolf
    nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
707 45aba42f Kevin Wolf
708 45aba42f Kevin Wolf
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
709 45aba42f Kevin Wolf
710 45aba42f Kevin Wolf
    /* We keep all QCOW_OFLAG_COPIED clusters */
711 45aba42f Kevin Wolf
712 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COPIED) {
713 45aba42f Kevin Wolf
        nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
714 45aba42f Kevin Wolf
                &l2_table[l2_index], 0, 0);
715 45aba42f Kevin Wolf
716 45aba42f Kevin Wolf
        cluster_offset &= ~QCOW_OFLAG_COPIED;
717 45aba42f Kevin Wolf
        m->nb_clusters = 0;
718 45aba42f Kevin Wolf
719 45aba42f Kevin Wolf
        goto out;
720 45aba42f Kevin Wolf
    }
721 45aba42f Kevin Wolf
722 45aba42f Kevin Wolf
    /* for the moment, multiple compressed clusters are not managed */
723 45aba42f Kevin Wolf
724 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COMPRESSED)
725 45aba42f Kevin Wolf
        nb_clusters = 1;
726 45aba42f Kevin Wolf
727 45aba42f Kevin Wolf
    /* how many available clusters ? */
728 45aba42f Kevin Wolf
729 45aba42f Kevin Wolf
    while (i < nb_clusters) {
730 45aba42f Kevin Wolf
        i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
731 45aba42f Kevin Wolf
                &l2_table[l2_index], i, 0);
732 4805bb66 Kevin Wolf
        if ((i >= nb_clusters) || be64_to_cpu(l2_table[l2_index + i])) {
733 45aba42f Kevin Wolf
            break;
734 4805bb66 Kevin Wolf
        }
735 45aba42f Kevin Wolf
736 45aba42f Kevin Wolf
        i += count_contiguous_free_clusters(nb_clusters - i,
737 45aba42f Kevin Wolf
                &l2_table[l2_index + i]);
738 4805bb66 Kevin Wolf
        if (i >= nb_clusters) {
739 4805bb66 Kevin Wolf
            break;
740 4805bb66 Kevin Wolf
        }
741 45aba42f Kevin Wolf
742 45aba42f Kevin Wolf
        cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
743 45aba42f Kevin Wolf
744 45aba42f Kevin Wolf
        if ((cluster_offset & QCOW_OFLAG_COPIED) ||
745 45aba42f Kevin Wolf
                (cluster_offset & QCOW_OFLAG_COMPRESSED))
746 45aba42f Kevin Wolf
            break;
747 45aba42f Kevin Wolf
    }
748 4805bb66 Kevin Wolf
    assert(i <= nb_clusters);
749 45aba42f Kevin Wolf
    nb_clusters = i;
750 45aba42f Kevin Wolf
751 f214978a Kevin Wolf
    /*
752 f214978a Kevin Wolf
     * Check if there already is an AIO write request in flight which allocates
753 f214978a Kevin Wolf
     * the same cluster. In this case we need to wait until the previous
754 f214978a Kevin Wolf
     * request has completed and updated the L2 table accordingly.
755 f214978a Kevin Wolf
     */
756 72cf2d4f Blue Swirl
    QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
757 f214978a Kevin Wolf
758 ee18e730 Frediano Ziglio
        uint64_t start = offset >> s->cluster_bits;
759 ee18e730 Frediano Ziglio
        uint64_t end = start + nb_clusters;
760 ee18e730 Frediano Ziglio
        uint64_t old_start = old_alloc->offset >> s->cluster_bits;
761 ee18e730 Frediano Ziglio
        uint64_t old_end = old_start + old_alloc->nb_clusters;
762 f214978a Kevin Wolf
763 ee18e730 Frediano Ziglio
        if (end < old_start || start > old_end) {
764 f214978a Kevin Wolf
            /* No intersection */
765 f214978a Kevin Wolf
        } else {
766 ee18e730 Frediano Ziglio
            if (start < old_start) {
767 f214978a Kevin Wolf
                /* Stop at the start of a running allocation */
768 ee18e730 Frediano Ziglio
                nb_clusters = old_start - start;
769 f214978a Kevin Wolf
            } else {
770 f214978a Kevin Wolf
                nb_clusters = 0;
771 f214978a Kevin Wolf
            }
772 f214978a Kevin Wolf
773 f214978a Kevin Wolf
            if (nb_clusters == 0) {
774 68d100e9 Kevin Wolf
                /* Wait for the dependency to complete. We need to recheck
775 68d100e9 Kevin Wolf
                 * the free/allocated clusters when we continue. */
776 68d100e9 Kevin Wolf
                qemu_co_mutex_unlock(&s->lock);
777 68d100e9 Kevin Wolf
                qemu_co_queue_wait(&old_alloc->dependent_requests);
778 68d100e9 Kevin Wolf
                qemu_co_mutex_lock(&s->lock);
779 68d100e9 Kevin Wolf
                goto again;
780 f214978a Kevin Wolf
            }
781 f214978a Kevin Wolf
        }
782 f214978a Kevin Wolf
    }
783 f214978a Kevin Wolf
784 f214978a Kevin Wolf
    if (!nb_clusters) {
785 f214978a Kevin Wolf
        abort();
786 f214978a Kevin Wolf
    }
787 f214978a Kevin Wolf
788 05140499 Frediano Ziglio
    /* save info needed for meta data update */
789 05140499 Frediano Ziglio
    m->offset = offset;
790 05140499 Frediano Ziglio
    m->n_start = n_start;
791 05140499 Frediano Ziglio
    m->nb_clusters = nb_clusters;
792 05140499 Frediano Ziglio
793 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
794 f214978a Kevin Wolf
795 45aba42f Kevin Wolf
    /* allocate a new cluster */
796 45aba42f Kevin Wolf
797 ed6ccf0f Kevin Wolf
    cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
798 5d757b56 Kevin Wolf
    if (cluster_offset < 0) {
799 29c1a730 Kevin Wolf
        ret = cluster_offset;
800 29c1a730 Kevin Wolf
        goto fail;
801 5d757b56 Kevin Wolf
    }
802 45aba42f Kevin Wolf
803 45aba42f Kevin Wolf
out:
804 29c1a730 Kevin Wolf
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
805 29c1a730 Kevin Wolf
    if (ret < 0) {
806 9e2a3701 Kevin Wolf
        goto fail_put;
807 29c1a730 Kevin Wolf
    }
808 29c1a730 Kevin Wolf
809 45aba42f Kevin Wolf
    m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
810 148da7ea Kevin Wolf
    m->cluster_offset = cluster_offset;
811 45aba42f Kevin Wolf
812 45aba42f Kevin Wolf
    *num = m->nb_available - n_start;
813 45aba42f Kevin Wolf
814 148da7ea Kevin Wolf
    return 0;
815 29c1a730 Kevin Wolf
816 29c1a730 Kevin Wolf
fail:
817 29c1a730 Kevin Wolf
    qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
818 9e2a3701 Kevin Wolf
fail_put:
819 9e2a3701 Kevin Wolf
    QLIST_REMOVE(m, next_in_flight);
820 29c1a730 Kevin Wolf
    return ret;
821 45aba42f Kevin Wolf
}
822 45aba42f Kevin Wolf
823 45aba42f Kevin Wolf
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
824 45aba42f Kevin Wolf
                             const uint8_t *buf, int buf_size)
825 45aba42f Kevin Wolf
{
826 45aba42f Kevin Wolf
    z_stream strm1, *strm = &strm1;
827 45aba42f Kevin Wolf
    int ret, out_len;
828 45aba42f Kevin Wolf
829 45aba42f Kevin Wolf
    memset(strm, 0, sizeof(*strm));
830 45aba42f Kevin Wolf
831 45aba42f Kevin Wolf
    strm->next_in = (uint8_t *)buf;
832 45aba42f Kevin Wolf
    strm->avail_in = buf_size;
833 45aba42f Kevin Wolf
    strm->next_out = out_buf;
834 45aba42f Kevin Wolf
    strm->avail_out = out_buf_size;
835 45aba42f Kevin Wolf
836 45aba42f Kevin Wolf
    ret = inflateInit2(strm, -12);
837 45aba42f Kevin Wolf
    if (ret != Z_OK)
838 45aba42f Kevin Wolf
        return -1;
839 45aba42f Kevin Wolf
    ret = inflate(strm, Z_FINISH);
840 45aba42f Kevin Wolf
    out_len = strm->next_out - out_buf;
841 45aba42f Kevin Wolf
    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
842 45aba42f Kevin Wolf
        out_len != out_buf_size) {
843 45aba42f Kevin Wolf
        inflateEnd(strm);
844 45aba42f Kevin Wolf
        return -1;
845 45aba42f Kevin Wolf
    }
846 45aba42f Kevin Wolf
    inflateEnd(strm);
847 45aba42f Kevin Wolf
    return 0;
848 45aba42f Kevin Wolf
}
849 45aba42f Kevin Wolf
850 66f82cee Kevin Wolf
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
851 45aba42f Kevin Wolf
{
852 66f82cee Kevin Wolf
    BDRVQcowState *s = bs->opaque;
853 45aba42f Kevin Wolf
    int ret, csize, nb_csectors, sector_offset;
854 45aba42f Kevin Wolf
    uint64_t coffset;
855 45aba42f Kevin Wolf
856 45aba42f Kevin Wolf
    coffset = cluster_offset & s->cluster_offset_mask;
857 45aba42f Kevin Wolf
    if (s->cluster_cache_offset != coffset) {
858 45aba42f Kevin Wolf
        nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
859 45aba42f Kevin Wolf
        sector_offset = coffset & 511;
860 45aba42f Kevin Wolf
        csize = nb_csectors * 512 - sector_offset;
861 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
862 66f82cee Kevin Wolf
        ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data, nb_csectors);
863 45aba42f Kevin Wolf
        if (ret < 0) {
864 8af36488 Kevin Wolf
            return ret;
865 45aba42f Kevin Wolf
        }
866 45aba42f Kevin Wolf
        if (decompress_buffer(s->cluster_cache, s->cluster_size,
867 45aba42f Kevin Wolf
                              s->cluster_data + sector_offset, csize) < 0) {
868 8af36488 Kevin Wolf
            return -EIO;
869 45aba42f Kevin Wolf
        }
870 45aba42f Kevin Wolf
        s->cluster_cache_offset = coffset;
871 45aba42f Kevin Wolf
    }
872 45aba42f Kevin Wolf
    return 0;
873 45aba42f Kevin Wolf
}
874 5ea929e3 Kevin Wolf
875 5ea929e3 Kevin Wolf
/*
876 5ea929e3 Kevin Wolf
 * This discards as many clusters of nb_clusters as possible at once (i.e.
877 5ea929e3 Kevin Wolf
 * all clusters in the same L2 table) and returns the number of discarded
878 5ea929e3 Kevin Wolf
 * clusters.
879 5ea929e3 Kevin Wolf
 */
880 5ea929e3 Kevin Wolf
static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
881 5ea929e3 Kevin Wolf
    unsigned int nb_clusters)
882 5ea929e3 Kevin Wolf
{
883 5ea929e3 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
884 5ea929e3 Kevin Wolf
    uint64_t l2_offset, *l2_table;
885 5ea929e3 Kevin Wolf
    int l2_index;
886 5ea929e3 Kevin Wolf
    int ret;
887 5ea929e3 Kevin Wolf
    int i;
888 5ea929e3 Kevin Wolf
889 5ea929e3 Kevin Wolf
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
890 5ea929e3 Kevin Wolf
    if (ret < 0) {
891 5ea929e3 Kevin Wolf
        return ret;
892 5ea929e3 Kevin Wolf
    }
893 5ea929e3 Kevin Wolf
894 5ea929e3 Kevin Wolf
    /* Limit nb_clusters to one L2 table */
895 5ea929e3 Kevin Wolf
    nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
896 5ea929e3 Kevin Wolf
897 5ea929e3 Kevin Wolf
    for (i = 0; i < nb_clusters; i++) {
898 5ea929e3 Kevin Wolf
        uint64_t old_offset;
899 5ea929e3 Kevin Wolf
900 5ea929e3 Kevin Wolf
        old_offset = be64_to_cpu(l2_table[l2_index + i]);
901 5ea929e3 Kevin Wolf
        old_offset &= ~QCOW_OFLAG_COPIED;
902 5ea929e3 Kevin Wolf
903 5ea929e3 Kevin Wolf
        if (old_offset == 0) {
904 5ea929e3 Kevin Wolf
            continue;
905 5ea929e3 Kevin Wolf
        }
906 5ea929e3 Kevin Wolf
907 5ea929e3 Kevin Wolf
        /* First remove L2 entries */
908 5ea929e3 Kevin Wolf
        qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
909 5ea929e3 Kevin Wolf
        l2_table[l2_index + i] = cpu_to_be64(0);
910 5ea929e3 Kevin Wolf
911 5ea929e3 Kevin Wolf
        /* Then decrease the refcount */
912 5ea929e3 Kevin Wolf
        qcow2_free_any_clusters(bs, old_offset, 1);
913 5ea929e3 Kevin Wolf
    }
914 5ea929e3 Kevin Wolf
915 5ea929e3 Kevin Wolf
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
916 5ea929e3 Kevin Wolf
    if (ret < 0) {
917 5ea929e3 Kevin Wolf
        return ret;
918 5ea929e3 Kevin Wolf
    }
919 5ea929e3 Kevin Wolf
920 5ea929e3 Kevin Wolf
    return nb_clusters;
921 5ea929e3 Kevin Wolf
}
922 5ea929e3 Kevin Wolf
923 5ea929e3 Kevin Wolf
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
924 5ea929e3 Kevin Wolf
    int nb_sectors)
925 5ea929e3 Kevin Wolf
{
926 5ea929e3 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
927 5ea929e3 Kevin Wolf
    uint64_t end_offset;
928 5ea929e3 Kevin Wolf
    unsigned int nb_clusters;
929 5ea929e3 Kevin Wolf
    int ret;
930 5ea929e3 Kevin Wolf
931 5ea929e3 Kevin Wolf
    end_offset = offset + (nb_sectors << BDRV_SECTOR_BITS);
932 5ea929e3 Kevin Wolf
933 5ea929e3 Kevin Wolf
    /* Round start up and end down */
934 5ea929e3 Kevin Wolf
    offset = align_offset(offset, s->cluster_size);
935 5ea929e3 Kevin Wolf
    end_offset &= ~(s->cluster_size - 1);
936 5ea929e3 Kevin Wolf
937 5ea929e3 Kevin Wolf
    if (offset > end_offset) {
938 5ea929e3 Kevin Wolf
        return 0;
939 5ea929e3 Kevin Wolf
    }
940 5ea929e3 Kevin Wolf
941 5ea929e3 Kevin Wolf
    nb_clusters = size_to_clusters(s, end_offset - offset);
942 5ea929e3 Kevin Wolf
943 5ea929e3 Kevin Wolf
    /* Each L2 table is handled by its own loop iteration */
944 5ea929e3 Kevin Wolf
    while (nb_clusters > 0) {
945 5ea929e3 Kevin Wolf
        ret = discard_single_l2(bs, offset, nb_clusters);
946 5ea929e3 Kevin Wolf
        if (ret < 0) {
947 5ea929e3 Kevin Wolf
            return ret;
948 5ea929e3 Kevin Wolf
        }
949 5ea929e3 Kevin Wolf
950 5ea929e3 Kevin Wolf
        nb_clusters -= ret;
951 5ea929e3 Kevin Wolf
        offset += (ret * s->cluster_size);
952 5ea929e3 Kevin Wolf
    }
953 5ea929e3 Kevin Wolf
954 5ea929e3 Kevin Wolf
    return 0;
955 5ea929e3 Kevin Wolf
}