mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 08:06:32 +00:00
命名管道系统调用以及文件系统兼容特殊文件类型的接口 (#397)
* 修复pipe2在读端或写端关闭后还阻塞问题。 * 实现命名管道机制,增加特殊文件类型兼容普通文件系统的接口。 * 普通文件系统能够适配特殊文件(命名管道等)
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
#![allow(dead_code)]
|
||||
use core::intrinsics::unlikely;
|
||||
use core::{any::Any, fmt::Debug};
|
||||
|
||||
use alloc::{
|
||||
@ -8,6 +9,8 @@ use alloc::{
|
||||
vec::Vec,
|
||||
};
|
||||
|
||||
use crate::filesystem::vfs::SpecialNodeData;
|
||||
use crate::ipc::pipe::LockedPipeInode;
|
||||
use crate::{
|
||||
driver::base::block::{block_device::LBA_SIZE, disk_info::Partition, SeekFrom},
|
||||
filesystem::vfs::{
|
||||
@ -25,6 +28,7 @@ use crate::{
|
||||
time::TimeSpec,
|
||||
};
|
||||
|
||||
use super::entry::FATFile;
|
||||
use super::{
|
||||
bpb::{BiosParameterBlock, FATType},
|
||||
entry::{FATDir, FATDirEntry, FATDirIter, FATEntry},
|
||||
@ -102,6 +106,9 @@ pub struct FATInode {
|
||||
|
||||
/// 根据不同的Inode类型,创建不同的私有字段
|
||||
inode_type: FATDirEntry,
|
||||
|
||||
/// 若该节点是特殊文件节点,该字段则为真正的文件节点
|
||||
special_node: Option<SpecialNodeData>,
|
||||
}
|
||||
|
||||
impl FATInode {
|
||||
@ -196,6 +203,7 @@ impl LockedFATInode {
|
||||
gid: 0,
|
||||
raw_dev: 0,
|
||||
},
|
||||
special_node: None,
|
||||
})));
|
||||
|
||||
inode.0.lock().self_ref = Arc::downgrade(&inode);
|
||||
@ -315,6 +323,7 @@ impl FATFileSystem {
|
||||
gid: 0,
|
||||
raw_dev: 0,
|
||||
},
|
||||
special_node: None,
|
||||
})));
|
||||
|
||||
let result: Arc<FATFileSystem> = Arc::new(FATFileSystem {
|
||||
@ -1565,7 +1574,15 @@ impl IndexNode for LockedFATInode {
|
||||
// 对目标inode上锁,以防更改
|
||||
let target_guard: SpinLockGuard<FATInode> = target.0.lock();
|
||||
// 先从缓存删除
|
||||
guard.children.remove(&name.to_uppercase());
|
||||
let nod = guard.children.remove(&name.to_uppercase());
|
||||
|
||||
// 若删除缓存中为管道的文件,则不需要再到磁盘删除
|
||||
if let Some(_) = nod {
|
||||
let file_type = target_guard.metadata.file_type;
|
||||
if file_type == FileType::Pipe {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let dir = match &guard.inode_type {
|
||||
FATDirEntry::File(_) | FATDirEntry::VolId(_) => {
|
||||
@ -1663,6 +1680,53 @@ impl IndexNode for LockedFATInode {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn mknod(
|
||||
&self,
|
||||
filename: &str,
|
||||
mode: ModeType,
|
||||
_dev_t: crate::driver::base::device::DeviceNumber,
|
||||
) -> Result<Arc<dyn IndexNode>, SystemError> {
|
||||
let mut inode = self.0.lock();
|
||||
if inode.metadata.file_type != FileType::Dir {
|
||||
return Err(SystemError::ENOTDIR);
|
||||
}
|
||||
|
||||
// 判断需要创建的类型
|
||||
if unlikely(mode.contains(ModeType::S_IFREG)) {
|
||||
// 普通文件
|
||||
return Ok(self.create(filename, FileType::File, mode)?);
|
||||
}
|
||||
|
||||
let nod = LockedFATInode::new(
|
||||
inode.fs.upgrade().unwrap(),
|
||||
inode.self_ref.clone(),
|
||||
FATDirEntry::File(FATFile::default()),
|
||||
);
|
||||
|
||||
if mode.contains(ModeType::S_IFIFO) {
|
||||
nod.0.lock().metadata.file_type = FileType::Pipe;
|
||||
// 创建pipe文件
|
||||
let pipe_inode = LockedPipeInode::new();
|
||||
// 设置special_node
|
||||
nod.0.lock().special_node = Some(SpecialNodeData::Pipe(pipe_inode));
|
||||
} else if mode.contains(ModeType::S_IFBLK) {
|
||||
nod.0.lock().metadata.file_type = FileType::BlockDevice;
|
||||
unimplemented!()
|
||||
} else if mode.contains(ModeType::S_IFCHR) {
|
||||
nod.0.lock().metadata.file_type = FileType::CharDevice;
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
inode
|
||||
.children
|
||||
.insert(String::from(filename).to_uppercase(), nod.clone());
|
||||
Ok(nod)
|
||||
}
|
||||
|
||||
fn special_node(&self) -> Option<SpecialNodeData> {
|
||||
self.0.lock().special_node.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for FATFsInfo {
|
||||
|
@ -1,4 +1,5 @@
|
||||
use core::any::Any;
|
||||
use core::intrinsics::unlikely;
|
||||
|
||||
use alloc::{
|
||||
collections::BTreeMap,
|
||||
@ -9,6 +10,7 @@ use alloc::{
|
||||
|
||||
use crate::{
|
||||
filesystem::vfs::{core::generate_inode_id, FileType},
|
||||
ipc::pipe::LockedPipeInode,
|
||||
libs::spinlock::{SpinLock, SpinLockGuard},
|
||||
syscall::SystemError,
|
||||
time::TimeSpec,
|
||||
@ -16,7 +18,7 @@ use crate::{
|
||||
|
||||
use super::vfs::{
|
||||
file::FilePrivateData, syscall::ModeType, FileSystem, FsInfo, IndexNode, InodeId, Metadata,
|
||||
PollStatus,
|
||||
PollStatus, SpecialNodeData,
|
||||
};
|
||||
|
||||
/// RamFS的inode名称的最大长度
|
||||
@ -52,6 +54,8 @@ pub struct RamFSInode {
|
||||
metadata: Metadata,
|
||||
/// 指向inode所在的文件系统对象的指针
|
||||
fs: Weak<RamFS>,
|
||||
/// 指向特殊节点
|
||||
special_node: Option<SpecialNodeData>,
|
||||
}
|
||||
|
||||
impl FileSystem for RamFS {
|
||||
@ -98,6 +102,7 @@ impl RamFS {
|
||||
raw_dev: 0,
|
||||
},
|
||||
fs: Weak::default(),
|
||||
special_node: None,
|
||||
})));
|
||||
|
||||
let result: Arc<RamFS> = Arc::new(RamFS { root_inode: root });
|
||||
@ -296,6 +301,7 @@ impl IndexNode for LockedRamFSInode {
|
||||
raw_dev: data,
|
||||
},
|
||||
fs: inode.fs.clone(),
|
||||
special_node: None,
|
||||
})));
|
||||
|
||||
// 初始化inode的自引用的weak指针
|
||||
@ -476,4 +482,72 @@ impl IndexNode for LockedRamFSInode {
|
||||
|
||||
return Ok(keys);
|
||||
}
|
||||
|
||||
fn mknod(
|
||||
&self,
|
||||
filename: &str,
|
||||
mode: ModeType,
|
||||
_dev_t: crate::driver::base::device::DeviceNumber,
|
||||
) -> Result<Arc<dyn IndexNode>, SystemError> {
|
||||
let mut inode = self.0.lock();
|
||||
if inode.metadata.file_type != FileType::Dir {
|
||||
return Err(SystemError::ENOTDIR);
|
||||
}
|
||||
|
||||
// 判断需要创建的类型
|
||||
if unlikely(mode.contains(ModeType::S_IFREG)) {
|
||||
// 普通文件
|
||||
return Ok(self.create(filename, FileType::File, mode)?);
|
||||
}
|
||||
|
||||
let nod = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode {
|
||||
parent: inode.self_ref.clone(),
|
||||
self_ref: Weak::default(),
|
||||
children: BTreeMap::new(),
|
||||
data: Vec::new(),
|
||||
metadata: Metadata {
|
||||
dev_id: 0,
|
||||
inode_id: generate_inode_id(),
|
||||
size: 0,
|
||||
blk_size: 0,
|
||||
blocks: 0,
|
||||
atime: TimeSpec::default(),
|
||||
mtime: TimeSpec::default(),
|
||||
ctime: TimeSpec::default(),
|
||||
file_type: FileType::Pipe,
|
||||
mode: mode,
|
||||
nlinks: 1,
|
||||
uid: 0,
|
||||
gid: 0,
|
||||
raw_dev: 0,
|
||||
},
|
||||
fs: inode.fs.clone(),
|
||||
special_node: None,
|
||||
})));
|
||||
|
||||
nod.0.lock().self_ref = Arc::downgrade(&nod);
|
||||
|
||||
if mode.contains(ModeType::S_IFIFO) {
|
||||
nod.0.lock().metadata.file_type = FileType::Pipe;
|
||||
// 创建pipe文件
|
||||
let pipe_inode = LockedPipeInode::new();
|
||||
// 设置special_node
|
||||
nod.0.lock().special_node = Some(SpecialNodeData::Pipe(pipe_inode));
|
||||
} else if mode.contains(ModeType::S_IFBLK) {
|
||||
nod.0.lock().metadata.file_type = FileType::BlockDevice;
|
||||
unimplemented!()
|
||||
} else if mode.contains(ModeType::S_IFCHR) {
|
||||
nod.0.lock().metadata.file_type = FileType::CharDevice;
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
inode
|
||||
.children
|
||||
.insert(String::from(filename).to_uppercase(), nod.clone());
|
||||
Ok(nod)
|
||||
}
|
||||
|
||||
fn special_node(&self) -> Option<super::vfs::SpecialNodeData> {
|
||||
return self.0.lock().special_node.clone();
|
||||
}
|
||||
}
|
||||
|
@ -15,12 +15,11 @@ use crate::{
|
||||
sysfs::sysfs_init,
|
||||
vfs::{mount::MountFS, syscall::ModeType, AtomicInodeId, FileSystem, FileType},
|
||||
},
|
||||
include::bindings::bindings::PAGE_4K_SIZE,
|
||||
kdebug, kerror, kinfo,
|
||||
syscall::SystemError,
|
||||
};
|
||||
|
||||
use super::{file::FileMode, utils::rsplit_path, IndexNode, InodeId};
|
||||
use super::{file::FileMode, utils::rsplit_path, IndexNode, InodeId, MAX_PATHLEN};
|
||||
|
||||
/// @brief 原子地生成新的Inode号。
|
||||
/// 请注意,所有的inode号都需要通过该函数来生成.全局的inode号,除了以下两个特殊的以外,都是唯一的
|
||||
@ -177,7 +176,7 @@ pub fn mount_root_fs() -> Result<(), SystemError> {
|
||||
/// @brief 创建文件/文件夹
|
||||
pub fn do_mkdir(path: &str, _mode: FileMode) -> Result<u64, SystemError> {
|
||||
// 文件名过长
|
||||
if path.len() > PAGE_4K_SIZE as usize {
|
||||
if path.len() > MAX_PATHLEN as usize {
|
||||
return Err(SystemError::ENAMETOOLONG);
|
||||
}
|
||||
|
||||
@ -209,7 +208,7 @@ pub fn do_mkdir(path: &str, _mode: FileMode) -> Result<u64, SystemError> {
|
||||
/// @brief 删除文件夹
|
||||
pub fn do_remove_dir(path: &str) -> Result<u64, SystemError> {
|
||||
// 文件名过长
|
||||
if path.len() > PAGE_4K_SIZE as usize {
|
||||
if path.len() > MAX_PATHLEN as usize {
|
||||
return Err(SystemError::ENAMETOOLONG);
|
||||
}
|
||||
|
||||
@ -245,7 +244,7 @@ pub fn do_remove_dir(path: &str) -> Result<u64, SystemError> {
|
||||
/// @brief 删除文件
|
||||
pub fn do_unlink_at(path: &str, _mode: FileMode) -> Result<u64, SystemError> {
|
||||
// 文件名过长
|
||||
if path.len() > PAGE_4K_SIZE as usize {
|
||||
if path.len() > MAX_PATHLEN as usize {
|
||||
return Err(SystemError::ENAMETOOLONG);
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@ use crate::{
|
||||
syscall::SystemError,
|
||||
};
|
||||
|
||||
use super::{Dirent, FileType, IndexNode, InodeId, Metadata};
|
||||
use super::{Dirent, FileType, IndexNode, InodeId, Metadata, SpecialNodeData};
|
||||
|
||||
/// 文件私有信息的枚举类型
|
||||
#[derive(Debug, Clone)]
|
||||
@ -115,7 +115,17 @@ impl File {
|
||||
/// @param inode 文件对象对应的inode
|
||||
/// @param mode 文件的打开模式
|
||||
pub fn new(inode: Arc<dyn IndexNode>, mode: FileMode) -> Result<Self, SystemError> {
|
||||
let file_type: FileType = inode.metadata()?.file_type;
|
||||
let mut inode = inode;
|
||||
let file_type = inode.metadata()?.file_type;
|
||||
match file_type {
|
||||
FileType::Pipe => {
|
||||
if let Some(SpecialNodeData::Pipe(pipe_inode)) = inode.special_node() {
|
||||
inode = pipe_inode;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let mut f = File {
|
||||
inode,
|
||||
offset: 0,
|
||||
|
@ -11,7 +11,13 @@ use ::core::{any::Any, fmt::Debug, sync::atomic::AtomicUsize};
|
||||
|
||||
use alloc::{string::String, sync::Arc, vec::Vec};
|
||||
|
||||
use crate::{libs::casting::DowncastArc, syscall::SystemError, time::TimeSpec};
|
||||
use crate::{
|
||||
driver::base::{block::block_device::BlockDevice, char::CharDevice, device::DeviceNumber},
|
||||
ipc::pipe::LockedPipeInode,
|
||||
libs::casting::DowncastArc,
|
||||
syscall::SystemError,
|
||||
time::TimeSpec,
|
||||
};
|
||||
|
||||
use self::{core::generate_inode_id, file::FileMode, syscall::ModeType};
|
||||
pub use self::{core::ROOT_INODE, file::FilePrivateData, mount::MountFS};
|
||||
@ -41,6 +47,16 @@ pub enum FileType {
|
||||
Socket,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SpecialNodeData {
|
||||
/// 管道文件
|
||||
Pipe(Arc<LockedPipeInode>),
|
||||
/// 字符设备
|
||||
CharDevice(Arc<dyn CharDevice>),
|
||||
/// 块设备
|
||||
BlockDevice(Arc<dyn BlockDevice>),
|
||||
}
|
||||
|
||||
/* these are defined by POSIX and also present in glibc's dirent.h */
|
||||
/// 完整含义请见 http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
|
||||
pub const DT_UNKNOWN: u16 = 0;
|
||||
@ -338,6 +354,23 @@ pub trait IndexNode: Any + Sync + Send + Debug {
|
||||
fn sync(&self) -> Result<(), SystemError> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
/// ## 创建一个特殊文件节点
|
||||
/// - _filename: 文件名
|
||||
/// - _mode: 权限信息
|
||||
fn mknod(
|
||||
&self,
|
||||
_filename: &str,
|
||||
_mode: ModeType,
|
||||
_dev_t: DeviceNumber,
|
||||
) -> Result<Arc<dyn IndexNode>, SystemError> {
|
||||
return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
|
||||
}
|
||||
|
||||
/// ## 返回特殊文件的inode
|
||||
fn special_node(&self) -> Option<SpecialNodeData> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl DowncastArc for dyn IndexNode {
|
||||
|
@ -8,7 +8,7 @@ use alloc::{
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
use crate::{libs::spinlock::SpinLock, syscall::SystemError};
|
||||
use crate::{driver::base::device::DeviceNumber, libs::spinlock::SpinLock, syscall::SystemError};
|
||||
|
||||
use super::{
|
||||
file::FileMode, syscall::ModeType, FilePrivateData, FileSystem, FileType, IndexNode, InodeId,
|
||||
@ -348,6 +348,26 @@ impl IndexNode for MountFSInode {
|
||||
.insert(metadata.inode_id, new_mount_fs.clone());
|
||||
return Ok(new_mount_fs);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn mknod(
|
||||
&self,
|
||||
filename: &str,
|
||||
mode: ModeType,
|
||||
dev_t: DeviceNumber,
|
||||
) -> Result<Arc<dyn IndexNode>, SystemError> {
|
||||
return Ok(MountFSInode {
|
||||
inner_inode: self.inner_inode.mknod(filename, mode, dev_t)?,
|
||||
mount_fs: self.mount_fs.clone(),
|
||||
self_ref: Weak::default(),
|
||||
}
|
||||
.wrap());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn special_node(&self) -> Option<super::SpecialNodeData> {
|
||||
self.inner_inode.special_node()
|
||||
}
|
||||
}
|
||||
|
||||
impl FileSystem for MountFS {
|
||||
|
@ -1,3 +1,5 @@
|
||||
use core::ffi::CStr;
|
||||
|
||||
use alloc::{
|
||||
string::{String, ToString},
|
||||
sync::Arc,
|
||||
@ -5,14 +7,14 @@ use alloc::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
driver::base::block::SeekFrom,
|
||||
driver::base::{block::SeekFrom, device::DeviceNumber},
|
||||
filesystem::vfs::file::FileDescriptorVec,
|
||||
include::bindings::bindings::{verify_area, AT_REMOVEDIR, PAGE_4K_SIZE, PROC_MAX_FD_NUM},
|
||||
include::bindings::bindings::{verify_area, AT_REMOVEDIR, PROC_MAX_FD_NUM},
|
||||
kerror,
|
||||
libs::rwlock::RwLockWriteGuard,
|
||||
mm::VirtAddr,
|
||||
process::ProcessManager,
|
||||
syscall::{Syscall, SystemError},
|
||||
syscall::{user_access::UserBufferReader, Syscall, SystemError},
|
||||
time::TimeSpec,
|
||||
};
|
||||
|
||||
@ -21,7 +23,7 @@ use super::{
|
||||
fcntl::{FcntlCommand, FD_CLOEXEC},
|
||||
file::{File, FileMode},
|
||||
utils::rsplit_path,
|
||||
Dirent, FileType, IndexNode, ROOT_INODE,
|
||||
Dirent, FileType, IndexNode, MAX_PATHLEN, ROOT_INODE,
|
||||
};
|
||||
|
||||
pub const SEEK_SET: u32 = 0;
|
||||
@ -137,7 +139,7 @@ impl Syscall {
|
||||
// kdebug!("open: path: {}, mode: {:?}", path, mode);
|
||||
|
||||
// 文件名过长
|
||||
if path.len() > PAGE_4K_SIZE as usize {
|
||||
if path.len() > MAX_PATHLEN as usize {
|
||||
return Err(SystemError::ENAMETOOLONG);
|
||||
}
|
||||
|
||||
@ -698,6 +700,38 @@ impl Syscall {
|
||||
}
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
pub fn mknod(
|
||||
path_ptr: *const i8,
|
||||
mode: ModeType,
|
||||
dev_t: DeviceNumber,
|
||||
) -> Result<usize, SystemError> {
|
||||
// 安全检验
|
||||
let len = unsafe { CStr::from_ptr(path_ptr).to_bytes().len() };
|
||||
let user_buffer = UserBufferReader::new(path_ptr, len, true)?;
|
||||
let buf = user_buffer.read_from_user::<u8>(0)?;
|
||||
let path = core::str::from_utf8(buf).map_err(|_| SystemError::EINVAL)?;
|
||||
|
||||
// 文件名过长
|
||||
if path.len() > MAX_PATHLEN as usize {
|
||||
return Err(SystemError::ENAMETOOLONG);
|
||||
}
|
||||
|
||||
let inode: Result<Arc<dyn IndexNode>, SystemError> = ROOT_INODE().lookup(path);
|
||||
|
||||
if inode.is_ok() {
|
||||
return Err(SystemError::EEXIST);
|
||||
}
|
||||
|
||||
let (filename, parent_path) = rsplit_path(path);
|
||||
|
||||
// 查找父目录
|
||||
let parent_inode: Arc<dyn IndexNode> = ROOT_INODE().lookup(parent_path.unwrap_or("/"))?;
|
||||
// 创建nod
|
||||
parent_inode.mknod(filename, mode, dev_t)?;
|
||||
|
||||
return Ok(0);
|
||||
}
|
||||
}
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
@ -7,11 +7,11 @@ use num_traits::{FromPrimitive, ToPrimitive};
|
||||
|
||||
use crate::{
|
||||
arch::{cpu::cpu_reset, interrupt::TrapFrame, MMArch},
|
||||
driver::base::block::SeekFrom,
|
||||
driver::base::{block::SeekFrom, device::DeviceNumber},
|
||||
filesystem::vfs::{
|
||||
fcntl::FcntlCommand,
|
||||
file::FileMode,
|
||||
syscall::{PosixKstat, SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET},
|
||||
syscall::{ModeType, PosixKstat, SEEK_CUR, SEEK_END, SEEK_MAX, SEEK_SET},
|
||||
MAX_PATHLEN,
|
||||
},
|
||||
include::bindings::bindings::{PAGE_2M_SIZE, PAGE_4K_SIZE},
|
||||
@ -375,6 +375,7 @@ pub const SYS_GETPGID: usize = 50;
|
||||
|
||||
pub const SYS_FCNTL: usize = 51;
|
||||
pub const SYS_FTRUNCATE: usize = 52;
|
||||
pub const SYS_MKNOD: usize = 53;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Syscall;
|
||||
@ -973,6 +974,14 @@ impl Syscall {
|
||||
res
|
||||
}
|
||||
|
||||
SYS_MKNOD => {
|
||||
let path = args[0];
|
||||
let flags = args[1];
|
||||
let dev_t = args[2];
|
||||
let flags: ModeType = ModeType::from_bits_truncate(flags as u32);
|
||||
Self::mknod(path as *const i8, flags, DeviceNumber::from(dev_t))
|
||||
}
|
||||
|
||||
_ => panic!("Unsupported syscall ID: {}", syscall_num),
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user