imago/format/drivers.rs
1//! Internal image format driver interface.
2//!
3//! Provides the internal interface for image format drivers to provide their services, on which
4//! the publically visible interface [`FormatAccess`] is built.
5
6use super::{Format, PreallocateMode};
7use crate::io_buffers::IoVectorMut;
8use crate::{FormatAccess, Storage};
9use async_trait::async_trait;
10use std::any::Any;
11use std::fmt::{Debug, Display};
12use std::io;
13
14/// Implementation of a disk image format.
15#[async_trait(?Send)]
16pub trait FormatDriverInstance: Any + Debug + Display + Send + Sync {
17 /// Type of storage used.
18 type Storage: Storage;
19
20 /// Return which format this is.
21 fn format(&self) -> Format;
22
23 /// Check whether `storage` has this format.
24 ///
25 /// This is only a rough test and does not guarantee that opening `storage` under this format
26 /// will succeed. Generally, it will only check the magic bytes (if available). For formats
27 /// that do not have distinct features (like raw), this will always return `true`.
28 ///
29 /// # Safety
30 /// Probing is inherently dangerous: Image formats like qcow2 allow referencing external files;
31 /// if you use imago to give untrusted parties (like VM guests) access to VM disk image files,
32 /// this will give those parties access to data in those files. Opening images from untrusted
33 /// sources can therefore be quite dangerous. Gating
34 /// ([`ImplicitOpenGate`](super::gate::ImplicitOpenGate)) can help mitigate this.
35 ///
36 /// If you do not know an image’s format, that is a sign it does not come from a trusted
37 /// source, and so opening it in a non-raw format may be quite dangerous.
38 ///
39 /// Perhaps most important to note is that giving an untrusted party (like a VM guest) access
40 /// to a raw image file allows that party to modify the whole file. It may write image headers
41 /// into this image file, causing a subsequent probe operation to recognize it as a non-raw
42 /// image, referencing arbitrary files on the host filesystem!
43 ///
44 /// When using imago to give an untrusted third party access to VM disk images, the guidelines
45 /// for probing are thus:
46 /// - Do not probe. If at all possible, obtain an image’s format from a trusted side channel.
47 /// - If there is no other way, probe each given image only once, before that untrusted third
48 /// party (like a VM guest) had write access to it; remember the probed format, and open the
49 /// image exclusively as that format.
50 ///
51 /// When working with even potentially untrusted images, you should always use an
52 /// [`ImplicitOpenGate`](super::gate::ImplicitOpenGate) to prevent access to files you do not
53 /// wish to access.
54 async unsafe fn probe(storage: &Self::Storage) -> io::Result<bool>
55 where
56 Self: Sized;
57
58 /// Size of the disk represented by this image.
59 fn size(&self) -> u64;
60
61 /// Granularity on which blocks can be marked as zero.
62 ///
63 /// This is the granularity for [`FormatDriverInstance::ensure_zero_mapping()`].
64 ///
65 /// Return `None` if zero blocks are not supported.
66 fn zero_granularity(&self) -> Option<u64> {
67 None
68 }
69
70 /// Recursively collect all storage objects associated with this image.
71 ///
72 /// “Recursive” means to recurse to other images like e.g. a backing file.
73 fn collect_storage_dependencies(&self) -> Vec<&Self::Storage>;
74
75 /// Return whether this image may be modified.
76 ///
77 /// This state must not change via interior mutability, i.e. as long as this FDI is wrapped in
78 /// a `FormatAccess`, its writability must remain constant.
79 fn writable(&self) -> bool;
80
81 /// Return the mapping at `offset`.
82 ///
83 /// Find what `offset` is mapped to, return that mapping information, and the length of that
84 /// continuous mapping (from `offset`).
85 ///
86 /// To determine that continuous mapping length, drivers should not perform additional I/O
87 /// beyond what is necessary to get mapping information for `offset` itself.
88 ///
89 /// `max_length` is a hint how long of a range is required at all, but the returned length may
90 /// exceed that value if that simplifies the implementation.
91 ///
92 /// The returned length must only be 0 if `ShallowMapping::Eof` is returned.
93 async fn get_mapping<'a>(
94 &'a self,
95 offset: u64,
96 max_length: u64,
97 ) -> io::Result<(ShallowMapping<'a, Self::Storage>, u64)>;
98
99 /// Ensure that `offset` is directly mapped to some storage object, up to a length of `length`.
100 ///
101 /// Return the storage object, the corresponding offset there, and the continuous length that
102 /// the driver was able to map (less than or equal to `length`).
103 ///
104 /// If the returned length is less than `length`, drivers can expect subsequent calls to
105 /// allocate the rest of the original range. Therefore, if a driver knows in advance that it
106 /// is impossible to fully map the given range (e.g. because it lies partially or fully beyond
107 /// the end of the disk), it should return an error immediately.
108 ///
109 /// If `overwrite` is true, the contents in the range are supposed to be overwritten and may be
110 /// discarded. Otherwise, they must be kept.
111 ///
112 /// Should not break existing data mappings, i.e. not discard or repurpose existing data
113 /// mappings. Making them unused, but retaining them as allocated so they can safely be
114 /// written to (albeit with no effect) is OK; discarding them so that they may be reused for
115 /// other mappings is not.
116 async fn ensure_data_mapping<'a>(
117 &'a self,
118 offset: u64,
119 length: u64,
120 overwrite: bool,
121 ) -> io::Result<(&'a Self::Storage, u64, u64)>;
122
123 /// Ensure that the given range is efficiently mapped as zeroes.
124 ///
125 /// Must not write any data. Return the range (offset and length) that could actually be
126 /// zeroed, which must be a subset of the range given by `offset` and `length`. The returned
127 /// offset must be as close to `offset` as possible, i.e. no zero mapping is possible between
128 /// `offset` and the returned offset (e.g. because of format-inherent granularity).
129 ///
130 /// The returned length may be zero in case zeroing would theoretically be possible, but not
131 /// for this range at this granularity.
132 ///
133 /// Should not break existing data mappings, i.e. not discard or repurpose existing data
134 /// mappings. Making them unused, but retaining them as allocated so they can safely be
135 /// written to (albeit with no effect) is OK; discarding them so that they may be reused for
136 /// other mappings is not.
137 async fn ensure_zero_mapping(&self, _offset: u64, _length: u64) -> io::Result<(u64, u64)> {
138 Err(io::ErrorKind::Unsupported.into())
139 }
140
141 /// Discard the given range, ensure it is read back as zeroes.
142 ///
143 /// Effectively the same as [`FormatDriverInstance::ensure_zero_mapping()`], but may break
144 /// existing data mappings thanks to the mutable `self` reference, which ensures that old data
145 /// mappings returned by [`FormatDriverInstance::get_mapping()`] cannot be held onto.
146 async fn discard_to_zero(&mut self, _offset: u64, _length: u64) -> io::Result<(u64, u64)> {
147 Err(io::ErrorKind::Unsupported.into())
148 }
149
150 /// Discard the given range.
151 ///
152 /// Effectively the same as [`FormatDriverInstance::discard_to_zero()`], but the discarded area
153 /// may read as any data. Backing file data should not reappear, however.
154 async fn discard_to_any(&mut self, _offset: u64, _length: u64) -> io::Result<(u64, u64)> {
155 Err(io::ErrorKind::Unsupported.into())
156 }
157
158 /// Discard the given range, such that the backing image becomes visible.
159 ///
160 /// Deallocate the range such that in deallocated blocks, the backing image’s data (if one
161 /// exists) will show, i.e. [`FormatDriverInstance::get_mapping()`] should return an indirect
162 /// mapping. When there is no backing image, those blocks should appear as zero.
163 ///
164 /// Return the range (offset and length) that could actually be discarded, which must be a
165 /// subset of `offset` and `length`, and the returned offset must be as close to `offset` as
166 /// possible (like for [`FormatDriverInstance::discard_to_backing()`].
167 ///
168 /// May break existing data mappings thanks to the mutable `self` reference.
169 async fn discard_to_backing(&mut self, _offset: u64, _length: u64) -> io::Result<(u64, u64)> {
170 Err(io::ErrorKind::Unsupported.into())
171 }
172
173 /// Read data from a `ShallowMapping::Special` area.
174 async fn readv_special(&self, _bufv: IoVectorMut<'_>, _offset: u64) -> io::Result<()> {
175 Err(io::ErrorKind::Unsupported.into())
176 }
177
178 /// Flush internal buffers.
179 ///
180 /// Does not need to ensure those buffers are synced to disk (hardware), and does not need to
181 /// drop them, i.e. they may still be used on later accesses.
182 async fn flush(&self) -> io::Result<()>;
183
184 /// Sync data already written to the storage hardware.
185 ///
186 /// Does not need to ensure internal buffers are written, i.e. should generally just be passed
187 /// through to `Storage::sync()` for all underlying storage objects.
188 async fn sync(&self) -> io::Result<()>;
189
190 /// Drop internal buffers.
191 ///
192 /// Drop all internal buffers, but do not flush them! All internal data must then be reloaded
193 /// from disk.
194 ///
195 /// # Safety
196 /// Not flushing internal buffers may cause image corruption. The caller must ensure the
197 /// on-disk state is consistent.
198 async unsafe fn invalidate_cache(&self) -> io::Result<()>;
199
200 /// Resize to the given size, which must be greater than the current size.
201 ///
202 /// Set the disk size to `new_size`, preallocating the new space according to `prealloc_mode`.
203 /// Depending on the image format, it is possible some preallocation modes are not supported,
204 /// in which case an [`std::io::ErrorKind::Unsupported`] is returned.
205 ///
206 /// If the current size is already `new_size` or greater, do nothing.
207 async fn resize_grow(&self, new_size: u64, prealloc_mode: PreallocateMode) -> io::Result<()>;
208
209 /// Truncate to the given size, which must be smaller than the current size.
210 ///
211 /// Set the disk size to `new_size`, discarding the data after `new_size`.
212 ///
213 /// May break existing data mappings thanks to the mutable `self` reference.
214 ///
215 /// If the current size is already `new_size` or smaller, do nothing.
216 async fn resize_shrink(&mut self, new_size: u64) -> io::Result<()>;
217}
218
219/// Non-recursive mapping information.
220///
221/// Mapping information as returned by [`FormatDriverInstance::get_mapping()`], only looking at
222/// that format layer’s information.
223#[derive(Debug)]
224#[non_exhaustive]
225pub enum ShallowMapping<'a, S: Storage + 'static> {
226 /// Raw data.
227 #[non_exhaustive]
228 Raw {
229 /// Storage object where this data is stored.
230 storage: &'a S,
231
232 /// Offset in `storage` where this data is stored.
233 offset: u64,
234
235 /// Whether this mapping may be written to.
236 ///
237 /// If `true`, you can directly write to `offset` on `storage` to change the disk image’s
238 /// data accordingly.
239 ///
240 /// If `false`, the disk image format does not allow writing to `offset` on `storage`; a
241 /// new mapping must be allocated first.
242 writable: bool,
243 },
244
245 /// Data lives in a different disk image (e.g. a backing file).
246 #[non_exhaustive]
247 Indirect {
248 /// Format instance where this data can be obtained.
249 layer: &'a FormatAccess<S>,
250
251 /// Offset in `layer` where this data can be obtained.
252 offset: u64,
253
254 /// Whether this mapping may be written to.
255 ///
256 /// If `true`, you can directly write to `offset` on `layer` to change the disk image’s
257 /// data accordingly.
258 ///
259 /// If `false`, the disk image format does not allow writing to `offset` on `layer`; a new
260 /// mapping must be allocated first.
261 writable: bool,
262 },
263
264 /// Range is to be read as zeroes.
265 #[non_exhaustive]
266 Zero {
267 /// Whether these zeroes are explicit on this layer.
268 ///
269 /// Differential image formats (like qcow2) track information about the status for all
270 /// blocks in the image (called clusters in case of qcow2). Perhaps most importantly, they
271 /// track whether a block is allocated or not:
272 /// - Allocated blocks have their data in the image.
273 /// - Unallocated blocks do not have their data in this image, but have to be read from a
274 /// backing image (which results in [`ShallowMapping::Indirect`] mappings).
275 ///
276 /// Thus, such images represent the difference from their backing image (hence
277 /// “differential”).
278 ///
279 /// Without a backing image, this feature can be used for sparse allocation: Unallocated
280 /// blocks are simply interpreted to be zero. These ranges will be noted as
281 /// [`ShallowMapping::Zero`] with `explicit` set to false.
282 ///
283 /// Formats like qcow2 can track more information beyond just the allocation status,
284 /// though, for example, whether a block should read as zero. Such blocks similarly do not
285 /// need to have their data stored in the image file, but are still not treated as
286 /// unallocated, so will never be read from a backing image, regardless of whether one
287 /// exists or not.
288 ///
289 /// These ranges are noted as [`ShallowMapping::Zero`] with `explicit` set to true.
290 explicit: bool,
291 },
292
293 /// End of file reached.
294 #[non_exhaustive]
295 Eof {},
296
297 /// Data is encoded in some manner, e.g. compressed or encrypted.
298 ///
299 /// Such data cannot be accessed directly, but must be interpreted by the image format driver.
300 #[non_exhaustive]
301 Special {
302 /// Original (“guest”) offset to pass to `FormatDriverInstance::readv_special()`.
303 offset: u64,
304 },
305}