xref: /DragonOS/kernel/src/filesystem/devfs/zero_dev.rs (revision 7cc4a02c7ff7bafd798b185beb7b0c2986b9f32f)
1 use crate::filesystem::vfs::file::FileMode;
2 use crate::filesystem::vfs::make_rawdev;
3 use crate::filesystem::vfs::{
4     core::generate_inode_id, FilePrivateData, FileSystem, FileType, IndexNode, Metadata, PollStatus,
5 };
6 use crate::{libs::spinlock::SpinLock, syscall::SystemError, time::TimeSpec};
7 use alloc::{
8     string::String,
9     sync::{Arc, Weak},
10     vec::Vec,
11 };
12 // use uuid::{uuid, Uuid};
13 use super::{DevFS, DeviceINode};
14 
15 #[derive(Debug)]
16 pub struct ZeroInode {
17     /// uuid 暂时不知道有什么用(x
18     // uuid: Uuid,
19     /// 指向自身的弱引用
20     self_ref: Weak<LockedZeroInode>,
21     /// 指向inode所在的文件系统对象的指针
22     fs: Weak<DevFS>,
23     /// INode 元数据
24     metadata: Metadata,
25 }
26 
27 #[derive(Debug)]
28 pub struct LockedZeroInode(SpinLock<ZeroInode>);
29 
30 impl LockedZeroInode {
31     pub fn new() -> Arc<Self> {
32         let inode = ZeroInode {
33             // uuid: Uuid::new_v5(),
34             self_ref: Weak::default(),
35             fs: Weak::default(),
36             metadata: Metadata {
37                 dev_id: 1,
38                 inode_id: generate_inode_id(),
39                 size: 0,
40                 blk_size: 0,
41                 blocks: 0,
42                 atime: TimeSpec::default(),
43                 mtime: TimeSpec::default(),
44                 ctime: TimeSpec::default(),
45                 file_type: FileType::CharDevice, // 文件夹,block设备,char设备
46                 mode: 0o666,
47                 nlinks: 1,
48                 uid: 0,
49                 gid: 0,
50                 raw_dev: make_rawdev(1, 3), // 这里用来作为device number
51             },
52         };
53 
54         let result = Arc::new(LockedZeroInode(SpinLock::new(inode)));
55         result.0.lock().self_ref = Arc::downgrade(&result);
56 
57         return result;
58     }
59 }
60 
61 impl DeviceINode for LockedZeroInode {
62     fn set_fs(&self, fs: Weak<DevFS>) {
63         self.0.lock().fs = fs;
64     }
65 }
66 
67 impl IndexNode for LockedZeroInode {
68     fn as_any_ref(&self) -> &dyn core::any::Any {
69         self
70     }
71 
72     fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> {
73         Err(SystemError::EOPNOTSUPP_OR_ENOTSUP)
74     }
75 
76     fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> {
77         Err(SystemError::EOPNOTSUPP_OR_ENOTSUP)
78     }
79 
80     fn metadata(&self) -> Result<Metadata, SystemError> {
81         return Ok(self.0.lock().metadata.clone());
82     }
83 
84     fn fs(&self) -> Arc<dyn FileSystem> {
85         return self.0.lock().fs.upgrade().unwrap();
86     }
87 
88     fn list(&self) -> Result<Vec<String>, SystemError> {
89         Err(SystemError::EOPNOTSUPP_OR_ENOTSUP)
90     }
91 
92     fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> {
93         let mut inode = self.0.lock();
94         inode.metadata.atime = metadata.atime;
95         inode.metadata.mtime = metadata.mtime;
96         inode.metadata.ctime = metadata.ctime;
97         inode.metadata.mode = metadata.mode;
98         inode.metadata.uid = metadata.uid;
99         inode.metadata.gid = metadata.gid;
100 
101         return Ok(());
102     }
103 
104     fn poll(&self) -> Result<PollStatus, SystemError> {
105         return Ok(PollStatus::READ | PollStatus::WRITE);
106     }
107 
108     /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写
109     fn read_at(
110         &self,
111         _offset: usize,
112         len: usize,
113         buf: &mut [u8],
114         _data: &mut FilePrivateData,
115     ) -> Result<usize, SystemError> {
116         if buf.len() < len {
117             return Err(SystemError::EINVAL);
118         }
119 
120         for i in 0..len {
121             buf[i] = 0;
122         }
123 
124         return Ok(len);
125     }
126 
127     /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写
128     fn write_at(
129         &self,
130         _offset: usize,
131         len: usize,
132         buf: &[u8],
133         _data: &mut FilePrivateData,
134     ) -> Result<usize, SystemError> {
135         if buf.len() < len {
136             return Err(SystemError::EINVAL);
137         }
138 
139         Ok(len)
140     }
141 }
142