]>
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 | { | |
31 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, false, | |
32 | errp); | |
33 | if (!bs->file) { | |
34 | return -EINVAL; | |
35 | } | |
36 | ||
228345bf | 37 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED | |
80f5c33f | 38 | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); |
6c6f24fd | 39 | |
228345bf | 40 | bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | |
80f5c33f KW |
41 | ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) & |
42 | bs->file->bs->supported_zero_flags); | |
6c6f24fd HR |
43 | |
44 | return 0; | |
45 | } | |
46 | ||
47 | ||
6c6f24fd HR |
48 | #define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \ |
49 | | BLK_PERM_WRITE \ | |
50 | | BLK_PERM_RESIZE) | |
51 | #define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH) | |
52 | ||
53 | static void cor_child_perm(BlockDriverState *bs, BdrvChild *c, | |
54 | const BdrvChildRole *role, | |
55 | BlockReopenQueue *reopen_queue, | |
56 | uint64_t perm, uint64_t shared, | |
57 | uint64_t *nperm, uint64_t *nshared) | |
58 | { | |
2b23f286 KW |
59 | *nperm = perm & PERM_PASSTHROUGH; |
60 | *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED; | |
6c6f24fd | 61 | |
2b23f286 KW |
62 | /* We must not request write permissions for an inactive node, the child |
63 | * cannot provide it. */ | |
64 | if (!(bs->open_flags & BDRV_O_INACTIVE)) { | |
65 | *nperm |= BLK_PERM_WRITE_UNCHANGED; | |
66 | } | |
6c6f24fd HR |
67 | } |
68 | ||
69 | ||
70 | static int64_t cor_getlength(BlockDriverState *bs) | |
71 | { | |
72 | return bdrv_getlength(bs->file->bs); | |
73 | } | |
74 | ||
75 | ||
6c6f24fd HR |
76 | static int coroutine_fn cor_co_preadv(BlockDriverState *bs, |
77 | uint64_t offset, uint64_t bytes, | |
78 | QEMUIOVector *qiov, int flags) | |
79 | { | |
80 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, | |
81 | flags | BDRV_REQ_COPY_ON_READ); | |
82 | } | |
83 | ||
84 | ||
85 | static int coroutine_fn cor_co_pwritev(BlockDriverState *bs, | |
86 | uint64_t offset, uint64_t bytes, | |
87 | QEMUIOVector *qiov, int flags) | |
88 | { | |
89 | ||
90 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); | |
91 | } | |
92 | ||
93 | ||
94 | static int coroutine_fn cor_co_pwrite_zeroes(BlockDriverState *bs, | |
95 | int64_t offset, int bytes, | |
96 | BdrvRequestFlags flags) | |
97 | { | |
98 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); | |
99 | } | |
100 | ||
101 | ||
102 | static int coroutine_fn cor_co_pdiscard(BlockDriverState *bs, | |
103 | int64_t offset, int bytes) | |
104 | { | |
0b9fd3f4 | 105 | return bdrv_co_pdiscard(bs->file, offset, bytes); |
6c6f24fd HR |
106 | } |
107 | ||
108 | ||
109 | static void cor_eject(BlockDriverState *bs, bool eject_flag) | |
110 | { | |
111 | bdrv_eject(bs->file->bs, eject_flag); | |
112 | } | |
113 | ||
114 | ||
115 | static void cor_lock_medium(BlockDriverState *bs, bool locked) | |
116 | { | |
117 | bdrv_lock_medium(bs->file->bs, locked); | |
118 | } | |
119 | ||
120 | ||
782b9d06 | 121 | static BlockDriver bdrv_copy_on_read = { |
6c6f24fd HR |
122 | .format_name = "copy-on-read", |
123 | ||
124 | .bdrv_open = cor_open, | |
6c6f24fd HR |
125 | .bdrv_child_perm = cor_child_perm, |
126 | ||
127 | .bdrv_getlength = cor_getlength, | |
6c6f24fd HR |
128 | |
129 | .bdrv_co_preadv = cor_co_preadv, | |
130 | .bdrv_co_pwritev = cor_co_pwritev, | |
131 | .bdrv_co_pwrite_zeroes = cor_co_pwrite_zeroes, | |
132 | .bdrv_co_pdiscard = cor_co_pdiscard, | |
133 | ||
134 | .bdrv_eject = cor_eject, | |
135 | .bdrv_lock_medium = cor_lock_medium, | |
136 | ||
137 | .bdrv_co_block_status = bdrv_co_block_status_from_file, | |
138 | ||
6c6f24fd HR |
139 | .has_variable_length = true, |
140 | .is_filter = true, | |
141 | }; | |
142 | ||
143 | static void bdrv_copy_on_read_init(void) | |
144 | { | |
145 | bdrv_register(&bdrv_copy_on_read); | |
146 | } | |
147 | ||
148 | block_init(bdrv_copy_on_read_init); |