Commit | Line | Data |
---|---|---|
6c6f24fd HR |
1 | /* |
2 | * Copy-on-read filter block driver | |
3 | * | |
4 | * Copyright (c) 2018 Red Hat, Inc. | |
5 | * | |
6 | * Author: | |
7 | * Max Reitz <mreitz@redhat.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License as | |
11 | * published by the Free Software Foundation; either version 2 or | |
12 | * (at your option) version 3 of the License. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
21 | */ | |
22 | ||
23 | #include "qemu/osdep.h" | |
24 | #include "block/block_int.h" | |
0b8fa32f | 25 | #include "qemu/module.h" |
6c6f24fd HR |
26 | |
27 | ||
28 | static int cor_open(BlockDriverState *bs, QDict *options, int flags, | |
29 | Error **errp) | |
30 | { | |
b3af2af4 HR |
31 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds, |
32 | BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, | |
33 | false, errp); | |
6c6f24fd HR |
34 | if (!bs->file) { |
35 | return -EINVAL; | |
36 | } | |
37 | ||
228345bf | 38 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED | |
80f5c33f | 39 | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); |
6c6f24fd | 40 | |
228345bf | 41 | bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | |
80f5c33f KW |
42 | ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) & |
43 | bs->file->bs->supported_zero_flags); | |
6c6f24fd HR |
44 | |
45 | return 0; | |
46 | } | |
47 | ||
48 | ||
6c6f24fd HR |
49 | #define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \ |
50 | | BLK_PERM_WRITE \ | |
51 | | BLK_PERM_RESIZE) | |
52 | #define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH) | |
53 | ||
54 | static void cor_child_perm(BlockDriverState *bs, BdrvChild *c, | |
bf8e925e | 55 | BdrvChildRole role, |
6c6f24fd HR |
56 | BlockReopenQueue *reopen_queue, |
57 | uint64_t perm, uint64_t shared, | |
58 | uint64_t *nperm, uint64_t *nshared) | |
59 | { | |
2b23f286 KW |
60 | *nperm = perm & PERM_PASSTHROUGH; |
61 | *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED; | |
6c6f24fd | 62 | |
2b23f286 KW |
63 | /* We must not request write permissions for an inactive node, the child |
64 | * cannot provide it. */ | |
65 | if (!(bs->open_flags & BDRV_O_INACTIVE)) { | |
66 | *nperm |= BLK_PERM_WRITE_UNCHANGED; | |
67 | } | |
6c6f24fd HR |
68 | } |
69 | ||
70 | ||
71 | static int64_t cor_getlength(BlockDriverState *bs) | |
72 | { | |
73 | return bdrv_getlength(bs->file->bs); | |
74 | } | |
75 | ||
76 | ||
6c6f24fd HR |
77 | static int coroutine_fn cor_co_preadv(BlockDriverState *bs, |
78 | uint64_t offset, uint64_t bytes, | |
79 | QEMUIOVector *qiov, int flags) | |
80 | { | |
81 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, | |
82 | flags | BDRV_REQ_COPY_ON_READ); | |
83 | } | |
84 | ||
85 | ||
86 | static int coroutine_fn cor_co_pwritev(BlockDriverState *bs, | |
87 | uint64_t offset, uint64_t bytes, | |
88 | QEMUIOVector *qiov, int flags) | |
89 | { | |
90 | ||
91 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); | |
92 | } | |
93 | ||
94 | ||
95 | static int coroutine_fn cor_co_pwrite_zeroes(BlockDriverState *bs, | |
96 | int64_t offset, int bytes, | |
97 | BdrvRequestFlags flags) | |
98 | { | |
99 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); | |
100 | } | |
101 | ||
102 | ||
103 | static int coroutine_fn cor_co_pdiscard(BlockDriverState *bs, | |
104 | int64_t offset, int bytes) | |
105 | { | |
0b9fd3f4 | 106 | return bdrv_co_pdiscard(bs->file, offset, bytes); |
6c6f24fd HR |
107 | } |
108 | ||
109 | ||
4935e8be HR |
110 | static int coroutine_fn cor_co_pwritev_compressed(BlockDriverState *bs, |
111 | uint64_t offset, | |
112 | uint64_t bytes, | |
113 | QEMUIOVector *qiov) | |
114 | { | |
115 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, | |
116 | BDRV_REQ_WRITE_COMPRESSED); | |
117 | } | |
118 | ||
119 | ||
6c6f24fd HR |
120 | static void cor_eject(BlockDriverState *bs, bool eject_flag) |
121 | { | |
122 | bdrv_eject(bs->file->bs, eject_flag); | |
123 | } | |
124 | ||
125 | ||
126 | static void cor_lock_medium(BlockDriverState *bs, bool locked) | |
127 | { | |
128 | bdrv_lock_medium(bs->file->bs, locked); | |
129 | } | |
130 | ||
131 | ||
782b9d06 | 132 | static BlockDriver bdrv_copy_on_read = { |
6c6f24fd HR |
133 | .format_name = "copy-on-read", |
134 | ||
135 | .bdrv_open = cor_open, | |
6c6f24fd HR |
136 | .bdrv_child_perm = cor_child_perm, |
137 | ||
138 | .bdrv_getlength = cor_getlength, | |
6c6f24fd HR |
139 | |
140 | .bdrv_co_preadv = cor_co_preadv, | |
141 | .bdrv_co_pwritev = cor_co_pwritev, | |
142 | .bdrv_co_pwrite_zeroes = cor_co_pwrite_zeroes, | |
143 | .bdrv_co_pdiscard = cor_co_pdiscard, | |
4935e8be | 144 | .bdrv_co_pwritev_compressed = cor_co_pwritev_compressed, |
6c6f24fd HR |
145 | |
146 | .bdrv_eject = cor_eject, | |
147 | .bdrv_lock_medium = cor_lock_medium, | |
148 | ||
6c6f24fd HR |
149 | .has_variable_length = true, |
150 | .is_filter = true, | |
151 | }; | |
152 | ||
153 | static void bdrv_copy_on_read_init(void) | |
154 | { | |
155 | bdrv_register(&bdrv_copy_on_read); | |
156 | } | |
157 | ||
158 | block_init(bdrv_copy_on_read_init); |