]> git.proxmox.com Git - qemu.git/blame - block/qed.c
qed: Table, L2 cache, and cluster functions
[qemu.git] / block / qed.c
CommitLineData
75411d23
SH
1/*
2 * QEMU Enhanced Disk Format
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Anthony Liguori <aliguori@us.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
15#include "qed.h"
16
17static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
18 const char *filename)
19{
20 const QEDHeader *header = (const QEDHeader *)buf;
21
22 if (buf_size < sizeof(*header)) {
23 return 0;
24 }
25 if (le32_to_cpu(header->magic) != QED_MAGIC) {
26 return 0;
27 }
28 return 100;
29}
30
31/**
32 * Check whether an image format is raw
33 *
34 * @fmt: Backing file format, may be NULL
35 */
36static bool qed_fmt_is_raw(const char *fmt)
37{
38 return fmt && strcmp(fmt, "raw") == 0;
39}
40
41static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
42{
43 cpu->magic = le32_to_cpu(le->magic);
44 cpu->cluster_size = le32_to_cpu(le->cluster_size);
45 cpu->table_size = le32_to_cpu(le->table_size);
46 cpu->header_size = le32_to_cpu(le->header_size);
47 cpu->features = le64_to_cpu(le->features);
48 cpu->compat_features = le64_to_cpu(le->compat_features);
49 cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
50 cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
51 cpu->image_size = le64_to_cpu(le->image_size);
52 cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
53 cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
54}
55
56static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
57{
58 le->magic = cpu_to_le32(cpu->magic);
59 le->cluster_size = cpu_to_le32(cpu->cluster_size);
60 le->table_size = cpu_to_le32(cpu->table_size);
61 le->header_size = cpu_to_le32(cpu->header_size);
62 le->features = cpu_to_le64(cpu->features);
63 le->compat_features = cpu_to_le64(cpu->compat_features);
64 le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
65 le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
66 le->image_size = cpu_to_le64(cpu->image_size);
67 le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
68 le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
69}
70
71static int qed_write_header_sync(BDRVQEDState *s)
72{
73 QEDHeader le;
74 int ret;
75
76 qed_header_cpu_to_le(&s->header, &le);
77 ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le));
78 if (ret != sizeof(le)) {
79 return ret;
80 }
81 return 0;
82}
83
84static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
85{
86 uint64_t table_entries;
87 uint64_t l2_size;
88
89 table_entries = (table_size * cluster_size) / sizeof(uint64_t);
90 l2_size = table_entries * cluster_size;
91
92 return l2_size * table_entries;
93}
94
95static bool qed_is_cluster_size_valid(uint32_t cluster_size)
96{
97 if (cluster_size < QED_MIN_CLUSTER_SIZE ||
98 cluster_size > QED_MAX_CLUSTER_SIZE) {
99 return false;
100 }
101 if (cluster_size & (cluster_size - 1)) {
102 return false; /* not power of 2 */
103 }
104 return true;
105}
106
107static bool qed_is_table_size_valid(uint32_t table_size)
108{
109 if (table_size < QED_MIN_TABLE_SIZE ||
110 table_size > QED_MAX_TABLE_SIZE) {
111 return false;
112 }
113 if (table_size & (table_size - 1)) {
114 return false; /* not power of 2 */
115 }
116 return true;
117}
118
119static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
120 uint32_t table_size)
121{
122 if (image_size % BDRV_SECTOR_SIZE != 0) {
123 return false; /* not multiple of sector size */
124 }
125 if (image_size > qed_max_image_size(cluster_size, table_size)) {
126 return false; /* image is too large */
127 }
128 return true;
129}
130
131/**
132 * Read a string of known length from the image file
133 *
134 * @file: Image file
135 * @offset: File offset to start of string, in bytes
136 * @n: String length in bytes
137 * @buf: Destination buffer
138 * @buflen: Destination buffer length in bytes
139 * @ret: 0 on success, -errno on failure
140 *
141 * The string is NUL-terminated.
142 */
143static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n,
144 char *buf, size_t buflen)
145{
146 int ret;
147 if (n >= buflen) {
148 return -EINVAL;
149 }
150 ret = bdrv_pread(file, offset, buf, n);
151 if (ret < 0) {
152 return ret;
153 }
154 buf[n] = '\0';
155 return 0;
156}
157
298800ca
SH
158QEDTable *qed_alloc_table(BDRVQEDState *s)
159{
160 /* Honor O_DIRECT memory alignment requirements */
161 return qemu_blockalign(s->bs,
162 s->header.cluster_size * s->header.table_size);
163}
164
75411d23
SH
165static int bdrv_qed_open(BlockDriverState *bs, int flags)
166{
167 BDRVQEDState *s = bs->opaque;
168 QEDHeader le_header;
169 int64_t file_size;
170 int ret;
171
172 s->bs = bs;
173
174 ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header));
175 if (ret < 0) {
176 return ret;
177 }
178 ret = 0; /* ret should always be 0 or -errno */
179 qed_header_le_to_cpu(&le_header, &s->header);
180
181 if (s->header.magic != QED_MAGIC) {
182 return -EINVAL;
183 }
184 if (s->header.features & ~QED_FEATURE_MASK) {
185 return -ENOTSUP; /* image uses unsupported feature bits */
186 }
187 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
188 return -EINVAL;
189 }
190
191 /* Round down file size to the last cluster */
192 file_size = bdrv_getlength(bs->file);
193 if (file_size < 0) {
194 return file_size;
195 }
196 s->file_size = qed_start_of_cluster(s, file_size);
197
198 if (!qed_is_table_size_valid(s->header.table_size)) {
199 return -EINVAL;
200 }
201 if (!qed_is_image_size_valid(s->header.image_size,
202 s->header.cluster_size,
203 s->header.table_size)) {
204 return -EINVAL;
205 }
206 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
207 return -EINVAL;
208 }
209
210 s->table_nelems = (s->header.cluster_size * s->header.table_size) /
211 sizeof(uint64_t);
212 s->l2_shift = ffs(s->header.cluster_size) - 1;
213 s->l2_mask = s->table_nelems - 1;
214 s->l1_shift = s->l2_shift + ffs(s->table_nelems) - 1;
215
216 if ((s->header.features & QED_F_BACKING_FILE)) {
217 if ((uint64_t)s->header.backing_filename_offset +
218 s->header.backing_filename_size >
219 s->header.cluster_size * s->header.header_size) {
220 return -EINVAL;
221 }
222
223 ret = qed_read_string(bs->file, s->header.backing_filename_offset,
224 s->header.backing_filename_size, bs->backing_file,
225 sizeof(bs->backing_file));
226 if (ret < 0) {
227 return ret;
228 }
229
230 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
231 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
232 }
233 }
234
235 /* Reset unknown autoclear feature bits. This is a backwards
236 * compatibility mechanism that allows images to be opened by older
237 * programs, which "knock out" unknown feature bits. When an image is
238 * opened by a newer program again it can detect that the autoclear
239 * feature is no longer valid.
240 */
241 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
242 !bdrv_is_read_only(bs->file)) {
243 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
244
245 ret = qed_write_header_sync(s);
246 if (ret) {
247 return ret;
248 }
249
250 /* From here on only known autoclear feature bits are valid */
251 bdrv_flush(bs->file);
252 }
253
298800ca
SH
254 s->l1_table = qed_alloc_table(s);
255 qed_init_l2_cache(&s->l2_cache);
256
257 ret = qed_read_l1_table_sync(s);
258 if (ret) {
259 qed_free_l2_cache(&s->l2_cache);
260 qemu_vfree(s->l1_table);
261 }
75411d23
SH
262 return ret;
263}
264
265static void bdrv_qed_close(BlockDriverState *bs)
266{
298800ca
SH
267 BDRVQEDState *s = bs->opaque;
268
269 qed_free_l2_cache(&s->l2_cache);
270 qemu_vfree(s->l1_table);
75411d23
SH
271}
272
273static int bdrv_qed_flush(BlockDriverState *bs)
274{
275 return bdrv_flush(bs->file);
276}
277
278static int qed_create(const char *filename, uint32_t cluster_size,
279 uint64_t image_size, uint32_t table_size,
280 const char *backing_file, const char *backing_fmt)
281{
282 QEDHeader header = {
283 .magic = QED_MAGIC,
284 .cluster_size = cluster_size,
285 .table_size = table_size,
286 .header_size = 1,
287 .features = 0,
288 .compat_features = 0,
289 .l1_table_offset = cluster_size,
290 .image_size = image_size,
291 };
292 QEDHeader le_header;
293 uint8_t *l1_table = NULL;
294 size_t l1_size = header.cluster_size * header.table_size;
295 int ret = 0;
296 BlockDriverState *bs = NULL;
297
298 ret = bdrv_create_file(filename, NULL);
299 if (ret < 0) {
300 return ret;
301 }
302
303 ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR | BDRV_O_CACHE_WB);
304 if (ret < 0) {
305 return ret;
306 }
307
308 if (backing_file) {
309 header.features |= QED_F_BACKING_FILE;
310 header.backing_filename_offset = sizeof(le_header);
311 header.backing_filename_size = strlen(backing_file);
312
313 if (qed_fmt_is_raw(backing_fmt)) {
314 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
315 }
316 }
317
318 qed_header_cpu_to_le(&header, &le_header);
319 ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header));
320 if (ret < 0) {
321 goto out;
322 }
323 ret = bdrv_pwrite(bs, sizeof(le_header), backing_file,
324 header.backing_filename_size);
325 if (ret < 0) {
326 goto out;
327 }
328
329 l1_table = qemu_mallocz(l1_size);
330 ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size);
331 if (ret < 0) {
332 goto out;
333 }
334
335 ret = 0; /* success */
336out:
337 qemu_free(l1_table);
338 bdrv_delete(bs);
339 return ret;
340}
341
342static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options)
343{
344 uint64_t image_size = 0;
345 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
346 uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
347 const char *backing_file = NULL;
348 const char *backing_fmt = NULL;
349
350 while (options && options->name) {
351 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
352 image_size = options->value.n;
353 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
354 backing_file = options->value.s;
355 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
356 backing_fmt = options->value.s;
357 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
358 if (options->value.n) {
359 cluster_size = options->value.n;
360 }
361 } else if (!strcmp(options->name, BLOCK_OPT_TABLE_SIZE)) {
362 if (options->value.n) {
363 table_size = options->value.n;
364 }
365 }
366 options++;
367 }
368
369 if (!qed_is_cluster_size_valid(cluster_size)) {
370 fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n",
371 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
372 return -EINVAL;
373 }
374 if (!qed_is_table_size_valid(table_size)) {
375 fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n",
376 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
377 return -EINVAL;
378 }
379 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
380 fprintf(stderr, "QED image size must be a non-zero multiple of "
381 "cluster size and less than %" PRIu64 " bytes\n",
382 qed_max_image_size(cluster_size, table_size));
383 return -EINVAL;
384 }
385
386 return qed_create(filename, cluster_size, image_size, table_size,
387 backing_file, backing_fmt);
388}
389
298800ca
SH
390typedef struct {
391 int is_allocated;
392 int *pnum;
393} QEDIsAllocatedCB;
394
395static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
396{
397 QEDIsAllocatedCB *cb = opaque;
398 *cb->pnum = len / BDRV_SECTOR_SIZE;
399 cb->is_allocated = ret == QED_CLUSTER_FOUND;
400}
401
75411d23
SH
402static int bdrv_qed_is_allocated(BlockDriverState *bs, int64_t sector_num,
403 int nb_sectors, int *pnum)
404{
298800ca
SH
405 BDRVQEDState *s = bs->opaque;
406 uint64_t pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
407 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
408 QEDIsAllocatedCB cb = {
409 .is_allocated = -1,
410 .pnum = pnum,
411 };
412 QEDRequest request = { .l2_table = NULL };
413
414 async_context_push();
415
416 qed_find_cluster(s, &request, pos, len, qed_is_allocated_cb, &cb);
417
418 while (cb.is_allocated == -1) {
419 qemu_aio_wait();
420 }
421
422 async_context_pop();
423
424 qed_unref_l2_cache_entry(request.l2_table);
425
426 return cb.is_allocated;
75411d23
SH
427}
428
429static int bdrv_qed_make_empty(BlockDriverState *bs)
430{
431 return -ENOTSUP;
432}
433
434static BlockDriverAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
435 int64_t sector_num,
436 QEMUIOVector *qiov, int nb_sectors,
437 BlockDriverCompletionFunc *cb,
438 void *opaque)
439{
440 return NULL;
441}
442
443static BlockDriverAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
444 int64_t sector_num,
445 QEMUIOVector *qiov, int nb_sectors,
446 BlockDriverCompletionFunc *cb,
447 void *opaque)
448{
449 return NULL;
450}
451
452static BlockDriverAIOCB *bdrv_qed_aio_flush(BlockDriverState *bs,
453 BlockDriverCompletionFunc *cb,
454 void *opaque)
455{
456 return bdrv_aio_flush(bs->file, cb, opaque);
457}
458
459static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset)
460{
461 return -ENOTSUP;
462}
463
464static int64_t bdrv_qed_getlength(BlockDriverState *bs)
465{
466 BDRVQEDState *s = bs->opaque;
467 return s->header.image_size;
468}
469
470static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
471{
472 BDRVQEDState *s = bs->opaque;
473
474 memset(bdi, 0, sizeof(*bdi));
475 bdi->cluster_size = s->header.cluster_size;
476 return 0;
477}
478
479static int bdrv_qed_change_backing_file(BlockDriverState *bs,
480 const char *backing_file,
481 const char *backing_fmt)
482{
483 BDRVQEDState *s = bs->opaque;
484 QEDHeader new_header, le_header;
485 void *buffer;
486 size_t buffer_len, backing_file_len;
487 int ret;
488
489 /* Refuse to set backing filename if unknown compat feature bits are
490 * active. If the image uses an unknown compat feature then we may not
491 * know the layout of data following the header structure and cannot safely
492 * add a new string.
493 */
494 if (backing_file && (s->header.compat_features &
495 ~QED_COMPAT_FEATURE_MASK)) {
496 return -ENOTSUP;
497 }
498
499 memcpy(&new_header, &s->header, sizeof(new_header));
500
501 new_header.features &= ~(QED_F_BACKING_FILE |
502 QED_F_BACKING_FORMAT_NO_PROBE);
503
504 /* Adjust feature flags */
505 if (backing_file) {
506 new_header.features |= QED_F_BACKING_FILE;
507
508 if (qed_fmt_is_raw(backing_fmt)) {
509 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
510 }
511 }
512
513 /* Calculate new header size */
514 backing_file_len = 0;
515
516 if (backing_file) {
517 backing_file_len = strlen(backing_file);
518 }
519
520 buffer_len = sizeof(new_header);
521 new_header.backing_filename_offset = buffer_len;
522 new_header.backing_filename_size = backing_file_len;
523 buffer_len += backing_file_len;
524
525 /* Make sure we can rewrite header without failing */
526 if (buffer_len > new_header.header_size * new_header.cluster_size) {
527 return -ENOSPC;
528 }
529
530 /* Prepare new header */
531 buffer = qemu_malloc(buffer_len);
532
533 qed_header_cpu_to_le(&new_header, &le_header);
534 memcpy(buffer, &le_header, sizeof(le_header));
535 buffer_len = sizeof(le_header);
536
537 memcpy(buffer + buffer_len, backing_file, backing_file_len);
538 buffer_len += backing_file_len;
539
540 /* Write new header */
541 ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len);
542 qemu_free(buffer);
543 if (ret == 0) {
544 memcpy(&s->header, &new_header, sizeof(new_header));
545 }
546 return ret;
547}
548
549static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result)
550{
551 return -ENOTSUP;
552}
553
554static QEMUOptionParameter qed_create_options[] = {
555 {
556 .name = BLOCK_OPT_SIZE,
557 .type = OPT_SIZE,
558 .help = "Virtual disk size (in bytes)"
559 }, {
560 .name = BLOCK_OPT_BACKING_FILE,
561 .type = OPT_STRING,
562 .help = "File name of a base image"
563 }, {
564 .name = BLOCK_OPT_BACKING_FMT,
565 .type = OPT_STRING,
566 .help = "Image format of the base image"
567 }, {
568 .name = BLOCK_OPT_CLUSTER_SIZE,
569 .type = OPT_SIZE,
570 .help = "Cluster size (in bytes)"
571 }, {
572 .name = BLOCK_OPT_TABLE_SIZE,
573 .type = OPT_SIZE,
574 .help = "L1/L2 table size (in clusters)"
575 },
576 { /* end of list */ }
577};
578
579static BlockDriver bdrv_qed = {
580 .format_name = "qed",
581 .instance_size = sizeof(BDRVQEDState),
582 .create_options = qed_create_options,
583
584 .bdrv_probe = bdrv_qed_probe,
585 .bdrv_open = bdrv_qed_open,
586 .bdrv_close = bdrv_qed_close,
587 .bdrv_create = bdrv_qed_create,
588 .bdrv_flush = bdrv_qed_flush,
589 .bdrv_is_allocated = bdrv_qed_is_allocated,
590 .bdrv_make_empty = bdrv_qed_make_empty,
591 .bdrv_aio_readv = bdrv_qed_aio_readv,
592 .bdrv_aio_writev = bdrv_qed_aio_writev,
593 .bdrv_aio_flush = bdrv_qed_aio_flush,
594 .bdrv_truncate = bdrv_qed_truncate,
595 .bdrv_getlength = bdrv_qed_getlength,
596 .bdrv_get_info = bdrv_qed_get_info,
597 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
598 .bdrv_check = bdrv_qed_check,
599};
600
601static void bdrv_qed_init(void)
602{
603 bdrv_register(&bdrv_qed);
604}
605
606block_init(bdrv_qed_init);