summaryrefslogtreecommitdiffstats
path: root/kernel/loop_file_fmt_qcow_main.c
blob: 7c3e36028ff96b2b4f728bff8f2526e02b9e80bf (plain) (blame)
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
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * xloop_file_fmt_qcow.c
 *
 * QCOW file format driver for the xloop device module.
 *
 * Copyright (C) 2019 Manuel Bentele <development@manuel-bentele.de>
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/limits.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/bvec.h>
#include <linux/mutex.h>
#include <linux/uio.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <linux/zlib.h>

#include "loop_file_fmt.h"
#include "loop_file_fmt_qcow_main.h"
#include "loop_file_fmt_qcow_cache.h"
#include "loop_file_fmt_qcow_cluster.h"

static int __qcow_file_fmt_header_read(struct file *file,
	struct xloop_file_fmt_qcow_header *header)
{
	ssize_t len;
	loff_t offset;
	int ret = 0;

	/* read QCOW header */
	offset = 0;
	len = kernel_read(file, header, sizeof(*header), &offset);
	if (len < 0) {
		printk(KERN_ERR "xloop_file_fmt_qcow: could not read QCOW "
			"header");
		return len;
	}

	header->magic = be32_to_cpu(header->magic);
	header->version = be32_to_cpu(header->version);
	header->backing_file_offset = be64_to_cpu(header->backing_file_offset);
	header->backing_file_size = be32_to_cpu(header->backing_file_size);
	header->cluster_bits = be32_to_cpu(header->cluster_bits);
	header->size = be64_to_cpu(header->size);
	header->crypt_method = be32_to_cpu(header->crypt_method);
	header->l1_size = be32_to_cpu(header->l1_size);
	header->l1_table_offset = be64_to_cpu(header->l1_table_offset);
	header->refcount_table_offset =
		be64_to_cpu(header->refcount_table_offset);
	header->refcount_table_clusters =
		be32_to_cpu(header->refcount_table_clusters);
	header->nb_snapshots = be32_to_cpu(header->nb_snapshots);
	header->snapshots_offset = be64_to_cpu(header->snapshots_offset);

	/* check QCOW file format and header version */
	if (header->magic != QCOW_MAGIC) {
		printk(KERN_ERR "xloop_file_fmt_qcow: image is not in QCOW "
			"format");
		return -EINVAL;
	}

	if (header->version < 2 || header->version > 3) {
		printk(KERN_ERR "xloop_file_fmt_qcow: unsupported QCOW version "
			"%d", header->version);
		return -ENOTSUPP;
	}

	/* initialize version 3 header fields */
	if (header->version == 2) {
		header->incompatible_features =  0;
		header->compatible_features   =  0;
		header->autoclear_features    =  0;
		header->refcount_order        =  4;
		header->header_length         = 72;
	} else {
		header->incompatible_features =
			be64_to_cpu(header->incompatible_features);
		header->compatible_features =
			be64_to_cpu(header->compatible_features);
		header->autoclear_features =
			be64_to_cpu(header->autoclear_features);
		header->refcount_order = be32_to_cpu(header->refcount_order);
		header->header_length = be32_to_cpu(header->header_length);

		if (header->header_length < 104) {
			printk(KERN_ERR "xloop_file_fmt_qcow: QCOW header too "
				"short");
			return -EINVAL;
		}
	}

	return ret;
}

static int __qcow_file_fmt_validate_table(struct xloop_file_fmt *xlo_fmt,
	u64 offset, u64 entries, size_t entry_len, s64 max_size_bytes,
	const char *table_name)
{
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;

	if (entries > max_size_bytes / entry_len) {
		printk(KERN_INFO "xloop_file_fmt_qcow: %s too large",
			table_name);
		return -EFBIG;
	}

	/* Use signed S64_MAX as the maximum even for u64 header fields,
	 * because values will be passed to qemu functions taking s64. */
	if ((S64_MAX - entries * entry_len < offset) || (
		xloop_file_fmt_qcow_offset_into_cluster(qcow_data, offset) != 0)
	) {
		printk(KERN_INFO "xloop_file_fmt_qcow: %s offset invalid",
			table_name);
		return -EINVAL;
	}

	return 0;
}

