Skip to content

Commit ed74d1c

Browse files
committed
virtio/block: wrap disk_image in a Mutex
Imago's discard_to_* operations require a mutable reference to Storage. Let's wrap disk_image in a Mutex so we can obtain such reference when needed. Since, in practice, we won't have multiple threads accessing the lock, the overhead should be very small (one or two operations on atomics). Signed-off-by: Sergio Lopez <[email protected]>
1 parent d8a0c55 commit ed74d1c

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

src/devices/src/virtio/block/device.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::os::linux::fs::MetadataExt;
1515
use std::os::macos::fs::MetadataExt;
1616
use std::path::PathBuf;
1717
use std::result;
18-
use std::sync::Arc;
18+
use std::sync::{Arc, Mutex};
1919
use std::thread::JoinHandle;
2020

2121
use imago::{
@@ -65,18 +65,18 @@ impl CacheType {
6565
/// Helper object for setting up all `Block` fields derived from its backing file.
6666
pub(crate) struct DiskProperties {
6767
cache_type: CacheType,
68-
pub(crate) file: Arc<SyncFormatAccess<Box<dyn DynStorage>>>,
68+
pub(crate) file: Arc<Mutex<SyncFormatAccess<Box<dyn DynStorage>>>>,
6969
nsectors: u64,
7070
image_id: Vec<u8>,
7171
}
7272

7373
impl DiskProperties {
7474
pub fn new(
75-
disk_image: Arc<SyncFormatAccess<Box<dyn DynStorage>>>,
75+
disk_image: Arc<Mutex<SyncFormatAccess<Box<dyn DynStorage>>>>,
7676
disk_image_id: Vec<u8>,
7777
cache_type: CacheType,
7878
) -> io::Result<Self> {
79-
let disk_size = disk_image.size();
79+
let disk_size = disk_image.lock().unwrap().size();
8080

8181
// We only support disk size, which uses the first two words of the configuration space.
8282
// If the image is not a multiple of the sector size, the tail bits are not exposed.
@@ -95,10 +95,6 @@ impl DiskProperties {
9595
})
9696
}
9797

98-
pub fn file(&self) -> &SyncFormatAccess<Box<dyn DynStorage>> {
99-
self.file.as_ref()
100-
}
101-
10298
pub fn nsectors(&self) -> u64 {
10399
self.nsectors
104100
}
@@ -146,11 +142,11 @@ impl Drop for DiskProperties {
146142
match self.cache_type {
147143
CacheType::Writeback => {
148144
// flush() first to force any cached data out.
149-
if self.file.flush().is_err() {
145+
if self.file.lock().unwrap().flush().is_err() {
150146
error!("Failed to flush block data on drop.");
151147
}
152148
// Sync data out to physical media on host.
153-
if self.file.sync().is_err() {
149+
if self.file.lock().unwrap().sync().is_err() {
154150
error!("Failed to sync block data on drop.")
155151
}
156152
}
@@ -206,7 +202,7 @@ pub struct Block {
206202
// Host file and properties.
207203
disk: Option<DiskProperties>,
208204
cache_type: CacheType,
209-
disk_image: Arc<SyncFormatAccess<Box<dyn DynStorage>>>,
205+
disk_image: Arc<Mutex<SyncFormatAccess<Box<dyn DynStorage>>>>,
210206
disk_image_id: Vec<u8>,
211207
worker_thread: Option<JoinHandle<()>>,
212208
worker_stopfd: EventFd,
@@ -271,10 +267,10 @@ impl Block {
271267
}
272268
};
273269

274-
let disk_image = Arc::new(disk_image);
270+
let disk_image = Arc::new(Mutex::new(disk_image));
275271

276272
let disk_properties =
277-
DiskProperties::new(Arc::clone(&disk_image), disk_image_id.clone(), cache_type)?;
273+
DiskProperties::new(disk_image.clone(), disk_image_id.clone(), cache_type)?;
278274

279275
let mut avail_features = (1u64 << VIRTIO_F_VERSION_1)
280276
| (1u64 << VIRTIO_BLK_F_FLUSH)

src/devices/src/virtio/block/worker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl BlockWorker {
228228
}
229229
VIRTIO_BLK_T_FLUSH => match self.disk.cache_type() {
230230
CacheType::Writeback => {
231-
let diskfile = self.disk.file();
231+
let diskfile = self.disk.file.lock().unwrap();
232232
diskfile.flush().map_err(RequestError::FlushingToDisk)?;
233233
diskfile.sync().map_err(RequestError::FlushingToDisk)?;
234234
Ok(0)

src/devices/src/virtio/file_traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl FileReadWriteAtVolatile for DiskProperties {
432432
.len()
433433
.try_into()
434434
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
435-
self.file().readv(iovec, offset)?;
435+
self.file.lock().unwrap().readv(iovec, offset)?;
436436
Ok(full_length)
437437
}
438438

@@ -450,7 +450,7 @@ impl FileReadWriteAtVolatile for DiskProperties {
450450
.len()
451451
.try_into()
452452
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
453-
self.file().writev(iovec, offset)?;
453+
self.file.lock().unwrap().writev(iovec, offset)?;
454454
Ok(full_length)
455455
}
456456
}

0 commit comments

Comments
 (0)