Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ 018faafd

History | View | Annotate | Download (37.9 kB)

1
/*
2
 * Block driver for the QCOW version 2 format
3
 *
4
 * Copyright (c) 2004-2006 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24

    
25
#include "qemu-common.h"
26
#include "block_int.h"
27
#include "block/qcow2.h"
28

    
29
static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size);
30
static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
31
                            int64_t offset, int64_t length,
32
                            int addend);
33

    
34

    
35
static int cache_refcount_updates = 0;
36

    
37
static int write_refcount_block(BlockDriverState *bs)
38
{
39
    BDRVQcowState *s = bs->opaque;
40
    size_t size = s->cluster_size;
41

    
42
    if (s->refcount_block_cache_offset == 0) {
43
        return 0;
44
    }
45

    
46
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE);
47
    if (bdrv_pwrite(bs->file, s->refcount_block_cache_offset,
48
            s->refcount_block_cache, size) != size)
49
    {
50
        return -EIO;
51
    }
52

    
53
    return 0;
54
}
55

    
56
/*********************************************************/
57
/* refcount handling */
58

    
59
int qcow2_refcount_init(BlockDriverState *bs)
60
{
61
    BDRVQcowState *s = bs->opaque;
62
    int ret, refcount_table_size2, i;
63

    
64
    s->refcount_block_cache = qemu_malloc(s->cluster_size);
65
    refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
66
    s->refcount_table = qemu_malloc(refcount_table_size2);
67
    if (s->refcount_table_size > 0) {
68
        BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD);
69
        ret = bdrv_pread(bs->file, s->refcount_table_offset,
70
                         s->refcount_table, refcount_table_size2);
71
        if (ret != refcount_table_size2)
72
            goto fail;
73
        for(i = 0; i < s->refcount_table_size; i++)
74
            be64_to_cpus(&s->refcount_table[i]);
75
    }
76
    return 0;
77
 fail:
78
    return -ENOMEM;
79
}
80

    
81
void qcow2_refcount_close(BlockDriverState *bs)
82
{
83
    BDRVQcowState *s = bs->opaque;
84
    qemu_free(s->refcount_block_cache);
85
    qemu_free(s->refcount_table);
86
}
87

    
88

    
89
static int load_refcount_block(BlockDriverState *bs,
90
                               int64_t refcount_block_offset)
91
{
92
    BDRVQcowState *s = bs->opaque;
93
    int ret;
94

    
95
    if (cache_refcount_updates) {
96
        write_refcount_block(bs);
97
    }
98

    
99
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD);
100
    ret = bdrv_pread(bs->file, refcount_block_offset, s->refcount_block_cache,
101
                     s->cluster_size);
102
    if (ret != s->cluster_size)
103
        return -EIO;
104
    s->refcount_block_cache_offset = refcount_block_offset;
105
    return 0;
106
}
107

    
108
/*
109
 * Returns the refcount of the cluster given by its index. Any non-negative
110
 * return value is the refcount of the cluster, negative values are -errno
111
 * and indicate an error.
112
 */
113
static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
114
{
115
    BDRVQcowState *s = bs->opaque;
116
    int refcount_table_index, block_index;
117
    int64_t refcount_block_offset;
118
    int ret;
119

    
120
    refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
121
    if (refcount_table_index >= s->refcount_table_size)
122
        return 0;
123
    refcount_block_offset = s->refcount_table[refcount_table_index];
124
    if (!refcount_block_offset)
125
        return 0;
126
    if (refcount_block_offset != s->refcount_block_cache_offset) {
127
        /* better than nothing: return allocated if read error */
128
        ret = load_refcount_block(bs, refcount_block_offset);
129
        if (ret < 0) {
130
            return ret;
131
        }
132
    }
133
    block_index = cluster_index &
134
        ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
135
    return be16_to_cpu(s->refcount_block_cache[block_index]);
136
}
137

    
138
/*
139
 * Rounds the refcount table size up to avoid growing the table for each single
140
 * refcount block that is allocated.
141
 */
142
static unsigned int next_refcount_table_size(BDRVQcowState *s,
143
    unsigned int min_size)
144
{
145
    unsigned int min_clusters = (min_size >> (s->cluster_bits - 3)) + 1;
146
    unsigned int refcount_table_clusters =
147
        MAX(1, s->refcount_table_size >> (s->cluster_bits - 3));
148

    
149
    while (min_clusters > refcount_table_clusters) {
150
        refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
151
    }
152

    
153
    return refcount_table_clusters << (s->cluster_bits - 3);
154
}
155

    
156

    
157
/* Checks if two offsets are described by the same refcount block */
158
static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a,
159
    uint64_t offset_b)
160
{
161
    uint64_t block_a = offset_a >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
162
    uint64_t block_b = offset_b >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
163

    
164
    return (block_a == block_b);
165
}
166

    
167
/*
168
 * Loads a refcount block. If it doesn't exist yet, it is allocated first
169
 * (including growing the refcount table if needed).
170
 *
171
 * Returns the offset of the refcount block on success or -errno in error case
172
 */