static inline loff_t __qcow_file_fmt_rq_get_pos(struct xloop_file_fmt *xlo_fmt,
						struct request *rq)
{
	struct xloop_device *xlo = xloop_file_fmt_get_xlo(xlo_fmt);
	return ((loff_t) blk_rq_pos(rq) << 9) + xlo->xlo_offset;
}

static int __qcow_file_fmt_compression_init(struct xloop_file_fmt *xlo_fmt)
{
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
	int ret = 0;

	qcow_data->strm = kzalloc(sizeof(*qcow_data->strm), GFP_KERNEL);
	if (!qcow_data->strm) {
		ret = -ENOMEM;
		goto out;
	}

	qcow_data->strm->workspace = vzalloc(zlib_inflate_workspacesize());
	if (!qcow_data->strm->workspace) {
		ret = -ENOMEM;
		goto out_free_strm;
	}

	qcow_data->cmp_last_coffset = ULLONG_MAX;
	qcow_data->cmp_out_buf = vmalloc(qcow_data->cluster_size);
	if (!qcow_data->cmp_out_buf) {
		ret = -ENOMEM;
		goto out_free_workspace;
	}

	return ret;

out_free_workspace:
	vfree(qcow_data->strm->workspace);
out_free_strm:
	kfree(qcow_data->strm);
out:
	return ret;
}

static void __qcow_file_fmt_compression_exit(struct xloop_file_fmt *xlo_fmt)
{
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;

	vfree(qcow_data->strm->workspace);
	kfree(qcow_data->strm);
	vfree(qcow_data->cmp_out_buf);
}

#ifdef CONFIG_DEBUG_FS
static void __qcow_file_fmt_header_to_buf(struct xloop_file_fmt *xlo_fmt,
	const struct xloop_file_fmt_qcow_header *header)
{
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
	char *header_buf = qcow_data->dbgfs_file_qcow_header_buf;
	ssize_t len = 0;

	len += sprintf(header_buf + len, "magic: %d\n",
		header->magic);
	len += sprintf(header_buf + len, "version: %d\n",
		header->version);
	len += sprintf(header_buf + len, "backing_file_offset: %lld\n",
		header->backing_file_offset);
	len += sprintf(header_buf + len, "backing_file_size: %d\n",
		header->backing_file_size);
	len += sprintf(header_buf + len, "cluster_bits: %d\n",
		header->cluster_bits);
	len += sprintf(header_buf + len, "size: %lld\n",
		header->size);
	len += sprintf(header_buf + len, "crypt_method: %d\n",
		header->crypt_method);
	len += sprintf(header_buf + len, "l1_size: %d\n",
		header->l1_size);
	len += sprintf(header_buf + len, "l1_table_offset: %lld\n",
		header->l1_table_offset);
	len += sprintf(header_buf + len, "refcount_table_offset: %lld\n",
		header->refcount_table_offset);
	len += sprintf(header_buf + len, "refcount_table_clusters: %d\n",
		header->refcount_table_clusters);
	len += sprintf(header_buf + len, "nb_snapshots: %d\n",
		header->nb_snapshots);
	len += sprintf(header_buf + len, "snapshots_offset: %lld\n",
		header->snapshots_offset);

	if (header->version == 3) {
		len += sprintf(header_buf + len,
			"incompatible_features: %lld\n",
			header->incompatible_features);
		len += sprintf(header_buf + len,
			"compatible_features: %lld\n",
			header->compatible_features);
		len += sprintf(header_buf + len,
			"autoclear_features: %lld\n",
			header->autoclear_features);
		len += sprintf(header_buf + len,
			"refcount_order: %d\n",
			header->refcount_order);
		len += sprintf(header_buf + len,
			"header_length: %d\n",
			header->header_length);
	}

	ASSERT(len < QCOW_HEADER_BUF_LEN);
}

static ssize_t __qcow_file_fmt_dbgfs_hdr_read(struct file *file,
	char __user *buf, size_t size, loff_t *ppos)
{
	struct xloop_file_fmt *xlo_fmt = file->private_data;
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
	char *header_buf = qcow_data->dbgfs_file_qcow_header_buf;

	return simple_read_from_buffer(buf, size, ppos, header_buf,
		strlen(header_buf));
}

static const struct file_operations qcow_file_fmt_dbgfs_hdr_fops = {
	.open = simple_open,
	.read = __qcow_file_fmt_dbgfs_hdr_read
};

