From d5cdc78d8eef7aac850cd6a3d2a054af3c00433f Mon Sep 17 00:00:00 2001 From: Jiang Liu Date: Wed, 21 Jun 2023 15:01:44 +0800 Subject: [PATCH] storage: use File instead of RawFd to avoid possible race conditions Use File instead of RawFd in struct LocalDiskBlob to avoid possible race conditions. Signed-off-by: Jiang Liu --- storage/src/backend/localdisk.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/storage/src/backend/localdisk.rs b/storage/src/backend/localdisk.rs index 38e1b5dddf2..64715ae25c6 100644 --- a/storage/src/backend/localdisk.rs +++ b/storage/src/backend/localdisk.rs @@ -47,7 +47,7 @@ impl From for BackendError { #[derive(Debug)] struct LocalDiskBlob { // The file descriptor of the disk - device_fd: i32, + device_file: File, // Start offset of the partition blob_offset: u64, // Length of the partition @@ -77,7 +77,12 @@ impl BlobReader for LocalDiskBlob { .ok_or(LocalDiskError::ReadBlob(msg))?; let sz = std::cmp::min(self.blob_length - offset, buf.len() as u64) as usize; - uio::pread(self.device_fd, &mut buf[..sz], actual_offset as i64).map_err(|e| { + uio::pread( + self.device_file.as_raw_fd(), + &mut buf[..sz], + actual_offset as i64, + ) + .map_err(|e| { let msg = format!( "localdisk: failed to read data from blob {}, {}", self.blob_id, e @@ -120,7 +125,7 @@ impl BlobReader for LocalDiskBlob { return Err(LocalDiskError::ReadBlob(msg).into()); } - readv(self.device_fd, &mut iovec, actual_offset).map_err(|e| { + readv(self.device_file.as_raw_fd(), &mut iovec, actual_offset).map_err(|e| { let msg = format!( "localdisk: failed to read data from blob {}, {}", self.blob_id, e @@ -254,9 +259,10 @@ impl LocalDisk { return Err(einval!(msg)); } + let device_file = self.device_file.try_clone()?; let partition = Arc::new(LocalDiskBlob { blob_id: name.clone(), - device_fd: self.device_file.as_raw_fd(), + device_file, blob_offset: base_offset, blob_length: length, metrics: self.metrics.clone(),