forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qcow.c
1520 lines (1326 loc) · 39.5 KB
/
qcow.c
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016 Red Hat, Inc.
*
* This file is licensed to you under your choice of the GNU Lesser
* General Public License, version 2.1 or any later version (LGPLv2.1 or
* later), or the Apache License 2.0.
*/
/* Based on QEMU block/qcow{2}.c, which has this license: */
/*
* Block driver for the QCOW format
*
* Original driver from QEMU
* Copyright (c) 2004-2006 Fabrice Bellard
*
* Modified for tcmu-runner by Chris Leech
* Copyright (c) 2015, Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <endian.h>
#include <limits.h>
#include <inttypes.h>
#include <libgen.h>
#include <alloca.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <scsi/scsi.h>
#include <assert.h>
#include <errno.h>
#include <zlib.h>
#if defined(HAVE_LINUX_FALLOC)
#include <linux/falloc.h>
#endif
#include "tcmu-runner.h"
#include "tcmur_device.h"
#include "scsi_defs.h"
#include "qcow.h"
#include "qcow2.h"
/* Block Device abstraction to support multiple image types */
struct bdev_ops;
static struct bdev_ops qcow_ops;
static struct bdev_ops qcow2_ops;
static struct bdev_ops raw_ops;
struct bdev {
char *config;
void *private;
struct bdev_ops *ops;
/* from TCMU configfs configuration */
int64_t size;
uint32_t block_size;
int fd; /* image file descriptor */
};
struct bdev_ops {
int (*probe) (struct bdev *dev, int dirfd, const char *pathname);
int (*open) (struct bdev *dev, int dirfd, const char *pathname, int flags);
void (*close) (struct bdev *dev);
ssize_t (*preadv) (struct bdev *bdev, struct iovec *iov, int iovcnt, off_t offset);
ssize_t (*pwritev) (struct bdev *bdev, struct iovec *iov, int iovcnt, off_t offset);
};
static int bdev_open(struct bdev *bdev, int dirfd, const char *pathname, int flags)
{
struct bdev_ops *bdev_ops[] = {
&qcow_ops,
&qcow2_ops,
&raw_ops,
NULL,
};
struct bdev_ops **ops;
for (ops = &bdev_ops[0]; *ops != NULL; ops++) {
if ((*ops)->probe(bdev, dirfd, pathname) == 0) {
if ((*ops)->open(bdev, dirfd, pathname, flags) == -1) {
tcmu_err("image open failed: %s\n", pathname);
goto err;
}
bdev->ops = *ops;
return 0;
}
}
tcmu_err("image format not recognized: %s\n", pathname);
err:
return -1;
}
static int get_dirfd(int fd)
{
char proc_path[64];
char *img_path;
char *dir;
int dirfd;
ssize_t len;
/* more than enough room for "/proc/self/fd/<INT_MAX>\0" */
snprintf(proc_path, 64, "/proc/self/fd/%d", fd);
/* can't use lstat in /proc to get name length :( */
img_path = malloc(PATH_MAX);
len = readlink(proc_path, img_path, PATH_MAX);
img_path[len] = '\0';
dir = dirname(img_path);
dirfd = open(dir, O_DIRECTORY | O_PATH);
free(img_path);
return dirfd;
}
/* QCOW version 1 */
static void qcow_header_bswap(struct qcow_header *be, struct qcow_header *dst)
{
dst->magic = be32toh(be->magic);
dst->version = be32toh(be->version);
dst->backing_file_offset = be64toh(be->backing_file_offset);
dst->backing_file_size = be32toh(be->backing_file_size);
dst->mtime = be32toh(be->mtime);
dst->size = be64toh(be->size);
dst->cluster_bits = be->cluster_bits;
dst->l2_bits = be->l2_bits;
dst->padding = be16toh(be->padding);
dst->crypt_method = be32toh(be->crypt_method);
dst->l1_table_offset = be64toh(be->l1_table_offset);
}
static void qcow2_header_bswap(struct qcow2_header *be, struct qcow2_header *dst)
{
dst->magic = be32toh(be->magic);
dst->version = be32toh(be->version);
dst->backing_file_offset = be64toh(be->backing_file_offset);
dst->backing_file_size = be32toh(be->backing_file_size);
dst->cluster_bits = be32toh(be->cluster_bits);
dst->size = be64toh(be->size) /* in bytes */;
dst->crypt_method = be32toh(be->crypt_method);
dst->l1_size = be32toh(be->l1_size);
dst->l1_table_offset = be64toh(be->l1_table_offset);
dst->refcount_table_offset = be64toh(be->refcount_table_offset);
dst->refcount_table_clusters = be32toh(be->refcount_table_clusters);
dst->nb_snapshots = be32toh(be->nb_snapshots);
dst->snapshots_offset = be64toh(be->snapshots_offset);
if (dst->version == 2) {
/* The following fields are only valid for version >= 3 */
dst->incompatible_features = 0;
dst->compatible_features = 0;
dst->autoclear_features = 0;
dst->refcount_order = 4;
dst->header_length = 72;
} else {
dst->incompatible_features = be64toh(be->incompatible_features);
dst->compatible_features = be64toh(be->compatible_features);
dst->autoclear_features = be64toh(be->autoclear_features);
dst->refcount_order = be32toh(be->refcount_order);
dst->header_length = be32toh(be->header_length);
}
}
#define RC_CACHE_SIZE L2_CACHE_SIZE
struct qcow_state
{
int fd;
uint64_t size;
unsigned int cluster_bits;
unsigned int cluster_size;
unsigned int cluster_sectors;
unsigned int l2_bits;
unsigned int l2_size;
uint64_t cluster_offset_mask;
/* L1 table, load entire thing into RAM */
unsigned int l1_size;
uint64_t l1_table_offset;
uint64_t *l1_table;
/* L2 cache */
uint64_t *l2_cache;
uint64_t l2_cache_offsets[L2_CACHE_SIZE];
int l2_cache_counts[L2_CACHE_SIZE];
/* cluster decompression cache */
uint8_t *cluster_cache;
uint8_t *cluster_data;
uint64_t cluster_cache_offset;
struct bdev *backing_image;
uint64_t cluster_compressed;
uint64_t cluster_copied;
uint64_t cluster_mask;
/* qcow2 refcount top level table */
uint64_t refcount_table_offset;
uint32_t refcount_table_size;
uint64_t *refcount_table;
/* refcount block cache */
unsigned int refcount_order;
void *rc_cache;
uint64_t rc_cache_offsets[RC_CACHE_SIZE];
int rc_cache_counts[RC_CACHE_SIZE];
uint64_t (*block_alloc) (struct qcow_state *s, size_t size);
int (*set_refcount) (struct qcow_state *s, uint64_t cluster_offset, uint64_t value);
uint64_t first_free_cluster;
};
static uint64_t qcow_block_alloc(struct qcow_state *s, size_t size);
static uint64_t qcow2_block_alloc(struct qcow_state *s, size_t size);
static int qcow_no_refcount(struct qcow_state *s, uint64_t cluster_offset, uint64_t value)
{
return 0;
}
static int qcow2_set_refcount(struct qcow_state *s, uint64_t cluster_offset, uint64_t value);
static int qcow_probe(struct bdev *bdev, int dirfd, const char *pathname)
{
int fd;
struct {
uint32_t magic;
uint32_t version;
} head;
tcmu_dbg("%s\n", __func__);
if (faccessat(dirfd, pathname, R_OK|W_OK, AT_EACCESS) == -1)
return -1;
if ((fd = openat(dirfd, pathname, O_RDONLY)) == -1)
return -1;
if (pread(fd, &head, sizeof(head), 0) == -1)
goto err;
if (be32toh(head.magic) != QCOW_MAGIC)
goto err;
if (be32toh(head.version) != 1)
goto err;
close(fd);
return 0;
err:
close(fd);
return -1;
}
static int qcow2_probe(struct bdev *bdev, int dirfd, const char *pathname)
{
int fd;
struct {
uint32_t magic;
uint32_t version;
} head;
tcmu_dbg("%s\n", __func__);
if (faccessat(dirfd, pathname, R_OK|W_OK, AT_EACCESS) == -1) {
tcmu_err("faccessat failed dirfd %d, pathname %s, errno %d\n",
dirfd, pathname, errno);
return -1;
}
if ((fd = openat(dirfd, pathname, O_RDONLY)) == -1) {
tcmu_err("openat failed dirfd %d, pathname %s, errno %d\n",
dirfd, pathname, errno);
return -1;
}
if (pread(fd, &head, sizeof(head), 0) == -1) {
tcmu_err("pread failed dirfd %d, pathname %s, errno %d\n",
dirfd, pathname, errno);
goto err;
}
if (be32toh(head.magic) != QCOW_MAGIC) {
tcmu_warn("not qcow: will treat as raw: %s", pathname);
goto err;
}
if (be32toh(head.version) < 2) {
tcmu_err("version = %d, pathname %s\n", head.version, pathname);
goto err;
}
close(fd);
return 0;
err:
close(fd);
return -1;
}
static int qcow_validate_header(struct qcow_header *header)
{
if (header->magic != QCOW_MAGIC) {
tcmu_err("header is not QCOW\n");
return -1;
}
if (header->version != 1) {
tcmu_err("version is %d, expected 1\n", header->version);
return -1;
}
if (header->cluster_bits < 9 || header->cluster_bits > 16) {
tcmu_err("bad cluster_bits = %d\n", header->cluster_bits);
return -1;
}
if (header->l2_bits < (9 - 3) || header->l2_bits > (16 - 3)) {
tcmu_err("bad l2_bits = %d\n", header->l2_bits);
return -1;
}
switch (header->crypt_method) {
case QCOW_CRYPT_NONE:
break;
case QCOW_CRYPT_AES:
tcmu_err("QCOW AES-CBC encryption has been deprecated\n");
tcmu_err("Convert to unencrypted image using qemu-img\n");
return -1;
default:
tcmu_err("Invalid encryption value %d\n", header->crypt_method);
return -1;
}
return 0;
}
static int qcow2_validate_header(struct qcow2_header *header)
{
/* TODO check other stuff ... L1, refcount, snapshots */
if (header->magic != QCOW_MAGIC) {
tcmu_err("header is not QCOW\n");
return -1;
}
if (header->version < 2) {
tcmu_err("version is %d, expected 2 or 3\n", header->version);
return -1;
}
if (header->cluster_bits < 9 || header->cluster_bits > 16) {
tcmu_err("bad cluster_bits = %d\n", header->cluster_bits);
return -1;
}
switch (header->crypt_method) {
case QCOW2_CRYPT_NONE:
break;
case QCOW2_CRYPT_AES:
tcmu_err("QCOW AES-CBC encryption has been deprecated\n");
tcmu_err("Convert to unencrypted image using qemu-img\n");
return -1;
default:
tcmu_err("Invalid encryption value %d\n", header->crypt_method);
return -1;
}
return 0;
}
static int qcow_setup_backing_file(struct bdev *bdev, struct qcow_header *header)
{
struct qcow_state *s = bdev->private;
char *backing_file;
uint64_t offset;
size_t len;
int dirfd;
int ret;
offset = header->backing_file_offset;
len = header->backing_file_size;
if (offset == 0 || len == 0)
return 0;
if (len >= PATH_MAX) {
tcmu_err("Backing file name too long\n");
return -1;
}
backing_file = alloca(len + 1);
if (pread(bdev->fd, backing_file, len, header->backing_file_offset) != len) {
tcmu_err("Error reading backing file name\n");
return -1;
}
backing_file[len] = '\0';
s->backing_image = calloc(1, sizeof(struct bdev));
if (!s->backing_image)
return -1;
/* backing file settings copied from overlay */
s->backing_image->size = bdev->size;
s->backing_image->block_size = bdev->block_size;
/* backing file pathname may be relative to the overlay image */
dirfd = get_dirfd(bdev->fd);
if (dirfd == -1)
goto fail;
ret = bdev_open(s->backing_image, dirfd, backing_file, O_RDONLY);
close(dirfd);
if (ret == -1)
goto fail;
return 0;
fail:
free(s->backing_image);
s->backing_image = NULL;
return -1;
}
static int qcow2_setup_backing_file(struct bdev *bdev, struct qcow2_header *header)
{
/* backing file info is at the same place in both headers,
* so we can cheat and use this for qcow2 also */
return qcow_setup_backing_file(bdev, (struct qcow_header *) header);
}
static int qcow_image_open(struct bdev *bdev, int dirfd, const char *pathname, int flags)
{
struct qcow_header buf;
struct qcow_header header;
struct qcow_state *s;
uint64_t l1_size;
unsigned int shift;
ssize_t read;
s = calloc(1, sizeof(struct qcow_state));
if (!s)
return -1;
bdev->private = s;
bdev->fd = openat(dirfd, pathname, flags);
s->fd = bdev->fd;
if (bdev->fd == -1) {
tcmu_err("Failed to open file: %s\n", pathname);
goto fail_nofd;
}
if (pread(bdev->fd, &buf, sizeof(buf), 0) != sizeof(buf)) {
tcmu_err("Failed to read file: %s\n", pathname);
goto fail;
}
qcow_header_bswap(&buf, &header);
if (qcow_validate_header(&header) < 0)
goto fail;
if (bdev->size != header.size) {
tcmu_err("size misconfigured, TCMU says %" PRId64
" but image says %" PRId64 "\n",
bdev->size, header.size);
goto fail;
}
s->size = bdev->size;
if (bdev->block_size != 512) {
tcmu_err("block_size misconfigured, TCMU says %" PRId32
" but qcow only supports 512\n",
bdev->block_size);
goto fail;
}
s->cluster_bits = header.cluster_bits;
s->cluster_size = 1 << s->cluster_bits;
s->cluster_sectors = 1 << (s->cluster_bits - 9);
s->l2_bits = header.l2_bits;
s->l2_size = 1 << s->l2_bits;
s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
shift = s->cluster_bits + s->l2_bits;
if (header.size > UINT64_MAX - (1LL << shift)) {
tcmu_err("Image size too big\n");
goto fail;
}
l1_size = (header.size + (1LL << shift) - 1) >> shift;
if (l1_size > INT_MAX / sizeof(uint64_t)) {
tcmu_err("Image size too big\n");
goto fail;
}
if (round_up(header.size, bdev->block_size) != header.size) {
tcmu_err("Image size is not an integer multiple"
" of the block size\n");
goto fail;
}
s->l1_size = l1_size;
s->l1_table_offset = header.l1_table_offset;
s->l1_table = calloc(s->l1_size, sizeof(uint64_t));
if (!s->l1_table) {
tcmu_err("Failed to allocate L1 table\n");
goto fail;
}
read = pread(bdev->fd, s->l1_table, s->l1_size * sizeof(uint64_t), s->l1_table_offset);
if (read != s->l1_size * sizeof(uint64_t)) {
tcmu_err("Failed to read L1 table\n");
goto fail;
}
s->l2_cache = calloc(L2_CACHE_SIZE, s->l2_size * sizeof(uint64_t));
if (s->l2_cache == NULL) {
tcmu_err("Failed to allocate L2 cache\n");
goto fail;
}
/* cluster decompression cache */
s->cluster_cache = calloc(1, s->cluster_size);
s->cluster_data = calloc(1, s->cluster_size);
s->cluster_cache_offset = -1;
if (!s->cluster_cache || !s->cluster_data) {
tcmu_err("Failed to allocate cluster decompression space\n");
goto fail;
}
if (qcow_setup_backing_file(bdev, &header) == -1)
goto fail;
s->cluster_compressed = QCOW_OFLAG_COMPRESSED;
s->cluster_mask = ~QCOW_OFLAG_COMPRESSED;
s->block_alloc = qcow_block_alloc;
s->set_refcount = qcow_no_refcount;
tcmu_dbg("%d: %s\n", bdev->fd, pathname);
return 0;
fail:
close(bdev->fd);
free(s->cluster_cache);
free(s->cluster_data);
free(s->l2_cache);
free(s->l1_table);
fail_nofd:
free(s);
return -1;
}
static int qcow2_image_open(struct bdev *bdev, int dirfd, const char *pathname, int flags)
{
struct qcow2_header buf;
struct qcow2_header header;
struct qcow_state *s;
uint64_t l1_size;
unsigned int shift;
ssize_t read;
s = calloc(1, sizeof(struct qcow_state));
if (!s)
return -1;
bdev->private = s;
bdev->fd = openat(dirfd, pathname, flags);
s->fd = bdev->fd;
if (bdev->fd == -1) {
tcmu_err("Failed to open file: %s\n", pathname);
goto fail_nofd;
}
if (pread(bdev->fd, &buf, sizeof(buf), 0) != sizeof(buf)) {
tcmu_err("Failed to read file: %s\n", pathname);
goto fail;
}
qcow2_header_bswap(&buf, &header);
if (qcow2_validate_header(&header) < 0)
goto fail;
if (bdev->size != header.size) {
tcmu_err("size misconfigured, TCMU says %" PRId64
" but image says %" PRId64 "\n",
bdev->size, header.size);
goto fail;
}
s->size = bdev->size;
if (bdev->block_size != 512) {
tcmu_err("block_size misconfigured, TCMU says %" PRId32
" but qcow only supports 512\n",
bdev->block_size);
goto fail;
}
s->cluster_bits = header.cluster_bits;
s->cluster_size = 1 << s->cluster_bits;
s->cluster_sectors = 1 << (s->cluster_bits - 9);
s->l2_bits = s->cluster_bits - 3; // L2 table is always 1 cluster in size (8 (2^3) byte entries)
s->l2_size = 1 << s->l2_bits;
s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
shift = s->cluster_bits + s->l2_bits;
if (header.size > UINT64_MAX - (1LL << shift)) {
tcmu_err("Image size too big\n");
goto fail;
}
l1_size = (header.size + (1LL << shift) - 1) >> shift;
if (l1_size > INT_MAX / sizeof(uint64_t)) {
tcmu_err("Image size too big\n");
goto fail;
}
if (round_up(header.size, bdev->block_size) != header.size) {
tcmu_err("Image size is not an integer multiple"
" of the block size\n");
goto fail;
}
s->l1_size = l1_size;
// why did they add this to qcow2 ?
if (header.l1_size != s->l1_size) {
tcmu_err("L1 size is incorrect\n");
goto fail;
}
s->l1_table_offset = header.l1_table_offset;
s->l1_table = calloc(s->l1_size, sizeof(uint64_t));
if (!s->l1_table) {
tcmu_err("Failed to allocate L1 table\n");
goto fail;
}
read = pread(bdev->fd, s->l1_table, s->l1_size * sizeof(uint64_t), s->l1_table_offset);
if (read != s->l1_size * sizeof(uint64_t)) {
tcmu_err("Failed to read L1 table\n");
goto fail;
}
s->l2_cache = calloc(L2_CACHE_SIZE, s->l2_size * sizeof(uint64_t));
if (s->l2_cache == NULL) {
tcmu_err("Failed to allocate L2 cache\n");
goto fail;
}
tcmu_dbg("s->l2_cache = %p\n", s->l2_cache);
/* cluster decompression cache */
s->cluster_cache = calloc(1, s->cluster_size);
s->cluster_data = calloc(1, s->cluster_size);
s->cluster_cache_offset = -1;
if (!s->cluster_cache || !s->cluster_data) {
tcmu_err("Failed to allocate cluster decompression space\n");
goto fail;
}
tcmu_dbg("s->cluster_cache = %p\n", s->cluster_cache);
/* refcount table */
s->refcount_table_offset = header.refcount_table_offset;
s->refcount_table_size = header.refcount_table_clusters << (s->cluster_bits - 3);
s->refcount_table = calloc(s->refcount_table_size, sizeof(uint64_t));
if (!s->refcount_table) {
tcmu_err("Failed to allocate refcount table\n");
goto fail;
}
read = pread(bdev->fd, s->refcount_table, s->refcount_table_size * sizeof(uint64_t), s->refcount_table_offset);
if (read != s->refcount_table_size * sizeof(uint64_t)) {
tcmu_err("Failed to read refcount table\n");
goto fail;
}
s->refcount_order = header.refcount_order;
s->rc_cache = calloc(RC_CACHE_SIZE, s->cluster_size);
if (s->rc_cache == NULL) {
tcmu_err("Failed to allocate refcount cache\n");
goto fail;
}
tcmu_dbg("s->rc_cache = %p\n", s->rc_cache);
if (qcow2_setup_backing_file(bdev, &header) == -1)
goto fail;
s->cluster_compressed = QCOW2_OFLAG_COMPRESSED;
s->cluster_copied = QCOW2_OFLAG_COPIED;
s->cluster_mask = ~(QCOW_OFLAG_COMPRESSED | QCOW2_OFLAG_COPIED | QCOW2_OFLAG_ZERO);
s->block_alloc = qcow2_block_alloc;
s->set_refcount = qcow2_set_refcount;
tcmu_dbg("%d: %s\n", bdev->fd, pathname);
return 0;
fail:
close(bdev->fd);
free(s->cluster_cache);
free(s->cluster_data);
free(s->rc_cache);
free(s->refcount_table);
free(s->l2_cache);
free(s->l1_table);
fail_nofd:
free(s);
return -1;
}
static void qcow_image_close(struct bdev *bdev)
{
struct qcow_state *s = bdev->private;
if (s->backing_image) {
s->backing_image->ops->close(s->backing_image);
free(s->backing_image);
}
close(bdev->fd);
free(s->cluster_cache);
free(s->cluster_data);
free(s->l1_table);
free(s->l2_cache);
free(s->refcount_table);
free(s->rc_cache);
free(s);
}
static uint64_t *l2_cache_lookup(struct qcow_state *s, uint64_t l2_offset)
{
int i, j;
int min_index = 0;
int min_count = INT_MAX;
uint64_t *l2_table;
ssize_t read;
/* l2 cache lookup */
for (i = 0; i < L2_CACHE_SIZE; i++) {
if (l2_offset == s->l2_cache_offsets[i]) {
if (++s->l2_cache_counts[i] == INT_MAX) {
for (j = 0; i < L2_CACHE_SIZE; j++) {
s->l2_cache_counts[j] >>= 1;
}
}
l2_table = s->l2_cache + (i << s->l2_bits);
tcmu_dbg("%s: l2 hit %"PRIx64" at index %d\n", __func__, *l2_table, i);
return l2_table;
}
}
/* not found, evict least used entry */
for (i = 0; i < L2_CACHE_SIZE; i++) {
if (s->l2_cache_counts[i] < min_count) {
min_count = s->l2_cache_counts[i];
min_index = i;
}
}
l2_table = s->l2_cache + (min_index << s->l2_bits);
read = pread(s->fd, l2_table, s->l2_size * sizeof(uint64_t), l2_offset);
if (read != s->l2_size * sizeof(uint64_t))
return NULL;
s->l2_cache_offsets[min_index] = l2_offset;
s->l2_cache_counts[min_index] = 1;
return l2_table;
}
static uint64_t qcow_cluster_alloc(struct qcow_state *s)
{
tcmu_dbg("%s\n", __func__);
return s->block_alloc(s, s->cluster_size);
}
/* qcow 1 simply grows the file as new clusters or L2 blocks are needed */
static uint64_t qcow_block_alloc(struct qcow_state *s, size_t size)
{
uint64_t offset;
off_t off;
off = lseek(s->fd, 0, SEEK_END);
if (off == -1)
return 0;
offset = (off + size - 1) & ~(size - 1);
if (ftruncate(s->fd, offset + size) == -1)
return 0;
return offset;
}
static uint64_t l2_table_alloc(struct qcow_state *s)
{
tcmu_dbg("%s\n", __func__);
return s->block_alloc(s, s->l2_size * sizeof(uint64_t));
}
static int l1_table_update(struct qcow_state *s, unsigned int l1_index, uint64_t l2_offset)
{
ssize_t ret;
tcmu_dbg("%s: setting L1[%u] to %"PRIx64"\n", __func__, l1_index, l2_offset);
s->l1_table[l1_index] = htobe64(l2_offset);
ret = pwrite(s->fd,
&s->l1_table[l1_index],
sizeof(uint64_t),
s->l1_table_offset + (l1_index * sizeof(uint64_t)));
if (ret != sizeof(uint64_t))
tcmu_err("%s: error, L1 writeback failed (%zd)\n", __func__, ret);
fdatasync(s->fd);
return ret;
}
/* refcount table */
static uint64_t get_refcount(unsigned int order, void *rcblock, size_t index)
{
switch (order) {
case 0:
return (((uint8_t *)rcblock)[index / 8] >> (index % 8)) & 0x1;
case 1:
return (((uint8_t *)rcblock)[index / 4] >> (2 * (index % 4))) & 0x3;
case 2:
return (((uint8_t *)rcblock)[index / 2] >> (4 * (index % 2))) & 0xf;
case 3:
return ((uint8_t *)rcblock)[index];
case 4:
return be16toh(((uint16_t *)rcblock)[index]);
case 5:
return be32toh(((uint32_t *)rcblock)[index]);
case 6:
return be64toh(((uint64_t *)rcblock)[index]);
default:
assert(0);
}
return 0; /* NOT REACHED */
}
static void set_refcount(unsigned int order, void *rcblock, size_t index, uint64_t value)
{
assert(!(value >> (1 << order)));
switch (order) {
case 0:
((uint8_t *)rcblock)[index / 8] &= ~(0x1 << (index % 8));
((uint8_t *)rcblock)[index / 8] |= value << (index % 8);
break;
case 1:
((uint8_t *)rcblock)[index / 4] &= ~(0x3 << (2 * (index % 4)));
((uint8_t *)rcblock)[index / 4] |= value << (2 * (index % 4));
break;
case 2:
((uint8_t *)rcblock)[index / 2] &= ~(0xf << (4 * (index % 2)));
((uint8_t *)rcblock)[index / 2] |= value << (4 * (index % 2));
break;
case 3:
((uint8_t *)rcblock)[index] = value;
break;
case 4:
((uint16_t *)rcblock)[index] = htobe16(value);
break;
case 5:
((uint32_t *)rcblock)[index] = htobe32(value);
break;
case 6:
((uint64_t *)rcblock)[index] = htobe64(value);
break;
default:
assert(0);
}
}
static void *rc_cache_lookup(struct qcow_state *s, uint64_t rc_offset)
{
int i, j;
int min_index = 0;
int min_count = INT_MAX;
void *rc_table;
ssize_t read;
/* rc cache lookup */
for (i = 0; i < RC_CACHE_SIZE; i++) {
if (rc_offset == s->rc_cache_offsets[i]) {
if (++s->rc_cache_counts[i] == INT_MAX) {
for (j = 0; i < RC_CACHE_SIZE; j++) {
s->rc_cache_counts[j] >>= 1;
}
}
rc_table = s->rc_cache + (i << s->cluster_bits);
return rc_table;
}
}
/* not found, evict least used entry */
for (i = 0; i < RC_CACHE_SIZE; i++) {
if (s->rc_cache_counts[i] < min_count) {
min_count = s->rc_cache_counts[i];
min_index = i;
}
}
rc_table = s->rc_cache + (min_index << s->cluster_bits);
read = pread(s->fd, rc_table, 1 << s->cluster_bits, rc_offset);
if (read != 1 << s->cluster_bits)
return NULL;
s->rc_cache_offsets[min_index] = rc_offset;
s->rc_cache_counts[min_index] = 1;
return rc_table;
}
static uint64_t qcow2_get_refcount(struct qcow_state *s, int64_t cluster_offset)
{
unsigned int refcount_bits;
uint64_t rc_index;
uint64_t refblock_offset;
uint64_t refblock_index;
void *refblock;
uint64_t rc;
refcount_bits = s->cluster_bits - s->refcount_order + 3;
rc_index = cluster_offset >> (s->cluster_bits + refcount_bits);
refblock_offset = be64toh(s->refcount_table[rc_index]);
if (!refblock_offset)
return 0;
refblock = rc_cache_lookup(s, refblock_offset);
if (!refblock)
return 0;
refblock_index = (cluster_offset >> s->cluster_bits) & ((1 << refcount_bits) - 1);
rc = get_refcount(s->refcount_order, refblock, refblock_index);
return rc;
}
static int rc_table_update(struct qcow_state *s, unsigned int rc_index, uint64_t refblock_offset)
{
ssize_t ret;
tcmu_dbg("%s: setting RC[%u] to %"PRIx64"\n", __func__, rc_index, refblock_offset);
s->refcount_table[rc_index] = htobe64(refblock_offset);
ret = pwrite(s->fd,
&s->refcount_table[rc_index],
sizeof(uint64_t),
s->refcount_table_offset + (rc_index * sizeof(uint64_t)));
if (ret != sizeof(uint64_t))
tcmu_err("%s: error, RC writeback failed (%zd)\n", __func__, ret);
fdatasync(s->fd);
return ret;
}
static int qcow2_set_refcount(struct qcow_state *s, uint64_t cluster_offset, uint64_t value)
{
unsigned int refcount_bits;
uint64_t rc_index;
uint64_t refblock_offset;
uint64_t refblock_index;
void *refblock;
ssize_t ret;
refcount_bits = s->cluster_bits - s->refcount_order + 3;
rc_index = cluster_offset >> (s->cluster_bits + refcount_bits);
refblock_offset = be64toh(s->refcount_table[rc_index]);
refblock_index = (cluster_offset >> s->cluster_bits) & ((1 << refcount_bits) - 1);
tcmu_dbg("%s: rc[%"PRIu64"][%"PRIu64"] = %"PRIx64"[%"PRIu64"] = %"PRIu64"\n",
__func__, rc_index, refblock_index, refblock_offset, refblock_index, value);
if (!refblock_offset) {
if (!(refblock_offset = qcow_cluster_alloc(s))) {
tcmu_err("refblock allocation failure\n");
return -1;
}
rc_table_update(s, rc_index, refblock_offset);
qcow2_set_refcount(s, refblock_offset, 1);
}
refblock = rc_cache_lookup(s, refblock_offset);
if (!refblock) {
tcmu_err("refblock cache failure\n");
return -1;
}
set_refcount(s->refcount_order, refblock, refblock_index, value);
/* for now this writes back the entire block */
ret = pwrite(s->fd, refblock, s->cluster_size, refblock_offset);
if (ret != s->cluster_size)
tcmu_err("%s: error, refblock writeback failed (%zd)\n", __func__, ret);
fdatasync(s->fd);
return ret;
}
/* qcow 2 uses the refcount table to find free clusters */
static uint64_t qcow2_block_alloc(struct qcow_state *s, size_t size)
{
uint64_t cluster;
int ret;
tcmu_dbg(" %s %zx\n", __func__, size);
/* all allocations for qcow2 should be of the same size */
assert(size == s->cluster_size);
cluster = s->first_free_cluster;
while (qcow2_get_refcount(s, cluster)) {
cluster += s->cluster_size;
}