use crate::errno;
use crate::serial_println;
use crate::syscall::user_ptr;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use lazy_static::lazy_static;
use spin::Mutex;
use crate::fs::{
BlkFsMountMode, BlkFsMountOptions, BlockCache, BlockDeviceHandle, DummyBlockDev, File,
FS_MANAGER, GLOBAL_FD_TABLE,
};
pub mod open_flags {
pub const O_RDONLY: usize = 0;
pub const O_WRONLY: usize = 1;
pub const O_RDWR: usize = 2;
pub const O_CREAT: usize = 0x40;
pub const O_TRUNC: usize = 0x200;
pub const O_APPEND: usize = 0x400;
}
lazy_static! {
static ref SYSCALL_MOUNT_DEVICES: Mutex<Vec<MountDeviceSlot>> = Mutex::new(Vec::new());
}
const DEFAULT_DUMMY_MOUNT_BLOCKS: usize = crate::fs::BLKFS_MIN_BLOCKS * 2;
const MAX_DUMMY_MOUNT_BLOCKS: usize = crate::fs::BLKFS_MIN_BLOCKS * 64;
const MAX_SYSCALL_MOUNT_DEVICE_SLOTS: usize = 16;
#[derive(Clone, Copy, PartialEq, Eq)]
enum MountDeviceSelector {
Auto,
Virtio,
Dummy { blocks: usize },
}
struct MountRequest {
path: String,
device: MountDeviceSelector,
options: BlkFsMountOptions,
}
struct MountDeviceSlot {
mount_path: String,
selector: MountDeviceSelector,
virtio_backed: bool,
dev: BlockDeviceHandle,
}
enum MountDeviceSlotChange {
Unchanged,
Inserted {
mount_path: String,
},
Replaced {
mount_path: String,
selector: MountDeviceSelector,
virtio_backed: bool,
dev: BlockDeviceHandle,
},
}
fn parse_mount_request(raw: &str) -> Result<MountRequest, &'static str> {
let trimmed = raw.trim();
if trimmed.is_empty() {
return Err("Mount request is empty");
}
let (path_part, options_part) = match trimmed.split_once('?') {
Some((path, options)) => (path.trim(), Some(options)),
None => (trimmed, None),
};
if path_part.is_empty() {
return Err("Mount path is empty");
}
let mount_path = normalize_syscall_mount_path(path_part)?;
let mut device = MountDeviceSelector::Auto;
let mut dummy_blocks: Option<usize> = None;
let mut options = BlkFsMountOptions::default();
if let Some(option_text) = options_part {
for pair in option_text.split('&') {
if pair.is_empty() {
continue;
}
let (key, value) = pair
.split_once('=')
.ok_or("Mount option must be key=value")?;
match key {
"device" | "source" => {
device = match value {
"auto" => MountDeviceSelector::Auto,
"virtio" | "virtio-blk" => MountDeviceSelector::Virtio,
"dummy" | "ram" => MountDeviceSelector::Dummy {
blocks: DEFAULT_DUMMY_MOUNT_BLOCKS,
},
_ => return Err("Unsupported mount device"),
};
}
"blocks" => {
dummy_blocks = Some(parse_dummy_blocks(value)?);
}
"mode" => {
options.mode = parse_mount_mode(value)?;
}
"format" => {
options.mode = parse_format_option(value)?;
}
_ => return Err("Unsupported mount option"),
}
}
}
if let Some(blocks) = dummy_blocks {
device = match device {
MountDeviceSelector::Auto | MountDeviceSelector::Dummy { .. } => {
MountDeviceSelector::Dummy { blocks }
}
MountDeviceSelector::Virtio => {
return Err("blocks option requires dummy mount device");
}
};
}
Ok(MountRequest {
path: mount_path,
device,
options,
})
}
fn normalize_syscall_mount_path(path: &str) -> Result<String, &'static str> {
let trimmed = path.trim();
if !trimmed.starts_with('/') {
return Err("Mount path must be absolute");
}
let dir_name = trimmed.trim_matches('/');
if dir_name.is_empty() {
return Err("Cannot mount over root");
}
if dir_name.contains('/') {
return Err("Nested mount paths are not supported");
}
let mut mount_path = String::from("/");
mount_path.push_str(dir_name);
Ok(mount_path)
}
fn parse_dummy_blocks(raw: &str) -> Result<usize, &'static str> {
let blocks = raw
.parse::<usize>()
.map_err(|_| "Invalid dummy block count")?;
if blocks < crate::fs::BLKFS_MIN_BLOCKS {
return Err("Dummy block device is too small for BlkFS");
}
if blocks > MAX_DUMMY_MOUNT_BLOCKS {
return Err("Dummy block device is too large for teaching mount");
}
Ok(blocks)
}
fn parse_mount_mode(raw: &str) -> Result<BlkFsMountMode, &'static str> {
match raw {
"open-or-create" | "auto" => Ok(BlkFsMountMode::OpenOrCreate),
"open-only" | "open" | "ro-open" => Ok(BlkFsMountMode::OpenOnly),
"format" | "create" => Ok(BlkFsMountMode::Format),
_ => Err("Unsupported BlkFS mount mode"),
}
}
fn parse_format_option(raw: &str) -> Result<BlkFsMountMode, &'static str> {
match raw {
"always" | "true" | "1" => Ok(BlkFsMountMode::Format),
"never" | "false" | "0" => Ok(BlkFsMountMode::OpenOnly),
"auto" => Ok(BlkFsMountMode::OpenOrCreate),
_ => Err("Unsupported format policy"),
}
}
fn create_mount_device(
selector: MountDeviceSelector,
) -> Result<(BlockDeviceHandle, bool), &'static str> {
let mut virtio_backed = false;
let raw: BlockDeviceHandle = match selector {
MountDeviceSelector::Auto => {
if let Some(virtio) = crate::block::VirtioBlkDev::probe() {
virtio_backed = true;
Arc::new(virtio)
} else {
Arc::new(DummyBlockDev::new(DEFAULT_DUMMY_MOUNT_BLOCKS))
}
}
MountDeviceSelector::Virtio => {
let virtio =
crate::block::VirtioBlkDev::probe().ok_or("VirtIO block device not found")?;
virtio_backed = true;
Arc::new(virtio)
}
MountDeviceSelector::Dummy { blocks } => Arc::new(DummyBlockDev::new(blocks)),
};
Ok((Arc::new(BlockCache::new(raw)), virtio_backed))
}
fn device_for_mount_request(
request: &MountRequest,
) -> Result<(BlockDeviceHandle, bool, MountDeviceSlotChange), &'static str> {
{
let devices = SYSCALL_MOUNT_DEVICES.lock();
if let Some(slot) = devices.iter().find(|slot| slot.mount_path == request.path) {
if slot.selector == request.device {
return Ok((
Arc::clone(&slot.dev),
slot.virtio_backed,
MountDeviceSlotChange::Unchanged,
));
}
} else if devices.len() >= MAX_SYSCALL_MOUNT_DEVICE_SLOTS {
return Err("Too many syscall-managed mount devices");
}
}
let (dev, virtio_backed) = create_mount_device(request.device)?;
let mut devices = SYSCALL_MOUNT_DEVICES.lock();
if let Some(slot) = devices
.iter_mut()
.find(|slot| slot.mount_path == request.path)
{
if slot.selector == request.device {
return Ok((
Arc::clone(&slot.dev),
slot.virtio_backed,
MountDeviceSlotChange::Unchanged,
));
}
let change = MountDeviceSlotChange::Replaced {
mount_path: request.path.clone(),
selector: slot.selector,
virtio_backed: slot.virtio_backed,
dev: Arc::clone(&slot.dev),
};
slot.selector = request.device;
slot.virtio_backed = virtio_backed;
slot.dev = Arc::clone(&dev);
return Ok((dev, virtio_backed, change));
}
if devices.len() >= MAX_SYSCALL_MOUNT_DEVICE_SLOTS {
return Err("Too many syscall-managed mount devices");
}
devices.push(MountDeviceSlot {
mount_path: request.path.clone(),
selector: request.device,
virtio_backed,
dev: Arc::clone(&dev),
});
Ok((
dev,
virtio_backed,
MountDeviceSlotChange::Inserted {
mount_path: request.path.clone(),
},
))
}
fn rollback_mount_device_slot(change: MountDeviceSlotChange) {
match change {
MountDeviceSlotChange::Unchanged => {}
MountDeviceSlotChange::Inserted { mount_path } => {
let mut devices = SYSCALL_MOUNT_DEVICES.lock();
devices.retain(|slot| slot.mount_path != mount_path);
}
MountDeviceSlotChange::Replaced {
mount_path,
selector,
virtio_backed,
dev,
} => {
let mut devices = SYSCALL_MOUNT_DEVICES.lock();
if let Some(slot) = devices
.iter_mut()
.find(|slot| slot.mount_path == mount_path)
{
slot.selector = selector;
slot.virtio_backed = virtio_backed;
slot.dev = dev;
} else if devices.len() < MAX_SYSCALL_MOUNT_DEVICE_SLOTS {
devices.push(MountDeviceSlot {
mount_path,
selector,
virtio_backed,
dev,
});
}
}
}
}
fn effective_mount_options(options: BlkFsMountOptions, virtio_backed: bool) -> BlkFsMountOptions {
if virtio_backed && options.mode != BlkFsMountMode::Format {
BlkFsMountOptions::open_only()
} else {
options
}
}
fn file_for_fd(fd: usize, syscall: &str) -> Result<Arc<Mutex<dyn File>>, errno::Errno> {
let fd_table = GLOBAL_FD_TABLE.lock();
match fd_table.get(fd) {
Some(file) => Ok(file),
None => {
serial_println!("[SYSCALL] {}: invalid fd={}", syscall, fd);
Err(errno::EBADF)
}
}
}
pub fn read_c_string(ptr: usize) -> Option<String> {
user_ptr::read_raw_c_string(ptr).ok()
}
pub fn sys_read(fd: usize, buf: usize, len: usize) -> isize {
serial_println!(
"[SYSCALL] sys_read: fd={}, buf=0x{:x}, len={}",
fd,
buf,
len
);
if len == 0 {
return 0;
}
if buf == 0 {
return errno::EINVAL;
}
if fd == 0 {
let ch = loop {
if let Some(c) = crate::task::keyboard::try_pop_char() {
break c;
}
let ret: isize;
unsafe {
core::arch::asm!(
"li a7, 2",
"ecall",
"mv {}, a0",
out(reg) ret,
options(nostack)
);
}
if ret >= 0 {
break ret as u8;
}
riscv::asm::wfi();
};
if user_ptr::raw_copy_to_user(buf, &[ch]).is_err() {
return errno::EFAULT;
}
return 1;
}
let file = match file_for_fd(fd, "sys_read") {
Ok(file) => file,
Err(errno) => return errno,
};
let capped = core::cmp::min(len, user_ptr::MAX_USER_COPY_LEN);
let mut kernel_buf = alloc::vec![0u8; capped];
let n = match file.lock().read(&mut kernel_buf) {
Ok(n) => n,
Err(e) => {
serial_println!("[SYSCALL] sys_read: read failed: {:?}", e);
return errno::from_file_error(e);
}
};
if user_ptr::raw_copy_to_user(buf, &kernel_buf[..n]).is_err() {
return errno::EFAULT;
}
serial_println!("[SYSCALL] sys_read: success, read {} bytes", n);
n as isize
}
pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
serial_println!(
"[SYSCALL] sys_write: fd={}, buf=0x{:x}, len={}",
fd,
buf as usize,
len
);
if len == 0 {
return 0;
}
if buf.is_null() {
return errno::EINVAL;
}
if fd == 1 || fd == 2 {
let kernel_buf = match user_ptr::raw_copy_from_user(buf, len) {
Ok(bytes) => bytes,
Err(errno) => return errno,
};
crate::serial::write_bytes(&kernel_buf);
return kernel_buf.len() as isize;
}
let file = match file_for_fd(fd, "sys_write") {
Ok(file) => file,
Err(errno) => return errno,
};
let kernel_buf = match user_ptr::raw_copy_from_user(buf, len) {
Ok(bytes) => bytes,
Err(errno) => return errno,
};
let n = match file.lock().write(&kernel_buf) {
Ok(n) => n,
Err(e) => {
serial_println!("[SYSCALL] sys_write: write failed: {:?}", e);
return errno::from_file_error(e);
}
};
serial_println!("[SYSCALL] sys_write: success, wrote {} bytes", n);
n as isize
}
pub fn sys_open(path: usize, flags: usize) -> isize {
serial_println!("[SYSCALL] sys_open: path=0x{:x}, flags=0x{:x}", path, flags);
let path_str = match read_c_string(path) {
Some(s) => s,
None => {
serial_println!("[SYSCALL] sys_open: invalid path pointer");
return errno::EFAULT;
}
};
serial_println!("[SYSCALL] sys_open: resolved path='{}'", path_str);
let file: Arc<Mutex<dyn File>> = match crate::fs::vfs::open_file(&path_str) {
Ok(f) => f,
Err(_) if (flags & open_flags::O_CREAT) != 0 => {
match crate::fs::vfs::create_file(&path_str) {
Ok(f) => f,
Err(e) => {
serial_println!("[SYSCALL] sys_open: create failed: {:?}", e);
return errno::from_file_error(e);
}
}
}
Err(e) => {
serial_println!("[SYSCALL] sys_open: open failed: {:?}", e);
return errno::from_file_error(e);
}
};
let fd = match GLOBAL_FD_TABLE.lock().alloc(file) {
Some(fd) => fd,
None => {
serial_println!("[SYSCALL] sys_open: no free fd");
return errno::ENFILE;
}
};
serial_println!("[SYSCALL] sys_open: success, fd={}, path={}", fd, path_str);
fd as isize
}
pub fn sys_close(fd: usize) -> isize {
serial_println!("[SYSCALL] sys_close: fd={}", fd);
if GLOBAL_FD_TABLE.lock().dealloc(fd) {
serial_println!("[SYSCALL] sys_close: success");
0
} else {
serial_println!("[SYSCALL] sys_close: invalid fd={}", fd);
errno::EBADF
}
}
pub fn sys_mkdir(path: usize) -> isize {
serial_println!("[SYSCALL] sys_mkdir: path=0x{:x}", path);
let path_str = match read_c_string(path) {
Some(s) => s,
None => {
serial_println!("[SYSCALL] sys_mkdir: invalid path pointer");
return -1;
}
};
match crate::fs::vfs::create_dir(&path_str) {
Ok(()) => {
serial_println!("[SYSCALL] sys_mkdir: success, created '{}'", path_str);
0
}
Err(e) => {
serial_println!("[SYSCALL] sys_mkdir: failed: {:?}", e);
errno::from_file_error(e)
}
}
}
pub fn sys_exit(exit_code: i32) -> isize {
let _ = exit_code;
sys_process_requires_trap_frame("exit")
}
pub fn sys_getpid() -> isize {
1
}
pub fn sys_process_requires_trap_frame(name: &str) -> isize {
serial_println!(
"[SYSCALL] sys_{} requires trap-frame process path; direct dispatcher is unsupported",
name
);
errno::ENOSYS
}
pub fn sys_fork() -> isize {
sys_process_requires_trap_frame("fork")
}
pub fn sys_exec(path: usize) -> isize {
let _ = path;
sys_process_requires_trap_frame("exec")
}
pub fn sys_wait(exit_code_ptr: usize) -> isize {
let _ = exit_code_ptr;
sys_process_requires_trap_frame("wait")
}
pub fn sys_mount(path: usize) -> isize {
let path_str = match read_c_string(path) {
Some(s) => s,
None => return -1,
};
serial_println!("[SYSCALL] mount: path={}", path_str);
let request = match parse_mount_request(&path_str) {
Ok(request) => request,
Err(e) => {
serial_println!("[SYSCALL] mount: invalid request - {}", e);
return errno::EINVAL;
}
};
let (dev, virtio_backed, slot_change) = match device_for_mount_request(&request) {
Ok(device) => device,
Err(e) => {
serial_println!("[SYSCALL] mount: device selection failed - {}", e);
return errno::EINVAL;
}
};
let options = effective_mount_options(request.options, virtio_backed);
let mut mgr = FS_MANAGER.lock();
match mgr.mount_blkfs_with_options(&request.path, dev, options) {
Ok(()) => {
serial_println!("[SYSCALL] mount: {} mounted", request.path);
0
}
Err(e) => {
rollback_mount_device_slot(slot_change);
serial_println!("[SYSCALL] mount: failed - {}", e);
errno::EINVAL
}
}
}
pub fn sys_umount(path: usize) -> isize {
let path_str = match read_c_string(path) {
Some(s) => s,
None => return -1,
};
serial_println!("[SYSCALL] umount: path={}", path_str);
let mut mgr = FS_MANAGER.lock();
match mgr.umount(&path_str) {
Ok(()) => {
drop(mgr);
serial_println!("[SYSCALL] umount: {} unmounted", path_str);
0
}
Err(e) => {
serial_println!("[SYSCALL] umount: failed - {}", e);
-1
}
}
}
use crate::fs::SeekFrom;
pub fn sys_lseek(fd: usize, offset: isize, whence: usize) -> isize {
let pos = match whence {
0 => SeekFrom::Start(offset as usize),
1 => SeekFrom::Current(offset),
2 => SeekFrom::End(offset),
_ => return -1,
};
let file = match file_for_fd(fd, "sys_lseek") {
Ok(file) => file,
Err(_) => return -1,
};
let result = file.lock().seek(pos);
match result {
Ok(new_offset) => new_offset as isize,
Err(_) => -1,
}
}
pub fn sys_unlink(path: usize) -> isize {
let path_str = match read_c_string(path) {
Some(s) => s,
None => return -1,
};
serial_println!("[SYSCALL] unlink: path={}", path_str);
match crate::fs::remove_file(&path_str) {
Ok(()) => 0,
Err(e) => {
serial_println!("[SYSCALL] unlink: failed - {}", e);
-1
}
}
}
pub fn sys_stat(path: usize, buf: usize) -> isize {
let path_str = match read_c_string(path) {
Some(s) => s,
None => return -1,
};
let meta = match crate::fs::stat_path(&path_str) {
Ok(m) => m,
Err(e) => {
serial_println!("[SYSCALL] stat: failed for {} - {}", path_str, e);
return -1;
}
};
if buf == 0 {
return -1;
}
let st = Stat {
st_dev: 0,
st_ino: 0,
st_mode: meta.mode,
st_nlink: 1,
st_uid: 0,
st_gid: 0,
st_rdev: 0,
__pad1: 0,
st_size: meta.size as u64,
st_blksize: 512,
__pad2: 0,
st_blocks: (meta.size as u64).div_ceil(512),
};
if user_ptr::raw_write_value(buf, st).is_err() {
return -1;
}
0
}
pub fn sys_getdents(fd_or_path: usize, buf: usize, len: usize) -> isize {
if buf == 0 || len == 0 {
return -1;
}
let path_str = match read_c_string(fd_or_path) {
Some(s) => s,
None => return -1,
};
let entries = match crate::fs::readdir_path(&path_str) {
Ok(v) => v,
Err(e) => {
serial_println!("[SYSCALL] getdents: failed for {} - {}", path_str, e);
return -1;
}
};
let mut out = alloc::vec::Vec::new();
for name in &entries {
let line = name.as_bytes();
if out.len() + line.len() + 1 > len {
break;
}
out.extend_from_slice(line);
out.push(b'\n');
}
if user_ptr::raw_copy_to_user(buf, &out).is_err() {
return -1;
}
out.len() as isize
}
pub fn sys_dup2(old_fd: usize, new_fd: usize) -> isize {
let mut fd_table = GLOBAL_FD_TABLE.lock();
match fd_table.dup2(old_fd, new_fd) {
Some(fd) => fd as isize,
None => -1,
}
}
use crate::fs::pipe::make_pipe;
pub fn sys_pipe(fds_ptr: usize) -> isize {
if fds_ptr == 0 {
return -1;
}
let (read_end, write_end) = make_pipe();
let read_arc = alloc::sync::Arc::new(spin::Mutex::new(read_end));
let write_arc = alloc::sync::Arc::new(spin::Mutex::new(write_end));
let mut fd_table = GLOBAL_FD_TABLE.lock();
let fd0 = match fd_table.alloc(read_arc) {
Some(fd) => fd as i32,
None => return -1,
};
let fd1 = match fd_table.alloc(write_arc) {
Some(fd) => fd as i32,
None => {
fd_table.dealloc(fd0 as usize);
return -1;
}
};
drop(fd_table);
if user_ptr::raw_write_values(fds_ptr, &[fd0, fd1]).is_err() {
let mut fd_table = GLOBAL_FD_TABLE.lock();
fd_table.dealloc(fd0 as usize);
fd_table.dealloc(fd1 as usize);
return -1;
}
0
}
#[cfg(test)]
mod tests {
use super::*;
fn clear_mount_devices() {
SYSCALL_MOUNT_DEVICES.lock().clear();
}
fn slot_count() -> usize {
SYSCALL_MOUNT_DEVICES.lock().len()
}
#[test_case]
fn mount_request_normalizes_slot_path_before_device_selection() {
let request = parse_mount_request(" ///mnt/?device=dummy&blocks=512&mode=open-only")
.expect("normalized mount request");
assert_eq!(request.path.as_str(), "/mnt");
assert!(request.device == MountDeviceSelector::Dummy { blocks: 512 });
assert_eq!(request.options.mode, BlkFsMountMode::OpenOnly);
assert_eq!(
parse_mount_request("/mnt/data").err(),
Some("Nested mount paths are not supported")
);
assert_eq!(
parse_mount_request("/").err(),
Some("Cannot mount over root")
);
}
#[test_case]
fn mount_request_blocks_option_is_dummy_only_and_order_independent() {
assert_eq!(
parse_mount_request("/mnt?device=virtio&blocks=512").err(),
Some("blocks option requires dummy mount device")
);
assert_eq!(
parse_mount_request("/mnt?blocks=512&device=virtio").err(),
Some("blocks option requires dummy mount device")
);
let request = parse_mount_request("/mnt?blocks=512&device=dummy").expect("dummy request");
assert!(request.device == MountDeviceSelector::Dummy { blocks: 512 });
let request = parse_mount_request("/mnt?blocks=640").expect("implicit dummy request");
assert!(request.device == MountDeviceSelector::Dummy { blocks: 640 });
}
#[test_case]
fn virtio_backed_mounts_do_not_open_or_create_unless_format_is_explicit() {
assert_eq!(
effective_mount_options(BlkFsMountOptions::open_or_create(), true).mode,
BlkFsMountMode::OpenOnly
);
assert_eq!(
effective_mount_options(BlkFsMountOptions::open_only(), true).mode,
BlkFsMountMode::OpenOnly
);
assert_eq!(
effective_mount_options(BlkFsMountOptions::format(), true).mode,
BlkFsMountMode::Format
);
assert_eq!(
effective_mount_options(BlkFsMountOptions::open_or_create(), false).mode,
BlkFsMountMode::OpenOrCreate
);
}
#[test_case]
fn mount_device_slot_insert_and_replace_changes_can_be_rolled_back() {
clear_mount_devices();
let original =
parse_mount_request("/slot?device=dummy&blocks=512").expect("original request");
let replacement =
parse_mount_request("/slot/?device=dummy&blocks=640").expect("replacement request");
let (_, _, inserted) = device_for_mount_request(&original).expect("insert slot");
assert_eq!(slot_count(), 1);
let (_, _, replaced) = device_for_mount_request(&replacement).expect("replace slot");
assert_eq!(slot_count(), 1);
{
let devices = SYSCALL_MOUNT_DEVICES.lock();
let slot = devices
.iter()
.find(|slot| slot.mount_path == "/slot")
.expect("slot after replacement");
assert!(slot.selector == replacement.device);
}
rollback_mount_device_slot(replaced);
{
let devices = SYSCALL_MOUNT_DEVICES.lock();
let slot = devices
.iter()
.find(|slot| slot.mount_path == "/slot")
.expect("slot after rollback");
assert!(slot.selector == original.device);
}
rollback_mount_device_slot(inserted);
assert_eq!(slot_count(), 0);
clear_mount_devices();
}
#[test_case]
fn mount_device_slots_are_retained_for_remount_and_rollback_is_explicit() {
clear_mount_devices();
let request = parse_mount_request("/norm?device=dummy&blocks=512").expect("request");
let (_, _, inserted) = device_for_mount_request(&request).expect("insert slot");
assert_eq!(slot_count(), 1);
let (_, _, unchanged) = device_for_mount_request(&request).expect("reuse retained slot");
assert_eq!(slot_count(), 1);
assert!(matches!(unchanged, MountDeviceSlotChange::Unchanged));
rollback_mount_device_slot(inserted);
clear_mount_devices();
}
}
#[derive(Clone, Copy)]
#[repr(C, packed)]
struct Stat {
st_dev: u64,
st_ino: u64,
st_mode: u32,
st_nlink: u32,
st_uid: u32,
st_gid: u32,
st_rdev: u64,
__pad1: u64,
st_size: u64,
st_blksize: u32,
__pad2: u32,
st_blocks: u64,
}