2 * Copyright (c) 2014 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/errno.h>
41 /* Default cluster sizes. */
42 #define QCOW1_CLSTR_LOG2SZ 12 /* 4KB */
43 #define QCOW2_CLSTR_LOG2SZ 16 /* 64KB */
45 /* Flag bits in cluster offsets */
46 #define QCOW_CLSTR_COMPRESSED (1ULL << 62)
47 #define QCOW_CLSTR_COPIED (1ULL << 63)
51 #define QCOW_MAGIC 0x514649fb
53 #define QCOW_VERSION_1 1
54 #define QCOW_VERSION_2 2
57 uint32_t clstr_log2sz; /* v2 only */
71 uint64_t refcnt_offset;
72 uint32_t refcnt_clstrs;
73 uint32_t snapshot_count;
74 uint64_t snapshot_offset;
79 static u_int clstr_log2sz;
82 round_clstr(uint64_t ofs)
86 clstrsz = 1UL << clstr_log2sz;
87 return ((ofs + clstrsz - 1) & ~(clstrsz - 1));
91 qcow_resize(lba_t imgsz, u_int version)
97 clstr_log2sz = QCOW1_CLSTR_LOG2SZ;
100 clstr_log2sz = QCOW2_CLSTR_LOG2SZ;
106 imagesz = round_clstr(imgsz * secsz);
109 fprintf(stderr, "QCOW: image size = %ju, cluster size = %u\n",
110 (uintmax_t)imagesz, (u_int)(1U << clstr_log2sz));
112 return (image_set_size(imagesz / secsz));
116 qcow1_resize(lba_t imgsz)
119 return (qcow_resize(imgsz, QCOW_VERSION_1));
123 qcow2_resize(lba_t imgsz)
126 return (qcow_resize(imgsz, QCOW_VERSION_2));
130 qcow_write(int fd, u_int version)
132 struct qcow_header *hdr;
133 uint64_t *l1tbl, *l2tbl, *rctbl;
135 uint64_t clstr_imgsz, clstr_l2tbls, clstr_l1tblsz;
136 uint64_t clstr_rcblks, clstr_rctblsz;
137 uint64_t n, imagesz, nclstrs, ofs, ofsflags;
138 lba_t blk, blkofs, blk_imgsz;
139 u_int l1clno, l2clno, rcclno;
140 u_int blk_clstrsz, refcnt_clstrs;
141 u_int clstrsz, l1idx, l2idx;
144 assert(clstr_log2sz != 0);
146 clstrsz = 1U << clstr_log2sz;
147 blk_clstrsz = clstrsz / secsz;
148 blk_imgsz = image_get_size();
149 imagesz = blk_imgsz * secsz;
150 clstr_imgsz = imagesz >> clstr_log2sz;
151 clstr_l2tbls = round_clstr(clstr_imgsz * 8) >> clstr_log2sz;
152 clstr_l1tblsz = round_clstr(clstr_l2tbls * 8) >> clstr_log2sz;
153 nclstrs = clstr_imgsz + clstr_l2tbls + clstr_l1tblsz + 1;
154 clstr_rcblks = clstr_rctblsz = 0;
156 n = clstr_rcblks + clstr_rctblsz;
157 clstr_rcblks = round_clstr((nclstrs + n) * 2) >> clstr_log2sz;
158 clstr_rctblsz = round_clstr(clstr_rcblks * 8) >> clstr_log2sz;
159 } while (n < (clstr_rcblks + clstr_rctblsz));
162 * We got all the sizes in clusters. Start the layout.
165 * 2 - RC table (v2 only)
167 * 4 - RC block (v2 only)
173 rctbl = l2tbl = l1tbl = NULL;
176 hdr = calloc(1, clstrsz);
180 be32enc(&hdr->magic, QCOW_MAGIC);
181 be32enc(&hdr->version, version);
182 be64enc(&hdr->disk_size, imagesz);
186 l2clno = l1clno + clstr_l1tblsz;
187 hdr->u.v1.clstr_log2sz = clstr_log2sz;
188 hdr->u.v1.l2_log2sz = clstr_log2sz - 3;
189 be64enc(&hdr->u.v1.l1_offset, clstrsz * l1clno);
192 ofsflags = QCOW_CLSTR_COPIED;
193 rcclno = l1clno + clstr_l1tblsz;
194 l2clno = rcclno + clstr_rctblsz;
195 be32enc(&hdr->clstr_log2sz, clstr_log2sz);
196 be32enc(&hdr->u.v2.l1_entries, clstr_l2tbls);
197 be64enc(&hdr->u.v2.l1_offset, clstrsz * l1clno);
198 be64enc(&hdr->u.v2.refcnt_offset, clstrsz * rcclno);
199 refcnt_clstrs = round_clstr(clstr_rcblks * 8) >> clstr_log2sz;
200 be32enc(&hdr->u.v2.refcnt_clstrs, refcnt_clstrs);
206 if (sparse_write(fd, hdr, clstrsz) < 0) {
214 ofs = clstrsz * l2clno;
215 nclstrs = 1 + clstr_l1tblsz + clstr_rctblsz;
217 l1tbl = calloc(clstr_l1tblsz, clstrsz);
223 for (n = 0; n < clstr_imgsz; n++) {
224 blk = n * blk_clstrsz;
225 if (image_data(blk, blk_clstrsz)) {
227 l1idx = n >> (clstr_log2sz - 3);
228 if (l1tbl[l1idx] == 0) {
229 be64enc(l1tbl + l1idx, ofs + ofsflags);
236 if (sparse_write(fd, l1tbl, clstrsz * clstr_l1tblsz) < 0) {
244 clstr_rcblks = round_clstr((nclstrs + n) * 2) >> clstr_log2sz;
245 } while (n < clstr_rcblks);
248 rctbl = calloc(clstr_rctblsz, clstrsz);
253 for (n = 0; n < clstr_rcblks; n++) {
254 be64enc(rctbl + n, ofs);
258 if (sparse_write(fd, rctbl, clstrsz * clstr_rctblsz) < 0) {
266 l2tbl = malloc(clstrsz);
272 for (l1idx = 0; l1idx < clstr_l2tbls; l1idx++) {
273 if (l1tbl[l1idx] == 0)
275 memset(l2tbl, 0, clstrsz);
276 blkofs = (lba_t)l1idx * blk_clstrsz * (clstrsz >> 3);
277 for (l2idx = 0; l2idx < (clstrsz >> 3); l2idx++) {
278 blk = blkofs + (lba_t)l2idx * blk_clstrsz;
279 if (blk >= blk_imgsz)
281 if (image_data(blk, blk_clstrsz)) {
282 be64enc(l2tbl + l2idx, ofs + ofsflags);
286 if (sparse_write(fd, l2tbl, clstrsz) < 0) {
298 rcblk = calloc(clstr_rcblks, clstrsz);
303 for (n = 0; n < nclstrs; n++)
304 be16enc(rcblk + n, 1);
305 if (sparse_write(fd, rcblk, clstrsz * clstr_rcblks) < 0) {
314 for (n = 0; n < clstr_imgsz; n++) {
315 blk = n * blk_clstrsz;
316 if (image_data(blk, blk_clstrsz)) {
317 error = image_copyout_region(fd, blk, blk_clstrsz);
323 error = image_copyout_done(fd);
343 return (qcow_write(fd, QCOW_VERSION_1));
350 return (qcow_write(fd, QCOW_VERSION_2));
353 static struct mkimg_format qcow1_format = {
355 .description = "QEMU Copy-On-Write, version 1",
356 .resize = qcow1_resize,
357 .write = qcow1_write,
359 FORMAT_DEFINE(qcow1_format);
361 static struct mkimg_format qcow2_format = {
363 .description = "QEMU Copy-On-Write, version 2",
364 .resize = qcow2_resize,
365 .write = qcow2_write,
367 FORMAT_DEFINE(qcow2_format);