-
-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(namespace) feat some namespace bugs #1033
base: master
Are you sure you want to change the base?
Changes from 18 commits
ac0e419
fa78e87
0b7830e
67cce32
00122f6
d2f07f1
1e08a46
80e2074
b3311bd
8f90bb6
6a1b404
9f03e49
eb855e1
2345338
b702398
6cbade5
2709fac
95a6274
5da999f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,8 @@ use alloc::sync::Arc; | |
use system_error::SystemError; | ||
|
||
use super::namespace::Namespace; | ||
use super::namespace::NsOperations; | ||
use super::ucount::Ucount::MntNamespaces; | ||
use super::{namespace::NsCommon, ucount::UCounts, user_namespace::UserNamespace}; | ||
use crate::container_of; | ||
use super::{ucount::UCounts, user_namespace::UserNamespace}; | ||
use crate::filesystem::vfs::mount::MountFSInode; | ||
use crate::filesystem::vfs::IndexNode; | ||
use crate::filesystem::vfs::InodeId; | ||
|
@@ -26,22 +24,20 @@ use crate::process::fork::CloneFlags; | |
use crate::process::ProcessManager; | ||
use crate::syscall::Syscall; | ||
#[allow(dead_code)] | ||
#[derive(Debug)] | ||
#[derive(Debug, Clone)] | ||
pub struct MntNamespace { | ||
/// namespace 共有的部分 | ||
ns_common: Arc<NsCommon>, | ||
/// 关联的用户名字空间 | ||
user_ns: Arc<UserNamespace>, | ||
/// 资源计数器 | ||
ucounts: Arc<UCounts>, | ||
/// 根文件系统 | ||
root: Option<Arc<MountFS>>, | ||
/// 红黑树用于挂载所有挂载点 | ||
mounts: RBTree<InodeId, MountFSInode>, | ||
mounts: Arc<RBTree<InodeId, MountFSInode>>, | ||
/// 等待队列 | ||
poll: WaitQueue, | ||
poll: Arc<WaitQueue>, | ||
/// 挂载序列号 | ||
seq: AtomicU64, | ||
seq: Arc<AtomicU64>, | ||
/// 挂载点的数量 | ||
nr_mounts: u32, | ||
/// 待处理的挂载点 | ||
|
@@ -50,7 +46,16 @@ pub struct MntNamespace { | |
|
||
impl Default for MntNamespace { | ||
fn default() -> Self { | ||
Self::new() | ||
Self { | ||
user_ns: Arc::new(UserNamespace::default()), | ||
ucounts: Arc::new(UCounts::default()), | ||
root: None, | ||
mounts: Arc::new(RBTree::new()), | ||
poll: Arc::new(WaitQueue::default()), | ||
seq: Arc::new(AtomicU64::new(0)), | ||
nr_mounts: 0, | ||
pending_mounts: 0, | ||
} | ||
} | ||
} | ||
|
||
|
@@ -69,18 +74,15 @@ pub struct FsStruct { | |
} | ||
impl Default for FsStruct { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl FsStruct { | ||
pub fn new() -> Self { | ||
Self { | ||
umask: 0o22, | ||
root: ROOT_INODE(), | ||
pwd: ROOT_INODE(), | ||
} | ||
} | ||
} | ||
|
||
impl FsStruct { | ||
pub fn set_root(&mut self, inode: Arc<dyn IndexNode>) { | ||
self.root = inode; | ||
} | ||
|
@@ -90,72 +92,41 @@ impl FsStruct { | |
} | ||
|
||
impl Namespace for MntNamespace { | ||
fn ns_common_to_ns(ns_common: Arc<NsCommon>) -> Arc<Self> { | ||
let ns_common_ptr = Arc::as_ptr(&ns_common); | ||
container_of!(ns_common_ptr, MntNamespace, ns_common) | ||
fn name(&self) -> String { | ||
"mnt".to_string() | ||
} | ||
} | ||
|
||
impl MntNsOperations { | ||
pub fn new(name: String) -> Self { | ||
Self { | ||
name, | ||
clone_flags: CloneFlags::CLONE_NEWNS, | ||
} | ||
fn get(&self, pid: crate::process::Pid) -> Option<Arc<dyn Namespace>> { | ||
ProcessManager::find(pid) | ||
.map(|pcb| pcb.get_nsproxy().read().mnt_namespace.clone() as Arc<dyn Namespace>) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
|
||
impl NsOperations for MntNsOperations { | ||
fn get(&self, pid: crate::process::Pid) -> Option<Arc<NsCommon>> { | ||
let pcb = ProcessManager::find(pid); | ||
pcb.map(|pcb| pcb.get_nsproxy().read().mnt_namespace.ns_common.clone()) | ||
fn clone_flags(&self) -> CloneFlags { | ||
CloneFlags::CLONE_NEWNS | ||
} | ||
// 不存在这个方法 | ||
fn get_parent(&self, _ns_common: Arc<NsCommon>) -> Result<Arc<NsCommon>, SystemError> { | ||
unreachable!() | ||
} | ||
fn install( | ||
&self, | ||
nsset: &mut super::NsSet, | ||
ns_common: Arc<NsCommon>, | ||
) -> Result<(), SystemError> { | ||
|
||
fn put(&self) {} | ||
|
||
fn install(&self, nsset: &mut super::NsSet) -> Result<(), SystemError> { | ||
let nsproxy = &mut nsset.nsproxy; | ||
let mnt_ns = MntNamespace::ns_common_to_ns(ns_common); | ||
if mnt_ns.is_anon_ns() { | ||
if self.is_anon_ns() { | ||
return Err(SystemError::EINVAL); | ||
} | ||
nsproxy.mnt_namespace = mnt_ns; | ||
nsproxy.mnt_namespace = Arc::new(self.clone()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
nsset.fs.lock().set_pwd(ROOT_INODE()); | ||
nsset.fs.lock().set_root(ROOT_INODE()); | ||
Ok(()) | ||
} | ||
fn owner(&self, ns_common: Arc<NsCommon>) -> Arc<UserNamespace> { | ||
let mnt_ns = MntNamespace::ns_common_to_ns(ns_common); | ||
mnt_ns.user_ns.clone() | ||
|
||
fn owner(&self) -> Arc<UserNamespace> { | ||
self.user_ns.clone() | ||
} | ||
fn put(&self, ns_common: Arc<NsCommon>) { | ||
let pid_ns = MntNamespace::ns_common_to_ns(ns_common); | ||
// 不存在这个方法 | ||
fn get_parent(&self) -> Result<Arc<dyn Namespace>, SystemError> { | ||
unreachable!() | ||
} | ||
} | ||
impl MntNamespace { | ||
pub fn new() -> Self { | ||
let ns_common = Arc::new(NsCommon::new(Box::new(MntNsOperations::new( | ||
"mnt".to_string(), | ||
)))); | ||
|
||
Self { | ||
ns_common, | ||
user_ns: Arc::new(UserNamespace::new()), | ||
ucounts: Arc::new(UCounts::new()), | ||
root: None, | ||
mounts: RBTree::new(), | ||
poll: WaitQueue::default(), | ||
seq: AtomicU64::new(0), | ||
nr_mounts: 0, | ||
pending_mounts: 0, | ||
} | ||
} | ||
/// anon 用来判断是否是匿名的.匿名函数的问题还需要考虑 | ||
pub fn create_mnt_namespace( | ||
&self, | ||
|
@@ -167,20 +138,16 @@ impl MntNamespace { | |
return Err(SystemError::ENOSPC); | ||
} | ||
let ucounts = ucounts.unwrap(); | ||
let ns_common = Arc::new(NsCommon::new(Box::new(MntNsOperations::new( | ||
"mnt".to_string(), | ||
)))); | ||
let seq = AtomicU64::new(0); | ||
let seq = Arc::new(AtomicU64::new(0)); | ||
if !anon { | ||
seq.fetch_add(1, core::sync::atomic::Ordering::SeqCst); | ||
} | ||
Ok(Self { | ||
ns_common, | ||
user_ns, | ||
ucounts, | ||
root: None, | ||
mounts: RBTree::new(), | ||
poll: WaitQueue::default(), | ||
mounts: Arc::new(RBTree::new()), | ||
poll: Arc::new(WaitQueue::default()), | ||
seq, | ||
nr_mounts: 0, | ||
pending_mounts: 0, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#![allow(dead_code, unused_variables, unused_imports)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 删掉全局的这种lint |
||
use alloc::string::String; | ||
use core::fmt::Debug; | ||
|
||
use crate::filesystem::procfs::ProcFSInode; | ||
|
@@ -12,30 +13,17 @@ use system_error::SystemError; | |
|
||
// 目前无credit功能,采用全局静态的user_namespace | ||
lazy_static! { | ||
pub static ref USER_NS: Arc<UserNamespace> = Arc::new(UserNamespace::new()); | ||
pub static ref USER_NS: Arc<UserNamespace> = Arc::new(UserNamespace::default()); | ||
} | ||
use super::{create_new_namespaces, NsProxy, NsSet}; | ||
pub trait NsOperations: Send + Sync + Debug { | ||
fn get(&self, pid: Pid) -> Option<Arc<NsCommon>>; | ||
fn put(&self, ns_common: Arc<NsCommon>); | ||
fn install(&self, nsset: &mut NsSet, ns_common: Arc<NsCommon>) -> Result<(), SystemError>; | ||
fn owner(&self, ns_common: Arc<NsCommon>) -> Arc<UserNamespace>; | ||
fn get_parent(&self, ns_common: Arc<NsCommon>) -> Result<Arc<NsCommon>, SystemError>; | ||
} | ||
#[derive(Debug)] | ||
pub struct NsCommon { | ||
ops: Box<dyn NsOperations>, | ||
stashed: Arc<dyn IndexNode>, | ||
} | ||
|
||
impl NsCommon { | ||
pub fn new(ops: Box<dyn NsOperations>) -> Self { | ||
let inode = ROOT_INODE().find("proc").unwrap_or_else(|_| ROOT_INODE()); | ||
Self { | ||
ops, | ||
stashed: inode, | ||
} | ||
} | ||
pub trait Namespace: Send + Sync + Debug { | ||
fn name(&self) -> String; | ||
fn clone_flags(&self) -> CloneFlags; | ||
fn get(&self, pid: Pid) -> Option<Arc<dyn Namespace>>; | ||
fn put(&self); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不需要put方法吧 |
||
fn install(&self, nsset: &mut NsSet) -> Result<(), SystemError>; | ||
fn owner(&self) -> Arc<UserNamespace>; | ||
fn get_parent(&self) -> Result<Arc<dyn Namespace>, SystemError>; | ||
} | ||
|
||
pub enum NsType { | ||
|
@@ -49,10 +37,6 @@ pub enum NsType { | |
Time, | ||
} | ||
|
||
pub trait Namespace { | ||
fn ns_common_to_ns(ns_common: Arc<NsCommon>) -> Arc<Self>; | ||
} | ||
|
||
pub fn check_unshare_flags(unshare_flags: u64) -> Result<usize, SystemError> { | ||
let valid_flags = CloneFlags::CLONE_THREAD | ||
| CloneFlags::CLONE_FS | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么每个成员变量都是
Arc<>
?这个访存效率低并且浪费内存。直接一个Arc<MntNamespace>
会不会好些。并且,mounts字段还有poll字段,你确定你测试过能用????这个一眼看过去就知道跑不了。