static ssize_t __qcow_file_fmt_dbgfs_ofs_read(struct file *file,
	char __user *buf, size_t size, loff_t *ppos)
{
	struct xloop_file_fmt *xlo_fmt = file->private_data;
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
	unsigned int cur_bytes = 1;
	u64 offset = 0;
	u64 cluster_offset = 0;
	s64 offset_in_cluster = 0;
	ssize_t len = 0;
	int ret = 0;

	/* read the share debugfs offset */
	ret = mutex_lock_interruptible(&qcow_data->dbgfs_qcow_offset_mutex);
	if (ret)
		return ret;

	offset = qcow_data->dbgfs_qcow_offset;
	mutex_unlock(&qcow_data->dbgfs_qcow_offset_mutex);

	/* calculate and print the cluster offset */
	ret = xloop_file_fmt_qcow_cluster_get_offset(xlo_fmt,
		offset, &cur_bytes, &cluster_offset);
	if (ret < 0)
		return -EINVAL;

	offset_in_cluster = xloop_file_fmt_qcow_offset_into_cluster(qcow_data,
		offset);

	len = sprintf(qcow_data->dbgfs_file_qcow_cluster_buf,
		"offset: %lld\ncluster_offset: %lld\noffset_in_cluster: %lld\n",
		offset, cluster_offset, offset_in_cluster);

	ASSERT(len < QCOW_CLUSTER_BUF_LEN);

	return simple_read_from_buffer(buf, size, ppos,
		qcow_data->dbgfs_file_qcow_cluster_buf, len);
}

static ssize_t __qcow_file_fmt_dbgfs_ofs_write(struct file *file,
	const char __user *buf, size_t size, loff_t *ppos)
{
	struct xloop_file_fmt *xlo_fmt = file->private_data;
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
	ssize_t len = 0;
	int ret = 0;

	if (*ppos > QCOW_OFFSET_BUF_LEN || size > QCOW_OFFSET_BUF_LEN)
		return -EINVAL;

	len = simple_write_to_buffer(qcow_data->dbgfs_file_qcow_offset_buf,
		QCOW_OFFSET_BUF_LEN, ppos, buf, size);
	if (len < 0)
		return len;

	qcow_data->dbgfs_file_qcow_offset_buf[len] = '\0';

	ret = mutex_lock_interruptible(&qcow_data->dbgfs_qcow_offset_mutex);
	if (ret)
		return ret;

	ret = kstrtou64(qcow_data->dbgfs_file_qcow_offset_buf, 10,
		&qcow_data->dbgfs_qcow_offset);
	if (ret < 0)
		goto out;

	ret = len;
out:
	mutex_unlock(&qcow_data->dbgfs_qcow_offset_mutex);
	return ret;
}

static const struct file_operations qcow_file_fmt_dbgfs_ofs_fops = {
	.open = simple_open,
	.read = __qcow_file_fmt_dbgfs_ofs_read,
	.write = __qcow_file_fmt_dbgfs_ofs_write
};

static int __qcow_file_fmt_dbgfs_init(struct xloop_file_fmt *xlo_fmt)
{
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
	struct xloop_device *xlo = xloop_file_fmt_get_xlo(xlo_fmt);
	int ret = 0;

	qcow_data->dbgfs_dir = debugfs_create_dir("QCOW", xlo->xlo_dbgfs_dir);
	if (IS_ERR_OR_NULL(qcow_data->dbgfs_dir)) {
		ret = -ENODEV;
		goto out;
	}

	qcow_data->dbgfs_file_qcow_header = debugfs_create_file("header",
		S_IRUGO, qcow_data->dbgfs_dir, xlo_fmt,
		&qcow_file_fmt_dbgfs_hdr_fops);
	if (IS_ERR_OR_NULL(qcow_data->dbgfs_file_qcow_header)) {
		ret = -ENODEV;
		goto out_free_dbgfs_dir;
	}

	qcow_data->dbgfs_file_qcow_offset = debugfs_create_file("offset",
		S_IRUGO | S_IWUSR, qcow_data->dbgfs_dir, xlo_fmt,
		&qcow_file_fmt_dbgfs_ofs_fops);
	if (IS_ERR_OR_NULL(qcow_data->dbgfs_file_qcow_offset)) {
		qcow_data->dbgfs_file_qcow_offset = NULL;
		ret = -ENODEV;
		goto out_free_dbgfs_hdr;
	}

	qcow_data->dbgfs_qcow_offset = 0;
	mutex_init(&qcow_data->dbgfs_qcow_offset_mutex);

	return ret;

out_free_dbgfs_hdr:
	debugfs_remove(qcow_data->dbgfs_file_qcow_header);
	qcow_data->dbgfs_file_qcow_header = NULL;
out_free_dbgfs_dir:
	debugfs_remove(qcow_data->dbgfs_dir);
	qcow_data->dbgfs_dir = NULL;
out:
	return ret;
}

