refactor: integrate block into vfs

This commit is contained in:
Mark Poliakov 2024-03-13 18:50:26 +02:00
parent 2dc162d1a0
commit d44bda6d62
19 changed files with 11 additions and 200 deletions

28
Cargo.lock generated
View File

@ -721,7 +721,6 @@ dependencies = [
"libk-util",
"log",
"vfs",
"ygg_driver_block",
"yggdrasil-abi",
]
@ -1507,15 +1506,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "uuid"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a"
dependencies = [
"bytemuck",
]
[[package]]
name = "vcpkg"
version = "0.2.15"
@ -1531,7 +1521,6 @@ dependencies = [
"libk-thread",
"libk-util",
"log",
"ygg_driver_block",
"yggdrasil-abi",
]
@ -1761,25 +1750,10 @@ dependencies = [
"static_assertions",
"tock-registers 0.8.1",
"vfs",
"ygg_driver_block",
"ygg_driver_pci",
"yggdrasil-abi",
]
[[package]]
name = "ygg_driver_block"
version = "0.1.0"
dependencies = [
"bytemuck",
"futures-util",
"libk-mm",
"libk-util",
"log",
"static_assertions",
"uuid",
"yggdrasil-abi",
]
[[package]]
name = "ygg_driver_input"
version = "0.1.0"
@ -1834,7 +1808,6 @@ dependencies = [
"static_assertions",
"tock-registers 0.8.1",
"vfs",
"ygg_driver_block",
"ygg_driver_pci",
"yggdrasil-abi",
]
@ -1974,7 +1947,6 @@ dependencies = [
"vmalloc",
"yboot-proto",
"ygg_driver_ahci",
"ygg_driver_block",
"ygg_driver_input",
"ygg_driver_net_core",
"ygg_driver_net_loopback",

View File

@ -29,7 +29,6 @@ kernel-arch = { path = "arch" }
# Drivers
ygg_driver_pci = { path = "driver/bus/pci" }
ygg_driver_usb = { path = "driver/bus/usb" }
ygg_driver_block = { path = "driver/block/core" }
ygg_driver_net_core = { path = "driver/net/core" }
ygg_driver_net_loopback = { path = "driver/net/loopback" }
ygg_driver_virtio_net = { path = "driver/virtio/net", features = ["pci"] }

View File

@ -13,7 +13,6 @@ device-api = { path = "../../../lib/device-api", features = ["derive"] }
vfs = { path = "../../../lib/vfs" }
ygg_driver_pci = { path = "../../bus/pci" }
ygg_driver_block = { path = "../../block/core" }
kernel-fs = { path = "../../fs/kernel-fs" }
log = "0.4.20"

View File

@ -19,7 +19,7 @@ use libk_util::{sync::IrqSafeSpinlock, OneTimeInit};
use port::AhciPort;
use regs::{PortRegs, Regs};
use tock_registers::interfaces::{ReadWriteable, Readable, Writeable};
use ygg_driver_block::{probe_partitions, NgBlockDeviceWrapper};
use vfs::block::{probe_partitions, NgBlockDeviceWrapper};
use ygg_driver_pci::{
device::{PciDeviceInfo, PreferredInterruptMode},
PciCommandRegister, PciConfigurationSpace,

View File

@ -10,7 +10,7 @@ use futures_util::{task::AtomicWaker, Future};
use libk_mm::{address::AsPhysicalAddress, device::DeviceMemoryIo, PageBox};
use libk_util::{sync::IrqSafeSpinlock, waker::QueueWaker, OneTimeInit};
use tock_registers::interfaces::{Readable, Writeable};
use ygg_driver_block::{IoOperation, IoRequest, IoSubmissionId, NgBlockDevice};
use vfs::block::{IoOperation, IoRequest, IoSubmissionId, NgBlockDevice};
use yggdrasil_abi::error::Error;
use crate::{

View File

@ -1,16 +0,0 @@
[package]
name = "ygg_driver_block"
version = "0.1.0"
edition = "2021"
authors = ["Mark Poliakov <mark@alnyan.me>"]
[dependencies]
yggdrasil-abi = { path = "../../../../lib/abi" }
libk-util = { path = "../../../libk/libk-util" }
libk-mm = { path = "../../../libk/libk-mm" }
log = "0.4.20"
futures-util = { version = "0.3.28", default-features = false, features = ["alloc", "async-await"] }
bytemuck = { version = "1.14.0", features = ["derive"] }
static_assertions = "1.1.0"
uuid = { version = "1.6.1", default-features = false, features = ["bytemuck"] }

View File

@ -1,137 +0,0 @@
use core::mem::{size_of, MaybeUninit};
use alloc::{vec, vec::Vec};
use bytemuck::{Pod, Zeroable};
use libk::mem::PageBox;
use static_assertions::const_assert_eq;
use uuid::Uuid;
use yggdrasil_abi::{error::Error, io::DeviceRequest};
use crate::{BlockDevice, NgBlockDevice, NgBlockDeviceWrapper};
pub struct Partition<'a, D: NgBlockDevice + 'a> {
pub device: &'a NgBlockDeviceWrapper<'a, D>,
pub lba_start: u64,
pub lba_end: u64,
}
#[derive(Clone, Copy)]
#[repr(C)]
struct GptHeader {
signature: [u8; 8],
revision: u32,
header_size: u32,
crc32: u32,
_0: u32,
header_lba: u64,
alternate_header_lba: u64,
first_usable_lba: u64,
last_usable_lba: u64,
guid: [u8; 16],
partition_table_lba: u64,
partition_table_len: u32,
partition_table_entry_size: u32,
partition_table_crc32: u32,
_1: [u8; 420],
}
#[derive(Clone, Copy, Zeroable, Pod)]
#[repr(C)]
struct GptEntry {
type_guid: Uuid,
part_guid: Uuid,
lba_start: u64,
lba_end: u64,
attrs: u64,
}
const_assert_eq!(size_of::<GptHeader>(), 512);
impl<'a, D: NgBlockDevice + 'a> Partition<'a, D> {
fn end_byte(&self) -> u64 {
self.lba_end * self.device.block_size
}
fn start_byte(&self) -> u64 {
self.lba_start * self.device.block_size
}
}
impl<'a, D: NgBlockDevice + 'a> BlockDevice for Partition<'a, D> {
fn read(&'static self, pos: u64, buf: &mut [u8]) -> Result<usize, Error> {
if pos >= self.end_byte() {
return Ok(0);
}
let start = self.start_byte() + pos;
let end = core::cmp::min(start + buf.len() as u64, self.end_byte());
let count = (end - start) as usize;
self.device.read(start, &mut buf[..count])
}
fn write(&'static self, pos: u64, buf: &[u8]) -> Result<usize, Error> {
if pos >= self.end_byte() {
return Ok(0);
}
let start = self.start_byte() + pos;
let end = core::cmp::min(start + buf.len() as u64, self.end_byte());
let count = (end - start) as usize;
self.device.write(start, &buf[..count])
}
fn size(&self) -> Result<u64, Error> {
Ok((self.lba_end - self.lba_start) * self.device.block_size)
}
fn device_request(&self, req: &mut DeviceRequest) -> Result<(), Error> {
self.device.device_request(req)
}
}
unsafe fn read_struct_lba<T>(dev: &'static dyn BlockDevice, lba: u64) -> Result<T, Error> {
assert_eq!(size_of::<T>(), 512);
let mut data = MaybeUninit::<T>::uninit();
let buffer = core::slice::from_raw_parts_mut(data.as_mut_ptr() as *mut u8, 512);
dev.read_exact(lba * 512, buffer)?;
Ok(data.assume_init())
}
pub(crate) fn probe_gpt<D: NgBlockDevice + 'static>(
dev: &'static NgBlockDeviceWrapper<'static, D>,
) -> Result<Option<Vec<Partition<'static, D>>>, Error> {
let header = unsafe { read_struct_lba::<GptHeader>(dev, 1) }?;
if &header.signature != b"EFI PART" {
// Not a GPT partition table
return Ok(None);
}
let pt_entsize = header.partition_table_entry_size as usize;
let pt_len = header.partition_table_len as usize;
let mut pt_data = PageBox::new_slice(0, pt_len * pt_entsize)?;
assert!(size_of::<GptEntry>() <= pt_entsize);
dev.read_exact(header.partition_table_lba * 512, &mut pt_data)?;
let mut partitions = vec![];
for i in 0..pt_len {
let pt_entry_data = &pt_data[i * pt_entsize..i * pt_entsize + size_of::<GptEntry>()];
let pt_entry: &GptEntry = bytemuck::from_bytes(pt_entry_data);
if pt_entry.type_guid.is_nil() {
continue;
}
partitions.push(Partition {
device: dev,
lba_start: pt_entry.lba_start,
lba_end: pt_entry.lba_end,
});
}
Ok(Some(partitions))
}

