asterinas/kernel/src/fs/devpts/mod.rs

307 lines
8.2 KiB
Rust
Raw Normal View History

2024-01-03 03:22:36 +00:00
// SPDX-License-Identifier: MPL-2.0
#![allow(unused_variables)]
use core::time::Duration;
2023-07-18 09:19:32 +00:00
use aster_util::slot_vec::SlotVec;
use id_alloc::IdAlloc;
2023-07-18 09:19:32 +00:00
use self::{ptmx::Ptmx, slave::PtySlaveInode};
2024-08-16 02:47:48 +00:00
use super::utils::MknodType;
use crate::{
device::PtyMaster,
fs::{
device::{Device, DeviceId, DeviceType},
utils::{
DirentVisitor, FileSystem, FsFlags, Inode, InodeMode, InodeType, IoctlCmd, Metadata,
SuperBlock, NAME_MAX,
},
},
prelude::*,
process::{Gid, Uid},
};
2023-07-18 09:19:32 +00:00
mod ptmx;
mod slave;
const DEVPTS_MAGIC: u64 = 0x1cd1;
const BLOCK_SIZE: usize = 1024;
2024-05-24 02:41:33 +00:00
const ROOT_INO: u64 = 1;
const PTMX_INO: u64 = 2;
const FIRST_SLAVE_INO: u64 = 3;
2023-07-18 09:19:32 +00:00
/// The max number of pty pairs.
const MAX_PTY_NUM: usize = 4096;
/// Devpts(device pseudo terminal filesystem) is a virtual filesystem.
///
/// It is normally mounted at "/dev/pts" and contains solely devices files which
/// represent slaves to the multiplexing master located at "/dev/ptmx".
///
/// Actually, the "/dev/ptmx" is a symlink to the real device at "/dev/pts/ptmx".
pub struct DevPts {
sb: SuperBlock,
2024-05-24 02:33:38 +00:00
root: Arc<RootInode>,
2023-07-18 09:19:32 +00:00
index_alloc: Mutex<IdAlloc>,
this: Weak<Self>,
}
impl DevPts {
pub fn new() -> Arc<Self> {
2023-09-04 03:04:42 +00:00
Arc::new_cyclic(|weak_self| Self {
2024-05-24 02:33:38 +00:00
sb: SuperBlock::new(DEVPTS_MAGIC, BLOCK_SIZE, NAME_MAX),
root: RootInode::new(weak_self.clone()),
2023-07-18 09:19:32 +00:00
index_alloc: Mutex::new(IdAlloc::with_capacity(MAX_PTY_NUM)),
this: weak_self.clone(),
2023-09-04 03:04:42 +00:00
})
2023-07-18 09:19:32 +00:00
}
/// Create the master and slave pair.
fn create_master_slave_pair(&self) -> Result<(Arc<PtyMaster>, Arc<PtySlaveInode>)> {
2023-07-18 09:19:32 +00:00
let index = self
.index_alloc
.lock()
.alloc()
.ok_or_else(|| Error::with_message(Errno::EIO, "cannot alloc index"))?;
2023-08-28 06:28:23 +00:00
let (master, slave) = crate::device::new_pty_pair(index as u32, self.root.ptmx.clone())?;
2023-07-18 09:19:32 +00:00
let slave_inode = PtySlaveInode::new(slave, self.this.clone());
self.root.add_slave(index.to_string(), slave_inode.clone());
Ok((master, slave_inode))
2023-07-18 09:19:32 +00:00
}
/// Remove the slave from fs.
///
/// This is called when the master is being dropped.
pub fn remove_slave(&self, index: u32) -> Option<Arc<PtySlaveInode>> {
2023-07-18 09:19:32 +00:00
let removed_slave = self.root.remove_slave(&index.to_string());
if removed_slave.is_some() {
2023-08-28 06:28:23 +00:00
self.index_alloc.lock().free(index as usize);
2023-07-18 09:19:32 +00:00
}
removed_slave
}
}
impl FileSystem for DevPts {
fn sync(&self) -> Result<()> {
Ok(())
}
fn root_inode(&self) -> Arc<dyn Inode> {
self.root.clone()
}
fn sb(&self) -> SuperBlock {
self.sb.clone()
}
fn flags(&self) -> FsFlags {
FsFlags::empty()
2023-07-18 09:19:32 +00:00
}
}
struct RootInode {
ptmx: Arc<Ptmx>,
slaves: RwLock<SlotVec<(String, Arc<PtySlaveInode>)>>,
2024-01-04 09:52:27 +00:00
metadata: RwLock<Metadata>,
2023-07-18 09:19:32 +00:00
fs: Weak<DevPts>,
}
impl RootInode {
2024-05-24 02:33:38 +00:00
pub fn new(fs: Weak<DevPts>) -> Arc<Self> {
2023-07-18 09:19:32 +00:00
Arc::new(Self {
2024-05-24 02:33:38 +00:00
ptmx: Ptmx::new(fs.clone()),
2023-07-18 09:19:32 +00:00
slaves: RwLock::new(SlotVec::new()),
2024-01-04 09:52:27 +00:00
metadata: RwLock::new(Metadata::new_dir(
ROOT_INO,
InodeMode::from_bits_truncate(0o755),
2024-05-24 02:33:38 +00:00
BLOCK_SIZE,
2024-01-04 09:52:27 +00:00
)),
2023-07-18 09:19:32 +00:00
fs,
})
}
fn add_slave(&self, name: String, slave: Arc<PtySlaveInode>) {
self.slaves.write().put((name, slave));
}
fn remove_slave(&self, name: &str) -> Option<Arc<PtySlaveInode>> {
let removed_slave = {
let mut slaves = self.slaves.write();
let pos = slaves
.idxes_and_items()
.find(|(_, (child, _))| child == name)
.map(|(pos, _)| pos);
match pos {
None => {
return None;
}
Some(pos) => slaves.remove(pos).map(|(_, node)| node).unwrap(),
}
};
Some(removed_slave)
}
}
impl Inode for RootInode {
2024-01-05 06:44:19 +00:00
fn size(&self) -> usize {
2024-01-04 09:52:27 +00:00
self.metadata.read().size
2023-07-18 09:19:32 +00:00
}
2023-09-18 03:47:17 +00:00
fn resize(&self, new_size: usize) -> Result<()> {
Err(Error::new(Errno::EISDIR))
}
2023-07-18 09:19:32 +00:00
fn metadata(&self) -> Metadata {
2024-01-04 09:52:27 +00:00
*self.metadata.read()
2023-07-18 09:19:32 +00:00
}
2023-09-18 03:47:17 +00:00
fn ino(&self) -> u64 {
2024-01-04 09:52:27 +00:00
self.metadata.read().ino as _
2023-09-18 03:47:17 +00:00
}
fn type_(&self) -> InodeType {
2024-01-04 09:52:27 +00:00
self.metadata.read().type_
}
2024-01-04 09:52:27 +00:00
fn mode(&self) -> Result<InodeMode> {
Ok(self.metadata.read().mode)
}
2024-01-04 09:52:27 +00:00
fn set_mode(&self, mode: InodeMode) -> Result<()> {
self.metadata.write().mode = mode;
Ok(())
}
fn owner(&self) -> Result<Uid> {
Ok(self.metadata.read().uid)
}
fn set_owner(&self, uid: Uid) -> Result<()> {
self.metadata.write().uid = uid;
Ok(())
}
fn group(&self) -> Result<Gid> {
Ok(self.metadata.read().gid)
}
fn set_group(&self, gid: Gid) -> Result<()> {
self.metadata.write().gid = gid;
Ok(())
}
2023-07-18 09:19:32 +00:00
fn atime(&self) -> Duration {
2024-01-04 09:52:27 +00:00
self.metadata.read().atime
2023-07-18 09:19:32 +00:00
}
2024-01-04 09:52:27 +00:00
fn set_atime(&self, time: Duration) {
self.metadata.write().atime = time;
}
2023-07-18 09:19:32 +00:00
fn mtime(&self) -> Duration {
2024-01-04 09:52:27 +00:00
self.metadata.read().mtime
2023-07-18 09:19:32 +00:00
}
2024-01-04 09:52:27 +00:00
fn set_mtime(&self, time: Duration) {
self.metadata.write().mtime = time;
}
2023-07-18 09:19:32 +00:00
fn ctime(&self) -> Duration {
self.metadata.read().ctime
}
fn set_ctime(&self, time: Duration) {
self.metadata.write().ctime = time;
}
2023-07-18 09:19:32 +00:00
fn create(&self, name: &str, type_: InodeType, mode: InodeMode) -> Result<Arc<dyn Inode>> {
Err(Error::new(Errno::EPERM))
}
2024-08-16 02:47:48 +00:00
fn mknod(&self, name: &str, mode: InodeMode, type_: MknodType) -> Result<Arc<dyn Inode>> {
2023-07-18 09:19:32 +00:00
Err(Error::new(Errno::EPERM))
}
fn readdir_at(&self, offset: usize, visitor: &mut dyn DirentVisitor) -> Result<usize> {
let try_readdir = |offset: &mut usize, visitor: &mut dyn DirentVisitor| -> Result<()> {
// Read the 3 special entries.
if *offset == 0 {
2024-01-04 09:52:27 +00:00
visitor.visit(".", self.ino(), self.type_(), *offset)?;
2023-07-18 09:19:32 +00:00
*offset += 1;
}
if *offset == 1 {
2024-01-04 09:52:27 +00:00
visitor.visit("..", self.ino(), self.type_(), *offset)?;
2023-07-18 09:19:32 +00:00
*offset += 1;
}
if *offset == 2 {
2024-01-04 09:52:27 +00:00
visitor.visit("ptmx", self.ptmx.ino(), self.ptmx.type_(), *offset)?;
2023-07-18 09:19:32 +00:00
*offset += 1;
}
// Read the slaves.
let slaves = self.slaves.read();
2023-09-04 03:04:42 +00:00
let start_offset = *offset;
2023-07-18 09:19:32 +00:00
for (idx, (name, node)) in slaves
.idxes_and_items()
.map(|(idx, (name, node))| (idx + 3, (name, node)))
.skip_while(|(idx, _)| idx < &start_offset)
{
2024-01-04 09:52:27 +00:00
visitor.visit(name.as_ref(), node.ino(), node.type_(), idx)?;
2023-07-18 09:19:32 +00:00
*offset = idx + 1;
}
Ok(())
};
let mut iterate_offset = offset;
match try_readdir(&mut iterate_offset, visitor) {
Err(e) if offset == iterate_offset => Err(e),
_ => Ok(iterate_offset - offset),
}
}
fn link(&self, old: &Arc<dyn Inode>, name: &str) -> Result<()> {
Err(Error::new(Errno::EPERM))
}
fn unlink(&self, name: &str) -> Result<()> {
Err(Error::new(Errno::EPERM))
}
fn rmdir(&self, name: &str) -> Result<()> {
Err(Error::new(Errno::EPERM))
}
fn lookup(&self, name: &str) -> Result<Arc<dyn Inode>> {
let inode = match name {
"." | ".." => self.fs().root_inode(),
// Call the "open" method of ptmx to create a master and slave pair.
"ptmx" => self.ptmx.clone(),
2023-07-18 09:19:32 +00:00
slave => self
.slaves
.read()
.idxes_and_items()
2023-09-04 03:04:42 +00:00
.find(|(_, (child_name, _))| child_name == slave)
2023-07-18 09:19:32 +00:00
.map(|(_, (_, node))| node.clone())
.ok_or(Error::new(Errno::ENOENT))?,
};
Ok(inode)
}
fn rename(&self, old_name: &str, target: &Arc<dyn Inode>, new_name: &str) -> Result<()> {
Err(Error::new(Errno::EPERM))
}
fn fs(&self) -> Arc<dyn FileSystem> {
self.fs.upgrade().unwrap()
}
fn is_dentry_cacheable(&self) -> bool {
false
}
2023-07-18 09:19:32 +00:00
}