static void __qcow_file_fmt_dbgfs_exit(struct xloop_file_fmt *xlo_fmt)
{
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;

	if (qcow_data->dbgfs_file_qcow_offset)
		debugfs_remove(qcow_data->dbgfs_file_qcow_offset);

	mutex_destroy(&qcow_data->dbgfs_qcow_offset_mutex);

	if (qcow_data->dbgfs_file_qcow_header)
		debugfs_remove(qcow_data->dbgfs_file_qcow_header);

	if (qcow_data->dbgfs_dir)
		debugfs_remove(qcow_data->dbgfs_dir);
}
#endif

static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt)
{
	struct xloop_file_fmt_qcow_data *qcow_data;
	struct xloop_device *xlo = xloop_file_fmt_get_xlo(xlo_fmt);
	struct xloop_file_fmt_qcow_header header;
	u64 l1_vm_state_index;
	u64 l2_cache_size;
	u64 l2_cache_entry_size;
	ssize_t len;
	unsigned int i;
	int ret = 0;

	/* allocate memory for saving QCOW file format data */
	qcow_data = kzalloc(sizeof(*qcow_data), GFP_KERNEL);
	if (!qcow_data)
		return -ENOMEM;

	xlo_fmt->private_data = qcow_data;

	/* read the QCOW file header */
	ret = __qcow_file_fmt_header_read(xlo->xlo_backing_file, &header);
	if (ret)
		goto free_qcow_data;

	/* save information of the header fields in human readable format in
	 * a file buffer to access it with debugfs */
#ifdef CONFIG_DEBUG_FS
	__qcow_file_fmt_header_to_buf(xlo_fmt, &header);
#endif

	qcow_data->qcow_version = header.version;

	/* Initialise cluster size */
	if (header.cluster_bits < QCOW_MIN_CLUSTER_BITS
		|| header.cluster_bits > QCOW_MAX_CLUSTER_BITS) {
		printk(KERN_ERR "xloop_file_fmt_qcow: unsupported cluster "
			"size: 2^%d", header.cluster_bits);
		ret = -EINVAL;
		goto free_qcow_data;
	}

	qcow_data->cluster_bits = header.cluster_bits;
	qcow_data->cluster_size = 1 << qcow_data->cluster_bits;
	qcow_data->cluster_sectors = 1 <<
		(qcow_data->cluster_bits - SECTOR_SHIFT);

	if (header.header_length > qcow_data->cluster_size) {
		printk(KERN_ERR "xloop_file_fmt_qcow: QCOW header exceeds "
			"cluster size");
		ret = -EINVAL;
		goto free_qcow_data;
	}

	if (header.backing_file_offset > qcow_data->cluster_size) {
		printk(KERN_ERR "xloop_file_fmt_qcow: invalid backing file "
			"offset");
		ret = -EINVAL;
		goto free_qcow_data;
	}

	if (header.backing_file_offset) {
		printk(KERN_ERR "xloop_file_fmt_qcow: backing file support not "
			"available");
		ret = -ENOTSUPP;
		goto free_qcow_data;
	}

	/* handle feature bits */
	qcow_data->incompatible_features = header.incompatible_features;
	qcow_data->compatible_features = header.compatible_features;
	qcow_data->autoclear_features = header.autoclear_features;

	if (qcow_data->incompatible_features & QCOW_INCOMPAT_DIRTY) {
		printk(KERN_ERR "xloop_file_fmt_qcow: image contains "
			"inconsistent refcounts");
		ret = -EACCES;
		goto free_qcow_data;
	}

	if (qcow_data->incompatible_features & QCOW_INCOMPAT_CORRUPT) {
		printk(KERN_ERR "xloop_file_fmt_qcow: image is corrupt; cannot "
			"be opened read/write");
		ret = -EACCES;
		goto free_qcow_data;
	}

	if (qcow_data->incompatible_features & QCOW_INCOMPAT_DATA_FILE) {
		printk(KERN_ERR "xloop_file_fmt_qcow: clusters in the external "
			"data file are not refcounted");
		ret = -EACCES;
		goto free_qcow_data;
	}

	/* Check support for various header values */
	if (header.refcount_order > 6) {
		printk(KERN_ERR "xloop_file_fmt_qcow: reference count entry "
			"width too large; may not exceed 64 bits");
		ret = -EINVAL;
		goto free_qcow_data;
	}
	qcow_data->refcount_order = header.refcount_order;
	qcow_data->refcount_bits = 1 << qcow_data->refcount_order;
	qcow_data->refcount_max = U64_C(1) << (qcow_data->refcount_bits - 1);
	qcow_data->refcount_max += qcow_data->refcount_max - 1;

	qcow_data->crypt_method_header = header.crypt_method;
	if (qcow_data->crypt_method_header) {
		printk(KERN_ERR "xloop_file_fmt_qcow: encryption support not "
			"available");
		ret = -ENOTSUPP;
		goto free_qcow_data;
	}

	/* L2 is always one cluster */
	qcow_data->l2_bits = qcow_data->cluster_bits - 3;
	qcow_data->l2_size = 1 << qcow_data->l2_bits;
	/* 2^(qcow_data->refcount_order - 3) is the refcount width in bytes */
	qcow_data->refcount_block_bits = qcow_data->cluster_bits -
		(qcow_data->refcount_order - 3);
	qcow_data->refcount_block_size = 1 << qcow_data->refcount_block_bits;
	qcow_data->size = header.size;
	qcow_data->csize_shift = (62 - (qcow_data->cluster_bits - 8));
	qcow_data->csize_mask = (1 << (qcow_data->cluster_bits - 8)) - 1;
	qcow_data->cluster_offset_mask = (1LL << qcow_data->csize_shift) - 1;

	qcow_data->refcount_table_offset = header.refcount_table_offset;
	qcow_data->refcount_table_size = header.refcount_table_clusters <<
		(qcow_data->cluster_bits - 3);

	if (header.refcount_table_clusters == 0) {
		printk(KERN_ERR "xloop_file_fmt_qcow: image does not contain a "
			"reference count table");
		ret = -EINVAL;
		goto free_qcow_data;
	}

	ret = __qcow_file_fmt_validate_table(xlo_fmt,
		qcow_data->refcount_table_offset,
		header.refcount_table_clusters, qcow_data->cluster_size,
		QCOW_MAX_REFTABLE_SIZE, "Reference count table");
	if (ret < 0) {
		goto free_qcow_data;
	}

	/* The total size in bytes of the snapshot table is checked in
	 * qcow2_read_snapshots() because the size of each snapshot is
	 * variable and we don't know it yet.
	 * Here we only check the offset and number of snapshots. */
	ret = __qcow_file_fmt_validate_table(xlo_fmt, header.snapshots_offset,
		header.nb_snapshots,
		sizeof(struct xloop_file_fmt_qcow_snapshot_header),
		sizeof(struct xloop_file_fmt_qcow_snapshot_header) *
		QCOW_MAX_SNAPSHOTS, "Snapshot table");
	if (ret < 0) {
		goto free_qcow_data;
	}

	/* read the level 1 table */
	ret = __qcow_file_fmt_validate_table(xlo_fmt, header.l1_table_offset,
		header.l1_size, sizeof(u64), QCOW_MAX_L1_SIZE,
		"Active L1 table");
	if (ret < 0) {
		goto free_qcow_data;
	}
	qcow_data->l1_size = header.l1_size;
	qcow_data->l1_table_offset = header.l1_table_offset;

	l1_vm_state_index = xloop_file_fmt_qcow_size_to_l1(qcow_data,
		header.size);
	if (l1_vm_state_index > INT_MAX) {
		printk(KERN_ERR "xloop_file_fmt_qcow: image is too big");
		ret = -EFBIG;
		goto free_qcow_data;
	}
	qcow_data->l1_vm_state_index = l1_vm_state_index;

	/* the L1 table must contain at least enough entries to put header.size
	 * bytes */
	if (qcow_data->l1_size < qcow_data->l1_vm_state_index) {
		printk(KERN_ERR "xloop_file_fmt_qcow: L1 table is too small");
		ret = -EINVAL;
		goto free_qcow_data;
	}

	if (qcow_data->l1_size > 0) {
		qcow_data->l1_table = vzalloc(round_up(qcow_data->l1_size *
			sizeof(u64), 512));
		if (qcow_data->l1_table == NULL) {
			printk(KERN_ERR "xloop_file_fmt_qcow: could not "
				"allocate L1 table");
			ret = -ENOMEM;
			goto free_qcow_data;
		}
		len = kernel_read(xlo->xlo_backing_file, qcow_data->l1_table,
			qcow_data->l1_size * sizeof(u64),
			&qcow_data->l1_table_offset);
		if (len < 0) {
			printk(KERN_ERR "xloop_file_fmt_qcow: could not read L1 "
				"table");
			ret = len;
			goto free_l1_table;
		}
		for (i = 0; i < qcow_data->l1_size; i++) {
			qcow_data->l1_table[i] =
				be64_to_cpu(qcow_data->l1_table[i]);
		}
	}

	/* Internal snapshots */
	qcow_data->snapshots_offset = header.snapshots_offset;
	qcow_data->nb_snapshots = header.nb_snapshots;

	if (qcow_data->nb_snapshots > 0) {
		printk(KERN_ERR "xloop_file_fmt_qcow: snapshots support not "
			"available");
		ret = -ENOTSUPP;
		goto free_l1_table;
	}


	/* create cache for L2 */
	l2_cache_size =  qcow_data->size / (qcow_data->cluster_size / 8);
	l2_cache_entry_size = min(qcow_data->cluster_size, (int)4096);

	/* limit the L2 size to maximum QCOW_DEFAULT_L2_CACHE_MAX_SIZE */
	l2_cache_size = min(l2_cache_size, (u64)QCOW_DEFAULT_L2_CACHE_MAX_SIZE);

	/* calculate the number of cache tables */
	l2_cache_size /= l2_cache_entry_size;
	if (l2_cache_size < QCOW_MIN_L2_CACHE_SIZE) {
		l2_cache_size = QCOW_MIN_L2_CACHE_SIZE;
	}

	if (l2_cache_size > INT_MAX) {
		printk(KERN_ERR "xloop_file_fmt_qcow: L2 cache size too big");
		ret = -EINVAL;
		goto free_l1_table;
	}

	qcow_data->l2_slice_size = l2_cache_entry_size / sizeof(u64);

	qcow_data->l2_table_cache = xloop_file_fmt_qcow_cache_create(xlo_fmt,
		l2_cache_size, l2_cache_entry_size);
	if (!qcow_data->l2_table_cache) {
		ret = -ENOMEM;
		goto free_l1_table;
	}

	/* initialize compression support */
	ret = __qcow_file_fmt_compression_init(xlo_fmt);
	if (ret < 0)
		goto free_l2_cache;

	/* initialize debugfs entries */
#ifdef CONFIG_DEBUG_FS
	ret = __qcow_file_fmt_dbgfs_init(xlo_fmt);
	if (ret < 0)
		goto free_l2_cache;
#endif

	return ret;

free_l2_cache:
	xloop_file_fmt_qcow_cache_destroy(xlo_fmt);
free_l1_table:
	vfree(qcow_data->l1_table);
free_qcow_data:
	kfree(qcow_data);
	xlo_fmt->private_data = NULL;
	return ret;
}

