Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 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 | { |
Max Reitz | b3af2af | 2020-05-13 13:05:36 +0200 | [diff] [blame] | 33 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds, |
| 34 | BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, |
| 35 | false, errp); |
Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 36 | if (!bs->file) { |
| 37 | return -EINVAL; |
| 38 | } |
| 39 | |
| 40 | if (!bs->file->bs->drv || !block_driver_can_compress(bs->file->bs->drv)) { |
| 41 | error_setg(errp, |
| 42 | "Compression is not supported for underlying format: %s", |
| 43 | bdrv_get_format_name(bs->file->bs) ?: "(no format)"); |
| 44 | |
| 45 | return -ENOTSUP; |
| 46 | } |
| 47 | |
| 48 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED | |
| 49 | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); |
| 50 | |
| 51 | bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | |
| 52 | ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) & |
| 53 | bs->file->bs->supported_zero_flags); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | static int64_t compress_getlength(BlockDriverState *bs) |
| 60 | { |
| 61 | return bdrv_getlength(bs->file->bs); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static int coroutine_fn compress_co_preadv_part(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | f7ef38d | 2021-09-03 13:27:59 +0300 | [diff] [blame] | 66 | int64_t offset, int64_t bytes, |
Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 67 | QEMUIOVector *qiov, |
| 68 | size_t qiov_offset, |
Vladimir Sementsov-Ogievskiy | f7ef38d | 2021-09-03 13:27:59 +0300 | [diff] [blame] | 69 | BdrvRequestFlags flags) |
Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 70 | { |
| 71 | return bdrv_co_preadv_part(bs->file, offset, bytes, qiov, qiov_offset, |
| 72 | flags); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | static int coroutine_fn compress_co_pwritev_part(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | e75abed | 2021-09-03 13:28:00 +0300 | [diff] [blame] | 77 | int64_t offset, |
| 78 | int64_t bytes, |
Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 79 | QEMUIOVector *qiov, |
Vladimir Sementsov-Ogievskiy | e75abed | 2021-09-03 13:28:00 +0300 | [diff] [blame] | 80 | size_t qiov_offset, |
| 81 | BdrvRequestFlags flags) |
Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 82 | { |
| 83 | return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset, |
| 84 | flags | BDRV_REQ_WRITE_COMPRESSED); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | static int coroutine_fn compress_co_pwrite_zeroes(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | f34b2bc | 2021-09-03 13:28:03 +0300 | [diff] [blame] | 89 | int64_t offset, int64_t bytes, |
Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 90 | BdrvRequestFlags flags) |
| 91 | { |
| 92 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static int coroutine_fn compress_co_pdiscard(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | 0c80228 | 2021-09-03 13:28:06 +0300 | [diff] [blame] | 97 | int64_t offset, int64_t bytes) |
Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 98 | { |
| 99 | return bdrv_co_pdiscard(bs->file, offset, bytes); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static void compress_refresh_limits(BlockDriverState *bs, Error **errp) |
| 104 | { |
| 105 | BlockDriverInfo bdi; |
| 106 | int ret; |
| 107 | |
| 108 | if (!bs->file) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | ret = bdrv_get_info(bs->file->bs, &bdi); |
| 113 | if (ret < 0 || bdi.cluster_size == 0) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | bs->bl.request_alignment = bdi.cluster_size; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | static void compress_eject(BlockDriverState *bs, bool eject_flag) |
| 122 | { |
| 123 | bdrv_eject(bs->file->bs, eject_flag); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static void compress_lock_medium(BlockDriverState *bs, bool locked) |
| 128 | { |
| 129 | bdrv_lock_medium(bs->file->bs, locked); |
| 130 | } |
| 131 | |
| 132 | |
Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 133 | static BlockDriver bdrv_compress = { |
| 134 | .format_name = "compress", |
| 135 | |
| 136 | .bdrv_open = compress_open, |
Max Reitz | 69dca43 | 2020-05-13 13:05:39 +0200 | [diff] [blame] | 137 | .bdrv_child_perm = bdrv_default_perms, |
Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 138 | |
| 139 | .bdrv_getlength = compress_getlength, |
| 140 | |
| 141 | .bdrv_co_preadv_part = compress_co_preadv_part, |
| 142 | .bdrv_co_pwritev_part = compress_co_pwritev_part, |
| 143 | .bdrv_co_pwrite_zeroes = compress_co_pwrite_zeroes, |
| 144 | .bdrv_co_pdiscard = compress_co_pdiscard, |
| 145 | .bdrv_refresh_limits = compress_refresh_limits, |
| 146 | |
| 147 | .bdrv_eject = compress_eject, |
| 148 | .bdrv_lock_medium = compress_lock_medium, |
| 149 | |
Andrey Shinkevich | f41388e | 2019-12-02 15:15:04 +0300 | [diff] [blame] | 150 | .has_variable_length = true, |
| 151 | .is_filter = true, |
| 152 | }; |
| 153 | |
| 154 | static void bdrv_compress_init(void) |
| 155 | { |
| 156 | bdrv_register(&bdrv_compress); |
| 157 | } |
| 158 | |
| 159 | block_init(bdrv_compress_init); |