summaryrefslogtreecommitdiffstats
path: root/usr.bin/mkimg/qcow.c
blob: a89761b80107263ba6e8a8def689dd01d96f74b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*-
 * 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/types.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "image.h"
#include "format.h"
#include "mkimg.h"

/* Default cluster sizes. */
#define	QCOW1_CLSTR_LOG2SZ	12	/* 4KB */
#define	QCOW2_CLSTR_LOG2SZ	16	/* 64KB */

/* Flag bits in cluster offsets */
#define	QCOW_CLSTR_COMPRESSED	(1ULL << 62)
#define	QCOW_CLSTR_COPIED	(1ULL << 63)

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;
			uint32_t	refcnt_clstrs;
			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)
{
	uint64_t imagesz;

	switch (version) {
	case QCOW_VERSION_1:
		clstr_log2sz = QCOW1_CLSTR_LOG2SZ;
		break;
	case QCOW_VERSION_2:
		clstr_log2sz = QCOW2_CLSTR_LOG2SZ;
		break;
	default:
		return (EDOOFUS);
	}

	imagesz = round_clstr(imgsz * secsz);

	if (verbose)
		fprintf(stderr, "QCOW: image size = %ju, cluster size = %u\n",
		    (uintmax_t)imagesz, (u_int)(1U << clstr_log2sz));

	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;
	uint64_t *l1tbl, *l2tbl, *rctbl;
	uint16_t *rcblk;
	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;
	u_int blk_clstrsz, refcnt_clstrs;
	u_int clstrsz, l1idx, l2idx;
	int error;

	if (clstr_log2sz == 0)
		return (EDOOFUS);

	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)
	 * 3 - L2 tables
	 * 4 - RC block (v2 only)
	 * 5 - data
	 */

	l1clno = 1;
	rcclno = 0;
	rctbl = l2tbl = l1tbl = NULL;
	rcblk = NULL;

	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:
		ofsflags = 0;
		l2clno = l1clno + clstr_l1tblsz;
		hdr->u.v1.clstr_log2sz = clstr_log2sz;
		hdr->u.v1.l2_log2sz = clstr_log2sz - 3;
		be64enc(&hdr->u.v1.l1_offset, clstrsz * l1clno);
		break;
	case QCOW_VERSION_2:
		ofsflags = QCOW_CLSTR_COPIED;
		rcclno = l1clno + clstr_l1tblsz;
		l2clno = rcclno + clstr_rctblsz;
		be32enc(&hdr->clstr_log2sz, clstr_log2sz);
		be32enc(&hdr->u.v2.l1_entries, clstr_l2tbls);
		be64enc(&hdr->u.v2.l1_offset, clstrsz * l1clno);
		be64enc(&hdr->u.v2.refcnt_offset, clstrsz * rcclno);
		refcnt_clstrs = round_clstr(clstr_rcblks * 8) >> clstr_log2sz;
		be32enc(&hdr->u.v2.refcnt_clstrs, refcnt_clstrs);
		break;
	default:
		return (EDOOFUS);
	}

	if (sparse_write(fd, hdr, clstrsz) < 0) {
		error = errno;
		goto out;
	}

	free(hdr);
	hdr = NULL;

	ofs = clstrsz * l2clno;
	nclstrs = 1 + clstr_l1tblsz + clstr_rctblsz;

	l1tbl = calloc(1, clstrsz * clstr_l1tblsz);
	if (l1tbl == NULL) {
		error = ENOMEM;
		goto out;
	}

	for (n = 0; n < clstr_imgsz; n++) {
		blk = n * blk_clstrsz;
		if (image_data(blk, blk_clstrsz)) {
			nclstrs++;
			l1idx = n >> (clstr_log2sz - 3);
			if (l1tbl[l1idx] == 0) {
				be64enc(l1tbl + l1idx, ofs + ofsflags);
				ofs += clstrsz;
				nclstrs++;
			}
		}
	}

	if (sparse_write(fd, l1tbl, clstrsz * clstr_l1tblsz) < 0) {
		error = errno;
		goto out;
	}

	clstr_rcblks = 0;
	do {
		n = clstr_rcblks;
		clstr_rcblks = round_clstr((nclstrs + n) * 2) >> clstr_log2sz;
	} while (n < clstr_rcblks);

	if (rcclno > 0) {
		rctbl = calloc(1, clstrsz * clstr_rctblsz);
		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;
		}
		free(rctbl);
		rctbl = NULL;
	}

	l2tbl = malloc(clstrsz);
	if (l2tbl == NULL) {
		error = ENOMEM;
		goto out;
	}

	for (l1idx = 0; l1idx < clstr_l2tbls; l1idx++) {
		if (l1tbl[l1idx] == 0)
			continue;
		memset(l2tbl, 0, clstrsz);
		blkofs = (lba_t)l1idx * blk_clstrsz * (clstrsz >> 3);
		for (l2idx = 0; l2idx < (clstrsz >> 3); l2idx++) {
			blk = blkofs + (lba_t)l2idx * blk_clstrsz;
			if (blk >= blk_imgsz)
				break;
			if (image_data(blk, blk_clstrsz)) {
				be64enc(l2tbl + l2idx, ofs + ofsflags);
				ofs += clstrsz;
			}
		}
		if (sparse_write(fd, l2tbl, clstrsz) < 0) {
			error = errno;
			goto out;
		}
	}

	free(l2tbl);
	l2tbl = NULL;
	free(l1tbl);
	l1tbl = NULL;

	if (rcclno > 0) {
		rcblk = calloc(1, clstrsz * clstr_rcblks);
		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;
	}

	error = 0;
	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);
			if (error)
				break;
		}
	}
	if (!error)
		error = image_copyout_done(fd);

 out:
	if (rcblk != NULL)
		free(rcblk);
	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);
OpenPOWER on IntegriCloud