static void qcow_file_fmt_exit(struct xloop_file_fmt *xlo_fmt)
{
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;

#ifdef CONFIG_DEBUG_FS
	__qcow_file_fmt_dbgfs_exit(xlo_fmt);
#endif

	__qcow_file_fmt_compression_exit(xlo_fmt);

	if (qcow_data->l1_table) {
		vfree(qcow_data->l1_table);
	}

	if (qcow_data->l2_table_cache) {
		xloop_file_fmt_qcow_cache_destroy(xlo_fmt);
	}

	if (qcow_data) {
		kfree(qcow_data);
		xlo_fmt->private_data = NULL;
	}
}

static ssize_t __qcow_file_fmt_buffer_decompress(struct xloop_file_fmt *xlo_fmt,
						 void *dest,
						 size_t dest_size,
						 const void *src,
						 size_t src_size)
{
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
	int ret = 0;

	qcow_data->strm->avail_in = src_size;
	qcow_data->strm->next_in = (void *) src;
	qcow_data->strm->avail_out = dest_size;
	qcow_data->strm->next_out = dest;

	ret = zlib_inflateInit2(qcow_data->strm, -12);
	if (ret != Z_OK) {
		return -1;
	}

	ret = zlib_inflate(qcow_data->strm, Z_FINISH);
	if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR)
		|| qcow_data->strm->avail_out != 0) {
		/* We approve Z_BUF_ERROR because we need @dest buffer to be
		 * filled, but @src buffer may be processed partly (because in
		 * qcow2 we know size of compressed data with precision of one
		 * sector) */
		ret = -1;
	} else {
		ret = 0;
	}
	return ret;
}

