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