xref: /DragonOS/kernel/src/namespaces/mnt_namespace.rs (revision 7b0ef10895108a0de5ff5ef3d2f93f40cf2e33a5)
1 #![allow(dead_code, unused_variables, unused_imports)]
2 use core::sync::atomic::AtomicU64;
3 use core::sync::atomic::Ordering;
4 
5 use alloc::boxed::Box;
6 use alloc::string::ToString;
7 
8 use alloc::string::String;
9 
10 use alloc::sync::Arc;
11 use system_error::SystemError;
12 
13 use super::namespace::Namespace;
14 use super::namespace::NsOperations;
15 use super::ucount::Ucount::MntNamespaces;
16 use super::{namespace::NsCommon, ucount::UCounts, user_namespace::UserNamespace};
17 use crate::container_of;
18 use crate::filesystem::vfs::mount::MountFSInode;
19 use crate::filesystem::vfs::IndexNode;
20 use crate::filesystem::vfs::InodeId;
21 use crate::filesystem::vfs::MountFS;
22 use crate::filesystem::vfs::ROOT_INODE;
23 use crate::libs::rbtree::RBTree;
24 use crate::libs::wait_queue::WaitQueue;
25 use crate::process::fork::CloneFlags;
26 use crate::process::ProcessManager;
27 use crate::syscall::Syscall;
28 #[allow(dead_code)]
29 #[derive(Debug)]
30 pub struct MntNamespace {
31     /// namespace 共有的部分
32     ns_common: Arc<NsCommon>,
33     /// 关联的用户名字空间
34     user_ns: Arc<UserNamespace>,
35     /// 资源计数器
36     ucounts: Arc<UCounts>,
37     /// 根文件系统
38     root: Option<Arc<MountFS>>,
39     /// 红黑树用于挂载所有挂载点
40     mounts: RBTree<InodeId, MountFSInode>,
41     /// 等待队列
42     poll: WaitQueue,
43     ///  挂载序列号
44     seq: AtomicU64,
45     /// 挂载点的数量
46     nr_mounts: u32,
47     /// 待处理的挂载点
48     pending_mounts: u32,
49 }
50 
51 impl Default for MntNamespace {
52     fn default() -> Self {
53         Self::new()
54     }
55 }
56 
57 #[derive(Debug)]
58 struct MntNsOperations {
59     name: String,
60     clone_flags: CloneFlags,
61 }
62 
63 /// 使用该结构体的时候加spinlock
64 #[derive(Clone, Debug)]
65 pub struct FsStruct {
66     umask: u32, //文件权限掩码
67     pub root: Arc<dyn IndexNode>,
68     pub pwd: Arc<dyn IndexNode>,
69 }
70 impl Default for FsStruct {
71     fn default() -> Self {
72         Self::new()
73     }
74 }
75 
76 impl FsStruct {
77     pub fn new() -> Self {
78         Self {
79             umask: 0o22,
80             root: ROOT_INODE(),
81             pwd: ROOT_INODE(),
82         }
83     }
84     pub fn set_root(&mut self, inode: Arc<dyn IndexNode>) {
85         self.root = inode;
86     }
87     pub fn set_pwd(&mut self, inode: Arc<dyn IndexNode>) {
88         self.pwd = inode;
89     }
90 }
91 
92 impl Namespace for MntNamespace {
93     fn ns_common_to_ns(ns_common: Arc<NsCommon>) -> Arc<Self> {
94         let ns_common_ptr = Arc::as_ptr(&ns_common);
95         container_of!(ns_common_ptr, MntNamespace, ns_common)
96     }
97 }
98 
99 impl MntNsOperations {
100     pub fn new(name: String) -> Self {
101         Self {
102             name,
103             clone_flags: CloneFlags::CLONE_NEWNS,
104         }
105     }
106 }
107 
108 impl NsOperations for MntNsOperations {
109     fn get(&self, pid: crate::process::Pid) -> Option<Arc<NsCommon>> {
110         let pcb = ProcessManager::find(pid);
111         pcb.map(|pcb| pcb.get_nsproxy().read().mnt_namespace.ns_common.clone())
112     }
113     // 不存在这个方法
114     fn get_parent(&self, _ns_common: Arc<NsCommon>) -> Result<Arc<NsCommon>, SystemError> {
115         unreachable!()
116     }
117     fn install(
118         &self,
119         nsset: &mut super::NsSet,
120         ns_common: Arc<NsCommon>,
121     ) -> Result<(), SystemError> {
122         let nsproxy = &mut nsset.nsproxy;
123         let mnt_ns = MntNamespace::ns_common_to_ns(ns_common);
124         if mnt_ns.is_anon_ns() {
125             return Err(SystemError::EINVAL);
126         }
127         nsproxy.mnt_namespace = mnt_ns;
128 
129         nsset.fs.lock().set_pwd(ROOT_INODE());
130         nsset.fs.lock().set_root(ROOT_INODE());
131         Ok(())
132     }
133     fn owner(&self, ns_common: Arc<NsCommon>) -> Arc<UserNamespace> {
134         let mnt_ns = MntNamespace::ns_common_to_ns(ns_common);
135         mnt_ns.user_ns.clone()
136     }
137     fn put(&self, ns_common: Arc<NsCommon>) {
138         let pid_ns = MntNamespace::ns_common_to_ns(ns_common);
139     }
140 }
141 impl MntNamespace {
142     pub fn new() -> Self {
143         let ns_common = Arc::new(NsCommon::new(Box::new(MntNsOperations::new(
144             "mnt".to_string(),
145         ))));
146 
147         Self {
148             ns_common,
149             user_ns: Arc::new(UserNamespace::new()),
150             ucounts: Arc::new(UCounts::new()),
151             root: None,
152             mounts: RBTree::new(),
153             poll: WaitQueue::default(),
154             seq: AtomicU64::new(0),
155             nr_mounts: 0,
156             pending_mounts: 0,
157         }
158     }
159     /// anon 用来判断是否是匿名的.匿名函数的问题还需要考虑
160     pub fn create_mnt_namespace(
161         &self,
162         user_ns: Arc<UserNamespace>,
163         anon: bool,
164     ) -> Result<Self, SystemError> {
165         let ucounts = self.inc_mnt_namespace(user_ns.clone())?;
166         if ucounts.is_none() {
167             return Err(SystemError::ENOSPC);
168         }
169         let ucounts = ucounts.unwrap();
170         let ns_common = Arc::new(NsCommon::new(Box::new(MntNsOperations::new(
171             "mnt".to_string(),
172         ))));
173         let seq = AtomicU64::new(0);
174         if !anon {
175             seq.fetch_add(1, core::sync::atomic::Ordering::SeqCst);
176         }
177         Ok(Self {
178             ns_common,
179             user_ns,
180             ucounts,
181             root: None,
182             mounts: RBTree::new(),
183             poll: WaitQueue::default(),
184             seq,
185             nr_mounts: 0,
186             pending_mounts: 0,
187         })
188     }
189 
190     pub fn inc_mnt_namespace(
191         &self,
192         user_ns: Arc<UserNamespace>,
193     ) -> Result<Option<Arc<UCounts>>, SystemError> {
194         Ok(self
195             .ucounts
196             .inc_ucounts(user_ns, Syscall::geteuid()?, MntNamespaces))
197     }
198 
199     pub fn dec_mnt_namespace(&self, uc: Arc<UCounts>) {
200         UCounts::dec_ucount(uc, super::ucount::Ucount::MntNamespaces)
201     }
202     //判断是不是匿名空间
203     pub fn is_anon_ns(&self) -> bool {
204         self.seq.load(Ordering::SeqCst) == 0
205     }
206 }
207