1use crate::buffer::{BufferFuture, BufferRef, MutableBufferRef};
15use anyhow::{Error, bail};
16use async_trait::async_trait;
17pub use block_protocol::{InlineCryptoOptions, ReadOptions, WriteOptions};
19use futures::channel::oneshot::{Sender, channel};
20use std::future::Future;
21use std::mem::ManuallyDrop;
22use std::ops::{Deref, Range};
23use std::sync::{Arc, OnceLock};
24
25pub mod buffer;
26pub mod buffer_allocator;
27
28#[cfg(target_os = "fuchsia")]
29pub mod block_device;
30
31#[cfg(target_family = "unix")]
32pub mod file_backed_device;
33
34pub mod fake_device;
35
36pub mod ranged_device;
37
38#[async_trait]
39pub trait Device: Send + Sync {
41 fn allocate_buffer(&self, size: usize) -> BufferFuture<'_>;
45
46 fn clean_transfer_buffer(&self) {}
48
49 fn block_size(&self) -> u32;
51
52 fn block_count(&self) -> u64;
54
55 fn size(&self) -> u64 {
57 self.block_size() as u64 * self.block_count()
58 }
59
60 async fn read(&self, offset: u64, buffer: MutableBufferRef<'_>) -> Result<(), Error> {
62 self.read_with_opts(offset, buffer, ReadOptions::default()).await
63 }
64
65 async fn read_with_opts(
67 &self,
68 offset: u64,
69 buffer: MutableBufferRef<'_>,
70 read_opts: ReadOptions,
71 ) -> Result<(), Error>;
72
73 async fn write(&self, offset: u64, buffer: BufferRef<'_>) -> Result<(), Error> {
75 self.write_with_opts(offset, buffer, WriteOptions::default()).await
76 }
77
78 async fn write_with_opts(
80 &self,
81 offset: u64,
82 buffer: BufferRef<'_>,
83 write_opts: WriteOptions,
84 ) -> Result<(), Error>;
85
86 async fn trim(&self, range: Range<u64>) -> Result<(), Error>;
88
89 async fn close(&self) -> Result<(), Error>;
92
93 async fn flush(&self) -> Result<(), Error>;
95
96 fn barrier(&self);
98
99 fn reopen(&self, _read_only: bool) {
101 unreachable!();
102 }
103 fn is_read_only(&self) -> bool;
105
106 fn supports_trim(&self) -> bool;
108
109 fn snapshot(&self) -> Result<DeviceHolder, Error> {
111 bail!("Not supported");
112 }
113
114 fn discard_random_since_last_flush(&self) -> Result<(), Error> {
116 bail!("Not supported");
117 }
118
119 fn poison(&self) -> Result<(), Error> {
121 bail!("Not supported");
122 }
123}
124
125pub struct DeviceHolder {
132 device: ManuallyDrop<Arc<dyn Device>>,
133 on_drop: OnceLock<Sender<DeviceHolder>>,
134}
135
136impl DeviceHolder {
137 pub fn new(device: impl Device + 'static) -> Self {
138 DeviceHolder { device: ManuallyDrop::new(Arc::new(device)), on_drop: OnceLock::new() }
139 }
140
141 pub fn ensure_unique(&self) {
144 assert_eq!(Arc::strong_count(&self.device), 1);
145 }
146
147 pub fn take_when_dropped(&self) -> impl Future<Output = DeviceHolder> + use<> {
148 let (sender, receiver) = channel::<DeviceHolder>();
149 self.on_drop
150 .set(sender)
151 .unwrap_or_else(|_| panic!("take_when_dropped should only be called once"));
152 async { receiver.await.unwrap() }
153 }
154}
155
156impl Drop for DeviceHolder {
157 fn drop(&mut self) {
158 if let Some(sender) = self.on_drop.take() {
159 let device = ManuallyDrop::new(unsafe { ManuallyDrop::take(&mut self.device) });
161 let _ = sender.send(DeviceHolder { device, on_drop: OnceLock::new() });
163 } else {
164 unsafe { ManuallyDrop::drop(&mut self.device) }
166 }
167 }
168}
169
170impl Deref for DeviceHolder {
171 type Target = Arc<dyn Device>;
172
173 fn deref(&self) -> &Self::Target {
174 &self.device
175 }
176}
177
178#[cfg(test)]
179mod tests {
180 use super::DeviceHolder;
181 use crate::fake_device::FakeDevice;
182
183 #[fuchsia::test]
184 async fn test_take_when_dropped() {
185 let holder = DeviceHolder::new(FakeDevice::new(1, 512));
186 let fut = holder.take_when_dropped();
187 std::mem::drop(holder);
188 fut.await.ensure_unique();
189 }
190}