1#![forbid(unsafe_code)]
25
26pub use disk_backend::Disk;
27pub use disk_backend::DiskError;
28use disk_backend::DiskIo;
29use disk_backend::UnmapBehavior;
30use disklayer_ram::ram_disk_with_sector_size;
31use inspect::Inspect;
32use scsi_buffers::RequestBuffers;
33use std::future::Future;
34use std::pin::Pin;
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub enum DiskOperation {
39 Read {
41 sector: u64,
43 len: usize,
45 },
46 Write {
48 sector: u64,
50 len: usize,
52 fua: bool,
54 },
55 SyncCache,
57 Unmap {
59 sector: u64,
61 count: u64,
63 block_level_only: bool,
65 },
66 Eject,
68}
69
70type HookFuture = Pin<Box<dyn Future<Output = Result<(), DiskError>> + Send>>;
71type Hook = dyn Fn(DiskOperation) -> HookFuture + Send + Sync;
72
73#[derive(Inspect)]
79pub struct HookDisk {
80 sector_size: u32,
81 physical_sector_size: u32,
82 disk_id: Option<[u8; 16]>,
83 read_only: bool,
84 fua_respected: bool,
85 unmap_behavior: UnmapBehavior,
86 optimal_unmap_sectors: u32,
87 disk: Disk,
88 #[inspect(skip)]
89 hook: Option<Box<Hook>>,
90}
91
92impl HookDisk {
93 pub fn new(size: u64, sector_size: u32) -> Self {
100 Self::wrap(
101 ram_disk_with_sector_size(size, false, sector_size)
102 .expect("invalid hook disk specification"),
103 )
104 }
105
106 pub fn wrap(disk: Disk) -> Self {
108 Self {
109 sector_size: disk.sector_size(),
110 physical_sector_size: disk.physical_sector_size(),
111 disk_id: disk.disk_id(),
112 read_only: disk.is_read_only(),
113 fua_respected: disk.is_fua_respected(),
114 unmap_behavior: disk.unmap_behavior(),
115 optimal_unmap_sectors: disk.optimal_unmap_sectors(),
116 disk,
117 hook: None,
118 }
119 }
120
121 pub fn into_disk(self) -> Disk {
128 Disk::new(self).expect("invalid hook disk metadata")
129 }
130
131 pub fn with_physical_sector_size(mut self, sector_size: u32) -> Self {
133 self.physical_sector_size = sector_size;
134 self
135 }
136
137 pub fn with_disk_id(mut self, disk_id: [u8; 16]) -> Self {
139 self.disk_id = Some(disk_id);
140 self
141 }
142
143 pub fn with_read_only(mut self, read_only: bool) -> Self {
147 self.read_only = read_only;
148 self
149 }
150
151 pub fn with_fua_respected(mut self, respected: bool) -> Self {
153 self.fua_respected = respected;
154 self
155 }
156
157 pub fn with_unmap_behavior(mut self, behavior: UnmapBehavior) -> Self {
163 self.unmap_behavior = behavior;
164 self
165 }
166
167 pub fn with_optimal_unmap_sectors(mut self, sectors: u32) -> Self {
169 self.optimal_unmap_sectors = sectors;
170 self
171 }
172
173 pub fn with_sync_hook<F>(mut self, hook: F) -> Self
178 where
179 F: Fn(DiskOperation) -> Result<(), DiskError> + Send + Sync + 'static,
180 {
181 self.hook = Some(Box::new(move |op| Box::pin(std::future::ready(hook(op)))));
182 self
183 }
184
185 pub fn with_async_hook<F, Fut>(mut self, hook: F) -> Self
190 where
191 F: Fn(DiskOperation) -> Fut + Send + Sync + 'static,
192 Fut: Future<Output = Result<(), DiskError>> + Send + 'static,
193 {
194 self.hook = Some(Box::new(move |op| Box::pin(hook(op))));
195 self
196 }
197
198 async fn before(&self, operation: DiskOperation) -> Result<(), DiskError> {
199 if let Some(hook) = &self.hook {
200 hook(operation).await?;
201 }
202 Ok(())
203 }
204
205 fn check_writable(&self, sector: u64, count: u64) -> Result<(), DiskError> {
208 if sector
209 .checked_add(count)
210 .is_none_or(|end| end > self.disk.sector_count())
211 {
212 return Err(DiskError::IllegalBlock);
213 }
214 if self.read_only {
215 return Err(DiskError::ReadOnly);
216 }
217 Ok(())
218 }
219}
220
221impl DiskIo for HookDisk {
222 fn disk_type(&self) -> &str {
223 "hook"
224 }
225
226 fn sector_count(&self) -> u64 {
227 self.disk.sector_count()
228 }
229
230 fn sector_size(&self) -> u32 {
231 self.sector_size
232 }
233
234 fn physical_sector_size(&self) -> u32 {
235 self.physical_sector_size
236 }
237
238 fn disk_id(&self) -> Option<[u8; 16]> {
239 self.disk_id
240 }
241
242 fn is_read_only(&self) -> bool {
243 self.read_only
244 }
245
246 fn is_fua_respected(&self) -> bool {
247 self.fua_respected
248 }
249
250 fn unmap_behavior(&self) -> UnmapBehavior {
251 self.unmap_behavior
252 }
253
254 fn optimal_unmap_sectors(&self) -> u32 {
255 self.optimal_unmap_sectors
256 }
257
258 fn pr(&self) -> Option<&dyn disk_backend::pr::PersistentReservation> {
259 self.disk.pr()
260 }
261
262 async fn read_vectored(
263 &self,
264 buffers: &RequestBuffers<'_>,
265 sector: u64,
266 ) -> Result<(), DiskError> {
267 self.before(DiskOperation::Read {
268 sector,
269 len: buffers.len(),
270 })
271 .await?;
272 self.disk.read_vectored(buffers, sector).await
273 }
274
275 async fn write_vectored(
276 &self,
277 buffers: &RequestBuffers<'_>,
278 sector: u64,
279 fua: bool,
280 ) -> Result<(), DiskError> {
281 self.before(DiskOperation::Write {
282 sector,
283 len: buffers.len(),
284 fua,
285 })
286 .await?;
287 let count = buffers.len() as u64 >> self.sector_size.trailing_zeros();
288 self.check_writable(sector, count)?;
289 self.disk.write_vectored(buffers, sector, fua).await
290 }
291
292 async fn sync_cache(&self) -> Result<(), DiskError> {
293 self.before(DiskOperation::SyncCache).await?;
294 self.disk.sync_cache().await
295 }
296
297 async fn unmap(
298 &self,
299 sector: u64,
300 count: u64,
301 block_level_only: bool,
302 ) -> Result<(), DiskError> {
303 self.before(DiskOperation::Unmap {
304 sector,
305 count,
306 block_level_only,
307 })
308 .await?;
309 self.check_writable(sector, count)?;
310 self.disk.unmap(sector, count, block_level_only).await
311 }
312
313 async fn eject(&self) -> Result<(), DiskError> {
314 self.before(DiskOperation::Eject).await?;
315 self.disk.eject().await
316 }
317
318 async fn wait_resize(&self, sector_count: u64) -> u64 {
319 self.disk.wait_resize(sector_count).await
320 }
321}
322
323#[cfg(test)]
324mod tests;