Skip to content

虚拟文件系统层 #299

@Josen-B

Description

@Josen-B

继续沿用 ArceOS 中的 axfs_vfs,axfs_vfs 提供了统一的虚拟文件系统(VFS)抽象层。该抽象层将节点操作和文件系统操作分别抽象为两个 Trait:VfsNodeOps VfsOps。具体的底层文件系统(如 FAT32、EXT4)通过各自的实现完成 axfs_vfs 所规定的接口。当用户发起文件系统调用时,VFS 会自动分发到相应的底层实现,使用户无需关心实际使用的是哪种文件系统。这一设计基本满足当前 AxVisor 中的文件系统访问需求。

axfs_vfs - 虚拟文件系统抽象层

1. 核心 Traits

1.1 VfsOps Trait

定义了文件系统级别的操作:

pub trait VfsOps: Send + Sync {
    fn mount(&self, _path: &str, _mount_point: VfsNodeRef) -> VfsResult;
    fn umount(&self) -> VfsResult;
    fn format(&self) -> VfsResult;
    fn statfs(&self) -> VfsResult<FileSystemInfo>;
    fn root_dir(&self) -> VfsNodeRef;
}
  • mount(): 文件系统挂载时的操作
  • umount(): 文件系统卸载时的操作
  • format(): 格式化文件系统
  • statfs(): 获取文件系统属性
  • root_dir(): 获取根目录节点
1.2 VfsNodeOps Trait

定义了文件/目录节点的操作:

pub trait VfsNodeOps: Send + Sync {
    // 通用操作
    fn open(&self) -> VfsResult;
    fn release(&self) -> VfsResult;
    fn get_attr(&self) -> VfsResult<VfsNodeAttr>;
    
    // 文件操作
    fn read_at(&self, offset: u64, buf: &mut [u8]) -> VfsResult<usize>;
    fn write_at(&self, offset: u64, buf: &[u8]) -> VfsResult<usize>;
    fn fsync(&self) -> VfsResult;
    fn truncate(&self, size: u64) -> VfsResult;
    
    // 目录操作
    fn parent(&self) -> Option<VfsNodeRef>;
    fn lookup(self: Arc<Self>, path: &str) -> VfsResult<VfsNodeRef>;
    fn create(&self, path: &str, ty: VfsNodeType) -> VfsResult;
    fn remove(&self, path: &str) -> VfsResult;
    fn read_dir(&self, start_idx: usize, dirents: &mut [VfsDirEntry]) -> VfsResult<usize>;
    fn rename(&self, src_path: &str, dst_path: &str) -> VfsResult;
}

2. 数据结构

2.1 VfsNodeAttr

文件/目录节点属性:

pub struct VfsNodeAttr {
    mode: VfsNodePerm,    // 权限模式
    ty: VfsNodeType,      // 节点类型
    size: u64,            // 大小(字节)
    blocks: u64,          // 占用块数
}
2.2 VfsNodeType

节点类型枚举:

pub enum VfsNodeType {
    Fifo = 0o1,           // 命名管道
    CharDevice = 0o2,     // 字符设备
    Dir = 0o4,            // 目录
    BlockDevice = 0o6,    // 块设备
    File = 0o10,          // 普通文件
    SymLink = 0o12,       // 符号链接
    Socket = 0o14,       // 套接字
}
2.3 VfsNodePerm

权限位标志:

bitflags::bitflags! {
    pub struct VfsNodePerm: u16 {
        const OWNER_READ = 0o400;
        const OWNER_WRITE = 0o200;
        const OWNER_EXEC = 0o100;
        const GROUP_READ = 0o40;
        const GROUP_WRITE = 0o20;
        const GROUP_EXEC = 0o10;
        const OTHER_READ = 0o4;
        const OTHER_WRITE = 0o2;
        const OTHER_EXEC = 0o1;
    }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions