145aba42fSKevin Wolf /* 245aba42fSKevin Wolf * Block driver for the QCOW version 2 format 345aba42fSKevin Wolf * 445aba42fSKevin Wolf * Copyright (c) 2004-2006 Fabrice Bellard 545aba42fSKevin Wolf * 645aba42fSKevin Wolf * Permission is hereby granted, free of charge, to any person obtaining a copy 745aba42fSKevin Wolf * of this software and associated documentation files (the "Software"), to deal 845aba42fSKevin Wolf * in the Software without restriction, including without limitation the rights 945aba42fSKevin Wolf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1045aba42fSKevin Wolf * copies of the Software, and to permit persons to whom the Software is 1145aba42fSKevin Wolf * furnished to do so, subject to the following conditions: 1245aba42fSKevin Wolf * 1345aba42fSKevin Wolf * The above copyright notice and this permission notice shall be included in 1445aba42fSKevin Wolf * all copies or substantial portions of the Software. 1545aba42fSKevin Wolf * 1645aba42fSKevin Wolf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1745aba42fSKevin Wolf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1845aba42fSKevin Wolf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1945aba42fSKevin Wolf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2045aba42fSKevin Wolf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2145aba42fSKevin Wolf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2245aba42fSKevin Wolf * THE SOFTWARE. 2345aba42fSKevin Wolf */ 2445aba42fSKevin Wolf 2545aba42fSKevin Wolf #include <zlib.h> 2645aba42fSKevin Wolf 2745aba42fSKevin Wolf #include "qemu-common.h" 28737e150eSPaolo Bonzini #include "block/block_int.h" 2945aba42fSKevin Wolf #include "block/qcow2.h" 303cce16f4SKevin Wolf #include "trace.h" 3145aba42fSKevin Wolf 3272893756SStefan Hajnoczi int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size) 3345aba42fSKevin Wolf { 3445aba42fSKevin Wolf BDRVQcowState *s = bs->opaque; 3545aba42fSKevin Wolf int new_l1_size, new_l1_size2, ret, i; 3645aba42fSKevin Wolf uint64_t *new_l1_table; 375d757b56SKevin Wolf int64_t new_l1_table_offset; 3845aba42fSKevin Wolf uint8_t data[12]; 3945aba42fSKevin Wolf 4072893756SStefan Hajnoczi if (min_size <= s->l1_size) 4145aba42fSKevin Wolf return 0; 4272893756SStefan Hajnoczi 4372893756SStefan Hajnoczi if (exact_size) { 4472893756SStefan Hajnoczi new_l1_size = min_size; 4572893756SStefan Hajnoczi } else { 4672893756SStefan Hajnoczi /* Bump size up to reduce the number of times we have to grow */ 4772893756SStefan Hajnoczi new_l1_size = s->l1_size; 48d191d12dSStefan Weil if (new_l1_size == 0) { 49d191d12dSStefan Weil new_l1_size = 1; 50d191d12dSStefan Weil } 5145aba42fSKevin Wolf while (min_size > new_l1_size) { 5245aba42fSKevin Wolf new_l1_size = (new_l1_size * 3 + 1) / 2; 5345aba42fSKevin Wolf } 5472893756SStefan Hajnoczi } 5572893756SStefan Hajnoczi 5645aba42fSKevin Wolf #ifdef DEBUG_ALLOC2 5735ee5e39SFrediano Ziglio fprintf(stderr, "grow l1_table from %d to %d\n", s->l1_size, new_l1_size); 5845aba42fSKevin Wolf #endif 5945aba42fSKevin Wolf 6045aba42fSKevin Wolf new_l1_size2 = sizeof(uint64_t) * new_l1_size; 617267c094SAnthony Liguori new_l1_table = g_malloc0(align_offset(new_l1_size2, 512)); 6245aba42fSKevin Wolf memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t)); 6345aba42fSKevin Wolf 6445aba42fSKevin Wolf /* write new table (align to cluster) */ 6566f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE); 66ed6ccf0fSKevin Wolf new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2); 675d757b56SKevin Wolf if (new_l1_table_offset < 0) { 687267c094SAnthony Liguori g_free(new_l1_table); 695d757b56SKevin Wolf return new_l1_table_offset; 705d757b56SKevin Wolf } 7129c1a730SKevin Wolf 7229c1a730SKevin Wolf ret = qcow2_cache_flush(bs, s->refcount_block_cache); 7329c1a730SKevin Wolf if (ret < 0) { 7480fa3341SKevin Wolf goto fail; 7529c1a730SKevin Wolf } 7645aba42fSKevin Wolf 7766f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE); 7845aba42fSKevin Wolf for(i = 0; i < s->l1_size; i++) 7945aba42fSKevin Wolf new_l1_table[i] = cpu_to_be64(new_l1_table[i]); 808b3b7206SKevin Wolf ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, new_l1_table, new_l1_size2); 818b3b7206SKevin Wolf if (ret < 0) 8245aba42fSKevin Wolf goto fail; 8345aba42fSKevin Wolf for(i = 0; i < s->l1_size; i++) 8445aba42fSKevin Wolf new_l1_table[i] = be64_to_cpu(new_l1_table[i]); 8545aba42fSKevin Wolf 8645aba42fSKevin Wolf /* set new table */ 8766f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE); 8845aba42fSKevin Wolf cpu_to_be32w((uint32_t*)data, new_l1_size); 89653df36bSAurelien Jarno cpu_to_be64wu((uint64_t*)(data + 4), new_l1_table_offset); 908b3b7206SKevin Wolf ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size), data,sizeof(data)); 918b3b7206SKevin Wolf if (ret < 0) { 9245aba42fSKevin Wolf goto fail; 93fb8fa77cSKevin Wolf } 947267c094SAnthony Liguori g_free(s->l1_table); 95ed6ccf0fSKevin Wolf qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t)); 9645aba42fSKevin Wolf s->l1_table_offset = new_l1_table_offset; 9745aba42fSKevin Wolf s->l1_table = new_l1_table; 9845aba42fSKevin Wolf s->l1_size = new_l1_size; 9945aba42fSKevin Wolf return 0; 10045aba42fSKevin Wolf fail: 1017267c094SAnthony Liguori g_free(new_l1_table); 102fb8fa77cSKevin Wolf qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2); 1038b3b7206SKevin Wolf return ret; 10445aba42fSKevin Wolf } 10545aba42fSKevin Wolf 10645aba42fSKevin Wolf /* 10745aba42fSKevin Wolf * l2_load 10845aba42fSKevin Wolf * 10945aba42fSKevin Wolf * Loads a L2 table into memory. If the table is in the cache, the cache 11045aba42fSKevin Wolf * is used; otherwise the L2 table is loaded from the image file. 11145aba42fSKevin Wolf * 11245aba42fSKevin Wolf * Returns a pointer to the L2 table on success, or NULL if the read from 11345aba42fSKevin Wolf * the image file failed. 11445aba42fSKevin Wolf */ 11545aba42fSKevin Wolf 11655c17e98SKevin Wolf static int l2_load(BlockDriverState *bs, uint64_t l2_offset, 11755c17e98SKevin Wolf uint64_t **l2_table) 11845aba42fSKevin Wolf { 11945aba42fSKevin Wolf BDRVQcowState *s = bs->opaque; 12055c17e98SKevin Wolf int ret; 12145aba42fSKevin Wolf 12229c1a730SKevin Wolf ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset, (void**) l2_table); 12345aba42fSKevin Wolf 12455c17e98SKevin Wolf return ret; 12555c17e98SKevin Wolf } 12655c17e98SKevin Wolf 12745aba42fSKevin Wolf /* 1286583e3c7SKevin Wolf * Writes one sector of the L1 table to the disk (can't update single entries 1296583e3c7SKevin Wolf * and we really don't want bdrv_pread to perform a read-modify-write) 1306583e3c7SKevin Wolf */ 1316583e3c7SKevin Wolf #define L1_ENTRIES_PER_SECTOR (512 / 8) 13266f82ceeSKevin Wolf static int write_l1_entry(BlockDriverState *bs, int l1_index) 1336583e3c7SKevin Wolf { 13466f82ceeSKevin Wolf BDRVQcowState *s = bs->opaque; 1356583e3c7SKevin Wolf uint64_t buf[L1_ENTRIES_PER_SECTOR]; 1366583e3c7SKevin Wolf int l1_start_index; 137f7defcb6SKevin Wolf int i, ret; 1386583e3c7SKevin Wolf 1396583e3c7SKevin Wolf l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1); 1406583e3c7SKevin Wolf for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) { 1416583e3c7SKevin Wolf buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]); 1426583e3c7SKevin Wolf } 1436583e3c7SKevin Wolf 14466f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE); 1458b3b7206SKevin Wolf ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset + 8 * l1_start_index, 146f7defcb6SKevin Wolf buf, sizeof(buf)); 147f7defcb6SKevin Wolf if (ret < 0) { 148f7defcb6SKevin Wolf return ret; 1496583e3c7SKevin Wolf } 1506583e3c7SKevin Wolf 1516583e3c7SKevin Wolf return 0; 1526583e3c7SKevin Wolf } 1536583e3c7SKevin Wolf 1546583e3c7SKevin Wolf /* 15545aba42fSKevin Wolf * l2_allocate 15645aba42fSKevin Wolf * 15745aba42fSKevin Wolf * Allocate a new l2 entry in the file. If l1_index points to an already 15845aba42fSKevin Wolf * used entry in the L2 table (i.e. we are doing a copy on write for the L2 15945aba42fSKevin Wolf * table) copy the contents of the old L2 table into the newly allocated one. 16045aba42fSKevin Wolf * Otherwise the new table is initialized with zeros. 16145aba42fSKevin Wolf * 16245aba42fSKevin Wolf */ 16345aba42fSKevin Wolf 164c46e1167SKevin Wolf static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table) 16545aba42fSKevin Wolf { 16645aba42fSKevin Wolf BDRVQcowState *s = bs->opaque; 1676583e3c7SKevin Wolf uint64_t old_l2_offset; 168f4f0d391SKevin Wolf uint64_t *l2_table; 169f4f0d391SKevin Wolf int64_t l2_offset; 170c46e1167SKevin Wolf int ret; 17145aba42fSKevin Wolf 17245aba42fSKevin Wolf old_l2_offset = s->l1_table[l1_index]; 17345aba42fSKevin Wolf 1743cce16f4SKevin Wolf trace_qcow2_l2_allocate(bs, l1_index); 1753cce16f4SKevin Wolf 17645aba42fSKevin Wolf /* allocate a new l2 entry */ 17745aba42fSKevin Wolf 178ed6ccf0fSKevin Wolf l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t)); 1795d757b56SKevin Wolf if (l2_offset < 0) { 180c46e1167SKevin Wolf return l2_offset; 1815d757b56SKevin Wolf } 18229c1a730SKevin Wolf 18329c1a730SKevin Wolf ret = qcow2_cache_flush(bs, s->refcount_block_cache); 18429c1a730SKevin Wolf if (ret < 0) { 18529c1a730SKevin Wolf goto fail; 18629c1a730SKevin Wolf } 18745aba42fSKevin Wolf 18845aba42fSKevin Wolf /* allocate a new entry in the l2 cache */ 18945aba42fSKevin Wolf 1903cce16f4SKevin Wolf trace_qcow2_l2_allocate_get_empty(bs, l1_index); 19129c1a730SKevin Wolf ret = qcow2_cache_get_empty(bs, s->l2_table_cache, l2_offset, (void**) table); 19229c1a730SKevin Wolf if (ret < 0) { 19329c1a730SKevin Wolf return ret; 19429c1a730SKevin Wolf } 19529c1a730SKevin Wolf 19629c1a730SKevin Wolf l2_table = *table; 19745aba42fSKevin Wolf 1988e37f681SKevin Wolf if ((old_l2_offset & L1E_OFFSET_MASK) == 0) { 19945aba42fSKevin Wolf /* if there was no old l2 table, clear the new table */ 20045aba42fSKevin Wolf memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); 20145aba42fSKevin Wolf } else { 20229c1a730SKevin Wolf uint64_t* old_table; 20329c1a730SKevin Wolf 20445aba42fSKevin Wolf /* if there was an old l2 table, read it from the disk */ 20566f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ); 2068e37f681SKevin Wolf ret = qcow2_cache_get(bs, s->l2_table_cache, 2078e37f681SKevin Wolf old_l2_offset & L1E_OFFSET_MASK, 20829c1a730SKevin Wolf (void**) &old_table); 20929c1a730SKevin Wolf if (ret < 0) { 21029c1a730SKevin Wolf goto fail; 21129c1a730SKevin Wolf } 21229c1a730SKevin Wolf 21329c1a730SKevin Wolf memcpy(l2_table, old_table, s->cluster_size); 21429c1a730SKevin Wolf 21529c1a730SKevin Wolf ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &old_table); 216c46e1167SKevin Wolf if (ret < 0) { 217175e1152SKevin Wolf goto fail; 218c46e1167SKevin Wolf } 21945aba42fSKevin Wolf } 22029c1a730SKevin Wolf 22145aba42fSKevin Wolf /* write the l2 table to the file */ 22266f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE); 22329c1a730SKevin Wolf 2243cce16f4SKevin Wolf trace_qcow2_l2_allocate_write_l2(bs, l1_index); 22529c1a730SKevin Wolf qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); 22629c1a730SKevin Wolf ret = qcow2_cache_flush(bs, s->l2_table_cache); 227c46e1167SKevin Wolf if (ret < 0) { 228175e1152SKevin Wolf goto fail; 229175e1152SKevin Wolf } 230175e1152SKevin Wolf 231175e1152SKevin Wolf /* update the L1 entry */ 2323cce16f4SKevin Wolf trace_qcow2_l2_allocate_write_l1(bs, l1_index); 233175e1152SKevin Wolf s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED; 234175e1152SKevin Wolf ret = write_l1_entry(bs, l1_index); 235175e1152SKevin Wolf if (ret < 0) { 236175e1152SKevin Wolf goto fail; 237c46e1167SKevin Wolf } 23845aba42fSKevin Wolf 239c46e1167SKevin Wolf *table = l2_table; 2403cce16f4SKevin Wolf trace_qcow2_l2_allocate_done(bs, l1_index, 0); 241c46e1167SKevin Wolf return 0; 242175e1152SKevin Wolf 243175e1152SKevin Wolf fail: 2443cce16f4SKevin Wolf trace_qcow2_l2_allocate_done(bs, l1_index, ret); 24529c1a730SKevin Wolf qcow2_cache_put(bs, s->l2_table_cache, (void**) table); 24668dba0bfSKevin Wolf s->l1_table[l1_index] = old_l2_offset; 247175e1152SKevin Wolf return ret; 24845aba42fSKevin Wolf } 24945aba42fSKevin Wolf 2502bfcc4a0SKevin Wolf /* 2512bfcc4a0SKevin Wolf * Checks how many clusters in a given L2 table are contiguous in the image 2522bfcc4a0SKevin Wolf * file. As soon as one of the flags in the bitmask stop_flags changes compared 2532bfcc4a0SKevin Wolf * to the first cluster, the search is stopped and the cluster is not counted 2542bfcc4a0SKevin Wolf * as contiguous. (This allows it, for example, to stop at the first compressed 2552bfcc4a0SKevin Wolf * cluster which may require a different handling) 2562bfcc4a0SKevin Wolf */ 25745aba42fSKevin Wolf static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size, 2582bfcc4a0SKevin Wolf uint64_t *l2_table, uint64_t start, uint64_t stop_flags) 25945aba42fSKevin Wolf { 26045aba42fSKevin Wolf int i; 2612bfcc4a0SKevin Wolf uint64_t mask = stop_flags | L2E_OFFSET_MASK; 2622bfcc4a0SKevin Wolf uint64_t offset = be64_to_cpu(l2_table[0]) & mask; 26345aba42fSKevin Wolf 26445aba42fSKevin Wolf if (!offset) 26545aba42fSKevin Wolf return 0; 26645aba42fSKevin Wolf 2672bfcc4a0SKevin Wolf for (i = start; i < start + nb_clusters; i++) { 2682bfcc4a0SKevin Wolf uint64_t l2_entry = be64_to_cpu(l2_table[i]) & mask; 2692bfcc4a0SKevin Wolf if (offset + (uint64_t) i * cluster_size != l2_entry) { 27045aba42fSKevin Wolf break; 2712bfcc4a0SKevin Wolf } 2722bfcc4a0SKevin Wolf } 27345aba42fSKevin Wolf 27445aba42fSKevin Wolf return (i - start); 27545aba42fSKevin Wolf } 27645aba42fSKevin Wolf 27745aba42fSKevin Wolf static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table) 27845aba42fSKevin Wolf { 2792bfcc4a0SKevin Wolf int i; 28045aba42fSKevin Wolf 2812bfcc4a0SKevin Wolf for (i = 0; i < nb_clusters; i++) { 2822bfcc4a0SKevin Wolf int type = qcow2_get_cluster_type(be64_to_cpu(l2_table[i])); 2832bfcc4a0SKevin Wolf 2842bfcc4a0SKevin Wolf if (type != QCOW2_CLUSTER_UNALLOCATED) { 2852bfcc4a0SKevin Wolf break; 2862bfcc4a0SKevin Wolf } 2872bfcc4a0SKevin Wolf } 28845aba42fSKevin Wolf 28945aba42fSKevin Wolf return i; 29045aba42fSKevin Wolf } 29145aba42fSKevin Wolf 29245aba42fSKevin Wolf /* The crypt function is compatible with the linux cryptoloop 29345aba42fSKevin Wolf algorithm for < 4 GB images. NOTE: out_buf == in_buf is 29445aba42fSKevin Wolf supported */ 295ed6ccf0fSKevin Wolf void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num, 29645aba42fSKevin Wolf uint8_t *out_buf, const uint8_t *in_buf, 29745aba42fSKevin Wolf int nb_sectors, int enc, 29845aba42fSKevin Wolf const AES_KEY *key) 29945aba42fSKevin Wolf { 30045aba42fSKevin Wolf union { 30145aba42fSKevin Wolf uint64_t ll[2]; 30245aba42fSKevin Wolf uint8_t b[16]; 30345aba42fSKevin Wolf } ivec; 30445aba42fSKevin Wolf int i; 30545aba42fSKevin Wolf 30645aba42fSKevin Wolf for(i = 0; i < nb_sectors; i++) { 30745aba42fSKevin Wolf ivec.ll[0] = cpu_to_le64(sector_num); 30845aba42fSKevin Wolf ivec.ll[1] = 0; 30945aba42fSKevin Wolf AES_cbc_encrypt(in_buf, out_buf, 512, key, 31045aba42fSKevin Wolf ivec.b, enc); 31145aba42fSKevin Wolf sector_num++; 31245aba42fSKevin Wolf in_buf += 512; 31345aba42fSKevin Wolf out_buf += 512; 31445aba42fSKevin Wolf } 31545aba42fSKevin Wolf } 31645aba42fSKevin Wolf 317aef4acb6SStefan Hajnoczi static int coroutine_fn copy_sectors(BlockDriverState *bs, 318aef4acb6SStefan Hajnoczi uint64_t start_sect, 319aef4acb6SStefan Hajnoczi uint64_t cluster_offset, 320aef4acb6SStefan Hajnoczi int n_start, int n_end) 32145aba42fSKevin Wolf { 32245aba42fSKevin Wolf BDRVQcowState *s = bs->opaque; 323aef4acb6SStefan Hajnoczi QEMUIOVector qiov; 324aef4acb6SStefan Hajnoczi struct iovec iov; 32545aba42fSKevin Wolf int n, ret; 3261b9f1491SKevin Wolf 3271b9f1491SKevin Wolf /* 3281b9f1491SKevin Wolf * If this is the last cluster and it is only partially used, we must only 3291b9f1491SKevin Wolf * copy until the end of the image, or bdrv_check_request will fail for the 3301b9f1491SKevin Wolf * bdrv_read/write calls below. 3311b9f1491SKevin Wolf */ 3321b9f1491SKevin Wolf if (start_sect + n_end > bs->total_sectors) { 3331b9f1491SKevin Wolf n_end = bs->total_sectors - start_sect; 3341b9f1491SKevin Wolf } 33545aba42fSKevin Wolf 33645aba42fSKevin Wolf n = n_end - n_start; 3371b9f1491SKevin Wolf if (n <= 0) { 33845aba42fSKevin Wolf return 0; 3391b9f1491SKevin Wolf } 3401b9f1491SKevin Wolf 341aef4acb6SStefan Hajnoczi iov.iov_len = n * BDRV_SECTOR_SIZE; 342aef4acb6SStefan Hajnoczi iov.iov_base = qemu_blockalign(bs, iov.iov_len); 343aef4acb6SStefan Hajnoczi 344aef4acb6SStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 3451b9f1491SKevin Wolf 34666f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_COW_READ); 347aef4acb6SStefan Hajnoczi 348aef4acb6SStefan Hajnoczi /* Call .bdrv_co_readv() directly instead of using the public block-layer 349aef4acb6SStefan Hajnoczi * interface. This avoids double I/O throttling and request tracking, 350aef4acb6SStefan Hajnoczi * which can lead to deadlock when block layer copy-on-read is enabled. 351aef4acb6SStefan Hajnoczi */ 352aef4acb6SStefan Hajnoczi ret = bs->drv->bdrv_co_readv(bs, start_sect + n_start, n, &qiov); 3531b9f1491SKevin Wolf if (ret < 0) { 3541b9f1491SKevin Wolf goto out; 3551b9f1491SKevin Wolf } 3561b9f1491SKevin Wolf 35745aba42fSKevin Wolf if (s->crypt_method) { 358ed6ccf0fSKevin Wolf qcow2_encrypt_sectors(s, start_sect + n_start, 359aef4acb6SStefan Hajnoczi iov.iov_base, iov.iov_base, n, 1, 36045aba42fSKevin Wolf &s->aes_encrypt_key); 36145aba42fSKevin Wolf } 3621b9f1491SKevin Wolf 36366f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE); 364aef4acb6SStefan Hajnoczi ret = bdrv_co_writev(bs->file, (cluster_offset >> 9) + n_start, n, &qiov); 3651b9f1491SKevin Wolf if (ret < 0) { 3661b9f1491SKevin Wolf goto out; 3671b9f1491SKevin Wolf } 3681b9f1491SKevin Wolf 3691b9f1491SKevin Wolf ret = 0; 3701b9f1491SKevin Wolf out: 371aef4acb6SStefan Hajnoczi qemu_vfree(iov.iov_base); 37245aba42fSKevin Wolf return ret; 37345aba42fSKevin Wolf } 37445aba42fSKevin Wolf 37545aba42fSKevin Wolf 37645aba42fSKevin Wolf /* 37745aba42fSKevin Wolf * get_cluster_offset 37845aba42fSKevin Wolf * 3791c46efaaSKevin Wolf * For a given offset of the disk image, find the cluster offset in 3801c46efaaSKevin Wolf * qcow2 file. The offset is stored in *cluster_offset. 38145aba42fSKevin Wolf * 382d57237f2SDevin Nakamura * on entry, *num is the number of contiguous sectors we'd like to 38345aba42fSKevin Wolf * access following offset. 38445aba42fSKevin Wolf * 385d57237f2SDevin Nakamura * on exit, *num is the number of contiguous sectors we can read. 38645aba42fSKevin Wolf * 38768d000a3SKevin Wolf * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error 38868d000a3SKevin Wolf * cases. 38945aba42fSKevin Wolf */ 3901c46efaaSKevin Wolf int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, 3911c46efaaSKevin Wolf int *num, uint64_t *cluster_offset) 39245aba42fSKevin Wolf { 39345aba42fSKevin Wolf BDRVQcowState *s = bs->opaque; 39480ee15a6SKevin Wolf unsigned int l1_index, l2_index; 3951c46efaaSKevin Wolf uint64_t l2_offset, *l2_table; 39645aba42fSKevin Wolf int l1_bits, c; 39780ee15a6SKevin Wolf unsigned int index_in_cluster, nb_clusters; 39880ee15a6SKevin Wolf uint64_t nb_available, nb_needed; 39955c17e98SKevin Wolf int ret; 40045aba42fSKevin Wolf 40145aba42fSKevin Wolf index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1); 40245aba42fSKevin Wolf nb_needed = *num + index_in_cluster; 40345aba42fSKevin Wolf 40445aba42fSKevin Wolf l1_bits = s->l2_bits + s->cluster_bits; 40545aba42fSKevin Wolf 40645aba42fSKevin Wolf /* compute how many bytes there are between the offset and 40745aba42fSKevin Wolf * the end of the l1 entry 40845aba42fSKevin Wolf */ 40945aba42fSKevin Wolf 41080ee15a6SKevin Wolf nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1)); 41145aba42fSKevin Wolf 41245aba42fSKevin Wolf /* compute the number of available sectors */ 41345aba42fSKevin Wolf 41445aba42fSKevin Wolf nb_available = (nb_available >> 9) + index_in_cluster; 41545aba42fSKevin Wolf 41645aba42fSKevin Wolf if (nb_needed > nb_available) { 41745aba42fSKevin Wolf nb_needed = nb_available; 41845aba42fSKevin Wolf } 41945aba42fSKevin Wolf 4201c46efaaSKevin Wolf *cluster_offset = 0; 42145aba42fSKevin Wolf 42245aba42fSKevin Wolf /* seek the the l2 offset in the l1 table */ 42345aba42fSKevin Wolf 42445aba42fSKevin Wolf l1_index = offset >> l1_bits; 42568d000a3SKevin Wolf if (l1_index >= s->l1_size) { 42668d000a3SKevin Wolf ret = QCOW2_CLUSTER_UNALLOCATED; 42745aba42fSKevin Wolf goto out; 42868d000a3SKevin Wolf } 42945aba42fSKevin Wolf 43068d000a3SKevin Wolf l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK; 43168d000a3SKevin Wolf if (!l2_offset) { 43268d000a3SKevin Wolf ret = QCOW2_CLUSTER_UNALLOCATED; 43345aba42fSKevin Wolf goto out; 43468d000a3SKevin Wolf } 43545aba42fSKevin Wolf 43645aba42fSKevin Wolf /* load the l2 table in memory */ 43745aba42fSKevin Wolf 43855c17e98SKevin Wolf ret = l2_load(bs, l2_offset, &l2_table); 43955c17e98SKevin Wolf if (ret < 0) { 44055c17e98SKevin Wolf return ret; 4411c46efaaSKevin Wolf } 44245aba42fSKevin Wolf 44345aba42fSKevin Wolf /* find the cluster offset for the given disk offset */ 44445aba42fSKevin Wolf 44545aba42fSKevin Wolf l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); 4461c46efaaSKevin Wolf *cluster_offset = be64_to_cpu(l2_table[l2_index]); 44745aba42fSKevin Wolf nb_clusters = size_to_clusters(s, nb_needed << 9); 44845aba42fSKevin Wolf 44968d000a3SKevin Wolf ret = qcow2_get_cluster_type(*cluster_offset); 45068d000a3SKevin Wolf switch (ret) { 45168d000a3SKevin Wolf case QCOW2_CLUSTER_COMPRESSED: 45268d000a3SKevin Wolf /* Compressed clusters can only be processed one by one */ 45368d000a3SKevin Wolf c = 1; 45468d000a3SKevin Wolf *cluster_offset &= L2E_COMPRESSED_OFFSET_SIZE_MASK; 45568d000a3SKevin Wolf break; 4566377af48SKevin Wolf case QCOW2_CLUSTER_ZERO: 457381b487dSPaolo Bonzini if (s->qcow_version < 3) { 458381b487dSPaolo Bonzini return -EIO; 459381b487dSPaolo Bonzini } 4606377af48SKevin Wolf c = count_contiguous_clusters(nb_clusters, s->cluster_size, 4616377af48SKevin Wolf &l2_table[l2_index], 0, 4626377af48SKevin Wolf QCOW_OFLAG_COMPRESSED | QCOW_OFLAG_ZERO); 4636377af48SKevin Wolf *cluster_offset = 0; 4646377af48SKevin Wolf break; 46568d000a3SKevin Wolf case QCOW2_CLUSTER_UNALLOCATED: 46645aba42fSKevin Wolf /* how many empty clusters ? */ 46745aba42fSKevin Wolf c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]); 46868d000a3SKevin Wolf *cluster_offset = 0; 46968d000a3SKevin Wolf break; 47068d000a3SKevin Wolf case QCOW2_CLUSTER_NORMAL: 47145aba42fSKevin Wolf /* how many allocated clusters ? */ 47245aba42fSKevin Wolf c = count_contiguous_clusters(nb_clusters, s->cluster_size, 4736377af48SKevin Wolf &l2_table[l2_index], 0, 4746377af48SKevin Wolf QCOW_OFLAG_COMPRESSED | QCOW_OFLAG_ZERO); 47568d000a3SKevin Wolf *cluster_offset &= L2E_OFFSET_MASK; 47668d000a3SKevin Wolf break; 4771417d7e4SKevin Wolf default: 4781417d7e4SKevin Wolf abort(); 47945aba42fSKevin Wolf } 48045aba42fSKevin Wolf 48129c1a730SKevin Wolf qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); 48229c1a730SKevin Wolf 48345aba42fSKevin Wolf nb_available = (c * s->cluster_sectors); 48468d000a3SKevin Wolf 48545aba42fSKevin Wolf out: 48645aba42fSKevin Wolf if (nb_available > nb_needed) 48745aba42fSKevin Wolf nb_available = nb_needed; 48845aba42fSKevin Wolf 48945aba42fSKevin Wolf *num = nb_available - index_in_cluster; 49045aba42fSKevin Wolf 49168d000a3SKevin Wolf return ret; 49245aba42fSKevin Wolf } 49345aba42fSKevin Wolf 49445aba42fSKevin Wolf /* 49545aba42fSKevin Wolf * get_cluster_table 49645aba42fSKevin Wolf * 49745aba42fSKevin Wolf * for a given disk offset, load (and allocate if needed) 49845aba42fSKevin Wolf * the l2 table. 49945aba42fSKevin Wolf * 50045aba42fSKevin Wolf * the l2 table offset in the qcow2 file and the cluster index 50145aba42fSKevin Wolf * in the l2 table are given to the caller. 50245aba42fSKevin Wolf * 5031e3e8f1aSKevin Wolf * Returns 0 on success, -errno in failure case 50445aba42fSKevin Wolf */ 50545aba42fSKevin Wolf static int get_cluster_table(BlockDriverState *bs, uint64_t offset, 50645aba42fSKevin Wolf uint64_t **new_l2_table, 50745aba42fSKevin Wolf int *new_l2_index) 50845aba42fSKevin Wolf { 50945aba42fSKevin Wolf BDRVQcowState *s = bs->opaque; 51080ee15a6SKevin Wolf unsigned int l1_index, l2_index; 511c46e1167SKevin Wolf uint64_t l2_offset; 512c46e1167SKevin Wolf uint64_t *l2_table = NULL; 51380ee15a6SKevin Wolf int ret; 51445aba42fSKevin Wolf 51545aba42fSKevin Wolf /* seek the the l2 offset in the l1 table */ 51645aba42fSKevin Wolf 51745aba42fSKevin Wolf l1_index = offset >> (s->l2_bits + s->cluster_bits); 51845aba42fSKevin Wolf if (l1_index >= s->l1_size) { 51972893756SStefan Hajnoczi ret = qcow2_grow_l1_table(bs, l1_index + 1, false); 5201e3e8f1aSKevin Wolf if (ret < 0) { 5211e3e8f1aSKevin Wolf return ret; 5221e3e8f1aSKevin Wolf } 52345aba42fSKevin Wolf } 5248e37f681SKevin Wolf 5258e37f681SKevin Wolf l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK; 52645aba42fSKevin Wolf 52745aba42fSKevin Wolf /* seek the l2 table of the given l2 offset */ 52845aba42fSKevin Wolf 5298e37f681SKevin Wolf if (s->l1_table[l1_index] & QCOW_OFLAG_COPIED) { 53045aba42fSKevin Wolf /* load the l2 table in memory */ 53155c17e98SKevin Wolf ret = l2_load(bs, l2_offset, &l2_table); 53255c17e98SKevin Wolf if (ret < 0) { 53355c17e98SKevin Wolf return ret; 5341e3e8f1aSKevin Wolf } 53545aba42fSKevin Wolf } else { 53616fde5f2SKevin Wolf /* First allocate a new L2 table (and do COW if needed) */ 537c46e1167SKevin Wolf ret = l2_allocate(bs, l1_index, &l2_table); 538c46e1167SKevin Wolf if (ret < 0) { 539c46e1167SKevin Wolf return ret; 5401e3e8f1aSKevin Wolf } 54116fde5f2SKevin Wolf 54216fde5f2SKevin Wolf /* Then decrease the refcount of the old table */ 54316fde5f2SKevin Wolf if (l2_offset) { 54416fde5f2SKevin Wolf qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t)); 54516fde5f2SKevin Wolf } 54645aba42fSKevin Wolf } 54745aba42fSKevin Wolf 54845aba42fSKevin Wolf /* find the cluster offset for the given disk offset */ 54945aba42fSKevin Wolf 55045aba42fSKevin Wolf l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); 55145aba42fSKevin Wolf 55245aba42fSKevin Wolf *new_l2_table = l2_table; 55345aba42fSKevin Wolf *new_l2_index = l2_index; 55445aba42fSKevin Wolf 5551e3e8f1aSKevin Wolf return 0; 55645aba42fSKevin Wolf } 55745aba42fSKevin Wolf 55845aba42fSKevin Wolf /* 55945aba42fSKevin Wolf * alloc_compressed_cluster_offset 56045aba42fSKevin Wolf * 56145aba42fSKevin Wolf * For a given offset of the disk image, return cluster offset in 56245aba42fSKevin Wolf * qcow2 file. 56345aba42fSKevin Wolf * 56445aba42fSKevin Wolf * If the offset is not found, allocate a new compressed cluster. 56545aba42fSKevin Wolf * 56645aba42fSKevin Wolf * Return the cluster offset if successful, 56745aba42fSKevin Wolf * Return 0, otherwise. 56845aba42fSKevin Wolf * 56945aba42fSKevin Wolf */ 57045aba42fSKevin Wolf 571ed6ccf0fSKevin Wolf uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, 57245aba42fSKevin Wolf uint64_t offset, 57345aba42fSKevin Wolf int compressed_size) 57445aba42fSKevin Wolf { 57545aba42fSKevin Wolf BDRVQcowState *s = bs->opaque; 57645aba42fSKevin Wolf int l2_index, ret; 5773948d1d4SKevin Wolf uint64_t *l2_table; 578f4f0d391SKevin Wolf int64_t cluster_offset; 57945aba42fSKevin Wolf int nb_csectors; 58045aba42fSKevin Wolf 5813948d1d4SKevin Wolf ret = get_cluster_table(bs, offset, &l2_table, &l2_index); 5821e3e8f1aSKevin Wolf if (ret < 0) { 58345aba42fSKevin Wolf return 0; 5841e3e8f1aSKevin Wolf } 58545aba42fSKevin Wolf 586b0b6862eSKevin Wolf /* Compression can't overwrite anything. Fail if the cluster was already 587b0b6862eSKevin Wolf * allocated. */ 58845aba42fSKevin Wolf cluster_offset = be64_to_cpu(l2_table[l2_index]); 589b0b6862eSKevin Wolf if (cluster_offset & L2E_OFFSET_MASK) { 5908f1efd00SKevin Wolf qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); 5918f1efd00SKevin Wolf return 0; 5928f1efd00SKevin Wolf } 59345aba42fSKevin Wolf 594ed6ccf0fSKevin Wolf cluster_offset = qcow2_alloc_bytes(bs, compressed_size); 5955d757b56SKevin Wolf if (cluster_offset < 0) { 59629c1a730SKevin Wolf qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); 5975d757b56SKevin Wolf return 0; 5985d757b56SKevin Wolf } 5995d757b56SKevin Wolf 60045aba42fSKevin Wolf nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) - 60145aba42fSKevin Wolf (cluster_offset >> 9); 60245aba42fSKevin Wolf 60345aba42fSKevin Wolf cluster_offset |= QCOW_OFLAG_COMPRESSED | 60445aba42fSKevin Wolf ((uint64_t)nb_csectors << s->csize_shift); 60545aba42fSKevin Wolf 60645aba42fSKevin Wolf /* update L2 table */ 60745aba42fSKevin Wolf 60845aba42fSKevin Wolf /* compressed clusters never have the copied flag */ 60945aba42fSKevin Wolf 61066f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED); 61129c1a730SKevin Wolf qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); 61245aba42fSKevin Wolf l2_table[l2_index] = cpu_to_be64(cluster_offset); 61329c1a730SKevin Wolf ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); 61429c1a730SKevin Wolf if (ret < 0) { 61545aba42fSKevin Wolf return 0; 61629c1a730SKevin Wolf } 61745aba42fSKevin Wolf 61845aba42fSKevin Wolf return cluster_offset; 61945aba42fSKevin Wolf } 62045aba42fSKevin Wolf 621593fb83cSKevin Wolf static int perform_cow(BlockDriverState *bs, QCowL2Meta *m, Qcow2COWRegion *r) 622593fb83cSKevin Wolf { 623593fb83cSKevin Wolf BDRVQcowState *s = bs->opaque; 624593fb83cSKevin Wolf int ret; 625593fb83cSKevin Wolf 626593fb83cSKevin Wolf if (r->nb_sectors == 0) { 627593fb83cSKevin Wolf return 0; 628593fb83cSKevin Wolf } 629593fb83cSKevin Wolf 630593fb83cSKevin Wolf qemu_co_mutex_unlock(&s->lock); 631593fb83cSKevin Wolf ret = copy_sectors(bs, m->offset / BDRV_SECTOR_SIZE, m->alloc_offset, 632593fb83cSKevin Wolf r->offset / BDRV_SECTOR_SIZE, 633593fb83cSKevin Wolf r->offset / BDRV_SECTOR_SIZE + r->nb_sectors); 634593fb83cSKevin Wolf qemu_co_mutex_lock(&s->lock); 635593fb83cSKevin Wolf 636593fb83cSKevin Wolf if (ret < 0) { 637593fb83cSKevin Wolf return ret; 638593fb83cSKevin Wolf } 639593fb83cSKevin Wolf 640593fb83cSKevin Wolf /* 641593fb83cSKevin Wolf * Before we update the L2 table to actually point to the new cluster, we 642593fb83cSKevin Wolf * need to be sure that the refcounts have been increased and COW was 643593fb83cSKevin Wolf * handled. 644593fb83cSKevin Wolf */ 645593fb83cSKevin Wolf qcow2_cache_depends_on_flush(s->l2_table_cache); 646593fb83cSKevin Wolf 647593fb83cSKevin Wolf return 0; 648593fb83cSKevin Wolf } 649593fb83cSKevin Wolf 650148da7eaSKevin Wolf int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) 65145aba42fSKevin Wolf { 65245aba42fSKevin Wolf BDRVQcowState *s = bs->opaque; 65345aba42fSKevin Wolf int i, j = 0, l2_index, ret; 654593fb83cSKevin Wolf uint64_t *old_cluster, *l2_table; 655250196f1SKevin Wolf uint64_t cluster_offset = m->alloc_offset; 65645aba42fSKevin Wolf 6573cce16f4SKevin Wolf trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters); 658f50f88b9SKevin Wolf assert(m->nb_clusters > 0); 65945aba42fSKevin Wolf 6607267c094SAnthony Liguori old_cluster = g_malloc(m->nb_clusters * sizeof(uint64_t)); 66145aba42fSKevin Wolf 66245aba42fSKevin Wolf /* copy content of unmodified sectors */ 663593fb83cSKevin Wolf ret = perform_cow(bs, m, &m->cow_start); 664593fb83cSKevin Wolf if (ret < 0) { 66545aba42fSKevin Wolf goto err; 66645aba42fSKevin Wolf } 66745aba42fSKevin Wolf 668593fb83cSKevin Wolf ret = perform_cow(bs, m, &m->cow_end); 669593fb83cSKevin Wolf if (ret < 0) { 67045aba42fSKevin Wolf goto err; 67145aba42fSKevin Wolf } 67245aba42fSKevin Wolf 673593fb83cSKevin Wolf /* Update L2 table. */ 67474c4510aSKevin Wolf if (s->use_lazy_refcounts) { 675280d3735SKevin Wolf qcow2_mark_dirty(bs); 676280d3735SKevin Wolf } 677bfe8043eSStefan Hajnoczi if (qcow2_need_accurate_refcounts(s)) { 678bfe8043eSStefan Hajnoczi qcow2_cache_set_dependency(bs, s->l2_table_cache, 679bfe8043eSStefan Hajnoczi s->refcount_block_cache); 680bfe8043eSStefan Hajnoczi } 681280d3735SKevin Wolf 6823948d1d4SKevin Wolf ret = get_cluster_table(bs, m->offset, &l2_table, &l2_index); 6831e3e8f1aSKevin Wolf if (ret < 0) { 68445aba42fSKevin Wolf goto err; 6851e3e8f1aSKevin Wolf } 68629c1a730SKevin Wolf qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); 68745aba42fSKevin Wolf 68845aba42fSKevin Wolf for (i = 0; i < m->nb_clusters; i++) { 68945aba42fSKevin Wolf /* if two concurrent writes happen to the same unallocated cluster 69045aba42fSKevin Wolf * each write allocates separate cluster and writes data concurrently. 69145aba42fSKevin Wolf * The first one to complete updates l2 table with pointer to its 69245aba42fSKevin Wolf * cluster the second one has to do RMW (which is done above by 69345aba42fSKevin Wolf * copy_sectors()), update l2 table with its cluster pointer and free 69445aba42fSKevin Wolf * old cluster. This is what this loop does */ 69545aba42fSKevin Wolf if(l2_table[l2_index + i] != 0) 69645aba42fSKevin Wolf old_cluster[j++] = l2_table[l2_index + i]; 69745aba42fSKevin Wolf 69845aba42fSKevin Wolf l2_table[l2_index + i] = cpu_to_be64((cluster_offset + 69945aba42fSKevin Wolf (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); 70045aba42fSKevin Wolf } 70145aba42fSKevin Wolf 7029f8e668eSKevin Wolf 70329c1a730SKevin Wolf ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); 704c835d00fSKevin Wolf if (ret < 0) { 70545aba42fSKevin Wolf goto err; 7064c1612d9SKevin Wolf } 70745aba42fSKevin Wolf 7087ec5e6a4SKevin Wolf /* 7097ec5e6a4SKevin Wolf * If this was a COW, we need to decrease the refcount of the old cluster. 7107ec5e6a4SKevin Wolf * Also flush bs->file to get the right order for L2 and refcount update. 7117ec5e6a4SKevin Wolf */ 7127ec5e6a4SKevin Wolf if (j != 0) { 7137ec5e6a4SKevin Wolf for (i = 0; i < j; i++) { 7148e37f681SKevin Wolf qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1); 7157ec5e6a4SKevin Wolf } 7167ec5e6a4SKevin Wolf } 71745aba42fSKevin Wolf 71845aba42fSKevin Wolf ret = 0; 71945aba42fSKevin Wolf err: 7207267c094SAnthony Liguori g_free(old_cluster); 72145aba42fSKevin Wolf return ret; 72245aba42fSKevin Wolf } 72345aba42fSKevin Wolf 72445aba42fSKevin Wolf /* 725bf319eceSKevin Wolf * Returns the number of contiguous clusters that can be used for an allocating 726bf319eceSKevin Wolf * write, but require COW to be performed (this includes yet unallocated space, 727bf319eceSKevin Wolf * which must copy from the backing file) 728bf319eceSKevin Wolf */ 729bf319eceSKevin Wolf static int count_cow_clusters(BDRVQcowState *s, int nb_clusters, 730bf319eceSKevin Wolf uint64_t *l2_table, int l2_index) 731bf319eceSKevin Wolf { 732143550a8SKevin Wolf int i; 733bf319eceSKevin Wolf 734143550a8SKevin Wolf for (i = 0; i < nb_clusters; i++) { 735143550a8SKevin Wolf uint64_t l2_entry = be64_to_cpu(l2_table[l2_index + i]); 736143550a8SKevin Wolf int cluster_type = qcow2_get_cluster_type(l2_entry); 737143550a8SKevin Wolf 738143550a8SKevin Wolf switch(cluster_type) { 739143550a8SKevin Wolf case QCOW2_CLUSTER_NORMAL: 740143550a8SKevin Wolf if (l2_entry & QCOW_OFLAG_COPIED) { 741143550a8SKevin Wolf goto out; 742143550a8SKevin Wolf } 743bf319eceSKevin Wolf break; 744143550a8SKevin Wolf case QCOW2_CLUSTER_UNALLOCATED: 745143550a8SKevin Wolf case QCOW2_CLUSTER_COMPRESSED: 7466377af48SKevin Wolf case QCOW2_CLUSTER_ZERO: 747143550a8SKevin Wolf break; 748143550a8SKevin Wolf default: 749143550a8SKevin Wolf abort(); 750143550a8SKevin Wolf } 751bf319eceSKevin Wolf } 752bf319eceSKevin Wolf 753143550a8SKevin Wolf out: 754bf319eceSKevin Wolf assert(i <= nb_clusters); 755bf319eceSKevin Wolf return i; 756bf319eceSKevin Wolf } 757bf319eceSKevin Wolf 758bf319eceSKevin Wolf /* 759250196f1SKevin Wolf * Check if there already is an AIO write request in flight which allocates 760250196f1SKevin Wolf * the same cluster. In this case we need to wait until the previous 761250196f1SKevin Wolf * request has completed and updated the L2 table accordingly. 76265eb2e35SKevin Wolf * 76365eb2e35SKevin Wolf * Returns: 76465eb2e35SKevin Wolf * 0 if there was no dependency. *cur_bytes indicates the number of 76565eb2e35SKevin Wolf * bytes from guest_offset that can be read before the next 76665eb2e35SKevin Wolf * dependency must be processed (or the request is complete) 76765eb2e35SKevin Wolf * 76865eb2e35SKevin Wolf * -EAGAIN if we had to wait for another request, previously gathered 76965eb2e35SKevin Wolf * information on cluster allocation may be invalid now. The caller 77065eb2e35SKevin Wolf * must start over anyway, so consider *cur_bytes undefined. 771250196f1SKevin Wolf */ 772226c3c26SKevin Wolf static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset, 77365eb2e35SKevin Wolf uint64_t *cur_bytes) 774226c3c26SKevin Wolf { 775226c3c26SKevin Wolf BDRVQcowState *s = bs->opaque; 776226c3c26SKevin Wolf QCowL2Meta *old_alloc; 77765eb2e35SKevin Wolf uint64_t bytes = *cur_bytes; 778226c3c26SKevin Wolf 779250196f1SKevin Wolf QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) { 780250196f1SKevin Wolf 78165eb2e35SKevin Wolf uint64_t start = guest_offset; 78265eb2e35SKevin Wolf uint64_t end = start + bytes; 78365eb2e35SKevin Wolf uint64_t old_start = l2meta_cow_start(old_alloc); 78465eb2e35SKevin Wolf uint64_t old_end = l2meta_cow_end(old_alloc); 785250196f1SKevin Wolf 786d9d74f41SKevin Wolf if (end <= old_start || start >= old_end) { 787250196f1SKevin Wolf /* No intersection */ 788250196f1SKevin Wolf } else { 789250196f1SKevin Wolf if (start < old_start) { 790250196f1SKevin Wolf /* Stop at the start of a running allocation */ 79165eb2e35SKevin Wolf bytes = old_start - start; 792250196f1SKevin Wolf } else { 79365eb2e35SKevin Wolf bytes = 0; 794250196f1SKevin Wolf } 795250196f1SKevin Wolf 79665eb2e35SKevin Wolf if (bytes == 0) { 797250196f1SKevin Wolf /* Wait for the dependency to complete. We need to recheck 798250196f1SKevin Wolf * the free/allocated clusters when we continue. */ 799250196f1SKevin Wolf qemu_co_mutex_unlock(&s->lock); 800250196f1SKevin Wolf qemu_co_queue_wait(&old_alloc->dependent_requests); 801250196f1SKevin Wolf qemu_co_mutex_lock(&s->lock); 802250196f1SKevin Wolf return -EAGAIN; 803250196f1SKevin Wolf } 804250196f1SKevin Wolf } 805250196f1SKevin Wolf } 806250196f1SKevin Wolf 80765eb2e35SKevin Wolf /* Make sure that existing clusters and new allocations are only used up to 80865eb2e35SKevin Wolf * the next dependency if we shortened the request above */ 80965eb2e35SKevin Wolf *cur_bytes = bytes; 810250196f1SKevin Wolf 811226c3c26SKevin Wolf return 0; 812226c3c26SKevin Wolf } 813226c3c26SKevin Wolf 814226c3c26SKevin Wolf /* 815226c3c26SKevin Wolf * Allocates new clusters for the given guest_offset. 816226c3c26SKevin Wolf * 817226c3c26SKevin Wolf * At most *nb_clusters are allocated, and on return *nb_clusters is updated to 818226c3c26SKevin Wolf * contain the number of clusters that have been allocated and are contiguous 819226c3c26SKevin Wolf * in the image file. 820226c3c26SKevin Wolf * 821226c3c26SKevin Wolf * If *host_offset is non-zero, it specifies the offset in the image file at 822226c3c26SKevin Wolf * which the new clusters must start. *nb_clusters can be 0 on return in this 823226c3c26SKevin Wolf * case if the cluster at host_offset is already in use. If *host_offset is 824226c3c26SKevin Wolf * zero, the clusters can be allocated anywhere in the image file. 825226c3c26SKevin Wolf * 826226c3c26SKevin Wolf * *host_offset is updated to contain the offset into the image file at which 827226c3c26SKevin Wolf * the first allocated cluster starts. 828226c3c26SKevin Wolf * 829226c3c26SKevin Wolf * Return 0 on success and -errno in error cases. -EAGAIN means that the 830226c3c26SKevin Wolf * function has been waiting for another request and the allocation must be 831226c3c26SKevin Wolf * restarted, but the whole request should not be failed. 832226c3c26SKevin Wolf */ 833226c3c26SKevin Wolf static int do_alloc_cluster_offset(BlockDriverState *bs, uint64_t guest_offset, 834226c3c26SKevin Wolf uint64_t *host_offset, unsigned int *nb_clusters) 835226c3c26SKevin Wolf { 836226c3c26SKevin Wolf BDRVQcowState *s = bs->opaque; 837226c3c26SKevin Wolf 838226c3c26SKevin Wolf trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset, 839226c3c26SKevin Wolf *host_offset, *nb_clusters); 840226c3c26SKevin Wolf 841250196f1SKevin Wolf /* Allocate new clusters */ 842250196f1SKevin Wolf trace_qcow2_cluster_alloc_phys(qemu_coroutine_self()); 843250196f1SKevin Wolf if (*host_offset == 0) { 844df021791SKevin Wolf int64_t cluster_offset = 845df021791SKevin Wolf qcow2_alloc_clusters(bs, *nb_clusters * s->cluster_size); 846250196f1SKevin Wolf if (cluster_offset < 0) { 847250196f1SKevin Wolf return cluster_offset; 848250196f1SKevin Wolf } 849250196f1SKevin Wolf *host_offset = cluster_offset; 850250196f1SKevin Wolf return 0; 851df021791SKevin Wolf } else { 85217a71e58SKevin Wolf int ret = qcow2_alloc_clusters_at(bs, *host_offset, *nb_clusters); 853df021791SKevin Wolf if (ret < 0) { 854df021791SKevin Wolf return ret; 855df021791SKevin Wolf } 856df021791SKevin Wolf *nb_clusters = ret; 857df021791SKevin Wolf return 0; 858df021791SKevin Wolf } 859250196f1SKevin Wolf } 860250196f1SKevin Wolf 861250196f1SKevin Wolf /* 86210f0ed8bSKevin Wolf * Allocates new clusters for an area that either is yet unallocated or needs a 86310f0ed8bSKevin Wolf * copy on write. If *host_offset is non-zero, clusters are only allocated if 86410f0ed8bSKevin Wolf * the new allocation can match the specified host offset. 86510f0ed8bSKevin Wolf * 86610f0ed8bSKevin Wolf * Note that guest_offset may not be cluster aligned. 86710f0ed8bSKevin Wolf * 86810f0ed8bSKevin Wolf * Returns: 86910f0ed8bSKevin Wolf * 0: if no clusters could be allocated. *bytes is set to 0, 87010f0ed8bSKevin Wolf * *host_offset is left unchanged. 87110f0ed8bSKevin Wolf * 87210f0ed8bSKevin Wolf * 1: if new clusters were allocated. *bytes may be decreased if the 87310f0ed8bSKevin Wolf * new allocation doesn't cover all of the requested area. 87410f0ed8bSKevin Wolf * *host_offset is updated to contain the host offset of the first 87510f0ed8bSKevin Wolf * newly allocated cluster. 87610f0ed8bSKevin Wolf * 87710f0ed8bSKevin Wolf * -errno: in error cases 87810f0ed8bSKevin Wolf * 879*3b8e2e26SKevin Wolf * TODO Get rid of n_start, n_end 88010f0ed8bSKevin Wolf * TODO Make *bytes actually behave as specified above 88110f0ed8bSKevin Wolf */ 88210f0ed8bSKevin Wolf static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset, 88310f0ed8bSKevin Wolf uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m, 884*3b8e2e26SKevin Wolf int n_start, int n_end) 88510f0ed8bSKevin Wolf { 88610f0ed8bSKevin Wolf BDRVQcowState *s = bs->opaque; 88710f0ed8bSKevin Wolf int l2_index; 88810f0ed8bSKevin Wolf uint64_t *l2_table; 88910f0ed8bSKevin Wolf uint64_t entry; 890f5bc6350SKevin Wolf unsigned int nb_clusters; 89110f0ed8bSKevin Wolf int ret; 89210f0ed8bSKevin Wolf 89310f0ed8bSKevin Wolf uint64_t alloc_offset; 89410f0ed8bSKevin Wolf uint64_t alloc_cluster_offset; 89510f0ed8bSKevin Wolf 89610f0ed8bSKevin Wolf trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset, 89710f0ed8bSKevin Wolf *bytes); 89810f0ed8bSKevin Wolf assert(*bytes > 0); 89910f0ed8bSKevin Wolf 900f5bc6350SKevin Wolf /* 901f5bc6350SKevin Wolf * Calculate the number of clusters to look for. We stop at L2 table 902f5bc6350SKevin Wolf * boundaries to keep things simple. 903f5bc6350SKevin Wolf */ 904f5bc6350SKevin Wolf l2_index = offset_to_l2_index(s, guest_offset); 905f5bc6350SKevin Wolf nb_clusters = MIN(size_to_clusters(s, *bytes), s->l2_size - l2_index); 906f5bc6350SKevin Wolf 90710f0ed8bSKevin Wolf /* Find L2 entry for the first involved cluster */ 90810f0ed8bSKevin Wolf ret = get_cluster_table(bs, guest_offset, &l2_table, &l2_index); 90910f0ed8bSKevin Wolf if (ret < 0) { 91010f0ed8bSKevin Wolf return ret; 91110f0ed8bSKevin Wolf } 91210f0ed8bSKevin Wolf 913*3b8e2e26SKevin Wolf entry = be64_to_cpu(l2_table[l2_index]); 91410f0ed8bSKevin Wolf 91510f0ed8bSKevin Wolf /* For the moment, overwrite compressed clusters one by one */ 91610f0ed8bSKevin Wolf if (entry & QCOW_OFLAG_COMPRESSED) { 91710f0ed8bSKevin Wolf nb_clusters = 1; 91810f0ed8bSKevin Wolf } else { 919*3b8e2e26SKevin Wolf nb_clusters = count_cow_clusters(s, nb_clusters, l2_table, l2_index); 92010f0ed8bSKevin Wolf } 92110f0ed8bSKevin Wolf 92210f0ed8bSKevin Wolf ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); 92310f0ed8bSKevin Wolf if (ret < 0) { 92410f0ed8bSKevin Wolf return ret; 92510f0ed8bSKevin Wolf } 92610f0ed8bSKevin Wolf 92710f0ed8bSKevin Wolf if (nb_clusters == 0) { 92810f0ed8bSKevin Wolf *bytes = 0; 92910f0ed8bSKevin Wolf return 0; 93010f0ed8bSKevin Wolf } 93110f0ed8bSKevin Wolf 93210f0ed8bSKevin Wolf /* Calculate start and size of allocation */ 933*3b8e2e26SKevin Wolf alloc_offset = guest_offset; 934*3b8e2e26SKevin Wolf alloc_cluster_offset = *host_offset; 93510f0ed8bSKevin Wolf 93610f0ed8bSKevin Wolf /* Allocate, if necessary at a given offset in the image file */ 93710f0ed8bSKevin Wolf ret = do_alloc_cluster_offset(bs, alloc_offset, &alloc_cluster_offset, 93810f0ed8bSKevin Wolf &nb_clusters); 93910f0ed8bSKevin Wolf if (ret < 0) { 94010f0ed8bSKevin Wolf goto fail; 94110f0ed8bSKevin Wolf } 94210f0ed8bSKevin Wolf 94310f0ed8bSKevin Wolf /* save info needed for meta data update */ 94410f0ed8bSKevin Wolf if (nb_clusters > 0) { 94510f0ed8bSKevin Wolf /* 94610f0ed8bSKevin Wolf * requested_sectors: Number of sectors from the start of the first 94710f0ed8bSKevin Wolf * newly allocated cluster to the end of the (possibly shortened 94810f0ed8bSKevin Wolf * before) write request. 94910f0ed8bSKevin Wolf * 95010f0ed8bSKevin Wolf * avail_sectors: Number of sectors from the start of the first 95110f0ed8bSKevin Wolf * newly allocated to the end of the last newly allocated cluster. 95210f0ed8bSKevin Wolf * 95310f0ed8bSKevin Wolf * nb_sectors: The number of sectors from the start of the first 95410f0ed8bSKevin Wolf * newly allocated cluster to the end of the aread that the write 95510f0ed8bSKevin Wolf * request actually writes to (excluding COW at the end) 95610f0ed8bSKevin Wolf */ 957*3b8e2e26SKevin Wolf int requested_sectors = n_end; 95810f0ed8bSKevin Wolf int avail_sectors = nb_clusters 95910f0ed8bSKevin Wolf << (s->cluster_bits - BDRV_SECTOR_BITS); 960*3b8e2e26SKevin Wolf int alloc_n_start = *host_offset == 0 ? n_start : 0; 96110f0ed8bSKevin Wolf int nb_sectors = MIN(requested_sectors, avail_sectors); 96210f0ed8bSKevin Wolf 963*3b8e2e26SKevin Wolf if (*host_offset == 0) { 96410f0ed8bSKevin Wolf *host_offset = alloc_cluster_offset; 96510f0ed8bSKevin Wolf } 96610f0ed8bSKevin Wolf 96710f0ed8bSKevin Wolf *m = g_malloc0(sizeof(**m)); 96810f0ed8bSKevin Wolf 96910f0ed8bSKevin Wolf **m = (QCowL2Meta) { 97010f0ed8bSKevin Wolf .alloc_offset = alloc_cluster_offset, 97110f0ed8bSKevin Wolf .offset = alloc_offset & ~(s->cluster_size - 1), 97210f0ed8bSKevin Wolf .nb_clusters = nb_clusters, 97310f0ed8bSKevin Wolf .nb_available = nb_sectors, 97410f0ed8bSKevin Wolf 97510f0ed8bSKevin Wolf .cow_start = { 97610f0ed8bSKevin Wolf .offset = 0, 97710f0ed8bSKevin Wolf .nb_sectors = alloc_n_start, 97810f0ed8bSKevin Wolf }, 97910f0ed8bSKevin Wolf .cow_end = { 98010f0ed8bSKevin Wolf .offset = nb_sectors * BDRV_SECTOR_SIZE, 98110f0ed8bSKevin Wolf .nb_sectors = avail_sectors - nb_sectors, 98210f0ed8bSKevin Wolf }, 98310f0ed8bSKevin Wolf }; 98410f0ed8bSKevin Wolf qemu_co_queue_init(&(*m)->dependent_requests); 98510f0ed8bSKevin Wolf QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight); 98610f0ed8bSKevin Wolf 98710f0ed8bSKevin Wolf *bytes = nb_clusters * s->cluster_size; 98810f0ed8bSKevin Wolf } else { 98910f0ed8bSKevin Wolf *bytes = 0; 99010f0ed8bSKevin Wolf return 0; 99110f0ed8bSKevin Wolf } 99210f0ed8bSKevin Wolf 99310f0ed8bSKevin Wolf return 1; 99410f0ed8bSKevin Wolf 99510f0ed8bSKevin Wolf fail: 99610f0ed8bSKevin Wolf if (*m && (*m)->nb_clusters > 0) { 99710f0ed8bSKevin Wolf QLIST_REMOVE(*m, next_in_flight); 99810f0ed8bSKevin Wolf } 99910f0ed8bSKevin Wolf return ret; 100010f0ed8bSKevin Wolf } 100110f0ed8bSKevin Wolf 100210f0ed8bSKevin Wolf /* 100345aba42fSKevin Wolf * alloc_cluster_offset 100445aba42fSKevin Wolf * 1005250196f1SKevin Wolf * For a given offset on the virtual disk, find the cluster offset in qcow2 1006250196f1SKevin Wolf * file. If the offset is not found, allocate a new cluster. 100745aba42fSKevin Wolf * 1008250196f1SKevin Wolf * If the cluster was already allocated, m->nb_clusters is set to 0 and 1009a7912369SFrediano Ziglio * other fields in m are meaningless. 101045aba42fSKevin Wolf * 1011148da7eaSKevin Wolf * If the cluster is newly allocated, m->nb_clusters is set to the number of 101268d100e9SKevin Wolf * contiguous clusters that have been allocated. In this case, the other 101368d100e9SKevin Wolf * fields of m are valid and contain information about the first allocated 101468d100e9SKevin Wolf * cluster. 1015148da7eaSKevin Wolf * 101668d100e9SKevin Wolf * If the request conflicts with another write request in flight, the coroutine 101768d100e9SKevin Wolf * is queued and will be reentered when the dependency has completed. 1018148da7eaSKevin Wolf * 1019148da7eaSKevin Wolf * Return 0 on success and -errno in error cases 102045aba42fSKevin Wolf */ 1021f4f0d391SKevin Wolf int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset, 1022f50f88b9SKevin Wolf int n_start, int n_end, int *num, uint64_t *host_offset, QCowL2Meta **m) 102345aba42fSKevin Wolf { 102445aba42fSKevin Wolf BDRVQcowState *s = bs->opaque; 1025250196f1SKevin Wolf int l2_index, ret, sectors; 10263948d1d4SKevin Wolf uint64_t *l2_table; 1027250196f1SKevin Wolf unsigned int nb_clusters, keep_clusters; 1028250196f1SKevin Wolf uint64_t cluster_offset; 102965eb2e35SKevin Wolf uint64_t cur_bytes; 103045aba42fSKevin Wolf 10313cce16f4SKevin Wolf trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, 10323cce16f4SKevin Wolf n_start, n_end); 10333cce16f4SKevin Wolf 103472424114SKevin Wolf again: 1035250196f1SKevin Wolf /* 1036250196f1SKevin Wolf * Calculate the number of clusters to look for. We stop at L2 table 1037250196f1SKevin Wolf * boundaries to keep things simple. 1038250196f1SKevin Wolf */ 103917a71e58SKevin Wolf l2_index = offset_to_l2_index(s, offset); 1040250196f1SKevin Wolf nb_clusters = MIN(size_to_clusters(s, n_end << BDRV_SECTOR_BITS), 1041250196f1SKevin Wolf s->l2_size - l2_index); 104265eb2e35SKevin Wolf n_end = MIN(n_end, nb_clusters * s->cluster_sectors); 104345aba42fSKevin Wolf 104417a71e58SKevin Wolf /* 104517a71e58SKevin Wolf * Now start gathering as many contiguous clusters as possible: 104617a71e58SKevin Wolf * 104717a71e58SKevin Wolf * 1. Check for overlaps with in-flight allocations 104817a71e58SKevin Wolf * 104917a71e58SKevin Wolf * a) Overlap not in the first cluster -> shorten this request and let 105017a71e58SKevin Wolf * the caller handle the rest in its next loop iteration. 105117a71e58SKevin Wolf * 105217a71e58SKevin Wolf * b) Real overlaps of two requests. Yield and restart the search for 105317a71e58SKevin Wolf * contiguous clusters (the situation could have changed while we 105417a71e58SKevin Wolf * were sleeping) 105517a71e58SKevin Wolf * 105617a71e58SKevin Wolf * c) TODO: Request starts in the same cluster as the in-flight 105717a71e58SKevin Wolf * allocation ends. Shorten the COW of the in-fight allocation, set 105817a71e58SKevin Wolf * cluster_offset to write to the same cluster and set up the right 105917a71e58SKevin Wolf * synchronisation between the in-flight request and the new one. 106017a71e58SKevin Wolf * 106117a71e58SKevin Wolf * 2. Count contiguous COPIED clusters. 106217a71e58SKevin Wolf * TODO: Consider cluster_offset if set in step 1c. 106317a71e58SKevin Wolf * 106417a71e58SKevin Wolf * 3. If the request still hasn't completed, allocate new clusters, 106517a71e58SKevin Wolf * considering any cluster_offset of steps 1c or 2. 106617a71e58SKevin Wolf */ 106765eb2e35SKevin Wolf cur_bytes = (n_end - n_start) * BDRV_SECTOR_SIZE; 106865eb2e35SKevin Wolf ret = handle_dependencies(bs, offset, &cur_bytes); 106917a71e58SKevin Wolf if (ret == -EAGAIN) { 107017a71e58SKevin Wolf goto again; 107117a71e58SKevin Wolf } else if (ret < 0) { 107217a71e58SKevin Wolf return ret; 107317a71e58SKevin Wolf } else { 107417a71e58SKevin Wolf /* handle_dependencies() may have decreased cur_bytes (shortened 107517a71e58SKevin Wolf * the allocations below) so that the next dependency is processed 107617a71e58SKevin Wolf * correctly during the next loop iteration. */ 107717a71e58SKevin Wolf } 107817a71e58SKevin Wolf 107965eb2e35SKevin Wolf nb_clusters = size_to_clusters(s, offset + cur_bytes) 108065eb2e35SKevin Wolf - (offset >> s->cluster_bits); 108165eb2e35SKevin Wolf 108217a71e58SKevin Wolf /* Find L2 entry for the first involved cluster */ 108317a71e58SKevin Wolf ret = get_cluster_table(bs, offset, &l2_table, &l2_index); 108417a71e58SKevin Wolf if (ret < 0) { 108517a71e58SKevin Wolf return ret; 108617a71e58SKevin Wolf } 108717a71e58SKevin Wolf 108845aba42fSKevin Wolf cluster_offset = be64_to_cpu(l2_table[l2_index]); 108945aba42fSKevin Wolf 1090037689d8SKevin Wolf /* Check how many clusters are already allocated and don't need COW */ 10918e37f681SKevin Wolf if (qcow2_get_cluster_type(cluster_offset) == QCOW2_CLUSTER_NORMAL 10928e37f681SKevin Wolf && (cluster_offset & QCOW_OFLAG_COPIED)) 10938e37f681SKevin Wolf { 1094250196f1SKevin Wolf /* We keep all QCOW_OFLAG_COPIED clusters */ 10956377af48SKevin Wolf keep_clusters = 10966377af48SKevin Wolf count_contiguous_clusters(nb_clusters, s->cluster_size, 10972bfcc4a0SKevin Wolf &l2_table[l2_index], 0, 10986377af48SKevin Wolf QCOW_OFLAG_COPIED | QCOW_OFLAG_ZERO); 1099250196f1SKevin Wolf assert(keep_clusters <= nb_clusters); 1100250196f1SKevin Wolf nb_clusters -= keep_clusters; 1101250196f1SKevin Wolf } else { 1102250196f1SKevin Wolf keep_clusters = 0; 1103250196f1SKevin Wolf cluster_offset = 0; 1104250196f1SKevin Wolf } 110545aba42fSKevin Wolf 11068e37f681SKevin Wolf cluster_offset &= L2E_OFFSET_MASK; 1107f5bc6350SKevin Wolf *host_offset = cluster_offset; 110845aba42fSKevin Wolf 110972424114SKevin Wolf /* 111072424114SKevin Wolf * The L2 table isn't used any more after this. As long as the cache works 111172424114SKevin Wolf * synchronously, it's important to release it before calling 111272424114SKevin Wolf * do_alloc_cluster_offset, which may yield if we need to wait for another 111372424114SKevin Wolf * request to complete. If we still had the reference, we could use up the 111472424114SKevin Wolf * whole cache with sleeping requests. 111572424114SKevin Wolf */ 111672424114SKevin Wolf ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); 111772424114SKevin Wolf if (ret < 0) { 111872424114SKevin Wolf return ret; 111972424114SKevin Wolf } 112072424114SKevin Wolf 1121250196f1SKevin Wolf /* If there is something left to allocate, do that now */ 112210f0ed8bSKevin Wolf if (nb_clusters == 0) { 112310f0ed8bSKevin Wolf goto done; 112410f0ed8bSKevin Wolf } 1125250196f1SKevin Wolf 1126*3b8e2e26SKevin Wolf int alloc_n_start; 1127*3b8e2e26SKevin Wolf int alloc_n_end; 1128*3b8e2e26SKevin Wolf 1129*3b8e2e26SKevin Wolf if (keep_clusters != 0) { 1130*3b8e2e26SKevin Wolf offset = start_of_cluster(s, offset 1131*3b8e2e26SKevin Wolf + keep_clusters * s->cluster_size); 1132*3b8e2e26SKevin Wolf cluster_offset = start_of_cluster(s, cluster_offset 1133*3b8e2e26SKevin Wolf + keep_clusters * s->cluster_size); 1134*3b8e2e26SKevin Wolf 1135*3b8e2e26SKevin Wolf alloc_n_start = 0; 1136*3b8e2e26SKevin Wolf alloc_n_end = n_end - keep_clusters * s->cluster_sectors; 1137*3b8e2e26SKevin Wolf } else { 1138*3b8e2e26SKevin Wolf alloc_n_start = n_start; 1139*3b8e2e26SKevin Wolf alloc_n_end = n_end; 1140*3b8e2e26SKevin Wolf } 1141*3b8e2e26SKevin Wolf 114210f0ed8bSKevin Wolf cur_bytes = nb_clusters * s->cluster_size; 1143*3b8e2e26SKevin Wolf 114410f0ed8bSKevin Wolf ret = handle_alloc(bs, offset, &cluster_offset, &cur_bytes, m, 1145*3b8e2e26SKevin Wolf alloc_n_start, alloc_n_end); 1146037689d8SKevin Wolf if (ret < 0) { 1147037689d8SKevin Wolf return ret; 1148037689d8SKevin Wolf } 1149037689d8SKevin Wolf 1150f5bc6350SKevin Wolf if (!*host_offset) { 1151f5bc6350SKevin Wolf *host_offset = cluster_offset; 1152f5bc6350SKevin Wolf } 115310f0ed8bSKevin Wolf nb_clusters = size_to_clusters(s, cur_bytes); 1154250196f1SKevin Wolf 1155250196f1SKevin Wolf /* Some cleanup work */ 115610f0ed8bSKevin Wolf done: 1157250196f1SKevin Wolf sectors = (keep_clusters + nb_clusters) << (s->cluster_bits - 9); 1158250196f1SKevin Wolf if (sectors > n_end) { 1159250196f1SKevin Wolf sectors = n_end; 1160250196f1SKevin Wolf } 116145aba42fSKevin Wolf 1162250196f1SKevin Wolf assert(sectors > n_start); 1163250196f1SKevin Wolf *num = sectors - n_start; 116445aba42fSKevin Wolf 1165148da7eaSKevin Wolf return 0; 116645aba42fSKevin Wolf } 116745aba42fSKevin Wolf 116845aba42fSKevin Wolf static int decompress_buffer(uint8_t *out_buf, int out_buf_size, 116945aba42fSKevin Wolf const uint8_t *buf, int buf_size) 117045aba42fSKevin Wolf { 117145aba42fSKevin Wolf z_stream strm1, *strm = &strm1; 117245aba42fSKevin Wolf int ret, out_len; 117345aba42fSKevin Wolf 117445aba42fSKevin Wolf memset(strm, 0, sizeof(*strm)); 117545aba42fSKevin Wolf 117645aba42fSKevin Wolf strm->next_in = (uint8_t *)buf; 117745aba42fSKevin Wolf strm->avail_in = buf_size; 117845aba42fSKevin Wolf strm->next_out = out_buf; 117945aba42fSKevin Wolf strm->avail_out = out_buf_size; 118045aba42fSKevin Wolf 118145aba42fSKevin Wolf ret = inflateInit2(strm, -12); 118245aba42fSKevin Wolf if (ret != Z_OK) 118345aba42fSKevin Wolf return -1; 118445aba42fSKevin Wolf ret = inflate(strm, Z_FINISH); 118545aba42fSKevin Wolf out_len = strm->next_out - out_buf; 118645aba42fSKevin Wolf if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || 118745aba42fSKevin Wolf out_len != out_buf_size) { 118845aba42fSKevin Wolf inflateEnd(strm); 118945aba42fSKevin Wolf return -1; 119045aba42fSKevin Wolf } 119145aba42fSKevin Wolf inflateEnd(strm); 119245aba42fSKevin Wolf return 0; 119345aba42fSKevin Wolf } 119445aba42fSKevin Wolf 119566f82ceeSKevin Wolf int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset) 119645aba42fSKevin Wolf { 119766f82ceeSKevin Wolf BDRVQcowState *s = bs->opaque; 119845aba42fSKevin Wolf int ret, csize, nb_csectors, sector_offset; 119945aba42fSKevin Wolf uint64_t coffset; 120045aba42fSKevin Wolf 120145aba42fSKevin Wolf coffset = cluster_offset & s->cluster_offset_mask; 120245aba42fSKevin Wolf if (s->cluster_cache_offset != coffset) { 120345aba42fSKevin Wolf nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1; 120445aba42fSKevin Wolf sector_offset = coffset & 511; 120545aba42fSKevin Wolf csize = nb_csectors * 512 - sector_offset; 120666f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED); 120766f82ceeSKevin Wolf ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data, nb_csectors); 120845aba42fSKevin Wolf if (ret < 0) { 12098af36488SKevin Wolf return ret; 121045aba42fSKevin Wolf } 121145aba42fSKevin Wolf if (decompress_buffer(s->cluster_cache, s->cluster_size, 121245aba42fSKevin Wolf s->cluster_data + sector_offset, csize) < 0) { 12138af36488SKevin Wolf return -EIO; 121445aba42fSKevin Wolf } 121545aba42fSKevin Wolf s->cluster_cache_offset = coffset; 121645aba42fSKevin Wolf } 121745aba42fSKevin Wolf return 0; 121845aba42fSKevin Wolf } 12195ea929e3SKevin Wolf 12205ea929e3SKevin Wolf /* 12215ea929e3SKevin Wolf * This discards as many clusters of nb_clusters as possible at once (i.e. 12225ea929e3SKevin Wolf * all clusters in the same L2 table) and returns the number of discarded 12235ea929e3SKevin Wolf * clusters. 12245ea929e3SKevin Wolf */ 12255ea929e3SKevin Wolf static int discard_single_l2(BlockDriverState *bs, uint64_t offset, 12265ea929e3SKevin Wolf unsigned int nb_clusters) 12275ea929e3SKevin Wolf { 12285ea929e3SKevin Wolf BDRVQcowState *s = bs->opaque; 12293948d1d4SKevin Wolf uint64_t *l2_table; 12305ea929e3SKevin Wolf int l2_index; 12315ea929e3SKevin Wolf int ret; 12325ea929e3SKevin Wolf int i; 12335ea929e3SKevin Wolf 12343948d1d4SKevin Wolf ret = get_cluster_table(bs, offset, &l2_table, &l2_index); 12355ea929e3SKevin Wolf if (ret < 0) { 12365ea929e3SKevin Wolf return ret; 12375ea929e3SKevin Wolf } 12385ea929e3SKevin Wolf 12395ea929e3SKevin Wolf /* Limit nb_clusters to one L2 table */ 12405ea929e3SKevin Wolf nb_clusters = MIN(nb_clusters, s->l2_size - l2_index); 12415ea929e3SKevin Wolf 12425ea929e3SKevin Wolf for (i = 0; i < nb_clusters; i++) { 12435ea929e3SKevin Wolf uint64_t old_offset; 12445ea929e3SKevin Wolf 12455ea929e3SKevin Wolf old_offset = be64_to_cpu(l2_table[l2_index + i]); 12468e37f681SKevin Wolf if ((old_offset & L2E_OFFSET_MASK) == 0) { 12475ea929e3SKevin Wolf continue; 12485ea929e3SKevin Wolf } 12495ea929e3SKevin Wolf 12505ea929e3SKevin Wolf /* First remove L2 entries */ 12515ea929e3SKevin Wolf qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); 12525ea929e3SKevin Wolf l2_table[l2_index + i] = cpu_to_be64(0); 12535ea929e3SKevin Wolf 12545ea929e3SKevin Wolf /* Then decrease the refcount */ 12555ea929e3SKevin Wolf qcow2_free_any_clusters(bs, old_offset, 1); 12565ea929e3SKevin Wolf } 12575ea929e3SKevin Wolf 12585ea929e3SKevin Wolf ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); 12595ea929e3SKevin Wolf if (ret < 0) { 12605ea929e3SKevin Wolf return ret; 12615ea929e3SKevin Wolf } 12625ea929e3SKevin Wolf 12635ea929e3SKevin Wolf return nb_clusters; 12645ea929e3SKevin Wolf } 12655ea929e3SKevin Wolf 12665ea929e3SKevin Wolf int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, 12675ea929e3SKevin Wolf int nb_sectors) 12685ea929e3SKevin Wolf { 12695ea929e3SKevin Wolf BDRVQcowState *s = bs->opaque; 12705ea929e3SKevin Wolf uint64_t end_offset; 12715ea929e3SKevin Wolf unsigned int nb_clusters; 12725ea929e3SKevin Wolf int ret; 12735ea929e3SKevin Wolf 12745ea929e3SKevin Wolf end_offset = offset + (nb_sectors << BDRV_SECTOR_BITS); 12755ea929e3SKevin Wolf 12765ea929e3SKevin Wolf /* Round start up and end down */ 12775ea929e3SKevin Wolf offset = align_offset(offset, s->cluster_size); 12785ea929e3SKevin Wolf end_offset &= ~(s->cluster_size - 1); 12795ea929e3SKevin Wolf 12805ea929e3SKevin Wolf if (offset > end_offset) { 12815ea929e3SKevin Wolf return 0; 12825ea929e3SKevin Wolf } 12835ea929e3SKevin Wolf 12845ea929e3SKevin Wolf nb_clusters = size_to_clusters(s, end_offset - offset); 12855ea929e3SKevin Wolf 12865ea929e3SKevin Wolf /* Each L2 table is handled by its own loop iteration */ 12875ea929e3SKevin Wolf while (nb_clusters > 0) { 12885ea929e3SKevin Wolf ret = discard_single_l2(bs, offset, nb_clusters); 12895ea929e3SKevin Wolf if (ret < 0) { 12905ea929e3SKevin Wolf return ret; 12915ea929e3SKevin Wolf } 12925ea929e3SKevin Wolf 12935ea929e3SKevin Wolf nb_clusters -= ret; 12945ea929e3SKevin Wolf offset += (ret * s->cluster_size); 12955ea929e3SKevin Wolf } 12965ea929e3SKevin Wolf 12975ea929e3SKevin Wolf return 0; 12985ea929e3SKevin Wolf } 1299621f0589SKevin Wolf 1300621f0589SKevin Wolf /* 1301621f0589SKevin Wolf * This zeroes as many clusters of nb_clusters as possible at once (i.e. 1302621f0589SKevin Wolf * all clusters in the same L2 table) and returns the number of zeroed 1303621f0589SKevin Wolf * clusters. 1304621f0589SKevin Wolf */ 1305621f0589SKevin Wolf static int zero_single_l2(BlockDriverState *bs, uint64_t offset, 1306621f0589SKevin Wolf unsigned int nb_clusters) 1307621f0589SKevin Wolf { 1308621f0589SKevin Wolf BDRVQcowState *s = bs->opaque; 1309621f0589SKevin Wolf uint64_t *l2_table; 1310621f0589SKevin Wolf int l2_index; 1311621f0589SKevin Wolf int ret; 1312621f0589SKevin Wolf int i; 1313621f0589SKevin Wolf 1314621f0589SKevin Wolf ret = get_cluster_table(bs, offset, &l2_table, &l2_index); 1315621f0589SKevin Wolf if (ret < 0) { 1316621f0589SKevin Wolf return ret; 1317621f0589SKevin Wolf } 1318621f0589SKevin Wolf 1319621f0589SKevin Wolf /* Limit nb_clusters to one L2 table */ 1320621f0589SKevin Wolf nb_clusters = MIN(nb_clusters, s->l2_size - l2_index); 1321621f0589SKevin Wolf 1322621f0589SKevin Wolf for (i = 0; i < nb_clusters; i++) { 1323621f0589SKevin Wolf uint64_t old_offset; 1324621f0589SKevin Wolf 1325621f0589SKevin Wolf old_offset = be64_to_cpu(l2_table[l2_index + i]); 1326621f0589SKevin Wolf 1327621f0589SKevin Wolf /* Update L2 entries */ 1328621f0589SKevin Wolf qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); 1329621f0589SKevin Wolf if (old_offset & QCOW_OFLAG_COMPRESSED) { 1330621f0589SKevin Wolf l2_table[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO); 1331621f0589SKevin Wolf qcow2_free_any_clusters(bs, old_offset, 1); 1332621f0589SKevin Wolf } else { 1333621f0589SKevin Wolf l2_table[l2_index + i] |= cpu_to_be64(QCOW_OFLAG_ZERO); 1334621f0589SKevin Wolf } 1335621f0589SKevin Wolf } 1336621f0589SKevin Wolf 1337621f0589SKevin Wolf ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); 1338621f0589SKevin Wolf if (ret < 0) { 1339621f0589SKevin Wolf return ret; 1340621f0589SKevin Wolf } 1341621f0589SKevin Wolf 1342621f0589SKevin Wolf return nb_clusters; 1343621f0589SKevin Wolf } 1344621f0589SKevin Wolf 1345621f0589SKevin Wolf int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors) 1346621f0589SKevin Wolf { 1347621f0589SKevin Wolf BDRVQcowState *s = bs->opaque; 1348621f0589SKevin Wolf unsigned int nb_clusters; 1349621f0589SKevin Wolf int ret; 1350621f0589SKevin Wolf 1351621f0589SKevin Wolf /* The zero flag is only supported by version 3 and newer */ 1352621f0589SKevin Wolf if (s->qcow_version < 3) { 1353621f0589SKevin Wolf return -ENOTSUP; 1354621f0589SKevin Wolf } 1355621f0589SKevin Wolf 1356621f0589SKevin Wolf /* Each L2 table is handled by its own loop iteration */ 1357621f0589SKevin Wolf nb_clusters = size_to_clusters(s, nb_sectors << BDRV_SECTOR_BITS); 1358621f0589SKevin Wolf 1359621f0589SKevin Wolf while (nb_clusters > 0) { 1360621f0589SKevin Wolf ret = zero_single_l2(bs, offset, nb_clusters); 1361621f0589SKevin Wolf if (ret < 0) { 1362621f0589SKevin Wolf return ret; 1363621f0589SKevin Wolf } 1364621f0589SKevin Wolf 1365621f0589SKevin Wolf nb_clusters -= ret; 1366621f0589SKevin Wolf offset += (ret * s->cluster_size); 1367621f0589SKevin Wolf } 1368621f0589SKevin Wolf 1369621f0589SKevin Wolf return 0; 1370621f0589SKevin Wolf } 1371