static int __qcow_file_fmt_read_compressed(struct xloop_file_fmt *xlo_fmt,
					   struct bio_vec *bvec,
					   u64 file_cluster_offset,
					   u64 offset,
					   u64 bytes,
					   u64 bytes_done)
{
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
	struct xloop_device *xlo = xloop_file_fmt_get_xlo(xlo_fmt);
	int ret = 0, csize, nb_csectors;
	u64 coffset;
	u8 *in_buf = NULL;
	ssize_t len;
	void *data;
	unsigned long irq_flags;
	int offset_in_cluster = xloop_file_fmt_qcow_offset_into_cluster(
		qcow_data, offset);

	coffset = file_cluster_offset & qcow_data->cluster_offset_mask;
	nb_csectors = ((file_cluster_offset >> qcow_data->csize_shift) &
		qcow_data->csize_mask) + 1;
	csize = nb_csectors * QCOW_COMPRESSED_SECTOR_SIZE -
		(coffset & ~QCOW_COMPRESSED_SECTOR_MASK);


	if (qcow_data->cmp_last_coffset != coffset) {
		in_buf = vmalloc(csize);
		if (!in_buf) {
			qcow_data->cmp_last_coffset = ULLONG_MAX;
			return -ENOMEM;
		}
		qcow_data->cmp_last_coffset = coffset;
		len = kernel_read(xlo->xlo_backing_file, in_buf, csize, &coffset);
		if (len < 0) {
			qcow_data->cmp_last_coffset = ULLONG_MAX;
			ret = len;
			goto out_free_in_buf;
		}

		if (__qcow_file_fmt_buffer_decompress(xlo_fmt, qcow_data->cmp_out_buf,
			qcow_data->cluster_size, in_buf, csize) < 0) {
			qcow_data->cmp_last_coffset = ULLONG_MAX;
			ret = -EIO;
			goto out_free_in_buf;
		}
	}

	ASSERT(bytes <= bvec->bv_len);
	data = bvec_kmap_irq(bvec, &irq_flags) + bytes_done;
	memcpy(data, qcow_data->cmp_out_buf + offset_in_cluster, bytes);
	flush_dcache_page(bvec->bv_page);
	bvec_kunmap_irq(data, &irq_flags);

out_free_in_buf:
	vfree(in_buf);

	return ret;
}