173
static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index)
174
{
175
    BDRVQcowState *s = bs->opaque;
176
    unsigned int refcount_table_index;
177
    int ret;
178

    
179
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
180

    
181
    /* Find the refcount block for the given cluster */
182
    refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
183

    
184
    if (refcount_table_index < s->refcount_table_size) {
185

    
186
        uint64_t refcount_block_offset =
187
            s->refcount_table[refcount_table_index];
188

    
189
        /* If it's already there, we're done */
190
        if (refcount_block_offset) {
191
            if (refcount_block_offset != s->refcount_block_cache_offset) {
192
                ret = load_refcount_block(bs, refcount_block_offset);
193
                if (ret < 0) {
194
                    return ret;
195
                }
196
            }
197
            return refcount_block_offset;
198
        }
199
    }
200

    
201
    /*
202
     * If we came here, we need to allocate something. Something is at least
203
     * a cluster for the new refcount block. It may also include a new refcount
204
     * table if the old refcount table is too small.
205
     *
206
     * Note that allocating clusters here needs some special care:
207
     *
208
     * - We can't use the normal qcow2_alloc_clusters(), it would try to
209
     *   increase the refcount and very likely we would end up with an endless
210
     *   recursion. Instead we must place the refcount blocks in a way that
211
     *   they can describe them themselves.
212
     *
213
     * - We need to consider that at this point we are inside update_refcounts
214
     *   and doing the initial refcount increase. This means that some clusters
215
     *   have already been allocated by the caller, but their refcount isn't
216
     *   accurate yet. free_cluster_index tells us where this allocation ends
217
     *   as long as we don't overwrite it by freeing clusters.
218
     *
219
     * - alloc_clusters_noref and qcow2_free_clusters may load a different
220
     *   refcount block into the cache
221
     */
222

    
223
    if (cache_refcount_updates) {
224
        ret = write_refcount_block(bs);
225
        if (ret < 0) {
226
            return ret;
227
        }
228
    }
229

    
230
    /* Allocate the refcount block itself and mark it as used */
231
    uint64_t new_block = alloc_clusters_noref(bs, s->cluster_size);
232

    
233
#ifdef DEBUG_ALLOC2
234
    fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64
235
        " at %" PRIx64 "\n",
236
        refcount_table_index, cluster_index << s->cluster_bits, new_block);
237
#endif
238

    
239
    if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) {
240
        /* Zero the new refcount block before updating it */
241
        memset(s->refcount_block_cache, 0, s->cluster_size);
242
        s->refcount_block_cache_offset = new_block;
243

    
244
        /* The block describes itself, need to update the cache */
245
        int block_index = (new_block >> s->cluster_bits) &
246
            ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
247
        s->refcount_block_cache[block_index] = cpu_to_be16(1);
248
    } else {
249
        /* Described somewhere else. This can recurse at most twice before we
250
         * arrive at a block that describes itself. */
251
        ret = update_refcount(bs, new_block, s->cluster_size, 1);
252
        if (ret < 0) {
253
            goto fail_block;
254
        }
255

    
256
        /* Initialize the new refcount block only after updating its refcount,
257
         * update_refcount uses the refcount cache itself */
258
        memset(s->refcount_block_cache, 0, s->cluster_size);
259
        s->refcount_block_cache_offset = new_block;
260
    }
261

    
262
    /* Now the new refcount block needs to be written to disk */
263
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
264
    ret = bdrv_pwrite(bs->file, new_block, s->refcount_block_cache,
265
        s->cluster_size);
266
    if (ret < 0) {
267
        goto fail_block;
268
    }
269

    
270
    /* If the refcount table is big enough, just hook the block up there */
271
    if (refcount_table_index < s->refcount_table_size) {
272
        uint64_t data64 = cpu_to_be64(new_block);
273
        BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP);
274
        ret = bdrv_pwrite(bs->file,
275
            s->refcount_table_offset + refcount_table_index * sizeof(uint64_t),
276
            &data64, sizeof(data64));
277
        if (ret < 0) {
278
            goto fail_block;
279
        }
280

    
281
        s->refcount_table[refcount_table_index] = new_block;
282
        return new_block;
283
    }
284

    
285
    /*
286
     * If we come here, we need to grow the refcount table. Again, a new
287
     * refcount table needs some space and we can't simply allocate to avoid
288
     * endless recursion.
289
     *
290
     * Therefore let's grab new refcount blocks at the end of the image, which
291
     * will describe themselves and the new refcount table. This way we can
292
     * reference them only in the new table and do the switch to the new
293
     * refcount table at once without producing an inconsistent state in
294
     * between.
295
     */
296
    BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
297

    
298
    /* Calculate the number of refcount blocks needed so far */
299
    uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT);
300
    uint64_t blocks_used = (s->free_cluster_index +
301
        refcount_block_clusters - 1) / refcount_block_clusters;
