mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-17 12:47:16 +00:00
Change the API of mknod
This commit is contained in:
parent
95f67b3d67
commit
ea64ddfde5
@ -125,7 +125,7 @@ pub fn add_node(device: Arc<dyn Device>, path: &str) -> Result<Arc<Dentry>> {
|
|||||||
dentry = dentry.mknod(
|
dentry = dentry.mknod(
|
||||||
next_name,
|
next_name,
|
||||||
InodeMode::from_bits_truncate(0o666),
|
InodeMode::from_bits_truncate(0o666),
|
||||||
device.clone(),
|
device.clone().into(),
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
// Mkdir parent path
|
// Mkdir parent path
|
||||||
|
@ -8,6 +8,7 @@ use aster_util::slot_vec::SlotVec;
|
|||||||
use id_alloc::IdAlloc;
|
use id_alloc::IdAlloc;
|
||||||
|
|
||||||
use self::{ptmx::Ptmx, slave::PtySlaveInode};
|
use self::{ptmx::Ptmx, slave::PtySlaveInode};
|
||||||
|
use super::utils::MknodType;
|
||||||
use crate::{
|
use crate::{
|
||||||
device::PtyMaster,
|
device::PtyMaster,
|
||||||
fs::{
|
fs::{
|
||||||
@ -222,7 +223,7 @@ impl Inode for RootInode {
|
|||||||
Err(Error::new(Errno::EPERM))
|
Err(Error::new(Errno::EPERM))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mknod(&self, name: &str, mode: InodeMode, dev: Arc<dyn Device>) -> Result<Arc<dyn Inode>> {
|
fn mknod(&self, name: &str, mode: InodeMode, type_: MknodType) -> Result<Arc<dyn Inode>> {
|
||||||
Err(Error::new(Errno::EPERM))
|
Err(Error::new(Errno::EPERM))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,11 +28,10 @@ use super::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
events::IoEvents,
|
events::IoEvents,
|
||||||
fs::{
|
fs::{
|
||||||
device::Device,
|
|
||||||
exfat::{dentry::ExfatDentryIterator, fat::ExfatChain, fs::ExfatFS},
|
exfat::{dentry::ExfatDentryIterator, fat::ExfatChain, fs::ExfatFS},
|
||||||
utils::{
|
utils::{
|
||||||
DirentVisitor, Extension, Inode, InodeMode, InodeType, IoctlCmd, Metadata, PageCache,
|
DirentVisitor, Extension, Inode, InodeMode, InodeType, IoctlCmd, Metadata, MknodType,
|
||||||
PageCacheBackend,
|
PageCache, PageCacheBackend,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
@ -1444,7 +1443,7 @@ impl Inode for ExfatInode {
|
|||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mknod(&self, name: &str, mode: InodeMode, dev: Arc<dyn Device>) -> Result<Arc<dyn Inode>> {
|
fn mknod(&self, name: &str, mode: InodeMode, type_: MknodType) -> Result<Arc<dyn Inode>> {
|
||||||
return_errno_with_message!(Errno::EINVAL, "unsupported operation")
|
return_errno_with_message!(Errno::EINVAL, "unsupported operation")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,11 +8,10 @@ use aster_rights::Full;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
fs::{
|
fs::{
|
||||||
device::Device,
|
|
||||||
ext2::{FilePerm, Inode as Ext2Inode},
|
ext2::{FilePerm, Inode as Ext2Inode},
|
||||||
utils::{
|
utils::{
|
||||||
DirentVisitor, Extension, FallocMode, FileSystem, Inode, InodeMode, InodeType,
|
DirentVisitor, Extension, FallocMode, FileSystem, Inode, InodeMode, InodeType,
|
||||||
IoctlCmd, Metadata,
|
IoctlCmd, Metadata, MknodType,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
@ -131,9 +130,17 @@ impl Inode for Ext2Inode {
|
|||||||
Ok(self.create(name, type_, mode.into())?)
|
Ok(self.create(name, type_, mode.into())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mknod(&self, name: &str, mode: InodeMode, dev: Arc<dyn Device>) -> Result<Arc<dyn Inode>> {
|
fn mknod(&self, name: &str, mode: InodeMode, type_: MknodType) -> Result<Arc<dyn Inode>> {
|
||||||
let inode = self.create(name, InodeType::from(dev.type_()), mode.into())?;
|
let inode_type = type_.inode_type();
|
||||||
inode.set_device_id(dev.id().into()).unwrap();
|
let inode = match type_ {
|
||||||
|
MknodType::CharDeviceNode(dev) | MknodType::BlockDeviceNode(dev) => {
|
||||||
|
let inode = self.create(name, inode_type, mode.into())?;
|
||||||
|
inode.set_device_id(dev.id().into()).unwrap();
|
||||||
|
inode
|
||||||
|
}
|
||||||
|
_ => todo!(),
|
||||||
|
};
|
||||||
|
|
||||||
Ok(inode)
|
Ok(inode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,9 +12,8 @@ use inherit_methods_macro::inherit_methods;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
fs::{
|
fs::{
|
||||||
device::Device,
|
|
||||||
path::mount::MountNode,
|
path::mount::MountNode,
|
||||||
utils::{FileSystem, Inode, InodeMode, InodeType, Metadata, NAME_MAX},
|
utils::{FileSystem, Inode, InodeMode, InodeType, Metadata, MknodType, NAME_MAX},
|
||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
process::{Gid, Uid},
|
process::{Gid, Uid},
|
||||||
@ -182,7 +181,7 @@ impl Dentry_ {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a Dentry_ by making a device inode.
|
/// Create a Dentry_ by making a device inode.
|
||||||
pub fn mknod(&self, name: &str, mode: InodeMode, device: Arc<dyn Device>) -> Result<Arc<Self>> {
|
pub fn mknod(&self, name: &str, mode: InodeMode, type_: MknodType) -> Result<Arc<Self>> {
|
||||||
if self.inode.type_() != InodeType::Dir {
|
if self.inode.type_() != InodeType::Dir {
|
||||||
return_errno!(Errno::ENOTDIR);
|
return_errno!(Errno::ENOTDIR);
|
||||||
}
|
}
|
||||||
@ -192,7 +191,7 @@ impl Dentry_ {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let child = {
|
let child = {
|
||||||
let inode = self.inode.mknod(name, mode, device)?;
|
let inode = self.inode.mknod(name, mode, type_)?;
|
||||||
let dentry = Self::new(
|
let dentry = Self::new(
|
||||||
inode,
|
inode,
|
||||||
DentryOptions::Leaf((String::from(name), self.this())),
|
DentryOptions::Leaf((String::from(name), self.this())),
|
||||||
@ -622,9 +621,9 @@ impl Dentry {
|
|||||||
Ok(child_mount)
|
Ok(child_mount)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a Dentry by making a device inode.
|
/// Create a Dentry by making a device inode or others.
|
||||||
pub fn mknod(&self, name: &str, mode: InodeMode, device: Arc<dyn Device>) -> Result<Arc<Self>> {
|
pub fn mknod(&self, name: &str, mode: InodeMode, type_: MknodType) -> Result<Arc<Self>> {
|
||||||
let inner = self.inner.mknod(name, mode, device)?;
|
let inner = self.inner.mknod(name, mode, type_)?;
|
||||||
Ok(Self::new(self.mount_node.clone(), inner.clone()))
|
Ok(Self::new(self.mount_node.clone(), inner.clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,10 +9,7 @@ use inherit_methods_macro::inherit_methods;
|
|||||||
|
|
||||||
use super::{Common, ProcFS};
|
use super::{Common, ProcFS};
|
||||||
use crate::{
|
use crate::{
|
||||||
fs::{
|
fs::utils::{DirentVisitor, FileSystem, Inode, InodeMode, InodeType, Metadata, MknodType},
|
||||||
device::Device,
|
|
||||||
utils::{DirentVisitor, FileSystem, Inode, InodeMode, InodeType, Metadata},
|
|
||||||
},
|
|
||||||
prelude::*,
|
prelude::*,
|
||||||
process::{Gid, Uid},
|
process::{Gid, Uid},
|
||||||
};
|
};
|
||||||
@ -97,12 +94,7 @@ impl<D: DirOps + 'static> Inode for ProcDir<D> {
|
|||||||
Err(Error::new(Errno::EPERM))
|
Err(Error::new(Errno::EPERM))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mknod(
|
fn mknod(&self, _name: &str, _mode: InodeMode, type_: MknodType) -> Result<Arc<dyn Inode>> {
|
||||||
&self,
|
|
||||||
_name: &str,
|
|
||||||
_mode: InodeMode,
|
|
||||||
_device: Arc<dyn Device>,
|
|
||||||
) -> Result<Arc<dyn Inode>> {
|
|
||||||
Err(Error::new(Errno::EPERM))
|
Err(Error::new(Errno::EPERM))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ use crate::{
|
|||||||
device::Device,
|
device::Device,
|
||||||
utils::{
|
utils::{
|
||||||
CStr256, DirentVisitor, Extension, FallocMode, FileSystem, FsFlags, Inode, InodeMode,
|
CStr256, DirentVisitor, Extension, FallocMode, FileSystem, FsFlags, Inode, InodeMode,
|
||||||
InodeType, IoctlCmd, Metadata, PageCache, PageCacheBackend, SuperBlock,
|
InodeType, IoctlCmd, Metadata, MknodType, PageCache, PageCacheBackend, SuperBlock,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
@ -676,12 +676,7 @@ impl Inode for RamInode {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mknod(
|
fn mknod(&self, name: &str, mode: InodeMode, type_: MknodType) -> Result<Arc<dyn Inode>> {
|
||||||
&self,
|
|
||||||
name: &str,
|
|
||||||
mode: InodeMode,
|
|
||||||
device: Arc<dyn Device>,
|
|
||||||
) -> Result<Arc<dyn Inode>> {
|
|
||||||
if name.len() > NAME_MAX {
|
if name.len() > NAME_MAX {
|
||||||
return_errno!(Errno::ENAMETOOLONG);
|
return_errno!(Errno::ENAMETOOLONG);
|
||||||
}
|
}
|
||||||
@ -693,22 +688,27 @@ impl Inode for RamInode {
|
|||||||
if self_inode.inner.as_direntry().unwrap().contains_entry(name) {
|
if self_inode.inner.as_direntry().unwrap().contains_entry(name) {
|
||||||
return_errno_with_message!(Errno::EEXIST, "entry exists");
|
return_errno_with_message!(Errno::EEXIST, "entry exists");
|
||||||
}
|
}
|
||||||
let device_inode = RamInode::new_device(
|
let inode = match type_ {
|
||||||
&self.fs.upgrade().unwrap(),
|
MknodType::CharDeviceNode(device) | MknodType::BlockDeviceNode(device) => {
|
||||||
mode,
|
RamInode::new_device(
|
||||||
Uid::new_root(),
|
&self.fs.upgrade().unwrap(),
|
||||||
Gid::new_root(),
|
mode,
|
||||||
device,
|
Uid::new_root(),
|
||||||
);
|
Gid::new_root(),
|
||||||
|
device,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_ => return_errno_with_message!(Errno::EPERM, "unimplemented file types"),
|
||||||
|
};
|
||||||
|
|
||||||
let mut self_inode = self_inode.upgrade();
|
let mut self_inode = self_inode.upgrade();
|
||||||
self_inode
|
self_inode
|
||||||
.inner
|
.inner
|
||||||
.as_direntry_mut()
|
.as_direntry_mut()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.append_entry(name, device_inode.clone());
|
.append_entry(name, inode.clone());
|
||||||
self_inode.inc_size();
|
self_inode.inc_size();
|
||||||
Ok(device_inode)
|
Ok(inode)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_device(&self) -> Option<Arc<dyn Device>> {
|
fn as_device(&self) -> Option<Arc<dyn Device>> {
|
||||||
|
@ -254,6 +254,33 @@ impl Metadata {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum MknodType {
|
||||||
|
NamedPipeNode,
|
||||||
|
CharDeviceNode(Arc<dyn Device>),
|
||||||
|
BlockDeviceNode(Arc<dyn Device>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MknodType {
|
||||||
|
pub fn inode_type(&self) -> InodeType {
|
||||||
|
match self {
|
||||||
|
MknodType::NamedPipeNode => InodeType::NamedPipe,
|
||||||
|
MknodType::CharDeviceNode(_) => InodeType::CharDevice,
|
||||||
|
MknodType::BlockDeviceNode(_) => InodeType::BlockDevice,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Arc<dyn Device>> for MknodType {
|
||||||
|
fn from(device: Arc<dyn Device>) -> Self {
|
||||||
|
let inode_type: InodeType = device.type_().into();
|
||||||
|
match inode_type {
|
||||||
|
InodeType::CharDevice => Self::CharDeviceNode(device),
|
||||||
|
InodeType::BlockDevice => Self::BlockDeviceNode(device),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait Inode: Any + Sync + Send {
|
pub trait Inode: Any + Sync + Send {
|
||||||
fn size(&self) -> usize;
|
fn size(&self) -> usize;
|
||||||
|
|
||||||
@ -313,7 +340,7 @@ pub trait Inode: Any + Sync + Send {
|
|||||||
Err(Error::new(Errno::ENOTDIR))
|
Err(Error::new(Errno::ENOTDIR))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mknod(&self, name: &str, mode: InodeMode, dev: Arc<dyn Device>) -> Result<Arc<dyn Inode>> {
|
fn mknod(&self, name: &str, mode: InodeMode, type_: MknodType) -> Result<Arc<dyn Inode>> {
|
||||||
Err(Error::new(Errno::ENOTDIR))
|
Err(Error::new(Errno::ENOTDIR))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ pub use falloc_mode::FallocMode;
|
|||||||
pub use file_creation_mask::FileCreationMask;
|
pub use file_creation_mask::FileCreationMask;
|
||||||
pub use flock::{FlockItem, FlockList, FlockType};
|
pub use flock::{FlockItem, FlockList, FlockType};
|
||||||
pub use fs::{FileSystem, FsFlags, SuperBlock};
|
pub use fs::{FileSystem, FsFlags, SuperBlock};
|
||||||
pub use inode::{Extension, Inode, InodeMode, InodeType, Metadata};
|
pub use inode::{Extension, Inode, InodeMode, InodeType, Metadata, MknodType};
|
||||||
pub use ioctl::IoctlCmd;
|
pub use ioctl::IoctlCmd;
|
||||||
pub use page_cache::{PageCache, PageCacheBackend};
|
pub use page_cache::{PageCache, PageCacheBackend};
|
||||||
pub use random_test::{generate_random_operation, new_fs_in_memory};
|
pub use random_test::{generate_random_operation, new_fs_in_memory};
|
||||||
|
@ -51,7 +51,7 @@ pub fn sys_mknodat(
|
|||||||
}
|
}
|
||||||
InodeType::CharDevice | InodeType::BlockDevice => {
|
InodeType::CharDevice | InodeType::BlockDevice => {
|
||||||
let device_inode = get_device(dev)?;
|
let device_inode = get_device(dev)?;
|
||||||
let _ = dir_dentry.mknod(&name, inode_mode, device_inode)?;
|
let _ = dir_dentry.mknod(&name, inode_mode, device_inode.into())?;
|
||||||
}
|
}
|
||||||
InodeType::NamedPipe | InodeType::Socket => {
|
InodeType::NamedPipe | InodeType::Socket => {
|
||||||
return_errno_with_message!(Errno::EINVAL, "unsupported file types")
|
return_errno_with_message!(Errno::EINVAL, "unsupported file types")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user