Annotation of qemu/block/qcow.c, revision 1.1.1.2

1.1       root        1: /*
                      2:  * Block driver for the QCOW 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: #include "qemu-common.h"
                     25: #include "block_int.h"
                     26: #include "module.h"
                     27: #include <zlib.h>
                     28: #include "aes.h"
                     29: 
                     30: /**************************************************************/
                     31: /* QEMU COW block driver with compression and encryption support */
                     32: 
                     33: #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
                     34: #define QCOW_VERSION 1
                     35: 
                     36: #define QCOW_CRYPT_NONE 0
                     37: #define QCOW_CRYPT_AES  1
                     38: 
                     39: #define QCOW_OFLAG_COMPRESSED (1LL << 63)
                     40: 
                     41: typedef struct QCowHeader {
                     42:     uint32_t magic;
                     43:     uint32_t version;
                     44:     uint64_t backing_file_offset;
                     45:     uint32_t backing_file_size;
                     46:     uint32_t mtime;
                     47:     uint64_t size; /* in bytes */
                     48:     uint8_t cluster_bits;
                     49:     uint8_t l2_bits;
                     50:     uint32_t crypt_method;
                     51:     uint64_t l1_table_offset;
                     52: } QCowHeader;
                     53: 
                     54: #define L2_CACHE_SIZE 16
                     55: 
                     56: typedef struct BDRVQcowState {
                     57:     BlockDriverState *hd;
                     58:     int cluster_bits;
                     59:     int cluster_size;
                     60:     int cluster_sectors;
                     61:     int l2_bits;
                     62:     int l2_size;
                     63:     int l1_size;
                     64:     uint64_t cluster_offset_mask;
                     65:     uint64_t l1_table_offset;
                     66:     uint64_t *l1_table;
                     67:     uint64_t *l2_cache;
                     68:     uint64_t l2_cache_offsets[L2_CACHE_SIZE];
                     69:     uint32_t l2_cache_counts[L2_CACHE_SIZE];
                     70:     uint8_t *cluster_cache;
                     71:     uint8_t *cluster_data;
                     72:     uint64_t cluster_cache_offset;
                     73:     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
                     74:     uint32_t crypt_method_header;
                     75:     AES_KEY aes_encrypt_key;
                     76:     AES_KEY aes_decrypt_key;
                     77: } BDRVQcowState;
                     78: 
                     79: static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
                     80: 
                     81: static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
                     82: {
                     83:     const QCowHeader *cow_header = (const void *)buf;
                     84: 
                     85:     if (buf_size >= sizeof(QCowHeader) &&
                     86:         be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
                     87:         be32_to_cpu(cow_header->version) == QCOW_VERSION)
                     88:         return 100;
                     89:     else
                     90:         return 0;
                     91: }
                     92: 
                     93: static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
                     94: {
                     95:     BDRVQcowState *s = bs->opaque;
                     96:     int len, i, shift, ret;
                     97:     QCowHeader header;
                     98: 
                     99:     ret = bdrv_file_open(&s->hd, filename, flags);
                    100:     if (ret < 0)
                    101:         return ret;
                    102:     if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
                    103:         goto fail;
                    104:     be32_to_cpus(&header.magic);
                    105:     be32_to_cpus(&header.version);
                    106:     be64_to_cpus(&header.backing_file_offset);
                    107:     be32_to_cpus(&header.backing_file_size);
                    108:     be32_to_cpus(&header.mtime);
                    109:     be64_to_cpus(&header.size);
                    110:     be32_to_cpus(&header.crypt_method);
                    111:     be64_to_cpus(&header.l1_table_offset);
                    112: 
                    113:     if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
                    114:         goto fail;
                    115:     if (header.size <= 1 || header.cluster_bits < 9)
                    116:         goto fail;
                    117:     if (header.crypt_method > QCOW_CRYPT_AES)
                    118:         goto fail;
                    119:     s->crypt_method_header = header.crypt_method;
                    120:     if (s->crypt_method_header)
                    121:         bs->encrypted = 1;
                    122:     s->cluster_bits = header.cluster_bits;
                    123:     s->cluster_size = 1 << s->cluster_bits;
                    124:     s->cluster_sectors = 1 << (s->cluster_bits - 9);
                    125:     s->l2_bits = header.l2_bits;
                    126:     s->l2_size = 1 << s->l2_bits;
                    127:     bs->total_sectors = header.size / 512;
                    128:     s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
                    129: 
                    130:     /* read the level 1 table */
                    131:     shift = s->cluster_bits + s->l2_bits;
                    132:     s->l1_size = (header.size + (1LL << shift) - 1) >> shift;
                    133: 
                    134:     s->l1_table_offset = header.l1_table_offset;
                    135:     s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
                    136:     if (!s->l1_table)
                    137:         goto fail;
                    138:     if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
                    139:         s->l1_size * sizeof(uint64_t))
                    140:         goto fail;
                    141:     for(i = 0;i < s->l1_size; i++) {
                    142:         be64_to_cpus(&s->l1_table[i]);
                    143:     }
                    144:     /* alloc L2 cache */
                    145:     s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
                    146:     if (!s->l2_cache)
                    147:         goto fail;
                    148:     s->cluster_cache = qemu_malloc(s->cluster_size);
                    149:     if (!s->cluster_cache)
                    150:         goto fail;
                    151:     s->cluster_data = qemu_malloc(s->cluster_size);
                    152:     if (!s->cluster_data)
                    153:         goto fail;
                    154:     s->cluster_cache_offset = -1;
                    155: 
                    156:     /* read the backing file name */
                    157:     if (header.backing_file_offset != 0) {
                    158:         len = header.backing_file_size;
                    159:         if (len > 1023)
                    160:             len = 1023;
                    161:         if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
                    162:             goto fail;
                    163:         bs->backing_file[len] = '\0';
                    164:     }
                    165:     return 0;
                    166: 
                    167:  fail:
                    168:     qemu_free(s->l1_table);
                    169:     qemu_free(s->l2_cache);
                    170:     qemu_free(s->cluster_cache);
                    171:     qemu_free(s->cluster_data);
                    172:     bdrv_delete(s->hd);
                    173:     return -1;
                    174: }
                    175: 
                    176: static int qcow_set_key(BlockDriverState *bs, const char *key)
                    177: {
                    178:     BDRVQcowState *s = bs->opaque;
                    179:     uint8_t keybuf[16];
                    180:     int len, i;
                    181: 
                    182:     memset(keybuf, 0, 16);
                    183:     len = strlen(key);
                    184:     if (len > 16)
                    185:         len = 16;
                    186:     /* XXX: we could compress the chars to 7 bits to increase
                    187:        entropy */
                    188:     for(i = 0;i < len;i++) {
                    189:         keybuf[i] = key[i];
                    190:     }
                    191:     s->crypt_method = s->crypt_method_header;
                    192: 
                    193:     if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
                    194:         return -1;
                    195:     if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
                    196:         return -1;
                    197: #if 0
                    198:     /* test */
                    199:     {
                    200:         uint8_t in[16];
                    201:         uint8_t out[16];
                    202:         uint8_t tmp[16];
                    203:         for(i=0;i<16;i++)
                    204:             in[i] = i;
                    205:         AES_encrypt(in, tmp, &s->aes_encrypt_key);
                    206:         AES_decrypt(tmp, out, &s->aes_decrypt_key);
                    207:         for(i = 0; i < 16; i++)
                    208:             printf(" %02x", tmp[i]);
                    209:         printf("\n");
                    210:         for(i = 0; i < 16; i++)
                    211:             printf(" %02x", out[i]);
                    212:         printf("\n");
                    213:     }
                    214: #endif
                    215:     return 0;
                    216: }
                    217: 
                    218: /* The crypt function is compatible with the linux cryptoloop
                    219:    algorithm for < 4 GB images. NOTE: out_buf == in_buf is
                    220:    supported */
                    221: static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
                    222:                             uint8_t *out_buf, const uint8_t *in_buf,
                    223:                             int nb_sectors, int enc,
                    224:                             const AES_KEY *key)
                    225: {
                    226:     union {
                    227:         uint64_t ll[2];
                    228:         uint8_t b[16];
                    229:     } ivec;
                    230:     int i;
                    231: 
                    232:     for(i = 0; i < nb_sectors; i++) {
                    233:         ivec.ll[0] = cpu_to_le64(sector_num);
                    234:         ivec.ll[1] = 0;
                    235:         AES_cbc_encrypt(in_buf, out_buf, 512, key,
                    236:                         ivec.b, enc);
                    237:         sector_num++;
                    238:         in_buf += 512;
                    239:         out_buf += 512;
                    240:     }
                    241: }
                    242: 
                    243: /* 'allocate' is:
                    244:  *
                    245:  * 0 to not allocate.
                    246:  *
                    247:  * 1 to allocate a normal cluster (for sector indexes 'n_start' to
                    248:  * 'n_end')
                    249:  *
                    250:  * 2 to allocate a compressed cluster of size
                    251:  * 'compressed_size'. 'compressed_size' must be > 0 and <
                    252:  * cluster_size
                    253:  *
                    254:  * return 0 if not allocated.
                    255:  */
                    256: static uint64_t get_cluster_offset(BlockDriverState *bs,
                    257:                                    uint64_t offset, int allocate,
                    258:                                    int compressed_size,
                    259:                                    int n_start, int n_end)
                    260: {
                    261:     BDRVQcowState *s = bs->opaque;
                    262:     int min_index, i, j, l1_index, l2_index;
                    263:     uint64_t l2_offset, *l2_table, cluster_offset, tmp;
                    264:     uint32_t min_count;
                    265:     int new_l2_table;
                    266: 
                    267:     l1_index = offset >> (s->l2_bits + s->cluster_bits);
                    268:     l2_offset = s->l1_table[l1_index];
                    269:     new_l2_table = 0;
                    270:     if (!l2_offset) {
                    271:         if (!allocate)
                    272:             return 0;
                    273:         /* allocate a new l2 entry */
                    274:         l2_offset = bdrv_getlength(s->hd);
                    275:         /* round to cluster size */
                    276:         l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
                    277:         /* update the L1 entry */
                    278:         s->l1_table[l1_index] = l2_offset;
                    279:         tmp = cpu_to_be64(l2_offset);
1.1.1.2 ! root      280:         if (bdrv_pwrite_sync(s->hd,
        !           281:                 s->l1_table_offset + l1_index * sizeof(tmp),
        !           282:                 &tmp, sizeof(tmp)) < 0)
1.1       root      283:             return 0;
                    284:         new_l2_table = 1;
                    285:     }
                    286:     for(i = 0; i < L2_CACHE_SIZE; i++) {
                    287:         if (l2_offset == s->l2_cache_offsets[i]) {
                    288:             /* increment the hit count */
                    289:             if (++s->l2_cache_counts[i] == 0xffffffff) {
                    290:                 for(j = 0; j < L2_CACHE_SIZE; j++) {
                    291:                     s->l2_cache_counts[j] >>= 1;
                    292:                 }
                    293:             }
                    294:             l2_table = s->l2_cache + (i << s->l2_bits);
                    295:             goto found;
                    296:         }
                    297:     }
                    298:     /* not found: load a new entry in the least used one */
                    299:     min_index = 0;
                    300:     min_count = 0xffffffff;
                    301:     for(i = 0; i < L2_CACHE_SIZE; i++) {
                    302:         if (s->l2_cache_counts[i] < min_count) {
                    303:             min_count = s->l2_cache_counts[i];
                    304:             min_index = i;
                    305:         }
                    306:     }
                    307:     l2_table = s->l2_cache + (min_index << s->l2_bits);
                    308:     if (new_l2_table) {
                    309:         memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
1.1.1.2 ! root      310:         if (bdrv_pwrite_sync(s->hd, l2_offset, l2_table,
        !           311:                 s->l2_size * sizeof(uint64_t)) < 0)
1.1       root      312:             return 0;
                    313:     } else {
                    314:         if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
                    315:             s->l2_size * sizeof(uint64_t))
                    316:             return 0;
                    317:     }
                    318:     s->l2_cache_offsets[min_index] = l2_offset;
                    319:     s->l2_cache_counts[min_index] = 1;
                    320:  found:
                    321:     l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
                    322:     cluster_offset = be64_to_cpu(l2_table[l2_index]);
                    323:     if (!cluster_offset ||
                    324:         ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
                    325:         if (!allocate)
                    326:             return 0;
                    327:         /* allocate a new cluster */
                    328:         if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
                    329:             (n_end - n_start) < s->cluster_sectors) {
                    330:             /* if the cluster is already compressed, we must
                    331:                decompress it in the case it is not completely
                    332:                overwritten */
                    333:             if (decompress_cluster(s, cluster_offset) < 0)
                    334:                 return 0;
                    335:             cluster_offset = bdrv_getlength(s->hd);
                    336:             cluster_offset = (cluster_offset + s->cluster_size - 1) &
                    337:                 ~(s->cluster_size - 1);
                    338:             /* write the cluster content */
                    339:             if (bdrv_pwrite(s->hd, cluster_offset, s->cluster_cache, s->cluster_size) !=
                    340:                 s->cluster_size)
                    341:                 return -1;
                    342:         } else {
                    343:             cluster_offset = bdrv_getlength(s->hd);
                    344:             if (allocate == 1) {
                    345:                 /* round to cluster size */
                    346:                 cluster_offset = (cluster_offset + s->cluster_size - 1) &
                    347:                     ~(s->cluster_size - 1);
                    348:                 bdrv_truncate(s->hd, cluster_offset + s->cluster_size);
                    349:                 /* if encrypted, we must initialize the cluster
                    350:                    content which won't be written */
                    351:                 if (s->crypt_method &&
                    352:                     (n_end - n_start) < s->cluster_sectors) {
                    353:                     uint64_t start_sect;
                    354:                     start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
                    355:                     memset(s->cluster_data + 512, 0x00, 512);
                    356:                     for(i = 0; i < s->cluster_sectors; i++) {
                    357:                         if (i < n_start || i >= n_end) {
                    358:                             encrypt_sectors(s, start_sect + i,
                    359:                                             s->cluster_data,
                    360:                                             s->cluster_data + 512, 1, 1,
                    361:                                             &s->aes_encrypt_key);
                    362:                             if (bdrv_pwrite(s->hd, cluster_offset + i * 512,
                    363:                                             s->cluster_data, 512) != 512)
                    364:                                 return -1;
                    365:                         }
                    366:                     }
                    367:                 }
                    368:             } else if (allocate == 2) {
                    369:                 cluster_offset |= QCOW_OFLAG_COMPRESSED |
                    370:                     (uint64_t)compressed_size << (63 - s->cluster_bits);
                    371:             }
                    372:         }
                    373:         /* update L2 table */
                    374:         tmp = cpu_to_be64(cluster_offset);
                    375:         l2_table[l2_index] = tmp;