302

    
303
    /* And now we need at least one block more for the new metadata */
304
    uint64_t table_size = next_refcount_table_size(s, blocks_used + 1);
305
    uint64_t last_table_size;
306
    uint64_t blocks_clusters;
307
    do {
308
        uint64_t table_clusters = size_to_clusters(s, table_size);
309
        blocks_clusters = 1 +
310
            ((table_clusters + refcount_block_clusters - 1)
311
            / refcount_block_clusters);
312
        uint64_t meta_clusters = table_clusters + blocks_clusters;
313

    
314
        last_table_size = table_size;
315
        table_size = next_refcount_table_size(s, blocks_used +
316
            ((meta_clusters + refcount_block_clusters - 1)
317
            / refcount_block_clusters));
318

    
319
    } while (last_table_size != table_size);
320

    
321
#ifdef DEBUG_ALLOC2
322
    fprintf(stderr, "qcow2: Grow refcount table %" PRId32 " => %" PRId64 "\n",
323
        s->refcount_table_size, table_size);
324
#endif
325

    
326
    /* Create the new refcount table and blocks */
327
    uint64_t meta_offset = (blocks_used * refcount_block_clusters) *
328
        s->cluster_size;
329
    uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
330
    uint16_t *new_blocks = qemu_mallocz(blocks_clusters * s->cluster_size);
331
    uint64_t *new_table = qemu_mallocz(table_size * sizeof(uint64_t));
332

    
333
    assert(meta_offset >= (s->free_cluster_index * s->cluster_size));
334

    
335
    /* Fill the new refcount table */
336
    memcpy(new_table, s->refcount_table,
337
        s->refcount_table_size * sizeof(uint64_t));
338
    new_table[refcount_table_index] = new_block;
339

    
340
    int i;
341
    for (i = 0; i < blocks_clusters; i++) {
342
        new_table[blocks_used + i] = meta_offset + (i * s->cluster_size);
343
    }
344

    
345
    /* Fill the refcount blocks */
346
    uint64_t table_clusters = size_to_clusters(s, table_size * sizeof(uint64_t));
347
    int block = 0;
348
    for (i = 0; i < table_clusters + blocks_clusters; i++) {
349
        new_blocks[block++] = cpu_to_be16(1);
350
    }
351

    
352
    /* Write refcount blocks to disk */
353
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
354
    ret = bdrv_pwrite(bs->file, meta_offset, new_blocks,
355
        blocks_clusters * s->cluster_size);
356
    qemu_free(new_blocks);
357
    if (ret < 0) {
358
        goto fail_table;
359
    }
360

    
361
    /* Write refcount table to disk */
362
    for(i = 0; i < table_size; i++) {
363
        cpu_to_be64s(&new_table[i]);
364
    }
365

    
366
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
367
    ret = bdrv_pwrite(bs->file, table_offset, new_table,
368
        table_size * sizeof(uint64_t));
369
    if (ret < 0) {
370
        goto fail_table;
371
    }
372

    
373
    for(i = 0; i < table_size; i++) {
374
        cpu_to_be64s(&new_table[i]);
375
    }
376

    
377
    /* Hook up the new refcount table in the qcow2 header */
378
    uint8_t data[12];
379
    cpu_to_be64w((uint64_t*)data, table_offset);
380
    cpu_to_be32w((uint32_t*)(data + 8), table_clusters);
381
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
382
    ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, refcount_table_offset),
383
        data, sizeof(data));
384
    if (ret < 0) {
385
        goto fail_table;
386
    }
387

    
388
    /* And switch it in memory */
389
    uint64_t old_table_offset = s->refcount_table_offset;
390
    uint64_t old_table_size = s->refcount_table_size;
391

    
392
    qemu_free(s->refcount_table);
393
    s->refcount_table = new_table;
394
    s->refcount_table_size = table_size;
395
    s->refcount_table_offset = table_offset;
396

    
397
    /* Free old table. Remember, we must not change free_cluster_index */
398
    uint64_t old_free_cluster_index = s->free_cluster_index;
399
    qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
400
    s->free_cluster_index = old_free_cluster_index;
401

    
402
    ret = load_refcount_block(bs, new_block);
403
    if (ret < 0) {
404
        goto fail_block;
405
    }
406

    
407
    return new_block;
408

    
409
fail_table:
410
    qemu_free(new_table);
411
fail_block:
412
    s->refcount_block_cache_offset = 0;
413
    return ret;
414
}
415

    
416
#define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
417
static int write_refcount_block_entries(BlockDriverState *bs,
418
    int64_t refcount_block_offset, int first_index, int last_index)
