xref: /DragonOS/kernel/src/filesystem/sysfs/mod.rs (revision b087521e07f601b30e3d48df788fcc2f09f19566)
1dd9f1fc1STingHuang use super::vfs::{
2dd9f1fc1STingHuang     core::generate_inode_id, file::FileMode, FileSystem, FileType, FsInfo, IndexNode, Metadata,
3dd9f1fc1STingHuang     PollStatus,
4dd9f1fc1STingHuang };
5dd9f1fc1STingHuang use crate::{
6*b087521eSChiichen     driver::base::platform::platform_bus_init,
7*b087521eSChiichen     filesystem::{sysfs::bus::sys_bus_init, vfs::ROOT_INODE},
8*b087521eSChiichen     kdebug, kinfo,
9*b087521eSChiichen     libs::{
10*b087521eSChiichen         once::Once,
11*b087521eSChiichen         spinlock::{SpinLock, SpinLockGuard},
12*b087521eSChiichen     },
13dd9f1fc1STingHuang     syscall::SystemError,
14dd9f1fc1STingHuang     time::TimeSpec,
15dd9f1fc1STingHuang };
16dd9f1fc1STingHuang use alloc::{
17dd9f1fc1STingHuang     boxed::Box,
18dd9f1fc1STingHuang     collections::BTreeMap,
19dd9f1fc1STingHuang     string::{String, ToString},
20dd9f1fc1STingHuang     sync::{Arc, Weak},
21dd9f1fc1STingHuang     vec::Vec,
22dd9f1fc1STingHuang };
23dd9f1fc1STingHuang use core::ptr::null_mut;
24dd9f1fc1STingHuang 
25dd9f1fc1STingHuang pub mod bus;
26dd9f1fc1STingHuang pub mod class;
27dd9f1fc1STingHuang pub mod devices;
28dd9f1fc1STingHuang pub mod fs;
29dd9f1fc1STingHuang 
30dd9f1fc1STingHuang const SYSFS_MAX_NAMELEN: usize = 64;
31dd9f1fc1STingHuang 
32dd9f1fc1STingHuang static mut __SYS_DEVICES_INODE: *mut Arc<dyn IndexNode> = null_mut();
33dd9f1fc1STingHuang static mut __SYS_BUS_INODE: *mut Arc<dyn IndexNode> = null_mut();
34dd9f1fc1STingHuang static mut __SYS_CLASS_INODE: *mut Arc<dyn IndexNode> = null_mut();
35dd9f1fc1STingHuang static mut __SYS_FS_INODE: *mut Arc<dyn IndexNode> = null_mut();
36dd9f1fc1STingHuang 
37dd9f1fc1STingHuang /// @brief 获取全局的sys/devices节点
38dd9f1fc1STingHuang #[inline(always)]
39dd9f1fc1STingHuang #[allow(non_snake_case)]
40dd9f1fc1STingHuang pub fn SYS_DEVICES_INODE() -> Arc<dyn IndexNode> {
41dd9f1fc1STingHuang     unsafe {
42dd9f1fc1STingHuang         return __SYS_DEVICES_INODE.as_ref().unwrap().clone();
43dd9f1fc1STingHuang     }
44dd9f1fc1STingHuang }
45dd9f1fc1STingHuang 
46dd9f1fc1STingHuang /// @brief 获取全局的sys/bus节点
47dd9f1fc1STingHuang #[inline(always)]
48dd9f1fc1STingHuang #[allow(non_snake_case)]
49dd9f1fc1STingHuang pub fn SYS_BUS_INODE() -> Arc<dyn IndexNode> {
50dd9f1fc1STingHuang     unsafe {
51dd9f1fc1STingHuang         return __SYS_BUS_INODE.as_ref().unwrap().clone();
52dd9f1fc1STingHuang     }
53dd9f1fc1STingHuang }
54dd9f1fc1STingHuang 
55dd9f1fc1STingHuang /// @brief 获取全局的sys/class节点
56dd9f1fc1STingHuang #[inline(always)]
57dd9f1fc1STingHuang #[allow(non_snake_case)]
58dd9f1fc1STingHuang pub fn SYS_CLASS_INODE() -> Arc<dyn IndexNode> {
59dd9f1fc1STingHuang     unsafe {
60dd9f1fc1STingHuang         return __SYS_CLASS_INODE.as_ref().unwrap().clone();
61dd9f1fc1STingHuang     }
62dd9f1fc1STingHuang }
63dd9f1fc1STingHuang 
64dd9f1fc1STingHuang /// @brief 获取全局的sys/fs节点
65dd9f1fc1STingHuang #[inline(always)]
66dd9f1fc1STingHuang #[allow(non_snake_case)]
67dd9f1fc1STingHuang pub fn SYS_FS_INODE() -> Arc<dyn IndexNode> {
68dd9f1fc1STingHuang     unsafe {
69dd9f1fc1STingHuang         return __SYS_FS_INODE.as_ref().unwrap().clone();
70dd9f1fc1STingHuang     }
71dd9f1fc1STingHuang }
72dd9f1fc1STingHuang 
73dd9f1fc1STingHuang /// @brief dev文件系统
74dd9f1fc1STingHuang #[derive(Debug)]
75dd9f1fc1STingHuang pub struct SysFS {
76dd9f1fc1STingHuang     // 文件系统根节点
77dd9f1fc1STingHuang     root_inode: Arc<LockedSysFSInode>,
78dd9f1fc1STingHuang }
79dd9f1fc1STingHuang 
80dd9f1fc1STingHuang impl FileSystem for SysFS {
81dd9f1fc1STingHuang     fn as_any_ref(&self) -> &dyn core::any::Any {
82dd9f1fc1STingHuang         self
83dd9f1fc1STingHuang     }
84dd9f1fc1STingHuang 
85dd9f1fc1STingHuang     fn root_inode(&self) -> Arc<dyn super::vfs::IndexNode> {
86dd9f1fc1STingHuang         return self.root_inode.clone();
87dd9f1fc1STingHuang     }
88dd9f1fc1STingHuang 
89dd9f1fc1STingHuang     fn info(&self) -> super::vfs::FsInfo {
90dd9f1fc1STingHuang         return FsInfo {
91dd9f1fc1STingHuang             blk_dev_id: 0,
92dd9f1fc1STingHuang             max_name_len: SYSFS_MAX_NAMELEN,
93dd9f1fc1STingHuang         };
94dd9f1fc1STingHuang     }
95dd9f1fc1STingHuang }
96dd9f1fc1STingHuang 
97dd9f1fc1STingHuang impl SysFS {
98dd9f1fc1STingHuang     pub fn new() -> Arc<Self> {
99dd9f1fc1STingHuang         // 初始化root inode
100dd9f1fc1STingHuang         let root: Arc<LockedSysFSInode> = Arc::new(LockedSysFSInode(SpinLock::new(
101dd9f1fc1STingHuang             // /sys 的权限设置为 读+执行,root 可以读写
102dd9f1fc1STingHuang             // root 的 parent 是空指针
103dd9f1fc1STingHuang             SysFSInode::new(FileType::Dir, 0o755 as u32, 0),
104dd9f1fc1STingHuang         )));
105dd9f1fc1STingHuang 
106dd9f1fc1STingHuang         let sysfs: Arc<SysFS> = Arc::new(SysFS { root_inode: root });
107dd9f1fc1STingHuang 
108dd9f1fc1STingHuang         // 对root inode加锁,并继续完成初始化工作
109dd9f1fc1STingHuang         let mut root_guard: SpinLockGuard<SysFSInode> = sysfs.root_inode.0.lock();
110dd9f1fc1STingHuang         root_guard.parent = Arc::downgrade(&sysfs.root_inode);
111dd9f1fc1STingHuang         root_guard.self_ref = Arc::downgrade(&sysfs.root_inode);
112dd9f1fc1STingHuang         root_guard.fs = Arc::downgrade(&sysfs);
113dd9f1fc1STingHuang         // 释放锁
114dd9f1fc1STingHuang         drop(root_guard);
115dd9f1fc1STingHuang 
116dd9f1fc1STingHuang         // 创建文件夹
117dd9f1fc1STingHuang         let root: &Arc<LockedSysFSInode> = &sysfs.root_inode;
118dd9f1fc1STingHuang         match root.add_dir("devices") {
119dd9f1fc1STingHuang             Ok(devices) => unsafe {
120dd9f1fc1STingHuang                 __SYS_DEVICES_INODE = Box::leak(Box::new(devices));
121dd9f1fc1STingHuang             },
122dd9f1fc1STingHuang             Err(_) => panic!("SysFS: Failed to create /sys/devices"),
123dd9f1fc1STingHuang         }
124dd9f1fc1STingHuang 
125dd9f1fc1STingHuang         match root.add_dir("bus") {
126dd9f1fc1STingHuang             Ok(bus) => unsafe {
127dd9f1fc1STingHuang                 __SYS_BUS_INODE = Box::leak(Box::new(bus));
128dd9f1fc1STingHuang             },
129dd9f1fc1STingHuang             Err(_) => panic!("SysFS: Failed to create /sys/bus"),
130dd9f1fc1STingHuang         }
131dd9f1fc1STingHuang 
132dd9f1fc1STingHuang         match root.add_dir("class") {
133dd9f1fc1STingHuang             Ok(class) => unsafe {
134dd9f1fc1STingHuang                 __SYS_CLASS_INODE = Box::leak(Box::new(class));
135dd9f1fc1STingHuang             },
136dd9f1fc1STingHuang             Err(_) => panic!("SysFS: Failed to create /sys/class"),
137dd9f1fc1STingHuang         }
138dd9f1fc1STingHuang 
139dd9f1fc1STingHuang         match root.add_dir("fs") {
140dd9f1fc1STingHuang             Ok(fs) => unsafe {
141dd9f1fc1STingHuang                 __SYS_FS_INODE = Box::leak(Box::new(fs));
142dd9f1fc1STingHuang             },
143dd9f1fc1STingHuang             Err(_) => panic!("SysFS: Failed to create /sys/fs"),
144dd9f1fc1STingHuang         }
145*b087521eSChiichen 
146dd9f1fc1STingHuang         return sysfs;
147dd9f1fc1STingHuang     }
148dd9f1fc1STingHuang }
149dd9f1fc1STingHuang 
150dd9f1fc1STingHuang /// @brief sys文件i节点(锁)
151dd9f1fc1STingHuang #[derive(Debug)]
152dd9f1fc1STingHuang pub struct LockedSysFSInode(SpinLock<SysFSInode>);
153dd9f1fc1STingHuang 
154dd9f1fc1STingHuang impl IndexNode for LockedSysFSInode {
155dd9f1fc1STingHuang     fn as_any_ref(&self) -> &dyn core::any::Any {
156dd9f1fc1STingHuang         self
157dd9f1fc1STingHuang     }
158dd9f1fc1STingHuang 
159dd9f1fc1STingHuang     fn open(
160dd9f1fc1STingHuang         &self,
161dd9f1fc1STingHuang         _data: &mut super::vfs::FilePrivateData,
162dd9f1fc1STingHuang         _mode: &FileMode,
163dd9f1fc1STingHuang     ) -> Result<(), SystemError> {
164dd9f1fc1STingHuang         return Ok(());
165dd9f1fc1STingHuang     }
166dd9f1fc1STingHuang 
167dd9f1fc1STingHuang     fn close(&self, _data: &mut super::vfs::FilePrivateData) -> Result<(), SystemError> {
168dd9f1fc1STingHuang         return Ok(());
169dd9f1fc1STingHuang     }
170dd9f1fc1STingHuang 
171dd9f1fc1STingHuang     fn read_at(
172dd9f1fc1STingHuang         &self,
173dd9f1fc1STingHuang         _offset: usize,
174dd9f1fc1STingHuang         _len: usize,
175dd9f1fc1STingHuang         _buf: &mut [u8],
176dd9f1fc1STingHuang         _data: &mut super::vfs::FilePrivateData,
177dd9f1fc1STingHuang     ) -> Result<usize, SystemError> {
178dd9f1fc1STingHuang         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
179dd9f1fc1STingHuang     }
180dd9f1fc1STingHuang 
181dd9f1fc1STingHuang     fn write_at(
182dd9f1fc1STingHuang         &self,
183dd9f1fc1STingHuang         _offset: usize,
184dd9f1fc1STingHuang         _len: usize,
185dd9f1fc1STingHuang         _buf: &[u8],
186dd9f1fc1STingHuang         _data: &mut super::vfs::FilePrivateData,
187dd9f1fc1STingHuang     ) -> Result<usize, SystemError> {
188dd9f1fc1STingHuang         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
189dd9f1fc1STingHuang     }
190dd9f1fc1STingHuang 
191dd9f1fc1STingHuang     fn poll(&self) -> Result<super::vfs::PollStatus, SystemError> {
192dd9f1fc1STingHuang         // 加锁
193dd9f1fc1STingHuang         let inode: SpinLockGuard<SysFSInode> = self.0.lock();
194dd9f1fc1STingHuang 
195dd9f1fc1STingHuang         // 检查当前inode是否为一个文件夹,如果是的话,就返回错误
196dd9f1fc1STingHuang         if inode.metadata.file_type == FileType::Dir {
197dd9f1fc1STingHuang             return Err(SystemError::EISDIR);
198dd9f1fc1STingHuang         }
199dd9f1fc1STingHuang 
200dd9f1fc1STingHuang         return Ok(PollStatus::READ | PollStatus::WRITE);
201dd9f1fc1STingHuang     }
202dd9f1fc1STingHuang 
203dd9f1fc1STingHuang     fn metadata(&self) -> Result<Metadata, SystemError> {
204dd9f1fc1STingHuang         return Ok(self.0.lock().metadata.clone());
205dd9f1fc1STingHuang     }
206dd9f1fc1STingHuang 
207dd9f1fc1STingHuang     fn fs(&self) -> Arc<dyn FileSystem> {
208dd9f1fc1STingHuang         return self.0.lock().fs.upgrade().unwrap();
209dd9f1fc1STingHuang     }
210dd9f1fc1STingHuang 
211dd9f1fc1STingHuang     fn get_entry_name(&self, ino: super::vfs::InodeId) -> Result<String, SystemError> {
212dd9f1fc1STingHuang         let inode: SpinLockGuard<SysFSInode> = self.0.lock();
213dd9f1fc1STingHuang         if inode.metadata.file_type != FileType::Dir {
214dd9f1fc1STingHuang             return Err(SystemError::ENOTDIR);
215dd9f1fc1STingHuang         }
216dd9f1fc1STingHuang 
217dd9f1fc1STingHuang         match ino {
218dd9f1fc1STingHuang             0 => {
219dd9f1fc1STingHuang                 return Ok(String::from("."));
220dd9f1fc1STingHuang             }
221dd9f1fc1STingHuang             1 => {
222dd9f1fc1STingHuang                 return Ok(String::from(".."));
223dd9f1fc1STingHuang             }
224dd9f1fc1STingHuang             ino => {
225dd9f1fc1STingHuang                 // 暴力遍历所有的children,判断inode id是否相同
226dd9f1fc1STingHuang                 // TODO: 优化这里,这个地方性能很差!
227dd9f1fc1STingHuang                 let mut key: Vec<String> = inode
228dd9f1fc1STingHuang                     .children
229dd9f1fc1STingHuang                     .keys()
230dd9f1fc1STingHuang                     .filter(|k| inode.children.get(*k).unwrap().metadata().unwrap().inode_id == ino)
231dd9f1fc1STingHuang                     .cloned()
232dd9f1fc1STingHuang                     .collect();
233dd9f1fc1STingHuang 
234dd9f1fc1STingHuang                 match key.len() {
235dd9f1fc1STingHuang                     0=>{return Err(SystemError::ENOENT);}
236dd9f1fc1STingHuang                     1=>{return Ok(key.remove(0));}
237dd9f1fc1STingHuang                     _ => panic!("Sysfs get_entry_name: key.len()={key_len}>1, current inode_id={inode_id}, to find={to_find}", key_len=key.len(), inode_id = inode.metadata.inode_id, to_find=ino)
238dd9f1fc1STingHuang                 }
239dd9f1fc1STingHuang             }
240dd9f1fc1STingHuang         }
241dd9f1fc1STingHuang     }
242dd9f1fc1STingHuang 
243dd9f1fc1STingHuang     fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> {
244dd9f1fc1STingHuang         let inode = self.0.lock();
245dd9f1fc1STingHuang 
246dd9f1fc1STingHuang         if inode.metadata.file_type != FileType::Dir {
247dd9f1fc1STingHuang             return Err(SystemError::ENOTDIR);
248dd9f1fc1STingHuang         }
249dd9f1fc1STingHuang 
250dd9f1fc1STingHuang         match name {
251dd9f1fc1STingHuang             "" | "." => {
252dd9f1fc1STingHuang                 return Ok(inode.self_ref.upgrade().ok_or(SystemError::ENOENT)?);
253dd9f1fc1STingHuang             }
254dd9f1fc1STingHuang             ".." => {
255dd9f1fc1STingHuang                 return Ok(inode.parent.upgrade().ok_or(SystemError::ENOENT)?);
256dd9f1fc1STingHuang             }
257dd9f1fc1STingHuang             name => {
258dd9f1fc1STingHuang                 // 在子目录项中查找
259dd9f1fc1STingHuang                 // match inode.children.get(name) {
260dd9f1fc1STingHuang                 //     Some(_) => {}
261dd9f1fc1STingHuang                 //     None => kdebug!("Sysfs find {} error", name),
262dd9f1fc1STingHuang                 // }
263dd9f1fc1STingHuang                 return Ok(inode.children.get(name).ok_or(SystemError::ENOENT)?.clone());
264dd9f1fc1STingHuang             }
265dd9f1fc1STingHuang         }
266dd9f1fc1STingHuang     }
267dd9f1fc1STingHuang 
268dd9f1fc1STingHuang     fn ioctl(&self, _cmd: u32, _data: usize) -> Result<usize, SystemError> {
269dd9f1fc1STingHuang         Err(SystemError::EOPNOTSUPP_OR_ENOTSUP)
270dd9f1fc1STingHuang     }
271dd9f1fc1STingHuang 
272dd9f1fc1STingHuang     fn list(&self) -> Result<Vec<String>, SystemError> {
273dd9f1fc1STingHuang         let info = self.metadata()?;
274dd9f1fc1STingHuang         if info.file_type != FileType::Dir {
275dd9f1fc1STingHuang             return Err(SystemError::ENOTDIR);
276dd9f1fc1STingHuang         }
277dd9f1fc1STingHuang 
278dd9f1fc1STingHuang         let mut keys: Vec<String> = Vec::new();
279dd9f1fc1STingHuang         keys.push(String::from("."));
280dd9f1fc1STingHuang         keys.push(String::from(".."));
281dd9f1fc1STingHuang         keys.append(&mut self.0.lock().children.keys().cloned().collect());
282dd9f1fc1STingHuang 
283dd9f1fc1STingHuang         return Ok(keys);
284dd9f1fc1STingHuang     }
285dd9f1fc1STingHuang }
286dd9f1fc1STingHuang 
287dd9f1fc1STingHuang impl LockedSysFSInode {
288dd9f1fc1STingHuang     fn do_create_with_data(
289dd9f1fc1STingHuang         &self,
290dd9f1fc1STingHuang         mut guard: SpinLockGuard<SysFSInode>,
291dd9f1fc1STingHuang         _name: &str,
292dd9f1fc1STingHuang         _file_type: FileType,
293dd9f1fc1STingHuang         _mode: u32,
294dd9f1fc1STingHuang         _data: usize,
295dd9f1fc1STingHuang     ) -> Result<Arc<dyn IndexNode>, SystemError> {
296dd9f1fc1STingHuang         if guard.metadata.file_type != FileType::Dir {
297dd9f1fc1STingHuang             return Err(SystemError::ENOTDIR);
298dd9f1fc1STingHuang         }
299dd9f1fc1STingHuang 
300dd9f1fc1STingHuang         // 如果有重名的,则返回
301dd9f1fc1STingHuang         if guard.children.contains_key(_name) {
302dd9f1fc1STingHuang             return Err(SystemError::EEXIST);
303dd9f1fc1STingHuang         }
304dd9f1fc1STingHuang 
305dd9f1fc1STingHuang         // 创建inode
306dd9f1fc1STingHuang         let result: Arc<LockedSysFSInode> = Arc::new(LockedSysFSInode(SpinLock::new(SysFSInode {
307dd9f1fc1STingHuang             parent: guard.self_ref.clone(),
308dd9f1fc1STingHuang             self_ref: Weak::default(),
309dd9f1fc1STingHuang             children: BTreeMap::new(),
310dd9f1fc1STingHuang             metadata: Metadata {
311dd9f1fc1STingHuang                 dev_id: 0,
312dd9f1fc1STingHuang                 inode_id: generate_inode_id(),
313dd9f1fc1STingHuang                 size: 0,
314dd9f1fc1STingHuang                 blk_size: 0,
315dd9f1fc1STingHuang                 blocks: 0,
316dd9f1fc1STingHuang                 atime: TimeSpec::default(),
317dd9f1fc1STingHuang                 mtime: TimeSpec::default(),
318dd9f1fc1STingHuang                 ctime: TimeSpec::default(),
319dd9f1fc1STingHuang                 file_type: _file_type,
320dd9f1fc1STingHuang                 mode: _mode,
321dd9f1fc1STingHuang                 nlinks: 1,
322dd9f1fc1STingHuang                 uid: 0,
323dd9f1fc1STingHuang                 gid: 0,
324dd9f1fc1STingHuang                 raw_dev: _data,
325dd9f1fc1STingHuang             },
326dd9f1fc1STingHuang             fs: guard.fs.clone(),
327dd9f1fc1STingHuang         })));
328dd9f1fc1STingHuang 
329dd9f1fc1STingHuang         // 初始化inode的自引用的weak指针
330dd9f1fc1STingHuang         result.0.lock().self_ref = Arc::downgrade(&result);
331dd9f1fc1STingHuang 
332dd9f1fc1STingHuang         // 将子inode插入父inode的B树中
333dd9f1fc1STingHuang         guard.children.insert(String::from(_name), result.clone());
334dd9f1fc1STingHuang         return Ok(result);
335dd9f1fc1STingHuang     }
336dd9f1fc1STingHuang 
337dd9f1fc1STingHuang     /// @brief 在当前目录下,创建一个目录
338dd9f1fc1STingHuang     /// @param name: 目录名
339dd9f1fc1STingHuang     /// @return 成功返回目录inode, 失败返回Err(错误码)
340dd9f1fc1STingHuang     #[inline]
341dd9f1fc1STingHuang     #[allow(dead_code)]
342dd9f1fc1STingHuang     pub fn add_dir(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> {
343dd9f1fc1STingHuang         let guard: SpinLockGuard<SysFSInode> = self.0.lock();
344dd9f1fc1STingHuang 
345dd9f1fc1STingHuang         if guard.children.contains_key(name) {
346dd9f1fc1STingHuang             return Err(SystemError::EEXIST);
347dd9f1fc1STingHuang         }
348dd9f1fc1STingHuang 
349dd9f1fc1STingHuang         match self.do_create_with_data(guard, name, FileType::Dir, 0o755 as u32, 0) {
350dd9f1fc1STingHuang             Ok(inode) => return Ok(inode),
351dd9f1fc1STingHuang             Err(err) => {
352dd9f1fc1STingHuang                 return Err(err);
353dd9f1fc1STingHuang             }
354dd9f1fc1STingHuang         };
355dd9f1fc1STingHuang     }
356dd9f1fc1STingHuang 
357dd9f1fc1STingHuang     /// @brief 在当前目录下,创建一个二进制文件
358dd9f1fc1STingHuang     /// @param name: 文件名
359dd9f1fc1STingHuang     /// @return 成功返回Ok(()), 失败返回Err(错误码)
360dd9f1fc1STingHuang     #[inline]
361dd9f1fc1STingHuang     #[allow(dead_code)]
362dd9f1fc1STingHuang     pub fn add_file(&self, name: &str, file: Arc<dyn IndexNode>) -> Result<(), SystemError> {
363dd9f1fc1STingHuang         let mut this = self.0.lock();
364dd9f1fc1STingHuang 
365dd9f1fc1STingHuang         if this.children.contains_key(name) {
366dd9f1fc1STingHuang             return Err(SystemError::EEXIST);
367dd9f1fc1STingHuang         }
368dd9f1fc1STingHuang 
369dd9f1fc1STingHuang         this.children.insert(name.to_string(), file);
370dd9f1fc1STingHuang         return Ok(());
371dd9f1fc1STingHuang     }
372dd9f1fc1STingHuang 
373dd9f1fc1STingHuang     /// @brief 为该inode创建硬链接
374dd9f1fc1STingHuang     /// @param None
375dd9f1fc1STingHuang     /// @return 当前inode强引用
376dd9f1fc1STingHuang     #[inline]
377dd9f1fc1STingHuang     #[allow(dead_code)]
378dd9f1fc1STingHuang     pub fn link(&self) -> Arc<dyn IndexNode> {
379dd9f1fc1STingHuang         return self
380dd9f1fc1STingHuang             .0
381dd9f1fc1STingHuang             .lock()
382dd9f1fc1STingHuang             .self_ref
383dd9f1fc1STingHuang             .clone()
384dd9f1fc1STingHuang             .upgrade()
385dd9f1fc1STingHuang             .ok_or(SystemError::E2BIG)
386dd9f1fc1STingHuang             .unwrap();
387dd9f1fc1STingHuang     }
388dd9f1fc1STingHuang 
389dd9f1fc1STingHuang     pub fn remove(&self, name: &str) -> Result<(), SystemError> {
390dd9f1fc1STingHuang         let x = self
391dd9f1fc1STingHuang             .0
392dd9f1fc1STingHuang             .lock()
393dd9f1fc1STingHuang             .children
394dd9f1fc1STingHuang             .remove(name)
395dd9f1fc1STingHuang             .ok_or(SystemError::ENOENT)?;
396dd9f1fc1STingHuang 
397dd9f1fc1STingHuang         drop(x);
398dd9f1fc1STingHuang         return Ok(());
399dd9f1fc1STingHuang     }
400dd9f1fc1STingHuang }
401dd9f1fc1STingHuang 
402dd9f1fc1STingHuang /// @brief sys文件i节点(无锁)
403dd9f1fc1STingHuang #[derive(Debug)]
404dd9f1fc1STingHuang pub struct SysFSInode {
405dd9f1fc1STingHuang     /// 指向父Inode的弱引用
406dd9f1fc1STingHuang     parent: Weak<LockedSysFSInode>,
407dd9f1fc1STingHuang     /// 指向自身的弱引用
408dd9f1fc1STingHuang     self_ref: Weak<LockedSysFSInode>,
409dd9f1fc1STingHuang     /// 子Inode的B树
410dd9f1fc1STingHuang     children: BTreeMap<String, Arc<dyn IndexNode>>,
411dd9f1fc1STingHuang     /// 指向inode所在的文件系统对象的指针
412dd9f1fc1STingHuang     fs: Weak<SysFS>,
413dd9f1fc1STingHuang     /// INode 元数据
414dd9f1fc1STingHuang     metadata: Metadata,
415dd9f1fc1STingHuang }
416dd9f1fc1STingHuang 
417dd9f1fc1STingHuang impl SysFSInode {
418dd9f1fc1STingHuang     pub fn new(dev_type_: FileType, mode_: u32, data_: usize) -> Self {
419dd9f1fc1STingHuang         return Self::new_with_parent(Weak::default(), dev_type_, mode_, data_);
420dd9f1fc1STingHuang     }
421dd9f1fc1STingHuang 
422dd9f1fc1STingHuang     pub fn new_with_parent(
423dd9f1fc1STingHuang         parent: Weak<LockedSysFSInode>,
424dd9f1fc1STingHuang         dev_type_: FileType,
425dd9f1fc1STingHuang         mode_: u32,
426dd9f1fc1STingHuang         data_: usize,
427dd9f1fc1STingHuang     ) -> Self {
428dd9f1fc1STingHuang         return SysFSInode {
429dd9f1fc1STingHuang             parent: parent,
430dd9f1fc1STingHuang             self_ref: Weak::default(),
431dd9f1fc1STingHuang             children: BTreeMap::new(),
432dd9f1fc1STingHuang             metadata: Metadata {
433dd9f1fc1STingHuang                 dev_id: 1,
434dd9f1fc1STingHuang                 inode_id: generate_inode_id(),
435dd9f1fc1STingHuang                 size: 0,
436dd9f1fc1STingHuang                 blk_size: 0,
437dd9f1fc1STingHuang                 blocks: 0,
438dd9f1fc1STingHuang                 atime: TimeSpec::default(),
439dd9f1fc1STingHuang                 mtime: TimeSpec::default(),
440dd9f1fc1STingHuang                 ctime: TimeSpec::default(),
441dd9f1fc1STingHuang                 file_type: dev_type_, // 文件夹
442dd9f1fc1STingHuang                 mode: mode_,
443dd9f1fc1STingHuang                 nlinks: 1,
444dd9f1fc1STingHuang                 uid: 0,
445dd9f1fc1STingHuang                 gid: 0,
446dd9f1fc1STingHuang                 raw_dev: data_,
447dd9f1fc1STingHuang             },
448dd9f1fc1STingHuang             fs: Weak::default(),
449dd9f1fc1STingHuang         };
450dd9f1fc1STingHuang     }
451dd9f1fc1STingHuang }
452*b087521eSChiichen 
453*b087521eSChiichen pub fn sysfs_init() -> Result<(), SystemError> {
454*b087521eSChiichen     static INIT: Once = Once::new();
455*b087521eSChiichen     let mut result = None;
456*b087521eSChiichen     INIT.call_once(|| {
457*b087521eSChiichen         kinfo!("Initializing SysFS...");
458*b087521eSChiichen         // 创建 sysfs 实例
459*b087521eSChiichen         let sysfs: Arc<SysFS> = SysFS::new();
460*b087521eSChiichen 
461*b087521eSChiichen         // sysfs 挂载
462*b087521eSChiichen         let _t = ROOT_INODE()
463*b087521eSChiichen             .find("sys")
464*b087521eSChiichen             .expect("Cannot find /sys")
465*b087521eSChiichen             .mount(sysfs)
466*b087521eSChiichen             .expect("Failed to mount sysfs");
467*b087521eSChiichen         kinfo!("SysFS mounted.");
468*b087521eSChiichen 
469*b087521eSChiichen         // 初始化platform总线
470*b087521eSChiichen         platform_bus_init().expect("platform bus init failed");
471*b087521eSChiichen 
472*b087521eSChiichen         sys_bus_init(&SYS_BUS_INODE()).unwrap_or_else(|err| {
473*b087521eSChiichen             panic!("sys_bus_init failed: {:?}", err);
474*b087521eSChiichen         });
475*b087521eSChiichen 
476*b087521eSChiichen         kdebug!("sys_bus_init result: {:?}", SYS_BUS_INODE().list());
477*b087521eSChiichen         result = Some(Ok(()));
478*b087521eSChiichen     });
479*b087521eSChiichen 
480*b087521eSChiichen     return result.unwrap();
481*b087521eSChiichen }
482