xref: /DragonOS/kernel/src/filesystem/devfs/zero_dev.rs (revision 78bf93f02f84bf5e024ddfb559f040e68ce39ccf)
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::ENOTSUP)
74     }
75 
76     fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> {
77         Err(SystemError::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::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 {
106             flags: PollStatus::READ_MASK | PollStatus::WRITE_MASK,
107         });
108     }
109 
110     /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写
111     fn read_at(
112         &self,
113         _offset: usize,
114         len: usize,
115         buf: &mut [u8],
116         _data: &mut FilePrivateData,
117     ) -> Result<usize, SystemError> {
118         if buf.len() < len {
119             return Err(SystemError::EINVAL);
120         }
121 
122         for i in 0..len {
123             buf[i] = 0;
124         }
125 
126         return Ok(len);
127     }
128 
129     /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写
130     fn write_at(
131         &self,
132         _offset: usize,
133         len: usize,
134         buf: &[u8],
135         _data: &mut FilePrivateData,
136     ) -> Result<usize, SystemError> {
137         if buf.len() < len {
138             return Err(SystemError::EINVAL);
139         }
140 
141         Ok(len)
142     }
143 }
144