419
{
420
    BDRVQcowState *s = bs->opaque;
421
    size_t size;
422
    int ret;
423

    
424
    if (cache_refcount_updates) {
425
        return 0;
426
    }
427

    
428
    if (first_index < 0) {
429
        return 0;
430
    }
431

    
432
    first_index &= ~(REFCOUNTS_PER_SECTOR - 1);
433
    last_index = (last_index + REFCOUNTS_PER_SECTOR)
434
        & ~(REFCOUNTS_PER_SECTOR - 1);
435

    
436
    size = (last_index - first_index) << REFCOUNT_SHIFT;
437

    
438
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
439
    ret = bdrv_pwrite(bs->file,
440
        refcount_block_offset + (first_index << REFCOUNT_SHIFT),
441
        &s->refcount_block_cache[first_index], size);
442
    if (ret < 0) {
443
        return ret;
444
    }
445

    
446
    return 0;
447
}
448

    
449
/* XXX: cache several refcount block clusters ? */
450
static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
451
    int64_t offset, int64_t length, int addend)
452
{
453
    BDRVQcowState *s = bs->opaque;
454
    int64_t start, last, cluster_offset;
455
    int64_t refcount_block_offset = 0;
456
    int64_t table_index = -1, old_table_index;
457
    int first_index = -1, last_index = -1;
458
    int ret;
459

    
460
#ifdef DEBUG_ALLOC2
461
    printf("update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
462
           offset, length, addend);
463
#endif
464
    if (length < 0) {
465
        return -EINVAL;
466
    } else if (length == 0) {
467
        return 0;
468
    }
469

    
470
    start = offset & ~(s->cluster_size - 1);
471
    last = (offset + length - 1) & ~(s->cluster_size - 1);
472
    for(cluster_offset = start; cluster_offset <= last;
473
        cluster_offset += s->cluster_size)
474
    {
475
        int block_index, refcount;
476
        int64_t cluster_index = cluster_offset >> s->cluster_bits;
477
        int64_t new_block;
478

    
479
        /* Only write refcount block to disk when we are done with it */
480
        old_table_index = table_index;
481
        table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
482
        if ((old_table_index >= 0) && (table_index != old_table_index)) {
483

    
484
            ret = write_refcount_block_entries(bs, refcount_block_offset,
485
                first_index, last_index);
486
            if (ret < 0) {
487
                return ret;
488
            }
489

    
490
            first_index = -1;
491
            last_index = -1;
492
        }
493

    
494
        /* Load the refcount block and allocate it if needed */
495
        new_block = alloc_refcount_block(bs, cluster_index);
496
        if (new_block < 0) {
497
            ret = new_block;
498
            goto fail;
499
        }
500
        refcount_block_offset = new_block;
501

    
502
        /* we can update the count and save it */
503
        block_index = cluster_index &
504
            ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
505
        if (first_index == -1 || block_index < first_index) {
506
            first_index = block_index;
507
        }
508
        if (block_index > last_index) {
509
            last_index = block_index;
510
        }
511

    
512
        refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
513
        refcount += addend;
514
        if (refcount < 0 || refcount > 0xffff) {
515
            ret = -EINVAL;
516
            goto fail;
517
        }
518
        if (refcount == 0 && cluster_index < s->free_cluster_index) {
519
            s->free_cluster_index = cluster_index;
520
        }
521
        s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
522
    }
523

    
524
    ret = 0;
525
fail:
526

    
527
    /* Write last changed block to disk */
528
    if (refcount_block_offset != 0) {
529
        int wret;
530
        wret = write_refcount_block_entries(bs, refcount_block_offset,
531
            first_index, last_index);
532
        if (wret < 0) {
533
            return ret < 0 ? ret : wret;
534
        }
535
    }
536

    
537
    /*
538
     * Try do undo any updates if an error is returned (This may succeed in
539
     * some cases like ENOSPC for allocating a new refcount block)
540
     */
541
    if (ret < 0) {
542
        int dummy;
543
        dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
544
    }
545

    
546
    return ret;
547
}
548

    
549
/*
550
 * Increases or decreases the refcount of a given cluster by one.
551
 * addend must be 1 or -1.
552
 *
553
 * If the return value is non-negative, it is the new refcount of the cluster.
554
 * If it is negative, it is -errno and indicates an error.
555
 */
556
static int update_cluster_refcount(BlockDriverState *bs,
557
                                   int64_t cluster_index,
558
                                   int addend)
559
{
560
    BDRVQcowState *s = bs->opaque;
561
    int ret;
562

    
563
    ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
564
    if (ret < 0) {
565
        return ret;
566
    }
567

    
568
    return get_refcount(bs, cluster_index);
569
}
570

    
571

    
572

    
573
/*********************************************************/
574
/* cluster allocation functions */
575

    
576

    
577

    
578
/* return < 0 if error */
579
static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
580
{
581
    BDRVQcowState *s = bs->opaque;
582
    int i, nb_clusters;
583

    
584
    nb_clusters = size_to_clusters(s, size);
585
retry:
586
    for(i = 0; i < nb_clusters; i++) {
587
        int64_t next_cluster_index = s->free_cluster_index++;
588
        if (get_refcount(bs, next_cluster_index) != 0)
589
            goto retry;
590
    }
591
#ifdef DEBUG_ALLOC2
592
    printf("alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
593
            size,
594
            (s->free_cluster_index - nb_clusters) << s->cluster_bits);
595
#endif
596
    return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
597
}
598

    
599
int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
600
{
601
    int64_t offset;
602
    int ret;
603

    
604
    BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
605
    offset = alloc_clusters_noref(bs, size);
606
    ret = update_refcount(bs, offset, size, 1);
607
    if (ret < 0) {
608
        return ret;
609
    }
610
    return offset;
611
}
612

    
613
/* only used to allocate compressed sectors. We try to allocate
614
   contiguous sectors. size must be <= cluster_size */
