123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761 |
- pub mod core;
- pub mod fcntl;
- pub mod file;
- pub mod mount;
- pub mod open;
- pub mod syscall;
- mod utils;
- use ::core::{any::Any, fmt::Debug, sync::atomic::AtomicUsize};
- use alloc::{string::String, sync::Arc, vec::Vec};
- use intertrait::CastFromSync;
- use system_error::SystemError;
- use crate::{
- driver::base::{
- block::block_device::BlockDevice, char::CharDevice, device::device_number::DeviceNumber,
- },
- ipc::pipe::LockedPipeInode,
- libs::{
- casting::DowncastArc,
- spinlock::{SpinLock, SpinLockGuard},
- },
- time::PosixTimeSpec,
- };
- use self::{core::generate_inode_id, file::FileMode, syscall::ModeType};
- pub use self::{core::ROOT_INODE, file::FilePrivateData, mount::MountFS};
- pub const MAX_PATHLEN: usize = 1024;
- int_like!(InodeId, AtomicInodeId, usize, AtomicUsize);
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
- pub enum FileType {
-
- File,
-
- Dir,
-
- BlockDevice,
-
- CharDevice,
-
- FramebufferDevice,
-
- KvmDevice,
-
- Pipe,
-
- SymLink,
-
- Socket,
- }
- #[allow(dead_code)]
- #[derive(Debug, Clone)]
- pub enum SpecialNodeData {
-
- Pipe(Arc<LockedPipeInode>),
-
- CharDevice(Arc<dyn CharDevice>),
-
- BlockDevice(Arc<dyn BlockDevice>),
- }
- #[allow(dead_code)]
- pub const DT_UNKNOWN: u16 = 0;
- pub const DT_FIFO: u16 = 1;
- pub const DT_CHR: u16 = 2;
- pub const DT_DIR: u16 = 4;
- pub const DT_BLK: u16 = 6;
- pub const DT_REG: u16 = 8;
- pub const DT_LNK: u16 = 10;
- pub const DT_SOCK: u16 = 12;
- #[allow(dead_code)]
- pub const DT_WHT: u16 = 14;
- #[allow(dead_code)]
- pub const DT_MAX: u16 = 16;
- pub const VFS_MAX_FOLLOW_SYMLINK_TIMES: usize = 8;
- impl FileType {
- pub fn get_file_type_num(&self) -> u16 {
- return match self {
- FileType::File => DT_REG,
- FileType::Dir => DT_DIR,
- FileType::BlockDevice => DT_BLK,
- FileType::CharDevice => DT_CHR,
- FileType::KvmDevice => DT_CHR,
- FileType::Pipe => DT_FIFO,
- FileType::SymLink => DT_LNK,
- FileType::Socket => DT_SOCK,
- FileType::FramebufferDevice => DT_CHR,
- };
- }
- }
- bitflags! {
-
- pub struct PollStatus: u8 {
- const WRITE = 1u8 << 0;
- const READ = 1u8 << 1;
- const ERROR = 1u8 << 2;
- }
- }
- pub trait IndexNode: Any + Sync + Send + Debug + CastFromSync {
-
-
-
-
- fn open(
- &self,
- _data: SpinLockGuard<FilePrivateData>,
- _mode: &FileMode,
- ) -> Result<(), SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
- fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
-
-
-
- fn read_at(
- &self,
- offset: usize,
- len: usize,
- buf: &mut [u8],
- _data: SpinLockGuard<FilePrivateData>,
- ) -> Result<usize, SystemError>;
-
-
-
-
-
-
-
-
-
- fn write_at(
- &self,
- offset: usize,
- len: usize,
- buf: &[u8],
- _data: SpinLockGuard<FilePrivateData>,
- ) -> Result<usize, SystemError>;
-
-
-
- fn poll(&self, _private_data: &FilePrivateData) -> Result<usize, SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
- fn metadata(&self) -> Result<Metadata, SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
- fn set_metadata(&self, _metadata: &Metadata) -> Result<(), SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
-
- fn resize(&self, _len: usize) -> Result<(), SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
-
-
- fn create(
- &self,
- name: &str,
- file_type: FileType,
- mode: ModeType,
- ) -> Result<Arc<dyn IndexNode>, SystemError> {
-
- return self.create_with_data(name, file_type, mode, 0);
- }
-
-
-
-
-
-
-
-
-
- fn create_with_data(
- &self,
- _name: &str,
- _file_type: FileType,
- _mode: ModeType,
- _data: usize,
- ) -> Result<Arc<dyn IndexNode>, SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
-
- fn link(&self, _name: &str, _other: &Arc<dyn IndexNode>) -> Result<(), SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
- fn unlink(&self, _name: &str) -> Result<(), SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
- fn rmdir(&self, _name: &str) -> Result<(), SystemError> {
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
-
-
-
-
- fn move_to(
- &self,
- _old_name: &str,
- _target: &Arc<dyn IndexNode>,
- _new_name: &str,
- ) -> Result<(), SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- fn rename(&self, _old_name: &str, _new_name: &str) -> Result<(), SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
- fn find(&self, _name: &str) -> Result<Arc<dyn IndexNode>, SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
- fn get_entry_name(&self, _ino: InodeId) -> Result<String, SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
-
-
-
- fn get_entry_name_and_metadata(&self, ino: InodeId) -> Result<(String, Metadata), SystemError> {
-
- let name = self.get_entry_name(ino)?;
- let entry = self.find(&name)?;
- return Ok((name, entry.metadata()?));
- }
-
-
-
-
-
-
-
- fn ioctl(
- &self,
- _cmd: u32,
- _data: usize,
- _private_data: &FilePrivateData,
- ) -> Result<usize, SystemError> {
-
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
- fn fs(&self) -> Arc<dyn FileSystem>;
-
-
- fn as_any_ref(&self) -> &dyn Any;
-
- fn list(&self) -> Result<Vec<String>, SystemError>;
-
-
- fn mount(&self, _fs: Arc<dyn FileSystem>) -> Result<Arc<MountFS>, SystemError> {
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
-
-
- fn truncate(&self, _len: usize) -> Result<(), SystemError> {
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
- fn sync(&self) -> Result<(), SystemError> {
- return Ok(());
- }
-
-
-
- fn mknod(
- &self,
- _filename: &str,
- _mode: ModeType,
- _dev_t: DeviceNumber,
- ) -> Result<Arc<dyn IndexNode>, SystemError> {
- return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
- }
-
- fn special_node(&self) -> Option<SpecialNodeData> {
- None
- }
- }
- impl DowncastArc for dyn IndexNode {
- fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any> {
- self
- }
- }
- impl dyn IndexNode {
-
-
- pub fn downcast_ref<T: IndexNode>(&self) -> Option<&T> {
- return self.as_any_ref().downcast_ref::<T>();
- }
-
-
-
-
-
-
- pub fn lookup(&self, path: &str) -> Result<Arc<dyn IndexNode>, SystemError> {
- return self.lookup_follow_symlink(path, 0);
- }
-
-
-
-
-
-
-
- pub fn lookup_follow_symlink(
- &self,
- path: &str,
- max_follow_times: usize,
- ) -> Result<Arc<dyn IndexNode>, SystemError> {
- if self.metadata()?.file_type != FileType::Dir {
- return Err(SystemError::ENOTDIR);
- }
-
-
-
- let (mut result, mut rest_path) = if let Some(rest) = path.strip_prefix('/') {
- (ROOT_INODE().clone(), String::from(rest))
- } else {
-
- (self.find(".")?, String::from(path))
- };
-
- while !rest_path.is_empty() {
-
- if result.metadata()?.file_type != FileType::Dir {
- return Err(SystemError::ENOTDIR);
- }
- let name;
-
- match rest_path.find('/') {
- Some(pos) => {
-
- name = String::from(&rest_path[0..pos]);
-
- rest_path = String::from(&rest_path[pos + 1..]);
- }
- None => {
- name = rest_path;
- rest_path = String::new();
- }
- }
-
- if name.is_empty() {
- continue;
- }
- let inode = result.find(&name)?;
-
- if inode.metadata()?.file_type == FileType::SymLink && max_follow_times > 0 {
- let mut content = [0u8; 256];
-
- let len = inode.read_at(
- 0,
- 256,
- &mut content,
- SpinLock::new(FilePrivateData::Unused).lock(),
- )?;
-
- let link_path = String::from(
- ::core::str::from_utf8(&content[..len]).map_err(|_| SystemError::ENOTDIR)?,
- );
- let new_path = link_path + "/" + &rest_path;
-
- return result.lookup_follow_symlink(&new_path, max_follow_times - 1);
- } else {
- result = inode;
- }
- }
- return Ok(result);
- }
- }
- #[derive(Debug, PartialEq, Eq, Clone)]
- pub struct Metadata {
-
- pub dev_id: usize,
-
- pub inode_id: InodeId,
-
-
-
- pub size: i64,
-
- pub blk_size: usize,
-
- pub blocks: usize,
-
- pub atime: PosixTimeSpec,
-
- pub mtime: PosixTimeSpec,
-
- pub ctime: PosixTimeSpec,
-
- pub file_type: FileType,
-
- pub mode: ModeType,
-
- pub nlinks: usize,
-
- pub uid: usize,
-
- pub gid: usize,
-
- pub raw_dev: DeviceNumber,
- }
- impl Default for Metadata {
- fn default() -> Self {
- return Self {
- dev_id: 0,
- inode_id: InodeId::new(0),
- size: 0,
- blk_size: 0,
- blocks: 0,
- atime: PosixTimeSpec::default(),
- mtime: PosixTimeSpec::default(),
- ctime: PosixTimeSpec::default(),
- file_type: FileType::File,
- mode: ModeType::empty(),
- nlinks: 1,
- uid: 0,
- gid: 0,
- raw_dev: DeviceNumber::default(),
- };
- }
- }
- #[derive(Debug, Clone)]
- pub struct SuperBlock {
-
- pub magic: Magic,
-
- pub bsize: u64,
-
- pub blocks: u64,
-
- pub bfree: u64,
-
- pub bavail: u64,
-
- pub files: u64,
-
- pub ffree: u64,
-
- pub fsid: u64,
-
- pub namelen: u64,
-
- pub frsize: u64,
-
- pub flags: u64,
- }
- impl SuperBlock {
- pub fn new(magic: Magic, bsize: u64, namelen: u64) -> Self {
- Self {
- magic,
- bsize,
- blocks: 0,
- bfree: 0,
- bavail: 0,
- files: 0,
- ffree: 0,
- fsid: 0,
- namelen,
- frsize: 0,
- flags: 0,
- }
- }
- }
- bitflags! {
- pub struct Magic: u64 {
- const DEVFS_MAGIC = 0x1373;
- const FAT_MAGIC = 0xf2f52011;
- const KER_MAGIC = 0x3153464b;
- const PROC_MAGIC = 0x9fa0;
- const RAMFS_MAGIC = 0x858458f6;
- const MOUNT_MAGIC = 61267;
- }
- }
- pub trait FileSystem: Any + Sync + Send + Debug {
-
- fn root_inode(&self) -> Arc<dyn IndexNode>;
-
- fn info(&self) -> FsInfo;
-
-
- fn as_any_ref(&self) -> &dyn Any;
- fn name(&self) -> &str;
- fn super_block(&self) -> SuperBlock;
- }
- impl DowncastArc for dyn FileSystem {
- fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any> {
- self
- }
- }
- #[derive(Debug)]
- pub struct FsInfo {
-
- pub blk_dev_id: usize,
-
- pub max_name_len: usize,
- }
- #[repr(C)]
- #[derive(Debug)]
- pub struct Dirent {
- d_ino: u64,
- d_off: i64,
- d_reclen: u16,
- d_type: u8,
- d_name: u8,
- }
- impl Metadata {
- pub fn new(file_type: FileType, mode: ModeType) -> Self {
- Metadata {
- dev_id: 0,
- inode_id: generate_inode_id(),
- size: 0,
- blk_size: 0,
- blocks: 0,
- atime: PosixTimeSpec::default(),
- mtime: PosixTimeSpec::default(),
- ctime: PosixTimeSpec::default(),
- file_type,
- mode,
- nlinks: 1,
- uid: 0,
- gid: 0,
- raw_dev: DeviceNumber::default(),
- }
- }
- }
- pub struct FileSystemMaker {
- function: &'static FileSystemNewFunction,
- name: &'static str,
- }
- impl FileSystemMaker {
- pub const fn new(
- name: &'static str,
- function: &'static FileSystemNewFunction,
- ) -> FileSystemMaker {
- FileSystemMaker { function, name }
- }
- pub fn call(&self) -> Result<Arc<dyn FileSystem>, SystemError> {
- (self.function)()
- }
- }
- pub type FileSystemNewFunction = fn() -> Result<Arc<dyn FileSystem>, SystemError>;
- #[macro_export]
- macro_rules! define_filesystem_maker_slice {
- ($name:ident) => {
- #[::linkme::distributed_slice]
- pub static $name: [FileSystemMaker] = [..];
- };
- () => {
- compile_error!("define_filesystem_maker_slice! requires at least one argument: slice_name");
- };
- }
- #[macro_export]
- macro_rules! producefs {
- ($initializer_slice:ident,$filesystem:ident) => {
- match $initializer_slice.iter().find(|&m| m.name == $filesystem) {
- Some(maker) => maker.call(),
- None => {
- kerror!("mismatch filesystem type : {}", $filesystem);
- Err(SystemError::EINVAL)
- }
- }
- };
- }
- define_filesystem_maker_slice!(FSMAKER);
|