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