615
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
616
{
617
    BDRVQcowState *s = bs->opaque;
618
    int64_t offset, cluster_offset;
619
    int free_in_cluster;
620

    
621
    BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
622
    assert(size > 0 && size <= s->cluster_size);
623
    if (s->free_byte_offset == 0) {
624
        s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
625
        if (s->free_byte_offset < 0) {
626
            return s->free_byte_offset;
627
        }
628
    }
629
 redo:
630
    free_in_cluster = s->cluster_size -
631
        (s->free_byte_offset & (s->cluster_size - 1));
632
    if (size <= free_in_cluster) {
633
        /* enough space in current cluster */
634
        offset = s->free_byte_offset;
635
        s->free_byte_offset += size;
636
        free_in_cluster -= size;
637
        if (free_in_cluster == 0)
638
            s->free_byte_offset = 0;
639
        if ((offset & (s->cluster_size - 1)) != 0)
640
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
641
    } else {
642
        offset = qcow2_alloc_clusters(bs, s->cluster_size);
643
        if (offset < 0) {
644
            return offset;
645
        }
646
        cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
647
        if ((cluster_offset + s->cluster_size) == offset) {
648
            /* we are lucky: contiguous data */
649
            offset = s->free_byte_offset;
650
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
651
            s->free_byte_offset += size;
652
        } else {
653
            s->free_byte_offset = offset;
654
            goto redo;
655
        }
656
    }
657
    return offset;
658
}
659

    
660
void qcow2_free_clusters(BlockDriverState *bs,
661
                          int64_t offset, int64_t size)
662
{
663
    int ret;
664

    
665
    BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
666
    ret = update_refcount(bs, offset, size, -1);
667
    if (ret < 0) {
668
        fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
669
        /* TODO Remember the clusters to free them later and avoid leaking */
670
    }
671
}
672

    
673
/*
674
 * free_any_clusters
675
 *
676
 * free clusters according to its type: compressed or not
677
 *
678
 */
679

    
680
void qcow2_free_any_clusters(BlockDriverState *bs,
681
    uint64_t cluster_offset, int nb_clusters)
682
{
683
    BDRVQcowState *s = bs->opaque;
684

    
685
    /* free the cluster */
686

    
687
    if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
688
        int nb_csectors;
689
        nb_csectors = ((cluster_offset >> s->csize_shift) &
690
                       s->csize_mask) + 1;
691
        qcow2_free_clusters(bs,
692
            (cluster_offset & s->cluster_offset_mask) & ~511,
693
            nb_csectors * 512);
694
        return;
695
    }
696

    
697
    qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
698

    
699
    return;
700
}
701

    
702

    
703

    
704
/*********************************************************/
705
/* snapshots and image creation */
706

    
707

    
708

    
709
void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
710
    int64_t size)
711
{
712
    int refcount;
713
    int64_t start, last, cluster_offset;
714
    uint16_t *p;
715

    
716
    start = offset & ~(s->cluster_size - 1);
717
    last = (offset + size - 1)  & ~(s->cluster_size - 1);
718
    for(cluster_offset = start; cluster_offset <= last;
719
        cluster_offset += s->cluster_size) {
720
        p = &s->refcount_block[cluster_offset >> s->cluster_bits];
721
        refcount = be16_to_cpu(*p);
722
        refcount++;
723
        *p = cpu_to_be16(refcount);
724
    }
725
}
726

    
727
/* update the refcounts of snapshots and the copied flag */
728
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
729
    int64_t l1_table_offset, int l1_size, int addend)
