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 // Safe: `&mut self` guarantees nobody has concurrent data mappings
148 unsafe { self.discard_to_zero_unsafe(offset, length).await }
149 }
150
151 /// Discard the given range, ensure it is read back as zeroes.
152 ///
153 /// Unsafe variant of [FormatDriverInstance::discard_to_zero()], only requiring an immutable
154 /// &self
155 ///
156 /// # Safety
157 /// This function is marked as unsafe because:
158 /// - It may invalidate all existing data mappings.
159 ///
160 /// The caller must ensure that no other references to this driver instance exist and that
161 /// the caller must ensure that all previously looked up mappings are no longer assumed to
162 /// be valid after this operation.
163 ///
164 /// Because mappings contain references to the block driver instance, one way to do so is
165 /// to have a mutable reference to the block driver instance, which will automatically
166 /// ensure there are no other references (and thus no mappings). In that case, you can use
167 /// the safe variant [`Self::discard_to_zero()`].
168 async unsafe fn discard_to_zero_unsafe(
169 &self,
170 _offset: u64,
171 _length: u64,
172 ) -> io::Result<(u64, u64)> {
173 Err(io::ErrorKind::Unsupported.into())
174 }
175
176 /// Discard the given range.
177 ///
178 /// Effectively the same as [`FormatDriverInstance::discard_to_zero()`], but the discarded area
179 /// may read as any data. Backing file data should not reappear, however.
180 async fn discard_to_any(&mut self, offset: u64, length: u64) -> io::Result<(u64, u64)> {
181 // Safe: `&mut self` guarantees nobody has concurrent data mappings
182 unsafe { self.discard_to_any_unsafe(offset, length).await }
183 }
184
185 /// Discard the given range.
186 ///
187 /// Unsafe variant of [FormatDriverInstance::discard_to_any()], only requiring an immutable
188 /// &self
189 ///
190 /// # Safety
191 /// This function is marked as unsafe because:
192 /// - It may invalidate all existing data mappings.
193 ///
194 /// The caller must ensure that no other references to this driver instance exist and that
195 /// the caller must ensure that all previously looked up mappings are no longer assumed to
196 /// be valid after this operation.
197 ///
198 /// Because mappings contain references to the block driver instance, one way to do so is
199 /// to have a mutable reference to the block driver instance, which will automatically
200 /// ensure there are no other references (and thus no mappings). In that case, you can use
201 /// the safe variant [`Self::discard_to_any()`].
202 async unsafe fn discard_to_any_unsafe(
203 &self,
204 _offset: u64,
205 _length: u64,
206 ) -> io::Result<(u64, u64)> {
207 Err(io::ErrorKind::Unsupported.into())
208 }
209
210 /// Discard the given range, such that the backing image becomes visible.
211 ///
212 /// Deallocate the range such that in deallocated blocks, the backing image’s data (if one
213 /// exists) will show, i.e. [`FormatDriverInstance::get_mapping()`] should return an indirect
214 /// mapping. When there is no backing image, those blocks should appear as zero.
215 ///
216 /// Return the range (offset and length) that could actually be discarded, which must be a
217 /// subset of `offset` and `length`, and the returned offset must be as close to `offset` as
218 /// possible (like for [`FormatDriverInstance::discard_to_backing()`].
219 ///
220 /// May break existing data mappings thanks to the mutable `self` reference.
221 async fn discard_to_backing(&mut self, offset: u64, length: u64) -> io::Result<(u64, u64)> {
222 // Safe: `&mut self` guarantees nobody has concurrent data mappings
223 unsafe { self.discard_to_backing_unsafe(offset, length).await }
224 }
225
226 /// Discard the given range, such that the backing image becomes visible.
227 ///
228 /// Unsafe variant of [FormatDriverInstance::discard_to_backing()], only requiring an immutable
229 /// &self
230 ///
231 /// # Safety
232 /// This function is marked as unsafe because:
233 /// - It may invalidate all existing data mappings.
234 ///
235 /// The caller must ensure that no other references to this driver instance exist and that
236 /// the caller must ensure that all previously looked up mappings are no longer assumed to
237 /// be valid after this operation.
238 ///
239 /// Because mappings contain references to the block driver instance, one way to do so is
240 /// to have a mutable reference to the block driver instance, which will automatically
241 /// ensure there are no other references (and thus no mappings). In that case, you can use
242 /// the safe variant [`Self::discard_to_backing()`].
243 async unsafe fn discard_to_backing_unsafe(
244 &self,
245 _offset: u64,
246 _length: u64,
247 ) -> io::Result<(u64, u64)> {
248 Err(io::ErrorKind::Unsupported.into())
249 }
250
251 /// Read data from a `ShallowMapping::Special` area.
252 async fn readv_special(&self, _bufv: IoVectorMut<'_>, _offset: u64) -> io::Result<()> {
253 Err(io::ErrorKind::Unsupported.into())
254 }
255
256 /// Flush internal buffers.
257 ///
258 /// Does not need to ensure those buffers are synced to disk (hardware), and does not need to
259 /// drop them, i.e. they may still be used on later accesses.
260 async fn flush(&self) -> io::Result<()>;
261
262 /// Sync data already written to the storage hardware.
263 ///
264 /// Does not need to ensure internal buffers are written, i.e. should generally just be passed
265 /// through to `Storage::sync()` for all underlying storage objects.
266 async fn sync(&self) -> io::Result<()>;
267
268 /// Drop internal buffers.
269 ///
270 /// Drop all internal buffers, but do not flush them! All internal data must then be reloaded
271 /// from disk.
272 ///
273 /// # Safety
274 /// Not flushing internal buffers may cause image corruption. The caller must ensure the
275 /// on-disk state is consistent.
276 async unsafe fn invalidate_cache(&self) -> io::Result<()>;
277
278 /// Resize to the given size, which must be greater than the current size.
279 ///
280 /// Set the disk size to `new_size`, preallocating the new space according to `prealloc_mode`.
281 /// Depending on the image format, it is possible some preallocation modes are not supported,
282 /// in which case an [`std::io::ErrorKind::Unsupported`] is returned.
283 ///
284 /// If the current size is already `new_size` or greater, do nothing.
285 async fn resize_grow(&self, new_size: u64, prealloc_mode: PreallocateMode) -> io::Result<()>;
286
287 /// Truncate to the given size, which must be smaller than the current size.
288 ///
289 /// Set the disk size to `new_size`, discarding the data after `new_size`.
290 ///
291 /// May break existing data mappings thanks to the mutable `self` reference.
292 ///
293 /// If the current size is already `new_size` or smaller, do nothing.
294 async fn resize_shrink(&mut self, new_size: u64) -> io::Result<()>;
295}
296
297/// Non-recursive mapping information.
298///
299/// Mapping information as returned by [`FormatDriverInstance::get_mapping()`], only looking at
300/// that format layer’s information.
301#[derive(Debug)]
302#[non_exhaustive]
303pub enum ShallowMapping<'a, S: Storage + 'static> {
304 /// Raw data.
305 #[non_exhaustive]
306 Raw {
307 /// Storage object where this data is stored.
308 storage: &'a S,
309
310 /// Offset in `storage` where this data is stored.
311 offset: u64,
312
313 /// Whether this mapping may be written to.
314 ///
315 /// If `true`, you can directly write to `offset` on `storage` to change the disk image’s
316 /// data accordingly.
317 ///
318 /// If `false`, the disk image format does not allow writing to `offset` on `storage`; a
319 /// new mapping must be allocated first.
320 writable: bool,
321 },
322
323 /// Data lives in a different disk image (e.g. a backing file).
324 #[non_exhaustive]
325 Indirect {
326 /// Format instance where this data can be obtained.
327 layer: &'a FormatAccess<S>,
328
329 /// Offset in `layer` where this data can be obtained.
330 offset: u64,
331
332 /// Whether this mapping may be written to.
333 ///
334 /// If `true`, you can directly write to `offset` on `layer` to change the disk image’s
335 /// data accordingly.
336 ///
337 /// If `false`, the disk image format does not allow writing to `offset` on `layer`; a new
338 /// mapping must be allocated first.
339 writable: bool,
340 },
341
342 /// Range is to be read as zeroes.
343 #[non_exhaustive]
344 Zero {
345 /// Whether these zeroes are explicit on this layer.
346 ///
347 /// Differential image formats (like qcow2) track information about the status for all
348 /// blocks in the image (called clusters in case of qcow2). Perhaps most importantly, they
349 /// track whether a block is allocated or not:
350 /// - Allocated blocks have their data in the image.
351 /// - Unallocated blocks do not have their data in this image, but have to be read from a
352 /// backing image (which results in [`ShallowMapping::Indirect`] mappings).
353 ///
354 /// Thus, such images represent the difference from their backing image (hence
355 /// “differential”).
356 ///
357 /// Without a backing image, this feature can be used for sparse allocation: Unallocated
358 /// blocks are simply interpreted to be zero. These ranges will be noted as
359 /// [`ShallowMapping::Zero`] with `explicit` set to false.
360 ///
361 /// Formats like qcow2 can track more information beyond just the allocation status,
362 /// though, for example, whether a block should read as zero. Such blocks similarly do not
363 /// need to have their data stored in the image file, but are still not treated as
364 /// unallocated, so will never be read from a backing image, regardless of whether one
365 /// exists or not.
366 ///
367 /// These ranges are noted as [`ShallowMapping::Zero`] with `explicit` set to true.
368 explicit: bool,
369 },
370
371 /// End of file reached.
372 #[non_exhaustive]
373 Eof {},
374
375 /// Data is encoded in some manner, e.g. compressed or encrypted.
376 ///
377 /// Such data cannot be accessed directly, but must be interpreted by the image format driver.
378 #[non_exhaustive]
379 Special {
380 /// Original (“guest”) offset to pass to `FormatDriverInstance::readv_special()`.
381 offset: u64,
382 },
383}