static int __qcow_file_fmt_read_bvec(struct xloop_file_fmt *xlo_fmt,
				     struct bio_vec *bvec,
				     loff_t *ppos)
{
	struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
	struct xloop_device *xlo = xloop_file_fmt_get_xlo(xlo_fmt);
	int offset_in_cluster;
	int ret;
	unsigned int cur_bytes; /* number of bytes in current iteration */
	u64 bytes;
	u64 cluster_offset = 0;
	u64 bytes_done = 0;
	void *data;
	unsigned long irq_flags;
	ssize_t len;
	loff_t pos_read;

	bytes = bvec->bv_len;

	while (bytes != 0) {

		/* prepare next request */
		cur_bytes = bytes;

		ret = xloop_file_fmt_qcow_cluster_get_offset(xlo_fmt, *ppos,
			&cur_bytes, &cluster_offset);
		if (ret < 0) {
			goto fail;
		}

		offset_in_cluster = xloop_file_fmt_qcow_offset_into_cluster(
			qcow_data, *ppos);

		switch (ret) {
		case QCOW_CLUSTER_UNALLOCATED:
		case QCOW_CLUSTER_ZERO_PLAIN:
		case QCOW_CLUSTER_ZERO_ALLOC:
			data = bvec_kmap_irq(bvec, &irq_flags) + bytes_done;
			memset(data, 0, cur_bytes);
			flush_dcache_page(bvec->bv_page);
			bvec_kunmap_irq(data, &irq_flags);
			break;

		case QCOW_CLUSTER_COMPRESSED:
			ret = __qcow_file_fmt_read_compressed(xlo_fmt, bvec,
				cluster_offset, *ppos, cur_bytes, bytes_done);
			if (ret < 0) {
				goto fail;
			}

			break;

		case QCOW_CLUSTER_NORMAL:
			if ((cluster_offset & 511) != 0) {
				ret = -EIO;
				goto fail;
			}

			pos_read = cluster_offset + offset_in_cluster;

			data = bvec_kmap_irq(bvec, &irq_flags) + bytes_done;
			len = kernel_read(xlo->xlo_backing_file, data, cur_bytes,
				&pos_read);
			flush_dcache_page(bvec->bv_page);
			bvec_kunmap_irq(data, &irq_flags);

			if (len < 0)
				return len;

			break;

		default:
			ret = -EIO;
			goto fail;
		}

		bytes -= cur_bytes;
		*ppos += cur_bytes;
		bytes_done += cur_bytes;
	}

	ret = 0;

fail:
	return ret;
}