730
{
731
    BDRVQcowState *s = bs->opaque;
732
    uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
733
    int64_t old_offset, old_l2_offset;
734
    int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
735

    
736
    qcow2_l2_cache_reset(bs);
737
    cache_refcount_updates = 1;
738

    
739
    l2_table = NULL;
740
    l1_table = NULL;
741
    l1_size2 = l1_size * sizeof(uint64_t);
742
    if (l1_table_offset != s->l1_table_offset) {
743
        if (l1_size2 != 0) {
744
            l1_table = qemu_mallocz(align_offset(l1_size2, 512));
745
        } else {
746
            l1_table = NULL;
747
        }
748
        l1_allocated = 1;
749
        if (bdrv_pread(bs->file, l1_table_offset,
750
                       l1_table, l1_size2) != l1_size2)
751
            goto fail;
752
        for(i = 0;i < l1_size; i++)
753
            be64_to_cpus(&l1_table[i]);
754
    } else {
755
        assert(l1_size == s->l1_size);
756
        l1_table = s->l1_table;
757
        l1_allocated = 0;
758
    }
759

    
760
    l2_size = s->l2_size * sizeof(uint64_t);
761
    l2_table = qemu_malloc(l2_size);
762
    l1_modified = 0;
763
    for(i = 0; i < l1_size; i++) {
764
        l2_offset = l1_table[i];
765
        if (l2_offset) {
766
            old_l2_offset = l2_offset;
767
            l2_offset &= ~QCOW_OFLAG_COPIED;
768
            l2_modified = 0;
769
            if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
770
                goto fail;
771
            for(j = 0; j < s->l2_size; j++) {
772
                offset = be64_to_cpu(l2_table[j]);
773
                if (offset != 0) {
774
                    old_offset = offset;
775
                    offset &= ~QCOW_OFLAG_COPIED;
776
                    if (offset & QCOW_OFLAG_COMPRESSED) {
777
                        nb_csectors = ((offset >> s->csize_shift) &
778
                                       s->csize_mask) + 1;
779
                        if (addend != 0) {
780
                            int ret;
781
                            ret = update_refcount(bs,
782
                                (offset & s->cluster_offset_mask) & ~511,
783
                                nb_csectors * 512, addend);
784
                            if (ret < 0) {
785
                                goto fail;
786
                            }
787
                        }
788
                        /* compressed clusters are never modified */
789
                        refcount = 2;
790
                    } else {
791
                        if (addend != 0) {
792
                            refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
793
                        } else {
794
                            refcount = get_refcount(bs, offset >> s->cluster_bits);
795
                        }
796

    
797
                        if (refcount < 0) {
798
                            goto fail;
799
                        }
800
                    }
801

    
802
                    if (refcount == 1) {
803
                        offset |= QCOW_OFLAG_COPIED;
804
                    }
805
                    if (offset != old_offset) {
806
                        l2_table[j] = cpu_to_be64(offset);
807
                        l2_modified = 1;
808
                    }
809
                }
810
            }
811
            if (l2_modified) {
812
                if (bdrv_pwrite(bs->file,
813
                                l2_offset, l2_table, l2_size) != l2_size)
814
                    goto fail;
815
            }
816

    
817
            if (addend != 0) {
818
                refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
819
            } else {
820
                refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
821
            }
822
            if (refcount < 0) {
823
                goto fail;
824
            } else if (refcount == 1) {
825
                l2_offset |= QCOW_OFLAG_COPIED;
826
            }
827
            if (l2_offset != old_l2_offset) {
828
                l1_table[i] = l2_offset;
829
                l1_modified = 1;
830
            }
831
        }
832
    }
833
    if (l1_modified) {
834
        for(i = 0; i < l1_size; i++)
835
            cpu_to_be64s(&l1_table[i]);
836
        if (bdrv_pwrite(bs->file, l1_table_offset, l1_table,
837
                        l1_size2) != l1_size2)
838
            goto fail;
839
        for(i = 0; i < l1_size; i++)
840
            be64_to_cpus(&l1_table[i]);
841
    }
842
    if (l1_allocated)
843
        qemu_free(l1_table);
844
    qemu_free(l2_table);
845
    cache_refcount_updates = 0;
846
    write_refcount_block(bs);
847
    return 0;
848
 fail:
849
    if (l1_allocated)
850
        qemu_free(l1_table);
851
    qemu_free(l2_table);
852
    cache_refcount_updates = 0;
853
    write_refcount_block(bs);
854
    return -EIO;
855
}
856

    
857

    
858

    
859

    
860
/*********************************************************/
861
/* refcount checking functions */
862

    
863

    
864

    
865
/*
866
 * Increases the refcount for a range of clusters in a given refcount table.
867
 * This is used to construct a temporary refcount table out of L1 and L2 tables
868
 * which can be compared the the refcount table saved in the image.
869
 *
870
 * Returns the number of errors in the image that were found
871
 */
872
static int inc_refcounts(BlockDriverState *bs,
873
                          uint16_t *refcount_table,
874
                          int refcount_table_size,
875
                          int64_t offset, int64_t size)
876
{
877
    BDRVQcowState *s = bs->opaque;
878
    int64_t start, last, cluster_offset;
879
    int k;
880
    int errors = 0;
881

    
882
    if (size <= 0)
883
        return 0;
884

    
885
    start = offset & ~(s->cluster_size - 1);
886
    last = (offset + size - 1) & ~(s->cluster_size - 1);
887
    for(cluster_offset = start; cluster_offset <= last;
888
        cluster_offset += s->cluster_size) {
889
        k = cluster_offset >> s->cluster_bits;
890
        if (k < 0 || k >= refcount_table_size) {
891
            fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
892
                cluster_offset);
893
            errors++;
894
        } else {
895
            if (++refcount_table[k] == 0) {
896
                fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
897
                    "\n", cluster_offset);
898
                errors++;
899
            }
900
        }
