Annotation of qemu/block/qcow2-cluster.c, revision 1.1.1.5

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.