xref: /DragonOS/kernel/src/filesystem/kernfs/mod.rs (revision a03c4f9dee5705207325c56629c0ccd219168f10)
1*a03c4f9dSLoGin use core::{cmp::min, fmt::Debug, intrinsics::unlikely};
26b4e7a29SLoGin 
36b4e7a29SLoGin use alloc::{
46b4e7a29SLoGin     string::String,
56b4e7a29SLoGin     sync::{Arc, Weak},
66b4e7a29SLoGin     vec::Vec,
76b4e7a29SLoGin };
86b4e7a29SLoGin use hashbrown::HashMap;
96b4e7a29SLoGin 
106b4e7a29SLoGin use crate::{
1106d5e247SLoGin     libs::{
1206d5e247SLoGin         casting::DowncastArc,
1306d5e247SLoGin         rwlock::RwLock,
1406d5e247SLoGin         spinlock::{SpinLock, SpinLockGuard},
1506d5e247SLoGin     },
166b4e7a29SLoGin     syscall::SystemError,
176b4e7a29SLoGin     time::TimeSpec,
186b4e7a29SLoGin };
196b4e7a29SLoGin 
206b4e7a29SLoGin use self::callback::{KernCallbackData, KernFSCallback, KernInodePrivateData};
216b4e7a29SLoGin 
226b4e7a29SLoGin use super::vfs::{
236b4e7a29SLoGin     core::generate_inode_id, file::FileMode, syscall::ModeType, FilePrivateData, FileSystem,
246b4e7a29SLoGin     FileType, FsInfo, IndexNode, InodeId, Metadata, PollStatus,
256b4e7a29SLoGin };
266b4e7a29SLoGin 
276b4e7a29SLoGin pub mod callback;
286b4e7a29SLoGin 
296b4e7a29SLoGin #[derive(Debug)]
306b4e7a29SLoGin pub struct KernFS {
316b4e7a29SLoGin     root_inode: Arc<KernFSInode>,
326b4e7a29SLoGin }
336b4e7a29SLoGin 
346b4e7a29SLoGin impl FileSystem for KernFS {
356b4e7a29SLoGin     fn as_any_ref(&self) -> &dyn core::any::Any {
366b4e7a29SLoGin         self
376b4e7a29SLoGin     }
386b4e7a29SLoGin 
396b4e7a29SLoGin     fn info(&self) -> FsInfo {
406b4e7a29SLoGin         return FsInfo {
416b4e7a29SLoGin             blk_dev_id: 0,
426b4e7a29SLoGin             max_name_len: KernFS::MAX_NAMELEN,
436b4e7a29SLoGin         };
446b4e7a29SLoGin     }
456b4e7a29SLoGin 
466b4e7a29SLoGin     fn root_inode(&self) -> Arc<dyn IndexNode> {
476b4e7a29SLoGin         return self.root_inode.clone();
486b4e7a29SLoGin     }
496b4e7a29SLoGin }
506b4e7a29SLoGin 
516b4e7a29SLoGin impl KernFS {
526b4e7a29SLoGin     pub const MAX_NAMELEN: usize = 4096;
536b4e7a29SLoGin 
546b4e7a29SLoGin     #[allow(dead_code)]
556b4e7a29SLoGin     pub fn new() -> Arc<Self> {
566b4e7a29SLoGin         let root_inode = Self::create_root_inode();
576b4e7a29SLoGin         let fs = Arc::new(Self {
586b4e7a29SLoGin             root_inode: root_inode.clone(),
596b4e7a29SLoGin         });
606b4e7a29SLoGin 
616b4e7a29SLoGin         {
626b4e7a29SLoGin             let ptr = root_inode.as_ref() as *const KernFSInode as *mut KernFSInode;
636b4e7a29SLoGin             unsafe {
646b4e7a29SLoGin                 (*ptr).self_ref = Arc::downgrade(&root_inode);
656b4e7a29SLoGin             }
666b4e7a29SLoGin         }
67*a03c4f9dSLoGin         root_inode.inner.write().parent = Arc::downgrade(&root_inode);
686b4e7a29SLoGin         *root_inode.fs.write() = Arc::downgrade(&fs);
696b4e7a29SLoGin         return fs;
706b4e7a29SLoGin     }
716b4e7a29SLoGin 
726b4e7a29SLoGin     fn create_root_inode() -> Arc<KernFSInode> {
736b4e7a29SLoGin         let metadata = Metadata {
746b4e7a29SLoGin             size: 0,
756b4e7a29SLoGin             mode: ModeType::from_bits_truncate(0o755),
766b4e7a29SLoGin             uid: 0,
776b4e7a29SLoGin             gid: 0,
786b4e7a29SLoGin             blk_size: 0,
796b4e7a29SLoGin             blocks: 0,
806b4e7a29SLoGin             atime: TimeSpec::new(0, 0),
816b4e7a29SLoGin             mtime: TimeSpec::new(0, 0),
826b4e7a29SLoGin             ctime: TimeSpec::new(0, 0),
836b4e7a29SLoGin             dev_id: 0,
846b4e7a29SLoGin             inode_id: generate_inode_id(),
856b4e7a29SLoGin             file_type: FileType::Dir,
866b4e7a29SLoGin             nlinks: 1,
876b4e7a29SLoGin             raw_dev: 0,
886b4e7a29SLoGin         };
896b4e7a29SLoGin         let root_inode = Arc::new(KernFSInode {
9006d5e247SLoGin             name: String::from(""),
91*a03c4f9dSLoGin             inner: RwLock::new(InnerKernFSInode {
926b4e7a29SLoGin                 parent: Weak::new(),
936b4e7a29SLoGin                 metadata,
94*a03c4f9dSLoGin                 symlink_target: None,
95*a03c4f9dSLoGin                 symlink_target_absolute_path: None,
966b4e7a29SLoGin             }),
976b4e7a29SLoGin             self_ref: Weak::new(),
986b4e7a29SLoGin             fs: RwLock::new(Weak::new()),
996b4e7a29SLoGin             private_data: SpinLock::new(None),
1006b4e7a29SLoGin             callback: None,
1016b4e7a29SLoGin             children: SpinLock::new(HashMap::new()),
1026b4e7a29SLoGin             inode_type: KernInodeType::Dir,
1036b4e7a29SLoGin         });
1046b4e7a29SLoGin 
1056b4e7a29SLoGin         return root_inode;
1066b4e7a29SLoGin     }
1076b4e7a29SLoGin }
1086b4e7a29SLoGin 
1096b4e7a29SLoGin #[derive(Debug)]
1106b4e7a29SLoGin pub struct KernFSInode {
111*a03c4f9dSLoGin     inner: RwLock<InnerKernFSInode>,
1126b4e7a29SLoGin     /// 指向当前Inode所属的文件系统的弱引用
1136b4e7a29SLoGin     fs: RwLock<Weak<KernFS>>,
1146b4e7a29SLoGin     /// 指向自身的弱引用
1156b4e7a29SLoGin     self_ref: Weak<KernFSInode>,
1166b4e7a29SLoGin     /// 私有数据
1176b4e7a29SLoGin     private_data: SpinLock<Option<KernInodePrivateData>>,
1186b4e7a29SLoGin     /// 回调函数
1196b4e7a29SLoGin     callback: Option<&'static dyn KernFSCallback>,
1206b4e7a29SLoGin     /// 子Inode
1216b4e7a29SLoGin     children: SpinLock<HashMap<String, Arc<KernFSInode>>>,
1226b4e7a29SLoGin     /// Inode类型
1236b4e7a29SLoGin     inode_type: KernInodeType,
12406d5e247SLoGin     /// Inode名称
12506d5e247SLoGin     name: String,
1266b4e7a29SLoGin }
1276b4e7a29SLoGin 
1286b4e7a29SLoGin #[derive(Debug)]
1296b4e7a29SLoGin pub struct InnerKernFSInode {
1306b4e7a29SLoGin     parent: Weak<KernFSInode>,
1316b4e7a29SLoGin 
1326b4e7a29SLoGin     /// 当前inode的元数据
1336b4e7a29SLoGin     metadata: Metadata,
134*a03c4f9dSLoGin     /// 符号链接指向的inode(仅当inode_type为SymLink时有效)
135*a03c4f9dSLoGin     symlink_target: Option<Weak<KernFSInode>>,
136*a03c4f9dSLoGin     symlink_target_absolute_path: Option<String>,
1376b4e7a29SLoGin }
1386b4e7a29SLoGin 
1396b4e7a29SLoGin impl IndexNode for KernFSInode {
1406b4e7a29SLoGin     fn as_any_ref(&self) -> &dyn core::any::Any {
1416b4e7a29SLoGin         self
1426b4e7a29SLoGin     }
1436b4e7a29SLoGin 
1446b4e7a29SLoGin     fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> {
1456b4e7a29SLoGin         if let Some(callback) = self.callback {
1466b4e7a29SLoGin             let callback_data =
1476b4e7a29SLoGin                 KernCallbackData::new(self.self_ref.upgrade().unwrap(), self.private_data.lock());
1486b4e7a29SLoGin             return callback.open(callback_data);
1496b4e7a29SLoGin         }
1506b4e7a29SLoGin 
1516b4e7a29SLoGin         return Ok(());
1526b4e7a29SLoGin     }
1536b4e7a29SLoGin 
1546b4e7a29SLoGin     fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> {
1556b4e7a29SLoGin         return Ok(());
1566b4e7a29SLoGin     }
1576b4e7a29SLoGin 
1586b4e7a29SLoGin     fn metadata(&self) -> Result<Metadata, SystemError> {
159*a03c4f9dSLoGin         return Ok(self.inner.read().metadata.clone());
1606b4e7a29SLoGin     }
1616b4e7a29SLoGin 
1626b4e7a29SLoGin     fn set_metadata(&self, _metadata: &Metadata) -> Result<(), SystemError> {
1636b4e7a29SLoGin         // 若文件系统没有实现此方法,则返回“不支持”
1646b4e7a29SLoGin         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
1656b4e7a29SLoGin     }
1666b4e7a29SLoGin 
1676b4e7a29SLoGin     fn resize(&self, _len: usize) -> Result<(), SystemError> {
1686b4e7a29SLoGin         return Ok(());
1696b4e7a29SLoGin     }
1706b4e7a29SLoGin 
1716b4e7a29SLoGin     fn create_with_data(
1726b4e7a29SLoGin         &self,
1736b4e7a29SLoGin         _name: &str,
1746b4e7a29SLoGin         _file_type: FileType,
1756b4e7a29SLoGin         _mode: ModeType,
1766b4e7a29SLoGin         _data: usize,
1776b4e7a29SLoGin     ) -> Result<Arc<dyn IndexNode>, SystemError> {
1786b4e7a29SLoGin         // 应当通过kernfs的其它方法来创建文件,而不能从用户态直接调用此方法。
1796b4e7a29SLoGin         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
1806b4e7a29SLoGin     }
1816b4e7a29SLoGin 
1826b4e7a29SLoGin     fn link(&self, _name: &str, _other: &Arc<dyn IndexNode>) -> Result<(), SystemError> {
1836b4e7a29SLoGin         // 应当通过kernfs的其它方法来操作文件,而不能从用户态直接调用此方法。
1846b4e7a29SLoGin         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
1856b4e7a29SLoGin     }
1866b4e7a29SLoGin 
1876b4e7a29SLoGin     fn unlink(&self, _name: &str) -> Result<(), SystemError> {
1886b4e7a29SLoGin         // 应当通过kernfs的其它方法来操作文件,而不能从用户态直接调用此方法。
1896b4e7a29SLoGin         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
1906b4e7a29SLoGin     }
1916b4e7a29SLoGin 
1926b4e7a29SLoGin     fn rmdir(&self, _name: &str) -> Result<(), SystemError> {
1936b4e7a29SLoGin         // 应当通过kernfs的其它方法来操作文件,而不能从用户态直接调用此方法。
1946b4e7a29SLoGin         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
1956b4e7a29SLoGin     }
1966b4e7a29SLoGin 
1976b4e7a29SLoGin     fn move_(
1986b4e7a29SLoGin         &self,
1996b4e7a29SLoGin         _old_name: &str,
2006b4e7a29SLoGin         _target: &Arc<dyn IndexNode>,
2016b4e7a29SLoGin         _new_name: &str,
2026b4e7a29SLoGin     ) -> Result<(), SystemError> {
2036b4e7a29SLoGin         // 应当通过kernfs的其它方法来操作文件,而不能从用户态直接调用此方法。
2046b4e7a29SLoGin         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
2056b4e7a29SLoGin     }
2066b4e7a29SLoGin 
2076b4e7a29SLoGin     fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> {
2086b4e7a29SLoGin         if unlikely(name.len() > KernFS::MAX_NAMELEN) {
2096b4e7a29SLoGin             return Err(SystemError::ENAMETOOLONG);
2106b4e7a29SLoGin         }
2116b4e7a29SLoGin         if unlikely(self.inode_type != KernInodeType::Dir) {
2126b4e7a29SLoGin             return Err(SystemError::ENOTDIR);
2136b4e7a29SLoGin         }
21406d5e247SLoGin         match name {
21506d5e247SLoGin             "" | "." => {
21606d5e247SLoGin                 return Ok(self.self_ref.upgrade().ok_or(SystemError::ENOENT)?);
21706d5e247SLoGin             }
21806d5e247SLoGin 
21906d5e247SLoGin             ".." => {
22006d5e247SLoGin                 return Ok(self
22106d5e247SLoGin                     .inner
222*a03c4f9dSLoGin                     .read()
22306d5e247SLoGin                     .parent
22406d5e247SLoGin                     .upgrade()
22506d5e247SLoGin                     .ok_or(SystemError::ENOENT)?);
22606d5e247SLoGin             }
22706d5e247SLoGin             name => {
22806d5e247SLoGin                 // 在子目录项中查找
22906d5e247SLoGin                 return Ok(self
2306b4e7a29SLoGin                     .children
2316b4e7a29SLoGin                     .lock()
2326b4e7a29SLoGin                     .get(name)
23306d5e247SLoGin                     .ok_or(SystemError::ENOENT)?
23406d5e247SLoGin                     .clone());
23506d5e247SLoGin             }
23606d5e247SLoGin         }
2376b4e7a29SLoGin     }
2386b4e7a29SLoGin 
2396b4e7a29SLoGin     fn get_entry_name(&self, ino: InodeId) -> Result<String, SystemError> {
2406b4e7a29SLoGin         if self.inode_type != KernInodeType::Dir {
2416b4e7a29SLoGin             return Err(SystemError::ENOTDIR);
2426b4e7a29SLoGin         }
2436b4e7a29SLoGin 
2446b4e7a29SLoGin         let children = self.children.lock();
2456b4e7a29SLoGin         let r = children
2466b4e7a29SLoGin             .iter()
2476b4e7a29SLoGin             .find(|(_, v)| v.metadata().unwrap().inode_id == ino)
2486b4e7a29SLoGin             .map(|(k, _)| k.clone());
2496b4e7a29SLoGin 
2506b4e7a29SLoGin         return r.ok_or(SystemError::ENOENT);
2516b4e7a29SLoGin     }
2526b4e7a29SLoGin 
2536b4e7a29SLoGin     fn get_entry_name_and_metadata(&self, ino: InodeId) -> Result<(String, Metadata), SystemError> {
2546b4e7a29SLoGin         // 如果有条件,请在文件系统中使用高效的方式实现本接口,而不是依赖这个低效率的默认实现。
2556b4e7a29SLoGin         let name = self.get_entry_name(ino)?;
2566b4e7a29SLoGin         let entry = self.find(&name)?;
2576b4e7a29SLoGin         return Ok((name, entry.metadata()?));
2586b4e7a29SLoGin     }
2596b4e7a29SLoGin 
2606b4e7a29SLoGin     fn ioctl(&self, _cmd: u32, _data: usize) -> Result<usize, SystemError> {
2616b4e7a29SLoGin         // 若文件系统没有实现此方法,则返回“不支持”
2626b4e7a29SLoGin         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
2636b4e7a29SLoGin     }
2646b4e7a29SLoGin 
2656b4e7a29SLoGin     fn truncate(&self, _len: usize) -> Result<(), SystemError> {
2666b4e7a29SLoGin         // 应当通过kernfs的其它方法来操作文件,而不能从用户态直接调用此方法。
2676b4e7a29SLoGin         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
2686b4e7a29SLoGin     }
2696b4e7a29SLoGin 
2706b4e7a29SLoGin     fn sync(&self) -> Result<(), SystemError> {
2716b4e7a29SLoGin         return Ok(());
2726b4e7a29SLoGin     }
2736b4e7a29SLoGin 
2746b4e7a29SLoGin     fn fs(&self) -> Arc<dyn FileSystem> {
2756b4e7a29SLoGin         return self.fs.read().upgrade().unwrap();
2766b4e7a29SLoGin     }
2776b4e7a29SLoGin 
2786b4e7a29SLoGin     fn list(&self) -> Result<Vec<String>, SystemError> {
27906d5e247SLoGin         let info = self.metadata()?;
28006d5e247SLoGin         if info.file_type != FileType::Dir {
28106d5e247SLoGin             return Err(SystemError::ENOTDIR);
2826b4e7a29SLoGin         }
28306d5e247SLoGin 
28406d5e247SLoGin         let mut keys: Vec<String> = Vec::new();
28506d5e247SLoGin         keys.push(String::from("."));
28606d5e247SLoGin         keys.push(String::from(".."));
28706d5e247SLoGin         self.children
28806d5e247SLoGin             .lock()
28906d5e247SLoGin             .keys()
29006d5e247SLoGin             .into_iter()
29106d5e247SLoGin             .for_each(|x| keys.push(x.clone()));
29206d5e247SLoGin 
29306d5e247SLoGin         return Ok(keys);
2946b4e7a29SLoGin     }
2956b4e7a29SLoGin 
2966b4e7a29SLoGin     fn poll(&self) -> Result<PollStatus, SystemError> {
2976b4e7a29SLoGin         // todo: 根据inode的具体attribute,返回PollStatus
2986b4e7a29SLoGin         return Ok(PollStatus::READ | PollStatus::WRITE);
2996b4e7a29SLoGin     }
3006b4e7a29SLoGin 
3016b4e7a29SLoGin     fn read_at(
3026b4e7a29SLoGin         &self,
3036b4e7a29SLoGin         offset: usize,
3046b4e7a29SLoGin         len: usize,
3056b4e7a29SLoGin         buf: &mut [u8],
3066b4e7a29SLoGin         _data: &mut FilePrivateData,
3076b4e7a29SLoGin     ) -> Result<usize, SystemError> {
308*a03c4f9dSLoGin         if self.inode_type == KernInodeType::SymLink {
309*a03c4f9dSLoGin             let inner = self.inner.read();
310*a03c4f9dSLoGin             if offset >= inner.symlink_target_absolute_path.as_ref().unwrap().len() {
311*a03c4f9dSLoGin                 return Ok(0);
312*a03c4f9dSLoGin             }
313*a03c4f9dSLoGin             let len = min(len, buf.len());
314*a03c4f9dSLoGin             let len = min(
315*a03c4f9dSLoGin                 len,
316*a03c4f9dSLoGin                 inner.symlink_target_absolute_path.as_ref().unwrap().len() - offset,
317*a03c4f9dSLoGin             );
318*a03c4f9dSLoGin             buf[0..len].copy_from_slice(
319*a03c4f9dSLoGin                 &inner
320*a03c4f9dSLoGin                     .symlink_target_absolute_path
321*a03c4f9dSLoGin                     .as_ref()
322*a03c4f9dSLoGin                     .unwrap()
323*a03c4f9dSLoGin                     .as_bytes()[offset..offset + len],
324*a03c4f9dSLoGin             );
325*a03c4f9dSLoGin             return Ok(len);
326*a03c4f9dSLoGin         }
3276b4e7a29SLoGin         if self.inode_type != KernInodeType::File {
3286b4e7a29SLoGin             return Err(SystemError::EISDIR);
3296b4e7a29SLoGin         }
3306b4e7a29SLoGin 
3316b4e7a29SLoGin         if self.callback.is_none() {
33206d5e247SLoGin             kwarn!("kernfs: callback is none");
3336b4e7a29SLoGin             return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
3346b4e7a29SLoGin         }
3356b4e7a29SLoGin 
3366b4e7a29SLoGin         let callback_data =
3376b4e7a29SLoGin             KernCallbackData::new(self.self_ref.upgrade().unwrap(), self.private_data.lock());
3386b4e7a29SLoGin         return self
3396b4e7a29SLoGin             .callback
3406b4e7a29SLoGin             .as_ref()
3416b4e7a29SLoGin             .unwrap()
3426b4e7a29SLoGin             .read(callback_data, &mut buf[..len], offset);
3436b4e7a29SLoGin     }
3446b4e7a29SLoGin 
3456b4e7a29SLoGin     fn write_at(
3466b4e7a29SLoGin         &self,
3476b4e7a29SLoGin         offset: usize,
3486b4e7a29SLoGin         len: usize,
3496b4e7a29SLoGin         buf: &[u8],
3506b4e7a29SLoGin         _data: &mut FilePrivateData,
3516b4e7a29SLoGin     ) -> Result<usize, SystemError> {
3526b4e7a29SLoGin         if self.inode_type != KernInodeType::File {
3536b4e7a29SLoGin             return Err(SystemError::EISDIR);
3546b4e7a29SLoGin         }
3556b4e7a29SLoGin 
3566b4e7a29SLoGin         if self.callback.is_none() {
3576b4e7a29SLoGin             return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
3586b4e7a29SLoGin         }
3596b4e7a29SLoGin 
3606b4e7a29SLoGin         let callback_data =
3616b4e7a29SLoGin             KernCallbackData::new(self.self_ref.upgrade().unwrap(), self.private_data.lock());
3626b4e7a29SLoGin         return self
3636b4e7a29SLoGin             .callback
3646b4e7a29SLoGin             .as_ref()
3656b4e7a29SLoGin             .unwrap()
3666b4e7a29SLoGin             .write(callback_data, &buf[..len], offset);
3676b4e7a29SLoGin     }
3686b4e7a29SLoGin }
3696b4e7a29SLoGin 
3706b4e7a29SLoGin impl KernFSInode {
371*a03c4f9dSLoGin     pub fn new(
372*a03c4f9dSLoGin         parent: Option<Arc<KernFSInode>>,
373*a03c4f9dSLoGin         name: String,
374*a03c4f9dSLoGin         mut metadata: Metadata,
375*a03c4f9dSLoGin         inode_type: KernInodeType,
376*a03c4f9dSLoGin         private_data: Option<KernInodePrivateData>,
377*a03c4f9dSLoGin         callback: Option<&'static dyn KernFSCallback>,
378*a03c4f9dSLoGin     ) -> Arc<KernFSInode> {
379*a03c4f9dSLoGin         metadata.file_type = inode_type.into();
380*a03c4f9dSLoGin         let parent: Weak<KernFSInode> = parent.map(|x| Arc::downgrade(&x)).unwrap_or_default();
381*a03c4f9dSLoGin 
382*a03c4f9dSLoGin         let inode = Arc::new(KernFSInode {
383*a03c4f9dSLoGin             name,
384*a03c4f9dSLoGin             inner: RwLock::new(InnerKernFSInode {
385*a03c4f9dSLoGin                 parent: parent.clone(),
386*a03c4f9dSLoGin                 metadata,
387*a03c4f9dSLoGin                 symlink_target: None,
388*a03c4f9dSLoGin                 symlink_target_absolute_path: None,
389*a03c4f9dSLoGin             }),
390*a03c4f9dSLoGin             self_ref: Weak::new(),
391*a03c4f9dSLoGin             fs: RwLock::new(Weak::new()),
392*a03c4f9dSLoGin             private_data: SpinLock::new(private_data),
393*a03c4f9dSLoGin             callback,
394*a03c4f9dSLoGin             children: SpinLock::new(HashMap::new()),
395*a03c4f9dSLoGin             inode_type,
396*a03c4f9dSLoGin         });
397*a03c4f9dSLoGin 
398*a03c4f9dSLoGin         {
399*a03c4f9dSLoGin             let ptr = inode.as_ref() as *const KernFSInode as *mut KernFSInode;
400*a03c4f9dSLoGin             unsafe {
401*a03c4f9dSLoGin                 (*ptr).self_ref = Arc::downgrade(&inode);
402*a03c4f9dSLoGin             }
403*a03c4f9dSLoGin         }
404*a03c4f9dSLoGin         if parent.strong_count() > 0 {
405*a03c4f9dSLoGin             let kernfs = parent
406*a03c4f9dSLoGin                 .upgrade()
407*a03c4f9dSLoGin                 .unwrap()
408*a03c4f9dSLoGin                 .fs()
409*a03c4f9dSLoGin                 .downcast_arc::<KernFS>()
410*a03c4f9dSLoGin                 .expect("KernFSInode::new: parent is not a KernFS instance");
411*a03c4f9dSLoGin             *inode.fs.write() = Arc::downgrade(&kernfs);
412*a03c4f9dSLoGin         }
413*a03c4f9dSLoGin         return inode;
414*a03c4f9dSLoGin     }
415*a03c4f9dSLoGin 
4166b4e7a29SLoGin     /// 在当前inode下增加子目录
4176b4e7a29SLoGin     ///
4186b4e7a29SLoGin     /// ## 参数
4196b4e7a29SLoGin     ///
4206b4e7a29SLoGin     /// - `name`:子目录名称
4216b4e7a29SLoGin     /// - `mode`:子目录权限
4226b4e7a29SLoGin     /// - `private_data`:子目录私有数据
4236b4e7a29SLoGin     /// - `callback`:子目录回调函数
4246b4e7a29SLoGin     ///
4256b4e7a29SLoGin     /// ## 返回值
4266b4e7a29SLoGin     ///
4276b4e7a29SLoGin     /// - 成功:子目录inode
4286b4e7a29SLoGin     /// - 失败:错误码
4296b4e7a29SLoGin     #[allow(dead_code)]
4306b4e7a29SLoGin     #[inline]
4316b4e7a29SLoGin     pub fn add_dir(
4326b4e7a29SLoGin         &self,
4336b4e7a29SLoGin         name: String,
4346b4e7a29SLoGin         mode: ModeType,
4356b4e7a29SLoGin         private_data: Option<KernInodePrivateData>,
4366b4e7a29SLoGin         callback: Option<&'static dyn KernFSCallback>,
4376b4e7a29SLoGin     ) -> Result<Arc<KernFSInode>, SystemError> {
4386b4e7a29SLoGin         if unlikely(self.inode_type != KernInodeType::Dir) {
4396b4e7a29SLoGin             return Err(SystemError::ENOTDIR);
4406b4e7a29SLoGin         }
4416b4e7a29SLoGin 
4426b4e7a29SLoGin         return self.inner_create(name, KernInodeType::Dir, mode, private_data, callback);
4436b4e7a29SLoGin     }
4446b4e7a29SLoGin 
4456b4e7a29SLoGin     /// 在当前inode下增加文件
4466b4e7a29SLoGin     ///
4476b4e7a29SLoGin     /// ## 参数
4486b4e7a29SLoGin     ///
4496b4e7a29SLoGin     /// - `name`:文件名称
4506b4e7a29SLoGin     /// - `mode`:文件权限
4516b4e7a29SLoGin     /// - `private_data`:文件私有数据
4526b4e7a29SLoGin     /// - `callback`:文件回调函数
4536b4e7a29SLoGin     ///
4546b4e7a29SLoGin     /// ## 返回值
4556b4e7a29SLoGin     ///
4566b4e7a29SLoGin     /// - 成功:文件inode
4576b4e7a29SLoGin     /// - 失败:错误码
4586b4e7a29SLoGin     #[allow(dead_code)]
4596b4e7a29SLoGin     #[inline]
4606b4e7a29SLoGin     pub fn add_file(
4616b4e7a29SLoGin         &self,
4626b4e7a29SLoGin         name: String,
4636b4e7a29SLoGin         mode: ModeType,
4646b4e7a29SLoGin         private_data: Option<KernInodePrivateData>,
4656b4e7a29SLoGin         callback: Option<&'static dyn KernFSCallback>,
4666b4e7a29SLoGin     ) -> Result<Arc<KernFSInode>, SystemError> {
4676b4e7a29SLoGin         if unlikely(self.inode_type != KernInodeType::Dir) {
4686b4e7a29SLoGin             return Err(SystemError::ENOTDIR);
4696b4e7a29SLoGin         }
4706b4e7a29SLoGin 
4716b4e7a29SLoGin         return self.inner_create(name, KernInodeType::File, mode, private_data, callback);
4726b4e7a29SLoGin     }
4736b4e7a29SLoGin 
4746b4e7a29SLoGin     fn inner_create(
4756b4e7a29SLoGin         &self,
4766b4e7a29SLoGin         name: String,
4776b4e7a29SLoGin         file_type: KernInodeType,
4786b4e7a29SLoGin         mode: ModeType,
4796b4e7a29SLoGin         private_data: Option<KernInodePrivateData>,
4806b4e7a29SLoGin         callback: Option<&'static dyn KernFSCallback>,
4816b4e7a29SLoGin     ) -> Result<Arc<KernFSInode>, SystemError> {
482*a03c4f9dSLoGin         let size;
483*a03c4f9dSLoGin         match file_type {
484*a03c4f9dSLoGin             KernInodeType::Dir | KernInodeType::SymLink => {
485*a03c4f9dSLoGin                 size = 0;
486*a03c4f9dSLoGin             }
487*a03c4f9dSLoGin             KernInodeType::File => {
488*a03c4f9dSLoGin                 size = 4096;
489*a03c4f9dSLoGin             }
490*a03c4f9dSLoGin         }
49106d5e247SLoGin 
4926b4e7a29SLoGin         let metadata = Metadata {
49306d5e247SLoGin             size,
4946b4e7a29SLoGin             mode,
4956b4e7a29SLoGin             uid: 0,
4966b4e7a29SLoGin             gid: 0,
4976b4e7a29SLoGin             blk_size: 0,
4986b4e7a29SLoGin             blocks: 0,
4996b4e7a29SLoGin             atime: TimeSpec::new(0, 0),
5006b4e7a29SLoGin             mtime: TimeSpec::new(0, 0),
5016b4e7a29SLoGin             ctime: TimeSpec::new(0, 0),
5026b4e7a29SLoGin             dev_id: 0,
5036b4e7a29SLoGin             inode_id: generate_inode_id(),
5046b4e7a29SLoGin             file_type: file_type.into(),
5056b4e7a29SLoGin             nlinks: 1,
5066b4e7a29SLoGin             raw_dev: 0,
5076b4e7a29SLoGin         };
5086b4e7a29SLoGin 
5096b4e7a29SLoGin         let new_inode: Arc<KernFSInode> = Self::new(
51006d5e247SLoGin             Some(self.self_ref.upgrade().unwrap()),
51106d5e247SLoGin             name.clone(),
5126b4e7a29SLoGin             metadata,
51306d5e247SLoGin             file_type,
5146b4e7a29SLoGin             private_data,
5156b4e7a29SLoGin             callback,
5166b4e7a29SLoGin         );
5176b4e7a29SLoGin 
5186b4e7a29SLoGin         self.children.lock().insert(name, new_inode.clone());
5196b4e7a29SLoGin 
5206b4e7a29SLoGin         return Ok(new_inode);
5216b4e7a29SLoGin     }
5226b4e7a29SLoGin 
5236b4e7a29SLoGin     /// 在当前inode下删除子目录或者文件
5246b4e7a29SLoGin     ///
5256b4e7a29SLoGin     /// 如果要删除的是子目录,且子目录不为空,则返回ENOTEMPTY
5266b4e7a29SLoGin     ///
5276b4e7a29SLoGin     /// ## 参数
5286b4e7a29SLoGin     ///
5296b4e7a29SLoGin     /// - `name`:子目录或者文件名称
5306b4e7a29SLoGin     ///
5316b4e7a29SLoGin     /// ## 返回值
5326b4e7a29SLoGin     ///
5336b4e7a29SLoGin     /// - 成功:()
5346b4e7a29SLoGin     /// - 失败:错误码
5356b4e7a29SLoGin     #[allow(dead_code)]
5366b4e7a29SLoGin     pub fn remove(&self, name: &str) -> Result<(), SystemError> {
5376b4e7a29SLoGin         if unlikely(self.inode_type != KernInodeType::Dir) {
5386b4e7a29SLoGin             return Err(SystemError::ENOTDIR);
5396b4e7a29SLoGin         }
5406b4e7a29SLoGin 
5416b4e7a29SLoGin         let mut children = self.children.lock();
5426b4e7a29SLoGin         let inode = children.get(name).ok_or(SystemError::ENOENT)?;
5436b4e7a29SLoGin         if inode.children.lock().is_empty() {
5446b4e7a29SLoGin             children.remove(name);
5456b4e7a29SLoGin             return Ok(());
5466b4e7a29SLoGin         } else {
5476b4e7a29SLoGin             return Err(SystemError::ENOTEMPTY);
5486b4e7a29SLoGin         }
5496b4e7a29SLoGin     }
5506b4e7a29SLoGin 
551*a03c4f9dSLoGin     /// add_link - create a symlink in kernfs
552*a03c4f9dSLoGin     ///
553*a03c4f9dSLoGin     /// ## 参数
554*a03c4f9dSLoGin     ///
555*a03c4f9dSLoGin     /// - `parent`: directory to create the symlink in
556*a03c4f9dSLoGin     /// - `name`: name of the symlink
557*a03c4f9dSLoGin     /// - `target`: target node for the symlink to point to
558*a03c4f9dSLoGin     ///
559*a03c4f9dSLoGin     /// Returns the created node on success
560*a03c4f9dSLoGin     ///
561*a03c4f9dSLoGin     /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/fs/kernfs/symlink.c#25
562*a03c4f9dSLoGin     pub fn add_link(
563*a03c4f9dSLoGin         &self,
56406d5e247SLoGin         name: String,
565*a03c4f9dSLoGin         target: &Arc<KernFSInode>,
566*a03c4f9dSLoGin         target_absolute_path: String,
567*a03c4f9dSLoGin     ) -> Result<Arc<KernFSInode>, SystemError> {
568*a03c4f9dSLoGin         // kdebug!("kernfs add link: name:{name}, target path={target_absolute_path}");
569*a03c4f9dSLoGin         let inode = self.inner_create(
57006d5e247SLoGin             name,
571*a03c4f9dSLoGin             KernInodeType::SymLink,
572*a03c4f9dSLoGin             ModeType::S_IFLNK | ModeType::from_bits_truncate(0o777),
573*a03c4f9dSLoGin             None,
574*a03c4f9dSLoGin             None,
575*a03c4f9dSLoGin         )?;
5766b4e7a29SLoGin 
577*a03c4f9dSLoGin         inode.inner.write().symlink_target = Some(Arc::downgrade(target));
578*a03c4f9dSLoGin         inode.inner.write().symlink_target_absolute_path = Some(target_absolute_path);
579*a03c4f9dSLoGin         return Ok(inode);
5806b4e7a29SLoGin     }
58106d5e247SLoGin 
58206d5e247SLoGin     pub fn name(&self) -> &str {
58306d5e247SLoGin         return &self.name;
58406d5e247SLoGin     }
58506d5e247SLoGin 
58606d5e247SLoGin     pub fn parent(&self) -> Option<Arc<KernFSInode>> {
587*a03c4f9dSLoGin         return self.inner.read().parent.upgrade();
58806d5e247SLoGin     }
58906d5e247SLoGin 
59006d5e247SLoGin     pub fn private_data_mut(&self) -> SpinLockGuard<Option<KernInodePrivateData>> {
59106d5e247SLoGin         return self.private_data.lock();
59206d5e247SLoGin     }
59306d5e247SLoGin 
594*a03c4f9dSLoGin     #[allow(dead_code)]
595*a03c4f9dSLoGin     pub fn symlink_target(&self) -> Option<Arc<KernFSInode>> {
596*a03c4f9dSLoGin         return self.inner.read().symlink_target.as_ref()?.upgrade();
597*a03c4f9dSLoGin     }
598*a03c4f9dSLoGin 
59906d5e247SLoGin     /// remove a kernfs_node recursively
60006d5e247SLoGin     pub fn remove_recursive(&self) {
60106d5e247SLoGin         let mut children = self.children.lock().drain().collect::<Vec<_>>();
60206d5e247SLoGin         while let Some((_, child)) = children.pop() {
60306d5e247SLoGin             children.append(&mut child.children.lock().drain().collect::<Vec<_>>());
60406d5e247SLoGin         }
60506d5e247SLoGin     }
60606d5e247SLoGin 
60706d5e247SLoGin     /// 删除当前的inode(包括其自身、子目录和子文件)
60806d5e247SLoGin     #[allow(dead_code)]
60906d5e247SLoGin     pub fn remove_inode_include_self(&self) {
61006d5e247SLoGin         let parent = self.parent();
61106d5e247SLoGin         if let Some(parent) = parent {
61206d5e247SLoGin             parent.children.lock().remove(self.name());
61306d5e247SLoGin         }
61406d5e247SLoGin         self.remove_recursive();
61506d5e247SLoGin     }
6166b4e7a29SLoGin }
6176b4e7a29SLoGin #[derive(Debug, Clone, Copy, PartialEq, Eq)]
61806d5e247SLoGin pub enum KernInodeType {
6196b4e7a29SLoGin     Dir,
6206b4e7a29SLoGin     File,
621*a03c4f9dSLoGin     SymLink,
6226b4e7a29SLoGin }
6236b4e7a29SLoGin 
6246b4e7a29SLoGin impl Into<FileType> for KernInodeType {
6256b4e7a29SLoGin     fn into(self) -> FileType {
6266b4e7a29SLoGin         match self {
6276b4e7a29SLoGin             KernInodeType::Dir => FileType::Dir,
6286b4e7a29SLoGin             KernInodeType::File => FileType::File,
629*a03c4f9dSLoGin             KernInodeType::SymLink => FileType::SymLink,
6306b4e7a29SLoGin         }
6316b4e7a29SLoGin     }
6326b4e7a29SLoGin }
633