901
    }
902

    
903
    return errors;
904
}
905

    
906
/*
907
 * Increases the refcount in the given refcount table for the all clusters
908
 * referenced in the L2 table. While doing so, performs some checks on L2
909
 * entries.
910
 *
911
 * Returns the number of errors found by the checks or -errno if an internal
912
 * error occurred.
913
 */
914
static int check_refcounts_l2(BlockDriverState *bs,
915
    uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
916
    int check_copied)
917
{
918
    BDRVQcowState *s = bs->opaque;
919
    uint64_t *l2_table, offset;
920
    int i, l2_size, nb_csectors, refcount;
921
    int errors = 0;
922

    
923
    /* Read L2 table from disk */
924
    l2_size = s->l2_size * sizeof(uint64_t);
925
    l2_table = qemu_malloc(l2_size);
926

    
927
    if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
928
        goto fail;
929

    
930
    /* Do the actual checks */
931
    for(i = 0; i < s->l2_size; i++) {
932
        offset = be64_to_cpu(l2_table[i]);
933
        if (offset != 0) {
934
            if (offset & QCOW_OFLAG_COMPRESSED) {
935
                /* Compressed clusters don't have QCOW_OFLAG_COPIED */
936
                if (offset & QCOW_OFLAG_COPIED) {
937
                    fprintf(stderr, "ERROR: cluster %" PRId64 ": "
938
                        "copied flag must never be set for compressed "
939
                        "clusters\n", offset >> s->cluster_bits);
940
                    offset &= ~QCOW_OFLAG_COPIED;
941
                    errors++;
942
                }
943

    
944
                /* Mark cluster as used */
945
                nb_csectors = ((offset >> s->csize_shift) &
946
                               s->csize_mask) + 1;
947
                offset &= s->cluster_offset_mask;
948
                errors += inc_refcounts(bs, refcount_table,
949
                              refcount_table_size,
950
                              offset & ~511, nb_csectors * 512);
951
            } else {
952
                /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
953
                if (check_copied) {
954
                    uint64_t entry = offset;
955
                    offset &= ~QCOW_OFLAG_COPIED;
956
                    refcount = get_refcount(bs, offset >> s->cluster_bits);
957
                    if (refcount < 0) {
958
                        fprintf(stderr, "Can't get refcount for offset %"
959
                            PRIx64 ": %s\n", entry, strerror(-refcount));
960
                    }
961
                    if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
962
                        fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
963
                            PRIx64 " refcount=%d\n", entry, refcount);
964
                        errors++;
965
                    }
966
                }
967

    
968
                /* Mark cluster as used */
969
                offset &= ~QCOW_OFLAG_COPIED;
970
                errors += inc_refcounts(bs, refcount_table,
971
                              refcount_table_size,
972
                              offset, s->cluster_size);
973

    
974
                /* Correct offsets are cluster aligned */
975
                if (offset & (s->cluster_size - 1)) {
976
                    fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
977
                        "properly aligned; L2 entry corrupted.\n", offset);
978
                    errors++;
979
                }
980
            }
981
        }
982
    }
983

    
984
    qemu_free(l2_table);
985
    return errors;
986

    
987
fail:
988
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
989
    qemu_free(l2_table);
990
    return -EIO;
991
}
992

    
993
/*
994
 * Increases the refcount for the L1 table, its L2 tables and all referenced
995
 * clusters in the given refcount table. While doing so, performs some checks
996
 * on L1 and L2 entries.
997
 *
998
 * Returns the number of errors found by the checks or -errno if an internal
999
 * error occurred.
1000
 */
1001
static int check_refcounts_l1(BlockDriverState *bs,
1002
                              uint16_t *refcount_table,
1003
                              int refcount_table_size,
1004
                              int64_t l1_table_offset, int l1_size,
1005
                              int check_copied)
1006
{
1007
    BDRVQcowState *s = bs->opaque;
1008
    uint64_t *l1_table, l2_offset, l1_size2;
1009
    int i, refcount, ret;
1010
    int errors = 0;
1011

    
1012
    l1_size2 = l1_size * sizeof(uint64_t);
1013

    
1014
    /* Mark L1 table as used */
1015
    errors += inc_refcounts(bs, refcount_table, refcount_table_size,
1016
                  l1_table_offset, l1_size2);
1017

    
1018
    /* Read L1 table entries from disk */
1019
    if (l1_size2 == 0) {
1020
        l1_table = NULL;
1021
    } else {
1022
        l1_table = qemu_malloc(l1_size2);
1023
        if (bdrv_pread(bs->file, l1_table_offset,
1024
                       l1_table, l1_size2) != l1_size2)
1025
            goto fail;
1026
        for(i = 0;i < l1_size; i++)
1027
            be64_to_cpus(&l1_table[i]);
1028
    }
1029

    
1030
    /* Do the actual checks */
1031
    for(i = 0; i < l1_size; i++) {
1032
        l2_offset = l1_table[i];
1033
        if (l2_offset) {
1034
            /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
1035
            if (check_copied) {
1036
                refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
1037
                    >> s->cluster_bits);
1038
                if (refcount < 0) {
1039
                    fprintf(stderr, "Can't get refcount for l2_offset %"
1040
                        PRIx64 ": %s\n", l2_offset, strerror(-refcount));
1041
                }
1042
                if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
1043
                    fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
1044
                        " refcount=%d\n", l2_offset, refcount);
1045
                    errors++;
1046
                }
1047
            }