static int qcow_file_fmt_read(struct xloop_file_fmt *xlo_fmt,
			      struct request *rq)
{
	struct bio_vec bvec;
	struct req_iterator iter;
	loff_t pos;
	int ret = 0;

	pos = __qcow_file_fmt_rq_get_pos(xlo_fmt, rq);

	rq_for_each_segment(bvec, rq, iter) {
		ret = __qcow_file_fmt_read_bvec(xlo_fmt, &bvec, &pos);
		if (ret)
			return ret;

		cond_resched();
	}

	return ret;
}

static loff_t qcow_file_fmt_sector_size(struct xloop_file_fmt *xlo_fmt,
				struct file *file, loff_t offset, loff_t sizelimit)
{
	struct xloop_file_fmt_qcow_header header;
	loff_t xloopsize;
	int ret;

	/* temporary read the QCOW file header of other QCOW image file */
	ret = __qcow_file_fmt_header_read(file, &header);
	if (ret)
		return 0;

	/* compute xloopsize in bytes */
	xloopsize = header.size;
	if (offset > 0)
		xloopsize -= offset;
	/* offset is beyond i_size, weird but possible */
	if (xloopsize < 0)
		return 0;

	if (sizelimit > 0 && sizelimit < xloopsize)
		xloopsize = sizelimit;
	/*
	 * Unfortunately, if we want to do I/O on the device,
	 * the number of 512-byte sectors has to fit into a sector_t.
	 */
	return xloopsize >> 9;
}

static struct xloop_file_fmt_ops qcow_file_fmt_ops = {
	.init = qcow_file_fmt_init,
	.exit = qcow_file_fmt_exit,
	.read = qcow_file_fmt_read,
	.write = NULL,
	.read_aio = NULL,
	.write_aio = NULL,
	.write_zeros = NULL,
	.discard = NULL,
	.flush = NULL,
	.sector_size = qcow_file_fmt_sector_size,
};

static struct xloop_file_fmt_driver qcow_file_fmt_driver = {
	.name = "QCOW",
	.file_fmt_type = XLO_FILE_FMT_QCOW,
	.ops = &qcow_file_fmt_ops,
	.owner = THIS_MODULE,
};

static int __init xloop_file_fmt_qcow_init(void)
{
	printk(KERN_INFO "xloop_file_fmt_qcow: init xloop device QCOW file "
		"format driver");
	return xloop_file_fmt_register_driver(&qcow_file_fmt_driver);
}

static void __exit xloop_file_fmt_qcow_exit(void)
{
	printk(KERN_INFO "xloop_file_fmt_qcow: exit xloop device QCOW file "
		"format driver");
	xloop_file_fmt_unregister_driver(&qcow_file_fmt_driver);
}

module_init(xloop_file_fmt_qcow_init);
module_exit(xloop_file_fmt_qcow_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Manuel Bentele <development@manuel-bentele.de>");
MODULE_DESCRIPTION("xloop device QCOW file format driver");
MODULE_SOFTDEP("pre: xloop");