]>
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" | |
25 | #include "block/block_int.h" | |
26 | #include "qemu/module.h" | |
27 | #include "qapi/error.h" | |
28 | ||
29 | ||
30 | static int compress_open(BlockDriverState *bs, QDict *options, int flags, | |
31 | Error **errp) | |
32 | { | |
33 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, false, | |
34 | errp); | |
35 | if (!bs->file) { | |
36 | return -EINVAL; | |
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 | ||
58 | static int64_t compress_getlength(BlockDriverState *bs) | |
59 | { | |
60 | return bdrv_getlength(bs->file->bs); | |
61 | } | |
62 | ||
63 | ||
64 | static int coroutine_fn compress_co_preadv_part(BlockDriverState *bs, | |
65 | uint64_t offset, uint64_t bytes, | |
66 | QEMUIOVector *qiov, | |
67 | size_t qiov_offset, | |
68 | int flags) | |
69 | { | |
70 | return bdrv_co_preadv_part(bs->file, offset, bytes, qiov, qiov_offset, | |
71 | flags); | |
72 | } | |
73 | ||
74 | ||
75 | static int coroutine_fn compress_co_pwritev_part(BlockDriverState *bs, | |
76 | uint64_t offset, | |
77 | uint64_t bytes, | |
78 | QEMUIOVector *qiov, | |
79 | size_t qiov_offset, int flags) | |
80 | { | |
81 | return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset, | |
82 | flags | BDRV_REQ_WRITE_COMPRESSED); | |
83 | } | |
84 | ||
85 | ||
86 | static int coroutine_fn compress_co_pwrite_zeroes(BlockDriverState *bs, | |
87 | int64_t offset, int bytes, | |
88 | BdrvRequestFlags flags) | |
89 | { | |
90 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); | |
91 | } | |
92 | ||
93 | ||
94 | static int coroutine_fn compress_co_pdiscard(BlockDriverState *bs, | |
95 | int64_t offset, int bytes) | |
96 | { | |
97 | return bdrv_co_pdiscard(bs->file, offset, bytes); | |
98 | } | |
99 | ||
100 | ||
101 | static void compress_refresh_limits(BlockDriverState *bs, Error **errp) | |
102 | { | |
103 | BlockDriverInfo bdi; | |
104 | int ret; | |
105 | ||
106 | if (!bs->file) { | |
107 | return; | |
108 | } | |
109 | ||
110 | ret = bdrv_get_info(bs->file->bs, &bdi); | |
111 | if (ret < 0 || bdi.cluster_size == 0) { | |
112 | return; | |
113 | } | |
114 | ||
115 | bs->bl.request_alignment = bdi.cluster_size; | |
116 | } | |
117 | ||
118 | ||
119 | static void compress_eject(BlockDriverState *bs, bool eject_flag) | |
120 | { | |
121 | bdrv_eject(bs->file->bs, eject_flag); | |
122 | } | |
123 | ||
124 | ||
125 | static void compress_lock_medium(BlockDriverState *bs, bool locked) | |
126 | { | |
127 | bdrv_lock_medium(bs->file->bs, locked); | |
128 | } | |
129 | ||
130 | ||
131 | static bool compress_recurse_is_first_non_filter(BlockDriverState *bs, | |
132 | BlockDriverState *candidate) | |
133 | { | |
134 | return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate); | |
135 | } | |
136 | ||
137 | ||
138 | static BlockDriver bdrv_compress = { | |
139 | .format_name = "compress", | |
140 | ||
141 | .bdrv_open = compress_open, | |
142 | .bdrv_child_perm = bdrv_filter_default_perms, | |
143 | ||
144 | .bdrv_getlength = compress_getlength, | |
145 | ||
146 | .bdrv_co_preadv_part = compress_co_preadv_part, | |
147 | .bdrv_co_pwritev_part = compress_co_pwritev_part, | |
148 | .bdrv_co_pwrite_zeroes = compress_co_pwrite_zeroes, | |
149 | .bdrv_co_pdiscard = compress_co_pdiscard, | |
150 | .bdrv_refresh_limits = compress_refresh_limits, | |
151 | ||
152 | .bdrv_eject = compress_eject, | |
153 | .bdrv_lock_medium = compress_lock_medium, | |
154 | ||
155 | .bdrv_co_block_status = bdrv_co_block_status_from_file, | |
156 | ||
157 | .bdrv_recurse_is_first_non_filter = compress_recurse_is_first_non_filter, | |
158 | ||
159 | .has_variable_length = true, | |
160 | .is_filter = true, | |
161 | }; | |
162 | ||
163 | static void bdrv_compress_init(void) | |
164 | { | |
165 | bdrv_register(&bdrv_compress); | |
166 | } | |
167 | ||
168 | block_init(bdrv_compress_init); |