Skip to content

Commit b32e080

Browse files
committed
mmap: support for devdax and block devices
metadata.len() returns 0 when trying to mmap files such as block devices and devdax (character devices) that are not regular files, hence returning MappingPastEof even if the mapping would fit at the provided file_offset. This patch adds support for checking the size of devdax and block devices, and returns a new error, InvalidFileType, if the mmap being created is not for a regular file, block or devdax device, or if the size the devices couldn't be found in sysfs. Usecase: Devdax and block devices can be used in cloud-hypervisor as memory-zone. MmapRegion::build from vm-memory is called while creating a GuestRegionMmap for the VM memory-zone. Reviewed-by: Fam Zheng <[email protected]> Signed-off-by: Usama Arif <[email protected]>
1 parent f6ef1b6 commit b32e080

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/mmap.rs

+59-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
use std::borrow::Borrow;
1616
use std::error;
1717
use std::fmt;
18+
use std::fs::{read_link, read_to_string, File};
1819
use std::io::{Read, Write};
1920
use std::ops::Deref;
21+
use std::os::unix::fs::FileTypeExt;
22+
use std::os::unix::io::AsRawFd;
2023
use std::result;
2124
use std::sync::atomic::Ordering;
2225
use std::sync::Arc;
@@ -106,6 +109,56 @@ impl fmt::Display for Error {
106109

107110
impl error::Error for Error {}
108111

112+
#[cfg(unix)]
113+
/// Return the size of `file`.
114+
///
115+
/// For regular files this returns the files' metadata.length().
116+
/// For devdax and block devices, the size is checked in sysfs.
117+
fn calculate_file_size(file: &File) -> Option<u64> {
118+
let metadata = file.metadata().ok()?;
119+
let file_type = metadata.file_type();
120+
121+
if file_type.is_file() {
122+
return Some(metadata.len());
123+
}
124+
125+
let mut device_size_multiplier = 0;
126+
let mut sys_fs_location = "";
127+
let proc_file_name = format!("/proc/self/fd/{}", file.as_raw_fd());
128+
let file_name = read_link(proc_file_name).ok()?;
129+
130+
if file_type.is_block_device() {
131+
// block device size are reported as 512 byte blocks.
132+
device_size_multiplier = 512;
133+
sys_fs_location = "/sys/block";
134+
} else if file_type.is_char_device() {
135+
if let Some(file_name) = file_name.to_str() {
136+
if file_name.starts_with("/dev/dax") {
137+
device_size_multiplier = 1;
138+
sys_fs_location = "/sys/bus/dax/devices";
139+
} else {
140+
return None;
141+
}
142+
}
143+
} else {
144+
return None;
145+
}
146+
147+
if let Some(file_name) = file_name.to_str() {
148+
if let Some(device_name) = file_name.split('/').last() {
149+
if let Ok(device_size_str) =
150+
read_to_string(format!("{}/{}/size", sys_fs_location, device_name))
151+
{
152+
if let Ok(device_size_int) = device_size_str.trim().parse::<u64>() {
153+
return Some(device_size_int * device_size_multiplier);
154+
}
155+
}
156+
}
157+
}
158+
159+
None
160+
}
161+
109162
// TODO: use this for Windows as well after we redefine the Error type there.
110163
#[cfg(unix)]
111164
/// Checks if a mapping of `size` bytes fits at the provided `file_offset`.
@@ -119,14 +172,16 @@ pub fn check_file_offset(
119172
let file = file_offset.file();
120173
let start = file_offset.start();
121174

122-
if let Some(end) = start.checked_add(size as u64) {
123-
if let Ok(metadata) = file.metadata() {
124-
if metadata.len() < end {
175+
if let Some(file_size) = calculate_file_size(file) {
176+
if let Some(end) = start.checked_add(size as u64) {
177+
if file_size < end {
125178
return Err(MmapRegionError::MappingPastEof);
126179
}
180+
} else {
181+
return Err(MmapRegionError::InvalidOffsetLength);
127182
}
128183
} else {
129-
return Err(MmapRegionError::InvalidOffsetLength);
184+
return Err(MmapRegionError::InvalidFileType);
130185
}
131186

132187
Ok(())

src/mmap_unix.rs

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub enum Error {
2929
InvalidOffsetLength,
3030
/// The specified pointer to the mapping is not page-aligned.
3131
InvalidPointer,
32+
/// The specified file type is invalid.
33+
InvalidFileType,
3234
/// The forbidden `MAP_FIXED` flag was specified.
3335
MapFixed,
3436
/// Mappings using the same fd overlap in terms of file offset and length.
@@ -50,6 +52,7 @@ impl fmt::Display for Error {
5052
f,
5153
"The specified pointer to the mapping is not page-aligned",
5254
),
55+
Error::InvalidFileType => write!(f, "The specified file type is invalid"),
5356
Error::MapFixed => write!(f, "The forbidden `MAP_FIXED` flag was specified"),
5457
Error::MappingOverlap => write!(
5558
f,

0 commit comments

Comments
 (0)