2014-09-22 15:05:28 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2014 Marcel Moolenaar
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/errno.h>
|
2016-09-26 00:41:08 +00:00
|
|
|
#include <assert.h>
|
2014-09-22 15:05:28 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-09-26 01:06:32 +00:00
|
|
|
#include "endian.h"
|
2014-09-22 15:05:28 +00:00
|
|
|
#include "image.h"
|
|
|
|
#include "format.h"
|
|
|
|
#include "mkimg.h"
|
|
|
|
|
|
|
|
/* Default cluster sizes. */
|
|
|
|
#define QCOW1_CLSTR_LOG2SZ 12 /* 4KB */
|
|
|
|
#define QCOW2_CLSTR_LOG2SZ 16 /* 64KB */
|
|
|
|
|
2014-09-24 15:14:01 +00:00
|
|
|
/* Flag bits in cluster offsets */
|
|
|
|
#define QCOW_CLSTR_COMPRESSED (1ULL << 62)
|
|
|
|
#define QCOW_CLSTR_COPIED (1ULL << 63)
|
|
|
|
|
2014-09-22 15:05:28 +00:00
|
|
|
struct qcow_header {
|
|
|
|
uint32_t magic;
|
|
|
|
#define QCOW_MAGIC 0x514649fb
|
|
|
|
uint32_t version;
|
|
|
|
#define QCOW_VERSION_1 1
|
|
|
|
#define QCOW_VERSION_2 2
|
|
|
|
uint64_t path_offset;
|
|
|
|
uint32_t path_length;
|
|
|
|
uint32_t clstr_log2sz; /* v2 only */
|
|
|
|
uint64_t disk_size;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint8_t clstr_log2sz;
|
|
|
|
uint8_t l2_log2sz;
|
|
|
|
uint16_t _pad;
|
|
|
|
uint32_t encryption;
|
|
|
|
uint64_t l1_offset;
|
|
|
|
} v1;
|
|
|
|
struct {
|
|
|
|
uint32_t encryption;
|
|
|
|
uint32_t l1_entries;
|
|
|
|
uint64_t l1_offset;
|
|
|
|
uint64_t refcnt_offset;
|
2014-12-12 06:13:31 +00:00
|
|
|
uint32_t refcnt_clstrs;
|
2014-09-22 15:05:28 +00:00
|
|
|
uint32_t snapshot_count;
|
|
|
|
uint64_t snapshot_offset;
|
|
|
|
} v2;
|
|
|
|
} u;
|
|
|
|
};
|
|
|
|
|
|
|
|
static u_int clstr_log2sz;
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
round_clstr(uint64_t ofs)
|
|
|
|
{
|
|
|
|
uint64_t clstrsz;
|
|
|
|
|
|
|
|
clstrsz = 1UL << clstr_log2sz;
|
|
|
|
return ((ofs + clstrsz - 1) & ~(clstrsz - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qcow_resize(lba_t imgsz, u_int version)
|
|
|
|
{
|
2014-09-24 15:14:01 +00:00
|
|
|
uint64_t imagesz;
|
2014-09-22 15:05:28 +00:00
|
|
|
|
|
|
|
switch (version) {
|
|
|
|
case QCOW_VERSION_1:
|
|
|
|
clstr_log2sz = QCOW1_CLSTR_LOG2SZ;
|
|
|
|
break;
|
|
|
|
case QCOW_VERSION_2:
|
|
|
|
clstr_log2sz = QCOW2_CLSTR_LOG2SZ;
|
|
|
|
break;
|
|
|
|
default:
|
2016-09-26 00:41:08 +00:00
|
|
|
assert(0);
|
2014-09-22 15:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
imagesz = round_clstr(imgsz * secsz);
|
|
|
|
|
|
|
|
if (verbose)
|
2014-09-24 15:14:01 +00:00
|
|
|
fprintf(stderr, "QCOW: image size = %ju, cluster size = %u\n",
|
|
|
|
(uintmax_t)imagesz, (u_int)(1U << clstr_log2sz));
|
2014-09-22 15:05:28 +00:00
|
|
|
|
|
|
|
return (image_set_size(imagesz / secsz));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qcow1_resize(lba_t imgsz)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (qcow_resize(imgsz, QCOW_VERSION_1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qcow2_resize(lba_t imgsz)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (qcow_resize(imgsz, QCOW_VERSION_2));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qcow_write(int fd, u_int version)
|
|
|
|
{
|
|
|
|
struct qcow_header *hdr;
|
2014-09-24 22:45:03 +00:00
|
|
|
uint64_t *l1tbl, *l2tbl, *rctbl;
|
|
|
|
uint16_t *rcblk;
|
2014-09-24 15:14:01 +00:00
|
|
|
uint64_t clstr_imgsz, clstr_l2tbls, clstr_l1tblsz;
|
|
|
|
uint64_t clstr_rcblks, clstr_rctblsz;
|
|
|
|
uint64_t n, imagesz, nclstrs, ofs, ofsflags;
|
|
|
|
lba_t blk, blkofs, blk_imgsz;
|
|
|
|
u_int l1clno, l2clno, rcclno;
|
2014-12-12 06:13:31 +00:00
|
|
|
u_int blk_clstrsz, refcnt_clstrs;
|
2014-09-24 15:14:01 +00:00
|
|
|
u_int clstrsz, l1idx, l2idx;
|
2014-09-22 15:05:28 +00:00
|
|
|
int error;
|
|
|
|
|
2016-09-26 00:41:08 +00:00
|
|
|
assert(clstr_log2sz != 0);
|
2014-09-22 15:05:28 +00:00
|
|
|
|
2014-09-24 15:14:01 +00:00
|
|
|
clstrsz = 1U << clstr_log2sz;
|
|
|
|
blk_clstrsz = clstrsz / secsz;
|
|
|
|
blk_imgsz = image_get_size();
|
|
|
|
imagesz = blk_imgsz * secsz;
|
|
|
|
clstr_imgsz = imagesz >> clstr_log2sz;
|
|
|
|
clstr_l2tbls = round_clstr(clstr_imgsz * 8) >> clstr_log2sz;
|
|
|
|
clstr_l1tblsz = round_clstr(clstr_l2tbls * 8) >> clstr_log2sz;
|
|
|
|
nclstrs = clstr_imgsz + clstr_l2tbls + clstr_l1tblsz + 1;
|
|
|
|
clstr_rcblks = clstr_rctblsz = 0;
|
|
|
|
do {
|
|
|
|
n = clstr_rcblks + clstr_rctblsz;
|
|
|
|
clstr_rcblks = round_clstr((nclstrs + n) * 2) >> clstr_log2sz;
|
|
|
|
clstr_rctblsz = round_clstr(clstr_rcblks * 8) >> clstr_log2sz;
|
|
|
|
} while (n < (clstr_rcblks + clstr_rctblsz));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We got all the sizes in clusters. Start the layout.
|
|
|
|
* 0 - header
|
|
|
|
* 1 - L1 table
|
|
|
|
* 2 - RC table (v2 only)
|
2014-09-24 22:45:03 +00:00
|
|
|
* 3 - L2 tables
|
|
|
|
* 4 - RC block (v2 only)
|
|
|
|
* 5 - data
|
2014-09-24 15:14:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
l1clno = 1;
|
|
|
|
rcclno = 0;
|
2014-09-24 22:45:03 +00:00
|
|
|
rctbl = l2tbl = l1tbl = NULL;
|
|
|
|
rcblk = NULL;
|
2014-09-22 15:05:28 +00:00
|
|
|
|
|
|
|
hdr = calloc(1, clstrsz);
|
|
|
|
if (hdr == NULL)
|
|
|
|
return (errno);
|
|
|
|
|
|
|
|
be32enc(&hdr->magic, QCOW_MAGIC);
|
|
|
|
be32enc(&hdr->version, version);
|
|
|
|
be64enc(&hdr->disk_size, imagesz);
|
|
|
|
switch (version) {
|
|
|
|
case QCOW_VERSION_1:
|
2014-09-24 15:14:01 +00:00
|
|
|
ofsflags = 0;
|
|
|
|
l2clno = l1clno + clstr_l1tblsz;
|
2014-09-22 15:05:28 +00:00
|
|
|
hdr->u.v1.clstr_log2sz = clstr_log2sz;
|
|
|
|
hdr->u.v1.l2_log2sz = clstr_log2sz - 3;
|
2014-09-24 15:14:01 +00:00
|
|
|
be64enc(&hdr->u.v1.l1_offset, clstrsz * l1clno);
|
2014-09-22 15:05:28 +00:00
|
|
|
break;
|
|
|
|
case QCOW_VERSION_2:
|
2014-09-24 15:14:01 +00:00
|
|
|
ofsflags = QCOW_CLSTR_COPIED;
|
|
|
|
rcclno = l1clno + clstr_l1tblsz;
|
2014-09-24 22:45:03 +00:00
|
|
|
l2clno = rcclno + clstr_rctblsz;
|
2014-09-22 15:05:28 +00:00
|
|
|
be32enc(&hdr->clstr_log2sz, clstr_log2sz);
|
2014-09-24 15:14:01 +00:00
|
|
|
be32enc(&hdr->u.v2.l1_entries, clstr_l2tbls);
|
|
|
|
be64enc(&hdr->u.v2.l1_offset, clstrsz * l1clno);
|
|
|
|
be64enc(&hdr->u.v2.refcnt_offset, clstrsz * rcclno);
|
2014-12-12 06:13:31 +00:00
|
|
|
refcnt_clstrs = round_clstr(clstr_rcblks * 8) >> clstr_log2sz;
|
|
|
|
be32enc(&hdr->u.v2.refcnt_clstrs, refcnt_clstrs);
|
2014-09-22 15:05:28 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-09-26 00:41:08 +00:00
|
|
|
assert(0);
|
2014-09-22 15:05:28 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 22:45:03 +00:00
|
|
|
if (sparse_write(fd, hdr, clstrsz) < 0) {
|
2014-12-12 06:13:31 +00:00
|
|
|
error = errno;
|
2014-09-24 22:45:03 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(hdr);
|
|
|
|
hdr = NULL;
|
|
|
|
|
|
|
|
ofs = clstrsz * l2clno;
|
|
|
|
nclstrs = 1 + clstr_l1tblsz + clstr_rctblsz;
|
2014-09-22 15:05:28 +00:00
|
|
|
|
2017-03-13 20:14:07 +00:00
|
|
|
l1tbl = calloc(clstr_l1tblsz, clstrsz);
|
2014-09-22 15:05:28 +00:00
|
|
|
if (l1tbl == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-09-24 15:14:01 +00:00
|
|
|
for (n = 0; n < clstr_imgsz; n++) {
|
|
|
|
blk = n * blk_clstrsz;
|
|
|
|
if (image_data(blk, blk_clstrsz)) {
|
2014-09-24 22:45:03 +00:00
|
|
|
nclstrs++;
|
|
|
|
l1idx = n >> (clstr_log2sz - 3);
|
|
|
|
if (l1tbl[l1idx] == 0) {
|
|
|
|
be64enc(l1tbl + l1idx, ofs + ofsflags);
|
|
|
|
ofs += clstrsz;
|
|
|
|
nclstrs++;
|
|
|
|
}
|
2014-09-22 15:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 22:45:03 +00:00
|
|
|
if (sparse_write(fd, l1tbl, clstrsz * clstr_l1tblsz) < 0) {
|
2014-09-22 15:05:28 +00:00
|
|
|
error = errno;
|
|
|
|
goto out;
|
2014-09-24 22:45:03 +00:00
|
|
|
}
|
2014-09-22 15:05:28 +00:00
|
|
|
|
2014-09-24 22:45:03 +00:00
|
|
|
clstr_rcblks = 0;
|
|
|
|
do {
|
|
|
|
n = clstr_rcblks;
|
|
|
|
clstr_rcblks = round_clstr((nclstrs + n) * 2) >> clstr_log2sz;
|
|
|
|
} while (n < clstr_rcblks);
|
|
|
|
|
|
|
|
if (rcclno > 0) {
|
2017-03-13 20:14:07 +00:00
|
|
|
rctbl = calloc(clstr_rctblsz, clstrsz);
|
2014-09-24 22:45:03 +00:00
|
|
|
if (rctbl == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (n = 0; n < clstr_rcblks; n++) {
|
|
|
|
be64enc(rctbl + n, ofs);
|
|
|
|
ofs += clstrsz;
|
|
|
|
nclstrs++;
|
|
|
|
}
|
|
|
|
if (sparse_write(fd, rctbl, clstrsz * clstr_rctblsz) < 0) {
|
|
|
|
error = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-09-22 15:05:28 +00:00
|
|
|
free(rctbl);
|
|
|
|
rctbl = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
l2tbl = malloc(clstrsz);
|
|
|
|
if (l2tbl == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-09-24 15:14:01 +00:00
|
|
|
for (l1idx = 0; l1idx < clstr_l2tbls; l1idx++) {
|
2014-09-22 15:05:28 +00:00
|
|
|
if (l1tbl[l1idx] == 0)
|
|
|
|
continue;
|
|
|
|
memset(l2tbl, 0, clstrsz);
|
2014-09-24 15:14:01 +00:00
|
|
|
blkofs = (lba_t)l1idx * blk_clstrsz * (clstrsz >> 3);
|
2014-09-22 15:05:28 +00:00
|
|
|
for (l2idx = 0; l2idx < (clstrsz >> 3); l2idx++) {
|
2014-09-24 15:14:01 +00:00
|
|
|
blk = blkofs + (lba_t)l2idx * blk_clstrsz;
|
|
|
|
if (blk >= blk_imgsz)
|
2014-09-22 15:05:28 +00:00
|
|
|
break;
|
2014-09-24 15:14:01 +00:00
|
|
|
if (image_data(blk, blk_clstrsz)) {
|
|
|
|
be64enc(l2tbl + l2idx, ofs + ofsflags);
|
2014-09-22 15:05:28 +00:00
|
|
|
ofs += clstrsz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sparse_write(fd, l2tbl, clstrsz) < 0) {
|
|
|
|
error = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(l2tbl);
|
|
|
|
l2tbl = NULL;
|
|
|
|
free(l1tbl);
|
|
|
|
l1tbl = NULL;
|
|
|
|
|
2014-09-24 22:45:03 +00:00
|
|
|
if (rcclno > 0) {
|
2017-03-13 20:14:07 +00:00
|
|
|
rcblk = calloc(clstr_rcblks, clstrsz);
|
2014-09-24 22:45:03 +00:00
|
|
|
if (rcblk == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (n = 0; n < nclstrs; n++)
|
|
|
|
be16enc(rcblk + n, 1);
|
|
|
|
if (sparse_write(fd, rcblk, clstrsz * clstr_rcblks) < 0) {
|
|
|
|
error = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
free(rcblk);
|
|
|
|
rcblk = NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-22 15:05:28 +00:00
|
|
|
error = 0;
|
2014-09-24 15:14:01 +00:00
|
|
|
for (n = 0; n < clstr_imgsz; n++) {
|
|
|
|
blk = n * blk_clstrsz;
|
|
|
|
if (image_data(blk, blk_clstrsz)) {
|
|
|
|
error = image_copyout_region(fd, blk, blk_clstrsz);
|
2014-09-22 15:05:28 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!error)
|
|
|
|
error = image_copyout_done(fd);
|
|
|
|
|
|
|
|
out:
|
2014-09-24 22:45:03 +00:00
|
|
|
if (rcblk != NULL)
|
|
|
|
free(rcblk);
|
2014-09-22 15:05:28 +00:00
|
|
|
if (l2tbl != NULL)
|
|
|
|
free(l2tbl);
|
|
|
|
if (rctbl != NULL)
|
|
|
|
free(rctbl);
|
|
|
|
if (l1tbl != NULL)
|
|
|
|
free(l1tbl);
|
|
|
|
if (hdr != NULL)
|
|
|
|
free(hdr);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qcow1_write(int fd)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (qcow_write(fd, QCOW_VERSION_1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qcow2_write(int fd)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (qcow_write(fd, QCOW_VERSION_2));
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct mkimg_format qcow1_format = {
|
|
|
|
.name = "qcow",
|
|
|
|
.description = "QEMU Copy-On-Write, version 1",
|
|
|
|
.resize = qcow1_resize,
|
|
|
|
.write = qcow1_write,
|
|
|
|
};
|
|
|
|
FORMAT_DEFINE(qcow1_format);
|
|
|
|
|
|
|
|
static struct mkimg_format qcow2_format = {
|
|
|
|
.name = "qcow2",
|
|
|
|
.description = "QEMU Copy-On-Write, version 2",
|
|
|
|
.resize = qcow2_resize,
|
|
|
|
.write = qcow2_write,
|
|
|
|
};
|
|
|
|
FORMAT_DEFINE(qcow2_format);
|