1.1.1.2 ! root      376:         if (bdrv_pwrite_sync(s->hd, l2_offset + l2_index * sizeof(tmp),
        !           377:                 &tmp, sizeof(tmp)) < 0)
1.1       root      378:             return 0;
                    379:     }
                    380:     return cluster_offset;
                    381: }
                    382: 
                    383: static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
                    384:                              int nb_sectors, int *pnum)
                    385: {
                    386:     BDRVQcowState *s = bs->opaque;
                    387:     int index_in_cluster, n;
                    388:     uint64_t cluster_offset;
                    389: 
                    390:     cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
                    391:     index_in_cluster = sector_num & (s->cluster_sectors - 1);
                    392:     n = s->cluster_sectors - index_in_cluster;
                    393:     if (n > nb_sectors)
                    394:         n = nb_sectors;
                    395:     *pnum = n;
                    396:     return (cluster_offset != 0);
                    397: }
                    398: 
                    399: static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
                    400:                              const uint8_t *buf, int buf_size)
                    401: {
                    402:     z_stream strm1, *strm = &strm1;
                    403:     int ret, out_len;
                    404: 
                    405:     memset(strm, 0, sizeof(*strm));
                    406: 
                    407:     strm->next_in = (uint8_t *)buf;
                    408:     strm->avail_in = buf_size;
                    409:     strm->next_out = out_buf;
                    410:     strm->avail_out = out_buf_size;
                    411: 
                    412:     ret = inflateInit2(strm, -12);
                    413:     if (ret != Z_OK)
                    414:         return -1;
                    415:     ret = inflate(strm, Z_FINISH);
                    416:     out_len = strm->next_out - out_buf;
                    417:     if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
                    418:         out_len != out_buf_size) {
                    419:         inflateEnd(strm);
                    420:         return -1;
                    421:     }
                    422:     inflateEnd(strm);
                    423:     return 0;
                    424: }
                    425: 
                    426: static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
                    427: {
                    428:     int ret, csize;
                    429:     uint64_t coffset;
                    430: 
                    431:     coffset = cluster_offset & s->cluster_offset_mask;
                    432:     if (s->cluster_cache_offset != coffset) {
                    433:         csize = cluster_offset >> (63 - s->cluster_bits);
                    434:         csize &= (s->cluster_size - 1);
                    435:         ret = bdrv_pread(s->hd, coffset, s->cluster_data, csize);
                    436:         if (ret != csize)
                    437:             return -1;
                    438:         if (decompress_buffer(s->cluster_cache, s->cluster_size,
                    439:                               s->cluster_data, csize) < 0) {
                    440:             return -1;
                    441:         }
                    442:         s->cluster_cache_offset = coffset;
                    443:     }
                    444:     return 0;
                    445: }
                    446: 
                    447: #if 0
                    448: 
                    449: static int qcow_read(BlockDriverState *bs, int64_t sector_num,
                    450:                      uint8_t *buf, int nb_sectors)
                    451: {
                    452:     BDRVQcowState *s = bs->opaque;
                    453:     int ret, index_in_cluster, n;
                    454:     uint64_t cluster_offset;
                    455: 
                    456:     while (nb_sectors > 0) {
                    457:         cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
                    458:         index_in_cluster = sector_num & (s->cluster_sectors - 1);
                    459:         n = s->cluster_sectors - index_in_cluster;
                    460:         if (n > nb_sectors)
                    461:             n = nb_sectors;
                    462:         if (!cluster_offset) {
                    463:             if (bs->backing_hd) {
                    464:                 /* read from the base image */
                    465:                 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
                    466:                 if (ret < 0)
                    467:                     return -1;
                    468:             } else {
                    469:                 memset(buf, 0, 512 * n);
                    470:             }
                    471:         } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
                    472:             if (decompress_cluster(s, cluster_offset) < 0)
                    473:                 return -1;
                    474:             memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
                    475:         } else {
                    476:             ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
                    477:             if (ret != n * 512)
                    478:                 return -1;
                    479:             if (s->crypt_method) {
                    480:                 encrypt_sectors(s, sector_num, buf, buf, n, 0,
                    481:                                 &s->aes_decrypt_key);
                    482:             }
                    483:         }
                    484:         nb_sectors -= n;
                    485:         sector_num += n;
                    486:         buf += n * 512;
                    487:     }
                    488:     return 0;
                    489: }
                    490: #endif
                    491: 
                    492: typedef struct QCowAIOCB {
                    493:     BlockDriverAIOCB common;
                    494:     int64_t sector_num;
                    495:     QEMUIOVector *qiov;
                    496:     uint8_t *buf;
                    497:     void *orig_buf;
                    498:     int nb_sectors;
                    499:     int n;
                    500:     uint64_t cluster_offset;
                    501:     uint8_t *cluster_data;
                    502:     struct iovec hd_iov;
                    503:     QEMUIOVector hd_qiov;
                    504:     BlockDriverAIOCB *hd_aiocb;
                    505: } QCowAIOCB;
                    506: 
                    507: static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
                    508: {
                    509:     QCowAIOCB *acb = (QCowAIOCB *)blockacb;
                    510:     if (acb->hd_aiocb)
                    511:         bdrv_aio_cancel(acb->hd_aiocb);
                    512:     qemu_aio_release(acb);
                    513: }
                    514: 
                    515: static AIOPool qcow_aio_pool = {
                    516:     .aiocb_size         = sizeof(QCowAIOCB),
                    517:     .cancel             = qcow_aio_cancel,
                    518: };
                    519: 
                    520: static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
                    521:         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
                    522:         BlockDriverCompletionFunc *cb, void *opaque, int is_write)
                    523: {
                    524:     QCowAIOCB *acb;
                    525: 
                    526:     acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque);
                    527:     if (!acb)
                    528:         return NULL;
                    529:     acb->hd_aiocb = NULL;
                    530:     acb->sector_num = sector_num;
                    531:     acb->qiov = qiov;
                    532:     if (qiov->niov > 1) {
                    533:         acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size);
                    534:         if (is_write)
                    535:             qemu_iovec_to_buffer(qiov, acb->buf);
                    536:     } else {
                    537:         acb->buf = (uint8_t *)qiov->iov->iov_base;
                    538:     }
                    539:     acb->nb_sectors = nb_sectors;
                    540:     acb->n = 0;
                    541:     acb->cluster_offset = 0;
                    542:     return acb;
                    543: }
                    544: 
                    545: static void qcow_aio_read_cb(void *opaque, int ret)
                    546: {
                    547:     QCowAIOCB *acb = opaque;
                    548:     BlockDriverState *bs = acb->common.bs;
                    549:     BDRVQcowState *s = bs->opaque;
                    550:     int index_in_cluster;
                    551: 
                    552:     acb->hd_aiocb = NULL;
                    553:     if (ret < 0)
                    554:         goto done;
                    555: 
                    556:  redo:
                    557:     /* post process the read buffer */
                    558:     if (!acb->cluster_offset) {
                    559:         /* nothing to do */
                    560:     } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
                    561:         /* nothing to do */
                    562:     } else {
                    563:         if (s->crypt_method) {
                    564:             encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
                    565:                             acb->n, 0,
                    566:                             &s->aes_decrypt_key);
                    567:         }
                    568:     }
                    569: 
                    570:     acb->nb_sectors -= acb->n;
                    571:     acb->sector_num += acb->n;
                    572:     acb->buf += acb->n * 512;
                    573: 
                    574:     if (acb->nb_sectors == 0) {
                    575:         /* request completed */
                    576:         ret = 0;
                    577:         goto done;
                    578:     }
                    579: 
                    580:     /* prepare next AIO request */
                    581:     acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9,
                    582:                                              0, 0, 0, 0);
                    583:     index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
                    584:     acb->n = s->cluster_sectors - index_in_cluster;
                    585:     if (acb->n > acb->nb_sectors)
                    586:         acb->n = acb->nb_sectors;
                    587: 
                    588:     if (!acb->cluster_offset) {
                    589:         if (bs->backing_hd) {
                    590:             /* read from the base image */
                    591:             acb->hd_iov.iov_base = (void *)acb->buf;
                    592:             acb->hd_iov.iov_len = acb->n * 512;
                    593:             qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
                    594:             acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
                    595:                 &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb);
                    596:             if (acb->hd_aiocb == NULL)
                    597:                 goto done;
                    598:         } else {
                    599:             /* Note: in this case, no need to wait */
                    600:             memset(acb->buf, 0, 512 * acb->n);
                    601:             goto redo;
                    602:         }
                    603:     } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
                    604:         /* add AIO support for compressed blocks ? */
                    605:         if (decompress_cluster(s, acb->cluster_offset) < 0)
                    606:             goto done;
                    607:         memcpy(acb->buf,
                    608:                s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
                    609:         goto redo;
                    610:     } else {
                    611:         if ((acb->cluster_offset & 511) != 0) {
                    612:             ret = -EIO;
                    613:             goto done;
                    614:         }
                    615:         acb->hd_iov.iov_base = (void *)acb->buf;
                    616:         acb->hd_iov.iov_len = acb->n * 512;
                    617:         qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
                    618:         acb->hd_aiocb = bdrv_aio_readv(s->hd,
                    619:                             (acb->cluster_offset >> 9) + index_in_cluster,
                    620:                             &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb);
                    621:         if (acb->hd_aiocb == NULL)
                    622:             goto done;
                    623:     }
                    624: 
                    625:     return;
                    626: 
                    627: done:
                    628:     if (acb->qiov->niov > 1) {
                    629:         qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
                    630:         qemu_vfree(acb->orig_buf);
                    631:     }
                    632:     acb->common.cb(acb->common.opaque, ret);
                    633:     qemu_aio_release(acb);
                    634: }
                    635: 
                    636: static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs,
                    637:         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
                    638:         BlockDriverCompletionFunc *cb, void *opaque)
                    639: {
                    640:     QCowAIOCB *acb;
                    641: 
                    642:     acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
                    643:     if (!acb)
                    644:         return NULL;
                    645: 
                    646:     qcow_aio_read_cb(acb, 0);
                    647:     return &acb->common;
                    648: }
                    649: 
                    650: static void qcow_aio_write_cb(void *opaque, int ret)
                    651: {
                    652:     QCowAIOCB *acb = opaque;
                    653:     BlockDriverState *bs = acb->common.bs;
                    654:     BDRVQcowState *s = bs->opaque;
                    655:     int index_in_cluster;
                    656:     uint64_t cluster_offset;
                    657:     const uint8_t *src_buf;
                    658: 
                    659:     acb->hd_aiocb = NULL;
                    660: 
                    661:     if (ret < 0)
                    662:         goto done;
                    663: 
                    664:     acb->nb_sectors -= acb->n;
                    665:     acb->sector_num += acb->n;
                    666:     acb->buf += acb->n * 512;
                    667: 
                    668:     if (acb->nb_sectors == 0) {
                    669:         /* request completed */
                    670:         ret = 0;
                    671:         goto done;
                    672:     }
                    673: 
                    674:     index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
                    675:     acb->n = s->cluster_sectors - index_in_cluster;
                    676:     if (acb->n > acb->nb_sectors)
                    677:         acb->n = acb->nb_sectors;
                    678:     cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, 1, 0,
                    679:                                         index_in_cluster,
                    680:                                         index_in_cluster + acb->n);
                    681:     if (!cluster_offset || (cluster_offset & 511) != 0) {
                    682:         ret = -EIO;
                    683:         goto done;
                    684:     }
                    685:     if (s->crypt_method) {
                    686:         if (!acb->cluster_data) {
                    687:             acb->cluster_data = qemu_mallocz(s->cluster_size);
                    688:             if (!acb->cluster_data) {
                    689:                 ret = -ENOMEM;
                    690:                 goto done;
                    691:             }
                    692:         }
                    693:         encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
                    694:                         acb->n, 1, &s->aes_encrypt_key);
                    695:         src_buf = acb->cluster_data;
                    696:     } else {
                    697:         src_buf = acb->buf;
                    698:     }
                    699: 
                    700:     acb->hd_iov.iov_base = (void *)src_buf;
                    701:     acb->hd_iov.iov_len = acb->n * 512;
                    702:     qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
                    703:     acb->hd_aiocb = bdrv_aio_writev(s->hd,
                    704:                                     (cluster_offset >> 9) + index_in_cluster,
                    705:                                     &acb->hd_qiov, acb->n,
                    706:                                     qcow_aio_write_cb, acb);
                    707:     if (acb->hd_aiocb == NULL)
                    708:         goto done;
                    709:     return;
                    710: 
                    711: done:
                    712:     if (acb->qiov->niov > 1)
                    713:         qemu_vfree(acb->orig_buf);
                    714:     acb->common.cb(acb->common.opaque, ret);
                    715:     qemu_aio_release(acb);
                    716: }
                    717: 
                    718: static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs,
                    719:         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
                    720:         BlockDriverCompletionFunc *cb, void *opaque)
                    721: {
                    722:     BDRVQcowState *s = bs->opaque;
                    723:     QCowAIOCB *acb;
                    724: 
                    725:     s->cluster_cache_offset = -1; /* disable compressed cache */
                    726: 
                    727:     acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
                    728:     if (!acb)
                    729:         return NULL;
                    730: 
                    731: 
                    732:     qcow_aio_write_cb(acb, 0);
                    733:     return &acb->common;
                    734: }
                    735: 
                    736: static void qcow_close(BlockDriverState *bs)
                    737: {
                    738:     BDRVQcowState *s = bs->opaque;
                    739:     qemu_free(s->l1_table);
                    740:     qemu_free(s->l2_cache);
                    741:     qemu_free(s->cluster_cache);
                    742:     qemu_free(s->cluster_data);
                    743:     bdrv_delete(s->hd);
                    744: }
                    745: 
                    746: static int qcow_create(const char *filename, QEMUOptionParameter *options)
                    747: {
                    748:     int fd, header_size, backing_filename_len, l1_size, i, shift;
                    749:     QCowHeader header;
                    750:     uint64_t tmp;
                    751:     int64_t total_size = 0;
                    752:     const char *backing_file = NULL;
                    753:     int flags = 0;
                    754: 
                    755:     /* Read out options */
                    756:     while (options && options->name) {
                    757:         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
                    758:             total_size = options->value.n / 512;
                    759:         } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
                    760:             backing_file = options->value.s;
                    761:         } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
                    762:             flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
                    763:         }
                    764:         options++;
                    765:     }
                    766: 
                    767:     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
                    768:     if (fd < 0)
                    769:         return -1;
                    770:     memset(&header, 0, sizeof(header));
                    771:     header.magic = cpu_to_be32(QCOW_MAGIC);
                    772:     header.version = cpu_to_be32(QCOW_VERSION);
                    773:     header.size = cpu_to_be64(total_size * 512);
                    774:     header_size = sizeof(header);
                    775:     backing_filename_len = 0;
                    776:     if (backing_file) {
                    777:         if (strcmp(backing_file, "fat:")) {
                    778:             header.backing_file_offset = cpu_to_be64(header_size);
                    779:             backing_filename_len = strlen(backing_file);
                    780:             header.backing_file_size = cpu_to_be32(backing_filename_len);
                    781:             header_size += backing_filename_len;
                    782:         } else {
                    783:             /* special backing file for vvfat */
                    784:             backing_file = NULL;
                    785:         }
                    786:         header.cluster_bits = 9; /* 512 byte cluster to avoid copying
                    787:                                     unmodifyed sectors */
                    788:         header.l2_bits = 12; /* 32 KB L2 tables */
                    789:     } else {
                    790:         header.cluster_bits = 12; /* 4 KB clusters */
                    791:         header.l2_bits = 9; /* 4 KB L2 tables */
                    792:     }
                    793:     header_size = (header_size + 7) & ~7;
                    794:     shift = header.cluster_bits + header.l2_bits;
                    795:     l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift;
                    796: 
                    797:     header.l1_table_offset = cpu_to_be64(header_size);
                    798:     if (flags & BLOCK_FLAG_ENCRYPT) {
                    799:         header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
                    800:     } else {
                    801:         header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
                    802:     }
                    803: 
                    804:     /* write all the data */
                    805:     write(fd, &header, sizeof(header));
                    806:     if (backing_file) {
                    807:         write(fd, backing_file, backing_filename_len);
                    808:     }
                    809:     lseek(fd, header_size, SEEK_SET);
                    810:     tmp = 0;
                    811:     for(i = 0;i < l1_size; i++) {
                    812:         write(fd, &tmp, sizeof(tmp));
                    813:     }
                    814:     close(fd);
                    815:     return 0;
                    816: }
                    817: 
                    818: static int qcow_make_empty(BlockDriverState *bs)
                    819: {
                    820:     BDRVQcowState *s = bs->opaque;
                    821:     uint32_t l1_length = s->l1_size * sizeof(uint64_t);
                    822:     int ret;
                    823: 
                    824:     memset(s->l1_table, 0, l1_length);
1.1.1.2 ! root      825:     if (bdrv_pwrite_sync(s->hd, s->l1_table_offset, s->l1_table,
        !           826:             l1_length) < 0)
        !           827:         return -1;
1.1       root      828:     ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
                    829:     if (ret < 0)
                    830:         return ret;
                    831: 
                    832:     memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
                    833:     memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
                    834:     memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
                    835: 
                    836:     return 0;
                    837: }
                    838: 
                    839: /* XXX: put compressed sectors first, then all the cluster aligned
                    840:    tables to avoid losing bytes in alignment */
                    841: static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
                    842:                                  const uint8_t *buf, int nb_sectors)
                    843: {
                    844:     BDRVQcowState *s = bs->opaque;
                    845:     z_stream strm;
                    846:     int ret, out_len;
                    847:     uint8_t *out_buf;
                    848:     uint64_t cluster_offset;
                    849: 
                    850:     if (nb_sectors != s->cluster_sectors)
                    851:         return -EINVAL;
                    852: 
                    853:     out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
                    854:     if (!out_buf)
                    855:         return -1;
                    856: 
                    857:     /* best compression, small window, no zlib header */
                    858:     memset(&strm, 0, sizeof(strm));
                    859:     ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
                    860:                        Z_DEFLATED, -12,
                    861:                        9, Z_DEFAULT_STRATEGY);
                    862:     if (ret != 0) {
                    863:         qemu_free(out_buf);
                    864:         return -1;
                    865:     }
                    866: 
                    867:     strm.avail_in = s->cluster_size;
                    868:     strm.next_in = (uint8_t *)buf;
                    869:     strm.avail_out = s->cluster_size;
                    870:     strm.next_out = out_buf;
                    871: 
                    872:     ret = deflate(&strm, Z_FINISH);
                    873:     if (ret != Z_STREAM_END && ret != Z_OK) {
                    874:         qemu_free(out_buf);
                    875:         deflateEnd(&strm);
                    876:         return -1;
                    877:     }
                    878:     out_len = strm.next_out - out_buf;
                    879: 
                    880:     deflateEnd(&strm);
                    881: 
                    882:     if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
                    883:         /* could not compress: write normal cluster */
                    884:         bdrv_write(bs, sector_num, buf, s->cluster_sectors);
                    885:     } else {
                    886:         cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
                    887:                                             out_len, 0, 0);
                    888:         cluster_offset &= s->cluster_offset_mask;
                    889:         if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
                    890:             qemu_free(out_buf);
                    891:             return -1;
                    892:         }
                    893:     }
                    894: 
                    895:     qemu_free(out_buf);
                    896:     return 0;
                    897: }
                    898: 
                    899: static void qcow_flush(BlockDriverState *bs)
                    900: {
                    901:     BDRVQcowState *s = bs->opaque;
                    902:     bdrv_flush(s->hd);
                    903: }
                    904: 
                    905: static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
                    906: {
                    907:     BDRVQcowState *s = bs->opaque;
                    908:     bdi->cluster_size = s->cluster_size;
                    909:     return 0;
                    910: }
                    911: 
                    912: 
                    913: static QEMUOptionParameter qcow_create_options[] = {
                    914:     {
                    915:         .name = BLOCK_OPT_SIZE,
                    916:         .type = OPT_SIZE,
                    917:         .help = "Virtual disk size"
                    918:     },
                    919:     {
                    920:         .name = BLOCK_OPT_BACKING_FILE,
                    921:         .type = OPT_STRING,
                    922:         .help = "File name of a base image"
                    923:     },
                    924:     {
                    925:         .name = BLOCK_OPT_ENCRYPT,
                    926:         .type = OPT_FLAG,
                    927:         .help = "Encrypt the image"
                    928:     },
                    929:     { NULL }
                    930: };
                    931: 
                    932: static BlockDriver bdrv_qcow = {
                    933:     .format_name       = "qcow",
                    934:     .instance_size     = sizeof(BDRVQcowState),
                    935:     .bdrv_probe                = qcow_probe,
                    936:     .bdrv_open         = qcow_open,
                    937:     .bdrv_close                = qcow_close,
                    938:     .bdrv_create       = qcow_create,
                    939:     .bdrv_flush                = qcow_flush,
                    940:     .bdrv_is_allocated = qcow_is_allocated,
                    941:     .bdrv_set_key      = qcow_set_key,
                    942:     .bdrv_make_empty   = qcow_make_empty,
                    943:     .bdrv_aio_readv    = qcow_aio_readv,
                    944:     .bdrv_aio_writev   = qcow_aio_writev,
                    945:     .bdrv_write_compressed = qcow_write_compressed,
                    946:     .bdrv_get_info     = qcow_get_info,
                    947: 
                    948:     .create_options = qcow_create_options,
                    949: };
                    950: 
                    951: static void bdrv_qcow_init(void)
                    952: {
                    953:     bdrv_register(&bdrv_qcow);
                    954: }
                    955: 
                    956: block_init(bdrv_qcow_init);

unix.superglobalmegacorp.com

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