View File

@ -13,7 +13,6 @@ device-api = { path = "../../../lib/device-api", features = ["derive"] }
vfs = { path = "../../../lib/vfs" }
ygg_driver_pci = { path = "../../bus/pci" }
ygg_driver_block = { path = "../../block/core" }
kernel-fs = { path = "../../fs/kernel-fs" }
log = "0.4.20"

View File

@ -5,7 +5,7 @@ use kernel_fs::devfs;
use libk_mm::address::AsPhysicalAddress;
use libk_thread::cpu_index;
use libk_util::waker::QueueWaker;
use ygg_driver_block::{
use vfs::block::{
probe_partitions, IoOperation, IoRequest, IoSubmissionId, NgBlockDevice, NgBlockDeviceWrapper,
};
use yggdrasil_abi::error::Error;

View File

@ -9,6 +9,4 @@ yggdrasil-abi = { path = "../../../../lib/abi" }
vfs = { path = "../../../lib/vfs" }
libk-util = { path = "../../../libk/libk-util" }
ygg_driver_block = { path = "../../block/core" }
log = "0.4.20"

View File

@ -3,8 +3,7 @@ use core::sync::atomic::{AtomicUsize, Ordering};
use alloc::{format, string::String};
use libk_util::OneTimeInit;
use vfs::{impls::MemoryDirectory, CharDevice, Node, NodeFlags, NodeRef};
use ygg_driver_block::BlockDevice;
use vfs::{block::BlockDevice, impls::MemoryDirectory, CharDevice, Node, NodeFlags, NodeRef};
use yggdrasil_abi::error::Error;
/// Describes the kind of a character device

View File

@ -12,7 +12,5 @@ libk-mm = { path = "../../libk/libk-mm" }
libk-util = { path = "../../libk/libk-util" }
libk-thread = { path = "../../libk/libk-thread" }
ygg_driver_block = { path = "../../driver/block/core" }
log = "0.4.20"
futures-util = { version = "0.3.28", default-features = false, features = ["alloc", "async-await"] }

View File

@ -12,7 +12,7 @@ use libk_mm::{address::PhysicalAddress, table::MapAttributes, PageBox, PageProvi
use libk_util::waker::QueueWaker;
use yggdrasil_abi::{error::Error, io::DeviceRequest};
use crate::{
use crate::block::{
request::{IoOperation, IoRequest, IoSubmissionId},
BlockDevice,
};

View File

@ -1,6 +1,4 @@
#![no_std]
extern crate alloc;
#![allow(missing_docs)]
use core::task::{Context, Poll};

View File

@ -1,7 +1,7 @@
use ygg_driver_block::BlockDevice;
use yggdrasil_abi::{error::Error, io::DeviceRequest};
use crate::{
block::BlockDevice,
node::{CommonImpl, NodeRef},
traits::FileReadiness,
};

View File

@ -15,6 +15,8 @@
extern crate alloc;
pub mod block;
pub(crate) mod channel;
pub(crate) mod device;
pub(crate) mod file;

View File

@ -2,7 +2,6 @@ use core::{any::Any, fmt};
use alloc::{boxed::Box, string::String, sync::Arc, vec::Vec};
use libk_util::sync::IrqSafeSpinlock;
use ygg_driver_block::BlockDevice;
use yggdrasil_abi::{
bitflags,
error::Error,
@ -21,6 +20,7 @@ pub use access::AccessToken;
pub use traits::{CommonImpl, DirectoryImpl, RegularImpl, SymlinkImpl};
use crate::{
block::BlockDevice,
device::{BlockDeviceWrapper, CharDevice, CharDeviceWrapper},
PseudoTerminalMaster, PseudoTerminalSlave,
};

View File

@ -15,7 +15,7 @@ use libk_mm::{
};
use libk_thread::thread::Thread;
use libk_util::sync::IrqSafeSpinlock;
use ygg_driver_block::BlockDevice;
use vfs::block::BlockDevice;
use crate::arch::L3;