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