1048

    
1049
            /* Mark L2 table as used */
1050
            l2_offset &= ~QCOW_OFLAG_COPIED;
1051
            errors += inc_refcounts(bs, refcount_table,
1052
                          refcount_table_size,
1053
                          l2_offset,
1054
                          s->cluster_size);
1055

    
1056
            /* L2 tables are cluster aligned */
1057
            if (l2_offset & (s->cluster_size - 1)) {
1058
                fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1059
                    "cluster aligned; L1 entry corrupted\n", l2_offset);
1060
                errors++;
1061
            }
1062

    
1063
            /* Process and check L2 entries */
1064
            ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
1065
                l2_offset, check_copied);
1066
            if (ret < 0) {
1067
                goto fail;
1068
            }
1069
            errors += ret;
1070
        }
1071
    }
1072
    qemu_free(l1_table);
1073
    return errors;
1074

    
1075
fail:
1076
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1077
    qemu_free(l1_table);
1078
    return -EIO;
1079
}
1080

    
1081
/*
1082
 * Checks an image for refcount consistency.
1083
 *
1084
 * Returns 0 if no errors are found, the number of errors in case the image is
1085
 * detected as corrupted, and -errno when an internal error occured.
1086
 */
1087
int qcow2_check_refcounts(BlockDriverState *bs)
1088
{
1089
    BDRVQcowState *s = bs->opaque;
1090
    int64_t size;
1091
    int nb_clusters, refcount1, refcount2, i;
1092
    QCowSnapshot *sn;
1093
    uint16_t *refcount_table;
1094
    int ret, errors = 0;
1095

    
1096
    size = bdrv_getlength(bs->file);
1097
    nb_clusters = size_to_clusters(s, size);
1098
    refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
1099

    
1100
    /* header */
1101
    errors += inc_refcounts(bs, refcount_table, nb_clusters,
1102
                  0, s->cluster_size);
1103

    
1104
    /* current L1 table */
1105
    ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
1106
                       s->l1_table_offset, s->l1_size, 1);
1107
    if (ret < 0) {
1108
        return ret;
1109
    }
1110
    errors += ret;
1111

    
1112
    /* snapshots */
1113
    for(i = 0; i < s->nb_snapshots; i++) {
1114
        sn = s->snapshots + i;
1115
        check_refcounts_l1(bs, refcount_table, nb_clusters,
1116
                           sn->l1_table_offset, sn->l1_size, 0);
1117
    }
1118
    errors += inc_refcounts(bs, refcount_table, nb_clusters,
1119
                  s->snapshots_offset, s->snapshots_size);
1120

    
1121
    /* refcount data */
1122
    errors += inc_refcounts(bs, refcount_table, nb_clusters,
1123
                  s->refcount_table_offset,
1124
                  s->refcount_table_size * sizeof(uint64_t));
1125
    for(i = 0; i < s->refcount_table_size; i++) {
1126
        int64_t offset;
1127
        offset = s->refcount_table[i];
1128

    
1129
        /* Refcount blocks are cluster aligned */
1130
        if (offset & (s->cluster_size - 1)) {
1131
            fprintf(stderr, "ERROR refcount block %d is not "
1132
                "cluster aligned; refcount table entry corrupted\n", i);
1133
            errors++;
1134
        }
1135

    
1136
        if (offset != 0) {
1137
            errors += inc_refcounts(bs, refcount_table, nb_clusters,
1138
                          offset, s->cluster_size);
1139
            if (refcount_table[offset / s->cluster_size] != 1) {
1140
                fprintf(stderr, "ERROR refcount block %d refcount=%d\n",
1141
                    i, refcount_table[offset / s->cluster_size]);
1142
            }
1143
        }
1144
    }
1145

    
1146
    /* compare ref counts */
1147
    for(i = 0; i < nb_clusters; i++) {
1148
        refcount1 = get_refcount(bs, i);
1149
        if (refcount1 < 0) {
1150
            fprintf(stderr, "Can't get refcount for cluster %d: %s\n",
1151
                i, strerror(-refcount1));
1152
        }
1153

    
1154
        refcount2 = refcount_table[i];
1155
        if (refcount1 != refcount2) {
1156
            fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
1157
                   i, refcount1, refcount2);
1158
            errors++;
1159
        }
1160
    }
1161

    
1162
    qemu_free(refcount_table);
1163

    
1164
    return errors;
1165
}
1166