]>
Commit | Line | Data |
---|---|---|
f41388e0 AS |
1 | /* |
2 | * Compress filter block driver | |
3 | * | |
4 | * Copyright (c) 2019 Virtuozzo International GmbH | |
5 | * | |
6 | * Author: | |
7 | * Andrey Shinkevich <andrey.shinkevich@virtuozzo.com> | |
8 | * (based on block/copy-on-read.c by Max Reitz) | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License as | |
12 | * published by the Free Software Foundation; either version 2 or | |
13 | * (at your option) any later version of the License. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
22 | */ | |
23 | ||
24 | #include "qemu/osdep.h" | |
e2c1c34f | 25 | #include "block/block-io.h" |
f41388e0 AS |
26 | #include "block/block_int.h" |
27 | #include "qemu/module.h" | |
28 | #include "qapi/error.h" | |
29 | ||
30 | ||
31 | static int compress_open(BlockDriverState *bs, QDict *options, int flags, | |
32 | Error **errp) | |
33 | { | |
83930780 VSO |
34 | int ret = bdrv_open_file_child(NULL, options, "file", bs, errp); |
35 | if (ret < 0) { | |
36 | return ret; | |
f41388e0 AS |
37 | } |
38 | ||
39 | if (!bs->file->bs->drv || !block_driver_can_compress(bs->file->bs->drv)) { | |
40 | error_setg(errp, | |
41 | "Compression is not supported for underlying format: %s", | |
42 | bdrv_get_format_name(bs->file->bs) ?: "(no format)"); | |
43 | ||
44 | return -ENOTSUP; | |
45 | } | |
46 | ||
47 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED | | |
48 | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); | |
49 | ||
50 | bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | | |
51 | ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) & | |
52 | bs->file->bs->supported_zero_flags); | |
53 | ||
54 | return 0; | |
55 | } | |
56 | ||
57 | ||
8ab8140a KW |
58 | static int64_t coroutine_fn GRAPH_RDLOCK |
59 | compress_co_getlength(BlockDriverState *bs) | |
f41388e0 | 60 | { |
c86422c5 | 61 | return bdrv_co_getlength(bs->file->bs); |
f41388e0 AS |
62 | } |
63 | ||
64 | ||
b9b10c35 KW |
65 | static int coroutine_fn GRAPH_RDLOCK |
66 | compress_co_preadv_part(BlockDriverState *bs, int64_t offset, int64_t bytes, | |
67 | QEMUIOVector *qiov, size_t qiov_offset, | |
68 | BdrvRequestFlags flags) | |
f41388e0 AS |
69 | { |
70 | return bdrv_co_preadv_part(bs->file, offset, bytes, qiov, qiov_offset, | |
71 | flags); | |
72 | } | |
73 | ||
74 | ||
b9b10c35 KW |
75 | static int coroutine_fn GRAPH_RDLOCK |
76 | compress_co_pwritev_part(BlockDriverState *bs, int64_t offset, int64_t bytes, | |
77 | QEMUIOVector *qiov, size_t qiov_offset, | |
78 | BdrvRequestFlags flags) | |
f41388e0 AS |
79 | { |
80 | return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset, | |
81 | flags | BDRV_REQ_WRITE_COMPRESSED); | |
82 | } | |
83 | ||
84 | ||
abaf8b75 KW |
85 | static int coroutine_fn GRAPH_RDLOCK |
86 | compress_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, | |
87 | BdrvRequestFlags flags) | |
f41388e0 AS |
88 | { |
89 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); | |
90 | } | |
91 | ||
92 | ||
9a5a1c62 EGE |
93 | static int coroutine_fn GRAPH_RDLOCK |
94 | compress_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes) | |
f41388e0 AS |
95 | { |
96 | return bdrv_co_pdiscard(bs->file, offset, bytes); | |
97 | } | |
98 | ||
99 | ||
100 | static void compress_refresh_limits(BlockDriverState *bs, Error **errp) | |
101 | { | |
102 | BlockDriverInfo bdi; | |
103 | int ret; | |
104 | ||
105 | if (!bs->file) { | |
106 | return; | |
107 | } | |
108 | ||
109 | ret = bdrv_get_info(bs->file->bs, &bdi); | |
110 | if (ret < 0 || bdi.cluster_size == 0) { | |
111 | return; | |
112 | } | |
113 | ||
114 | bs->bl.request_alignment = bdi.cluster_size; | |
115 | } | |
116 | ||
117 | ||
79a292e5 | 118 | static void coroutine_fn GRAPH_RDLOCK |
2531b390 | 119 | compress_co_eject(BlockDriverState *bs, bool eject_flag) |
f41388e0 | 120 | { |
2531b390 | 121 | bdrv_co_eject(bs->file->bs, eject_flag); |
f41388e0 AS |
122 | } |
123 | ||
124 | ||
79a292e5 | 125 | static void coroutine_fn GRAPH_RDLOCK |
2c75261c | 126 | compress_co_lock_medium(BlockDriverState *bs, bool locked) |
f41388e0 | 127 | { |
2c75261c | 128 | bdrv_co_lock_medium(bs->file->bs, locked); |
f41388e0 AS |
129 | } |
130 | ||
131 | ||
f41388e0 AS |
132 | static BlockDriver bdrv_compress = { |
133 | .format_name = "compress", | |
134 | ||
135 | .bdrv_open = compress_open, | |
69dca43d | 136 | .bdrv_child_perm = bdrv_default_perms, |
f41388e0 | 137 | |
c86422c5 | 138 | .bdrv_co_getlength = compress_co_getlength, |
f41388e0 AS |
139 | |
140 | .bdrv_co_preadv_part = compress_co_preadv_part, | |
141 | .bdrv_co_pwritev_part = compress_co_pwritev_part, | |
142 | .bdrv_co_pwrite_zeroes = compress_co_pwrite_zeroes, | |
143 | .bdrv_co_pdiscard = compress_co_pdiscard, | |
144 | .bdrv_refresh_limits = compress_refresh_limits, | |
145 | ||
2531b390 | 146 | .bdrv_co_eject = compress_co_eject, |
2c75261c | 147 | .bdrv_co_lock_medium = compress_co_lock_medium, |
f41388e0 | 148 | |
f41388e0 AS |
149 | .is_filter = true, |
150 | }; | |
151 | ||
152 | static void bdrv_compress_init(void) | |
153 | { | |
154 | bdrv_register(&bdrv_compress); | |
155 | } | |
156 | ||
157 | block_init(bdrv_compress_init); |